This topic explains on an example how to manage additional attributes of GN4 objects.
Note: The auto-compute doesn't work for the custom attributes, added in <objectname>Extra section. Instead to use an attribute, add it as an element.
Let's assume we want to add a new attribute myattr to the Page object.
Have in mind that attributes can be added as attributes, or as elements. Some guidelines can be found here: http://www.ibm.com/developerworks/xml/library/x-eleatt/index.html.
The first step is to modify the schema and add the attribute. If you want to add an attribute as an attribute, in GN4.XSD locate the pageExtra xs:attributeGroup, and add the attribute there: <xs:attributeGroup name="pageExtra"> <xs:annotation> <xs:documentation>Additional attributes of the 'page' object type that are rendered as XML attributes</xs:documentation> </xs:annotation> <xs:attribute name="myattr" use="optional" type="xs:string"> <xs:annotation> <xs:documentation>Test extra attribute</xs:documentation> </xs:annotation> </xs:attribute> </xs:attributeGroup> If you want to add attribute as element, in GN4.XSD locate the pageExtra xs:group, and add the attribute as element there, e.g. notes or authorRef in articleExtra: <xs:group name="articleExtra"> <xs:annotation> <xs:documentation>Additional attributes of the 'article' object type thatare rendered as XML element</xs:documentation> </xs:annotation> <xs:sequence> <xs:group ref="archiveObjectExtra"/> <xs:element name="authorRef" type="reference" minOccurs="0" nillable="true" gs:refer="user"> <xs:annotation> <xs:documentation>Author of the article - it can be different</xs:documentation> </xs:annotation> </xs:element> <xs:element name="notes" type="tDescription" minOccurs="0"> <xs:annotation> <xs:documentation>Copy-flow notes</xs:documentation> </xs:annotation> </xs:element> </xs:sequence> </xs:group> |
In the second step, import the modified schema in the database: srv4 dbupdate -s "..\config\schemas\gn4.xsd" -d "" Then, perform IISRESET, or recycle the application pool, to update the server cache. |
Now, let's open a page in Fred4 and in script editor identify the page id: Dim pageId As Integer pageId = Page.GetPageId MsgBox (pageId) Let's assume that the page id is 2022. To view the page XML, in the Web browser, type this URL: http://localhost/GN4/do.ashx?cmd=objs&ids=2022 and then we'll see that the myattr exists and it's empty: <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="obj2022" name="section12 zone1" creationDate="2010-06-23T13:15:32.383Z" modifiedDate="2010-06-23T13:15:32.383Z" descName="title1 24/06/2010 editionNum1 edition1 section12 zone1" shortName="section12 zone1" number="2" logicalPage="false" leftColor="true" rightColor="true" leftSpot="0" rightSpot="0" deadline="0" myattr="" xmlns="urn:schemas-teradp-com:gn4tera"> <creatorRef idref="obj4" /> <modifierRef idref="obj4" /> <description /> <editionRef idref="obj2015" /> <sectionRef idref="obj2009" /> <zoneRef idref="obj2008" /> <masterRef idref="obj2016" /> <layers> <ref posX="0" posY="0" idref="obj2020" /> </layers> </page>
Having a page id, you can create the iGenericAttrObj that allows you to manage attributes in scripting. There are already functions that create and return such object both for the objects, referenced by article elements in Ted4 and for objects, dropped on a Fred4's page. Dim pageId As Integer Dim attrObj as IGenericAttrObj pageId = Page.GetPageId attrObj = UtilsBase.GetAttributes(pageId) 'create a new IGenericAttrObj The above provides: •display of standard attributes with ShowAttributes method of UtilsBase class: Dim pageId As Integer Dim attrObj as IGenericAttrObj pageId = Page.GetPageId attrObj = UtilsBase.GetAttributes(pageId) 'create a new IGenericAttrObj UtilsBase.ShowAttributes(attrObj) •modify the additional attribute with the syntax SetAttribute([IGenericAttrObj], [attr_name], [attr_value]) •read the additional attribute with the syntax GetString([IGenericAttrObj], [attr_name]) Dim pageId As Integer Dim attrObj as IGenericAttrObj Dim myattrValue as String pageId = Page.GetPageId attrObj = UtilsBase.GetAttributes(pageId) 'create a new IGenericAttrObj myattrValue = "life is beatiful" 'set the attribute in memory SetAttribute(attrObj, "myattr", myattrValue) 'read the attribute from memory MsgBox(GetString(attrObj, "myattr")) 'save the attribute into the database attrObj.SaveAttributes Please note that explicit save (attrObj.SaveAttributes) is required to store changed in the database (the LoadAttributes method is used to read from database). Thus, now you can read and modify the new attribute from scripting. |
To simplify usage, you may want to create scripting functions that allow the obj.myattr syntax instead to be limited to GetString generic function. Moreover, you may want to create the common method that will show all attributes - standard and extra. To do this, add the following code to the CustomPage.vb file. •add this Import on top of the file Imports System.Runtime.CompilerServices •add on the bottom of the CustomPage.vb the following module SiteExtensions with get/set methods for myattr. '---------------- start of "your custom code" zone " <summary>Extension methods for read/write object attributes</summary> " <remarks>The extra-attribute can be added into the GN4.xsd schema file, " see for example the 'imgextra' attribute group and similar.</remarks> Public Module SiteExtensions #Region "'pageExtra' attributes"
" <summary>Test extra attribute</summary> " <remarks>ObjectType: page</remarks> <Extension()> _ Public Function myattr(ByVal obj As IGenericAttrObj) As String Return GetString(obj, "myattr") End Function
" <summary>Test extra attribute</summary> " <remarks>ObjectType: txt</remarks> <Extension()> _ Public Sub Setmyattr(ByVal obj As IGenericAttrObj, ByVal value As String) SetAttribute(obj, "myattr", value) End Sub
#End Region #Region "show methods"
" <summary>Return the extra properties of the input object as string</summary> Public Function GetExtraAttributes(ByVal obj As IGenericAttrObj) As String If obj Is Nothing Then Return Nothing End If
'check if the object is 'page' or derived Return GetPageExtraAttributes(obj) End Function
" <summary>Return the extra properties of the input 'page' as string</summary> Private Function GetPageExtraAttributes(ByVal obj As IGenericAttrObj) As String If obj Is Nothing Then Return Nothing End If
If obj.IsKindOf("page") Then Dim message As String ' "myattr" attribute message = "myattr: '" & obj.myattr & "'" & Chr(13) & Chr(10)
Return message End If Return Nothing End Function
#End Region
'---------------- end of "your custom code" zone End Module ' do not modify this End Module command •add in the CustomPage.vb withing the CustomPageDesignerAddIn new methods to show the standard and extra attributes: #Region "Custom Attributes Scripts"
" <summary>Display the properties of the selected object</summary> Public Sub ShowPageAttributes() Dim pageId As Integer Dim attrObj As IGenericAttrObj
pageId = Page.GetPageId attrObj = UtilsBase.GetAttributes(pageId)
If attrObj Is Nothing Then MsgBox("No current page", MsgBoxStyle.OkOnly, "Show Page Attributes") Else 'show the page's attribute, including the extra ones ShowExtraAttributes(attrObj) End If End Sub
" <summary>Display all the properties of the object (including the site attributes)</summary> Public Shared Sub ShowExtraAttributes(ByVal obj As IGenericAttrObj) If obj Is Nothing Then MsgBox("No object to read", MsgBoxStyle.OkOnly, "Show attributes") Else Dim message As String Dim title As String title = "Attributes 1 ('" & obj.ObjectTypeName & "')" 'read the 'standard' editorial attributes message = GetEditorialAttributes(obj) 'read the extra-attributes (defined into the SiteTxtAttributes.vb) message &= GetExtraAttributes(obj) MsgBox(message, MsgBoxStyle.OkOnly, title) End If End Sub #End Region Now, the standard command ShowPageAttributes will show all the attributes of current page in Fred4, including the extra attribute myattr. |