01: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
02: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
03: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
04: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
05: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
06: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
07: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
08: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
09: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
10: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
11: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
12: // POSSIBILITY OF SUCH DAMAGE.
13: //
14: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
15: package com.metaboss.sdlctools.services.codegeneration.storageimplementationgenerator.relational;
16:
17: import java.util.ArrayList;
18: import java.util.Collection;
19: import java.util.HashMap;
20: import java.util.Iterator;
21: import java.util.List;
22: import java.util.Map;
23:
24: import javax.jmi.reflect.JmiException;
25:
26: import com.metaboss.enterprise.bs.BSException;
27: import com.metaboss.enterprise.bs.BSServiceProviderException;
28: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.AssociationRole;
29: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Attribute;
30: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.AttributeStereotypeEnum;
31: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Entity;
32: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.PrimaryKeyElement;
33:
34: /** Utilities to do with specific SQL/oracle features */
35: public class SQLJDBCUtil {
36: private static Object cCommonUtilInitialisationSemaphore = new Object();
37:
38: // Helper. Returns map of primary key fields per table. Each key to the map is Entity and
39: // each value is a list of the objects from this table, which are involved in the primary key
40: // This method helps to deal with the fact that in the subtype entity (with inheritance)
41: // elements of the primary key may be in different entities
42: public static Map getPrimaryKeyElementsPerTableMap(Entity pEntity)
43: throws BSException {
44: try {
45: Map lPrimaryKeyElementsPerTableMap = new HashMap();
46: Collection lPrimaryKeyElements = pEntity
47: .getPrimaryKeyElements();
48: if (lPrimaryKeyElements.isEmpty())
49: throw new BSException("Undefined PrimaryKey. Entity : "
50: + pEntity.getName());
51: for (Iterator lPrimaryKeyElementsIterator = lPrimaryKeyElements
52: .iterator(); lPrimaryKeyElementsIterator.hasNext();) {
53: Entity lOwnerEntity = null;
54: PrimaryKeyElement lPrimaryKeyElement = (PrimaryKeyElement) lPrimaryKeyElementsIterator
55: .next();
56: if (lPrimaryKeyElement instanceof Attribute) {
57: Attribute lAttribute = (Attribute) lPrimaryKeyElement;
58: // This attribute can not be optional
59: if (lAttribute.getStereotype().equals(
60: AttributeStereotypeEnum.OPTIONAL))
61: throw new BSException(
62: "Optional attribute can not be part of entity primary key. Entity : "
63: + pEntity.getName()
64: + ". Attribute : "
65: + lAttribute.getName());
66: lOwnerEntity = lAttribute.getEntity();
67: } else if (lPrimaryKeyElement instanceof AssociationRole) {
68: AssociationRole lReference = (AssociationRole) lPrimaryKeyElement;
69: if (lReference.isOptional())
70: throw new BSException(
71: "For Reference to be part of PrimaryKey, it must have mandatory cardinality (minimum of one). AssociationRoleRef: "
72: + lReference.getRef());
73: // Set opposite entity as owner
74: lOwnerEntity = lReference.getReferencingEntity();
75: }
76: // Find existing or crreate a new table entry
77: List lTableKeyElements = (List) lPrimaryKeyElementsPerTableMap
78: .get(lOwnerEntity);
79: if (lTableKeyElements == null)
80: lPrimaryKeyElementsPerTableMap.put(lOwnerEntity,
81: lTableKeyElements = new ArrayList());
82: lTableKeyElements.add(lPrimaryKeyElement);
83: }
84: return lPrimaryKeyElementsPerTableMap;
85: } catch (JmiException e) {
86: throw new BSServiceProviderException(
87: "Error while processing Primary Key", e);
88: }
89: }
90: }
|