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.applications.designstudio.domainmodel;
016:
017: import java.awt.Color;
018: import java.awt.Point;
019: import java.util.ArrayList;
020: import java.util.Map;
021: import java.util.Set;
022:
023: import org.jgraph.graph.DefaultEdge;
024: import org.jgraph.graph.DefaultGraphCell;
025: import org.jgraph.graph.DefaultPort;
026: import org.jgraph.graph.GraphConstants;
027:
028: import com.metaboss.applications.designstudio.Application;
029: import com.metaboss.applications.designstudio.BaseGraphModel;
030: import com.metaboss.applications.designstudio.BaseUserObject;
031: import com.metaboss.applications.designstudio.userobjects.AssociationUserObject;
032: import com.metaboss.applications.designstudio.userobjects.EntityGeneralizationUserObject;
033: import com.metaboss.applications.designstudio.userobjects.EntityUserObject;
034: import com.metaboss.sdlctools.models.metabossmodel.ModelElement;
035: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.AggregationTypeEnum;
036: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Association;
037: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.AssociationRole;
038: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Domain;
039: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.Entity;
040: import com.metaboss.sdlctools.models.metabossmodel.visualmodel.DiagramModelElement;
041: import com.metaboss.sdlctools.models.metabossmodel.visualmodel.DomainEntitiesDiagram;
042:
043: /* Domain graph Model class */
044:
045: public class DomainViewModel extends BaseGraphModel {
046: /** Constructor. */
047: public DomainViewModel(DomainEntitiesDiagram pDiagram)
048: throws Exception {
049: super (pDiagram.getDomain(), pDiagram);
050: }
051:
052: // return domain
053: public Domain getDomain() {
054: Object lObject = getBOObject();
055: if (lObject != null && lObject instanceof Domain)
056: return (Domain) lObject;
057: else
058: return null;
059: }
060:
061: // get diagram interface
062: /*public Diagram getDiaram() throws Exception
063: {
064: Domain lDomain = getDomain();
065: if (lDomain!=null)
066: {
067: Object[] lDigrams = lDomain.getEntityClassDiagrams().toArray();
068: if (lDigrams.length>0)
069: return (Diagram)lDigrams[0];
070: else
071: {
072: Diagram lDiagram = null;
073: Application.beginTransaction();
074: try
075: {
076: lDiagram = getPackage().getVisualModel().getDomainEntitiesDiagram().createDomainEntitiesDiagram();
077: lDiagram.setName("DomainEntitiesDiagram");
078: lDomain.getEntityClassDiagrams().add(lDiagram);
079: Application.commit();
080: }
081: finally
082: {
083: if (Application.isInTransaction()) Application.rollback();
084: }
085: return lDiagram;
086: }
087: }
088: return null;
089: }*/
090:
091: // reload graph model
092: public void loadModel() throws Exception {
093: super .loadModel();
094:
095: DomainEntitiesDiagram lDiagram = (DomainEntitiesDiagram) mDiagram;
096: Object[] lElements = lDiagram.getElements().toArray();
097:
098: // Entity cells
099: for (int i = 0; i < lElements.length; i++) {
100: DiagramModelElement lElement = (DiagramModelElement) lElements[i];
101: ModelElement lModelElement = lElement.getModelElement();
102: if (lModelElement != null
103: && lModelElement instanceof Entity) {
104: Entity lEntity = (Entity) lModelElement;
105: addEntityCell(lEntity, getPosition(lEntity));
106: }
107: }
108: // Generalizations
109: for (int i = 0; i < lElements.length; i++) {
110: DiagramModelElement lElement = (DiagramModelElement) lElements[i];
111: ModelElement lModelElement = lElement.getModelElement();
112: if (lModelElement != null
113: && lModelElement instanceof Entity) {
114: Entity lEntity = (Entity) lModelElement;
115: if (lEntity.getSupertype() != null)
116: addGeneralization(lEntity, loadEdgePoints(lEntity));
117: }
118: }
119: // Associations
120: for (int i = 0; i < lElements.length; i++) {
121: DiagramModelElement lElement = (DiagramModelElement) lElements[i];
122: ModelElement lModelElement = lElement.getModelElement();
123: if (lModelElement != null
124: && lModelElement instanceof Association) {
125: Association lAssociation = (Association) lModelElement;
126: java.util.List lList = loadEdgePoints(lAssociation);
127: addAssociationCell(lAssociation,
128: getPosition(lAssociation), lList);
129: }
130: }
131: }
132:
133: // add entity cell
134: public DefaultGraphCell addEntityCell(Entity pEntity,
135: Point lPosition) throws Exception {
136: return addVertex(new EntityUserObject(pEntity), lPosition,
137: null, getVertexStyle());
138: }
139:
140: public class AssociationParams {
141: String mFromID = null;
142: String mToID = null;
143: Map mEdgeStyle = null;
144:
145: public AssociationParams(String pFromID, String pToID,
146: Map pEdgeStyle) {
147: mFromID = pFromID;
148: mToID = pToID;
149: mEdgeStyle = pEdgeStyle;
150: }
151: }
152:
153: // calculate association params
154: public AssociationParams fillAssociationParams(
155: Association pAssociation) throws Exception {
156: if (pAssociation != null) {
157: java.util.List lList = pAssociation.getRoles();
158: AssociationRole lRoleA = (lList != null && lList.size() > 0) ? (AssociationRole) lList
159: .get(0)
160: : null;
161: AssociationRole lRoleB = (lList != null && lList.size() > 1) ? (AssociationRole) lList
162: .get(1)
163: : null;
164: String lFromID = null;
165: String lToID = null;
166: Map lEdgeStyle = null;
167:
168: if (lRoleB == null) {
169: // Should never happen. Throw exception. Never allow Association with other than two Association roles
170: throw new Exception("Unexpected condition.");
171: } else {
172: Entity lRoleAEntity = lRoleA.getEntity();
173: Entity lRoleBEntity = lRoleB.getEntity();
174:
175: // This is entity association
176: if (lRoleA.getAggregationType().equals(
177: AggregationTypeEnum.COMPOSITION)) {
178: lEdgeStyle = getCompositionStyle();
179: if (lRoleAEntity != null)
180: lFromID = lRoleAEntity.refMofId();
181: if (lRoleBEntity != null)
182: lToID = lRoleBEntity.refMofId();
183: lEdgeStyle.put("CardinalityFrom", lRoleA
184: .getCardinality());
185: lEdgeStyle.put("CardinalityTo", lRoleB
186: .getCardinality());
187: lEdgeStyle.put("FromName", lRoleA.getName());
188: lEdgeStyle.put("ToName", lRoleB.getName());
189: } else if (lRoleA.getAggregationType().equals(
190: AggregationTypeEnum.AGGREGATION)) {
191: lEdgeStyle = getAggregationStyle();
192: if (lRoleAEntity != null)
193: lFromID = lRoleAEntity.refMofId();
194: if (lRoleBEntity != null)
195: lToID = lRoleBEntity.refMofId();
196: lEdgeStyle.put("CardinalityFrom", lRoleA
197: .getCardinality());
198: lEdgeStyle.put("CardinalityTo", lRoleB
199: .getCardinality());
200: lEdgeStyle.put("FromName", lRoleA.getName());
201: lEdgeStyle.put("ToName", lRoleB.getName());
202: } else if (lRoleB.getAggregationType().equals(
203: AggregationTypeEnum.COMPOSITION)) {
204: lEdgeStyle = getCompositionStyle();
205: lEdgeStyle.put("1", "1");
206: if (lRoleBEntity != null)
207: lFromID = lRoleBEntity.refMofId();
208: if (lRoleAEntity != null)
209: lToID = lRoleAEntity.refMofId();
210: lEdgeStyle.put("CardinalityFrom", lRoleB
211: .getCardinality());
212: lEdgeStyle.put("CardinalityTo", lRoleA
213: .getCardinality());
214: lEdgeStyle.put("FromName", lRoleB.getName());
215: lEdgeStyle.put("ToName", lRoleA.getName());
216: } else if (lRoleB.getAggregationType().equals(
217: AggregationTypeEnum.AGGREGATION)) {
218: lEdgeStyle = getAggregationStyle();
219: lEdgeStyle.put("1", "1");
220: if (lRoleBEntity != null)
221: lFromID = lRoleBEntity.refMofId();
222: if (lRoleAEntity != null)
223: lToID = lRoleAEntity.refMofId();
224: lEdgeStyle.put("CardinalityFrom", lRoleB
225: .getCardinality());
226: lEdgeStyle.put("CardinalityTo", lRoleA
227: .getCardinality());
228: lEdgeStyle.put("FromName", lRoleB.getName());
229: lEdgeStyle.put("ToName", lRoleA.getName());
230: } else {
231: // Should never happen. Throw exception. Never allow Association without either aggregation
232: // or composition on one end
233: throw new Exception("Unexpected condition.");
234: }
235: }
236: return new AssociationParams(lFromID, lToID, lEdgeStyle);
237: }
238: return null;
239: }
240:
241: // add Association Cell
242: public void addAssociationCell(Association pAssociation,
243: Point pLabelPosition, java.util.List pPoints)
244: throws Exception {
245: if (pAssociation == null)
246: return;
247:
248: java.util.List lList = pAssociation.getRoles();
249: AssociationRole lRoleA = (lList != null && lList.size() > 0) ? (AssociationRole) lList
250: .get(0)
251: : null;
252: AssociationRole lRoleB = (lList != null && lList.size() > 1) ? (AssociationRole) lList
253: .get(1)
254: : null;
255: AssociationParams lParams = fillAssociationParams(pAssociation);
256:
257: if (lParams != null) {
258: // create circled edge
259: if (lRoleA != null
260: && lRoleB != null
261: && lRoleA.getEntity() != null
262: && lRoleB.getEntity() != null
263: && lRoleA.getEntity().getRef().equals(
264: lRoleB.getEntity().getRef())) {
265: if (pPoints == null)
266: pPoints = new ArrayList();
267: if (pPoints.size() == 0)
268: createCircledPoints(
269: getPosition(lRoleA.getEntity()), pPoints);
270: }
271: addEdge(new AssociationUserObject(pAssociation),
272: pLabelPosition, pPoints, lParams.mFromID,
273: lParams.mToID, lParams.mEdgeStyle);
274: }
275: }
276:
277: // check association edge
278: public void checkAssociation(Association pAssociation, Map pViewMap)
279: throws Exception {
280: DefaultEdge lAssociationEdge = findAssociationCell(pAssociation);
281: //if (lAssociationEdge==null)
282: // addAssociationCell(pAssociation, null, null);
283: //else
284: if (lAssociationEdge != null) {
285: AssociationParams lParams = fillAssociationParams(pAssociation);
286: if (lParams != null) {
287: lAssociationEdge.setSource(getPort(lParams.mFromID));
288: lAssociationEdge.setTarget(getPort(lParams.mToID));
289:
290: java.util.List lList = GraphConstants
291: .clonePoints(GraphConstants
292: .getPoints(lAssociationEdge
293: .getAttributes()));
294: Point lPoint = GraphConstants
295: .getLabelPosition(lAssociationEdge
296: .getAttributes());
297: lAssociationEdge.changeAttributes(lParams.mEdgeStyle);
298: GraphConstants.setPoints(lAssociationEdge
299: .getAttributes(), lList);
300: GraphConstants.setLabelPosition(lAssociationEdge
301: .getAttributes(), lPoint);
302:
303: pViewMap.put(lAssociationEdge, lAssociationEdge
304: .getAttributes());
305: }
306: }
307: }
308:
309: // add generalization cell
310: public DefaultEdge addGeneralization(Entity pEntity,
311: java.util.List pPoints) throws Exception {
312: DefaultEdge lResult = null;
313: if (pEntity != null) {
314: String lTarget = (pEntity.getSupertype() != null) ? pEntity
315: .getSupertype().refMofId() : null;
316: lResult = addEdge(new EntityGeneralizationUserObject(
317: pEntity), null, pPoints, pEntity.refMofId(),
318: lTarget, getGeneralizationStyle());
319: }
320: return lResult;
321: }
322:
323: // Check Generalizations
324: public void checkGeneralizationsForEntity(Entity pEntity,
325: Map pViewMap) throws Exception {
326: if (pEntity != null) {
327: Object[] lEdjes = getEntityGeneralizationsCells(pEntity);
328: if (lEdjes.length > 0)
329: remove(lEdjes);
330:
331: Entity lSuperEntity = pEntity.getSupertype();
332: if (lSuperEntity != null) {
333: DefaultGraphCell lEntityCell = findElementCell(pEntity);
334: DefaultGraphCell lSuperEntityCell = findElementCell(lSuperEntity);
335: if (lEntityCell != null && lSuperEntityCell != null) {
336: DefaultEdge lEdge = addGeneralization(pEntity, null);
337: if (lEdge != null && pViewMap != null)
338: pViewMap.put(lEdge, lEdge.getAttributes());
339: }
340: }
341: }
342: }
343:
344: // get Generalizations
345: public Object[] getEntityGeneralizationsCells(Entity pEntity)
346: throws Exception {
347: ArrayList pList = new ArrayList();
348: if (pEntity != null) {
349: Set lEdgesSet = getEdges(this ,
350: getUserObjectCells(new EntityUserObject(pEntity)));
351: Object[] lEdjes = lEdgesSet.toArray();
352:
353: for (int i = 0; i < lEdjes.length; i++) {
354: DefaultEdge lEdge = (DefaultEdge) lEdjes[i];
355: Object lObject = lEdge.getUserObject();
356: if (lObject instanceof EntityGeneralizationUserObject) {
357: if (((EntityGeneralizationUserObject) lObject)
358: .getEntity().equals(pEntity)) {
359: pList.add(lEdge);
360: //break;
361: }
362: }
363: }
364: }
365: return pList.toArray();
366: }
367:
368: // find BOEntity graph cell
369: public DefaultGraphCell findEntityCell(Entity pEntity) {
370: return findElementCell(pEntity);
371: }
372:
373: // find BOAssociation edge cell
374: public DefaultEdge findAssociationCell(Association pAssociation)
375: throws Exception {
376: return (DefaultEdge) findElementCell(pAssociation);
377: }
378:
379: public Map getVertexStyle() {
380: Map lResult = super .getVertexStyle();
381: GraphConstants.setBackground(lResult, new Color(0xA6, 0xCA,
382: 0xF0));
383: return lResult;
384: }
385:
386: // process cell change
387: protected void processCellChange(DefaultGraphCell pCell) {
388: super .processCellChange(pCell);
389: if (pCell == null)
390: return;
391:
392: Object lChangedCellUserObject = pCell.getUserObject();
393: if (lChangedCellUserObject instanceof EntityGeneralizationUserObject) {
394: EntityGeneralizationUserObject lGeneralization = (EntityGeneralizationUserObject) lChangedCellUserObject;
395: Object lTargetEntityObject = ((DefaultEdge) pCell)
396: .getTarget();
397: Object lSourceEntityObject = ((DefaultEdge) pCell)
398: .getSource();
399: if (lSourceEntityObject != null
400: && ((DefaultPort) lSourceEntityObject)
401: .getUserObject() instanceof EntityUserObject) {
402: try {
403: Entity lTargetEntity = null;
404: Entity lSourceEntity = null;
405:
406: if (lTargetEntityObject != null) {
407: Object lObject = ((DefaultPort) lTargetEntityObject)
408: .getUserObject();
409: if (lObject != null
410: && lObject instanceof EntityUserObject)
411: lTargetEntity = ((EntityUserObject) ((DefaultPort) lTargetEntityObject)
412: .getUserObject()).getEntity();
413: }
414: if (lSourceEntityObject != null) {
415: Object lObject = ((DefaultPort) lSourceEntityObject)
416: .getUserObject();
417: if (lObject != null
418: && lObject instanceof EntityUserObject)
419: lSourceEntity = ((EntityUserObject) ((DefaultPort) lSourceEntityObject)
420: .getUserObject()).getEntity();
421: }
422:
423: boolean lRefresh = false;
424: if (lGeneralization
425: .isSuperTypeChanged(lTargetEntity)) {
426: lGeneralization.setNewSuperType(lTargetEntity);
427: lRefresh = true;
428: }
429: if (lGeneralization.isEnityChanged(lSourceEntity)) {
430: lGeneralization.setEntity(lSourceEntity);
431: lRefresh = true;
432: }
433: if (lRefresh)
434: Application.fireRefreshDiagram(mDiagram,
435: lGeneralization);
436: } catch (Exception e) {
437: e.printStackTrace();
438: }
439: }
440: } else if (lChangedCellUserObject instanceof AssociationUserObject) {
441: AssociationUserObject lAssociationObject = (AssociationUserObject) lChangedCellUserObject;
442: Object lSourceEntityObject = ((DefaultEdge) pCell)
443: .getSource();
444: Object lTargetEntityObject = ((DefaultEdge) pCell)
445: .getTarget();
446: try {
447: Entity lRoleAEntity = null;
448: Entity lRoleBEntity = null;
449:
450: if (pCell.getAttributes().get("1") != null) {
451: if (lSourceEntityObject != null
452: && ((DefaultPort) lSourceEntityObject)
453: .getUserObject() instanceof EntityUserObject)
454: lRoleBEntity = ((EntityUserObject) ((DefaultPort) lSourceEntityObject)
455: .getUserObject()).getEntity();
456: if (lTargetEntityObject != null
457: && ((DefaultPort) lTargetEntityObject)
458: .getUserObject() instanceof EntityUserObject)
459: lRoleAEntity = ((EntityUserObject) ((DefaultPort) lTargetEntityObject)
460: .getUserObject()).getEntity();
461: } else {
462: if (lSourceEntityObject != null
463: && ((DefaultPort) lSourceEntityObject)
464: .getUserObject() instanceof EntityUserObject)
465: lRoleAEntity = ((EntityUserObject) ((DefaultPort) lSourceEntityObject)
466: .getUserObject()).getEntity();
467: if (lTargetEntityObject != null
468: && ((DefaultPort) lTargetEntityObject)
469: .getUserObject() instanceof EntityUserObject)
470: lRoleBEntity = ((EntityUserObject) ((DefaultPort) lTargetEntityObject)
471: .getUserObject()).getEntity();
472: }
473:
474: Application.beginTransaction();
475: try {
476: lAssociationObject.setNewEntities(lRoleAEntity,
477: lRoleBEntity);
478: Application.commit();
479: Application.fireRefreshDiagram(mDiagram,
480: lAssociationObject);
481: } finally {
482: Application.checkAndRollback();
483: }
484: } catch (Exception e) {
485: e.printStackTrace();
486: }
487: }
488: }
489:
490: // save cell position
491: protected void saveCellPosition(DefaultGraphCell pCell)
492: throws Exception {
493: if (pCell != null) {
494: Object lCellUserObject = pCell.getUserObject();
495: if (lCellUserObject instanceof EntityGeneralizationUserObject) {
496: ModelElement lElement = ((BaseUserObject) lCellUserObject)
497: .getBOObject();
498: saveEdgePoints(lElement, GraphConstants.getPoints(pCell
499: .getAttributes()));
500: return;
501: }
502: }
503: super .saveCellPosition(pCell);
504: }
505:
506: /*protected Object[] getVertexesObjects()
507: {
508: Domain lDomain = getDomain();
509: if (lDomain!=null) return lDomain.getEntities().toArray();
510: return null;
511: }*/
512: }
|