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 javax.xml.bind.JAXBException;
018:
019: import com.metaboss.enterprise.ps.PSDataSourceOperationInvocationException;
020: import com.metaboss.enterprise.ps.PSException;
021: import com.metaboss.enterprise.ps.PSIllegalArgumentException;
022: import com.metaboss.enterprise.ps.PSUnexpectedProgramConditionException;
023: import com.metaboss.sdlctools.domains.enterprisemodel.storage.PSAssociationRole;
024: import com.metaboss.sdlctools.domains.enterprisemodel.storage.STAssociationRole;
025: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.AssociationDefType;
026: import com.metaboss.sdlctools.domains.enterprisemodel.storage.xmlfileimpl.dom.AssociationRoleDefType;
027:
028: public class PSAssociationRoleImpl implements PSAssociationRole {
029: /** Returns details structure corresponding to Role, specified by role reference */
030: public STAssociationRole getAssociationRole(
031: String pAssociationRoleRef) throws PSException {
032: int lLastDot = pAssociationRoleRef.lastIndexOf('.');
033: if (lLastDot <= 0)
034: throw new PSIllegalArgumentException(
035: "Invalid AssociationRoleRef: "
036: + pAssociationRoleRef);
037: String lAssociationRef = pAssociationRoleRef.substring(0,
038: lLastDot);
039: String lAssociationRoleName = pAssociationRoleRef
040: .substring(lLastDot + 1);
041: AssociationDefType lAssociationDef = Storage
042: .getAssociation(lAssociationRef);
043: if (lAssociationDef == null)
044: return null;
045: AssociationRoleDefType lAssociationRoleADef = (AssociationRoleDefType) lAssociationDef
046: .getAssociationRoleDef().get(0);
047: if (lAssociationRoleADef.getName().equals(lAssociationRoleName))
048: return getRoleStruct(pAssociationRoleRef, true,
049: lAssociationRoleADef);
050: AssociationRoleDefType lAssociationRoleBDef = (AssociationRoleDefType) lAssociationDef
051: .getAssociationRoleDef().get(1);
052: if (lAssociationRoleBDef.getName().equals(lAssociationRoleName))
053: return getRoleStruct(pAssociationRoleRef, false,
054: lAssociationRoleBDef);
055: return null;
056: }
057:
058: /** Returns details structure corresponding to RoleA or RoleB of the specified association */
059: public STAssociationRole getAssociationRole(String pAssociationRef,
060: boolean pIsRoleA) throws PSException {
061: AssociationDefType lAssociationDef = Storage
062: .getAssociation(pAssociationRef);
063: if (lAssociationDef == null)
064: return null;
065: AssociationRoleDefType lAssociationRoleDef = (AssociationRoleDefType) lAssociationDef
066: .getAssociationRoleDef().get(pIsRoleA ? 0 : 1);
067: return getRoleStruct(pAssociationRef + "."
068: + lAssociationRoleDef.getName(), pIsRoleA,
069: lAssociationRoleDef);
070: }
071:
072: /* Inserts new association record into the database */
073: public void insertAssociationRole(String pAssociationRef,
074: STAssociationRole pRecord) throws PSException {
075: try {
076: AssociationDefType lAssociationDef = Storage
077: .getAssociation(pAssociationRef);
078: if (lAssociationDef == null)
079: throw new PSIllegalArgumentException(
080: "Association not found. AssociationRef: "
081: + pAssociationRef);
082: AssociationRoleDefType lAssociationRoleDef = getRoleDef(pRecord);
083: lAssociationDef.getAssociationRoleDef().set(
084: (pRecord.IsRoleA ? 0 : 1), lAssociationRoleDef);
085: Storage.updateAssociation(lAssociationDef);
086: } catch (JAXBException e) {
087: throw new PSDataSourceOperationInvocationException(
088: "Unable to insert new Association", e);
089: }
090: }
091:
092: /* Updates new association record into the database */
093: public void updateAssociationRole(String pAssociationRef,
094: STAssociationRole pRecord) throws PSException {
095: try {
096: AssociationDefType lAssociationDef = Storage
097: .getAssociation(pAssociationRef);
098: if (lAssociationDef == null)
099: throw new PSIllegalArgumentException(
100: "Association not found. AssociationRef: "
101: + pAssociationRef);
102: AssociationRoleDefType lAssociationRoleDef = getRoleDef(pRecord);
103: lAssociationDef.getAssociationRoleDef().set(
104: (pRecord.IsRoleA ? 0 : 1), lAssociationRoleDef);
105: Storage.updateAssociation(lAssociationDef);
106: } catch (JAXBException e) {
107: throw new PSDataSourceOperationInvocationException(
108: "Unable to insert new Association", e);
109: }
110: }
111:
112: public static STAssociationRole getRoleStruct(
113: String pAssociationRoleRef, boolean pIsRoleA,
114: AssociationRoleDefType pRoleDef) throws PSException {
115: STAssociationRole lStruct = new STAssociationRole();
116: if (pRoleDef != null) {
117: lStruct.EntityRef = pRoleDef.getEntityRef();
118: lStruct.Name = pRoleDef.getName();
119: lStruct.Description = pRoleDef.getDescription();
120: lStruct.Cardinality = com.metaboss.sdlctools.types.enterprisemodel.AssociationRoleCardinalityTextHelper
121: .fromTextObject(pRoleDef
122: .getAssociationRoleCardinality());
123: lStruct.Stereotype = com.metaboss.sdlctools.types.enterprisemodel.AssociationRoleStereotypeTextHelper
124: .fromTextObject(pRoleDef
125: .getAssociationRoleStereotype());
126: lStruct.PluralName = pRoleDef.getPluralName();
127: lStruct.IsRoleA = pIsRoleA;
128: // Good place for now to put some validation tests
129: if (lStruct.Name.equalsIgnoreCase("InstanceId"))
130: throw new PSUnexpectedProgramConditionException(
131: "Role name 'InstanceId' is reserved and can not be used in association definition. AssociationRoleRef : "
132: + pAssociationRoleRef);
133: if (lStruct.Name.equalsIgnoreCase("VersionId"))
134: throw new PSUnexpectedProgramConditionException(
135: "Role name 'VersionId' is reserved and can not be used in association definition. AssociationRoleRef : "
136: + pAssociationRoleRef);
137: if (lStruct.Name.equalsIgnoreCase("State"))
138: throw new PSUnexpectedProgramConditionException(
139: "Role name 'State' is reserved and can not be used in association definition. AssociationRoleRef : "
140: + pAssociationRoleRef);
141: if (lStruct.Cardinality.isEmpty())
142: throw new PSUnexpectedProgramConditionException(
143: "Role cardinality can not be empty in the association definition. AssociationRoleRef : "
144: + pAssociationRoleRef);
145: }
146: return lStruct;
147: }
148:
149: public static AssociationRoleDefType getRoleDef(
150: STAssociationRole pRoleStruct) throws JAXBException {
151: AssociationRoleDefType lRoleDef = Util.getObjectFactory()
152: .createAssociationRoleDef();
153: lRoleDef.setEntityRef(pRoleStruct.EntityRef);
154: lRoleDef.setName(pRoleStruct.Name);
155: lRoleDef.setPluralName(pRoleStruct.PluralName);
156: lRoleDef.setDescription(pRoleStruct.Description);
157: lRoleDef
158: .setAssociationRoleCardinality(com.metaboss.sdlctools.types.enterprisemodel.AssociationRoleCardinalityTextHelper
159: .toTextObject(pRoleStruct.Cardinality));
160: lRoleDef
161: .setAssociationRoleStereotype(com.metaboss.sdlctools.types.enterprisemodel.AssociationRoleStereotypeTextHelper
162: .toTextObject(pRoleStruct.Stereotype));
163: return lRoleDef;
164: }
165: }
|