Translate

Sunday, June 28, 2015

Creating custom Event Listeners

Oracle WebCenter Sites (erstwhile FatWire) provides few API's to perform general auditing tasks or housekeeping processes like generating reports/weekly assessment on asset operations and publishing process which may or may not required by an organization but certainly is demanding feature for many customers.

Albeit there is no such simple button/process to do so within Oracle WebCenter Sites UI and hence, developers have to provide custom solutions to meet their customer requirements. And to do so, this custom business logic has to be implemented in form java classes and deploy to <webapps>/WEB-INF/lib folder.

Before browsing through this blog, it better to understand this topic: Event Listener from developer's guide. The process of registering listener is already mentioned in the guide, hence, I won't describe here but rather a sample snippet of code code on how to implement listener.

Note: All the below methods and classes are available with latest Oracle WebCenter Sites patch which may or may not be present in older version of FatWire/Oracle WCS.

There are 2 type of event listener which are of more importance:

Asset Event Listener: which fires on following asset operations: Add/Update/Delete/UndoCheckout/Approved/UnApproved

 public final class MyAssetEventListener extends AbstractAssetEventListener  
   {  
     public void assetAdded(AssetId id)  
     {  
       log.info("Asset Type: " + id.getType() + " and id:" + id.getId() + " added");  
     }  
     public void assetUpdated(AssetId id)  
     {  
      log.info("Asset Type: " + id.getType() + " and id:" + id.getId() + " updated");  
     }  
     public void assetDeleted(AssetId id)  
     {  
      log.info("Asset Type: " + id.getType() + " and id:" + id.getId() + " deleted");  
     }  
     public void assetApproved(AssetId id)  
     {  
      log.info("Asset Type: " + id.getType() + " and id:" + id.getId() + " approved");  
     }  
     public void assetUnapproved(AssetId id)  
     {  
      log.info("Asset Type: " + id.getType() + " and id:" + id.getId() + " unapproved");  
     }  
     public void assetUnapproved(AssetId id, Map<String, Object> properties)  
     {  
      log.info(" User " + properties.get("user") + " has unapproved an asset:" + id + " for the target:" + properties.get("targetId") + ".");  
     }  
   }  

Publish Event Listener: which fires on publishing tasks

 public class MyPublishEventListener implements PublishingEventListener {  
      public void onEvent(PublishingEvent event) {  
           PublishingTasks task = event.getTaskName();  
           /*  
            * task can be any one of the following: GATHERER, PACKAGER, TRANSPORTER, UNPACKER, CACHEUPDATER, NOTEPUBLISH, SESSION  
            */  
           PublishingStatusEnum status = event.getStatus();  
           /*  
            * status can be one of the following: STARTED, DONE, FAILED, CANCELLED, SUBTASK_FINISHED  
            */  
           List<AssetId> assetids = event.getAssetIds();  
           /*  
            * AssetId contains info about type and id in following format: ASSETTYPE:ID  
            */  
           String pubSessionId = event.getPubSessionId();  
           /*  
            * Publication Session  
            */  
           /*  
            * Publishing target Id and target Name  
            */  
           String targetId = event.getTargetId();  
           String targetName = event.getTargetname();  
           /*  
            * Username  
            */  
           String userName = event.getUsername();   
           if (PublishingTasks.NOTEPUBLISH.equals(task)) {  
                if (PublishingStatusEnum.STARTED.equals(status)) {  
                     for (AssetId id : assetids) {  
                          // do something  
                     }  
                }  
                if (PublishingStatusEnum.DONE.equals(status)) {  
                     for (AssetId id : assetids) {  
                          // do something  
                     }  
                }  
                if (PublishingStatusEnum.FAILED.equals(status)) {  
                     for (AssetId id : assetids) {  
                          // do something  
                     }  
                }  
           }  
      }  
 }  
Required jars: basic.jar, assetapi.jar, cs-core.jar, cs.jar, xcelerate.jar and log jars if logging.

Performance: There are caveats associated using custom listeners as it can affect authoring experience if frequent amount of asset activity is carried out. But again it depends on the method which you have implemented. Even if you are using single method of particular listener class, it then boils down to what further actions are taken. For e.g. Just printing a single message in log will not affect much but if process includes asset retrieval, streaming output in file system or database and notifying certain other processes, etc. can really degrade performance.

----------------------------------------------------
SUGGESTIONS/COMMENTS ARE INVITED
----------------------------------------------------


No comments: