001: /*
002: * $Id: BPELElementFactory.java,v 1.13 2004/12/09 12:34:14 kowap Exp $
003: *
004: * Copyright (c) 2004 Patric Fornasier, Pawel Kowalski
005: * Berne University of Applied Sciences
006: * School of Engineering and Information Technology
007: * All rights reserved.
008: */
009: package bexee.model.xmltobpel;
010:
011: import java.util.Hashtable;
012: import java.util.Map;
013:
014: import javax.xml.namespace.QName;
015:
016: import bexee.model.StandardAttributes;
017: import bexee.model.activity.Assign;
018: import bexee.model.activity.Invoke;
019: import bexee.model.activity.Receive;
020: import bexee.model.activity.Reply;
021: import bexee.model.activity.Sequence;
022: import bexee.model.activity.Switch;
023: import bexee.model.activity.impl.AssignImpl;
024: import bexee.model.activity.impl.InvokeImpl;
025: import bexee.model.activity.impl.ReceiveImpl;
026: import bexee.model.activity.impl.ReplyImpl;
027: import bexee.model.activity.impl.SequenceImpl;
028: import bexee.model.activity.impl.SwitchImpl;
029: import bexee.model.elements.BpelCase;
030: import bexee.model.elements.Correlation;
031: import bexee.model.elements.CorrelationPattern;
032: import bexee.model.elements.From;
033: import bexee.model.elements.PartnerLink;
034: import bexee.model.elements.To;
035: import bexee.model.elements.Variable;
036: import bexee.model.elements.Variables;
037: import bexee.model.elements.impl.BpelCaseImpl;
038: import bexee.model.elements.impl.CorrelationImpl;
039: import bexee.model.elements.impl.CorrelationPatternImpl;
040: import bexee.model.elements.impl.FromImpl;
041: import bexee.model.elements.impl.PartnerLinkImpl;
042: import bexee.model.elements.impl.RolesImpl;
043: import bexee.model.elements.impl.ToImpl;
044: import bexee.model.elements.impl.VariableImpl;
045: import bexee.model.elements.impl.VariablesImpl;
046: import bexee.model.expression.impl.BooleanExpressionImpl;
047: import bexee.model.process.BPELProcess;
048: import bexee.model.process.Process;
049: import bexee.model.process.impl.ProcessImpl;
050: import bexee.util.BooleanUtils;
051: import bexee.util.StringUtils;
052:
053: /**
054: * This is a factory for the creation of BPEL process elements. The
055: *
056: * @author Pawel Kowalski
057: * @version $Revision: 1.13 $, $Date: 2004/12/09 12:34:14 $
058: */
059: public class BPELElementFactory {
060:
061: //**************************************************/
062: // instances accessor
063: //**************************************************/
064:
065: private static Map instances = new Hashtable();
066:
067: /**
068: * Create a new factory instance or if one already exists for this
069: * BPELProcessm, retrieve the existing one.
070: *
071: * @param process
072: * @return
073: */
074: public static BPELElementFactory getInstance(BPELProcess process) {
075: if (instances.get(process) == null) {
076: BPELElementFactory elementFactory = new BPELElementFactory();
077: instances.put(process, elementFactory);
078: }
079: return (BPELElementFactory) instances.get(process);
080: }
081:
082: //**************************************************/
083: // the element factory
084: //**************************************************/
085:
086: private Map variables;
087:
088: private Map partnerLinks;
089:
090: private Map nameSpaces;
091:
092: //**************************************************/
093: // ctor's
094: //**************************************************/
095:
096: /**
097: * Create a BPELElementFactory object and initialize it.
098: */
099: public BPELElementFactory() {
100: variables = new Hashtable();
101: partnerLinks = new Hashtable();
102: nameSpaces = new Hashtable();
103: }
104:
105: //**************************************************/
106: // create methods
107: //**************************************************/
108:
109: //**************************************************/
110: // create process
111: //**************************************************/
112:
113: /**
114: * Create a Process instance with all possible attributes. Default values
115: * will be set when an attribute is empty or null.
116: *
117: * @param name
118: * @param targetNamespace
119: * @param queryLanguage
120: * @param expressionLanguage
121: * @param suppressJoinFailure
122: * @param enableInstanceCompensation
123: * @param abstractProcess
124: * @param xmlns
125: * @param nameSpaces
126: * @return
127: */
128: public Process createProcess(String name, String targetNamespace,
129: String queryLanguage, String expressionLanguage,
130: String suppressJoinFailure,
131: String enableInstanceCompensation, String abstractProcess,
132: String xmlns, Map nameSpaces) {
133:
134: Process process = new ProcessImpl(name, targetNamespace,
135: queryLanguage, expressionLanguage, suppressJoinFailure,
136: enableInstanceCompensation);
137:
138: if (nameSpaces != null) {
139: this .nameSpaces = nameSpaces;
140: }
141:
142: return process;
143: }
144:
145: //**************************************************/
146: // create process elements
147: //**************************************************/
148:
149: /**
150: * Create a PartnerLink with all possible attributes. Register the created
151: * PartnerLink for retrieval by an Activity using PartnerLinks.
152: *
153: * @param partnerLinkName
154: * @param partnerLinkType
155: * @param myRole
156: * @param partnerRole
157: * @return
158: */
159: public PartnerLink createPartnerLink(String partnerLinkName,
160: String partnerLinkType, String myRole, String partnerRole) {
161:
162: PartnerLink partnerLink = new PartnerLinkImpl();
163:
164: partnerLink.setName(partnerLinkName);
165: partnerLink.setPartnerLinkType(expandToQName(partnerLinkType));
166: partnerLink.setMyRole(myRole);
167: partnerLink.setPartnerRole(partnerRole);
168:
169: partnerLinks.put(partnerLinkName, partnerLink);
170:
171: return partnerLink;
172: }
173:
174: /**
175: * Describe <code>createVariables</code> method here.
176: *
177: * @return
178: */
179: public Variables createVariables() {
180: return new VariablesImpl();
181: }
182:
183: /**
184: * Create a Variable with all possible attributes. Register the created
185: * Variable for retrieval by an invoke or receive element.
186: *
187: * @param name
188: * @param messageType
189: * @param type
190: * @param element
191: * @return a <code>Variable</code> value
192: */
193: public Variable createVariable(String name, String messageType,
194: String type, String element) {
195:
196: Variable variable = new VariableImpl();
197:
198: variable.setName(name);
199: variable.setMessageType(expandToQName(messageType));
200: variable.setType(expandToQName(type));
201: variable.setElement(expandToQName(element));
202:
203: variables.put(name, variable);
204:
205: return variable;
206: }
207:
208: /**
209: * Create a Correlation with all parameters. If the pattern parameter is
210: * null or empty, no CorrelationPattern object will be set.
211: *
212: * @param set
213: * @param initiate
214: * @param pattern
215: * @return
216: */
217: public Object createCorrelation(String set, String initiate,
218: String pattern) {
219: Correlation correlation = new CorrelationImpl();
220: correlation.setSet(set);
221: correlation.setInitiate(BooleanUtils.yesNoToBoolean(initiate));
222: if (StringUtils.isNullOrEmpty(pattern)) {
223: correlation.setPattern(createCorrelationPattern(pattern));
224: }
225: return correlation;
226: }
227:
228: /**
229: * Create a CorrelationPattern with a patternString.
230: *
231: * @param patternString
232: * a <code>String</code> value
233: * @return a CorrelationPattern object
234: */
235: public CorrelationPattern createCorrelationPattern(
236: String patternString) {
237: CorrelationPattern pattern = new CorrelationPatternImpl();
238: pattern.setCorrelationString(patternString);
239: return pattern;
240: }
241:
242: //**************************************************/
243: // create activities
244: //**************************************************/
245:
246: /**
247: * Describe <code>createAssign</code> method here.
248: *
249: * @param standardAttributes
250: * @return assign BPEL activity
251: */
252: public Assign createAssign(StandardAttributes standardAttributes) {
253: return new AssignImpl(standardAttributes);
254: }
255:
256: /**
257: * Create a new Sequence BPEL activity.
258: *
259: * @param standardAttributes
260: * a <code>StandardAttributes</code> value
261: * @return Sequence activity
262: */
263: public Sequence createSequence(StandardAttributes standardAttributes) {
264: return new SequenceImpl(standardAttributes);
265: }
266:
267: /**
268: * Create a Receive Activity with all possible attributes. A Receive created
269: * this way will be linked with an already defined existing Variables.
270: *
271: * @param standardAttributes
272: * a <code>StandardAttributes</code> value
273: * @param partnerLinkName
274: * a <code>String</code> value
275: * @param portTypeString
276: * a <code>String</code> value
277: * @param operation
278: * @param variableName
279: * a <code>String</code> value
280: * @param createInstance
281: * @return Receive activity
282: */
283: public Receive createReceive(StandardAttributes standardAttributes,
284: String partnerLinkName, String portTypeString,
285: String operation, String variableName, String createInstance) {
286:
287: PartnerLink partnerLink = retrievePartnerLink(partnerLinkName);
288: QName portType = expandToQName(portTypeString);
289: Variable variable = retrieveVariable(variableName);
290:
291: Receive receive = new ReceiveImpl(standardAttributes,
292: partnerLink, portType, operation, variable,
293: createInstance);
294:
295: return receive;
296: }
297:
298: /**
299: * Create an Invoke Activity with all possible attributes. An Invoke created
300: * this way will be linked with already defined and existing Variables and
301: * PartnerLinks.
302: *
303: * @param standardAttributes
304: * a <code>StandardAttributes</code> value
305: * @param partnerLinkName
306: * a <code>String</code> value
307: * @param portTypeString
308: * a <code>String</code> value
309: * @param operation
310: * @param inVariableName
311: * a <code>String</code> value
312: * @param outVariableName
313: * a <code>String</code> value
314: * @return
315: */
316: public Object createInvoke(StandardAttributes standardAttributes,
317: String partnerLinkName, String portTypeString,
318: String operation, String inVariableName,
319: String outVariableName) {
320:
321: PartnerLink partnerLink = retrievePartnerLink(partnerLinkName);
322: QName portType = expandToQName(portTypeString);
323: Variable inVariable = retrieveVariable(inVariableName);
324: Variable outVariable = retrieveVariable(outVariableName);
325:
326: Invoke invoke = new InvokeImpl(standardAttributes, partnerLink,
327: portType, operation, inVariable, outVariable);
328:
329: return invoke;
330: }
331:
332: /**
333: * Describe <code>createReply</code> method here.
334: *
335: * @param attributes
336: * @param partnerLink
337: * @param portType
338: * @param operation
339: * @param variable
340: * @param faultName
341: * @return
342: */
343: public Reply createReply(StandardAttributes attributes,
344: String partnerLink, String portType, String operation,
345: String variable, String faultName) {
346:
347: Reply reply = new ReplyImpl(attributes);
348:
349: reply.setPartnerLink(retrievePartnerLink(partnerLink));
350: reply.setPortType(expandToQName(portType));
351: reply.setOperation(operation);
352: reply.setVariable(retrieveVariable(variable));
353: reply.setFaultName(expandToQName(faultName));
354:
355: return reply;
356: }
357:
358: /**
359: * Describe <code>createSwitch</code> method here.
360: *
361: * @param standardAttributes
362: * @return
363: */
364: public Object createSwitch(StandardAttributes standardAttributes) {
365: Switch bpelSwitch = new SwitchImpl(standardAttributes);
366: return bpelSwitch;
367: }
368:
369: //**************************************************/
370: // create elements
371: //**************************************************/
372:
373: /**
374: * Create a From with all possible attributes. Variables and PartnerLinks
375: * will not be created but retrieved from the enclosing process instead.
376: *
377: * @param variable
378: * @param part
379: * @param property
380: * @param partnerLink
381: * @param endpointReference
382: * @param expression
383: * @param opaque
384: * @return
385: */
386: public From createFrom(String variable, String part,
387: String property, String partnerLink,
388: String endpointReference, String expression, String opaque) {
389:
390: From from = new FromImpl();
391:
392: from.setVariable(retrieveVariable(variable));
393: from.setPart(part);
394: from.setProperty(expandToQName(property));
395: from.setPartnerLink(retrievePartnerLink(partnerLink));
396: from.setEndpointReference(new RolesImpl(endpointReference));
397: from.setExpression(expression);
398: from.setOpaque(BooleanUtils.yesNoToBoolean(opaque));
399:
400: return from;
401: }
402:
403: /**
404: * Create a To with all possible attributes. Variables and PartnerLinks will
405: * not be created but retrieved from the enclosing process instead.
406: *
407: * @param variable
408: * @param part
409: * @param property
410: * @param partnerLink
411: * @return
412: */
413: public To createTo(String variable, String part, String property,
414: String partnerLink) {
415:
416: To to = new ToImpl();
417:
418: to.setVariable(retrieveVariable(variable));
419: to.setPart(part);
420: to.setProperty(expandToQName(property));
421: to.setPartnerLink(retrievePartnerLink(partnerLink));
422:
423: return to;
424: }
425:
426: /**
427: * Create a new BpelCase for the use within a Switch activity. Create this
428: * BpelCase with a boolean condition used for selecting the BpelCase among
429: * all cases within a Switch.
430: *
431: * @param condition
432: * @return
433: */
434: public BpelCase createBpelCase(String condition) {
435: BpelCase bpelCase = new BpelCaseImpl();
436: bpelCase.setBooleanExpression(new BooleanExpressionImpl(
437: condition));
438: return bpelCase;
439: }
440:
441: //**************************************************/
442: // retrieve methods
443: //**************************************************/
444:
445: /**
446: * Retrieve an already created PartnerLink for an Activity using
447: * partnerLinks. If the partnerLinkName is null, empty or a PartnerLink with
448: * this name has not been created, this method will return a null value.
449: *
450: * @param partnerLinkName
451: * the name of the PartnerLink to be retrieved.
452: * @return a PartnerLink or null.
453: */
454: public PartnerLink retrievePartnerLink(String partnerLinkName) {
455: if (StringUtils.isNullOrEmpty(partnerLinkName)) {
456: return null;
457: }
458: return (PartnerLink) partnerLinks.get(partnerLinkName);
459: }
460:
461: /**
462: * Retrieve an already created Variable for an Activity using variables. If
463: * the variableName parameter is null, empty or the Variable has not been
464: * created, this method will return null value.
465: *
466: * @param variableName
467: * the name of the Variable to be retrieved.
468: * @return a Variable or null if the variableName is null, empty or a
469: * Variable with this name has not yet been created.
470: */
471: public Variable retrieveVariable(String variableName) {
472: if (StringUtils.isNullOrEmpty(variableName)) {
473: return null;
474: }
475: return (Variable) variables.get(variableName);
476: }
477:
478: //**************************************************/
479: // helper methods
480: //**************************************************/
481:
482: /**
483: * @param qName
484: * @return
485: */
486: private QName expandToQName(String qName) {
487: String[] parts = StringUtils.split(qName);
488: String namespaceUri;
489: if (nameSpaces.get(parts[0]) == null) {
490: namespaceUri = "";
491: } else {
492: namespaceUri = (String) nameSpaces.get(parts[0]);
493: }
494: return new QName(namespaceUri, parts[1]);
495: }
496:
497: }
|