001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.module.financial.util;
017:
018: import java.util.Iterator;
019: import java.util.Map;
020: import java.util.Set;
021:
022: import org.kuali.core.bo.PersistableBusinessObject;
023:
024: /**
025: * This class...
026: */
027: public abstract class CodeDescriptionFormatterBase implements
028: CodeDescriptionFormatter {
029:
030: public static final String DEFAULT_DESCRIPTION = "(description unavailable)";
031:
032: /**
033: * The output string will probably be larger than the default StringBuffer size of 10, so lets avoid 1 memory allocation and
034: * copy
035: */
036: public static final int INIT_BUFFER_SIZE = 20;
037:
038: /**
039: * @see org.kuali.module.financial.util.CodeDescriptionFormatter#getFormattedStringWithDescriptions(java.util.Set,
040: * java.lang.String, java.lang.String)
041: */
042: public String getFormattedStringWithDescriptions(Set values,
043: String startConjunction, String endConjunction) {
044: Map<String, PersistableBusinessObject> valueToBOMap = getValuesToBusinessObjectsMap(values);
045: StringBuffer buf = new StringBuffer();
046:
047: Iterator valuesIter = values.iterator();
048:
049: if (valuesIter.hasNext()) {
050: if (startConjunction != null
051: && !"".equals(startConjunction)) {
052: buf.append(startConjunction).append(" ");
053: }
054: String currValue = (String) valuesIter.next();
055: buf.append(currValue).append(", ");
056:
057: PersistableBusinessObject bo = valueToBOMap.get(currValue);
058: buf.append(bo == null ? getDefaultDescription()
059: : getDescriptionOfBO(bo));
060: } else {
061: buf.append("(none)");
062: }
063:
064: while (valuesIter.hasNext()) {
065: buf.append("; ");
066:
067: String currValue = (String) valuesIter.next();
068: if (!valuesIter.hasNext()) {
069: // no more values after this, it's time to put the end conjunction
070: buf.append(endConjunction).append(" ");
071: }
072:
073: buf.append(currValue).append(", ");
074:
075: PersistableBusinessObject bo = valueToBOMap.get(currValue);
076: buf.append(bo == null ? getDefaultDescription()
077: : getDescriptionOfBO(bo));
078: }
079: return buf.toString();
080: }
081:
082: /**
083: * 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
084: * 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
085: * 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.
086: *
087: * @param values a set of values to limit the retrieval from (optional feature), may be null
088: * @return a map from value string to BO
089: */
090: protected abstract Map<String, PersistableBusinessObject> getValuesToBusinessObjectsMap(
091: Set values);
092:
093: /**
094: * Returns the description of a BO
095: *
096: * @param bo
097: * @return
098: */
099: protected abstract String getDescriptionOfBO(
100: PersistableBusinessObject bo);
101:
102: protected String getDefaultDescription() {
103: return DEFAULT_DESCRIPTION;
104: }
105: }
|