summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/lifecycle/instance/predefined/PredefinedStep.java
blob: 2e2869d45068926f43e74461d6d0bfa72ee04b17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.c2kernel.lifecycle.instance.predefined;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.InputSource;

import com.c2kernel.lifecycle.instance.Activity;
import com.c2kernel.persistency.outcome.Outcome;
import com.c2kernel.utils.Logger;
/***********************************************************************************************************************************************************************************************************************************************************************************************************
 * @author $Author: sgaspard $ $Date: 2004/09/21 10:32:17 $
 * @version $Revision: 1.14 $
 **********************************************************************************************************************************************************************************************************************************************************************************************************/
public class PredefinedStep extends Activity
{
	/*******************************************************************************************************************************************************************************************************************************************************************************************************
	 * predefined Steps are always Active, and have only one transition subclasses could override this method (if necessary)
	 ******************************************************************************************************************************************************************************************************************************************************************************************************/
	private boolean isPredefined = false;
	@Override
	public boolean getActive()
	{
		if (isPredefined)
			return true;
		else
			return super.getActive();
	}
	
	public PredefinedStep() {
		super();
		getProperties().put("SchemaType", "PredefinedStepOutcome");
		getProperties().put("SchemaVersion", "0");
	}

	public static final int DONE = 0;
	public static final int AVAILABLE = 0;
	@Override
	public String getTransitions()
	{
		if (isPredefined)
			return "<PossibleTransitions>done</PossibleTransitions>";
		else
			return super.getTransitions();
	}
	@Override
	public String getErrors()
	{
		if (isPredefined)
			return getName();
		else
			return super.getErrors();
	}
	@Override
	public boolean verify()
	{
		if (isPredefined)
			return true;
		else
			return super.verify();
	}
	/**
	 * Returns the isPredefined.
	 *
	 * @return boolean
	 */
	public boolean getIsPredefined()
	{
		return isPredefined;
	}
	/**
	 * Sets the isPredefined.
	 *
	 * @param isPredefined
	 *            The isPredefined to set
	 */
	public void setIsPredefined(boolean isPredefined)
	{
		this.isPredefined = isPredefined;
	}
	@Override
	public String getType()
	{
		return getName();
	}
	// generic bundling of parameters
	static public String bundleData(String[] data)
	{
		try
		{
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document dom = builder.newDocument();
			Element root = dom.createElement("PredefinedStepOutcome");
			dom.appendChild(root);
			for (String element : data) {
				Element param = dom.createElement("param");
				Text t = dom.createTextNode(element);
				param.appendChild(t);
				root.appendChild(param);
			}
			
			return Outcome.serialize(dom, false);

		}
		catch (Exception e)
		{
            Logger.error(e);
			StringBuffer xmlData = new StringBuffer().append("<PredefinedStepOutcome>");
			for (String element : data)
				xmlData.append("<param><![CDATA[").append(element).append("]]></param>");
			xmlData.append("</PredefinedStepOutcome>");
			return xmlData.toString();
		}
	}
	// generic bundling of single parameter
	static public String bundleData(String data)
	{
		return "<PredefinedStepOutcome><param><![CDATA[" + data + "]]></param></PredefinedStepOutcome>";
	}
	static public String[] getDataList(String xmlData)
	{
		try
		{
			Document scriptDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(xmlData)));
			NodeList nodeList = scriptDoc.getElementsByTagName("param");
			String[] result = new String[nodeList.getLength()];
			for (int i = 0; i < nodeList.getLength(); i++)
            {
                Node n = nodeList.item(i).getFirstChild();
                if (n instanceof CDATASection)
                	result[i] = ((CDATASection) n).getData();
                else if (n instanceof Text)
                    result[i] = ((Text) n).getData();
            }
			return result;
		}
		catch (Exception ex)
		{
			Logger.error("Exception::PredefinedStep::getDataList()");
			Logger.error(ex);
		}
		return null;
	}
}