001/**
002 * Copyright 2005-2016 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.ui;
017
018import org.apache.commons.lang.StringUtils;
019import org.displaytag.decorator.DisplaytagColumnDecorator;
020import org.displaytag.exception.DecoratorException;
021import org.displaytag.properties.MediaTypeEnum;
022import org.kuali.rice.kns.web.comparator.CellComparatorHelper;
023import org.kuali.rice.krad.util.KRADConstants;
024
025import javax.servlet.jsp.PageContext;
026
027/** @see #decorate(Object, PageContext, MediaTypeEnum) */
028@Deprecated
029public class FormatAwareDecorator implements DisplaytagColumnDecorator {
030
031    /**
032     * Empty values don't show up properly in HTML. So, the String " " is substituted for an empty or null value of cellValue
033     * if mediaType is MediaTypeEnum.HTML. If mediaType is not {@link MediaTypeEnum.HTML} and cellValue is not null, then
034     * <code>CellComparatorHelper.getSanitizedValue(cellValue.toString())</code> is returned.
035     * 
036     * @param cellValue
037     * @param pageContext
038     * @param mediaType
039     */
040    public Object decorate(Object cellValue, PageContext pageContext, MediaTypeEnum mediaType) throws DecoratorException {
041
042        if (null == cellValue) {
043            return getEmptyStringFor(mediaType);
044        }
045        
046        final String decoratedOutput;
047        
048        if (isCollection(cellValue)) {
049                decoratedOutput = createCollectionString(cellValue);
050        } else {
051                decoratedOutput = MediaTypeEnum.HTML.equals(mediaType) ? cellValue.toString() : CellComparatorHelper
052                    .getSanitizedStaticValue(cellValue.toString());
053        }
054
055        return StringUtils.isBlank(decoratedOutput) ? getEmptyStringFor(mediaType) : StringUtils.trim(decoratedOutput);
056    }
057    
058    /**
059     * Takes a cellValue which is a collection and creates a String representations.
060     * 
061     * <p>
062     * If a column resulting from lookup contains collection values, each of the collection entry
063     * should be printed on one line (i.e. separated by a <br/>). If there is no entry in the
064     * collection, then we'll just print an &nbsp for the column.
065     * </p>
066     * 
067     * @param cellValue the cell value to convert
068     * @return the string representation of the cell value
069     */
070    private static String createCollectionString(Object cellValue) {
071        String decoratedOutput = "";
072        
073        String cellContentToBeParsed = cellValue.toString().substring(1, cellValue.toString().indexOf("]"));
074        if (StringUtils.isNotBlank(cellContentToBeParsed)) {
075            String[] parsed = cellContentToBeParsed.split(",");
076            for (String elem : parsed) {
077                decoratedOutput = decoratedOutput + elem + "<br/>";                    
078            }
079        }
080        return decoratedOutput;
081    }
082    
083    /**
084     * Checks if a cell value is a Collection
085     * 
086     * @param cellValue to check
087     * @return true if a Collection
088     */
089    private static boolean isCollection(Object cellValue) {
090        return cellValue != null && (cellValue.toString().indexOf("[") == 0 && cellValue.toString().indexOf("]") > 0 && ((cellValue.toString().length() -1) == cellValue.toString().indexOf("]"))); 
091    }
092    
093    /**
094     * Gets an empty string type based on the media type.
095     * 
096     * @param mediaType the media type
097     * @return the empty string 
098     */
099    private static String getEmptyStringFor(MediaTypeEnum mediaType) {
100        return MediaTypeEnum.HTML.equals(mediaType) ? "&nbsp" : KRADConstants.EMPTY_STRING;
101    }
102
103}