ObjectScript Tutorial
Exercise 7: Third Lookup Class
|
|
Class ObjectScript.Lookup3 { /// delete chosen record (lock, start a txn, kill global nodes, commit txn, unlock) ClassMethod Delete(id as %Integer, record as %String) { // try to lock the record for 5 seconds lock +^PersonD(id):5 if '$test { write "...someone else is editing this person. Try again later." quit } // retrieve data set $listbuild(name, phone, intdob) = record set last = $piece(name, ",", 1), first = $piece(name, ",", 2) set chunk = (id\64000) + 1, position = (id#64000) + 1 // change all globals inside a transaction tstart kill ^PersonD(id) kill ^PersonI("Name", last, first, id) kill ^PersonI("Phone", phone) kill ^PersonI("DOB", intdob, id) set $bit(^PersonI("Bitmap-ID", chunk), position) = 0 tcommit write "...deleted" lock -^PersonD(id) } }
Class ObjectScript.Lookup3 { /// edit chosen record (lock, reprompt, compare, update globals, unlock) ClassMethod Edit(id as %Integer, record as %String) { // try to lock the record for 5 seconds lock +^PersonD(id):5 if '$test { write "...someone else is editing this person. Try again later." quit } // show current data and prompt for updates do ..Reprompt(record, .newanswers) // if changes were made, update the record if '$listsame(record, newanswers) {do ..Update(id, record, newanswers)} lock -^PersonD(id) } }
Class ObjectScript.Lookup3 { /// prompt for updates - similar to ##class(ObjectScript.DataEntry4).Prompt() ClassMethod Reprompt(currentdata as %String, ByRef newanswers as %String) { // get current name, phone, intdob so that they can be displayed within prompts set $listbuild(currentname, currentphone, currentintdob) = currentdata do { write !, "Name: ", currentname, " => " read newname // enter nothing to keep current value if (newname = "") { set newname = currentname quit } } while '##class(ObjectScript.DataEntry4).ValidName(newname) do { write !, "Phone: ", currentphone, " => " read "(617): ", newphone // enter nothing to keep current value if (newphone = "") { set newphone = currentphone quit } } while '##class(ObjectScript.DataEntry4).ValidPhone(.newphone) do { write !, "DOB: ", $zdate(currentintdob, 2), "=> " read newdob // enter nothing to keep current value if (newdob = "") { set newintdob = currentintdob quit } } while '##class(ObjectScript.DataEntry4).ValidDOB(newdob, .newintdob) set newanswers = $listbuild(newname, newphone, newintdob) } }
Class ObjectScript.Lookup3 { /// save the updated record (start a txn, updating data and index globals using set and kill, commit txn) ClassMethod Update(id as %Integer, currentdata as %String, ByRef newanswers as %String) { read !, "Store updates? (y/n): ", yn // ask if user wants to store // only go on if user says yes if ((yn '= "y") && (yn '= "Y")) { write "...not stored." quit } // get current and new data for comparisons set $listbuild(currentname, currentphone, currentintdob) = currentdata set currentlast = $piece(currentname, ",", 1), currentfirst = $piece(currentname, ",", 2) set $listbuild(newname, newphone, newintdob) = newanswers set newlast = $piece(newname, ",", 1), newfirst = $piece(newname, ",", 2) // update all globals inside a transaction // only update indices if the data was changed tstart set ^PersonD(id) = newanswers if (newname '= currentname) { // kill old name and add new name to index kill ^PersonI("Name", currentlast, currentfirst, id) set ^PersonI("Name", newlast, newfirst, id) = "" } if (newphone '= currentphone) { // kill old phone and add new phone to index kill ^PersonI("Phone", currentphone) set ^PersonI("Phone", newphone) = id } if (newintdob '= currentintdob) { // kill old dob and add new dob to index kill ^PersonI("DOB", currentintdob, id) set ^PersonI("DOB", newintdob, id) = "" } tcommit // commit the transaction write "...updated." } }