Copy part of the properties to another object
Sometimes, we need to copy part of the properties of an object into a different one.
The simplest thing would be to do the following:
Set obj1.FirstName = obj2.FirstName
Set obj1.SecondName = obj2.SecondName
What happens if the object contains a large number of properties? or we just want to extract an important group of data, and complement the information in another object?
Having the following classes:
Class Kurro.PersonalInfo Extends %Library.SerialObject
{
/// Code of personal
Property Code As %String;
/// First name of personal
Property FirstName As %String;
/// Second name of personal
Property SecondName As %String;
/// Date of birthday of personal
Property DateOfBirthday As %DateTime;
/// Passport ID number
Property PassportId As %String;
}
Class Kurro.NameInfo Extends %Library.SerialObject
{
/// First name of personal
Property FirstName As %String;
/// Second name of personal
Property SecondName As %String;
/// Date of birthday of personal
Property DateOfBirthday As %DateTime;
/// Relationship
Property Relationship As %String;
}
I've populated the object PersonalInfo
ENSDEMO>zw personal
personal=<OBJECT REFERENCE>[1@Kurro.PersonalInfo]
+----------------- general information ---------------
| oref value: 1
| class name: Kurro.PersonalInfo
| reference count: 2
+----------------- attribute values ------------------
| Code = "FLH01"
| DateOfBirthday = 47878
| FirstName = "Francisco"
| PassportId = "ESP-123456"
| SecondName = "Lopez"
+-----------------------------------------------------
I've created the following utils class
Class Kurro.Utils.Functions
{
/// Copy properties
Method CopyProps(Output obj) As %Status [ CodeMode = objectgenerator ]
{
set properties = %compiledclass.Properties
for i=1:1:properties.Count()
{
#dim prop As %Dictionary.CompiledProperty = properties.GetAt(i)
if prop.Name'="",prop.Name'["%"
{
set propName = prop.Name
if prop.Name["_"
{
set propName = """"_prop.Name_""""
}
do %code.WriteLine(" If (obj.ExistsProperty("""_propName_""")) { set obj."_propName _"=.."_propName_" } ")
}
}
quit $$$OK
}
/// Check if a property exists
ClassMethod ExistsProperty(pPropName As %String = "") As %Boolean
{
Set tExists = 0
If (pPropName '= "")
{
Set tExists = ##class(%Dictionary.CompiledProperty).IDKEYExists($classname($this),pPropName)
}
Quit tExists
}
}
So now we need to extend our classes also to this utils class
Class Kurro.PersonalInfo Extends (%Library.SerialObject, Kurro.Utils.Functions)
Class Kurro.NameInfo Extends (%Library.SerialObject, Kurro.Utils.Functions)
You can check how it works using the following code:
set personal = ##class(Kurro.PersonalInfo).%New()
set personal.Code="FLH01"
set personal.FirstName="Francisco"
set personal.SecondName="Lopez"
set personal.DateOfBirthday=$ZDATEH("1972-02-01",3)
set personal.PassportId ="ESP-123456"
set name = ##class(Kurro.NameInfo).%New()
do personal.CopyProps(.name)
zw name
This is the result:
ENSDEMO>zw name name=<OBJECT REFERENCE>[3@Kurro.NameInfo] +----------------- general information --------------- | oref value: 3 | class name: Kurro.NameInfo | reference count: 2 +----------------- attribute values ------------------ | DateOfBirthday = 47878 | FirstName = "Francisco" | Relationship = "" | SecondName = "Lopez" +-----------------------------------------------------
Important: Only you can copy content to a class that extends to this functions, because it needs the method ExistsProperty.
I hope this trick is as useful to you as it has been to me
Best regards.
Update: This account is discontinued, please follow the new account: @Kurro Lopez