#Code Snippet

6 Followers · 302 Posts

Code snippet is a programming term for a small region of re-usable source code, machine code, or text.

Article Gevorg Arutiunian · Jan 11, 2019 2m read

This code snippet sends an XML request to a server and saves the response to a file. The class method "test" runs the code:


Class objectscript.postXML
{
	classmethod test() {
		Set HTTPRequest = ##class(%Net.HttpRequest).%New()
	    Set HTTPRequest.ContentType = "text/xml"
	    Set HTTPRequest.NoDefaultContentCharset = 1
	    Set HTTPRequest.Location = "ITOMCZ"
	    Set HTTPRequest.Server = "wph.foactive.com"
	    Do HTTPRequest.RemoveHeader("User-Agent")  
	    Do HTTPRequest.RemoveHeader("Accept-Encoding") 
	    Do HTTPRequest.RemoveHeader("Connection")
	    Do HTTPRequest.SetHeader("Expect","100-continue")
    Set RequestXML = ##class(%Library.File).%New("c:\test.xml")
    Do RequestXML.Open("RS")
    Do HTTPRequest.EntityBody.CopyFrom(RequestXML)
    Do RequestXML.%Close()
 
    Do HTTPRequest.Post(HTTPRequest.Location)
 
    Do $System.OBJ.Dump(HTTPRequest)
    Do $System.OBJ.Dump(HTTPRequest.HttpResponse)
 
    Write HTTPRequest.HttpResponse.Data.Size
    Write HTTPRequest.ContentLength
 
    Set ResponseStream = ##class(%Stream.FileBinary).%New()
    // Second part is typically the file extension, i.e.: application/pdf -> pdf
    Set FileType = $Piece(HTTPRequest.HttpResponse.GetHeader("CONTENT-TYPE"),"/",2)
    Set ResponseStream.Filename = "C:\test."_FileType
 
    Write ResponseStream.CopyFrom(HTTPRequest.HttpResponse.Data)
 
    Write ResponseStream.%Save()
    Do ResponseStream.%Close()
}

}

Here's a link to the code on GitHub

0
0 972
Article Gevorg Arutiunian · Dec 27, 2018 2m read

The following code walks a DOM using %XML.Node. It also prevents %XML.Writer to change whitespace. Run the code using the class method "test":


Class objectscript.walkDOM Extends %Persistent
{
	ClassMethod dfs(node As %XML.Node)
	{
	    s entrynode=node.NodeId
	    do {
	    //element nodes with one whitespacetyped child are the ones we want to change
	    if (node.NodeType=$$$xmlELEMENTNODE){
	        s snode=node.NodeId     
	        if (node.MoveToFirstChild())            
	            {
	                i ('node.MoveToNextSibling()){
	                    i (node.NodeType=$$$xmlWHITESPACENODE){
	                        s node.NodeType=$$$xmlTEXTNODE
	                        s node.NodeId=snode
	                    }
	                }
	        }
	        s node.NodeId=snode     
	    }   
	    if (node.HasChildNodes()){
	        d node.MoveToFirstChild()
	        d ..dfs(node)
	    }
	    } while (node.NodeType'="" && node.MoveToNextSibling())
	    s node.NodeId=entrynode
	     
	}
	 
	ClassMethod test()
	{
	  set xml = "abcdefg<![CDATA[   ]]>"
	 
	  s reader=##class(%XML.Reader).%New()
	  do reader.OpenString(xml)  
	  set writer = ##class(%XML.Writer).%New()
	  //do some magic
	  d ..dfs(reader.Document)
	   
	  w !,"with indent=1:",!
	  set writer.Indent = 1
	  do writer.OutputToString()
	  do writer.Document(reader.Document)
	  w writer.GetXMLString()
	  set writer.Indent = 0
	  w !,"with indent=0:",!
	  do writer.OutputToString()
	  do writer.Document(reader.Document)
	  w writer.GetXMLString()
	}
}

Here's a link to the code on GitHub: https://github.com/intersystems-community/code-snippets/blob/master/src/cls/objectscript/walkDOM.cls

0
0 418
Question David Foard · Dec 3, 2018

I am trying to use %INLIST in  SQL query using a cursor and the query fails to return results. It appears that the variable I use against %INLIST returns an empty string. All the examples I have seen use result sets and build the query as a string. Is it possible to use %INLIST in a cursor-based query? Below is a a snippet of the code I am using:

Property mylist as %String (MAXLEN="") [InitialExpression = "route1, route2, route3"];

set routeList = $LISTBUILD(mylist)

&sql(DECLARE MyReport CURSOR FOR

SELECT

      ProcessStartDate,

      ProcessCompleteDate,

      ProcessName,

     RouteName

FROM

3
0 752
Article Wilber Tang · Jul 25, 2016 1m read

EnsLib.HL7.Message.cls provides many API methods for manipulating an HL7 message.  RemoveSegmentAt(), for example, can be used to remove a segment by path or index, but only one segment at a time.   There may be times that you'll need to remove all segments within a group or even many groups of segments from the HL7 message.  Surely you can iterate through each segment in each group and remove them one by one, but there's a much easier way. 

With just one command, like below, you can remove all OBX segments in an ORU_R01 message (msg):

1
0 2622
Article Gevorg Arutiunian · Aug 22, 2018 1m read

(Originally posted on Intersystems CODE by @Eduard Lebedyuk, 10/12/15) The following code snippet outputs all filenames in the file path "dir" in the Cache/IRIS terminal. The class method "test" runs the code:


Class eduardlebedyuk.filenamesInDir Extends %RegisteredObject
{
	classmethod test() {
		// replace dir with file path you want
		set dir = "D:\directory" 
		set dir = ##class(%File).NormalizeDirectory(dir)
		set file=$ZSEARCH(dir_"*")
		while file'="" {
			write !,file
			set file=$ZSEARCH("")
		}
	}
}
3
2 2075
Article Gevorg Arutiunian · Oct 26, 2018 1m read

This code snippet contains the class method "test" which sends an HTML email. Change the literal strings in the method to customize the email's from address, to address, subject, and body:


Class objectscript.sendEmail Extends %RegisteredObject
{
	classmethod test() {
		set m=##class(%Net.MailMessage).%New()
		set m.From="user@company.com"
		 
		set m.IsHTML=1
		 
		do m.To.Insert("user@company.com")
		set m.Subject="Sent by IRIS mail"
		set m.Charset="iso-8859-1"
		do m.TextData.Write("<HTML><HEAD><TITLE></TITLE>"_$char(13,10))
		do m.TextData.Write("<META http-equiv=Content-Type content=""text/html; charset=iso-8859-2""></HEAD>"_$char(13,10))
		do m.TextData.Write("<BODY><FONT face=Arial size=2>Test <B>Test</B></FONT></BODY></HTML>")
		set s=##class(%Net.SMTP).%New()
		set s.smtpserver="mail.company.com"
		set status=s.Send(m)
	}
}

Here's a link to the code on GitHub

0
1 1280
Article Gevorg Arutiunian · Oct 18, 2018 1m read

This code snippet uses %ZEN.Auxiliary.jsonSQLProvider. The namespace and string of SQL can be edited for different situations. The class method "test" runs the code:


Class eduardlebedyuk.passQuestionParams
{
	classmethod test(pValue = 50) {
		s ns = $Namespace
	    zn "samples"
	    s tSQL = "SELECT ID, Name FROM Sample.Person WHERE Id > ?"
	    s tPR = ##class(%ZEN.Auxiliary.jsonSQLProvider).%New()
	    s tPR.sql = tSQL
	    s tPR.%Format = "tw"
	    s tPR.maxRows = 100
    s tParam = ##class(%ZEN.Auxiliary.parameter).%New()
    s tParam.value = pValue
    d tPR.parameters.SetAt(tParam,1)
  
    d tPR.%DrawJSON() 
    //d ##class(%ZEN.Auxiliary.jsonSQLProvider).%WriteJSONFromSQL(,,,,,tPR)  //same thing
    zn ns
}

}

(Originally posted to Intersystems CODE by @Eduard Lebedyuk, 5/13/15)

Here's a link to the code on GitHub

0
0 347
Article Alexey Maslov · Nov 23, 2017 12m read

It's well-known that namespace global mapping helps us to write code independent on database storage details (Caché instance name, directory path). But sometimes we can face problems accessing an unsubscripted global which has subscript level mapping (SLM) defined. Most of such cases are evident and associated with administrative tasks that should be done on database level, but some of them can confuse even an experienced developer. Just to start:

  • We can't export such a global using Caché Block format (a.k.a. GOF).
  • We can't kill such a global.

Maybe you can continue this list.

8
0 1494
Article Gevorg Arutiunian · Oct 8, 2018 2m read

This code snippet provides a ZEN page that downloads a stream from its database directly:


/// We assume that you have stored your data within this schema:
/// MyApp.Model.Storage: Filename,FileSize,Content,ContentType
Class zen.downloadStream Extends (%ZEN.Component.page,%CSP.StreamServer)
{
/// Wrapper to get the id of the download, we assume that the id is passed to this zen page
/// as a URI parameter, i.e.: MyApp.Downloads.cls?OID=1234
ClassMethod GetId()
{
    Quit $Get(%request.Data("OID",1))
}
 
/// Set the appropriate header for the file.
ClassMethod OnPreHTTP() As %Boolean
{
    Set tId = ..GetId()
 
    If ##Class(MyApp.Model.Storage).%ExistsId(tId) {
        Set tStream = ##Class(MyApp.Model.Storage).%OpenId(tId)
        // You could "guess" the content type by its file extension
        // or you can store it (before) in the database separately (like in this example).
        // Set Extension = $Piece(tStream.Filename,".",$Length(tStream.Filename,"."))
        // Set ContentType = ..FileClassify(Extension)
 
        Set %response.ContentType = tStream.ContentType
        Do %response.SetHeader("content-disposition","attachment; filename="_tStream.Filename)
        Do %response.SetHeader("Content-Length",tStream.FileSize)
    }
    Else {
        Set %response.Status="404 File Not Found"
        Quit 0
    }
    Quit $$$OK
}
 
ClassMethod OnPage() As %Status
{
    Set Download = ##Class(MyApp.Model.Storage).%OpenId(..GetId())
    Do Download.Content.OutputToDevice()
    Quit $$$OK
}

}

Link to code on GitHub

1
2 671
Question Rosti Zacharias · Oct 4, 2018

Hi,

It has been pointed out to me that one of my applications fails to display all of the contents when displayed on an iPhone or a tablet of smaller res that a desktop screen.

Desktop..

iPhone..

The code does not change between the two views of the same page. 

To maintain easy alignment and control, I have used standard <tr> and <td> <table> codes, expecting same behaviour in any browser on any platform. My guess is using <div> controls for display will make a difference, but I'd be interested if anyone has any experience of this kind and if there is a simple fix.

1
0 388
Article Gevorg Arutiunian · Sep 27, 2018 2m read

The following class method "test" contains code that can create a new class, create new properties for classes, or create new methods for classes. "test" runs all three of these processes once, and the code that performs each action is labelled by comments in the method:


ClassMethod test() As %Status
{
	set sc = $$$OK
// Create a class
set class = ##class(%ClassDefinition).%New("MyClass")
set class.Description = "This is my test class"_$c(13,10)_"testing %ClassDefinition"
set class.Super = "%Persistent"

// Create a property and add it
set property = ##class(%PropertyDefinition).%New("MyClass.MyProperty")
set property.Type = "%String"
set property.Description="This is a property"
set sc1 = class.Properties.Insert(property)
do:$$$ISERR(sc1) $system.Status.DisplayError(sc1)
set sc = $$$ADDSC(sc, sc1)

// Create a method and add it
set method = ##class(%MethodDefinition).%New("MyClass.MyMethod")
set method.ReturnType = "%Integer"
set method.FormalSpec = "x:%Integer,y:%Integer=10"
set method.Description = "Return product of x and y"
set method.CodeMode = "code"
set method.Code = " new result"_$c(13,10)_" set result=x*y"_$c(13,10)_" quit result"
set sc2 = class.Methods.Insert(method)
do:$$$ISERR(sc2) $system.Status.DisplayError(sc2)
set sc = $$$ADDSC(sc, sc2)

// Save the class definition
set sc3 = class.%Save()
do:$$$ISERR(sc3) $system.Status.DisplayError(sc3)
set sc = $$$ADDSC(sc, sc3)

return sc

}

Here's a link to the code on GitHub

1
2 773
Article Gevorg Arutiunian · Sep 13, 2018 1m read

The following code snippet includes a class method "test" that runs code to find a class based on the class's name. "test" takes one argument, which is the name of the table:


Class objectscript.findTable Extends %RegisteredObject
{
	classmethod test(name as %String="mytable")  
    {
			#Dim result as %ResultSet
			#Dim tName as %String
			#Dim contain as %Integer
		Set contain=0
		Set result = ##class(%ResultSet).%New("%Dictionary.ClassDefinition:Summary")
		Do result.Execute()

		While(result.Next()) 
		{
			Set tName=$get(result.Data("Name"))
			&sql(select position (:name in :tName) into :contain)
			Write:contain'=0 tName, " ... ", name, " (", contain,")", !
		}
	    Return $$$OK
 }

}

Here's a link to the code on GitHub

6
0 841
Article Gevorg Arutiunian · Sep 20, 2018 2m read

This code snippet determines the day of the week associated with a date. The class method "test" takes a date as a string in "mm/dd/yyyy" format, and returns an integer corresponding to a day of the week:


Class cartertiernan.getDayfromDate Extends %RegisteredObject
{
	classmethod test(date) as %Integer {
		//Set date = $ZDATE(date) //  Looks like: mm/dd/yyyy
    Set monthList = $LISTBUILD(0,3,3,6,1,4,6,2,5,0,3,5) // (Jan,Feb,Mar,Apr,...)
    Set centuryList = $LISTBUILD(6,4,2,0) // first two digits divisiable by 4, then subsequent centuries. EX (2000, 2100, 2200, 2300)
    Set dayList = $LISTBUILD("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday") // Index goes from 0-6
     
    Set day = $PIECE(date,"/",2) // get the day 
    Set monthVal = $LIST(monthList,($PIECE( date,"/",1 ))) // get the month value
    Set first2DigsYear = $PIECE( date,"/",3 ) \ 100 // get the last 2 digits of the year
    Set last2DigsYear = $PIECE( date,"/",3 ) # 100 // get the first 2 digits of the year
     
    // Used for DEBUG perpouses
    /*write !,"day: ",day
    write !,"Month: ",monthVal
    write !,"last2: ",last2DigsYear
    write !,"first2: ",first2DigsYear
    write !,"cen Val: ",$LIST(centuryList,(first2DigsYear # 4) + 1),!!*/
     
    // Look here for formula explination (its the "Basic method for mental calculation")
    // http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
    Set dayOfWeekVal = ( day + monthVal + last2DigsYear + (last2DigsYear\4) + $LIST(centuryList,(first2DigsYear # 4) + 1 ) ) # 7
 
    Quit dayOfWeekVal
}

}

Here's a link to the code on GitHub

(originally posted to CODE by Carter Tiernan, 6/18/14)

1
1 1165
Question Scott Roth · Sep 13, 2018

We have the need to write a function that can loop through say a field in an OBX segment within HL7 and compare it to a string passed. Is it possible to have the user enter the Operator ( >,<,=,<>) as a variable inside Cache object script? Does anyone have any examples they can share?

Thanks

Scott Roth

The Ohio State University Wexner Medical Center

8
0 647
Article Gevorg Arutiunian · Sep 6, 2018 1m read

This code snippet allows for a file on the web to be saved into the file system. Specify the server and GET request, as well as the directory the file should be saved to. The class method "test" runs the code:


Class objectscript.saveFileHTTP Extends %RegisteredObject
{
	classmethod test() {
		Set httprequest = ##class(%Net.HttpRequest).%New()
		Set httprequest.Server = "docs.intersystems.com"
		Do httprequest.Get("documentation/cache/20172/pdfs/GJSON.pdf")
    Do $System.OBJ.Dump(httprequest.HttpResponse)
	 
	Set stream=##class(%FileBinaryStream).%New()
	Set stream.Filename="c:\test.pdf"

	Write stream.CopyFrom(httprequest.HttpResponse.Data)
	Write stream.%Save()
}

}

Here's a link to the code on GitHub

0
1 1071
Question Jack Huser · Aug 22, 2018

Hello Everyone,

I was wondering about the best way to initialize several variables, or several lists of variables.

Would it be better to write it like:

set (var1, var2, var3) = "value1"
set (var4, var5) = "value2"
set (var6,var7,var8) = "value3"

or

set var1="value1", var2="value1", var3="value1", var4="value2", var5="value2", var6="value3", var7="value3", var8="value3"

or

set var1 = "value1"
set var2 = "value1"
set var3 = "value1"
set var4 = "value2"
set var5 = "value2"
set var6 = "value3"
set var7 = "value3"
set var8 = "value3"

When comparing the speed of each methods:

6
0 613
Discussion Evgeny Shvarov · Apr 22, 2018

Hi, Community!

Have a question for general discussion. 

In ObjectScript we have cls for classes and mac code, which both compile into int code. 

Is there any reason when you use mac instead of cls  for non-persistent classes?

For me the benefits for cls are:

1. Inheritance and other OOP features

2. Auto-documented code 

For mac one visible benefit is easier call in terminal:

do method^Utils(p1,p2)

vs

do ##class(Package.Utils).method(p1,p2)

What is your choice and why?

43
1 3053