Blame view

framework/common/webcommon/WEB-INF/actions/includes/FindAutocompleteOptions.groovy 5.48 KB
c16038356   Anchit Jindal   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing,
   * software distributed under the License is distributed on an
   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   * KIND, either express or implied.  See the License for the
   * specific language governing permissions and limitations
   * under the License.
   */
  
  import org.ofbiz.base.util.StringUtil;
  import org.ofbiz.base.util.Debug;
  import org.ofbiz.base.util.UtilProperties;
  import org.ofbiz.entity.util.EntityFindOptions;
  import org.ofbiz.entity.condition.EntityCondition;
  import org.ofbiz.entity.condition.EntityConditionList;
  import org.ofbiz.entity.condition.EntityExpr;
  import org.ofbiz.entity.condition.EntityFieldValue;
  import org.ofbiz.entity.condition.EntityFunction;
  import org.ofbiz.entity.condition.EntityOperator;
  
  def mainAndConds = [];
  def orExprs = [];
  def entityName = context.entityName;
  def searchFields = context.searchFields;
  def displayFields = context.displayFields ?: searchFields;
  def searchDistinct = Boolean.valueOf(context.searchDistinct ?: false);
  
  def searchValueFieldName = parameters.term;
  def fieldValue = null;
  if (searchValueFieldName) {
      fieldValue = searchValueFieldName; 
  } else if (parameters.searchValueFieldName) { // This is to find the description of a lookup value on initialization.
      fieldValue = parameters.get(parameters.searchValueFieldName);
      context.description = "true";
  }
  
  def searchType = context.searchType;
  def displayFieldsSet = null;
  
  if (searchFields && fieldValue) {
      def searchFieldsList = StringUtil.toList(searchFields);
      displayFieldsSet = StringUtil.toSet(displayFields);
      if (context.description && fieldValue instanceof java.lang.String) {
          returnField = parameters.searchValueFieldName;
      } else {
          returnField = searchFieldsList[0]; //default to first element of searchFields
          displayFieldsSet.add(returnField); //add it to select fields, in case it is missing
      }
      context.returnField = returnField;
      context.displayFieldsSet = displayFieldsSet;
      if ("STARTS_WITH".equals(searchType)) {
          searchValue = fieldValue.toUpperCase() + "%";
      } else if ("EQUALS".equals(searchType)) {
          searchValue = fieldValue;
      } else {//default is CONTAINS
          searchValue = "%" + fieldValue.toUpperCase() + "%";
      }
      searchFieldsList.each { fieldName ->
          if ("EQUALS".equals(searchType)) {
              orExprs.add(EntityCondition.makeCondition(EntityFieldValue.makeFieldValue(searchFieldsList[0]), EntityOperator.EQUALS, searchValue));    
              return;//in case of EQUALS, we search only a match for the returned field
          } else {
              orExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER(EntityFieldValue.makeFieldValue(fieldName)), EntityOperator.LIKE, searchValue));
          }        
      }
  }
  
  /* the following is part of an attempt to handle additional parameters that are passed in from other form fields at run-time,
   * but that is not supported by the scrip.aculo.us Ajax.Autocompleter, but this is still useful to pass parameters from the
   * lookup screen definition:
   */
  def conditionFields = context.conditionFields;
  if (conditionFields) {
      // these fields are for additonal conditions, this is a Map of name/value pairs
      for (conditionFieldEntry in conditionFields.entrySet()) {
          if (conditionFieldEntry.getValue() instanceof java.util.List) {
              def orCondFields = [];
              for (entry in conditionFieldEntry.getValue()) {
                  orCondFields.add(EntityCondition.makeCondition(EntityFieldValue.makeFieldValue(conditionFieldEntry.getKey()), EntityOperator.EQUALS, entry));                
              }
              mainAndConds.add(EntityCondition.makeCondition(orCondFields, EntityOperator.OR));            
          } else {
              mainAndConds.add(EntityCondition.makeCondition(EntityFieldValue.makeFieldValue(conditionFieldEntry.getKey()), EntityOperator.EQUALS, conditionFieldEntry.getValue()));
          }
      }
  }
  
  if (orExprs && entityName && displayFieldsSet) {
      mainAndConds.add(EntityCondition.makeCondition(orExprs, EntityOperator.OR));
  
      //if there is an extra condition, add it to main condition list
      if (context.andCondition && context.andCondition instanceof EntityCondition) {
          mainAndConds.add(context.andCondition);
      }
      
      def entityConditionList = EntityCondition.makeCondition(mainAndConds, EntityOperator.AND);    
      
      String viewSizeStr = context.autocompleterViewSize;
      if (viewSizeStr == null) {
          viewSizeStr = UtilProperties.getPropertyValue("widget", "widget.autocompleter.defaultViewSize");
      }
      Integer autocompleterViewSize = Integer.valueOf(viewSizeStr ?: 10);
      EntityFindOptions findOptions = new EntityFindOptions();
      findOptions.setMaxRows(autocompleterViewSize);
      findOptions.setDistinct(searchDistinct);
      
      autocompleteOptions = delegator.findList(entityName, entityConditionList, displayFieldsSet, StringUtil.toList(displayFields), findOptions, false);
      if (autocompleteOptions) {
          context.autocompleteOptions = autocompleteOptions;
      }
  }