KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > common > FindServices


1 /*
2  * $Id: FindServices.java 6697 2006-02-07 06:17:14Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.common;
26
27 import java.sql.Timestamp JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import org.ofbiz.base.util.Debug;
35 import org.ofbiz.base.util.ObjectType;
36 import org.ofbiz.base.util.StringUtil;
37 import org.ofbiz.base.util.UtilDateTime;
38 import org.ofbiz.base.util.UtilHttp;
39 import org.ofbiz.base.util.UtilValidate;
40 import org.ofbiz.base.util.UtilMisc;
41 import org.ofbiz.entity.GenericDelegator;
42 import org.ofbiz.entity.GenericEntityException;
43 import org.ofbiz.entity.GenericValue;
44 import org.ofbiz.entity.condition.EntityComparisonOperator;
45 import org.ofbiz.entity.condition.EntityConditionList;
46 import org.ofbiz.entity.condition.EntityCondition;
47 import org.ofbiz.entity.condition.EntityExpr;
48 import org.ofbiz.entity.condition.EntityJoinOperator;
49 import org.ofbiz.entity.condition.EntityOperator;
50 import org.ofbiz.entity.condition.EntityFunction;
51 import org.ofbiz.entity.condition.EntityFieldValue;
52 import org.ofbiz.entity.model.ModelEntity;
53 import org.ofbiz.entity.util.EntityFindOptions;
54 import org.ofbiz.entity.util.EntityListIterator;
55 import org.ofbiz.entity.util.EntityUtil;
56 import org.ofbiz.service.DispatchContext;
57 import org.ofbiz.service.ServiceUtil;
58 import org.ofbiz.service.GenericServiceException;
59 import org.ofbiz.service.LocalDispatcher;
60
61 /**
62  * FindServices Class
63  *
64  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
65  * @version $Rev: 6697 $
66  * @since 2.2
67  */

68 public class FindServices {
69
70     public static final String JavaDoc module = FindServices.class.getName();
71
72     public static HashMap JavaDoc entityOperators;
73
74     static {
75         entityOperators = new HashMap JavaDoc();
76         entityOperators.put("and", EntityOperator.AND);
77         entityOperators.put("between", EntityOperator.BETWEEN);
78         entityOperators.put("equals", EntityOperator.EQUALS);
79         entityOperators.put("greaterThan", EntityOperator.GREATER_THAN);
80         entityOperators.put("greaterThanEqualTo", EntityOperator.GREATER_THAN_EQUAL_TO);
81         entityOperators.put("in", EntityOperator.IN);
82         entityOperators.put("lessThan", EntityOperator.LESS_THAN);
83         entityOperators.put("lessThanEqualTo", EntityOperator.LESS_THAN_EQUAL_TO);
84         entityOperators.put("like", EntityOperator.LIKE);
85         entityOperators.put("not", EntityOperator.NOT);
86         entityOperators.put("notEqual", EntityOperator.NOT_EQUAL);
87         entityOperators.put("or", EntityOperator.OR);
88     }
89
90     public FindServices() {}
91
92     /**
93      * prepareField, analyse inputFields to created normalizedFields a map with field name and operator.
94      *
95      * This is use to the generic method that expects entity data affixed with special suffixes
96      * to indicate their purpose in formulating an SQL query statement.
97      * @param inputFields Input parameters run thru UtilHttp.getParameterMap
98      * @return a map with field name and operator
99      */

100     public static HashMap JavaDoc prepareField(Map JavaDoc inputFields, Map JavaDoc queryStringMap, Map JavaDoc origValueMap) {
101         // Strip the "_suffix" off of the parameter name and
102
// build a three-level map of values keyed by fieldRoot name,
103
// fld0 or fld1, and, then, "op" or "value"
104
// ie. id
105
// - fld0
106
// - op:like
107
// - value:abc
108
// - fld1 (if there is a range)
109
// - op:lessThan
110
// - value:55 (note: these two "flds" wouldn't really go together)
111
// Also note that op/fld can be in any order. (eg. id_fld1_equals or id_equals_fld1)
112
// Note that "normalizedFields" will contain values other than those
113
// Contained in the associated entity.
114
// Those extra fields will be ignored in the second half of this method.
115
HashMap JavaDoc normalizedFields = new HashMap JavaDoc();
116         Iterator JavaDoc ifIter = inputFields.keySet().iterator();
117         //StringBuffer queryStringBuf = new StringBuffer();
118
while (ifIter.hasNext()) {
119             String JavaDoc fieldNameRaw = null; // The name as it appeas in the HTML form
120
String JavaDoc fieldNameRoot = null; // The entity field name. Everything to the left of the first "_" if
121
// it exists, or the whole word, if not.
122
String JavaDoc fieldPair = null; // "fld0" or "fld1" - begin/end of range or just fld0 if no range.
123
Object JavaDoc fieldValue = null; // If it is a "value" field, it will be the value to be used in the query.
124
// If it is an "op" field, it will be "equals", "greaterThan", etc.
125
int iPos = -1;
126             int iPos2 = -1;
127             HashMap JavaDoc subMap = null;
128             HashMap JavaDoc subMap2 = null;
129             String JavaDoc fieldMode = null;
130
131             fieldNameRaw = (String JavaDoc) ifIter.next();
132             fieldValue = inputFields.get(fieldNameRaw);
133             if (ObjectType.isEmpty(fieldValue)) {
134                 continue;
135             }
136
137             //queryStringBuffer.append(fieldNameRaw + "=" + fieldValue);
138
queryStringMap.put(fieldNameRaw, fieldValue);
139             iPos = fieldNameRaw.indexOf("_"); // Look for suffix
140

141             // This is a hack to skip fields from "multi" forms
142
// These would have the form "fieldName_o_1"
143
if (iPos >= 0) {
144                 String JavaDoc suffix = fieldNameRaw.substring(iPos + 1);
145                 iPos2 = suffix.indexOf("_");
146                 if (iPos2 == 1) {
147                     continue;
148                 }
149             }
150
151             // If no suffix, assume no range (default to fld0) and operations of equals
152
// If no field op is present, it will assume "equals".
153
if (iPos < 0) {
154                 fieldNameRoot = fieldNameRaw;
155                 fieldPair = "fld0";
156                 fieldMode = "value";
157             } else { // Must have at least "fld0/1" or "equals, greaterThan, etc."
158
// Some bogus fields will slip in, like "ENTITY_NAME", but they will be ignored
159

160                 fieldNameRoot = fieldNameRaw.substring(0, iPos);
161                 String JavaDoc suffix = fieldNameRaw.substring(iPos + 1);
162                 iPos2 = suffix.indexOf("_");
163                 if (iPos2 < 0) {
164                     if (suffix.startsWith("fld")) {
165                         // If only one token and it starts with "fld"
166
// assume it is a value field, not an op
167
fieldPair = suffix;
168                         fieldMode = "value";
169                     } else {
170                         // if it does not start with fld, assume it is an op or the 'ignore case' (ic) field
171
fieldPair = "fld0";
172                         fieldMode = suffix;
173                     }
174                 } else {
175                     String JavaDoc tkn0 = suffix.substring(0, iPos2);
176                     String JavaDoc tkn1 = suffix.substring(iPos2 + 1);
177                     // If suffix has two parts, let them be in any order
178
// One will be "fld0/1" and the other will be the op (eg. equals, greaterThan_
179
if (tkn0.startsWith("fld")) {
180                         fieldPair = tkn0;
181                         fieldMode = tkn1;
182                     } else {
183                         fieldPair = tkn1;
184                         fieldMode = tkn0;
185                     }
186                 }
187             }
188             subMap = (HashMap JavaDoc) normalizedFields.get(fieldNameRoot);
189             if (subMap == null) {
190                 subMap = new HashMap JavaDoc();
191                 normalizedFields.put(fieldNameRoot, subMap);
192             }
193             subMap2 = (HashMap JavaDoc) subMap.get(fieldPair);
194             if (subMap2 == null) {
195                 subMap2 = new HashMap JavaDoc();
196                 subMap.put(fieldPair, subMap2);
197             }
198             subMap2.put(fieldMode, fieldValue);
199
200             List JavaDoc origList = (List JavaDoc) origValueMap.get(fieldNameRoot);
201             if (origList == null) {
202                 origList = new ArrayList JavaDoc();
203                 origValueMap.put(fieldNameRoot, origList);
204             }
205             Object JavaDoc [] origValues = {fieldNameRaw, fieldValue};
206             origList.add(origValues);
207         }
208         return normalizedFields;
209     }
210     
211     /**
212      * createCondition, comparing the normalizedFields with the list of keys, .
213      *
214      * This is use to the generic method that expects entity data affixed with special suffixes
215      * to indicate their purpose in formulating an SQL query statement.
216      * @param keys list of field for which it's possible to make the query
217      * @param normalizedFields list of field the user have populated
218      * @return a arrayList usable to create an entityCondition
219      */

220     public static ArrayList JavaDoc createCondition(List JavaDoc keys, HashMap JavaDoc normalizedFields, Map JavaDoc queryStringMap, Map JavaDoc origValueMap) {
221         String JavaDoc fieldName = null;
222         HashMap JavaDoc subMap = null;
223         HashMap JavaDoc subMap2 = null;
224         EntityOperator fieldOp = null;
225         String JavaDoc fieldValue = null; // If it is a "value" field, it will be the value to be used in the query.
226
// If it is an "op" field, it will be "equals", "greaterThan", etc.
227
Iterator JavaDoc iter = keys.iterator();
228         EntityExpr cond = null;
229         ArrayList JavaDoc tmpList = new ArrayList JavaDoc();
230         String JavaDoc opString = null;
231         String JavaDoc ignoreCase = null;
232         int count = 0;
233         while (iter.hasNext()) {
234             fieldName = (String JavaDoc) iter.next();
235             subMap = (HashMap JavaDoc) normalizedFields.get(fieldName);
236             if (subMap == null) {
237                 continue;
238             }
239
240             subMap2 = (HashMap JavaDoc) subMap.get("fld0");
241             opString = (String JavaDoc) subMap2.get("op");
242             ignoreCase = (String JavaDoc) subMap2.get("ic");
243
244             if (opString != null) {
245                 if (opString.equals("contains")) {
246                     fieldOp = EntityOperator.LIKE;
247                 } else if (opString.equals("empty")) {
248                     fieldOp = EntityOperator.EQUALS;
249                 } else {
250                     fieldOp = (EntityOperator) entityOperators.get(opString);
251                 }
252             } else {
253                 fieldOp = EntityOperator.EQUALS;
254             }
255
256             fieldValue = (String JavaDoc) subMap2.get("value");
257             if (fieldValue == null) {
258                 continue;
259             }
260
261             if (opString != null) {
262                 if (opString.equals("contains")) {
263                     fieldOp = EntityOperator.LIKE;
264                     fieldValue = "%" + fieldValue + "%";
265                 } else if (opString.equals("empty")) {
266                     fieldOp = EntityOperator.EQUALS;
267                     fieldValue = null;
268                     ignoreCase = null;
269                 } else if (opString.equals("like")) {
270                     fieldOp = EntityOperator.LIKE;
271                     fieldValue += "%";
272                 } else if (opString.equals("greaterThanFromDayStart")) {
273                     fieldValue = dayStart(fieldValue, 0);
274                     fieldOp = EntityOperator.GREATER_THAN;
275                     ignoreCase = null;
276                 } else if (opString.equals("sameDay")) {
277                     String JavaDoc timeStampString = fieldValue;
278                     fieldValue = dayStart(timeStampString, 0);
279                     fieldOp = EntityOperator.GREATER_THAN_EQUAL_TO;
280                     ignoreCase = null;
281                     // Set up so next part finds ending conditions for same day
282
subMap2 = (HashMap JavaDoc) subMap.get("fld1");
283                     if (subMap2 == null) {
284                         subMap2 = new HashMap JavaDoc();
285                         subMap.put("fld1", subMap2);
286                     }
287                     String JavaDoc endOfDay = dayStart(timeStampString, 1);
288                     subMap2.put("value", endOfDay);
289                     subMap2.put("op", "lessThan");
290                 } else {
291                     fieldOp = (EntityOperator) entityOperators.get(opString);
292                 }
293             } else {
294                 fieldOp = EntityOperator.EQUALS;
295             }
296
297             if (ignoreCase != null && ignoreCase.equals("Y")) {
298                 cond = new EntityExpr(new EntityFunction.UPPER(new EntityFieldValue(fieldName)), (EntityComparisonOperator) fieldOp, new EntityFunction.UPPER(fieldValue.toUpperCase()));
299             } else {
300                 cond = new EntityExpr(fieldName, (EntityComparisonOperator) fieldOp, fieldValue);
301             }
302             tmpList.add(cond);
303             count++;
304
305             // Repeat above operations if there is a "range" - second value
306
subMap2 = (HashMap JavaDoc) subMap.get("fld1");
307             if (subMap2 == null) {
308                 continue;
309             }
310             opString = (String JavaDoc) subMap2.get("op");
311
312             if (opString != null) {
313                 if (opString.equals("contains")) {
314                     fieldOp = EntityOperator.LIKE;
315                 } else if (opString.equals("empty")) {
316                     fieldOp = EntityOperator.EQUALS;
317                 } else {
318                     fieldOp = (EntityOperator) entityOperators.get(opString);
319                 }
320             } else {
321                 fieldOp = EntityOperator.EQUALS;
322             }
323
324             fieldValue = (String JavaDoc) subMap2.get("value");
325             if (fieldValue == null) {
326                 continue;
327             }
328             if (opString.equals("like")) {
329                 fieldValue += "%";
330             } else if (opString.equals("contains")) {
331                 fieldValue += "%" + fieldValue + "%";
332             } else if (opString.equals("empty")) {
333                 fieldOp = EntityOperator.EQUALS;
334                 fieldValue = null;
335             } else if (opString.equals("upToDay")) {
336                 fieldValue = dayStart(fieldValue, 0);
337                 fieldOp = EntityOperator.LESS_THAN;
338             } else if (opString.equals("upThruDay")) {
339                 fieldValue = dayStart(fieldValue, 1);
340                 fieldOp = EntityOperator.LESS_THAN;
341             }
342             // String rhs = fieldValue.toString();
343
cond = new EntityExpr(fieldName, (EntityComparisonOperator) fieldOp, fieldValue);
344             tmpList.add(cond);
345
346             // add to queryStringMap
347
List JavaDoc origList = (List JavaDoc)origValueMap.get(fieldName);
348             if (origList != null && origList.size() > 0) {
349                 Iterator JavaDoc origIter = origList.iterator();
350                 while (origIter.hasNext()) {
351                     Object JavaDoc [] arr = (Object JavaDoc [])origIter.next();
352                     queryStringMap.put(arr[0], arr[1]);
353                 }
354             }
355         }
356         return tmpList;
357     }
358     
359     /**
360      * performFind
361      *
362      * This is a generic method that expects entity data affixed with special suffixes
363      * to indicate their purpose in formulating an SQL query statement.
364      */

365     public static Map JavaDoc performFind(DispatchContext dctx, Map JavaDoc context) {
366         String JavaDoc entityName = (String JavaDoc) context.get("entityName");
367         String JavaDoc orderBy = (String JavaDoc) context.get("orderBy");
368         Map JavaDoc inputFields = (Map JavaDoc) context.get("inputFields"); // Input
369
String JavaDoc noConditionFind = (String JavaDoc) context.get("noConditionFind");
370         if (UtilValidate.isEmpty(noConditionFind)) {
371             // try finding in inputFields Map
372
noConditionFind = (String JavaDoc) inputFields.get("noConditionFind");
373         }
374         String JavaDoc filterByDate = (String JavaDoc) context.get("filterByDate");
375         if (UtilValidate.isEmpty(filterByDate)) {
376             // try finding in inputFields Map
377
filterByDate = (String JavaDoc) inputFields.get("filterByDate");
378         }
379
380         LocalDispatcher dispatcher = dctx.getDispatcher();
381
382         Map JavaDoc prepareResult = null;
383         try {
384             prepareResult = dispatcher.runSync("prepareFind", UtilMisc.toMap("entityName", entityName, "orderBy", orderBy, "inputFields", inputFields, "filterByDate", filterByDate));
385         } catch (GenericServiceException gse) {
386             return ServiceUtil.returnError("Error preparing conditions: " + gse.getMessage());
387         }
388         EntityConditionList exprList = (EntityConditionList)prepareResult.get("entityConditionList");
389         List JavaDoc orderByList = (List JavaDoc)prepareResult.get("orderByList");
390         
391         Map JavaDoc executeResult = null;
392         try {
393             executeResult = dispatcher.runSync("executeFind", UtilMisc.toMap("entityName", entityName, "orderByList", orderByList, "entityConditionList", exprList, "noConditionFind", noConditionFind));
394         } catch (GenericServiceException gse) {
395             return ServiceUtil.returnError("Error finding iterator: " + gse.getMessage());
396         }
397
398         if (executeResult.get("listIt") == null) {
399             Debug.logInfo("No list iterator found for query string + [" + prepareResult.get("queryString") + "]", module);
400         }
401         
402         Map JavaDoc results = ServiceUtil.returnSuccess();
403         results.put("listIt", executeResult.get("listIt"));
404         results.put("queryString", prepareResult.get("queryString"));
405         results.put("queryStringMap", prepareResult.get("queryStringMap"));
406         return results;
407     }
408     
409     /**
410      * prepareFind
411      *
412      * This is a generic method that expects entity data affixed with special suffixes
413      * to indicate their purpose in formulating an SQL query statement.
414      */

415     public static Map JavaDoc prepareFind(DispatchContext dctx, Map JavaDoc context) {
416         String JavaDoc entityName = (String JavaDoc) context.get("entityName");
417         String JavaDoc orderBy = (String JavaDoc) context.get("orderBy");
418         Map JavaDoc inputFields = (Map JavaDoc) context.get("inputFields"); // Input
419
String JavaDoc noConditionFind = (String JavaDoc) context.get("noConditionFind");
420         if (UtilValidate.isEmpty(noConditionFind)) {
421             // try finding in inputFields Map
422
noConditionFind = (String JavaDoc) inputFields.get("noConditionFind");
423         }
424         String JavaDoc filterByDate = (String JavaDoc) context.get("filterByDate");
425         if (UtilValidate.isEmpty(filterByDate)) {
426             // try finding in inputFields Map
427
filterByDate = (String JavaDoc) inputFields.get("filterByDate");
428         }
429
430         // parameters run thru UtilHttp.getParameterMap
431
Map JavaDoc queryStringMap = new HashMap JavaDoc();
432         Map JavaDoc origValueMap = new HashMap JavaDoc();
433         HashMap JavaDoc normalizedFields = prepareField(inputFields, queryStringMap, origValueMap);
434
435         // Now use only the values that correspond to entity fields to build
436
// an EntityConditionList
437
GenericDelegator delegator = dctx.getDelegator();
438
439         GenericValue entityValue = delegator.makeValue(entityName, new HashMap JavaDoc());
440
441         ModelEntity modelEntity = entityValue.getModelEntity();
442         List JavaDoc keys = modelEntity.getAllFieldNames();
443         ArrayList JavaDoc tmpList = createCondition(keys, normalizedFields, queryStringMap, origValueMap);
444
445         /* the filter by date condition should only be added when there are other conditions or when
446          * the user has specified a noConditionFind. Otherwise, specifying filterByDate will become
447          * its own condition.
448          */

449         if ((tmpList.size() > 0) || ((noConditionFind != null) && (noConditionFind.equals("Y")))) {
450             if (!UtilValidate.isEmpty(filterByDate) && "Y".equals(filterByDate)) {
451                 EntityCondition filterByDateCondition = EntityUtil.getFilterByDateExpr();
452                 tmpList.add(filterByDateCondition);
453             }
454         }
455
456         EntityConditionList exprList = null;
457         if (tmpList.size() > 0) {
458             exprList = new EntityConditionList(tmpList, (EntityJoinOperator) EntityOperator.AND);
459         }
460         
461         List JavaDoc orderByList = null;
462         if (UtilValidate.isNotEmpty(orderBy)) {
463             orderByList = StringUtil.split(orderBy,"|");
464         }
465
466         Map JavaDoc results = ServiceUtil.returnSuccess();
467         Map JavaDoc reducedQueryStringMap = buildReducedQueryString(inputFields, entityName, delegator);
468         reducedQueryStringMap.put("noConditionFind", noConditionFind);
469         reducedQueryStringMap.put("filterByDate", filterByDate);
470         String JavaDoc queryString = UtilHttp.urlEncodeArgs(reducedQueryStringMap);
471         results.put("queryString", queryString);
472         results.put("queryStringMap", reducedQueryStringMap);
473         
474         results.put("orderByList", orderByList);
475         results.put("entityConditionList", exprList);
476         return results;
477     }
478
479     /**
480      * executeFind
481      *
482      * This is a generic method that returns an EntityListIterator.
483      */

484     public static Map JavaDoc executeFind(DispatchContext dctx, Map JavaDoc context) {
485         String JavaDoc entityName = (String JavaDoc) context.get("entityName");
486         EntityConditionList entityConditionList = (EntityConditionList) context.get("entityConditionList");
487         List JavaDoc orderByList = (List JavaDoc) context.get("orderByList");
488         boolean noConditionFind = "Y".equals((String JavaDoc) context.get("noConditionFind"));
489         
490         GenericDelegator delegator = dctx.getDelegator();
491         
492         // Retrieve entities - an iterator over all the values
493
EntityListIterator listIt = null;
494         try {
495             if (noConditionFind || (entityConditionList != null && entityConditionList.getConditionListSize() > 0)) {
496                 listIt = delegator.findListIteratorByCondition(entityName, entityConditionList,
497                         null, null, orderByList, new EntityFindOptions(true, EntityFindOptions.TYPE_SCROLL_INSENSITIVE, EntityFindOptions.CONCUR_READ_ONLY, false));
498             }
499         } catch (GenericEntityException e) {
500             return ServiceUtil.returnError("Error running Find on the [" + entityName + "] entity: " + e.getMessage());
501         }
502
503         Map JavaDoc results = ServiceUtil.returnSuccess();
504         results.put("listIt", listIt);
505         return results;
506     }
507     
508     private static String JavaDoc dayStart(String JavaDoc timeStampString, int daysLater) {
509         String JavaDoc retValue = null;
510         Timestamp JavaDoc ts = null;
511         Timestamp JavaDoc startTs = null;
512         try {
513             ts = Timestamp.valueOf(timeStampString);
514         } catch (IllegalArgumentException JavaDoc e) {
515             timeStampString += " 00:00:00.000";
516             try {
517                 ts = Timestamp.valueOf(timeStampString);
518             } catch (IllegalArgumentException JavaDoc e2) {
519                 return retValue;
520             }
521         }
522         startTs = UtilDateTime.getDayStart(ts, daysLater);
523         retValue = startTs.toString();
524         return retValue;
525     }
526
527     public static HashMap JavaDoc buildReducedQueryString(Map JavaDoc inputFields, String JavaDoc entityName, GenericDelegator delegator) {
528         // Strip the "_suffix" off of the parameter name and
529
// build a three-level map of values keyed by fieldRoot name,
530
// fld0 or fld1, and, then, "op" or "value"
531
// ie. id
532
// - fld0
533
// - op:like
534
// - value:abc
535
// - fld1 (if there is a range)
536
// - op:lessThan
537
// - value:55 (note: these two "flds" wouldn't really go together)
538
// Also note that op/fld can be in any order. (eg. id_fld1_equals or id_equals_fld1)
539
// Note that "normalizedFields" will contain values other than those
540
// Contained in the associated entity.
541
// Those extra fields will be ignored in the second half of this method.
542
ModelEntity modelEntity = delegator.getModelEntity(entityName);
543         HashMap JavaDoc normalizedFields = new HashMap JavaDoc();
544         Iterator JavaDoc ifIter = inputFields.keySet().iterator();
545         //StringBuffer queryStringBuf = new StringBuffer();
546
while (ifIter.hasNext()) {
547             String JavaDoc fieldNameRaw = null; // The name as it appeas in the HTML form
548
String JavaDoc fieldNameRoot = null; // The entity field name. Everything to the left of the first "_" if
549
// it exists, or the whole word, if not.
550
String JavaDoc fieldPair = null; // "fld0" or "fld1" - begin/end of range or just fld0 if no range.
551
Object JavaDoc fieldValue = null; // If it is a "value" field, it will be the value to be used in the query.
552
// If it is an "op" field, it will be "equals", "greaterThan", etc.
553
int iPos = -1;
554             int iPos2 = -1;
555             HashMap JavaDoc subMap = null;
556             HashMap JavaDoc subMap2 = null;
557             String JavaDoc fieldMode = null;
558
559             fieldNameRaw = (String JavaDoc) ifIter.next();
560             fieldValue = inputFields.get(fieldNameRaw);
561             if (ObjectType.isEmpty(fieldValue)) {
562                 continue;
563             }
564
565             //queryStringBuffer.append(fieldNameRaw + "=" + fieldValue);
566
iPos = fieldNameRaw.indexOf("_"); // Look for suffix
567

568             // This is a hack to skip fields from "multi" forms
569
// These would have the form "fieldName_o_1"
570
if (iPos >= 0) {
571                 String JavaDoc suffix = fieldNameRaw.substring(iPos + 1);
572                 iPos2 = suffix.indexOf("_");
573                 if (iPos2 == 1) {
574                     continue;
575                 }
576             }
577
578             // If no suffix, assume no range (default to fld0) and operations of equals
579
// If no field op is present, it will assume "equals".
580
if (iPos < 0) {
581                 fieldNameRoot = fieldNameRaw;
582                 fieldPair = "fld0";
583                 fieldMode = "value";
584             } else { // Must have at least "fld0/1" or "equals, greaterThan, etc."
585
// Some bogus fields will slip in, like "ENTITY_NAME", but they will be ignored
586

587                 fieldNameRoot = fieldNameRaw.substring(0, iPos);
588             }
589             if (modelEntity.isField(fieldNameRoot)) {
590                 normalizedFields.put(fieldNameRaw, fieldValue);
591             }
592         }
593         return normalizedFields;
594     }
595 }
596
Popular Tags