KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > util > EntityTypeUtil


1 /*
2  * $Id: EntityTypeUtil.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.entity.util;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 import org.ofbiz.base.util.Debug;
33 import org.ofbiz.entity.GenericEntityException;
34 import org.ofbiz.entity.GenericValue;
35
36 /**
37  * Makes it easier to deal with entities that follow the
38  * extensibility pattern and that can be of various types as identified in the database.
39  *
40  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
41  * @version $Rev: 5462 $
42  * @since 2.0
43  */

44 public class EntityTypeUtil {
45     
46     public static final String JavaDoc module = EntityTypeUtil.class.getName();
47
48     public static boolean isType(Collection JavaDoc thisCollection, String JavaDoc typeRelation, GenericValue targetType) {
49         Iterator JavaDoc iter = thisCollection.iterator();
50
51         while (iter.hasNext()) {
52             try {
53                 GenericValue related = ((GenericValue) iter.next()).getRelatedOne(typeRelation);
54
55                 if (isType(related, targetType)) {
56                     return true;
57                 } // else keep looking
58
} catch (GenericEntityException e) {
59                 continue;
60             }
61         }
62         return false;
63     }
64
65     /* public static boolean isType(Collection thisTypeCollection, GenericValue targetType) {
66      Iterator iter = thisTypeCollection.iterator();
67      while (iter.hasNext()) {
68      if (isType((GenericValue) iter.next(), targetType)) {
69      return true;
70      }//else keep looking
71      }
72      return false;
73      }*/

74
75     /* private static Object getTypeID(GenericValue typeValue) {
76      Collection keys = typeValue.getAllKeys();
77      if (keys.size() == 1) {
78      return keys.iterator().next();
79      } else {
80      throw new IllegalArgumentException("getTypeID expecting value with single key");
81      }
82      }*/

83
84     private static GenericValue getParentType(GenericValue typeValue) {
85         // assumes Parent relation is "Parent<entityName>"
86
try {
87             return typeValue.getRelatedOneCache("Parent" + typeValue.getEntityName());
88         } catch (GenericEntityException e) {
89             Debug.logWarning(e, module);
90             return null;
91         }
92     }
93
94     public static List JavaDoc getDescendantTypes(GenericValue typeValue) {
95         // assumes Child relation is "Child<entityName>"
96
List JavaDoc descendantTypes = new ArrayList JavaDoc();
97
98         // first get all childrenTypes ...
99
List JavaDoc childrenTypes = null;
100         try {
101             childrenTypes = typeValue.getRelatedCache("Child" + typeValue.getEntityName());
102         } catch (GenericEntityException e) {
103             Debug.logWarning(e, module);
104             return null;
105         }
106         if (childrenTypes == null)
107             return null;
108
109         // ... and add them as direct descendants
110
descendantTypes.addAll(childrenTypes);
111
112         // then add all descendants of the children
113
Iterator JavaDoc childrenTypeIter = childrenTypes.iterator();
114         while (childrenTypeIter.hasNext()) {
115             GenericValue childType = (GenericValue) childrenTypeIter.next();
116             List JavaDoc childTypeDescendants = getDescendantTypes(childType);
117             if (childTypeDescendants != null) {
118                 descendantTypes.addAll(childTypeDescendants);
119             }
120         }
121
122         return descendantTypes;
123     }
124
125     /**
126      * Description of the Method
127      *
128      *@param catName Description of Parameter
129      *@exception java.rmi.RemoteException Description of Exception
130      */

131     public static boolean isType(GenericValue thisType, GenericValue targetType) {
132         if (thisType == null) {
133             return false;
134         } else if (targetType.equals(thisType)) {
135             return true;
136         } else {
137             return isType(getParentType(thisType), targetType);
138         }
139     }
140 }
141
Popular Tags