forked from intersystems/Samples-ObjectScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLookup3.cls
More file actions
331 lines (306 loc) · 10.1 KB
/
Copy pathLookup3.cls
File metadata and controls
331 lines (306 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
Class ObjectScript.Lookup3
{
/// main loop section, dispatch to different methods based on user input
ClassMethod Main()
{
do ..CurrentCount()
while ..GetInput(.type, .search) {
if (type = "help") { do ..Help() set id = "" }
elseif (type = "phone") { do ..Phone(search, .id) }
elseif (type = "name") { do ..Name(search, .id) }
elseif (type = "dob") { do ..DOB(search, .id) }
if ((type '= "") && (id '= "")) { do ..TakeAction(id) }
}
}
/// 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 = ""
}
// the RegEx accepts ###- or ###-###-#### only
elseif $match(lookup, "\d{3}-(\d{3}-\d{4})?") {
set type = "phone", search = lookup
}
/* the $zconvert converts the last name and first name entered to Last,First format
the pattern match accepts Lastname only, or Lastname,Firstname */
elseif ($zconvert(lookup, "W")?1U.L.1(1","1U.L)) {
set type = "name", search = $zconvert(lookup, "W")
}
elseif (##class(ObjectScript.DataEntry4).ValidDOB(lookup, .convdate)) {
set type = "dob", search = convdate
}
else {
// this is a hack for invalid input
// ValidDOB() writes an error message, and the text below gets added to that
write ", name, or phone"
set (type, search) = ""
}
return 1
}
/// display lookup options
ClassMethod Help()
{
write !, "You can enter:",
!?10, "* date of birth",
!?10, "* full phone number or area code only ""617-""",
!?10, "* full name: Smith,John",
!?10, "* last name: Smith",
!?10, "* partial name: Sm,J or Smith,J or Sm,John", !
}
/// exact date of birth lookup
ClassMethod DOB(intdob As %Date, Output id As %Integer)
{
set id = ""
// 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 = "")
set matches(count) = id // keep track of matches
write !, count, ") "
do ..DisplayLine(id)
}
do ..Select(.matches, .id)
}
/// lookup phone or area code
ClassMethod Phone(phone As %String, Output id As %Integer)
{
set id = ""
set count = 0
// handle exact match first
set id = $get(^PersonI("Phone", phone))
if (id '= "") {
set count = 1
set matches(1) = id // keep track of exact match
write !, "1) "
do ..DisplayLine(id)
quit
}
// handle area code matches next
elseif (phone?3n1"-") {
// use 3-argument $order to get first matching phone number and its ID number
set ph = $order(^PersonI("Phone", phone), 1, id)
// loop through matching phones, and number them
while ($extract(ph, 1, $length(phone)) = phone) {
write:(count = 0) "...finding area code matches"
set count = count + 1
set matches(count) = id // keep track of matches
write !, count, ") "
do ..DisplayLine(id)
// use 3-arg $order to get the next phone number and its ID number
set ph = $order(^PersonI("Phone", ph), 1, id)
}
}
if (count = 0) { write "...no matches" }
else { do ..Select(.matches, .id) }
}
/// lookup names in these forms: Smith; Smith,John; Smith,J; Sm,John; Sm,J
ClassMethod Name(name As %String, Output id As %Integer)
{
set id = ""
set count = 0
set last = $piece(name, ",", 1), first = $piece(name, ",", 2)
// last may be an exact match, so find preceding last name
set ln = $order(^PersonI("Name", last), -1)
// loop through last names
for {
set ln = $order(^PersonI("Name", ln))
// quit as soon as last name doesn't match original
quit:($extract(ln, 1, $length(last)) '= last)
// first may be "". Otherwise, it may be an exact match, so find preceding first name
if (first = "") { set fn = "" }
else { set fn = $order(^PersonI("Name", ln, first), -1) }
// loop through first names
for {
set fn = $order(^PersonI("Name", ln, fn))
// quit as soon as first name doesn't match original, or is ""
quit:(($extract(fn, 1, $length(first)) '= first) || (fn = ""))
set id = ""
// loop through all IDs
for {
set id = $order(^PersonI("Name", ln, fn, id))
quit:(id = "")
write:(count = 0) "...finding name matches"
set count = count + 1
set matches(count) = id // keep track of matches
write !, count, ") "
do ..DisplayLine(id)
}
}
}
if (count = 0) { write "...no matches" }
else { do ..Select(.matches, .id) }
}
/// 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)
}
/// count the "1" bits from the chunks of the Bitmap-ID index
ClassMethod CurrentCount()
{
set records = 0, chunk = ""
for {
// use the 3-argument $order to get the next chunk and the bits stored there
set chunk = $order(^PersonI("Bitmap-ID", chunk), 1, bits)
quit:(chunk = "")
// add the "1" bits to the count
set records = records + $bitcount(bits, 1)
}
write !, "There are ", records, " records in the database."
}
/// user makes a choice from the matches array, return the corresponding ID or ""
ClassMethod Select(ByRef matches As %Integer, Output id As %Integer)
{
set id = ""
for {
read !!, "Choose by number: ", choice
quit:(choice = "")
set id = $get(matches(choice))
quit:(id '= "") // stop looping if user makes a valid choice
write "...Invalid choice"
}
}
/// display chosen record, and optionally delete/edit/write to file
ClassMethod TakeAction(id As %Integer)
{
set record = ^PersonD(id)
do ##class(ObjectScript.DataEntry4).Display(record)
// ask if user wants to delete
read !, "Delete? (y/n): ", yn
if ((yn = "y") || (yn = "Y")) {
do ..Delete(id, record)
quit
}
// ask if user wants to edit
read !, "Edit? (y/n): ", yn
if ((yn = "y") || (yn = "Y")) {
do ..Edit(id, record)
quit
}
}
/// 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)
}
/// 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)
}
/// 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)
}
/// 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."
}
}