Skip to content

Commit

Permalink
Initial form adaptor
Browse files Browse the repository at this point in the history
  • Loading branch information
eduard93 committed Jun 21, 2016
1 parent 5756aca commit 343ed55
Show file tree
Hide file tree
Showing 7 changed files with 458 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Form.inc.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<Export generator="Cache" version="25">
<Routine name="Form" type="INC"><![CDATA[
#define formGlobal ^form.forms
#define catGlobal ^form.catalogs
#define settingsGlobal ^form.settings
#define formNameParam "FORMNAME"
#define metadata "metadata"
#define forAll(%in,%gn) s gn%in=$na(%gn) s %in="" f { s %in=$o(@gn%in@(%in)) q:%in=""
#define forAllk(%in,%kn,%gn) s gn%in=$na(%gn) s %in="" f %kn=1:1 { s %in=$o(@gn%in@(%in)) q:%in=""
#define endFor }
#define formatJSON "aeloqtwc"
#define formatJSONSQL "tw"
#define formatDate 3
#define formatTime 7
#define toJSONClassMethod "toJSON"
#define updateClassMethod "updateIdFromObject"
#define fileClass "Form.AttachmentType"
#define fileDir $g($$$settingsGlobal("fileDir"), ##class(%Library.File).GetDirectory())
]]></Routine>
</Export>
152 changes: 152 additions & 0 deletions Form/Adaptor.cls.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?xml version="1.0" encoding="UTF-8"?>
<Export generator="Cache" version="25">
<Class name="Form.Adaptor">
<Description>
Базовый класс-адаптер для форм</Description>
<Abstract>1</Abstract>
<CompileAfter>Form.Info</CompileAfter>
<IncludeCode>Form</IncludeCode>
<PropertyClass>Form.Field</PropertyClass>
<TimeCreated>64090,75147.034342</TimeCreated>
<DependsOn>Form.Info</DependsOn>

<Parameter name="FORMNAME">
<Description>
Имя формы: латиница, менее 500 символов, в нижнем регистре </Description>
<Type>%String</Type>
</Parameter>

<Parameter name="FORMDISPLAYNAME">
<Description>
Отображаемое имя формы</Description>
<Type>%String</Type>
</Parameter>

<Method name="generateMetadata">
<Description>
Сгенерировать метаданные для формы</Description>
<ClassMethod>1</ClassMethod>
<CodeMode>objectgenerator</CodeMode>
<GenerateAfter>onFormCompile</GenerateAfter>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
#define formGlobal ^form.forms
#define formNameParam "FORMNAME"
#define metadata "metadata"
#define displayNameParam "FORMDISPLAYNAME"
#define propDisplayNameParam "DISPLAYNAME"
#dim sc As %Status = $$$OK
set className = %compiledclass.Name
return:className="Form.Adaptor" sc
set metadata = {}
set metadata.name = %parameter($$$formNameParam)
set metadata.class = className
set metadata.fields = []
for i = 1:1:%compiledclass.Properties.Count() {
#dim property As %Dictionary.CompiledProperty
set property = %compiledclass.Properties.GetAt(i)
set propertyName = property.Name
CONTINUE:((propertyName="%%OID") || (propertyName="%Concurrency"))
/// В метаинформацию попадают только свойства у которых задан DISPLAYNAME
if (property.Parameters.GetAt($$$propDisplayNameParam)'="") {
set propertyMetaData = ##class(Form.Property).propertyToMetadata(property)
do metadata.fields.$push(propertyMetaData)
}
}
set $$$formGlobal(className, $$$metadata) = metadata.$toJSON()
return sc
]]></Implementation>
</Method>

<Method name="toJSON">
<Description>
Выводит объект Id в формате JSON на текущее устройство
Переопределите этот метод для конкретной формы, если есть какие-то особенности её обработки
Возможно: заменить метод на генератор, который будет генерить код для формы
Подразумевается, что форма хранимая</Description>
<ClassMethod>1</ClassMethod>
<FormalSpec>id:%Integer=""</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
#dim sc As %Status = $$$OK
set obj = ..%OpenId(id,,.sc)
return:$$$ISERR(sc) sc
return ##class(Form.REST.JSON).%ObjectToJSON(obj,,,$$$formatJSON)
]]></Implementation>
</Method>

<Method name="updateIdFromObject">
<Description>
Открывает объект по Id и перезаписывает все его свойства
соответствующими значениями из object
После чего сохраняет объект</Description>
<ClassMethod>1</ClassMethod>
<FormalSpec>id:%Integer="",object=""</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
#dim sc As %Status = $$$OK
return:'$isObject(object) $$$ERROR($$$GeneralError, "Pass an object as a second argument to updateIdFromObject from " _ $className())
set obj = ..%OpenId(id,,.sc)
return:$$$ISERR(sc) sc
set sc = obj.updateFromObject(object)
return:$$$ISERR(sc) sc
set sc = obj.%Save()
return sc
]]></Implementation>
</Method>

<Method name="updateFromObject">
<Description>
Устанавливает все поля текущего объекта из переданного объекта
Это может быть динамический объект или объект того же класса</Description>
<CodeMode>objectgenerator</CodeMode>
<FormalSpec>object</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
for i = 1:1:%class.Properties.Count() {
set prop = %class.Properties.GetAt(i)
if prop.Internal || prop.Calculated || prop.ReadOnly || prop.Private || prop.Identity || prop.MultiDimensional continue
// Вопросы:
// 1. Что с объектами - в принципе
// 2. Что со списками
// 3. Пустые поля - пропускать или что делать?
// 4. Что с потоками
do %code.WriteLine(" set .." _ prop.Name _ " = object." _ prop.Name)
}
Do %code.WriteLine(" Quit $$$OK")
]]></Implementation>
</Method>

<Method name="getObjectInfo">
<Abstract>1</Abstract>
<ClassMethod>1</ClassMethod>
<FormalSpec>id:%Integer</FormalSpec>
<ReturnType>%Object</ReturnType>
<Implementation><![CDATA[ Return {}
]]></Implementation>
</Method>

<Method name="getObjectsInfo">
<ClassMethod>1</ClassMethod>
<FormalSpec>list:%ListOfDataTypes</FormalSpec>
<ReturnType>%Array</ReturnType>
<Implementation><![CDATA[
set objectsList = []
for i=1:1:list.Count() {
set id = list.GetAt(i)
set item = ..getObjectInfo(id)
do objectsList.$push(item)
}
return objectsList
]]></Implementation>
</Method>
</Class>
</Export>
15 changes: 15 additions & 0 deletions Form/Field.cls.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<Export generator="Cache" version="25">
<Class name="Form.Field">
<Description>
Form field properties</Description>
<Abstract>1</Abstract>
<TimeCreated>64090,75443.892382</TimeCreated>

<Parameter name="DISPLAYNAME">
<Description>
Field display name</Description>
<Type>%String</Type>
</Parameter>
</Class>
</Export>
58 changes: 58 additions & 0 deletions Form/Info.cls.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<Export generator="Cache" version="25">
<Class name="Form.Info">
<Abstract>1</Abstract>
<IncludeCode>Form</IncludeCode>
<TimeCreated>64090,75344.051371</TimeCreated>

<Method name="getFormsList">
<Description>
Получить список всех форм</Description>
<ClassMethod>1</ClassMethod>
<ReturnType>%Array</ReturnType>
<Implementation><![CDATA[
set forms = []
$$$forAll(form, $$$formGlobal)
do forms.$push(form)
$$$endFor
quit forms
]]></Implementation>
</Method>

<Method name="formExists">
<Description>
Check that form with this classname exist</Description>
<ClassMethod>1</ClassMethod>
<FormalSpec>class:%String</FormalSpec>
<ReturnType>%Boolean</ReturnType>
<Implementation><![CDATA[
return:class="" $$$NO
return $select($data($$$formGlobal(class))#2=1:$$$YES, 1:$$$NO)
]]></Implementation>
</Method>

<Method name="formExistsStatus">
<Description>
Check that form with this classname exist</Description>
<ClassMethod>1</ClassMethod>
<FormalSpec>class:%String</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
#dim sc As %Status = $$$OK
set formExists = ..formExists(class)
if 'formExists {
set sc = $$$ERROR($$$GeneralError, "Form " _ class _ " does not exist")
}
return sc
]]></Implementation>
</Method>

<Method name="getFormMetadata">
<ClassMethod>1</ClassMethod>
<FormalSpec>class:%String=""</FormalSpec>
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[ return $$$formGlobal(class, $$$metadata)
]]></Implementation>
</Method>
</Class>
</Export>
Loading

0 comments on commit 343ed55

Please sign in to comment.