View Javadoc
1   /**
2    * Copyright 2005-2016 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.rice.krad.demo.uif.lookup;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.util.RiceKeyConstants;
20  import org.kuali.rice.krad.lookup.LookupForm;
21  import org.kuali.rice.krad.lookup.LookupableImpl;
22  import org.kuali.rice.krad.util.GlobalVariables;
23  
24  import java.text.NumberFormat;
25  import java.text.ParseException;
26  import java.util.HashSet;
27  import java.util.Map;
28  import java.util.Set;
29  
30  /**
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  public class TravelLookupableImpl extends LookupableImpl {
34  
35      /**
36       * Add additional validation to check that numeric values are positive
37       *
38       * @see LookupableImpl#validateSearchParameters(org.kuali.rice.krad.lookup.LookupForm, java.util.Map)
39       */
40      @Override
41      protected boolean validateSearchParameters(LookupForm form, Map<String, String> searchCriteria) {
42          boolean valid = super.validateSearchParameters(form, searchCriteria);
43  
44          if (form.getViewPostMetadata() != null && form.getViewPostMetadata().getLookupCriteria() != null) {
45              for (Map.Entry<String, Map<String, Object>> lookupCriteria : form.getViewPostMetadata().getLookupCriteria().entrySet()) {
46                  String propertyName = lookupCriteria.getKey();
47  
48                  validateSearchParameterPositiveValues(form, propertyName, searchCriteria.get(propertyName));
49              }
50          }
51  
52          if (GlobalVariables.getMessageMap().hasErrors()) {
53              valid = false;
54          }
55  
56          return valid;
57      }
58  
59      /**
60       * Validates that any numeric value is non-negative.
61       *
62       * @param form lookup form instance containing the lookup data
63       * @param propertyName property name of the search criteria field to be validated
64       * @param searchPropertyValue value given for field to search for
65       */
66      protected void validateSearchParameterPositiveValues(LookupForm form, String propertyName, String searchPropertyValue) {
67          if (StringUtils.isBlank(searchPropertyValue)) {
68              return;
69          }
70  
71          NumberFormat format = NumberFormat.getInstance();
72          Number number = null;
73          try {
74              number = format.parse(searchPropertyValue);
75          } catch (ParseException e) {
76              return;
77          }
78  
79          if (Math.signum(number.doubleValue()) < 0) {
80              GlobalVariables.getMessageMap().putError(propertyName,
81                          RiceKeyConstants.ERROR_NEGATIVES_NOT_ALLOWED_ON_FIELD, getCriteriaLabel(form, propertyName));
82          }
83      }
84  }