View Javadoc
1   /*
2    * Copyright 2007-2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.module.purap.businessobject.lookup;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.vnd.VendorConstants;
20  import org.kuali.ole.vnd.VendorKeyConstants;
21  import org.kuali.ole.vnd.VendorPropertyConstants;
22  import org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl;
23  import org.kuali.rice.krad.bo.BusinessObject;
24  import org.kuali.rice.krad.lookup.LookupUtils;
25  import org.kuali.rice.krad.util.BeanPropertyComparator;
26  import org.kuali.rice.krad.util.GlobalVariables;
27  import org.kuali.rice.krad.util.KRADConstants;
28  
29  import java.util.Collections;
30  import java.util.List;
31  import java.util.Map;
32  
33  /**
34   * This lookupable helper service is used to support lookups on the Threshold BO because it deals with the vendor number,
35   * which isn't really a field, but rather a combination of 2 fields.
36   * <p/>
37   * This code mostly copies {@link org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl}, but differs in that this class will
38   * not remove search criteria containing values corresponding to hidden fields.
39   */
40  public class ThresholdLookupableHelperService extends AbstractLookupableHelperServiceImpl {
41  
42      public void validateSearchParameters(Map fieldValues) {
43          super.validateSearchParameters(fieldValues);
44  
45          validateVendorNumber(fieldValues);
46      }
47  
48      /**
49       * Validates that the Vendor Number has no more than one dash in it, and does not consist solely of one dash. Then it calls
50       * extractVendorNumberToVendorIds to obtain vendorHeaderGeneratedId and vendorDetailAssignedId and if either one of the ids
51       * cannot be converted to integers, it will add error that the vendor number must be numerics or numerics separated by a dash.
52       *
53       * @param fieldValues a Map containing only those key-value pairs that have been filled in on the lookup
54       */
55      protected void validateVendorNumber(Map fieldValues) {
56          String vendorNumber = (String) fieldValues.get(VendorPropertyConstants.VENDOR_NUMBER);
57          if (StringUtils.isNotBlank(vendorNumber)) {
58              int dashPos1 = vendorNumber.indexOf(VendorConstants.DASH);
59              if (dashPos1 > -1) { // There's a dash in the number.
60                  if (vendorNumber.indexOf(VendorConstants.DASH, dashPos1 + 1) > -1) { // There can't be more than one.
61                      GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_NUMBER, VendorKeyConstants.ERROR_VENDOR_LOOKUP_VNDR_NUM_TOO_MANY_DASHES);
62                  }
63                  if (vendorNumber.matches("\\-*")) {
64                      GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_NUMBER, VendorKeyConstants.ERROR_VENDOR_LOOKUP_VNDR_NUM_DASHES_ONLY);
65                  }
66              }
67              extractVendorNumberToVendorIds(fieldValues, vendorNumber);
68          }
69      }
70  
71      /**
72       * Parses the vendorNumber string into vendorHeaderGeneratedIdentifier and vendorDetailAssignedIdentifier, validates that both
73       * fields would be able to be converted into integers, if so it will add both fields into the search criterias map in the
74       * fieldValues and remove the vendorNumber from the fieldValues. If the two fields cannot be converted into integers, this
75       * method will add error message to the errorMap in GlobalVariables that the vendor number must be numeric or numerics separated
76       * by a dash.
77       *
78       * @param fieldValues  a Map containing only those key-value pairs that have been filled in on the lookup
79       * @param vendorNumber venodr number String
80       */
81      protected void extractVendorNumberToVendorIds(Map fieldValues, String vendorNumber) {
82          String vendorHeaderGeneratedIdentifier = null;
83          String vendorDetailAssignedIdentifier = null;
84          int indexOfDash = vendorNumber.indexOf(VendorConstants.DASH);
85          if (indexOfDash < 0) {
86              vendorHeaderGeneratedIdentifier = vendorNumber;
87          } else {
88              vendorHeaderGeneratedIdentifier = vendorNumber.substring(0, indexOfDash);
89              vendorDetailAssignedIdentifier = vendorNumber.substring(indexOfDash + 1, vendorNumber.length());
90          }
91          try {
92              if (StringUtils.isNotEmpty(vendorHeaderGeneratedIdentifier)) {
93                  Integer.parseInt(vendorHeaderGeneratedIdentifier);
94              }
95              if (StringUtils.isNotEmpty(vendorDetailAssignedIdentifier)) {
96                  Integer.parseInt(vendorDetailAssignedIdentifier);
97              }
98              fieldValues.remove(VendorPropertyConstants.VENDOR_NUMBER);
99              fieldValues.put(VendorPropertyConstants.VENDOR_HEADER_GENERATED_ID, vendorHeaderGeneratedIdentifier);
100             fieldValues.put(VendorPropertyConstants.VENDOR_DETAIL_ASSIGNED_ID, vendorDetailAssignedIdentifier);
101         } catch (NumberFormatException headerExc) {
102             GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_NUMBER, VendorKeyConstants.ERROR_VENDOR_LOOKUP_VNDR_NUM_NUMERIC_DASH_SEPARATED);
103         }
104     }
105 
106     /**
107      * Uses Lookup Service to provide a basic search.
108      *
109      * @param fieldValues - Map containing prop name keys and search values
110      * @return List found business objects
111      * @see org.kuali.rice.kns.lookup.LookupableHelperService#getSearchResults(java.util.Map)
112      */
113     public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
114         return getSearchResultsHelper(LookupUtils.forceUppercase(getBusinessObjectClass(), fieldValues), false);
115     }
116 
117 
118     /**
119      * Uses Lookup Service to provide a basic unbounded search.
120      *
121      * @param fieldValues - Map containing prop name keys and search values
122      * @return List found business objects
123      * @see org.kuali.rice.kns.lookup.LookupableHelperService#getSearchResultsUnbounded(java.util.Map)
124      */
125     public List<? extends BusinessObject> getSearchResultsUnbounded(Map<String, String> fieldValues) {
126         return getSearchResultsHelper(LookupUtils.forceUppercase(getBusinessObjectClass(), fieldValues), true);
127     }
128 
129     /**
130      * This method does the actual search, with the parameters specified, and returns the result.
131      * <p/>
132      * NOTE that it will not do any upper-casing based on the DD forceUppercase. That is handled through an external call to
133      * LookupUtils.forceUppercase().
134      *
135      * @param fieldValues A Map of the fieldNames and fieldValues to be searched on.
136      * @param unbounded   Whether the results should be bounded or not to a certain max size.
137      * @return A List of search results.
138      */
139     @SuppressWarnings("unchecked")
140     protected List<? extends BusinessObject> getSearchResultsHelper(Map<String, String> fieldValues, boolean unbounded) {
141         // pretty much the same code as exists in KualiLookupableHelperServiceImpl, except that we're not removing hidden fields
142 
143         boolean searchUsingOnlyPrimaryKeyValues = getLookupService().allPrimaryKeyValuesPresentAndNotWildcard(getBusinessObjectClass(), fieldValues);
144 
145         setBackLocation(fieldValues.get(KRADConstants.BACK_LOCATION));
146         setDocFormKey(fieldValues.get(KRADConstants.DOC_FORM_KEY));
147         setReferencesToRefresh(fieldValues.get(KRADConstants.REFERENCES_TO_REFRESH));
148         List searchResults = (List) getLookupService().findCollectionBySearchHelper(getBusinessObjectClass(), fieldValues, unbounded);
149         // sort list if default sort column given
150         List defaultSortColumns = getDefaultSortColumns();
151         if (defaultSortColumns.size() > 0) {
152             Collections.sort(searchResults, new BeanPropertyComparator(getDefaultSortColumns(), true));
153         }
154         return searchResults;
155     }
156 }
157