001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl;
016:
017: import java.util.ArrayList;
018: import java.util.Arrays;
019: import java.util.HashSet;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Set;
023:
024: import javax.xml.bind.JAXBException;
025:
026: import com.metaboss.enterprise.ps.PSAttributeConstraintViolationException;
027: import com.metaboss.enterprise.ps.PSDataSourceOperationInvocationException;
028: import com.metaboss.enterprise.ps.PSException;
029: import com.metaboss.enterprise.ps.PSIllegalArgumentException;
030: import com.metaboss.enterprise.ps.PSUnexpectedProgramConditionException;
031: import com.metaboss.sdlctools.domains.enterprisemodel.storage.PSEntity;
032: import com.metaboss.sdlctools.domains.enterprisemodel.storage.STAssociationRole;
033: import com.metaboss.sdlctools.domains.enterprisemodel.storage.STAttribute;
034: import com.metaboss.sdlctools.domains.enterprisemodel.storage.STConstraint;
035: import com.metaboss.sdlctools.domains.enterprisemodel.storage.STEntity;
036: import com.metaboss.sdlctools.domains.enterprisemodel.storage.STField;
037: import com.metaboss.sdlctools.domains.enterprisemodel.storage.STSelector;
038: import com.metaboss.sdlctools.domains.enterprisemodel.storage.STState;
039: import com.metaboss.sdlctools.domains.enterprisemodel.storage.STStateTransition;
040: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.AssociationRoleRefListType;
041: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.AttributeDefListType;
042: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.AttributeDefType;
043: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.ConstraintDefListType;
044: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.ConstraintDefType;
045: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.EntityDefType;
046: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.FieldDefType;
047: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.InputFieldDefListType;
048: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.PrimaryKeyType;
049: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.SelectorDefListType;
050: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.SelectorDefType;
051: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.StateDefType;
052: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.StateMachineType;
053: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.TransitionDefType;
054: import com.metaboss.sdlctools.types.enterprisemodel.AttributeStereotype;
055:
056: public class PSEntityImpl implements PSEntity {
057: /** Returns details entity corresponding to given reference or null struct if definition not found */
058: public STEntity getEntity(String pEntityRef) throws PSException {
059: EntityDefType lEntityDef = Storage.getEntity(pEntityRef);
060: if (lEntityDef == null)
061: return null;
062: STEntity lStruct = new STEntity();
063: lStruct.EntityRef = lEntityDef.getEntityRef();
064: lStruct.Description = lEntityDef.getDescription();
065: lStruct.PluralName = lEntityDef.getPluralName();
066: lStruct.Stereotype = com.metaboss.sdlctools.types.enterprisemodel.EntityStereotypeTextHelper
067: .fromTextObject(lEntityDef.getEntityStereotype());
068: lStruct.IsAbstract = lEntityDef.isIsAbstract();
069: lStruct.IsFinal = lEntityDef.isIsFinal();
070: lStruct.SupertypeEntityRef = lEntityDef.getSupertypeEntityRef();
071: // Good spot to check constraints
072: validateSTEntityAttributes(lStruct);
073: // If we have a supertype - we will run a few tests ensuring that our entity
074: // does not override attributes and associations of the supertype. This involves
075: // recursive call to getEntity() for the parent entity. This means that by getting an entity
076: // we validate all the way up the inheritance tree. We are not validating down the tree
077: // just to make things simpler
078: if (lStruct.SupertypeEntityRef != null
079: && lStruct.SupertypeEntityRef.length() != 0) {
080: Set lSupertypes = new HashSet();
081: lSupertypes.add(pEntityRef);
082: for (String lSupertypeRef = lStruct.SupertypeEntityRef; lSupertypeRef != null
083: && lSupertypeRef.length() != 0;) {
084: EntityDefType lSupertypeEntityDef = Storage
085: .getEntity(lSupertypeRef);
086: if (lSupertypeEntityDef == null)
087: throw new PSUnexpectedProgramConditionException(
088: "Undefined supertype entity in the inheritance hierearchy. EntityRef: "
089: + pEntityRef
090: + " SupertypeEntityRef: "
091: + lSupertypeRef);
092: if (!lSupertypes
093: .add(lSupertypeEntityDef.getEntityRef()))
094: throw new PSUnexpectedProgramConditionException(
095: "Circularity in the inheritance hierarchy is not allowed. EntityRef : "
096: + pEntityRef);
097: if (lSupertypeEntityDef.isIsFinal())
098: throw new PSUnexpectedProgramConditionException(
099: "Entity definition contains reference to supertype entity, which is final anf therefore can not have subtypes. EntityRef: "
100: + pEntityRef
101: + " SupertypeEntityRef: "
102: + lSupertypeRef);
103: lSupertypeRef = lSupertypeEntityDef
104: .getSupertypeEntityRef();
105: }
106: }
107: return lStruct;
108: }
109:
110: /** Inserts new entity record into the database */
111: public void insertEntity(STEntity pRecord) throws PSException {
112: // Good spot to check attribute constraints
113: validateSTEntityAttributes(pRecord);
114: try {
115: EntityDefType lEntityDef = Util.getObjectFactory()
116: .createEntityDef();
117: lEntityDef.setEntityRef(pRecord.EntityRef);
118: lEntityDef.setDescription(pRecord.Description);
119: lEntityDef.setPluralName(pRecord.PluralName);
120: lEntityDef
121: .setEntityStereotype(com.metaboss.sdlctools.types.enterprisemodel.EntityStereotypeTextHelper
122: .toTextObject(pRecord.Stereotype));
123: lEntityDef.setIsAbstract(pRecord.IsAbstract);
124: lEntityDef.setIsFinal(pRecord.IsFinal);
125: if (pRecord.SupertypeEntityRef != null
126: && pRecord.SupertypeEntityRef.length() > 0)
127: lEntityDef
128: .setSupertypeEntityRef(pRecord.SupertypeEntityRef);
129: AttributeDefListType lAttributeDefList = Util
130: .getObjectFactory().createAttributeDefList();
131: lEntityDef.setAttributeDefList(lAttributeDefList);
132: AssociationRoleRefListType lAssociationRoleRefList = Util
133: .getObjectFactory().createAssociationRoleRefList();
134: lEntityDef
135: .setAssociationRoleRefList(lAssociationRoleRefList);
136: StateMachineType lStateMachine = Util.getObjectFactory()
137: .createStateMachine();
138: lEntityDef.setStateMachine(lStateMachine);
139: PrimaryKeyType lPrimaryKey = Util.getObjectFactory()
140: .createPrimaryKey();
141: lEntityDef.setPrimaryKey(lPrimaryKey);
142: SelectorDefListType lSelectorDefList = Util
143: .getObjectFactory().createSelectorDefList();
144: lEntityDef.setSelectorDefList(lSelectorDefList);
145: Storage.insertEntity(lEntityDef);
146: } catch (JAXBException e) {
147: throw new PSDataSourceOperationInvocationException(
148: "Unable to insert new Entity", e);
149: }
150: }
151:
152: /** Updates entity details in the database */
153: public void updateEntity(STEntity pUpdatedRecord)
154: throws PSException {
155: // Good spot to check attribute constraints
156: validateSTEntityAttributes(pUpdatedRecord);
157: // Retrieve and update the entity
158: EntityDefType lEntityDef = Storage
159: .getEntity(pUpdatedRecord.EntityRef);
160: if (lEntityDef == null)
161: throw new PSIllegalArgumentException(
162: "Entity not found. EntityRef : "
163: + pUpdatedRecord.EntityRef);
164: lEntityDef.setDescription(pUpdatedRecord.Description);
165: lEntityDef.setPluralName(pUpdatedRecord.PluralName);
166: lEntityDef
167: .setEntityStereotype(com.metaboss.sdlctools.types.enterprisemodel.EntityStereotypeTextHelper
168: .toTextObject(pUpdatedRecord.Stereotype));
169: lEntityDef.setIsAbstract(pUpdatedRecord.IsAbstract);
170: lEntityDef.setIsFinal(pUpdatedRecord.IsFinal);
171: if (pUpdatedRecord.SupertypeEntityRef != null
172: && pUpdatedRecord.SupertypeEntityRef.length() > 0)
173: lEntityDef
174: .setSupertypeEntityRef(pUpdatedRecord.SupertypeEntityRef);
175: else
176: lEntityDef.setSupertypeEntityRef(null);
177: Storage.updateEntity(lEntityDef);
178: }
179:
180: /** Deletes existing entity */
181: public void deleteEntity(String pEntityRef) throws PSException {
182: Storage.deleteEntity(pEntityRef);
183: }
184:
185: /* Returns names of elements (atributes or relationships) comprising entity's primary key */
186: public String[] getPrimaryKey(String pEntityRef) throws PSException {
187: EntityDefType lEntityDef = Storage.getEntity(pEntityRef);
188: if (lEntityDef == null)
189: return null;
190: String[] lPrimaryKeyElementsNames = (String[]) lEntityDef
191: .getPrimaryKey().getElementName().toArray(
192: new String[lEntityDef.getPrimaryKey()
193: .getElementName().size()]);
194: // Good spot to check on validity of primary key entries
195: if (lPrimaryKeyElementsNames.length > 0) {
196: STAttribute[] lAttributes = getCombinedAttributes(pEntityRef);
197: String[] lRoleRefs = getCombinedAssociationRoleRefs(pEntityRef);
198: for (int i = 0; i < lPrimaryKeyElementsNames.length; i++) {
199: String lPrimaryKeyElementName = lPrimaryKeyElementsNames[i];
200: boolean lMatch = false;
201: // First look in attributes
202: for (int j = 0; j < lAttributes.length
203: && lMatch == false; j++) {
204: STAttribute lAttribute = lAttributes[j];
205: if ((lMatch = lAttribute.Name
206: .equals(lPrimaryKeyElementName)) == true) {
207: if ((!lAttribute.Stereotype
208: .equals(AttributeStereotype.MANDATORY))
209: && (!lAttribute.Stereotype
210: .equals(AttributeStereotype.CREATE_STAMP)))
211: throw new PSUnexpectedProgramConditionException(
212: "Attribute '"
213: + lAttribute.Name
214: + "' can not be a part of the primary key as it is neither mandatory nor create stamp. EntityRef : "
215: + pEntityRef);
216: }
217: }
218: // Go through associations if necessary
219: for (int j = 0; j < lRoleRefs.length && lMatch == false; j++) {
220: String lRoleRef = lRoleRefs[j];
221: if ((lMatch = lRoleRef.endsWith("."
222: + lPrimaryKeyElementName)) == true) {
223: STAssociationRole lRoleDetails = new PSAssociationRoleImpl()
224: .getAssociationRole(lRoleRef);
225: if (lRoleDetails == null)
226: throw new PSUnexpectedProgramConditionException(
227: "Role '"
228: + lRoleRef
229: + "' is not found in the model.");
230:
231: if ((!lRoleDetails.Cardinality.isMandatory())
232: || (!lRoleDetails.Cardinality
233: .isSingular()))
234: throw new PSUnexpectedProgramConditionException(
235: "Role '"
236: + lRoleRef
237: + "' can not be a part of the primary key as its cardinality is singular and not mandatory. EntityRef : "
238: + pEntityRef);
239: }
240: }
241: if (!lMatch)
242: throw new PSUnexpectedProgramConditionException(
243: "Unable to match primary key element '"
244: + lPrimaryKeyElementName
245: + "' to any association role or attribute. EntityRef : "
246: + pEntityRef);
247: }
248: }
249: return lPrimaryKeyElementsNames;
250: }
251:
252: /* Returns all references to the associations of this entity */
253: public String[] getAssociationRoleRefs(String pEntityRef)
254: throws PSException {
255: EntityDefType lEntityDef = Storage.getEntity(pEntityRef);
256: if (lEntityDef == null)
257: return null;
258: return (String[]) lEntityDef.getAssociationRoleRefList()
259: .getAssociationRoleRef().toArray(
260: new String[lEntityDef
261: .getAssociationRoleRefList()
262: .getAssociationRoleRef().size()]);
263: }
264:
265: /* Updates all references to the association roles of this entity. */
266: public void updateAssociationRoleRefs(String pEntityRef,
267: String[] pAssociationRoleRefs) throws PSException {
268: EntityDefType lEntityDef = Storage.getEntity(pEntityRef);
269: if (lEntityDef == null)
270: throw new PSIllegalArgumentException(
271: "Entity not found. EntityRef : " + pEntityRef);
272: List lAssociationRoleRefList = lEntityDef
273: .getAssociationRoleRefList().getAssociationRoleRef();
274: lAssociationRoleRefList.clear();
275: lAssociationRoleRefList.addAll(Arrays
276: .asList(pAssociationRoleRefs));
277: Storage.updateEntity(lEntityDef);
278: }
279:
280: /* Returns all references to the associations of this entity */
281: private String[] getCombinedAssociationRoleRefs(String pEntityRef)
282: throws PSException {
283: EntityDefType lEntityDef = Storage.getEntity(pEntityRef);
284: if (lEntityDef == null)
285: return null;
286: if (lEntityDef.getSupertypeEntityRef() != null
287: && lEntityDef.getSupertypeEntityRef().length() > 0) {
288: ArrayList lCombinedList = new ArrayList(lEntityDef
289: .getAssociationRoleRefList()
290: .getAssociationRoleRef());
291: lCombinedList.addAll(Arrays
292: .asList(getCombinedAssociationRoleRefs(lEntityDef
293: .getSupertypeEntityRef())));
294: return (String[]) lCombinedList
295: .toArray(new String[lCombinedList.size()]);
296: } else
297: return (String[]) lEntityDef.getAssociationRoleRefList()
298: .getAssociationRoleRef().toArray(
299: new String[lEntityDef
300: .getAssociationRoleRefList()
301: .getAssociationRoleRef().size()]);
302: }
303:
304: /* Returns all attributes comprising entity */
305: private STAttribute[] getCombinedAttributes(String pEntityRef)
306: throws PSException {
307: EntityDefType lEntityDef = Storage.getEntity(pEntityRef);
308: if (lEntityDef == null)
309: return null;
310: ArrayList lResultAttributeList = new ArrayList();
311: AttributeDefListType lAttributeDefList = lEntityDef
312: .getAttributeDefList();
313: Iterator lIter = lAttributeDefList.getAttributeDef().iterator();
314: while (lIter.hasNext())
315: lResultAttributeList.add(convertAttributeDef(pEntityRef,
316: (AttributeDefType) lIter.next()));
317: if (lEntityDef.getSupertypeEntityRef() != null
318: && lEntityDef.getSupertypeEntityRef().length() > 0)
319: lResultAttributeList.addAll(Arrays
320: .asList(getCombinedAttributes(lEntityDef
321: .getSupertypeEntityRef())));
322: return (STAttribute[]) lResultAttributeList
323: .toArray(new STAttribute[lResultAttributeList.size()]);
324: }
325:
326: /* Returns single attribute with the specified name from specified entity */
327: public STAttribute getAttribute(String pEntityRef,
328: String pAttributeName) throws PSException {
329: EntityDefType lEntityDef = Storage.getEntity(pEntityRef);
330: if (lEntityDef == null)
331: return null;
332: AttributeDefListType lAttributeDefList = lEntityDef
333: .getAttributeDefList();
334: Iterator lIter = lAttributeDefList.getAttributeDef().iterator();
335: for (int i = 0; lIter.hasNext(); i++) {
336: AttributeDefType lAttrDef = (AttributeDefType) lIter.next();
337: if (lAttrDef.getName().equals(pAttributeName))
338: return convertAttributeDef(pEntityRef, lAttrDef);
339: }
340: return null;
341: }
342:
343: /* Returns all attributes comprising entity */
344: public STAttribute[] getAttributes(String pEntityRef)
345: throws PSException {
346: EntityDefType lEntityDef = Storage.getEntity(pEntityRef);
347: if (lEntityDef == null)
348: return null;
349: AttributeDefListType lAttributeDefList = lEntityDef
350: .getAttributeDefList();
351: STAttribute[] lReturn = new STAttribute[lAttributeDefList
352: .getAttributeDef().size()];
353: Iterator lIter = lAttributeDefList.getAttributeDef().iterator();
354: for (int i = 0; lIter.hasNext(); i++) {
355: lReturn[i] = convertAttributeDef(pEntityRef,
356: (AttributeDefType) lIter.next());
357: }
358: return lReturn;
359: }
360:
361: /** Creates attribute in the specified entity. */
362: public void insertAttribute(String pEntityRef,
363: STAttribute pAttribute) throws PSException {
364: try {
365: EntityDefType lEntityDef = Storage.getEntity(pEntityRef);
366: if (lEntityDef == null)
367: throw new PSIllegalArgumentException(
368: "Entity not found. EntityRef : " + pEntityRef);
369: List lAttributeDefList = lEntityDef.getAttributeDefList()
370: .getAttributeDef();
371: lAttributeDefList.add(convertSTAttribute(pEntityRef,
372: pAttribute));
373: Storage.updateEntity(lEntityDef);
374: } catch (JAXBException e) {
375: throw new PSDataSourceOperationInvocationException(
376: "Unable to update Atributes for the entity", e);
377: }
378: }
379:
380: /** Deletes attribute in the specified entity. */
381: public void deleteAttribute(String pEntityRef, String pAttributeName)
382: throws PSException {
383: EntityDefType lEntityDef = Storage.getEntity(pEntityRef);
384: if (lEntityDef == null)
385: throw new PSIllegalArgumentException(
386: "Entity not found. EntityRef : " + pEntityRef);
387: List lAttributeDefList = lEntityDef.getAttributeDefList()
388: .getAttributeDef();
389: int lElementPos = 0;
390: for (Iterator lIter = lAttributeDefList.iterator(); lIter
391: .hasNext(); lElementPos++) {
392: AttributeDefType lAttributeDef = (AttributeDefType) lIter
393: .next();
394: if (lAttributeDef.getName().equals(pAttributeName))
395: break;
396: }
397: if (lElementPos >= lAttributeDefList.size())
398: throw new PSIllegalArgumentException(
399: "Atrribute not found. EntityRef: " + pEntityRef
400: + " AttributeName: " + pAttributeName);
401: lAttributeDefList.remove(lElementPos);
402: Storage.updateEntity(lEntityDef);
403: }
404:
405: /* Updates attribute in the specified entity. */
406: public void updateAttribute(String pEntityRef,
407: STAttribute pAttribute) throws PSException {
408: try {
409: EntityDefType lEntityDef = Storage.getEntity(pEntityRef);
410: if (lEntityDef == null)
411: throw new PSIllegalArgumentException(
412: "Entity not found. EntityRef : " + pEntityRef);
413: List lAttributeDefList = lEntityDef.getAttributeDefList()
414: .getAttributeDef();
415: int lElementPos = 0;
416: for (Iterator lIter = lAttributeDefList.iterator(); lIter
417: .hasNext(); lElementPos++) {
418: AttributeDefType lAttributeDef = (AttributeDefType) lIter
419: .next();
420: if (lAttributeDef.getName().equals(pAttribute.Name))
421: break;
422: }
423: if (lElementPos >= lAttributeDefList.size())
424: throw new PSIllegalArgumentException(
425: "Atrribute not found. EntityRef: " + pEntityRef
426: + " AttributeName: " + pAttribute.Name);
427: lAttributeDefList.set(lElementPos, convertSTAttribute(
428: pEntityRef, pAttribute));
429: Storage.updateEntity(lEntityDef);
430: } catch (JAXBException e) {
431: throw new PSDataSourceOperationInvocationException(
432: "Unable to update Atributes for the entity", e);
433: }
434: }
435:
436: /* Returns all states defined in the state machine of this entity */
437: public STState[] getStates(String pEntityRef) throws PSException {
438: EntityDefType lEntityDef = Storage.getEntity(pEntityRef);
439: if (lEntityDef == null)
440: throw new PSIllegalArgumentException(
441: "Unknown entity. EntityRef : " + pEntityRef);
442: StateMachineType lStateMachine = lEntityDef.getStateMachine();
443: STState[] lReturn = new STState[lStateMachine.getStateDef()
444: .size()];
445: Iterator lIter = lStateMachine.getStateDef().iterator();
446: for (int i = 0; lIter.hasNext(); i++) {
447: lReturn[i] = convertStateDef((StateDefType) lIter.next());
448: }
449: return lReturn;
450: }
451:
452: /* Returns single state with the specified name from specified entity */
453: public STState getState(String pEntityRef, String pStateName)
454: throws PSException {
455: EntityDefType lEntityDef = Storage.getEntity(pEntityRef);
456: if (lEntityDef == null)
457: throw new PSIllegalArgumentException(
458: "Unknown entity. EntityRef : " + pEntityRef);
459: StateMachineType lStateMachine = lEntityDef.getStateMachine();
460: STState[] lReturn = new STState[lStateMachine.getStateDef()
461: .size()];
462: Iterator lIter = lStateMachine.getStateDef().iterator();
463: for (int i = 0; lIter.hasNext(); i++) {
464: StateDefType lStateDef = (StateDefType) lIter.next();
465: if (lStateDef.getName().equals(pStateName))
466: return convertStateDef(lStateDef);
467: }
468: return null;
469: }
470:
471: /* Returns count of all states defined in the state machine of this entity */
472: public int getStatesCount(String pEntityRef) throws PSException {
473: EntityDefType lEntityDef = Storage.getEntity(pEntityRef);
474: if (lEntityDef == null)
475: throw new com.metaboss.enterprise.ps.PSIllegalArgumentException(
476: "Unknown entity. EntityRef : " + pEntityRef);
477: StateMachineType lStateMachine = lEntityDef.getStateMachine();
478: return lStateMachine.getStateDef().size();
479: }
480:
481: /* Returns all state transitions defined for the given state in the given entity */
482: public STStateTransition[] getStateTransitions(String pEntityRef,
483: String pStateName) throws PSException {
484: EntityDefType lEntityDef = Storage.getEntity(pEntityRef);
485: if (lEntityDef == null)
486: throw new PSIllegalArgumentException(
487: "Unknown entity. EntityRef : " + pEntityRef);
488: StateMachineType lStateMachine = lEntityDef.getStateMachine();
489: StateDefType lFoundStateDef = null;
490: Iterator lIter = lStateMachine.getStateDef().iterator();
491: for (int i = 0; lIter.hasNext(); i++) {
492: StateDefType lStateDef = (StateDefType) lIter.next();
493: if (lStateDef.getName().equals(pStateName)) {
494: lFoundStateDef = lStateDef;
495: break;
496: }
497: }
498: if (lFoundStateDef == null)
499: throw new PSIllegalArgumentException(
500: "Unknown state of the entity. EntityRef : "
501: + pEntityRef + " State : " + pStateName);
502: STStateTransition[] lReturn = new STStateTransition[lFoundStateDef
503: .getTransitionDefList().getTransitionDef().size()];
504: Iterator lTransitionsIter = lFoundStateDef
505: .getTransitionDefList().getTransitionDef().iterator();
506: for (int i = 0; lTransitionsIter.hasNext(); i++) {
507: lReturn[i] = convertTransitionDef((TransitionDefType) lTransitionsIter
508: .next());
509: }
510: return lReturn;
511: }
512:
513: /* Returns count of all state transitions defined for the given state in the given entity */
514: public int getStateTransitionsCount(String pEntityRef,
515: String pStateName) throws PSException {
516: EntityDefType lEntityDef = Storage.getEntity(pEntityRef);
517: if (lEntityDef == null)
518: throw new PSIllegalArgumentException(
519: "Unknown entity. EntityRef : " + pEntityRef);
520: StateMachineType lStateMachine = lEntityDef.getStateMachine();
521: StateDefType lFoundStateDef = null;
522: Iterator lIter = lStateMachine.getStateDef().iterator();
523: for (int i = 0; lIter.hasNext(); i++) {
524: StateDefType lStateDef = (StateDefType) lIter.next();
525: if (lStateDef.getName().equals(pStateName)) {
526: lFoundStateDef = lStateDef;
527: break;
528: }
529: }
530: if (lFoundStateDef == null)
531: throw new PSIllegalArgumentException(
532: "Unknown state of the entity. EntityRef : "
533: + pEntityRef + " State : " + pStateName);
534: return lFoundStateDef.getTransitionDefList().getTransitionDef()
535: .size();
536: }
537:
538: /* Returns all selectors defined for the entity */
539: public STSelector[] getSelectors(String pEntityRef)
540: throws PSException {
541: EntityDefType lEntityDef = Storage.getEntity(pEntityRef);
542: if (lEntityDef == null)
543: return null;
544: SelectorDefListType lSelectorDefList = lEntityDef
545: .getSelectorDefList();
546: STSelector[] lReturn = new STSelector[lSelectorDefList
547: .getSelectorDef().size()];
548: Iterator lIter = lSelectorDefList.getSelectorDef().iterator();
549: for (int i = 0; lIter.hasNext(); i++) {
550: lReturn[i] = convertSelectorDef(pEntityRef,
551: (SelectorDefType) lIter.next());
552: }
553: return lReturn;
554: }
555:
556: /* Returns all constraints defined for the entity */
557: public STConstraint[] getConstraints(String pEntityRef)
558: throws PSException {
559: EntityDefType lEntityDef = Storage.getEntity(pEntityRef);
560: if (lEntityDef == null)
561: throw new PSException("Entity not found. EntityRef : "
562: + pEntityRef);
563: ConstraintDefListType lConstraintDefList = lEntityDef
564: .getConstraintDefList();
565: if (lConstraintDefList != null
566: && lConstraintDefList.getConstraintDef() != null) {
567: STConstraint[] lReturn = new STConstraint[lConstraintDefList
568: .getConstraintDef().size()];
569: Iterator lIter = lConstraintDefList.getConstraintDef()
570: .iterator();
571: for (int i = 0; lIter.hasNext(); i++) {
572: lReturn[i] = Util
573: .getConstraint((ConstraintDefType) lIter.next());
574: }
575: return lReturn;
576: }
577: return null;
578: }
579:
580: /* Returns selectors with given name from the given entity */
581: public STSelector getSelector(String pEntityRef,
582: String pSelectorName) throws PSException {
583: EntityDefType lEntityDef = Storage.getEntity(pEntityRef);
584: if (lEntityDef == null)
585: return null;
586: SelectorDefListType lSelectorDefList = lEntityDef
587: .getSelectorDefList();
588: Iterator lIter = lSelectorDefList.getSelectorDef().iterator();
589: for (int i = 0; lIter.hasNext(); i++) {
590: SelectorDefType lSelectorDef = (SelectorDefType) lIter
591: .next();
592: if (lSelectorDef.getName().equals(pSelectorName))
593: return convertSelectorDef(pEntityRef, lSelectorDef);
594: }
595: return null;
596: }
597:
598: /* Inserts selector with in the given entity */
599: public void insertSelector(String pEntityRef,
600: STSelector pNewSelectorDetails) throws PSException {
601: EntityDefType lEntityDef = Storage.getEntity(pEntityRef);
602: if (lEntityDef == null)
603: throw new PSIllegalArgumentException(
604: "Entity not found. EntityRef: " + pEntityRef);
605: // Make sure that selector with the specified name does not exists yet
606: SelectorDefListType lSelectorDefList = lEntityDef
607: .getSelectorDefList();
608: Iterator lIter = lSelectorDefList.getSelectorDef().iterator();
609: while (lIter.hasNext()) {
610: SelectorDefType lSelectorDef = (SelectorDefType) lIter
611: .next();
612: if (lSelectorDef.getName().equals(pNewSelectorDetails.Name))
613: throw new PSIllegalArgumentException(
614: "Attempt to insert duplicate selector. EntityRef: "
615: + pEntityRef + ". SelectorName: "
616: + pNewSelectorDetails.Name);
617: }
618: SelectorDefType lNewSelectorDef = convertSelectorDetails(pNewSelectorDetails);
619: lSelectorDefList.getSelectorDef().add(lNewSelectorDef);
620: Storage.updateEntity(lEntityDef);
621: }
622:
623: /* Returns array of the inpput fields for the selector. May be zero length if there are no inputs */
624: public STField[] getSelectorInputFields(String pEntityRef,
625: String pSelectorName) throws PSException {
626: EntityDefType lEntityDef = Storage.getEntity(pEntityRef);
627: if (lEntityDef == null)
628: return null;
629: SelectorDefListType lSelectorDefList = lEntityDef
630: .getSelectorDefList();
631: Iterator lIter = lSelectorDefList.getSelectorDef().iterator();
632: for (int i = 0; lIter.hasNext(); i++) {
633: SelectorDefType lSelectorDef = (SelectorDefType) lIter
634: .next();
635: if (lSelectorDef.getName().equals(pSelectorName)) {
636: InputFieldDefListType lInputFieldDefList = lSelectorDef
637: .getInputFieldDefList();
638: STField[] lReturn = new STField[lInputFieldDefList
639: .getFieldDef().size()];
640: Iterator lIter1 = lInputFieldDefList.getFieldDef()
641: .iterator();
642: for (int j = 0; lIter1.hasNext(); j++) {
643: lReturn[j] = Util.getField((FieldDefType) lIter1
644: .next());
645: }
646: return lReturn;
647: }
648: }
649: return null;
650: }
651:
652: /* Inserts input field in the selector */
653: public void insertSelectorInputField(String pEntityRef,
654: String pSelectorName, STField pFieldDetails)
655: throws PSException {
656: EntityDefType lEntityDef = Storage.getEntity(pEntityRef);
657: if (lEntityDef == null)
658: throw new PSIllegalArgumentException(
659: "Entity not found. EntityRef: " + pEntityRef);
660: SelectorDefListType lSelectorDefList = lEntityDef
661: .getSelectorDefList();
662: Iterator lIter = lSelectorDefList.getSelectorDef().iterator();
663: for (int i = 0; lIter.hasNext(); i++) {
664: SelectorDefType lSelectorDef = (SelectorDefType) lIter
665: .next();
666: if (lSelectorDef.getName().equals(pSelectorName)) {
667: // Found selector. Make sure that the field does not exists
668: InputFieldDefListType lFieldDefList = lSelectorDef
669: .getInputFieldDefList();
670: Iterator lFieldIter = lFieldDefList.getFieldDef()
671: .iterator();
672: while (lFieldIter.hasNext()) {
673: FieldDefType lFieldDef = (FieldDefType) lFieldIter
674: .next();
675: if (lFieldDef.getName().equals(pFieldDetails.Name))
676: throw new PSIllegalArgumentException(
677: "Attempt to insert duplicate input field into the selector. EntityRef: "
678: + pEntityRef
679: + ". SelectorName: "
680: + pSelectorName
681: + ". FieldName: "
682: + pFieldDetails.Name);
683: }
684: lFieldDefList.getFieldDef().add(
685: Util.getFieldDef(pFieldDetails));
686: Storage.updateEntity(lEntityDef);
687: return;
688: }
689: }
690: throw new PSIllegalArgumentException(
691: "Selector not found. EntityRef: " + pEntityRef
692: + ". SelectorName: " + pSelectorName);
693: }
694:
695: // Helper. Converts xml struct to the PS struct
696: private STAttribute convertAttributeDef(String pEntityRef,
697: AttributeDefType pAttributeDef) throws PSException {
698: if (pAttributeDef == null)
699: return null;
700: STAttribute lStruct = new STAttribute();
701: lStruct.Name = pAttributeDef.getName();
702: lStruct.CanUseForOrdering = pAttributeDef.isCanUseForOrdering();
703: lStruct.Description = pAttributeDef.getDescription();
704: lStruct.Stereotype = com.metaboss.sdlctools.types.enterprisemodel.AttributeStereotypeTextHelper
705: .fromTextObject(pAttributeDef.getAttributeStereotype());
706: lStruct.DataTypeRef = pAttributeDef.getDataTypeRef();
707: // Good place for now to put some validation tests
708: if (lStruct.Name.equalsIgnoreCase("InstanceId"))
709: throw new PSUnexpectedProgramConditionException(
710: "Attribute name 'InstanceId' is reserved and can not be used in entity definition. EntityRef : "
711: + pEntityRef);
712: if (lStruct.Name.equalsIgnoreCase("VersionId"))
713: throw new PSUnexpectedProgramConditionException(
714: "Attribute name 'VersionId' is reserved and can not be used in entity definition. EntityRef : "
715: + pEntityRef);
716: if (lStruct.Name.equalsIgnoreCase("State"))
717: throw new PSUnexpectedProgramConditionException(
718: "Attribute name 'State' is reserved and can not be used in entity definition. EntityRef : "
719: + pEntityRef);
720: return lStruct;
721: }
722:
723: // Helper. Converts xml struct to the PS struct
724: private AttributeDefType convertSTAttribute(String pEntityRef,
725: STAttribute pStruct) throws PSException, JAXBException {
726: AttributeDefType lAttributeDef = Util.getObjectFactory()
727: .createAttributeDefType();
728: lAttributeDef.setName(pStruct.Name);
729: lAttributeDef.setCanUseForOrdering(pStruct.CanUseForOrdering);
730: lAttributeDef.setDescription(pStruct.Description);
731: lAttributeDef
732: .setAttributeStereotype(com.metaboss.sdlctools.types.enterprisemodel.AttributeStereotypeTextHelper
733: .toTextObject(pStruct.Stereotype));
734: lAttributeDef.setDataTypeRef(pStruct.DataTypeRef);
735: // Good place for now to put some validation tests
736: if (pStruct.Name.equalsIgnoreCase("InstanceId"))
737: throw new PSUnexpectedProgramConditionException(
738: "Attribute name 'InstanceId' is reserved and can not be used in entity definition. EntityRef : "
739: + pEntityRef);
740: if (pStruct.Name.equalsIgnoreCase("VersionId"))
741: throw new PSUnexpectedProgramConditionException(
742: "Attribute name 'VersionId' is reserved and can not be used in entity definition. EntityRef : "
743: + pEntityRef);
744: if (pStruct.Name.equalsIgnoreCase("State"))
745: throw new PSUnexpectedProgramConditionException(
746: "Attribute name 'State' is reserved and can not be used in entity definition. EntityRef : "
747: + pEntityRef);
748: return lAttributeDef;
749: }
750:
751: // Helper. Converts xml struct to the PS struct
752: private STState convertStateDef(StateDefType pStateDef)
753: throws PSException {
754: if (pStateDef == null)
755: return null;
756: STState lStruct = new STState();
757: lStruct.Name = pStateDef.getName();
758: lStruct.Description = pStateDef.getDescription();
759: lStruct.Type = com.metaboss.sdlctools.types.enterprisemodel.StateTypeTextHelper
760: .fromTextObject(pStateDef.getStateType());
761: return lStruct;
762: }
763:
764: // Helper. Converts xml struct to the PS struct
765: private STStateTransition convertTransitionDef(
766: TransitionDefType pTransitionDef) throws PSException {
767: if (pTransitionDef == null)
768: return null;
769: STStateTransition lStruct = new STStateTransition();
770: lStruct.DestinationStateName = pTransitionDef.getStateName();
771: lStruct.Description = pTransitionDef.getDescription();
772: return lStruct;
773: }
774:
775: // Helper. Converts xml struct to the PS struct
776: private SelectorDefType convertSelectorDetails(
777: STSelector pSelectorDetails) throws PSException {
778: try {
779: if (pSelectorDetails == null)
780: return null;
781: SelectorDefType lSelectorDef = Util.getObjectFactory()
782: .createSelectorDef();
783: lSelectorDef.setName(pSelectorDetails.Name);
784: lSelectorDef.setDescription(pSelectorDetails.Description);
785: lSelectorDef.setIsImplicit(pSelectorDetails.IsImplicit);
786: lSelectorDef
787: .setSelectorCardinality(com.metaboss.sdlctools.types.enterprisemodel.SelectorCardinalityTextHelper
788: .toTextObject(pSelectorDetails.Cardinality));
789: lSelectorDef.setJavaSelector(pSelectorDetails.JavaSelector);
790: lSelectorDef.setSQLSelector(pSelectorDetails.SQLSelector);
791: InputFieldDefListType lInputFieldDefList = Util
792: .getObjectFactory().createInputFieldDefList();
793: lSelectorDef.setInputFieldDefList(lInputFieldDefList);
794: return lSelectorDef;
795: } catch (JAXBException e) {
796: throw new PSDataSourceOperationInvocationException(e);
797: }
798: }
799:
800: // Helper. Converts xml struct to the PS struct
801: private STSelector convertSelectorDef(String pEntityRef,
802: SelectorDefType pSelectorDef) throws PSException {
803: if (pSelectorDef == null)
804: return null;
805: STSelector lStruct = new STSelector();
806: lStruct.Name = pSelectorDef.getName();
807: lStruct.Description = pSelectorDef.getDescription();
808: lStruct.IsImplicit = pSelectorDef.isIsImplicit();
809: lStruct.Cardinality = com.metaboss.sdlctools.types.enterprisemodel.SelectorCardinalityTextHelper
810: .fromTextObject(pSelectorDef.getSelectorCardinality());
811: lStruct.JavaSelector = pSelectorDef.getJavaSelector();
812: lStruct.SQLSelector = pSelectorDef.getSQLSelector();
813: // Good place for now to put some validation tests
814: if (lStruct.Name == null || lStruct.Name.length() == 0)
815: throw new PSUnexpectedProgramConditionException(
816: "Selector Name field can not be empty in entity selector definition. EntityRef: "
817: + pEntityRef);
818: if (lStruct.Cardinality.isEmpty())
819: throw new PSUnexpectedProgramConditionException(
820: "Selector Cardinality field can not be empty in entity selector definition. EntityRef: "
821: + pEntityRef
822: + " SelectorName: "
823: + lStruct.Name);
824: if (lStruct.Name.equals("EntitiesInState")
825: || lStruct.Name.equals("EntitiesInStates"))
826: throw new PSUnexpectedProgramConditionException(
827: "Selector name can not be used explicitly as it is reserved for implicit (autogenerated) selector. EntityRef: "
828: + pEntityRef
829: + " SelectorName: "
830: + lStruct.Name);
831: return lStruct;
832: }
833:
834: // Validates attributes of the STEntity
835: private static void validateSTEntityAttributes(STEntity pStruct)
836: throws PSAttributeConstraintViolationException {
837: if (pStruct.EntityRef == null
838: || pStruct.EntityRef.trim().length() == 0)
839: throw new PSAttributeConstraintViolationException(
840: "EntityRef can not be empty in the Entity definition.");
841: if (pStruct.PluralName == null
842: || pStruct.PluralName.trim().length() == 0)
843: throw new PSAttributeConstraintViolationException(
844: "PluralName can not be empty in the Entity definition. EntityRef:"
845: + pStruct.EntityRef);
846: }
847: }
|