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.models.impl.metabossmodel.enterprisemodel.systemimplementationmodel;
016:
017: import java.util.Collection;
018: import java.util.List;
019:
020: import javax.jmi.reflect.ConstraintViolationException;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.netbeans.mdr.storagemodel.StorableObject;
025:
026: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.AggregationType;
027: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.AggregationTypeEnum;
028: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Association;
029: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.AssociationRole;
030: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.AssociationRoleCardinality;
031: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.AssociationRoleCardinalityEnum;
032: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Entity;
033:
034: public abstract class AssociationRoleImpl extends PrimaryKeyElementImpl
035: implements AssociationRole {
036: // Commons Logging instance.
037: private static final Log sLogger = LogFactory
038: .getLog(AssociationRoleImpl.class);
039:
040: // Required constructor
041: protected AssociationRoleImpl(StorableObject storable) {
042: super (storable);
043: }
044:
045: // Verify certain constraints
046: protected Collection _verify(Collection pViolations) {
047: // First call superclass
048: Collection lViolations = super ._verify(pViolations);
049:
050: // Constraint #1 - the association role with aggregation type of composition
051: // can only have 'one and only one' multiplicity
052: {
053: AggregationType lAssociationRoleAggregationType = getAggregationType();
054: if (lAssociationRoleAggregationType != null
055: && lAssociationRoleAggregationType
056: .equals(AggregationTypeEnum.COMPOSITION)) {
057: if (!getCardinality().equals(
058: AssociationRoleCardinalityEnum.ONE))
059: lViolations
060: .add(new ConstraintViolationException(
061: this ,
062: refMetaObject(),
063: "AssociationRole with aggregation type of 'Composition' has unsupported cardinality. Only 'exactly one' cardinality is supported."));
064: }
065: }
066: return lViolations;
067: }
068:
069: // Returns true if this association has plural cardinality
070: public boolean isPlural() {
071: AssociationRoleCardinality lCardinality = getCardinality();
072: return lCardinality != null
073: && (lCardinality
074: .equals(AssociationRoleCardinalityEnum.ZERO_TO_MANY) || lCardinality
075: .equals(AssociationRoleCardinalityEnum.ONE_TO_MANY));
076: }
077:
078: // Returns true if this association has singular cardinality
079: public boolean isSingular() {
080: AssociationRoleCardinality lCardinality = getCardinality();
081: return lCardinality != null
082: && (lCardinality
083: .equals(AssociationRoleCardinalityEnum.ZERO_OR_ONE) || lCardinality
084: .equals(AssociationRoleCardinalityEnum.ONE));
085: }
086:
087: // Returns true if this association has optional cardinality
088: public boolean isOptional() {
089: AssociationRoleCardinality lCardinality = getCardinality();
090: return lCardinality != null
091: && (lCardinality
092: .equals(AssociationRoleCardinalityEnum.ZERO_TO_MANY) || lCardinality
093: .equals(AssociationRoleCardinalityEnum.ZERO_OR_ONE));
094: }
095:
096: // Returns true if this association has mandatory cardinality
097: public boolean isMandatory() {
098: AssociationRoleCardinality lCardinality = getCardinality();
099: return lCardinality != null
100: && (lCardinality
101: .equals(AssociationRoleCardinalityEnum.ONE_TO_MANY) || lCardinality
102: .equals(AssociationRoleCardinalityEnum.ONE));
103: }
104:
105: /** Returns the role on the other side of the association */
106: public AssociationRole getOppositeRole() {
107: Association lAssociation = ((AssociationRole) this )
108: .getAssociation();
109: if (lAssociation == null)
110: return null; // Model has a problem unable to get the opposite role
111: List lAssociationRoles = lAssociation.getRoles();
112: if (lAssociationRoles.size() != 2)
113: return null; // Model has a problem unable to get the opposite role
114: int lOppositeRoleIndex = lAssociationRoles.indexOf(this ) == 0 ? 1
115: : 0;
116: return (AssociationRole) lAssociationRoles
117: .get(lOppositeRoleIndex);
118: }
119:
120: /** Returns the entity, which references this role. In other words - an entity playing the opposite role in the association */
121: public Entity getReferencingEntity() {
122: AssociationRole lOppositeRole = getOppositeRole();
123: return lOppositeRole != null ? lOppositeRole.getEntity() : null;
124: }
125: }
|