001: /*
002: * $Id: EntityTypeUtil.java,v 1.1 2003/08/17 04:56:26 jonesde Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.entity.util;
026:
027: import java.util.ArrayList;
028: import java.util.Collection;
029: import java.util.Iterator;
030: import java.util.List;
031:
032: import org.ofbiz.base.util.Debug;
033: import org.ofbiz.entity.GenericEntityException;
034: import org.ofbiz.entity.GenericValue;
035:
036: /**
037: * Makes it easier to deal with entities that follow the
038: * extensibility pattern and that can be of various types as identified in the database.
039: *
040: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
041: * @version $Revision: 1.1 $
042: * @since 2.0
043: */
044: public class EntityTypeUtil {
045:
046: public static final String module = EntityTypeUtil.class.getName();
047:
048: public static boolean isType(Collection this Collection,
049: String typeRelation, GenericValue targetType) {
050: Iterator iter = this Collection.iterator();
051:
052: while (iter.hasNext()) {
053: try {
054: GenericValue related = ((GenericValue) iter.next())
055: .getRelatedOne(typeRelation);
056:
057: if (isType(related, targetType)) {
058: return true;
059: } // else keep looking
060: } catch (GenericEntityException e) {
061: continue;
062: }
063: }
064: return false;
065: }
066:
067: /* public static boolean isType(Collection thisTypeCollection, GenericValue targetType) {
068: Iterator iter = thisTypeCollection.iterator();
069: while (iter.hasNext()) {
070: if (isType((GenericValue) iter.next(), targetType)) {
071: return true;
072: }//else keep looking
073: }
074: return false;
075: }*/
076:
077: /* private static Object getTypeID(GenericValue typeValue) {
078: Collection keys = typeValue.getAllKeys();
079: if (keys.size() == 1) {
080: return keys.iterator().next();
081: } else {
082: throw new IllegalArgumentException("getTypeID expecting value with single key");
083: }
084: }*/
085:
086: private static GenericValue getParentType(GenericValue typeValue) {
087: // assumes Parent relation is "Parent<entityName>"
088: try {
089: return typeValue.getRelatedOneCache("Parent"
090: + typeValue.getEntityName());
091: } catch (GenericEntityException e) {
092: Debug.logWarning(e, module);
093: return null;
094: }
095: }
096:
097: public static List getDescendantTypes(GenericValue typeValue) {
098: // assumes Child relation is "Child<entityName>"
099: List descendantTypes = new ArrayList();
100:
101: // first get all childrenTypes ...
102: List childrenTypes = null;
103: try {
104: childrenTypes = typeValue.getRelatedCache("Child"
105: + typeValue.getEntityName());
106: } catch (GenericEntityException e) {
107: Debug.logWarning(e, module);
108: return null;
109: }
110: if (childrenTypes == null)
111: return null;
112:
113: // ... and add them as direct descendants
114: descendantTypes.addAll(childrenTypes);
115:
116: // then add all descendants of the children
117: Iterator childrenTypeIter = childrenTypes.iterator();
118: while (childrenTypeIter.hasNext()) {
119: GenericValue childType = (GenericValue) childrenTypeIter
120: .next();
121: List childTypeDescendants = getDescendantTypes(childType);
122: if (childTypeDescendants != null) {
123: descendantTypes.addAll(childTypeDescendants);
124: }
125: }
126:
127: return descendantTypes;
128: }
129:
130: /**
131: * Description of the Method
132: *
133: *@param catName Description of Parameter
134: *@exception java.rmi.RemoteException Description of Exception
135: */
136: public static boolean isType(GenericValue this Type,
137: GenericValue targetType) {
138: if (this Type == null) {
139: return false;
140: } else if (targetType.equals(this Type)) {
141: return true;
142: } else {
143: return isType(getParentType(thisType), targetType);
144: }
145: }
146: }
|