Working with event rules in the database
Provides examples of using the Java™ API to work with event rules in the database.
The following examples indicate how you use the classes to work with event rules in the database:
Example 6: Adding an event rule to the database
Follow these steps:
- Define the event rule:
String eventRuleName = "SampleEventRule"; String eventRuleDescription = "Define Event Rule; test MessageLoggerPlugIn and TWSObjectsMonitorPlugIn"; Date today = new Date(System.currentTimeMillis()); Date tomorrow = new Date(System.currentTimeMillis() + 86400000L); //EventRule definition EventRule er = new EventRule(); er.setName(eventRuleName); er.setDescription(eventRuleDescription); er.setRuleType(EventRuleType.FILTER); er.setDraft(false); er.setValidFrom(today); er.setValidTo(tomorrow);
- Define the event condition. In this case the condition is a job submission:
EventCondition evCond = new EventCondition(); evCond.setPluginName(TWSObjectsMonitorPlugIn.PLUGIN_NAME); evCond.setEventType(JobUtil.EVENT_JOB_SUBMIT);
- Define the conditions that the event condition has to satisfy to trigger the rule action (the filtering predicate):
String filterPred = "<attributeFilter name=\"JobStreamWorkstation\" operator=\"eq\">" + "<value>MYWS</value>" + "</attributeFilter>" + "<attributeFilter name=\"JobStreamName\" operator=\"eq\">" + "<value>JS1</value>" + "</attributeFilter>" + "<attributeFilter name=\"JobName\" operator=\"eq\">" + "<value>JOB1</value>" + "</attributeFilter>" + "<attributeFilter name=\"Workstation\" operator=\"eq\">" + "<value>MYHOST</value>" + "</attributeFilter>" + "<attributeFilter name=\"Priority\" operator=\"range\">" + "<value>10</value>" + "<value>30</value>" + "</attributeFilter>" + "<attributeFilter name=\"Monitored\" operator=\"eq\">" + "<value>TRUE</value>" + "</attributeFilter>" + "<attributeFilter name=\"EstimatedDuration\" operator=\"ge\">" + "<value>400</value>" + "</attributeFilter>" + "<attributeFilter name=\"Login\" operator=\"eq\">" + "<value>TWSUser</value>" + "</attributeFilter>" + "<attributeFilter name=\"EveryFrequency\" operator=\"ge\">" + "<value>400</value>" + "</attributeFilter>";
- Complete the event condition:
evCond.setFilteringPredicate(filterPred);
- Add the event condition to the event rule:
er.getTriggerEvents().add(evCond);
- Define the rule action. In this example, the rule action logs a message in the database:
RuleAction action = new RuleAction(); action.setPluginName(MessageLoggerPlugIn.PLUGIN_NAME); action.setActionType(MessageLoggerPlugInConstants.ACTION_TYPE_MESSAGE_LOG); action.setDescription("Adding the Message logger Plugin"); action.setResponseType(RuleResponseType.ON_DETECTION);
- Define the value for the rule action parameter:
Map parameterMap = new HashMap(); parameterMap.put(MessageLoggerPlugInConstants.MESSAGE, "message"); parameterMap.put(MessageLoggerPlugInConstants.OBJECT_KEY, "object key");
- Complete the rule action:
action.getParameterMap().putAll(parameterMap);
- Add the rule action to the event rule:
er.getActions().add(action);
- Add the event rule to the ConnModel interface:
ConnModel myModel = null; //Get an instance of ConnModel interface... //... //Add the object Identifier erId = null; try { erId = myModel.addTWSObject(er, null); } catch (ConnException e) { //Do something to recover... }
Example 7: Retrieve an event rule from the database by ID
Follow these steps:
- Obtain the event rule ID to be retrieved by any means appropriate to your interface
- Retrieve the event rule:
EventRule eRuleRead = new EventRule(); try { eRuleRead = (EventRule) myModel.getTWSObject(EventRule.class, erId, false, null); } catch (ConnException e) { //Do something to recover... }
Example 8: Retrieve an event rule from the database by key (name)
Follow these steps:
- Obtain the event rule key (name) to be retrieved by any means appropriate to your interface
- Retrieve the event rule:
EventRule eRuleRead = new EventRule(); try { eRuleRead = (EventRule) myModel.getTWSObject(EventRule.class, new EventRuleKey(eventRuleName), false, null); } catch (ConnException e) { //Do something to recover... }
Example 9: Delete an event rule from the database by ID
Follow these steps:
- Retrieve by ID the event rule to be deleted, as shown in example 7.1
- If the event rule has been successfully retrieved, delete it:
{ myModel.removeTWSObject(EventRule.class, eRuleRead.getId(), null); } catch (ConnException exc) { //Do something to recover... }
Example 10: Delete an event rule from the database by key (name)
Follow these steps:
- Retrieve by key, the event rule to be deleted, as shown in example 7.2
- If the event rule has been successfully retrieved, delete it:
{ myModel.removeTWSObject(EventRule.class, new EventRuleKey(eventRuleName), null); } catch (ConnException exc) { //Do something to recover... }