docs.intersystems.com
Home  /  Application Development: Core Topics  /  ObjectScript Tutorial  /  Exercise 5: First Lookup Class


ObjectScript Tutorial
Exercise 5: First Lookup Class
[Back]
InterSystems: The power behind what matters   
Search:  


Click here to return to the exercise description in the main part of the tutorial.
  1. Create a new class. Use ObjectScript for the package name and Lookup1 for the class name. You'll see this code:
    Class ObjectScript.Lookup1
    {
    
    }
    
  2. Within the {}, write the Main() method.
    Class ObjectScript.Lookup1
    {
    
    /// main loop section, dispatch to different methods based on user input
    ClassMethod Main()
    {
        while ..GetInput(.type, .search) {  
            if (type = "help") { do ..Help() }
            elseif (type = "dob") { do ..DOB(search) }
        }
        
    }
    }
  3. Next, write the GetInput() method.
    Class ObjectScript.Lookup1
    {
    
    /// prompt user for a lookup string, return search type and search string
    ClassMethod GetInput(output type as %String, output search as %String) as %Boolean
    {
        read !, "Lookup: ", lookup
        return:(lookup = "") 0  // user entered nothing so return FALSE
        if (lookup = "?") {
            set type = "help", search = ""
        }
        elseif (##class(ObjectScript.DataEntry4).ValidDOB(lookup, .convdate)) {
            set type = "dob", search = convdate
        }
        else {
            set (type, search) = ""
        }
        return 1
    }
    }
  4. Next, write the Help() method.
    Class ObjectScript.Lookup1
    {
    
    /// display lookup options
    ClassMethod Help()
    {
        write !, "You can enter:",
              !?10, "* date of birth", !
    }
    }
  5. Next, write the DOB() method, which takes the internal DOB as an argument. It starts by using $Data on ^PersonI("DOB", intdob) to see if the internal date of birth exists in the global; notice the single quote (negation) before $Data. If there are matches, the $Order loop finds all the IDs for that date of birth, starting at the beginning (set id = ""), and continuing until the end (quit:(id = "")). It also numbers the matches using For.
    Class ObjectScript.Lookup1
    {
    
    /// exact date of birth lookup
    ClassMethod DOB(intdob as %Date)
    {
        // is the date of birth in the index?
        if '$data(^PersonI("DOB", intdob) ) {
            write "...no matches"
            quit
        }
        
        write "...finding birthday matches"
        // loop through IDs, and number them
        set id = ""
        for count = 1:1 {
            set id = $order(^PersonI("DOB", intdob, id))
            quit:(id = "")
            write !, count, ") "
            do ..DisplayLine(id)
            }
    }
    }
  6. Last, write the DisplayLine() method, which is similar to Display() from ObjectScript.DataEntry4.
    Class ObjectScript.Lookup1
    {
    
    //// given an ID, retrieve data and write it on a line
    ClassMethod DisplayLine(id as %Integer)
    {
        set $listbuild(name, phone, intdob) = ^PersonD(id)
        /* the line above is equivalent to
           set answers = ^PersonD(id),
               name = $list(answers, 1),
               phone = $list(answers, 2),
               intdob = $list(answers, 3) */
        write name, ?20, phone, ?35, $zdate(intdob, 2)    
    }
    }
  7. Click the Save and Compile buttons.
  8. Start Terminal, and run your method, by typing do ##class(ObjectScript.Lookup1).Main().