001: package org.enhydra.shark.utilities;
002:
003: import java.util.ArrayList;
004: import java.util.Arrays;
005: import java.util.HashMap;
006: import java.util.Iterator;
007: import java.util.List;
008: import java.util.Map;
009:
010: import org.enhydra.shark.api.client.wfmc.wapi.WMAttribute;
011: import org.enhydra.shark.api.client.wfmc.wapi.WMFilter;
012: import org.enhydra.shark.api.client.wfmc.wapi.WMSessionHandle;
013: import org.enhydra.shark.api.client.wfservice.NameValue;
014: import org.enhydra.shark.api.client.wfservice.WMEntity;
015: import org.enhydra.shark.api.client.wfservice.WMEntityIterator;
016: import org.enhydra.shark.api.client.wfservice.XPDLBrowser;
017:
018: /**
019: * The various utilities for WMEntity handling.
020: *
021: * @author Sasa Bojanic
022: */
023: public class WMEntityUtilities {
024:
025: /**
026: * Returns (double) string array of all extended attributes for the given entity. The
027: * first column of each row represents extended attribute's name attribute, and second
028: * column represents its value attribute.
029: */
030: public static String[][] getExtAttribNVPairs(WMSessionHandle sh,
031: XPDLBrowser xpdlb, WMEntity ent) throws Exception {
032:
033: WMFilter filter = new WMFilter("Type", WMFilter.EQ,
034: "ExtendedAttribute");
035: filter.setFilterType(XPDLBrowser.SIMPLE_TYPE_XPDL);
036:
037: WMEntity[] enti = xpdlb.listEntities(sh, ent, filter, true)
038: .getArray();
039: String[][] eaNVP = new String[enti.length][2];
040:
041: for (int i = 0; i < enti.length; i++) {
042: WMEntity ea = enti[i];
043: WMAttribute[] attrs = xpdlb.listAttributes(sh, ea, null,
044: true).getArray();
045: eaNVP[i][0] = attrs[0].getValue().toString();
046: eaNVP[i][1] = attrs[1].getValue().toString();
047: }
048:
049: return eaNVP;
050: }
051:
052: /**
053: * Searchs for the extended attribute for the specified entity with a given name. If
054: * attribute is found, the extended attribute's value attribute value is returned, and
055: * otherwise null is returned.
056: */
057: public static String findEAAndGetValue(WMSessionHandle sh,
058: XPDLBrowser xpdlb, WMEntity ent, String eaName)
059: throws Exception {
060:
061: WMFilter filter = new WMFilter("Type", WMFilter.EQ,
062: "ExtendedAttribute");
063: filter.setFilterType(XPDLBrowser.SIMPLE_TYPE_XPDL);
064:
065: WMEntity[] enti = xpdlb.listEntities(sh, ent, filter, true)
066: .getArray();
067:
068: for (int i = 0; i < enti.length; i++) {
069: WMEntity ea = enti[i];
070: WMAttribute[] attrs = xpdlb.listAttributes(sh, ea, null,
071: true).getArray();
072: if (attrs[0].getValue().toString().equals(eaName)) {
073: return attrs[1].getValue().toString();
074: }
075: }
076: return null;
077: }
078:
079: /**
080: * Retrieves all the Shark entitites representing uploaded XPDL Packages.
081: */
082: public static WMEntity[] getAllPackages(WMSessionHandle shandle,
083: XPDLBrowser xpdlb) throws Exception {
084: WMFilter filter = new WMFilter("Type", WMFilter.EQ, "Package");
085: filter.setFilterType(XPDLBrowser.SIMPLE_TYPE_XPDL);
086: WMEntityIterator ei = xpdlb.listEntities(shandle, null, filter,
087: true);
088: return ei.getArray();
089: }
090:
091: /**
092: * Retrieves all the Shark entitites representing workflow processes for the given
093: * Package entity.
094: */
095: public static WMEntity[] getAllWorkflowProcesses(
096: WMSessionHandle shandle, XPDLBrowser xpdlb, WMEntity pkg)
097: throws Exception {
098: WMFilter filter = new WMFilter("Type", WMFilter.EQ,
099: "WorkflowProcess");
100: filter.setFilterType(XPDLBrowser.SIMPLE_TYPE_XPDL);
101: WMEntityIterator ei = xpdlb.listEntities(shandle, pkg, filter,
102: true);
103: return ei.getArray();
104: }
105:
106: /**
107: * Retrieves all the Shark entitites representing activity sets for the given
108: * WorkflowProcess entity.
109: */
110: public static WMEntity[] getAllActivitySets(
111: WMSessionHandle shandle, XPDLBrowser xpdlb, WMEntity wp)
112: throws Exception {
113: WMFilter filter = new WMFilter("Type", WMFilter.EQ,
114: "ActivitySet");
115: filter.setFilterType(XPDLBrowser.SIMPLE_TYPE_XPDL);
116: WMEntityIterator ei = xpdlb.listEntities(shandle, wp, filter,
117: true);
118: return ei.getArray();
119: }
120:
121: /**
122: * Retrieves all the Shark entitites representing participants from all the packages
123: * and their workflow processes.
124: */
125: public static WMEntity[] getAllParticipants(
126: WMSessionHandle shandle, XPDLBrowser xpdlb)
127: throws Exception {
128: List partEntities = new ArrayList();
129: WMEntity[] pkgs = getAllPackages(shandle, xpdlb);
130: for (int i = 0; i < pkgs.length; i++) {
131: WMEntity[] pkgpars = getAllParticipants(shandle, xpdlb,
132: pkgs[i]);
133: partEntities.addAll(Arrays.asList(pkgpars));
134: WMEntity[] wps = getAllWorkflowProcesses(shandle, xpdlb,
135: pkgs[i]);
136: for (int j = 0; j < wps.length; j++) {
137: WMEntity[] wppars = getAllParticipants(shandle, xpdlb,
138: wps[j]);
139: partEntities.addAll(Arrays.asList(wppars));
140: }
141: }
142: WMEntity[] pars = new WMEntity[partEntities.size()];
143: partEntities.toArray(pars);
144: return pars;
145: }
146:
147: /**
148: * Retrieves all the Shark entitites representing participants from the given package
149: * or workflow process.
150: */
151: public static WMEntity[] getAllParticipants(
152: WMSessionHandle shandle, XPDLBrowser xpdlb, WMEntity pkgOrWp)
153: throws Exception {
154: WMFilter filter = new WMFilter("Type", WMFilter.EQ,
155: "Participant");
156: filter.setFilterType(XPDLBrowser.SIMPLE_TYPE_XPDL);
157: WMEntityIterator ei = xpdlb.listEntities(shandle, pkgOrWp,
158: filter, true);
159: return ei.getArray();
160: }
161:
162: /**
163: * Retrieves all the Shark entitites representing applications from all the packages
164: * and their workflow processes.
165: */
166: public static WMEntity[] getAllApplications(
167: WMSessionHandle shandle, XPDLBrowser xpdlb)
168: throws Exception {
169: List appEntities = new ArrayList();
170: WMEntity[] pkgs = getAllPackages(shandle, xpdlb);
171: for (int i = 0; i < pkgs.length; i++) {
172: WMEntity[] pkgpars = getAllApplications(shandle, xpdlb,
173: pkgs[i]);
174: appEntities.addAll(Arrays.asList(pkgpars));
175: WMEntity[] wps = getAllWorkflowProcesses(shandle, xpdlb,
176: pkgs[i]);
177: for (int j = 0; j < wps.length; j++) {
178: WMEntity[] wppars = getAllApplications(shandle, xpdlb,
179: wps[j]);
180: appEntities.addAll(Arrays.asList(wppars));
181: }
182: }
183: WMEntity[] apps = new WMEntity[appEntities.size()];
184: appEntities.toArray(apps);
185: return apps;
186: }
187:
188: /**
189: * Retrieves all the Shark entitites representing applications from the given package
190: * or workflow process.
191: */
192: public static WMEntity[] getAllApplications(
193: WMSessionHandle shandle, XPDLBrowser xpdlb, WMEntity pkgOrWp)
194: throws Exception {
195: WMFilter filter = new WMFilter("Type", WMFilter.EQ,
196: "Application");
197: filter.setFilterType(XPDLBrowser.SIMPLE_TYPE_XPDL);
198: WMEntityIterator ei = xpdlb.listEntities(shandle, pkgOrWp,
199: filter, true);
200: return ei.getArray();
201: }
202:
203: /**
204: * Retrieves all the Shark entitites representing activities from the given workflow
205: * process and their activity sets.
206: */
207: public static WMEntity[] getOverallActivities(
208: WMSessionHandle shandle, XPDLBrowser xpdlb, WMEntity wp)
209: throws Exception {
210: List actEntities = new ArrayList();
211: WMEntity[] pkgpars = getAllActivities(shandle, xpdlb, wp);
212: actEntities.addAll(Arrays.asList(pkgpars));
213: WMEntity[] ass = getAllActivitySets(shandle, xpdlb, wp);
214: for (int j = 0; j < ass.length; j++) {
215: WMEntity[] asacts = getAllActivities(shandle, xpdlb, ass[j]);
216: actEntities.addAll(Arrays.asList(asacts));
217: }
218: WMEntity[] acts = new WMEntity[actEntities.size()];
219: actEntities.toArray(acts);
220: return acts;
221: }
222:
223: /**
224: * Retrieves all the Shark entitites representing activities from the given workflow
225: * process or activity set.
226: */
227: public static WMEntity[] getAllActivities(WMSessionHandle shandle,
228: XPDLBrowser xpdlb, WMEntity wpOrAs) throws Exception {
229: WMFilter filter = new WMFilter("Type", WMFilter.EQ, "Activity");
230: filter.setFilterType(XPDLBrowser.SIMPLE_TYPE_XPDL);
231: WMEntityIterator ei = xpdlb.listEntities(shandle, wpOrAs,
232: filter, true);
233: return ei.getArray();
234: }
235:
236: /**
237: * Returns given entity's sub-entity referenced by the name or null if it does not
238: * exist.
239: */
240: public static WMEntity getSubEntity(WMSessionHandle shandle,
241: XPDLBrowser xpdlb, WMEntity ent, String subEntName)
242: throws Exception {
243: WMFilter filter = new WMFilter("Type", WMFilter.EQ, subEntName);
244: filter.setFilterType(XPDLBrowser.SIMPLE_TYPE_XPDL);
245:
246: WMEntity[] enti = xpdlb
247: .listEntities(shandle, ent, filter, true).getArray();
248:
249: if (enti != null && enti.length > 0) {
250: return enti[0];
251: }
252: return null;
253: }
254:
255: /**
256: * Returns given entity's attribute referenced by the name or null if it does not
257: * exist.
258: */
259: public static WMAttribute getAttribute(WMSessionHandle shandle,
260: XPDLBrowser xpdlb, WMEntity ent, String attrName)
261: throws Exception {
262: WMFilter filter = new WMFilter("Name", WMFilter.EQ, attrName);
263: filter.setFilterType(XPDLBrowser.SIMPLE_TYPE_XPDL);
264: WMAttribute[] attrs = xpdlb.listAttributes(shandle, ent,
265: filter, true).getArray();
266: if (attrs != null && attrs.length > 0) {
267: return attrs[0];
268: }
269: return null;
270: }
271:
272: /**
273: * Returns given entity's attribute value or null if it does not exist. Attribute name
274: * is specified as parameter.
275: */
276: public static String getAttributeValue(WMSessionHandle shandle,
277: XPDLBrowser xpdlb, WMEntity ent, String attrName)
278: throws Exception {
279: WMAttribute attr = getAttribute(shandle, xpdlb, ent, attrName);
280: if (attr != null) {
281: return attr.getValue().toString();
282: }
283: return null;
284: }
285:
286: /**
287: * Converts array of WMAttribute objects to a java Map where keys are WMAttribute names
288: * (variable Ids) and values are attribute/variable values.
289: */
290: public static Map getMapFromWMAttributeArray(WMAttribute[] attrs)
291: throws Exception {
292: Map m = null;
293: if (attrs != null) {
294: m = new HashMap();
295: for (int i = 0; i < attrs.length; i++) {
296: m.put(attrs[i].getName(), attrs[i].getValue());
297: }
298: }
299: return m;
300: }
301:
302: /**
303: * Converts variable Map to array of WMAttribute objects. Map keys become WMAttribute
304: * names and values WMAttribute values. Context signature of the corresponding process
305: * definition is mandatory parameter which can't be null.
306: */
307: public static WMAttribute[] getWMAttributeArrayFromMap(Map context,
308: Map contextSignature) throws Exception {
309: WMAttribute[] attrs = null;
310: if (context != null) {
311: attrs = new WMAttribute[context.size()];
312: int c = 0;
313: for (Iterator iter = context.entrySet().iterator(); iter
314: .hasNext(); c++) {
315: Map.Entry element = (Map.Entry) iter.next();
316: String elementName = (String) element.getKey();
317: attrs[c] = new WMAttribute();
318: attrs[c].setName(elementName);
319: attrs[c].setValue(element.getValue());
320: int type = WMAttribute.EXTERNAL_REFERENCE_TYPE;
321: Object elementType = contextSignature.get(elementName);
322: if (elementType != null) {
323: if (elementType.equals("java.lang.Long")) {
324: type = WMAttribute.INTEGER_TYPE;
325: } else if (elementType.equals("java.lang.Boolean")) {
326: type = WMAttribute.BOOLEAN_TYPE;
327: } else if (elementType.equals("java.lang.Double")) {
328: type = WMAttribute.FLOAT_TYPE;
329: } else if (elementType.equals("java.lang.String")) {
330: type = WMAttribute.STRING_TYPE;
331: } else if (elementType.equals("java.util.Date")) {
332: type = WMAttribute.DATETIME_TYPE;
333: } else if (elementType.equals("org.w3c.dom.Node")) {
334: type = WMAttribute.SCHEMA_TYPE;
335: }
336: }
337: attrs[c].setType(type);
338: }
339: }
340: return attrs;
341: }
342:
343: public static NameValue[] getNameValueArrayFromMap(Map context)
344: throws Exception {
345: NameValue[] nvarr = null;
346: if (context != null) {
347: nvarr = new NameValue[context.size()];
348: int c = 0;
349: for (Iterator iter = context.entrySet().iterator(); iter
350: .hasNext(); c++) {
351: Map.Entry element = (Map.Entry) iter.next();
352: String elementName = (String) element.getKey();
353: nvarr[c] = new NameValue(elementName, element
354: .getValue());
355: }
356: }
357: return nvarr;
358: }
359:
360: }
|