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.shark.expression;
019:
020: import java.io.Serializable;
021: import java.util.ArrayList;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.apache.commons.collections.map.LinkedMap;
028: import org.enhydra.shark.api.common.ExpressionBuilder;
029: import org.ofbiz.base.util.StringUtil;
030: import org.ofbiz.base.util.UtilMisc;
031: import org.ofbiz.base.util.UtilObject;
032: import org.ofbiz.entity.GenericDelegator;
033: import org.ofbiz.entity.GenericEntityException;
034: import org.ofbiz.entity.util.EntityListIterator;
035: import org.ofbiz.entity.condition.EntityCondition;
036: import org.ofbiz.entity.condition.EntityConditionList;
037: import org.ofbiz.entity.condition.EntityOperator;
038: import org.ofbiz.entity.model.DynamicViewEntity;
039: import org.ofbiz.shark.container.SharkContainer;
040:
041: public abstract class BaseEntityCondExprBldr implements
042: ExpressionBuilder, Serializable {
043:
044: public static final String module = BaseEntityCondExprBldr.class
045: .getName();
046:
047: protected EntityCondition condition = null;
048: protected Map entityNames = new LinkedMap();
049: protected Map fieldNames = new LinkedMap();
050: protected List autoFields = new ArrayList();
051: protected List viewLinks = new ArrayList();
052:
053: protected boolean isOrSet = false;
054: protected boolean isNotSet = false;
055: protected boolean isComplete = true;
056:
057: // ExpressionBuilder Methods
058:
059: public boolean isComplete() {
060: return this .isComplete;
061: }
062:
063: public String toSQL() {
064: return BaseEntityCondExprBldr.getHexString(this );
065: }
066:
067: public String toScript() {
068: return "";
069: }
070:
071: public String toExpression() {
072: StringBuffer ret = new StringBuffer();
073: if (!this .isComplete()) {
074: ret.append("/*FORCE*/\n");
075: }
076:
077: ret.append(this .toScript());
078: ret.append("\n/*sql ").append(this .toSQL()).append(" sql*/");
079:
080: return ret.toString();
081: }
082:
083: // Special Purpose Methods
084:
085: public EntityCondition getCondition() {
086: return this .condition;
087: }
088:
089: public List runQuery() throws GenericEntityException {
090: GenericDelegator delegator = this .getDelegator();
091: DynamicViewEntity view = this .makeView();
092: EntityListIterator eli = null;
093: List result = null;
094: try {
095: eli = delegator.findListIteratorByCondition(view, this
096: .getCondition(), null, null, null, null);
097: result = eli.getCompleteList();
098: } catch (GenericEntityException e) {
099: throw e;
100: } finally {
101: eli.close();
102: }
103: return result;
104: }
105:
106: protected void setNot(boolean not) {
107: this .isNotSet = not;
108: }
109:
110: protected void setOr(boolean or) {
111: this .isOrSet = or;
112: }
113:
114: protected void addCondition(EntityCondition current) {
115: if (condition == null) {
116: condition = current;
117: } else {
118: List condList = UtilMisc.toList(condition, current);
119: if (this .isOrSet) {
120: condition = new EntityConditionList(condList,
121: EntityOperator.OR);
122: } else {
123: condition = new EntityConditionList(condList,
124: EntityOperator.AND);
125: }
126: }
127: // reset the NOT value
128: this .setNot(false);
129: }
130:
131: protected synchronized void addEntity(String alias, String entity) {
132: entityNames.put(alias, entity);
133: }
134:
135: protected synchronized void addField(String entityAlias,
136: String fieldName, String fieldAlias) {
137: Map fieldAliasMap = (Map) fieldNames.get(entityAlias);
138: if (fieldAliasMap == null) {
139: fieldAliasMap = new HashMap();
140: fieldNames.put(entityAlias, fieldAliasMap);
141: }
142: fieldAliasMap.put(fieldName, fieldAlias);
143: }
144:
145: protected synchronized void addAllFields(String entityAlias) {
146: autoFields.add(entityAlias);
147: }
148:
149: protected synchronized void addLink(String entityAlias,
150: String relEntityAlias, boolean opt, List keyMap) {
151: this .viewLinks.add(new ViewLink(entityAlias, relEntityAlias,
152: opt, keyMap));
153: }
154:
155: protected GenericDelegator getDelegator() {
156: return SharkContainer.getDelegator();
157: }
158:
159: private DynamicViewEntity makeView() {
160: DynamicViewEntity view = new DynamicViewEntity();
161:
162: // create the members
163: Iterator eni = this .entityNames.entrySet().iterator();
164: while (eni.hasNext()) {
165: Map.Entry entry = (Map.Entry) eni.next();
166: view.addMemberEntity((String) entry.getKey(),
167: (String) entry.getValue());
168: }
169:
170: // set alias all fields
171: Iterator aai = autoFields.iterator();
172: while (aai.hasNext()) {
173: String alias = (String) aai.next();
174: view.addAliasAll(alias, "");
175: }
176:
177: // create the other field aliases
178: Iterator fni = fieldNames.keySet().iterator();
179: while (fni.hasNext()) {
180: String entityAlias = (String) fni.next();
181: Map fieldMap = (Map) fieldNames.get(entityAlias);
182: Iterator fmi = fieldMap.entrySet().iterator();
183: while (fmi.hasNext()) {
184: Map.Entry entry = (Map.Entry) fmi.next();
185: view
186: .addAlias(entityAlias, (String) entry
187: .getValue(), (String) entry.getKey(),
188: null, null, null, null);
189: }
190: }
191:
192: // add the view links
193: Iterator vli = this .viewLinks.iterator();
194: while (vli.hasNext()) {
195: ViewLink vl = (ViewLink) vli.next();
196: view.addViewLink(vl.entityAlias, vl.relEntityAlias,
197: new Boolean(vl.relOptional), vl.keyMaps);
198: }
199:
200: return view;
201: }
202:
203: // Standard Static Serialization Methods
204:
205: public static String getHexString(BaseEntityCondExprBldr builder) {
206: byte[] builderBytes = UtilObject.getBytes(builder);
207: return StringUtil.toHexString(builderBytes);
208: }
209:
210: public static BaseEntityCondExprBldr getBuilder(String hexString) {
211: byte[] builderBytes = StringUtil.fromHexString(hexString);
212: return (BaseEntityCondExprBldr) UtilObject
213: .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,
224: boolean optional, List keyMaps) {
225: this.entityAlias = entityAlias;
226: this.relEntityAlias = relEntityAlias;
227: this.relOptional = optional;
228: this.keyMaps = keyMaps;
229: }
230: }
231: }
|