-
Notifications
You must be signed in to change notification settings - Fork 0
/
Person.cls
94 lines (79 loc) · 2.14 KB
/
Person.cls
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
Class sample.Person Extends (%Persistent, %XML.Adaptor, %JSON.Adaptor)
{
/// Name
Property Name As %String;
/// Last name
Property LastName As %String(%JSONFIELDNAME = "last");
/// Birth date
Property BirthDate As %Date;
/// Sex
Property AdministrativeSex As %String(VALUELIST = ",M,F");
/// Address
Property Company As Company(%JSONREFERENCE = "ID");
ForeignKey FKCompany(Company) References Company();
/// Sample method that exports as JSON and XML an object
/// call it using: do ##class(sample.Person).RunSample()
ClassMethod RunSample()
{
// company
set company = ##class(sample.Company).CodeIdxOpen("ACME")
if '$isobject(company) {
set company = ##class(sample.Company).%New()
set company.Code = "ACME"
set company.Name = "A.C.M.E. Corp."
set company.Description = "A cool company called ACME"
}
// create new object
set person = ##class(sample.Person).%New()
// set object properties
set person.Name = "Joe"
set person.LastName = "Burrow"
set person.BirthDate = $zdateh("1996-12-10", 3)
set person.Company = company
// save object
do person.%Save()
// export object to JSON
do person.%JSONExportToString(.jsonString)
write !,"JSON: "
write !,jsonString
// export object to XML
set writer=##class(%XML.Writer).%New()
set sc=writer.OutputToString()
do writer.RootObject(person)
set xmlString = writer.GetXMLString()
write !,"XML: "
write !,xmlString
}
Storage Default
{
<Data name="PersonDefaultData">
<Value name="1">
<Value>%%CLASSNAME</Value>
</Value>
<Value name="2">
<Value>Name</Value>
</Value>
<Value name="3">
<Value>LastName</Value>
</Value>
<Value name="4">
<Value>BirthDate</Value>
</Value>
<Value name="5">
<Value>AdministrativeSex</Value>
</Value>
<Value name="6">
<Value>Address</Value>
</Value>
<Value name="7">
<Value>Company</Value>
</Value>
</Data>
<DataLocation>^sample.PersonD</DataLocation>
<DefaultData>PersonDefaultData</DefaultData>
<IdLocation>^sample.PersonD</IdLocation>
<IndexLocation>^sample.PersonI</IndexLocation>
<StreamLocation>^sample.PersonS</StreamLocation>
<Type>%Storage.Persistent</Type>
}
}