001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.entity.util;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.ofbiz.base.util.Debug;
026: import org.ofbiz.entity.GenericEntityException;
027: import org.ofbiz.entity.GenericValue;
028:
029: /**
030: * Makes it easier to deal with entities that follow the
031: * extensibility pattern and that can be of various types as identified in the database.
032: */
033: public class EntityTypeUtil {
034:
035: public static final String module = EntityTypeUtil.class.getName();
036:
037: public static boolean isType(Collection this Collection,
038: String typeRelation, GenericValue targetType) {
039: Iterator iter = this Collection.iterator();
040:
041: while (iter.hasNext()) {
042: try {
043: GenericValue related = ((GenericValue) iter.next())
044: .getRelatedOne(typeRelation);
045:
046: if (isType(related, targetType)) {
047: return true;
048: } // else keep looking
049: } catch (GenericEntityException e) {
050: continue;
051: }
052: }
053: return false;
054: }
055:
056: /* public static boolean isType(Collection thisTypeCollection, GenericValue targetType) {
057: Iterator iter = thisTypeCollection.iterator();
058: while (iter.hasNext()) {
059: if (isType((GenericValue) iter.next(), targetType)) {
060: return true;
061: }//else keep looking
062: }
063: return false;
064: }*/
065:
066: /* private static Object getTypeID(GenericValue typeValue) {
067: Collection keys = typeValue.getAllKeys();
068: if (keys.size() == 1) {
069: return keys.iterator().next();
070: } else {
071: throw new IllegalArgumentException("getTypeID expecting value with single key");
072: }
073: }*/
074:
075: private static GenericValue getParentType(GenericValue typeValue) {
076: // assumes Parent relation is "Parent<entityName>"
077: try {
078: return typeValue.getRelatedOneCache("Parent"
079: + typeValue.getEntityName());
080: } catch (GenericEntityException e) {
081: Debug.logWarning(e, module);
082: return null;
083: }
084: }
085:
086: public static List getDescendantTypes(GenericValue typeValue) {
087: // assumes Child relation is "Child<entityName>"
088: List descendantTypes = new ArrayList();
089:
090: // first get all childrenTypes ...
091: List childrenTypes = null;
092: try {
093: childrenTypes = typeValue.getRelatedCache("Child"
094: + typeValue.getEntityName());
095: } catch (GenericEntityException e) {
096: Debug.logWarning(e, module);
097: return null;
098: }
099: if (childrenTypes == null)
100: return null;
101:
102: // ... and add them as direct descendants
103: descendantTypes.addAll(childrenTypes);
104:
105: // then add all descendants of the children
106: Iterator childrenTypeIter = childrenTypes.iterator();
107: while (childrenTypeIter.hasNext()) {
108: GenericValue childType = (GenericValue) childrenTypeIter
109: .next();
110: List childTypeDescendants = getDescendantTypes(childType);
111: if (childTypeDescendants != null) {
112: descendantTypes.addAll(childTypeDescendants);
113: }
114: }
115:
116: return descendantTypes;
117: }
118:
119: /**
120: * Description of the Method
121: *
122: *@param catName Description of Parameter
123: *@exception java.rmi.RemoteException Description of Exception
124: */
125: public static boolean isType(GenericValue this Type,
126: GenericValue targetType) {
127: if (this Type == null) {
128: return false;
129: } else if (targetType.equals(this Type)) {
130: return true;
131: } else {
132: return isType(getParentType(thisType), targetType);
133: }
134: }
135: }
|