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