forked from jakehowlett/codestore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWrapperClasses.lss
604 lines (489 loc) · 13.1 KB
/
WrapperClasses.lss
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
%REM
EXAMPLE OF USE OF WRAPPER CLASSES
%END REM
Sub Initialize
Dim factory As New InvoiceFactory
Dim invoices As InvoiceCollection
Dim invoice As Invoice
Set invoices = factory.GetAllInvoices()
If invoices.Count > 0 Then
Set invoice = invoices.getFirst()
While Not invoice Is Nothing
Print "<h1>" + invoice.Title + "</h1><p>" + invoice.ValueFormattedAsString + "</p>"
Print "<h4>As JSON</h4>"
Print "<p><code>" + invoice.AsJSON + "</code></p>"
Print "<h3>Customer</h3>"
Print "<p>Customer's name is " + invoice.Customer.FullNameReversed + " ("+ invoice.CustomerID + "), who has " + CStr(invoice.Customer.Invoices.Count) + " invoices in total.</p>"
Print "<h4>As JSON</h4>"
Print "<p><code>" + invoice.Customer.AsJSON + "</code></p>"
Print "<hr>"
Set invoice = invoices.getNext()
Wend
Else
Print "NO INVOICES ("+CStr(invoices.count)")"
End If
End Sub
%REM
CLASSES REFERENCED BY THE DEMO CODE ABOVE
%END REM
%REM
Class InvoiceFactory
Description: Comments for Class
%END REM
Class InvoiceFactory As DocumentFactory
Sub New(), DocumentFactory()
End Sub
%REM
Function GetAllInvoiecs
Description: Comments for Function
%END REM
Function GetAllInvoices() As InvoiceCollection
Set view_ = database.GetView("InvoicesByTitle")
Set GetAllInvoices = New InvoiceCollection(view_.Allentries)
End Function
%REM
Function GetInvoicesByTitle
Description: Comments for Function
%END REM
Function GetInvoicesByTitle(title As String) As InvoiceCollection
Set view_ = database.GetView("Invoices")
Set GetInvoicesByTitle = New InvoiceCollection(view_.Getalldocumentsbykey(title, False))
End Function
%REM
Function GetInvoicesByCustomer
Description: Comments for Function
%END REM
Function GetInvoicesByCustomer(id As String) As InvoiceCollection
Set view_ = database.GetView("InvoicesByCustomerID")
Set GetInvoicesByCustomer = New InvoiceCollection(view_.GetAllDocumentsByKey(id, True))
End Function
End Class
%REM
Class InvoiceCollection
Description: Comments for Class
%END REM
Class InvoiceCollection As DocumentCollection
Sub New(docs As Variant), DocumentCollection(docs)
End Sub
Function getFirst() As DocumentWrapper
If getFirstDoc() Is Nothing Then
Set getFirst = Nothing
Else
Set getFirst = New Invoice(CurrentDocument)
End If
End Function
Function getNext() As DocumentWrapper
If getNextDoc() Is Nothing Then
Set getNext = Nothing
Else
Set getNext = New Invoice(CurrentDocument)
End If
End Function
End Class
%REM
Class Invoice
Description: Comments for Class
%END REM
Class Invoice As DocumentWrapper
Private Customer_ As Customer
Sub New(doc As NotesDocument), DocumentWrapper(doc)
End Sub
%REM
Property Get Title
Description: Comments for Property Get
%END REM
Property Get Title As String
Title = GetFieldValue("Title")
End Property
%REM
Property Get Value
Description: Comments for Property Get
%END REM
Property Get Value As Currency
Value = GetFieldValue("Value")
End Property
%REM
Property Get Customer
Description: Comments for Property Get
%END REM
Property Get Customer As Customer
If Customer_ Is Nothing Then
Dim fact As New CustomerFactory
Set Customer = fact.GetCustomerByID(CustomerID)
Else
Set Customer = Customer_
End If
End Property
%REM
Property Get CustomerID
Description: Comments for Property Get
%END REM
Property Get CustomerID As String
CustomerID = GetFieldValue("CustomerID")
End Property
%REM
Property Get ValueFormattedAsString
Description: Comments for Property Get
%END REM
Property Get ValueFormattedAsString As String
ValueFormattedAsString = Format(Value, "#,###,##0.00")
End Property
%REM
Property Get AsJSON
Description: Comments for Property Get
%END REM
Property Get AsJSON As String
AsJSON = |{| +_
DocumentDetailAsJSON +|, |+_
| "Title": "| + Sanitize(Title) + |",|+_
| "Value": {"Raw": |+CStr(Value) + |,"Formatted": "|+ValueFormattedAsString+|"}|+_
|}|
End Property
%REM
Sub Approve
Description: Comments for Sub
%END REM
Sub Approve()
'
End Sub
End Class
%REM
Class Customer
Description: Comments for Class
%END REM
Class Customer As DocumentWrapper
Sub New(doc As NotesDocument), DocumentWrapper(doc)
End Sub
%REM
Property Get FullName
Description: Comments for Property Get
%END REM
Property Get FullName As String
FullName = FirstName + " " + LastName
End Property
%REM
Property Get FullNameReversed
Description: Comments for Property Get
%END REM
Property Get FullNameReversed As String
FullNameReversed = LastName + ", " + FirstName
End Property
Property Get FirstName As String
FirstName = GetFieldValue("FirstName")
End Property
Property Get LastName As String
LastName = GetFieldValue("LastName")
End Property
%REM
Property Get Invoices
Description: Comments for Property Get
%END REM
Property Get Invoices As InvoiceCollection
Dim factory As New InvoiceFactory
Set Invoices = factory.GetInvoicesByCustomer(CustomerID)
End Property
%REM
Property Get CustomerID
Description: Comments for Property Get
%END REM
Property Get CustomerID As String
CustomerID = GetFieldValue("CustomerID")
End Property
%REM
Property Get AsJSON
Description: Comments for Property Get
%END REM
Property Get AsJSON As String
AsJSON = |{| +_
DocumentDetailAsJSON +|, |+_
| "ID": "| + CustomerID + |",|+_
| "Name": {"First": "|+Sanitize(FirstName)+|", "Last": "|+Sanitize(LastName)+|"}|+_
|}|
End Property
End Class
%REM
Class CustomerCollection
Description: Comments for Class
%END REM
Class CustomerCollection As DocumentCollection
Sub New(docs As Variant), DocumentCollection(docs)
End Sub
Function getFirst() As DocumentWrapper
If getFirstDoc() Is Nothing Then
Set getFirst = Nothing
Else
Set getFirst = New Customer(CurrentDocument)
End If
End Function
Function getNext() As DocumentWrapper
If getNextDoc() Is Nothing Then
Set getNext = Nothing
Else
Set getNext = New Customer(CurrentDocument)
End If
End Function
End Class
Class CustomerFactory As DocumentFactory
Sub New(), DocumentFactory()
End Sub
%REM
Function GetAllCustomers
Description: Comments for Function
%END REM
Function GetAllCustomers() As CustomerCollection
Set view_ = database.GetView("AllCustomers")
Set GetAllCustomers = New CustomerCollection(view_.Allentries)
End Function
%REM
Function GetCustomersByCity
Description: Comments for Function
%END REM
Function GetCustomersByCity(city As String) As CustomerCollection
Set view_ = database.GetView("CustomersByCity")
Set GetCustomersByCity = New CustomerCollection(view_.Getalldocumentsbykey(city, False))
End Function
%REM
Function GetCustomerByID
Description: Comments for Function
%END REM
Function GetCustomerByID(id As String) As Customer
Set view_ = database.GetView("CustomersByID")
Set GetCustomerByID = New Customer(view_.getDocumentByKey(id, True))
End Function
End Class
%REM
***********************************************************************
BELOW ARE THE BASE CLASSES ON WHICH THEY ARE DERIVED.....
***********************************************************************
%ENDREM
%REM
Class DocumentFactory
Description: Comments for Class
%END REM
Class DocumentFactory
Private Database_ As NotesDatabase
Private view_ As NotesView
%REM
Sub New
Description: Comments for Sub
%END REM
Sub New()
Dim session As New NotesSession
Set Database_ = session.Currentdatabase
End Sub
%REM
Property Get Database
Description: Comments for Property Get
%END REM
Property Get Database As NotesDatabase
Set Database = Database_
End Property
%REM
Sub printJSONFullArrayToWeb
Description: Comments for Sub
%END REM
Sub printJSONArrayToWebFull(coll As DocumentCollection)
Call printJSONArrayToWeb(coll, False)
End Sub
%REM
Sub printJSONFullArrayToWeb
Description: Comments for Sub
%END REM
Sub printJSONArrayToWebPickerFriendly(coll As DocumentCollection)
Call printJSONArrayToWeb(coll, True)
End Sub
%REM
Sub printJSONArrayToWeb
Description: Comments for Sub
%END REM
Private Sub printJSONArrayToWeb(coll As DocumentCollection, pickerFriendly As Boolean)
Dim o As Variant, Processed As Integer, buffer As String
Print "["
Set o = coll.getFirst()
While Not o Is Nothing
Processed = Processed + 1
If pickerFriendly Then
buffer = o.AsPickerReadyJSON
Else
buffer = o.AsJSON
End If
If processed < coll.count Then
buffer=buffer+","
End If
Print buffer
Set o = coll.getNext()
Wend
Print "]"
End Sub
End Class
%REM
Class DocumentWrapper
Description: Comments for Class
%END REM
Class DocumentWrapper
Private document_ As NotesDocument
%REM
Sub New
Description: Comments for Sub
%END REM
Sub New(doc As NotesDocument)
Set document_ = doc
End Sub
'Document
Property Get Document As NotesDocument
Set Document = document_
End Property
%REM
Property Get AsJSON
Description: Comments for Property Get
%END REM
Property Get AsJSON As String
'OVERRIDE THIS!
AsJSON = "{}"
End Property
%REM
Property Get DocumentDetailAsJSON
Description: Comments for Property Get
%END REM
Private Property Get DocumentDetailAsJSON As String
DocumentDetailAsJSON = | "Document": {|+_
| "UNID": "| + document.universalID + |",|+_
| "Form": "|+GetFieldValue("Form")+|",|+_
| "Created": new Date(|+Format(document.created, "yyyy")+|, (|+Format(document.created, "m")+|-1), |+Format(document.created, "dd")+|), |+_
| "Modified": new Date(|+Format(document.Lastmodified, "yyyy")+|, (|+Format(document.Lastmodified, "m")+|-1), |+Format(document.Lastmodified, "dd")+|)|+_
| }|
End Property
%REM
Function getFieldValue
Description: Comments for Function
%END REM
Function GetFieldValue(fieldName As String) As Variant
getFieldValue = document_.Getitemvalue(fieldName)(0)
End Function
%REM
Function Sanitize
Description: Escapes the " so as not to break the JSON
%END REM
Function Sanitize(s As String) As String
Sanitize = Replace(s, |"|, |\"|)
End Function
%REM
Sub Save
Description: Comments for Sub
%END REM
Sub Save()
Call document.save(True, True)
End Sub
End Class
%REM
Base "abstract" class to build collections of document-based objects upon
%ENDREM
Class DocumentCollection
Private Navigator_ As NotesViewNavigator
Private Collection_ As NotesDocumentCollection
Private Entries_ As NotesViewEntryCollection
Private Entry_ As NotesViewEntry
Private Document_ As NotesDocument
%REM
Sub New
Description: Comments for Sub
%END REM
Sub New (docs As Variant)
If docs IsA "NotesViewNavigator" Then
Set Navigator_ = docs
ElseIf docs IsA "NotesDocumentCollection" Then
Set Collection_ = docs
ElseIf docs IsA "NotesViewEntryCollection" Then
Set Entries_ = docs
Else
End If
End Sub
%REM
Function getFirst
Description: Comments for Function
%END REM
Function getFirst() As DocumentWrapper
'OVERRIDE
End Function
%REM
Function getFirst
Description: Comments for Function
%END REM
Function getNext() As DocumentWrapper
'OVERRIDE
End Function
%REM
Property Get CurrentDocument
Description: Comments for Property Get
%END REM
Property Get CurrentDocument As NotesDocument
Set CurrentDocument = Document_
End Property
Private Function getFirstDoc() As NotesDocument
If Not Navigator_ Is Nothing Then
Set Entry_ = Navigator_.Getfirst()
If Entry_ Is Nothing Then
Set getFirstDoc = Nothing
Else
Set Document_ = Entry_.Document
Set getFirstDoc = Document_
End If
ElseIf Not Entries_ Is Nothing Then
Set Entry_ = Entries_.Getfirstentry()
If Entry_ Is Nothing Then
Set getFirstDoc = Nothing
Else
Set Document_ = Entry_.Document
Set getFirstDoc = Document_
End If
ElseIf Not Collection_ Is Nothing Then
Set Document_ = Collection_.Getfirstdocument()
If Document_ Is Nothing Then
Set getFirstDoc = Nothing
Else
Set getFirstDoc = Document_
End If
End If
End Function
Private Function getNextDoc() As NotesDocument
If Not Navigator_ Is Nothing Then
Set Entry_ = Navigator_.GetNext(Entry_)
If Entry_ Is Nothing Then
Set getNextDoc = Nothing
Else
Set Document_ = Entry_.Document
Set getNextDoc = Document_
End If
ElseIf Not Entries_ Is Nothing Then
Set Entry_ = Entries_.GetNextentry(Entry_)
If Entry_ Is Nothing Then
Set getNextDoc = Nothing
Else
Set Document_ = Entry_.Document
Set getNextDoc = Document_
End If
ElseIf Not Collection_ Is Nothing Then
Set Document_ = Collection_.Getnextdocument(Document_)
If Document_ Is Nothing Then
Set getNextDoc = Nothing
Else
Set getNextDoc = Document_
End If
End If
End Function
%REM
Property Get Count
Description: Comments for Property Get
%END REM
Property Get Count As Long
If Not Collection_ Is Nothing Then
Count = Collection_.Count
ElseIf Not Navigator_ Is Nothing Then
Count = Navigator_.Count
ElseIf Not Entries_ Is Nothing Then
Count = Entries_.Count
Else
Count = 0
End If
End Property
End Class