001: /*
002: * Copyright 2007 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.workflow.commonimpl;
017:
018: import org.outerj.daisy.workflow.*;
019: import org.outerj.daisy.workflow.TaskUpdateData.VariableKey;
020: import org.outerj.daisy.workflow.TaskUpdateData.VariableValue;
021: import org.outerj.daisy.i18n.I18nMessage;
022: import org.outerx.daisy.x10Workflow.*;
023: import org.apache.xmlbeans.XmlObject;
024: import org.apache.xmlbeans.XmlCursor;
025: import org.apache.xmlbeans.XmlSaxHandler;
026: import org.apache.xmlbeans.XmlString;
027:
028: import java.util.*;
029:
030: public class WfXmlHelper {
031: public static WfVariable instantiateVariable(
032: VariableDocument.Variable xml) {
033: VariableScope scope = VariableScope.fromString(xml.getScope()
034: .toString());
035: ValueData data = getValue(xml, "variable");
036: return new WfVariableImpl(xml.getName(), scope, data.type,
037: data.value);
038: }
039:
040: public static ValueData getValue(VariableValuesType xml, String what) {
041: WfValueType type;
042: Object value;
043: if (xml.isSetString()) {
044: type = WfValueType.STRING;
045: value = xml.getString();
046: } else if (xml.isSetLong()) {
047: type = WfValueType.LONG;
048: value = xml.getLong();
049: } else if (xml.isSetDate()) {
050: type = WfValueType.DATE;
051: value = xml.getDate().getTime();
052: } else if (xml.isSetDateTime()) {
053: type = WfValueType.DATETIME;
054: value = xml.getDateTime().getTime();
055: } else if (xml.isSetDaisyLink()) {
056: type = WfValueType.DAISY_LINK;
057: value = instantiateVersionKey(xml.getDaisyLink());
058: } else if (xml.isSetActor()) {
059: type = WfValueType.ACTOR;
060: VariableDocument.Variable.Actor actor = xml.getActor();
061: if (actor.getPool()) {
062: if (actor.isSetId2()) {
063: List<Long> poolIds = new ArrayList<Long>(1);
064: poolIds.add(actor.getId2());
065: value = new WfActorKey(poolIds);
066: } else {
067: List<Long> poolIds = actor.getIdList();
068: List<Long> poolIdsList = new ArrayList<Long>(
069: poolIds.size());
070: for (long poolId : poolIds) {
071: poolIdsList.add(poolId);
072: }
073: value = new WfActorKey(poolIdsList);
074: }
075: } else {
076: value = new WfActorKey(actor.getId2());
077: }
078: } else if (xml.isSetBoolean()) {
079: type = WfValueType.BOOLEAN;
080: value = xml.getBoolean() ? Boolean.TRUE : Boolean.FALSE;
081: } else if (xml.isSetUser()) {
082: type = WfValueType.USER;
083: value = new WfUserKey(xml.getUser());
084: } else if (xml.isSetId()) {
085: type = WfValueType.ID;
086: value = xml.getId();
087: } else {
088: throw new RuntimeException(
089: "Missing tag for the value in XML representation of "
090: + what);
091: }
092: ValueData data = new ValueData();
093: data.value = value;
094: data.type = type;
095: return data;
096: }
097:
098: public static class ValueData {
099: public Object value;
100: public WfValueType type;
101: }
102:
103: public static WfVersionKey instantiateVersionKey(
104: VariableDocument.Variable.DaisyLink xml) {
105: return new WfVersionKey(xml.getDocumentId(), xml.getBranchId(),
106: xml.getLanguageId(), xml.getVersion());
107: }
108:
109: public static void setValue(VariableValuesType xml,
110: WfValueType type, Object value) {
111: switch (type) {
112: case STRING:
113: xml.setString((String) value);
114: break;
115: case LONG:
116: xml.setLong((Long) value);
117: break;
118: case DATE:
119: xml.setDate(getCalendar((Date) value));
120: break;
121: case DATETIME:
122: xml.setDateTime(getCalendar((Date) value));
123: break;
124: case DAISY_LINK:
125: WfVersionKey versionKey = (WfVersionKey) value;
126: VariableDocument.Variable.DaisyLink daisyLink = xml
127: .addNewDaisyLink();
128: daisyLink.setDocumentId(versionKey.getDocumentId());
129: daisyLink.setBranchId(versionKey.getBranchId());
130: daisyLink.setLanguageId(versionKey.getLanguageId());
131: if (versionKey.getVersion() != null)
132: daisyLink.setVersion(versionKey.getVersion());
133: break;
134: case ACTOR:
135: WfActorKey actorKey = (WfActorKey) value;
136: VariableDocument.Variable.Actor actorXml = xml
137: .addNewActor();
138: if (actorKey.isPool()) {
139: actorXml.setPool(true);
140: List<Long> poolIdsList = actorKey.getPoolIds();
141: long[] poolIds = new long[poolIdsList.size()];
142: for (int i = 0; i < poolIds.length; i++)
143: poolIds[i] = poolIdsList.get(i);
144: actorXml.setIdArray(poolIds);
145: } else {
146: actorXml.setPool(false);
147: actorXml.setId2(actorKey.getUserId());
148: }
149: break;
150: case BOOLEAN:
151: xml.setBoolean((Boolean) value);
152: break;
153: case USER:
154: xml.setUser(((WfUserKey) value).getId());
155: break;
156: case ID:
157: xml.setId((String) value);
158: break;
159: default:
160: throw new RuntimeException("Unexpected value type: " + type);
161: }
162: }
163:
164: public static Calendar getCalendar(Date date) {
165: GregorianCalendar calendar = new GregorianCalendar();
166: calendar.setTime(date);
167: return calendar;
168: }
169:
170: public static TaskUpdateData instantiateTaskUpdateData(
171: TaskUpdateDataDocument.TaskUpdateData xml) {
172: TaskUpdateData taskUpdateData = new TaskUpdateData();
173:
174: for (VariableDocument.Variable variableXml : xml
175: .getVariableList()) {
176: WfVariable variable = WfXmlHelper
177: .instantiateVariable(variableXml);
178: taskUpdateData.setVariable(variable.getName(), variable
179: .getScope(), variable.getType(), variable
180: .getValue());
181: }
182:
183: for (TaskUpdateDataDocument.TaskUpdateData.DeletedVariable deletedVarXml : xml
184: .getDeletedVariableList()) {
185: VariableScope scope = VariableScope
186: .fromString(deletedVarXml.getScope().toString());
187: taskUpdateData.deleteVariable(deletedVarXml.getName(),
188: scope);
189: }
190:
191: TaskPriority taskPriority = xml.isSetPriority() ? TaskPriority
192: .fromString(xml.getPriority().toString()) : null;
193: taskUpdateData.setPriority(taskPriority);
194:
195: if (xml.isSetDueDate()) {
196: if (xml.getDueDate().getClear())
197: taskUpdateData.clearDueDate();
198: else
199: taskUpdateData.setDueDate(xml.getDueDate()
200: .getCalendarValue().getTime());
201: }
202:
203: return taskUpdateData;
204: }
205:
206: public static TaskUpdateDataDocument getTaskUpdateXml(
207: TaskUpdateData taskUpdateData) {
208: TaskUpdateDataDocument document = TaskUpdateDataDocument.Factory
209: .newInstance();
210: TaskUpdateDataDocument.TaskUpdateData xml = document
211: .addNewTaskUpdateData();
212:
213: for (Map.Entry<VariableKey, VariableValue> entry : taskUpdateData
214: .getVariables().entrySet()) {
215: VariableDocument.Variable variableXml = xml
216: .addNewVariable();
217: variableXml.setName(entry.getKey().getName());
218: variableXml.setScope(ScopeType.Enum.forString(entry
219: .getKey().getScope().toString()));
220: WfValueType type = entry.getValue().getType();
221: Object value = entry.getValue().getValue();
222: setValue(variableXml, type, value);
223: }
224:
225: for (VariableKey deletedVar : taskUpdateData
226: .getDeletedVariables()) {
227: TaskUpdateDataDocument.TaskUpdateData.DeletedVariable deletedVarXml = xml
228: .addNewDeletedVariable();
229: deletedVarXml.setName(deletedVar.getName());
230: deletedVarXml.setScope(ScopeType.Enum.forString(deletedVar
231: .getScope().toString()));
232: }
233:
234: if (taskUpdateData.getDueDate() != null) {
235: xml.addNewDueDate().setCalendarValue(
236: getCalendar(taskUpdateData.getDueDate()));
237: } else if (taskUpdateData.getClearDueDate()) {
238: xml.addNewDueDate().setClear(true);
239: }
240: if (taskUpdateData.getPriority() != null)
241: xml.setPriority(PriorityType.Enum.forString(taskUpdateData
242: .getPriority().toString()));
243:
244: return document;
245: }
246:
247: public static void addAttribute(XmlObject xmlObject, String name,
248: String value) {
249: XmlCursor cursor = xmlObject.newCursor();
250: cursor.toNextToken();
251: cursor.insertAttributeWithValue(name, value);
252: cursor.dispose();
253: }
254:
255: public static XmlObject i18nMessageToXml(I18nMessage msg) {
256: try {
257: XmlSaxHandler saxHandler = XmlObject.Factory
258: .newXmlSaxHandler();
259: saxHandler.getContentHandler().startDocument();
260: msg.generateSaxFragment(saxHandler.getContentHandler());
261: saxHandler.getContentHandler().endDocument();
262: return saxHandler.getObject();
263: } catch (Exception e) {
264: throw new RuntimeException(
265: "Unexpected exception converting I18nMessage to XmlObject.",
266: e);
267: }
268: }
269:
270: public static XmlString stringToXml(String msg) {
271: XmlString msgXml = XmlString.Factory.newInstance();
272: msgXml.setStringValue(msg);
273: return msgXml;
274: }
275:
276: public static VariablesDocument getVariablesAsXml(
277: List<WfVariable> variables) {
278: VariablesDocument variablesDoc = VariablesDocument.Factory
279: .newInstance();
280: VariablesDocument.Variables variablesXml = variablesDoc
281: .addNewVariables();
282:
283: for (WfVariable variable : variables) {
284: VariableDocument.Variable variableXml = variablesXml
285: .addNewVariable();
286: variableXml.setName(variable.getName());
287: variableXml.setScope(ScopeType.Enum.forString(variable
288: .getScope().toString()));
289: setValue(variableXml, variable.getType(), variable
290: .getValue());
291: }
292:
293: return variablesDoc;
294: }
295: }
|