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