Client Toolkit Activity Template

The Java code below can be copied and pasted into a new file and used as a template for utilising an activity from within the OGSA-DAI Client Toolkit.

package my.package;

import org.w3c.dom.Element;

import uk.org.ogsadai.client.toolkit.activity.Activity;
import uk.org.ogsadai.client.toolkit.activity.ActivityOutput;
import uk.org.ogsadai.client.toolkit.activity.NoActivityOutputException;
import uk.org.ogsadai.client.toolkit.exception.DataFormatException;
import uk.org.ogsadai.common.xml.XMLDocumentException;
import uk.org.ogsadai.common.xml.XMLParseException;
import uk.org.ogsadai.common.xml.XMLUtilities;


/**
 * This is a template class that can be used for writing new client toolkit
 * activity.
 *
 * @author The OGSA-DAI Project Team
 */
public class ActivityTemplate extends Activity
{

    // 1. Define instance fields for storing any configuration information
    //    that will be passsed to the constructor and set methods.

    /** A required setting. */
    private String mSettingA;

    /** Another required setting. */
    private int mSettingB;

    /** An optional setting. */
    private String mSettingC;

    // 2. Modify this constructor to register any inputs and/or outputs with the
    //    superclass. The constructor should also have parameters to allow
    //    any required configuration information to be specified. Multiple
    //    constructors may be defined if necessary.

    /**
     * Constructs the activity.
     */
    public ActivityTemplate(String settingA, int settingB)
    {
        addInput("DisplayNameForInput");
        addOutput("DisplayNameForOutput");
        mSettingA = settingA;
        mSettingB = settingB;
    }

    // 3. Define set methods for any optional configuration settings

    public void setSettingC(String setting)
    {
        if (setting == null)
        {
            throw new IllegalArgumentException("Argument must not be null");
        }
        mSettingC = setting;
    }

    // 4. Define set methods for any inputs to the activity.
    //    An example is provided suitable for an single input activity.
    //    This should be removed if the activity does not have any inputs.

    /**
     * Connected the input of this activity to output of another activity. By
     * this mechanism, activities can be chained to one another.
     *
     * @param input
     *            output from another activity
     * @throws IllegalArgumentException
     *             if the argument is null
     */
    public final void setInput(final ActivityOutput input)
    {
        if (input == null)
        {
            throw new IllegalArgumentException("Argument must not be null");
        }
        super.setInput(0, input.getName());
    }

    // 5. Define get methods for accessing any outputs of the activity. These
    //    outputs can be connected to the inputs of other activities to form
    //    activity chains.

    /**
     * Gets the activity output.
     *
     * @return the activity output
     */
    public ActivityOutput getOutput()
    {
        return getOutputs()[0];
    }

    // 6. Define get methods for accessing any outputs generated by the activity
    //    in a usable fasion. For example, the SQLQuery activity has a
    //    getResultSet method that gets the output of the activity as a
    //    java.sql.ResultSet rather than an XML string.

    /**
     * Gets something useful from the result data produced by the activity, if
     * there was any result data.
     *
     * @return something useful
     * @throws DataFormatException
     *             if the result data is not in the expected format
     * @throws NoActivityOutputException
     *             if there is no result data for this activity.
     */
    public int getSomething() 
        throws DataFormatException, NoActivityOutputException
    {
        int something = 0;

        ActivityOutput output = getOutputs()[0];
        String outputData = output.getData();

        try
        {
            Element element = XMLUtilities.xmlStringToDOM(
                outputData, false).getDocumentElement();
            something = Integer.parseInt(element.getAttribute("someAttribute"));
        } 
        catch (XMLDocumentException e) 
        {
            throw new DataFormatException(e);
        } 
        catch (XMLParseException e)
        {
            throw new DataFormatException(e);
        }
        return something;
    }

    // 7. Modify the generateXML method to construct the XML that will be
    //    embedded into a perform document for this activity. This usually
    //    consists of appending element and attribute XML to a StringBuffer
    //    and depends on the configuration information passed to the constructor
    //    and set methods.

    /* (non-Javadoc)
     * @see uk.org.ogsadai.client.toolkit.Activity#generateXML()
     */
    protected String generateXML()
    {
        final StringBuffer xml = new StringBuffer();

        xml.append("<activityElement>\n");

        // append activity input sub-element

        xml.append("<input from=\"");
        xml.append(getInputParameters()[0].getOutputName());
        xml.append("\"/>\n");

        // append activity configuration element(s)

        xml.append("  <settings a=\"");
        xml.append(mSettingA);
        xml.append("\" b=\"");
        xml.append(mSettingB);
        xml.append("\"/>\n");

        // append optional activity configuration element

        if (mSettingC != null)
        {
            xml.append("  <optionalSetting>");
            xml.append(mSettingC);
            xml.append("</optionalSetting>");
        }

        // append activity output sub-element

        xml.append("  <output name=\"");
        xml.append(getOutputs()[0].getName());
        xml.append("\"/>\n");

        xml.append("</activityElement>");

        return xml.toString();
    }
}

For further details it is recommended to download the OGSA-DAI source distribution and study the source code for the abstract OGSA-DAI/src/java/core/uk/org/ogsadai/client/toolkit/activity/Activity.java class.