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.kns.web.comparator;
017
018import org.apache.commons.collections.comparators.ComparableComparator;
019import org.apache.commons.lang.StringUtils;
020import org.displaytag.model.Cell;
021import org.kuali.rice.core.api.util.type.TypeUtils;
022import org.kuali.rice.krad.comparator.NumericValueComparator;
023import org.kuali.rice.krad.comparator.StringValueComparator;
024import org.kuali.rice.krad.comparator.TemporalValueComparator;
025
026import java.util.Comparator;
027import java.util.regex.Matcher;
028import java.util.regex.Pattern;
029
030/**
031 * This class...
032 *
033 * @deprecated KNS Struts deprecated, use KRAD and the Spring MVC framework.
034 */
035@Deprecated
036public class CellComparatorHelper {
037
038    static private Pattern HREF_ENCLOSURE = Pattern.compile("<a [^>]+>([^<]*)</a>.*", Pattern.MULTILINE);
039
040    /**
041     * This method is intended to be used in conjunction with displaytag.
042     * 
043     * @see #getSanitizedStaticValue(String)
044     * 
045     * @param cell
046     * @return a sanitized version of cell.getStaticValue().toString().
047     */
048    static public String getSanitizedStaticValue(Cell cell) {
049        if (null == cell) {
050            return null;
051        }
052
053        return null == cell.getStaticValue() ? null : getSanitizedStaticValue(cell.getStaticValue().toString());
054    }
055
056    /**
057     * Remove all end-of-line and tab characters (\r, \n, \t). If the value is enclosed in an html anchor tag, strip the html anchor
058     * tag. If the value ends in one or many "&nbsp;"s, strip them off. Return the modified String.
059     * 
060     * @param staticValue
061     * @return a sanitized version of staticValue
062     */
063    static public String getSanitizedStaticValue(String staticValue) {
064
065        if (null == staticValue) {
066            return null;
067        }
068
069        staticValue = StringUtils.replace(staticValue, "\r", "");
070        staticValue = StringUtils.replace(staticValue, "\n", "");
071        staticValue = StringUtils.replace(staticValue, "\t", "");
072
073        String sanitizedValue = staticValue;
074
075        // Extract the value if it's wrapped in an href.
076        Matcher matcher = HREF_ENCLOSURE.matcher(staticValue);
077        if (matcher.matches()) {
078
079            sanitizedValue = matcher.group(1).trim();
080
081        }
082
083        // Strip off any "&nbsp;"s if they come at the end of the value.
084        while (sanitizedValue.endsWith("&nbsp;")) {
085
086            sanitizedValue = sanitizedValue.substring(0, sanitizedValue.length() - 6).trim();
087
088        }
089
090        return sanitizedValue;
091
092    }
093
094    /**
095     * This method returns a comparator to be used for comparing the contents of cells, that is
096     * the compareTo method will be invoked w/ displaytag Cell objects
097     * @param propClass
098     * @return
099     */
100    public static Comparator getAppropriateComparatorForPropertyClass(Class propClass) {
101        // TODO, do we really need to create so many comparators (1 per each cell)?
102        if (propClass == null) {
103            return new NullCellComparator();
104        }
105        else if (TypeUtils.isDecimalClass(propClass) || TypeUtils.isIntegralClass(propClass)) {
106            return new NumericCellComparator();
107        }
108        else if (TypeUtils.isTemporalClass(propClass)) {
109            return new TemporalCellComparator();
110        }
111        else if (String.class.equals(propClass)) {
112            // StringCellComparator is smarter about nulls than String.CASE_INSENSITIVE_ORDER
113            return new StringCellComparator();
114        }
115        else {
116            return ComparableComparator.getInstance();
117        }
118    }
119    
120    /**
121     * This method returns a comparator to be used for comparing propertyValues (in String form)
122     * @param propClass
123     * @return
124     */
125    public static Comparator getAppropriateValueComparatorForPropertyClass(Class propClass) {
126        if (propClass == null) {
127            return NullValueComparator.getInstance();
128        }
129        else if (TypeUtils.isDecimalClass(propClass) || TypeUtils.isIntegralClass(propClass)) {
130            return NumericValueComparator.getInstance();
131        }
132        else if (TypeUtils.isTemporalClass(propClass)) {
133            return TemporalValueComparator.getInstance();
134        }
135        else if (String.class.equals(propClass)) {
136            // StringCellComparator is smarter about nulls than String.CASE_INSENSITIVE_ORDER
137            return StringValueComparator.getInstance();
138        }
139        else {
140            return ComparableComparator.getInstance();
141        }
142    }
143}