001 /* 002 * Copyright 2011 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 */ 016 package org.kuali.kfs.sys.document.web; 017 018 import java.util.Iterator; 019 import java.util.Map; 020 import java.util.Set; 021 022 import org.kuali.rice.kns.bo.PersistableBusinessObject; 023 024 /** 025 * This class... 026 */ 027 public abstract class CodeDescriptionFormatterBase implements CodeDescriptionFormatter { 028 029 public static final String DEFAULT_DESCRIPTION = "(description unavailable)"; 030 031 /** 032 * The output string will probably be larger than the default StringBuffer size of 10, so lets avoid 1 memory allocation and 033 * copy 034 */ 035 public static final int INIT_BUFFER_SIZE = 20; 036 037 /** 038 * @see org.kuali.kfs.sys.document.webCodeDescriptionFormatter#getFormattedStringWithDescriptions(java.util.Set, 039 * java.lang.String, java.lang.String) 040 */ 041 public String getFormattedStringWithDescriptions(Set values, String startConjunction, String endConjunction) { 042 Map<String, PersistableBusinessObject> valueToBOMap = getValuesToBusinessObjectsMap(values); 043 StringBuffer buf = new StringBuffer(); 044 045 Iterator valuesIter = values.iterator(); 046 047 if (valuesIter.hasNext()) { 048 if (startConjunction != null && !"".equals(startConjunction)) { 049 buf.append(startConjunction).append(" "); 050 } 051 String currValue = (String) valuesIter.next(); 052 buf.append(currValue).append(", "); 053 054 PersistableBusinessObject bo = valueToBOMap.get(currValue); 055 buf.append(bo == null ? getDefaultDescription() : getDescriptionOfBO(bo)); 056 } 057 else { 058 buf.append("(none)"); 059 } 060 061 while (valuesIter.hasNext()) { 062 buf.append("; "); 063 064 String currValue = (String) valuesIter.next(); 065 if (!valuesIter.hasNext()) { 066 // no more values after this, it's time to put the end conjunction 067 buf.append(endConjunction).append(" "); 068 } 069 070 buf.append(currValue).append(", "); 071 072 PersistableBusinessObject bo = valueToBOMap.get(currValue); 073 buf.append(bo == null ? getDefaultDescription() : getDescriptionOfBO(bo)); 074 } 075 return buf.toString(); 076 } 077 078 /** 079 * Returns a Map such that the values in the values set will map to the appropriate BO There may be mappings for values that are 080 * not in the parameter set Use this method sparingly, as it will likely cause an access to the DB It may be desirable to use 081 * the values to limit the breadth of the search, and it is up to the implementation to decide whether to use it to do so. 082 * 083 * @param values a set of values to limit the retrieval from (optional feature), may be null 084 * @return a map from value string to BO 085 */ 086 protected abstract Map<String, PersistableBusinessObject> getValuesToBusinessObjectsMap(Set values); 087 088 /** 089 * Returns the description of a BO 090 * 091 * @param bo 092 * @return 093 */ 094 protected abstract String getDescriptionOfBO(PersistableBusinessObject bo); 095 096 protected String getDefaultDescription() { 097 return DEFAULT_DESCRIPTION; 098 } 099 }