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.Arrays;
018: import java.util.List;
019:
020: import javax.xml.bind.JAXBException;
021:
022: import com.metaboss.enterprise.ps.PSDataSourceOperationInvocationException;
023: import com.metaboss.enterprise.ps.PSException;
024: import com.metaboss.enterprise.ps.PSUnexpectedProgramConditionException;
025: import com.metaboss.sdlctools.domains.enterprisemodel.storage.PSAssociation;
026: import com.metaboss.sdlctools.domains.enterprisemodel.storage.STAssociation;
027: import com.metaboss.sdlctools.domains.enterprisemodel.storage.STAssociationRole;
028: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.AssociationDefType;
029: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.AssociationRoleDefType;
030: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.EntityDefType;
031: import com.metaboss.sdlctools.types.enterprisemodel.AssociationRoleStereotype;
032:
033: public class PSAssociationImpl implements PSAssociation {
034: /* Returns details structure corresponding to given reference or null struct if definition not found */
035: public STAssociation getAssociation(String pAssociationRef)
036: throws PSException {
037: AssociationDefType lAssociationDef = Storage
038: .getAssociation(pAssociationRef);
039: if (lAssociationDef == null)
040: return null;
041: STAssociation lStruct = new STAssociation();
042: lStruct.AssociationRef = lAssociationDef.getAssociationRef();
043: lStruct.Description = lAssociationDef.getDescription();
044:
045: // Good place for now to put some validation tests
046: STAssociationRole lRoleA = PSAssociationRoleImpl.getRoleStruct(
047: pAssociationRef, true,
048: (AssociationRoleDefType) lAssociationDef
049: .getAssociationRoleDef().get(0));
050: STAssociationRole lRoleB = PSAssociationRoleImpl.getRoleStruct(
051: pAssociationRef, false,
052: (AssociationRoleDefType) lAssociationDef
053: .getAssociationRoleDef().get(1));
054: // Good place for now to put some validation tests
055: // Some form of aggregation must be specified
056: if (lRoleA.Stereotype
057: .equals(AssociationRoleStereotype.ASSOCIATION)
058: && lRoleB.Stereotype
059: .equals(AssociationRoleStereotype.ASSOCIATION))
060: throw new PSUnexpectedProgramConditionException(
061: "Unsupported association. Both association roles have 'ASSOCIATION' stereotype. AssociationRef : "
062: + pAssociationRef);
063: // Aggregation must be specified on one side only
064: if ((!lRoleA.Stereotype
065: .equals(AssociationRoleStereotype.ASSOCIATION))
066: && (!lRoleB.Stereotype
067: .equals(AssociationRoleStereotype.ASSOCIATION)))
068: throw new PSUnexpectedProgramConditionException(
069: "Unsupported association. Neither association role has 'ASSOCIATION' stereotype. AssociationRef : "
070: + pAssociationRef);
071: // Composition is only supported in limited cases
072: if (lRoleA.Stereotype
073: .equals(AssociationRoleStereotype.COMPOSITION)) {
074: if (!lRoleB.Cardinality
075: .equals(com.metaboss.sdlctools.types.enterprisemodel.AssociationRoleCardinality.ONE))
076: throw new PSUnexpectedProgramConditionException(
077: "Unsupported association. Association role on the other side of 'COMPOSITION' stereotype must have cardinality of 'ONE'. AssociationRef : "
078: + pAssociationRef);
079: }
080: if (lRoleB.Stereotype
081: .equals(AssociationRoleStereotype.COMPOSITION)) {
082: if (!lRoleA.Cardinality
083: .equals(com.metaboss.sdlctools.types.enterprisemodel.AssociationRoleCardinality.ONE))
084: throw new PSUnexpectedProgramConditionException(
085: "Unsupported association. Association role on the other side of 'COMPOSITION' stereotype must have cardinality of 'ONE'. AssociationRef : "
086: + pAssociationRef);
087: }
088: // Check if both sides of association are joined
089: assertRoleReferencedFromEntity(pAssociationRef, lRoleA.Name,
090: lRoleB.EntityRef);
091: assertRoleReferencedFromEntity(pAssociationRef, lRoleB.Name,
092: lRoleA.EntityRef);
093: return lStruct;
094: }
095:
096: /** Inserts new association record into the database */
097: public void insertAssociation(STAssociation pRecord)
098: throws PSException {
099: try {
100: AssociationDefType lAssociationDef = Util
101: .getObjectFactory().createAssociationDef();
102: lAssociationDef.setAssociationRef(pRecord.AssociationRef);
103: lAssociationDef.setDescription(pRecord.Description);
104: List lAssociationRoleDefList = lAssociationDef
105: .getAssociationRoleDef();
106: AssociationRoleDefType lAssociationRoleADef = Util
107: .getObjectFactory().createAssociationRoleDef();
108: lAssociationRoleDefList.add(lAssociationRoleADef);
109: AssociationRoleDefType lAssociationRoleBDef = Util
110: .getObjectFactory().createAssociationRoleDef();
111: lAssociationRoleDefList.add(lAssociationRoleBDef);
112: Storage.insertAssociation(lAssociationDef);
113: } catch (JAXBException e) {
114: throw new PSDataSourceOperationInvocationException(
115: "Unable to insert new Association", e);
116: }
117: }
118:
119: /** Updates existing association details in the database */
120: public void updateAssociation(STAssociation pUpdatedRecord)
121: throws PSException {
122: AssociationDefType lAssociationDef = Storage
123: .getAssociation(pUpdatedRecord.AssociationRef);
124: if (lAssociationDef == null)
125: throw new PSException(
126: "Association not found. AssociationRef : "
127: + pUpdatedRecord.AssociationRef);
128: lAssociationDef.setDescription(pUpdatedRecord.Description);
129: List lAssociationRoleDefList = lAssociationDef
130: .getAssociationRoleDef();
131: Storage.updateAssociation(lAssociationDef);
132: }
133:
134: /** Deletes existing association from the database */
135: public void deleteAssociation(String pAssociationRef)
136: throws PSException {
137: Storage.deleteAssociation(pAssociationRef);
138: }
139:
140: // Helper. Checks if role is referenced back from the entity
141: private void assertRoleReferencedFromEntity(String pAssociationRef,
142: String pRoleName, String pRoleEntityRef) throws PSException {
143: EntityDefType lRoleEntityDef = Storage
144: .getEntity(pRoleEntityRef);
145: if (lRoleEntityDef == null)
146: throw new PSUnexpectedProgramConditionException(
147: "Association references entity, which does not exists. AssociationRef: "
148: + pAssociationRef + ", EntityRef: "
149: + pRoleEntityRef);
150: List lRolesList = Arrays.asList((String[]) lRoleEntityDef
151: .getAssociationRoleRefList().getAssociationRoleRef()
152: .toArray(
153: new String[lRoleEntityDef
154: .getAssociationRoleRefList()
155: .getAssociationRoleRef().size()]));
156: if (!lRolesList.contains(pAssociationRef + "." + pRoleName))
157: throw new PSUnexpectedProgramConditionException(
158: "Association references entity, which does not references back this association. AssociationRef: "
159: + pAssociationRef
160: + ", Role Name: "
161: + pRoleName
162: + ", EntityRef: "
163: + pRoleEntityRef);
164: }
165: }
|