1 25 package org.ofbiz.entity.util; 26 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 import org.ofbiz.base.util.Debug; 33 import org.ofbiz.entity.GenericEntityException; 34 import org.ofbiz.entity.GenericValue; 35 36 44 public class EntityTypeUtil { 45 46 public static final String module = EntityTypeUtil.class.getName(); 47 48 public static boolean isType(Collection thisCollection, String typeRelation, GenericValue targetType) { 49 Iterator 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 } } catch (GenericEntityException e) { 59 continue; 60 } 61 } 62 return false; 63 } 64 65 74 75 83 84 private static GenericValue getParentType(GenericValue typeValue) { 85 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 getDescendantTypes(GenericValue typeValue) { 95 List descendantTypes = new ArrayList (); 97 98 List 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 descendantTypes.addAll(childrenTypes); 111 112 Iterator childrenTypeIter = childrenTypes.iterator(); 114 while (childrenTypeIter.hasNext()) { 115 GenericValue childType = (GenericValue) childrenTypeIter.next(); 116 List childTypeDescendants = getDescendantTypes(childType); 117 if (childTypeDescendants != null) { 118 descendantTypes.addAll(childTypeDescendants); 119 } 120 } 121 122 return descendantTypes; 123 } 124 125 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 |