001/**
002 * Copyright 2005-2015 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.demo.uif.lookup;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.util.RiceKeyConstants;
020import org.kuali.rice.krad.lookup.LookupForm;
021import org.kuali.rice.krad.lookup.LookupableImpl;
022import org.kuali.rice.krad.util.GlobalVariables;
023
024import java.text.NumberFormat;
025import java.text.ParseException;
026import java.util.HashSet;
027import java.util.Map;
028import java.util.Set;
029
030/**
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 */
033public class TravelLookupableImpl extends LookupableImpl {
034
035    /**
036     * Add additional validation to check that numeric values are positive
037     *
038     * @see LookupableImpl#validateSearchParameters(org.kuali.rice.krad.lookup.LookupForm, java.util.Map)
039     */
040    @Override
041    protected boolean validateSearchParameters(LookupForm form, Map<String, String> searchCriteria) {
042        boolean valid = super.validateSearchParameters(form, searchCriteria);
043
044        if (form.getViewPostMetadata() != null && form.getViewPostMetadata().getLookupCriteria() != null) {
045            for (Map.Entry<String, Map<String, Object>> lookupCriteria : form.getViewPostMetadata().getLookupCriteria().entrySet()) {
046                String propertyName = lookupCriteria.getKey();
047
048                validateSearchParameterPositiveValues(form, propertyName, searchCriteria.get(propertyName));
049            }
050        }
051
052        if (GlobalVariables.getMessageMap().hasErrors()) {
053            valid = false;
054        }
055
056        return valid;
057    }
058
059    /**
060     * Validates that any numeric value is non-negative.
061     *
062     * @param form lookup form instance containing the lookup data
063     * @param propertyName property name of the search criteria field to be validated
064     * @param searchPropertyValue value given for field to search for
065     */
066    protected void validateSearchParameterPositiveValues(LookupForm form, String propertyName, String searchPropertyValue) {
067        if (StringUtils.isBlank(searchPropertyValue)) {
068            return;
069        }
070
071        NumberFormat format = NumberFormat.getInstance();
072        Number number = null;
073        try {
074            number = format.parse(searchPropertyValue);
075        } catch (ParseException e) {
076            return;
077        }
078
079        if (Math.signum(number.doubleValue()) < 0) {
080            GlobalVariables.getMessageMap().putError(propertyName,
081                        RiceKeyConstants.ERROR_NEGATIVES_NOT_ALLOWED_ON_FIELD, getCriteriaLabel(form, propertyName));
082        }
083    }
084}