1 25 package org.ofbiz.shark.expression; 26 27 import java.io.Serializable ; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 import org.apache.commons.collections.map.LinkedMap; 35 import org.enhydra.shark.api.common.ExpressionBuilder; 36 import org.ofbiz.base.util.StringUtil; 37 import org.ofbiz.base.util.UtilMisc; 38 import org.ofbiz.base.util.UtilObject; 39 import org.ofbiz.entity.GenericDelegator; 40 import org.ofbiz.entity.GenericEntityException; 41 import org.ofbiz.entity.util.EntityListIterator; 42 import org.ofbiz.entity.condition.EntityCondition; 43 import org.ofbiz.entity.condition.EntityConditionList; 44 import org.ofbiz.entity.condition.EntityOperator; 45 import org.ofbiz.entity.model.DynamicViewEntity; 46 import org.ofbiz.shark.container.SharkContainer; 47 48 54 public abstract class BaseEntityCondExprBldr implements ExpressionBuilder, Serializable { 55 56 public static final String module = BaseEntityCondExprBldr.class.getName(); 57 58 protected EntityCondition condition = null; 59 protected Map entityNames = new LinkedMap(); 60 protected Map fieldNames = new LinkedMap(); 61 protected List autoFields = new ArrayList (); 62 protected List viewLinks = new ArrayList (); 63 64 protected boolean isOrSet = false; 65 protected boolean isNotSet = false; 66 protected boolean isComplete = true; 67 68 70 public boolean isComplete() { 71 return this.isComplete; 72 } 73 74 public String toSQL() { 75 return BaseEntityCondExprBldr.getHexString(this); 76 } 77 78 public String toScript() { 79 return ""; 80 } 81 82 public String toExpression() { 83 StringBuffer ret = new StringBuffer (); 84 if (!this.isComplete()) { 85 ret.append("/*FORCE*/\n"); 86 } 87 88 ret.append(this.toScript()); 89 ret.append("\n/*sql ").append(this.toSQL()).append(" sql*/"); 90 91 return ret.toString(); 92 } 93 94 96 public EntityCondition getCondition() { 97 return this.condition; 98 } 99 100 public List runQuery() throws GenericEntityException { 101 GenericDelegator delegator = this.getDelegator(); 102 DynamicViewEntity view = this.makeView(); 103 EntityListIterator eli = null; 104 List result = null; 105 try { 106 eli = delegator.findListIteratorByCondition(view, this.getCondition(), null, null, null, null); 107 result = eli.getCompleteList(); 108 } catch (GenericEntityException e) { 109 throw e; 110 } finally { 111 eli.close(); 112 } 113 return result; 114 } 115 116 protected void setNot(boolean not) { 117 this.isNotSet = not; 118 } 119 120 protected void setOr(boolean or) { 121 this.isOrSet = or; 122 } 123 124 protected void addCondition(EntityCondition current) { 125 if (condition == null) { 126 condition = current; 127 } else { 128 List condList = UtilMisc.toList(condition, current); 129 if (this.isOrSet) { 130 condition = new EntityConditionList(condList, EntityOperator.OR); 131 } else { 132 condition = new EntityConditionList(condList, EntityOperator.AND); 133 } 134 } 135 this.setNot(false); 137 } 138 139 protected synchronized void addEntity(String alias, String entity) { 140 entityNames.put(alias, entity); 141 } 142 143 protected synchronized void addField(String entityAlias, String fieldName, String fieldAlias) { 144 Map fieldAliasMap = (Map ) fieldNames.get(entityAlias); 145 if (fieldAliasMap == null) { 146 fieldAliasMap = new HashMap (); 147 fieldNames.put(entityAlias, fieldAliasMap); 148 } 149 fieldAliasMap.put(fieldName, fieldAlias); 150 } 151 152 protected synchronized void addAllFields(String entityAlias) { 153 autoFields.add(entityAlias); 154 } 155 156 protected synchronized void addLink(String entityAlias, String relEntityAlias, boolean opt, List keyMap) { 157 this.viewLinks.add(new ViewLink(entityAlias, relEntityAlias, opt, keyMap)); 158 } 159 160 protected GenericDelegator getDelegator() { 161 return SharkContainer.getDelegator(); 162 } 163 164 private DynamicViewEntity makeView() { 165 DynamicViewEntity view = new DynamicViewEntity(); 166 167 168 Iterator eni = this.entityNames.entrySet().iterator(); 170 while (eni.hasNext()) { 171 Map.Entry entry = (Map.Entry ) eni.next(); 172 view.addMemberEntity((String ) entry.getKey(), (String ) entry.getValue()); 173 } 174 175 Iterator aai = autoFields.iterator(); 177 while (aai.hasNext()) { 178 String alias = (String ) aai.next(); 179 view.addAliasAll(alias, ""); 180 } 181 182 Iterator fni = fieldNames.keySet().iterator(); 184 while (fni.hasNext()) { 185 String entityAlias = (String ) fni.next(); 186 Map fieldMap = (Map ) fieldNames.get(entityAlias); 187 Iterator fmi = fieldMap.entrySet().iterator(); 188 while (fmi.hasNext()) { 189 Map.Entry entry = (Map.Entry ) fmi.next(); 190 view.addAlias(entityAlias, (String ) entry.getValue(), (String ) entry.getKey(), null, null, null, null); 191 } 192 } 193 194 Iterator vli = this.viewLinks.iterator(); 196 while (vli.hasNext()) { 197 ViewLink vl = (ViewLink) vli.next(); 198 view.addViewLink(vl.entityAlias, vl.relEntityAlias, new Boolean (vl.relOptional), vl.keyMaps); 199 } 200 201 return view; 202 } 203 204 206 public static String getHexString(BaseEntityCondExprBldr builder) { 207 byte[] builderBytes = UtilObject.getBytes(builder); 208 return StringUtil.toHexString(builderBytes); 209 } 210 211 public static BaseEntityCondExprBldr getBuilder(String hexString) { 212 byte[] builderBytes = StringUtil.fromHexString(hexString); 213 return (BaseEntityCondExprBldr) UtilObject.getObject(builderBytes); 214 } 215 216 class ViewLink implements Serializable { 217 218 public String entityAlias; 219 public String relEntityAlias; 220 public boolean relOptional = false; 221 public List keyMaps = new ArrayList (); 222 223 public ViewLink(String entityAlias, String relEntityAlias, boolean optional, List keyMaps) { 224 this.entityAlias = entityAlias; 225 this.relEntityAlias = relEntityAlias; 226 this.relOptional = optional; 227 this.keyMaps = keyMaps; 228 } 229 } 230 } 231 | Popular Tags |