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.modelassistant.metabossmodel.domainsupport;
016:
017: import java.util.ArrayList;
018: import java.util.Collection;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Set;
022:
023: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.AssociationRole;
024: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Entity;
025: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.PrimaryKeyElement;
026:
027: /** Implementation of the simple stylesheet class, which is
028: * responsible for creation of derived names */
029: public class Util {
030: // Helper method. Collects entities, instances of which may be not be found when
031: // attempting to load given top entity. This helper is basically expected to
032: // study elements of the primary key for this entity and add all associated entities, which are part of the primary key
033: // The method is called recirsively to deal with primary keys of the associated entities, which areparts of primary key
034: // to any depth.
035: static void collectEntitiesWhichMayBeNotFoundWhenLoadingInstance(
036: Entity pInstanceEntity, Set pCollectedEntities) {
037: // Look at all associations, which are parts of primary key
038: // We also may have entities, which are parts of primary key not found
039: for (Iterator lPrimaryKeyElementsIterator = pInstanceEntity
040: .getPrimaryKeyElements().iterator(); lPrimaryKeyElementsIterator
041: .hasNext();) {
042: PrimaryKeyElement lPrimaryKeyElement = (PrimaryKeyElement) lPrimaryKeyElementsIterator
043: .next();
044: if (lPrimaryKeyElement instanceof AssociationRole) {
045: AssociationRole lRole = (AssociationRole) lPrimaryKeyElement;
046: // Only register this entity and its dependents if it is not already in the list
047: Entity lAssociatedEntityInPrimaryKey = lRole
048: .getEntity();
049: if (!pCollectedEntities
050: .contains(lAssociatedEntityInPrimaryKey)) {
051: // Add this entity to collected entities and call this operation recursively
052: pCollectedEntities
053: .add(lAssociatedEntityInPrimaryKey);
054: collectEntitiesWhichMayBeNotFoundWhenLoadingInstance(
055: lAssociatedEntityInPrimaryKey,
056: pCollectedEntities);
057: }
058: }
059: }
060: }
061:
062: // Helper method. Opposite to the one above. It will collect all
063: // instance entities which may need to find the given entity when loading
064: static void collectInstancesWhichMayNeedToFindEntityWhenLoading(
065: Entity pEntity, Set pCollectedEntities) {
066: // Look at all associations, which are parts of primary key
067: for (Iterator lReferencesIterator = pEntity.getReferences()
068: .iterator(); lReferencesIterator.hasNext();) {
069: AssociationRole lReference = (AssociationRole) lReferencesIterator
070: .next();
071: AssociationRole lOppositeReference = lReference
072: .getOppositeRole();
073: Entity lInstanceEntity = lReference.getEntity();
074: List lEntitiesToConsider = new ArrayList();
075: lEntitiesToConsider.add(lInstanceEntity);
076: lEntitiesToConsider.addAll(lInstanceEntity
077: .getCombinedSubtypes());
078: for (Iterator lInstanceEntitiesIterator = lEntitiesToConsider
079: .iterator(); lInstanceEntitiesIterator.hasNext();) {
080: Entity lEntityElement = (Entity) lInstanceEntitiesIterator
081: .next();
082: if (!pCollectedEntities.contains(lEntityElement)
083: && lEntityElement.getPrimaryKeyElements()
084: .contains(lOppositeReference)) {
085: pCollectedEntities.add(lEntityElement);
086: collectInstancesWhichMayNeedToFindEntityWhenLoading(
087: lEntityElement, pCollectedEntities);
088: }
089: }
090: }
091: }
092:
093: // Helper method. Collects entities, instances of which may be not be found when
094: // attempting to update or create given top entity. This helper is basically expected to
095: // add all association roles present in the entity
096: static void collectEntitiesWhichMayBeNotFoundWhenUpdatingOrCreatingInstance(
097: Entity pEntity, Set pCollectedEntities) {
098: // Now look through all associations for which we have a reference
099: Collection lReferences = pEntity.getCombinedReferences();
100: for (Iterator lReferencesIterator = lReferences.iterator(); lReferencesIterator
101: .hasNext();) {
102: AssociationRole lRole = (AssociationRole) lReferencesIterator
103: .next();
104: // Singular multiplicities resolved simply by having the other entity id as the member of the structure
105: // Not singular cardinality is resolved by having a separate service call
106: if (lRole.isSingular()) {
107: // Only register this entity and its dependents if it is not already in the list
108: Entity lAssociatedEntityInRole = lRole.getEntity();
109: if (!pCollectedEntities
110: .contains(lAssociatedEntityInRole)) {
111: // Add this entity to collected entities and collect all entities, which may go wrong when editing this one
112: pCollectedEntities.add(lAssociatedEntityInRole);
113: collectEntitiesWhichMayBeNotFoundWhenLoadingInstance(
114: lAssociatedEntityInRole, pCollectedEntities);
115: }
116: }
117: }
118: }
119: }
|