Lab 2.4: Implementing a Job Result Handler
Time Estimates for Completion
Create a job result handler for the SimpleModelGeometryCheck action.
Description:
5 minutes
Lab exercise:
15 minutes
Description
A job result handler must implement the interface IJobResultHandler:
setInputDirectory — This method is called to set the directory from where to read the job result data.
getInputDirectory — the corresponding getter method to setInputDirectory.
importJobResultData(String elementElid, Properties jobParameters) — This method is called to process the result of the job execution.
Instead of implementing the interface IJobResultHandler it may be more convenient to extend the class AbstractJobResultHandler that provides basic support for:
Reading the result index.xml.
Accessing result files.
The job result handler implementations of interest in this lab are:
EmailJobResultHandler: Sends job execution results (like generated neutral formats) by e-mail.
SaveToFileSystemJobResultHandler: Saves job execution results (like generated neutral formats) to the file system.
StoreToDatabaseJobResultHandler: Stores job execution results (like generated neutral formats) into the database.
The job's result index.xml
The job result needs to contain an index.xml file with additional information regarding the job execution, such as error states, result files, etc. The job result handlers use the information in the index.xml file.
The Simple Model Geometry Check returns the check result in the modelcheck attribute of the results element.
<?xml version="1.0" encoding="utf-8" ?>
<job type="modeling.simplegeometrycheck" version="20.8">.
<element id="BYNP7GNE2VLHYS" timestamp="1196780288000" />
<results modelcheck="model_corrupt">
<result-file format="PKG" mimetype="" zipname="model.pkg" name="model.pkg" />
</results>
</job>
* 
The EmailJobResultHandler, StoreToDatabaseJobResultHandler, and SaveToFileSystemJobResultHandler check the <result-file> entries for files to e-mail, store to the database, or save to the file system. For information on the supported attributes of the result-file tag, see the AbstractJobResultHandler.ResultFileEntry JavaDoc.
Lab Exercise
In this lab you will extend EmailJobResultHandler to e-mail the annotated model only if the geometry check detects problems or errors, and update the job's xml configuration file to use the new result handler.
Prerequisites
You need the SimpleModelGeometryCheckJob.xml and SimpleModelGeometryCheckTemplate.lsp files from Lab 2.3.
Extend the EmailJobResultHandler
For the Simple Model Geometry Check we now want to extend EmailJobResultHandler so that it only e-mails the annotated model if the geometry check detected any problems or errors. (Click here for a file you can copy and paste.)
package com.acme;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import com.osm.automation.EmailJobResultHandler;
import com.osm.automation.JobExecutionFailedException;
import com.osm.automation.JobResultOutOfDateException;
import com.osm.exception.WMException;
import com.osm.util.Strings;

public class SimpleModelGeometryCheckJobResultHandler extends EmailJobResultHandler {
@Override
public void importJobResultData(String elementElid, Properties jobParams)
throws JobExecutionFailedException, WMException, IOException, JobResultOutOfDateException {
final SimpleModelGeometryCheckJobResultIndex index = (SimpleModelGeometryCheckJobResultIndex) getResultIndex();
if (!index.isModelOK()) {
// only send e-mail if model is not o.k.
super.importJobResultData(elementElid, jobParams);
}
}
@Override
protected JobResultIndex createResultIndex(File indexXmlFile)
throws Exception {
return new SimpleModelGeometryCheckJobResultIndex(indexXmlFile);
}
// special JobResultIndex that evaluates the modelcheck attribute
private static class SimpleModelGeometryCheckJobResultIndex extends JobResultIndex {
private static String XPATH_MODEL_CHECK = "//results/@modelcheck"; //frozen
private String modelcheck;
public SimpleModelGeometryCheckJobResultIndex(File xmlFile)
throws Exception {
super(xmlFile);
}
@Override
protected void parseXml(Document indexDoc) throws Exception {
super.parseXml(indexDoc);
final XPath xpath = XPathFactory.newInstance().newXPath();
this.modelcheck = xpath.evaluate(XPATH_MODEL_CHECK, indexDoc);
}
public boolean isModelOK() {
return !Strings.isEmpty(modelcheck) && modelcheck.equals("model_ok");
}
}
}
The special implementation SimpleModelGeometryCheckJobResultIndex reads the index.xml file, providing easy access to the error state of the model geometry check.
Configure the new SimpleModelGeometryCheckJobResultHandler in the XML file
Add the new SimpleModelGeometryCheckJobResultHandler to the SimpleModelGeometryCheckJob.xml job configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<Job type="modeling.simplegeometrycheck" has_result_data="true">

<DisplayName>Simple Model Geometry Check</DisplayName>

<UI>
<ActionMenu wm_java_class="com.osm.datamgmt.biz.Model" include_derived="true">
<Name>Simple Model Geometry Check</Name>
<JobGroup>Model Checks</JobGroup>
<Action java_class="com.osm.automation.action.MultiSelectCreateJobScheduleAction">
<Description>Checks a model's geometry</Description>
</Action>
</ActionMenu>
</UI>
<Exporter java_class="com.acme.SimpleModelGeometryCheckJobExporter">
<StartScriptTemplate>SimpleModelGeometryCheckTemplate.lsp</StartScriptTemplate>
<ModelLoadRule catalog="model" msg_num="728">Highest Revisions</ModelLoadRule>
</Exporter>
<ResultHandler java_class="com.acme.SimpleModelGeometryCheckJobResultHandler">
<EmailFiles>
<Subject>Model check had failures</Subject>
<Body> Model check had failures for '{1}'. Annotated model is attached.</Body>
</EmailFiles>
</ResultHandler>
</Job>
Optional: Configure multiple job result handlers in the XML file
In some cases it may be desirable to have more than one job result handler configured to process the job result in different ways, (sending an e-mail notification in case of errors and storing the result to the database or the file system). The CompoundJobResultHandler is a special job result handler that aggregates multiple (optional) job result handlers.
To store the annotated model that is returned by a Model Geometry Check we can configure the CompoundJobResultHandler to include SimpleModelGeometryCheckJobResultHandler and SaveToFileSystemJobResultHandler:
<?xml version="1.0" encoding="UTF-8"?>
<Job type="modeling.simplegeometrycheck" has_result_data="true">
<DisplayName>Simple Model Geometry Check</DisplayName>

<UI>
<ActionMenu wm_java_class="com.osm.datamgmt.biz.Model" include_derived="true">
<Name>Simple Model Geometry Check</Name>
<JobGroup>Model Checks</JobGroup>
<Action java_class="com.osm.automation.action.MultiSelectCreateJobScheduleAction">
<Description>Checks a model's geometry</Description>
</Action>
</ActionMenu>
</UI>
<Exporter java_class="com.acme.SimpleModelGeometryCheckJobExporter">
<StartScriptTemplate>SimpleModelGeometryCheckTemplate.lsp</StartScriptTemplate>
<ModelLoadRule catalog="model" msg_num="728">Highest Revisions</ModelLoadRule>
</Exporter>
<ResultHandler java_class="com.osm.automation.CompoundJobResultHandler">
<ResultHandlerOption name="MailOnFailureOption" ignore_errors="false">true
<ResultHandler java_class="com.acme.SimpleModelGeometryCheckJobResultHandler">
<DisplayName>Send e-mail in case of failures</DisplayName>
<EmailFiles>
<Subject>Model check had failures</Subject>
<Body> Model check had failures for '{1}'. Annotated model is attached.</Body>
</EmailFiles>
</ResultHandler>
</ResultHandlerOption>
<ResultHandlerOption name="SaveToFileSystem" ignore_errors="false">true
<ResultHandler java_class="com.osm.automation.SaveToFileSystemJobResultHandler">
<DisplayName>Save annotated model to server directory</DisplayName>
<TargetDirectory>C:\taskagent</TargetDirectory>
</ResultHandler>
</ResultHandlerOption>
</ResultHandler>
</Job>
The value of the ResultHandlerOption determines if the result handler is activated or not by default. In case the job's UI configuration allows the creation of task schedules in the Creo Elements/Direct Model Manager client, the aggregated job result handlers SimpleModelGeometryCheckJobResultHandler and SaveToFileSystemJobResultHandler appear in the Create Task dialog as independent result handling options.
Was this helpful?