| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| FormatAwareDecorator |
|
| 3.5;3.5 |
| 1 | /* | |
| 2 | * Copyright 2007-2008 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.kns.web.ui; | |
| 17 | ||
| 18 | import javax.servlet.jsp.PageContext; | |
| 19 | ||
| 20 | import org.apache.commons.lang.StringUtils; | |
| 21 | import org.displaytag.decorator.DisplaytagColumnDecorator; | |
| 22 | import org.displaytag.exception.DecoratorException; | |
| 23 | import org.displaytag.properties.MediaTypeEnum; | |
| 24 | import org.kuali.rice.kns.util.KNSConstants; | |
| 25 | import org.kuali.rice.kns.web.comparator.CellComparatorHelper; | |
| 26 | ||
| 27 | /** @see #decorate(Object, PageContext, MediaTypeEnum) */ | |
| 28 | 0 | public class FormatAwareDecorator implements DisplaytagColumnDecorator { |
| 29 | ||
| 30 | /** | |
| 31 | * Empty values don't show up properly in HTML. So, the String " " is substituted for an empty or null value of cellValue | |
| 32 | * if mediaType is MediaTypeEnum.HTML. If mediaType is not {@link MediaTypeEnum.HTML} and cellValue is not null, then | |
| 33 | * <code>CellComparatorHelper.getSanitizedValue(cellValue.toString())</code> is returned. | |
| 34 | * | |
| 35 | * @param cellValue | |
| 36 | * @param pageContext | |
| 37 | * @param mediaType | |
| 38 | */ | |
| 39 | public Object decorate(Object cellValue, PageContext pageContext, MediaTypeEnum mediaType) throws DecoratorException { | |
| 40 | ||
| 41 | 0 | if (null == cellValue) { |
| 42 | 0 | return getEmptyStringFor(mediaType); |
| 43 | } | |
| 44 | ||
| 45 | final String decoratedOutput; | |
| 46 | ||
| 47 | 0 | if (isCollection(cellValue)) { |
| 48 | 0 | decoratedOutput = createCollectionString(cellValue); |
| 49 | } else { | |
| 50 | 0 | decoratedOutput = MediaTypeEnum.HTML.equals(mediaType) ? cellValue.toString() : CellComparatorHelper.getSanitizedStaticValue(cellValue.toString()); |
| 51 | } | |
| 52 | ||
| 53 | 0 | return StringUtils.isBlank(decoratedOutput) ? getEmptyStringFor(mediaType) : StringUtils.trim(decoratedOutput); |
| 54 | } | |
| 55 | ||
| 56 | /** | |
| 57 | * Takes a cellValue which is a collection and creates a String representations. | |
| 58 | * | |
| 59 | * <p> | |
| 60 | * If a column resulting from lookup contains collection values, each of the collection entry | |
| 61 | * should be printed on one line (i.e. separated by a <br/>). If there is no entry in the | |
| 62 | * collection, then we'll just print an   for the column. | |
| 63 | * </p> | |
| 64 | * | |
| 65 | * @param cellValue the cell value to convert | |
| 66 | * @return the string representation of the cell value | |
| 67 | */ | |
| 68 | private static String createCollectionString(Object cellValue) { | |
| 69 | 0 | String decoratedOutput = ""; |
| 70 | ||
| 71 | 0 | String cellContentToBeParsed = cellValue.toString().substring(1, cellValue.toString().indexOf("]")); |
| 72 | 0 | if (StringUtils.isNotBlank(cellContentToBeParsed)) { |
| 73 | 0 | String[] parsed = cellContentToBeParsed.split(","); |
| 74 | 0 | for (String elem : parsed) { |
| 75 | 0 | decoratedOutput = decoratedOutput + elem + "<br/>"; |
| 76 | } | |
| 77 | } | |
| 78 | 0 | return decoratedOutput; |
| 79 | } | |
| 80 | ||
| 81 | /** | |
| 82 | * Checks if a cell value is a Collection | |
| 83 | * | |
| 84 | * @param cellValue to check | |
| 85 | * @return true if a Collection | |
| 86 | */ | |
| 87 | private static boolean isCollection(Object cellValue) { | |
| 88 | 0 | return cellValue != null && cellValue.toString().indexOf("[") == 0 && cellValue.toString().indexOf("]") > 0; |
| 89 | } | |
| 90 | ||
| 91 | /** | |
| 92 | * Gets an empty string type based on the media type. | |
| 93 | * | |
| 94 | * @param mediaType the media type | |
| 95 | * @return the empty string | |
| 96 | */ | |
| 97 | private static String getEmptyStringFor(MediaTypeEnum mediaType) { | |
| 98 | 0 | return MediaTypeEnum.HTML.equals(mediaType) ? " " : KNSConstants.EMPTY_STRING; |
| 99 | } | |
| 100 | ||
| 101 | } |