KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > util > ProjectUtil


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.modeler.util;
57
58 import java.util.ArrayList JavaDoc;
59 import java.util.Collection JavaDoc;
60 import java.util.Collections JavaDoc;
61 import java.util.Iterator JavaDoc;
62 import java.util.List JavaDoc;
63
64 import org.objectstyle.cayenne.access.DataDomain;
65 import org.objectstyle.cayenne.access.DataNode;
66 import org.objectstyle.cayenne.conf.Configuration;
67 import org.objectstyle.cayenne.map.Attribute;
68 import org.objectstyle.cayenne.map.DataMap;
69 import org.objectstyle.cayenne.map.DbAttribute;
70 import org.objectstyle.cayenne.map.DbEntity;
71 import org.objectstyle.cayenne.map.DbJoin;
72 import org.objectstyle.cayenne.map.DbRelationship;
73 import org.objectstyle.cayenne.map.Entity;
74 import org.objectstyle.cayenne.map.EntityResolver;
75 import org.objectstyle.cayenne.map.MappingNamespace;
76 import org.objectstyle.cayenne.map.ObjAttribute;
77 import org.objectstyle.cayenne.map.ObjEntity;
78 import org.objectstyle.cayenne.map.ObjRelationship;
79 import org.objectstyle.cayenne.map.Procedure;
80 import org.objectstyle.cayenne.map.ProcedureParameter;
81 import org.objectstyle.cayenne.map.Relationship;
82 import org.objectstyle.cayenne.query.Query;
83 import org.objectstyle.cayenne.util.Util;
84
85 /**
86  * Provides utility methods to perform various manipulations with project objects.
87  */

88 public class ProjectUtil {
89
90     public static void setProcedureParameterName(
91             ProcedureParameter parameter,
92             String JavaDoc newName) {
93
94         String JavaDoc oldName = parameter.getName();
95
96         // If name hasn't changed, just return
97
if (Util.nullSafeEquals(oldName, newName)) {
98             return;
99         }
100
101         Procedure procedure = parameter.getEntity();
102         procedure.removeCallParameter(parameter.getName());
103         parameter.setName(newName);
104         procedure.addCallParameter(parameter);
105     }
106
107     public static void setDataMapName(DataDomain domain, DataMap map, String JavaDoc newName) {
108         String JavaDoc oldName = map.getName();
109
110         // If name hasn't changed, just return
111
if (Util.nullSafeEquals(oldName, newName)) {
112             return;
113         }
114
115         // must fully relink renamed map
116
List JavaDoc nodes = new ArrayList JavaDoc();
117         Iterator JavaDoc allNodes = domain.getDataNodes().iterator();
118         while (allNodes.hasNext()) {
119             DataNode node = (DataNode) allNodes.next();
120             if (node.getDataMaps().contains(map)) {
121                 nodes.add(node);
122             }
123         }
124
125         domain.removeMap(oldName);
126         map.setName(newName);
127         domain.addMap(map);
128
129         Iterator JavaDoc relinkNodes = nodes.iterator();
130         while (relinkNodes.hasNext()) {
131             DataNode node = (DataNode) relinkNodes.next();
132             node.removeDataMap(oldName);
133             node.addDataMap(map);
134         }
135     }
136
137     public static void setDataDomainName(
138             Configuration configuration,
139             DataDomain domain,
140             String JavaDoc newName) {
141
142         String JavaDoc oldName = domain.getName();
143         // If name hasn't changed, just return
144
if (Util.nullSafeEquals(oldName, newName)) {
145             return;
146         }
147
148         domain.setName(newName);
149         configuration.removeDomain(oldName);
150         configuration.addDomain(domain);
151     }
152
153     public static void setDataNodeName(DataDomain domain, DataNode node, String JavaDoc newName) {
154         String JavaDoc oldName = node.getName();
155         node.setName(newName);
156         domain.removeDataNode(oldName);
157         domain.addNode(node);
158     }
159
160     public static void setProcedureName(DataMap map, Procedure procedure, String JavaDoc newName) {
161
162         String JavaDoc oldName = procedure.getName();
163
164         // If name hasn't changed, just return
165
if (Util.nullSafeEquals(oldName, newName)) {
166             return;
167         }
168
169         procedure.setName(newName);
170         map.removeProcedure(oldName);
171         map.addProcedure(procedure);
172
173         // important - clear parent namespace:
174
MappingNamespace ns = map.getNamespace();
175         if (ns instanceof EntityResolver) {
176             ((EntityResolver) ns).clearCache();
177         }
178     }
179
180     public static void setQueryName(DataMap map, Query query, String JavaDoc newName) {
181
182         String JavaDoc oldName = query.getName();
183
184         // If name hasn't changed, just return
185
if (Util.nullSafeEquals(oldName, newName)) {
186             return;
187         }
188
189         query.setName(newName);
190         map.removeQuery(oldName);
191         map.addQuery(query);
192
193         // important - clear parent namespace:
194
MappingNamespace ns = map.getNamespace();
195         if (ns instanceof EntityResolver) {
196             ((EntityResolver) ns).clearCache();
197         }
198     }
199
200     public static void setObjEntityName(DataMap map, ObjEntity entity, String JavaDoc newName) {
201         String JavaDoc oldName = entity.getName();
202
203         // If name hasn't changed, just return
204
if (Util.nullSafeEquals(oldName, newName)) {
205             return;
206         }
207
208         entity.setName(newName);
209         map.removeObjEntity(oldName, false);
210         map.addObjEntity(entity);
211
212         // important - clear parent namespace:
213
MappingNamespace ns = map.getNamespace();
214         if (ns instanceof EntityResolver) {
215             ((EntityResolver) ns).clearCache();
216         }
217     }
218
219     /**
220      * Renames a DbEntity and changes the name of all references.
221      */

222     public static void setDbEntityName(DbEntity entity, String JavaDoc newName) {
223         String JavaDoc oldName = entity.getName();
224
225         // If name hasn't changed, just return
226
if (Util.nullSafeEquals(oldName, newName)) {
227             return;
228         }
229
230         entity.setName(newName);
231         DataMap map = entity.getDataMap();
232
233         if (map != null) {
234             map.removeDbEntity(oldName, false);
235             map.addDbEntity(entity);
236
237             // important - clear parent namespace:
238
MappingNamespace ns = map.getNamespace();
239             if (ns instanceof EntityResolver) {
240                 ((EntityResolver) ns).clearCache();
241             }
242         }
243     }
244
245     /**
246      * Changes the name of the attribute and all references to this attribute.
247      */

248     public static void setAttributeName(Attribute attribute, String JavaDoc newName) {
249         String JavaDoc oldName = attribute.getName();
250
251         attribute.setName(newName);
252
253         Entity entity = attribute.getEntity();
254
255         if (entity != null) {
256             entity.removeAttribute(oldName);
257             entity.addAttribute(attribute);
258         }
259     }
260
261     /** Changes the name of the attribute in all places in DataMap. */
262     public static void setRelationshipName(Entity entity, Relationship rel, String JavaDoc newName) {
263
264         if (rel == null || rel != entity.getRelationship(rel.getName())) {
265             return;
266         }
267
268         entity.removeRelationship(rel.getName());
269         rel.setName(newName);
270         entity.addRelationship(rel);
271     }
272
273     /**
274      * Cleans any mappings of ObjEntities, ObjAttributes, ObjRelationship to the
275      * corresponding Db* objects that not longer exist.
276      */

277     public static void cleanObjMappings(DataMap map) {
278         Iterator JavaDoc ents = map.getObjEntities().iterator();
279         while (ents.hasNext()) {
280             ObjEntity ent = (ObjEntity) ents.next();
281             DbEntity dbEnt = ent.getDbEntity();
282
283             // the whole entity mapping is invalid
284
if (dbEnt != null && map.getDbEntity(dbEnt.getName()) != dbEnt) {
285                 clearDbMapping(ent);
286                 continue;
287             }
288
289             // check indiv. attributes
290
Iterator JavaDoc atts = ent.getAttributes().iterator();
291             while (atts.hasNext()) {
292                 ObjAttribute att = (ObjAttribute) atts.next();
293                 DbAttribute dbAtt = att.getDbAttribute();
294                 if (dbAtt != null) {
295                     if (dbEnt.getAttribute(dbAtt.getName()) != dbAtt) {
296                         att.setDbAttribute(null);
297                     }
298                 }
299             }
300
301             // check indiv. relationships
302
Iterator JavaDoc rels = ent.getRelationships().iterator();
303             while (rels.hasNext()) {
304                 ObjRelationship rel = (ObjRelationship) rels.next();
305
306                 Iterator JavaDoc dbRels = new ArrayList JavaDoc(rel.getDbRelationships()).iterator();
307                 while (dbRels.hasNext()) {
308                     DbRelationship dbRel = (DbRelationship) dbRels.next();
309                     Entity srcEnt = dbRel.getSourceEntity();
310                     if (srcEnt == null
311                             || map.getDbEntity(srcEnt.getName()) != srcEnt
312                             || srcEnt.getRelationship(dbRel.getName()) != dbRel) {
313                         rel.removeDbRelationship(dbRel);
314                     }
315                 }
316             }
317
318         }
319     }
320
321     /**
322      * Clears all the mapping between this obj entity and its current db entity. Clears
323      * mapping between entities, attributes and relationships.
324      */

325     public static void clearDbMapping(ObjEntity entity) {
326         DbEntity db_entity = entity.getDbEntity();
327         if (db_entity == null) {
328             return;
329         }
330
331         Iterator JavaDoc it = entity.getAttributeMap().values().iterator();
332         while (it.hasNext()) {
333             ObjAttribute objAttr = (ObjAttribute) it.next();
334             DbAttribute dbAttr = objAttr.getDbAttribute();
335             if (null != dbAttr) {
336                 objAttr.setDbAttribute(null);
337             }
338         }
339
340         Iterator JavaDoc rel_it = entity.getRelationships().iterator();
341         while (rel_it.hasNext()) {
342             ObjRelationship obj_rel = (ObjRelationship) rel_it.next();
343             obj_rel.clearDbRelationships();
344         }
345         entity.setDbEntity(null);
346     }
347
348     /**
349      * Returns true if one of relationship joins uses a given attribute as a source.
350      */

351     public static boolean containsSourceAttribute(
352             DbRelationship relationship,
353             DbAttribute attribute) {
354         if (attribute.getEntity() != relationship.getSourceEntity()) {
355             return false;
356         }
357
358         Iterator JavaDoc it = relationship.getJoins().iterator();
359         while (it.hasNext()) {
360             DbJoin join = (DbJoin) it.next();
361             if (join.getSource() == attribute) {
362                 return true;
363             }
364         }
365
366         return false;
367     }
368
369     /**
370      * Returns true if one of relationship joins uses a given attribute as a target.
371      */

372     public static boolean containsTargetAttribute(
373             DbRelationship relationship,
374             DbAttribute attribute) {
375         if (attribute.getEntity() != relationship.getTargetEntity()) {
376             return false;
377         }
378
379         Iterator JavaDoc it = relationship.getJoins().iterator();
380         while (it.hasNext()) {
381             DbJoin join = (DbJoin) it.next();
382             if (join.getTarget() == attribute) {
383                 return true;
384             }
385         }
386
387         return false;
388     }
389
390     /**
391      * Returns a collection of DbRelationships that use this attribute as a source.
392      */

393     public static Collection JavaDoc getRelationshipsUsingAttributeAsSource(DbAttribute attribute) {
394         Entity parent = attribute.getEntity();
395
396         if (parent == null) {
397             return Collections.EMPTY_LIST;
398         }
399
400         Collection JavaDoc parentRelationships = parent.getRelationships();
401         Collection JavaDoc relationships = new ArrayList JavaDoc(parentRelationships.size());
402         Iterator JavaDoc it = parentRelationships.iterator();
403         while (it.hasNext()) {
404             DbRelationship relationship = (DbRelationship) it.next();
405             if (ProjectUtil.containsSourceAttribute(relationship, attribute)) {
406                 relationships.add(relationship);
407             }
408         }
409         return relationships;
410     }
411
412     /**
413      * Returns a collection of DbRelationships that use this attribute as a source.
414      */

415     public static Collection JavaDoc getRelationshipsUsingAttributeAsTarget(DbAttribute attribute) {
416         Entity parent = attribute.getEntity();
417
418         if (parent == null) {
419             return Collections.EMPTY_LIST;
420         }
421
422         DataMap map = parent.getDataMap();
423         if (map == null) {
424             return Collections.EMPTY_LIST;
425         }
426
427         Collection JavaDoc relationships = new ArrayList JavaDoc();
428
429         Iterator JavaDoc it = map.getDbEntities().iterator();
430         while (it.hasNext()) {
431             Entity entity = (Entity) it.next();
432             if (entity == parent) {
433                 continue;
434             }
435
436             Collection JavaDoc entityRelationships = entity.getRelationships();
437             Iterator JavaDoc relationshipsIt = entityRelationships.iterator();
438             while (relationshipsIt.hasNext()) {
439                 DbRelationship relationship = (DbRelationship) relationshipsIt.next();
440                 if (ProjectUtil.containsTargetAttribute(relationship, attribute)) {
441                     relationships.add(relationship);
442                 }
443             }
444         }
445         return relationships;
446     }
447 }
Popular Tags