1 /*
2 * Copyright 2006-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.ole.sys.document.web;
17
18 import java.util.Iterator;
19 import java.util.Map;
20 import java.util.Set;
21
22 import org.kuali.rice.krad.bo.PersistableBusinessObject;
23
24 /**
25 * This class...
26 */
27 public abstract class CodeDescriptionFormatterBase implements CodeDescriptionFormatter {
28
29 public static final String DEFAULT_DESCRIPTION = "(description unavailable)";
30
31 /**
32 * The output string will probably be larger than the default StringBuffer size of 10, so lets avoid 1 memory allocation and
33 * copy
34 */
35 public static final int INIT_BUFFER_SIZE = 20;
36
37 /**
38 * @see org.kuali.ole.sys.document.webCodeDescriptionFormatter#getFormattedStringWithDescriptions(java.util.Set,
39 * java.lang.String, java.lang.String)
40 */
41 public String getFormattedStringWithDescriptions(Set values, String startConjunction, String endConjunction) {
42 Map<String, PersistableBusinessObject> valueToBOMap = getValuesToBusinessObjectsMap(values);
43 StringBuffer buf = new StringBuffer();
44
45 Iterator valuesIter = values.iterator();
46
47 if (valuesIter.hasNext()) {
48 if (startConjunction != null && !"".equals(startConjunction)) {
49 buf.append(startConjunction).append(" ");
50 }
51 String currValue = (String) valuesIter.next();
52 buf.append(currValue).append(", ");
53
54 PersistableBusinessObject bo = valueToBOMap.get(currValue);
55 buf.append(bo == null ? getDefaultDescription() : getDescriptionOfBO(bo));
56 }
57 else {
58 buf.append("(none)");
59 }
60
61 while (valuesIter.hasNext()) {
62 buf.append("; ");
63
64 String currValue = (String) valuesIter.next();
65 if (!valuesIter.hasNext()) {
66 // no more values after this, it's time to put the end conjunction
67 buf.append(endConjunction).append(" ");
68 }
69
70 buf.append(currValue).append(", ");
71
72 PersistableBusinessObject bo = valueToBOMap.get(currValue);
73 buf.append(bo == null ? getDefaultDescription() : getDescriptionOfBO(bo));
74 }
75 return buf.toString();
76 }
77
78 /**
79 * 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
80 * 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
81 * 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.
82 *
83 * @param values a set of values to limit the retrieval from (optional feature), may be null
84 * @return a map from value string to BO
85 */
86 protected abstract Map<String, PersistableBusinessObject> getValuesToBusinessObjectsMap(Set values);
87
88 /**
89 * Returns the description of a BO
90 *
91 * @param bo
92 * @return
93 */
94 protected abstract String getDescriptionOfBO(PersistableBusinessObject bo);
95
96 protected String getDefaultDescription() {
97 return DEFAULT_DESCRIPTION;
98 }
99 }