Translate

Tuesday, December 15, 2015

Using PreUpdate or PostUpdate element for flex asset

In WebCenter Sites, whenever an asset is added/read/edited/deleted, many elements are called internally before an asset's CRUD operations are performed. These elements are stored under OpenMarket/Xcelerate/AssetType/[Your custom AssetType] folder.

Among the elements, PreUpdate and PostUpdate are called many times whenever any CRUD operations are performed on an asset. Hence, if any custom update is required either before or after an asset is saved in database, you can write your logic in PreUpdate element (before saving) or in PostUpdate element (after save).

Details related to which method should be updated is clearly mentioned in Developer's Guide. As not much detail is present in guide for any implementation or usage, I am discussing one very simple use case which is very common ask by any Client.

USE CASE:
If an asset is created in one site, it should be shared automatically with other sites. This is very practical and generic requirement for e.g. client may want to show News (all English ones) from all sites in other site's English version of News page or global site's News page.

Let us consider that there are 2 sites - FirstSite and FirstSite II, and both sites have product family and corresponding other child flex families. If a product asset of type - FSII Audio is created in FirstSite, it should be automatically shared with FirstSite II and vice-versa.

At first glance, this use case seems very easy and one can think of implementing a custom asset listener by updating the assetAdded method. But there is a catch; it seems that there is one bug that one cannot fetch asset subtype value using any method in asset listener while an asset is created and thus, cannot share only that asset whose type (definition) is FSII Audio. As asset is not saved in database at the time of call of asset listener, it does not have any way to retrieve asset's subtype value (or other custom attribute values) and hence, fails to meet the purpose. In such scenario, either you have to write flex filter or update that element which will be called just before an asset is shown in contributor UI after its created. How to develop flex filter is mentioned in detail in Developer's Guide, I have provided steps for updating core element method.

Elements while are called while CRUD operations for Product_C are located under "OpenMarket/Xcelerate/AssetType/Product_C/" folder. Create/Update element calls the PreUpdate and PostUpdate elements just before saving and after saving asset respectively. Hence, you can update one of the element to perform the task of sharing the asset. As we want to update asset based on subtype, PostUpdate element is most suitable option for this task as database entry has the subtype value present for the asset before PostUpdate element is called.

STEPS:
  1. Internally, PostUpdate element for each AssetType folder calls the element: OpenMarket/Gator/FlexibleAsset/Common/PostUpdate. PostUpdate is xml file containing various checks on operations like create, delete, update, remotepost, etc. These operations are briefly defined in developer's guide.
  2. If you are using Windows and have access to Sites Explorer, then navigate to the element and edit using desired text editor.
  3. If you are using Mac or Linux, one cannot use Sites Explorer, so just create new CSElement with same name and the element code will be visible in the element tab.
  4. In our case, need to add your code under "create" condition.
  5. As writing code in XML can be difficult and cannot use any JAVA within XML, call your JSP element using <CALLELEMENT> tag under Variable.updatetype=create condition and write your logic in JSP element to share the asset.
  6. You have the following ics variables available: AssetType as asset type and id as asset id, which can be used to find the subtype using <asset:getsubtype> tag and then this current asset can be shared using <asset:share> tag. You don't need to use <asset:save> tag.
  7. Save your element and test by creating new asset. As soon as product asset of type FSIIAudio is saved, it will be shared to the sites according to your code.
CAVEATS:
  1. As this is a type of customization, one has to keep track of such elements in event of upgrades in future and update accordingly after upgrade.
  2. Always keep backup of original element before updating any core element as it may or may not behave as intended.
  3. If an user has access to only one site say FirstSite and creates FSIIAudio product asset, it will be shared to FirstSite II also, according to our implementation. But if the same user wants to unshare/delete this asset, then he/she will not be able to do so as share function will not be accessible to unshare and/or then delete the asset. In such scenario, either you have to develop  custom unshare/delete button or user has to request the admin editor, who has access to all sites, to unshare/delete the asset. For creating custom button, you can check my post.
----------------------------------------------------
SUGGESTIONS/COMMENTS ARE INVITED
----------------------------------------------------

No comments: