KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > wocompat > EOModelProcessor


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20
21 package org.apache.cayenne.wocompat;
22
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Comparator JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.StringTokenizer JavaDoc;
34
35 import org.apache.cayenne.dba.TypesMapping;
36 import org.apache.cayenne.map.DataMap;
37 import org.apache.cayenne.map.DbEntity;
38 import org.apache.cayenne.map.DbJoin;
39 import org.apache.cayenne.map.DbRelationship;
40 import org.apache.cayenne.map.ObjEntity;
41 import org.apache.cayenne.map.ObjRelationship;
42 import org.apache.cayenne.project.NamedObjectFactory;
43 import org.apache.cayenne.query.AbstractQuery;
44 import org.apache.cayenne.query.Query;
45 import org.apache.cayenne.util.ResourceLocator;
46 import org.apache.cayenne.wocompat.parser.Parser;
47 import org.apache.commons.collections.CollectionUtils;
48 import org.apache.commons.collections.Predicate;
49 import org.apache.commons.collections.PredicateUtils;
50
51 /**
52  * Class for converting stored Apple EOModel mapping files to Cayenne DataMaps.
53  */

54 public class EOModelProcessor {
55
56     protected Predicate prototypeChecker;
57
58     public EOModelProcessor() {
59         prototypeChecker = new Predicate() {
60
61             public boolean evaluate(Object JavaDoc object) {
62                 if (object == null) {
63                     return false;
64                 }
65
66                 String JavaDoc entityName = object.toString();
67                 return entityName.startsWith("EO") && entityName.endsWith("Prototypes");
68             }
69         };
70     }
71
72     /**
73      * Returns index.eomodeld contents as a Map.
74      *
75      * @since 1.1
76      */

77     // TODO: refactor EOModelHelper to provide a similar method without loading
78
// all entity files in memory... here we simply copied stuff from EOModelHelper
79
public Map JavaDoc loadModeIndex(String JavaDoc path) throws Exception JavaDoc {
80
81         ResourceLocator locator = new ResourceLocator();
82         locator.setSkipClasspath(false);
83         locator.setSkipCurrentDirectory(false);
84         locator.setSkipHomeDirectory(true);
85         locator.setSkipAbsolutePath(false);
86
87         if (!path.endsWith(".eomodeld")) {
88             path += ".eomodeld";
89         }
90
91         URL JavaDoc base = locator.findDirectoryResource(path);
92         if (base == null) {
93             throw new FileNotFoundException JavaDoc("Can't find EOModel: " + path);
94         }
95
96         Parser plistParser = new Parser();
97         InputStream JavaDoc in = new URL JavaDoc(base, "index.eomodeld").openStream();
98
99         try {
100             plistParser.ReInit(in);
101             return (Map JavaDoc) plistParser.propertyList();
102         }
103         finally {
104             in.close();
105         }
106     }
107
108     /**
109      * Performs EOModel loading.
110      *
111      * @param path A path to ".eomodeld" directory. If path doesn't end with ".eomodeld",
112      * ".eomodeld" suffix is automatically assumed.
113      */

114     public DataMap loadEOModel(String JavaDoc path) throws Exception JavaDoc {
115         return loadEOModel(path, false);
116     }
117
118     /**
119      * Performs EOModel loading.
120      *
121      * @param path A path to ".eomodeld" directory. If path doesn't end with ".eomodeld",
122      * ".eomodeld" suffix is automatically assumed.
123      * @param generateClientClass if true then loading of EOModel is java client classes
124      * aware and the following processing will work with Java client class
125      * settings of the EOModel.
126      */

127     public DataMap loadEOModel(String JavaDoc path, boolean generateClientClass) throws Exception JavaDoc {
128         EOModelHelper helper = makeHelper(path, generateClientClass);
129
130         // create empty map
131
DataMap dataMap = helper.getDataMap();
132
133         // process enitities ... throw out prototypes ... for now
134
List JavaDoc modelNames = new ArrayList JavaDoc(helper.modelNamesAsList());
135         CollectionUtils.filter(modelNames, PredicateUtils.notPredicate(prototypeChecker));
136
137         Iterator JavaDoc it = modelNames.iterator();
138         while (it.hasNext()) {
139             String JavaDoc name = (String JavaDoc) it.next();
140
141             // create and register entity
142
makeEntity(helper, name, generateClientClass);
143         }
144
145         // now sort following inheritance hierarchy
146
Collections.sort(modelNames, new InheritanceComparator(dataMap));
147
148         // after all entities are loaded, process attributes
149
it = modelNames.iterator();
150         while (it.hasNext()) {
151             String JavaDoc name = (String JavaDoc) it.next();
152
153             EOObjEntity e = (EOObjEntity) dataMap.getObjEntity(name);
154             // process entity attributes
155
makeAttributes(helper, e);
156         }
157
158         // after all entities are loaded, process relationships
159
it = modelNames.iterator();
160         while (it.hasNext()) {
161             String JavaDoc name = (String JavaDoc) it.next();
162             makeRelationships(helper, dataMap.getObjEntity(name));
163         }
164
165         // after all normal relationships are loaded, process flattened relationships
166
it = modelNames.iterator();
167         while (it.hasNext()) {
168             String JavaDoc name = (String JavaDoc) it.next();
169             makeFlatRelationships(helper, dataMap.getObjEntity(name));
170         }
171
172         // now create missing reverse DB (but not OBJ) relationships
173
// since Cayenne requires them
174
it = modelNames.iterator();
175         while (it.hasNext()) {
176             String JavaDoc name = (String JavaDoc) it.next();
177             DbEntity dbEntity = dataMap.getObjEntity(name).getDbEntity();
178
179             if (dbEntity != null) {
180                 makeReverseDbRelationships(dbEntity);
181             }
182         }
183
184         // build SelectQueries out of EOFetchSpecifications...
185
it = modelNames.iterator();
186         while (it.hasNext()) {
187             String JavaDoc name = (String JavaDoc) it.next();
188             Iterator JavaDoc queries = helper.queryNames(name);
189             while (queries.hasNext()) {
190                 String JavaDoc queryName = (String JavaDoc) queries.next();
191                 EOObjEntity entity = (EOObjEntity) dataMap.getObjEntity(name);
192                 makeQuery(helper, entity, queryName);
193             }
194         }
195
196         return dataMap;
197     }
198
199     /**
200      * Returns whether an Entity is an EOF EOPrototypes entity. According to EOF
201      * conventions EOPrototypes and EO[Adapter]Prototypes entities are considered to be
202      * prototypes.
203      *
204      * @since 1.1
205      */

206     protected boolean isPrototypesEntity(String JavaDoc entityName) {
207         return prototypeChecker.evaluate(entityName);
208     }
209
210     /**
211      * Creates an returns new EOModelHelper to process EOModel. Exists mostly for the
212      * benefit of subclasses.
213      */

214     protected EOModelHelper makeHelper(String JavaDoc path, boolean genereateClientClass)
215             throws Exception JavaDoc {
216         return new EOModelHelper(path);
217     }
218
219     /**
220      * Creates a Cayenne query out of EOFetchSpecification data.
221      *
222      * @since 1.1
223      */

224     protected Query makeQuery(EOModelHelper helper, EOObjEntity entity, String JavaDoc queryName) {
225
226         DataMap dataMap = helper.getDataMap();
227         Map JavaDoc queryPlist = helper.queryPListMap(entity.getName(), queryName);
228         if (queryPlist == null) {
229             return null;
230         }
231         
232         AbstractQuery query;
233         if (queryPlist.containsKey("hints")) { // just a predefined SQL query
234
query = new EOSQLQuery(entity, queryPlist);
235         } else {
236                 query = new EOQuery(entity, queryPlist);
237         }
238         query.setName(entity.qualifiedQueryName(queryName));
239         dataMap.addQuery(query);
240         
241         return query;
242     }
243
244     /**
245      * Creates and returns a new ObjEntity linked to a corresponding DbEntity.
246      */

247     protected EOObjEntity makeEntity(
248             EOModelHelper helper,
249             String JavaDoc name,
250             boolean generateClientClass) {
251
252         DataMap dataMap = helper.getDataMap();
253         Map JavaDoc entityPlist = helper.entityPListMap(name);
254
255         // create ObjEntity
256
EOObjEntity objEntity = new EOObjEntity(name);
257         objEntity.setEoMap(entityPlist);
258         objEntity.setServerOnly(!generateClientClass);
259         String JavaDoc parent = (String JavaDoc) entityPlist.get("parent");
260         objEntity.setClassName(helper.entityClass(name, generateClientClass));
261
262         if (parent != null) {
263             objEntity.setSubclass(true);
264             objEntity.setSuperClassName(helper.entityClass(parent, generateClientClass));
265         }
266
267         // add flag whether this entity is set as abstract in the model
268
objEntity.setAbstractEntity("Y".equals(entityPlist.get("isAbstractEntity")));
269
270         // create DbEntity...since EOF allows the same table to be
271
// associated with multiple EOEntities, check for name duplicates
272
String JavaDoc dbEntityName = (String JavaDoc) entityPlist.get("externalName");
273         if (dbEntityName != null) {
274
275             // ... if inheritance is involved and parent hierarchy uses the same DBEntity,
276
// do not create a DbEntity...
277
boolean createDbEntity = true;
278             if (parent != null) {
279                 String JavaDoc parentName = parent;
280                 while (parentName != null) {
281                     Map JavaDoc parentData = helper.entityPListMap(parentName);
282                     if (parentData == null) {
283                         break;
284                     }
285
286                     String JavaDoc parentExternalName = (String JavaDoc) parentData.get("externalName");
287                     if (parentExternalName == null) {
288                         parentName = (String JavaDoc) parentData.get("parent");
289                         continue;
290                     }
291
292                     if (dbEntityName.equals(parentExternalName)) {
293                         createDbEntity = false;
294                     }
295
296                     break;
297                 }
298             }
299
300             if (createDbEntity) {
301                 int i = 0;
302                 String JavaDoc dbEntityBaseName = dbEntityName;
303                 while (dataMap.getDbEntity(dbEntityName) != null) {
304                     dbEntityName = dbEntityBaseName + i++;
305                 }
306
307                 objEntity.setDbEntityName(dbEntityName);
308                 DbEntity de = new DbEntity(dbEntityName);
309                 dataMap.addDbEntity(de);
310             }
311         }
312
313         // set various flags
314
objEntity.setReadOnly("Y".equals(entityPlist.get("isReadOnly")));
315         objEntity.setSuperEntityName((String JavaDoc) entityPlist.get("parent"));
316
317         dataMap.addObjEntity(objEntity);
318
319         return objEntity;
320     }
321
322     /**
323      * Create ObjAttributes of the specified entity, as well as DbAttributes of the
324      * corresponding DbEntity.
325      */

326     protected void makeAttributes(EOModelHelper helper, EOObjEntity objEntity) {
327         Map JavaDoc entityPlistMap = helper.entityPListMap(objEntity.getName());
328         List JavaDoc primaryKeys = (List JavaDoc) entityPlistMap.get("primaryKeyAttributes");
329
330         List JavaDoc classProperties;
331         if (objEntity.isServerOnly()) {
332             classProperties = (List JavaDoc) entityPlistMap.get("classProperties");
333         }
334         else {
335             classProperties = (List JavaDoc) entityPlistMap.get("clientClassProperties");
336         }
337
338         List JavaDoc attributes = (List JavaDoc) entityPlistMap.get("attributes");
339         DbEntity dbEntity = objEntity.getDbEntity();
340
341         if (primaryKeys == null) {
342             primaryKeys = Collections.EMPTY_LIST;
343         }
344
345         if (classProperties == null) {
346             classProperties = Collections.EMPTY_LIST;
347         }
348
349         if (attributes == null) {
350             attributes = Collections.EMPTY_LIST;
351         }
352
353         // detect single table inheritance
354
boolean singleTableInheritance = false;
355         String JavaDoc parentName = (String JavaDoc) entityPlistMap.get("parent");
356         while (parentName != null) {
357             Map JavaDoc parentData = helper.entityPListMap(parentName);
358             if (parentData == null) {
359                 break;
360             }
361
362             String JavaDoc parentExternalName = (String JavaDoc) parentData.get("externalName");
363             if (parentExternalName == null) {
364                 parentName = (String JavaDoc) parentData.get("parent");
365                 continue;
366             }
367
368             if (dbEntity.getName() != null
369                     && dbEntity.getName().equals(parentExternalName)) {
370                 singleTableInheritance = true;
371             }
372
373             break;
374         }
375
376         Iterator JavaDoc it = attributes.iterator();
377         while (it.hasNext()) {
378             Map JavaDoc attrMap = (Map JavaDoc) it.next();
379
380             String JavaDoc prototypeName = (String JavaDoc) attrMap.get("prototypeName");
381             Map JavaDoc prototypeAttrMap = helper.getPrototypeAttributeMapFor(prototypeName);
382
383             String JavaDoc dbAttrName = (String JavaDoc) attrMap.get("columnName");
384             if (null == dbAttrName) {
385                 dbAttrName = (String JavaDoc) prototypeAttrMap.get("columnName");
386             }
387
388             String JavaDoc attrName = (String JavaDoc) attrMap.get("name");
389             if (null == attrName) {
390                 attrName = (String JavaDoc) prototypeAttrMap.get("name");
391             }
392
393             String JavaDoc attrType = (String JavaDoc) attrMap.get("valueClassName");
394             if (null == attrType) {
395                 attrType = (String JavaDoc) prototypeAttrMap.get("valueClassName");
396             }
397
398             String JavaDoc valueType = (String JavaDoc) attrMap.get("valueType");
399             if (valueType == null) {
400                 valueType = (String JavaDoc) prototypeAttrMap.get("valueType");
401             }
402
403             String JavaDoc javaType = helper.javaTypeForEOModelerType(attrType, valueType);
404             EODbAttribute dbAttr = null;
405
406             if (dbAttrName != null && dbEntity != null) {
407
408                 // if inherited attribute, skip it for DbEntity...
409
if (!singleTableInheritance || dbEntity.getAttribute(dbAttrName) == null) {
410
411                     // create DbAttribute...since EOF allows the same column name for
412
// more than one Java attribute, we need to check for name duplicates
413
int i = 0;
414                     String JavaDoc dbAttributeBaseName = dbAttrName;
415                     while (dbEntity.getAttribute(dbAttrName) != null) {
416                         dbAttrName = dbAttributeBaseName + i++;
417                     }
418
419                     dbAttr = new EODbAttribute(dbAttrName, TypesMapping
420                             .getSqlTypeByJava(javaType), dbEntity);
421                     dbAttr.setEoAttributeName(attrName);
422                     dbEntity.addAttribute(dbAttr);
423
424                     int width = getInt("width", attrMap, prototypeAttrMap, -1);
425                     if (width >= 0) {
426                         dbAttr.setMaxLength(width);
427                     }
428                     
429                     int scale = getInt("scale", attrMap, prototypeAttrMap, -1);
430                     if (scale >= 0) {
431                         dbAttr.setScale(scale);
432                     }
433
434                     if (primaryKeys.contains(attrName))
435                         dbAttr.setPrimaryKey(true);
436
437                     Object JavaDoc allowsNull = attrMap.get("allowsNull");
438                     // TODO: Unclear that allowsNull should be inherited from EOPrototypes
439
// if (null == allowsNull) allowsNull =
440
// prototypeAttrMap.get("allowsNull");;
441

442                     dbAttr.setMandatory(!"Y".equals(allowsNull));
443                 }
444             }
445
446             if (classProperties.contains(attrName)) {
447                 EOObjAttribute attr = new EOObjAttribute(attrName, javaType, objEntity);
448
449                 // set readOnly flag of Attribute if either attribute is read or
450
// if entity is readOnly
451
String JavaDoc entityReadOnlyString = (String JavaDoc) entityPlistMap.get("isReadOnly");
452                 String JavaDoc attributeReadOnlyString = (String JavaDoc) attrMap.get("isReadOnly");
453                 if ("Y".equals(entityReadOnlyString)
454                         || "Y".equals(attributeReadOnlyString)) {
455                     attr.setReadOnly(true);
456                 }
457
458                 // set name instead of the actual attribute, as it may be inherited....
459
attr.setDbAttributeName(dbAttrName);
460                 objEntity.addAttribute(attr);
461             }
462         }
463     }
464     
465     int getInt(String JavaDoc key, Map JavaDoc map, Map JavaDoc prototypes, int defaultValue) {
466
467         Object JavaDoc value = map.get(key);
468         if (value == null) {
469             value = prototypes.get(key);
470         }
471
472         if (value == null) {
473             return defaultValue;
474         }
475
476         // per CAY-752, value can be a String or a Number, so handle both
477
if (value instanceof Number JavaDoc) {
478             return ((Number JavaDoc) value).intValue();
479         }
480         else {
481             try {
482                 return Integer.parseInt(value.toString());
483             }
484             catch(NumberFormatException JavaDoc nfex) {
485                 return defaultValue;
486             }
487         }
488     }
489
490     /**
491      * Create ObjRelationships of the specified entity, as well as DbRelationships of the
492      * corresponding DbEntity.
493      */

494     protected void makeRelationships(EOModelHelper helper, ObjEntity objEntity) {
495         Map JavaDoc entityPlistMap = helper.entityPListMap(objEntity.getName());
496         List JavaDoc classProps = (List JavaDoc) entityPlistMap.get("classProperties");
497         List JavaDoc rinfo = (List JavaDoc) entityPlistMap.get("relationships");
498
499         Collection JavaDoc attributes = (Collection JavaDoc) entityPlistMap.get("attributes");
500
501         if (rinfo == null) {
502             return;
503         }
504
505         if (classProps == null) {
506             classProps = Collections.EMPTY_LIST;
507         }
508
509         if (attributes == null) {
510             attributes = Collections.EMPTY_LIST;
511         }
512
513         DbEntity dbSrc = objEntity.getDbEntity();
514         Iterator JavaDoc it = rinfo.iterator();
515         while (it.hasNext()) {
516             Map JavaDoc relMap = (Map JavaDoc) it.next();
517             String JavaDoc targetName = (String JavaDoc) relMap.get("destination");
518
519             // ignore flattened relationships for now
520
if (targetName == null) {
521                 continue;
522             }
523
524             String JavaDoc relName = (String JavaDoc) relMap.get("name");
525             boolean toMany = "Y".equals(relMap.get("isToMany"));
526             boolean toDependentPK = "Y".equals(relMap.get("propagatesPrimaryKey"));
527             ObjEntity target = helper.getDataMap().getObjEntity(targetName);
528
529             // target maybe null for cross-EOModel relationships
530
// ignoring those now.
531
if (target == null) {
532                 continue;
533             }
534
535             DbEntity dbTarget = target.getDbEntity();
536             Map JavaDoc targetPlistMap = helper.entityPListMap(targetName);
537             Collection JavaDoc targetAttributes = (Collection JavaDoc) targetPlistMap.get("attributes");
538             DbRelationship dbRel = null;
539
540             // process underlying DbRelationship
541
// Note: there is no flattened rel. support here....
542
// Note: source maybe null, e.g. an abstract entity.
543
if (dbSrc != null && dbTarget != null) {
544
545                 // in case of inheritance EOF stores duplicates of all inherited
546
// relationships, so we must skip this relationship in DB entity if it is
547
// already there...
548

549                 dbRel = (DbRelationship) dbSrc.getRelationship(relName);
550                 if (dbRel == null) {
551
552                     dbRel = new DbRelationship();
553                     dbRel.setSourceEntity(dbSrc);
554                     dbRel.setTargetEntity(dbTarget);
555                     dbRel.setToMany(toMany);
556                     dbRel.setName(relName);
557                     dbRel.setToDependentPK(toDependentPK);
558                     dbSrc.addRelationship(dbRel);
559
560                     List JavaDoc joins = (List JavaDoc) relMap.get("joins");
561                     Iterator JavaDoc jIt = joins.iterator();
562                     while (jIt.hasNext()) {
563                         Map JavaDoc joinMap = (Map JavaDoc) jIt.next();
564
565                         DbJoin join = new DbJoin(dbRel);
566
567                         // find source attribute dictionary and extract the column name
568
String JavaDoc sourceAttributeName = (String JavaDoc) joinMap
569                                 .get("sourceAttribute");
570                         join.setSourceName(columnName(attributes, sourceAttributeName));
571
572                         String JavaDoc targetAttributeName = (String JavaDoc) joinMap
573                                 .get("destinationAttribute");
574
575                         join.setTargetName(columnName(
576                                 targetAttributes,
577                                 targetAttributeName));
578                         dbRel.addJoin(join);
579                     }
580                 }
581             }
582
583             // only create obj relationship if it is a class property
584
if (classProps.contains(relName)) {
585                 ObjRelationship rel = new ObjRelationship();
586                 rel.setName(relName);
587                 rel.setSourceEntity(objEntity);
588                 rel.setTargetEntity(target);
589                 objEntity.addRelationship(rel);
590
591                 if (dbRel != null) {
592                     rel.addDbRelationship(dbRel);
593                 }
594             }
595         }
596     }
597
598     /**
599      * Create reverse DbRelationships that were not created so far, since Cayenne requires
600      * them.
601      *
602      * @since 1.0.5
603      */

604     protected void makeReverseDbRelationships(DbEntity dbEntity) {
605         if (dbEntity == null) {
606             throw new NullPointerException JavaDoc(
607                     "Attempt to create reverse relationships for the null DbEntity.");
608         }
609
610         // iterate over a copy of the collection, since in case of
611
// reflexive relationships, we may modify source entity relationship map
612
Collection JavaDoc clone = new ArrayList JavaDoc(dbEntity.getRelationships());
613         Iterator JavaDoc it = clone.iterator();
614         while (it.hasNext()) {
615             DbRelationship relationship = (DbRelationship) it.next();
616
617             if (relationship.getReverseRelationship() == null) {
618                 DbRelationship reverse = relationship.createReverseRelationship();
619
620                 String JavaDoc name = NamedObjectFactory.createName(DbRelationship.class, reverse
621                         .getSourceEntity(), relationship.getName() + "Reverse");
622                 reverse.setName(name);
623                 relationship.getTargetEntity().addRelationship(reverse);
624             }
625         }
626     }
627
628     /**
629      * Create Flattened ObjRelationships of the specified entity.
630      */

631     protected void makeFlatRelationships(EOModelHelper helper, ObjEntity e) {
632         Map JavaDoc info = helper.entityPListMap(e.getName());
633         List JavaDoc rinfo = (List JavaDoc) info.get("relationships");
634         if (rinfo == null) {
635             return;
636         }
637
638         Iterator JavaDoc it = rinfo.iterator();
639         while (it.hasNext()) {
640             Map JavaDoc relMap = (Map JavaDoc) it.next();
641             String JavaDoc targetPath = (String JavaDoc) relMap.get("definition");
642
643             // ignore normal relationships
644
if (targetPath == null) {
645                 continue;
646             }
647
648             ObjRelationship flatRel = new ObjRelationship();
649             flatRel.setName((String JavaDoc) relMap.get("name"));
650             flatRel.setSourceEntity(e);
651             flatRel.setDbRelationshipPath(targetPath);
652
653             // find target entity
654
Map JavaDoc entityInfo = info;
655             StringTokenizer JavaDoc toks = new StringTokenizer JavaDoc(targetPath, ".");
656             while (toks.hasMoreTokens() && entityInfo != null) {
657                 String JavaDoc pathComponent = toks.nextToken();
658
659                 // get relationship info and reset entityInfo, so that we could use
660
// entityInfo state as an indicator of valid flat relationship enpoint
661
// outside the loop
662
Collection JavaDoc relationshipInfo = (Collection JavaDoc) entityInfo
663                         .get("relationships");
664                 entityInfo = null;
665
666                 if (relationshipInfo == null) {
667                     break;
668                 }
669
670                 Iterator JavaDoc rit = relationshipInfo.iterator();
671                 while (rit.hasNext()) {
672                     Map JavaDoc pathRelationship = (Map JavaDoc) rit.next();
673                     if (pathComponent.equals(pathRelationship.get("name"))) {
674                         String JavaDoc targetName = (String JavaDoc) pathRelationship.get("destination");
675                         entityInfo = helper.entityPListMap(targetName);
676                         break;
677                     }
678                 }
679             }
680             
681             if(entityInfo != null) {
682                 flatRel.setTargetEntityName((String JavaDoc) entityInfo.get("name"));
683             }
684             
685
686             e.addRelationship(flatRel);
687         }
688     }
689
690     /**
691      * Locates an attribute map matching the name and returns column name for this
692      * attribute.
693      *
694      * @since 1.1
695      */

696     String JavaDoc columnName(Collection JavaDoc entityAttributes, String JavaDoc attributeName) {
697         if (attributeName == null) {
698             return null;
699         }
700
701         Iterator JavaDoc it = entityAttributes.iterator();
702         while (it.hasNext()) {
703             Map JavaDoc map = (Map JavaDoc) it.next();
704             if (attributeName.equals(map.get("name"))) {
705                 return (String JavaDoc) map.get("columnName");
706             }
707         }
708
709         return null;
710     }
711
712     // sorts ObjEntities so that subentities in inheritance hierarchy are shown last
713
final class InheritanceComparator implements Comparator JavaDoc {
714
715         DataMap dataMap;
716
717         InheritanceComparator(DataMap dataMap) {
718             this.dataMap = dataMap;
719         }
720
721         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
722             if (o1 == null) {
723                 return o2 != null ? -1 : 0;
724             }
725             else if (o2 == null) {
726                 return 1;
727             }
728
729             String JavaDoc name1 = o1.toString();
730             String JavaDoc name2 = o2.toString();
731
732             ObjEntity e1 = dataMap.getObjEntity(name1);
733             ObjEntity e2 = dataMap.getObjEntity(name2);
734
735             return compareEntities(e1, e2);
736         }
737
738         int compareEntities(ObjEntity e1, ObjEntity e2) {
739             if (e1 == null) {
740                 return e2 != null ? -1 : 0;
741             }
742             else if (e2 == null) {
743                 return 1;
744             }
745
746             // entity goes first if it is a direct or indirect superentity of another
747
// one
748
if (e1.isSubentityOf(e2)) {
749                 return 1;
750             }
751
752             if (e2.isSubentityOf(e1)) {
753                 return -1;
754             }
755
756             // sort alphabetically
757
return e1.getName().compareTo(e2.getName());
758         }
759     }
760 }
761
Popular Tags