View Javadoc
1   /**
2    * Copyright 2005-2016 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.datadictionary.exporter;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.kns.datadictionary.control.ButtonControlDefinition;
20  import org.kuali.rice.kns.datadictionary.control.CurrencyControlDefinition;
21  import org.kuali.rice.kns.datadictionary.control.LinkControlDefinition;
22  import org.kuali.rice.krad.datadictionary.AttributeDefinition;
23  import org.kuali.rice.krad.datadictionary.DataDictionaryEntryBase;
24  import org.kuali.rice.krad.datadictionary.control.ControlDefinition;
25  import org.kuali.rice.krad.datadictionary.exporter.ExportMap;
26  
27  import java.util.ArrayList;
28  import java.util.Collections;
29  import java.util.Comparator;
30  import java.util.List;
31  import java.util.Map;
32  
33  /**
34   * AttributesMapBuilder
35   *
36   * @deprecated Only used by KNS classes, no replacement.
37   */
38  @Deprecated
39  public class AttributesMapBuilder {
40  
41      /**
42       * Default constructor
43       */
44      public AttributesMapBuilder() {
45      }
46  
47  
48      /**
49       * @param entry
50       * @return ExportMap containing the standard entries for the entry's AttributesDefinition
51       */
52      public ExportMap buildAttributesMap(DataDictionaryEntryBase entry) {
53          ExportMap attributesMap = new ExportMap("attributes");
54  
55          for ( AttributeDefinition attribute : entry.getAttributes() ) {
56              attributesMap.set(buildAttributeMap(attribute, entry.getFullClassName()));
57          }
58  
59          return attributesMap;
60      }
61  
62      public ExportMap buildAttributeMap(AttributeDefinition attribute, String fullClassName) {
63          ExportMap attributeMap = new ExportMap(attribute.getName());
64  
65          // simple properties
66          attributeMap.set("name", attribute.getName());
67          attributeMap.set("forceUppercase", attribute.getForceUppercase().toString());
68          attributeMap.set("label", attribute.getLabel());
69          attributeMap.set("shortLabel", attribute.getShortLabel());
70  
71          //KULRICE-9144 remove maxLength non null assumption
72          Integer maxLength = attribute.getMaxLength();
73          if (maxLength != null) {
74              attributeMap.set("maxLength", maxLength.toString());
75          }
76          String exclusiveMin = attribute.getExclusiveMin();
77          if (exclusiveMin != null) {
78              attributeMap.set("exclusiveMin", exclusiveMin/*.toString()*/);
79          }
80          String exclusiveMax = attribute.getInclusiveMax();
81          if (exclusiveMax != null) {
82              attributeMap.set("exclusiveMax", exclusiveMax/*.toString()*/);
83          }
84  
85          if (attribute.isRequired() != null) {
86              attributeMap.set("required", attribute.isRequired().toString());
87          } else {
88              attributeMap.set("required", "false");
89          }
90          if (attribute.getSummary() != null) {
91              attributeMap.set("summary", attribute.getSummary());
92          }
93          if (attribute.getDescription() != null) {
94              attributeMap.set("description", attribute.getDescription());
95          }
96          if (attribute.hasFormatterClass()) {
97              attributeMap.set("formatterClass", attribute.getFormatterClass());
98          }
99  /**
100         // complex properties
101         if (attribute.hasValidationPattern()) {
102             attributeMap.set(attribute.getValidationPattern().buildExportMap("validationPattern"));
103         }
104 
105         if(attribute.hasAttributeSecurity()){
106         	attributeMap.set("attributeSecurityMask", String.valueOf(attribute.getAttributeSecurity().isMask()));
107         	attributeMap.set("attributeSecurityPartialMask", String.valueOf(attribute.getAttributeSecurity().isPartialMask()));
108         	attributeMap.set("attributeSecurityHide", String.valueOf(attribute.getAttributeSecurity().isHide()));
109         	attributeMap.set("attributeSecurityReadOnly", String.valueOf(attribute.getAttributeSecurity().isReadOnly()));
110   	
111         	// TODO: consider whether to export class names from the attribute security
112         }
113 */
114         attributeMap.set(buildControlMap(attribute));
115         if (attribute.getOptionsFinder() != null) {
116             attributeMap.set(buildKeyLabelMap(attribute));
117         }
118         if (StringUtils.isNotBlank(fullClassName)) {
119             attributeMap.set("fullClassName", fullClassName);
120         }
121 
122         return attributeMap;
123     }
124 
125     private ExportMap buildKeyLabelMap(AttributeDefinition attribute) {
126 
127         ExportMap keyLabelMap = new ExportMap("keyLabelMap");
128 
129         List<Map.Entry<String, String>> keyLabelList = new ArrayList<Map.Entry<String, String>>(attribute.getOptionsFinder().getKeyLabelMap().entrySet());
130         Collections.sort(keyLabelList, new Comparator<Map.Entry<String, String>>() {
131             @Override
132             public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
133                  return o1.getValue().compareTo(o2.getValue());
134             }
135         });
136         for (Map.Entry<String, String> entry : keyLabelList) {
137             keyLabelMap.set(entry.getKey(), entry.getValue());
138         }
139         return keyLabelMap;
140     }
141 
142     private ExportMap buildControlMap(AttributeDefinition attribute) {
143         ControlDefinition control = attribute.getControl();
144         ExportMap controlMap = new ExportMap("control");
145 
146         if ( control != null ) {
147             if (control.isCheckbox()) {
148                 controlMap.set("checkbox", "true");
149             }
150             else if (control.isHidden()) {
151                 controlMap.set("hidden", "true");
152             }
153             else if (control.isKualiUser()) {
154                 controlMap.set("kualiUser", "true");
155             }
156             else if (control.isRadio()) {
157                 controlMap.set("radio", "true");
158                 if (control.getValuesFinderClass() != null) {
159                     controlMap.set("valuesFinder", control.getValuesFinderClass());
160                 }
161                 if (control.getBusinessObjectClass() != null) {
162                     controlMap.set("businessObject", control.getBusinessObjectClass());
163                 }
164                 if (StringUtils.isNotEmpty(control.getKeyAttribute())) {
165                     controlMap.set("keyAttribute", control.getKeyAttribute());
166                 }
167                 if (StringUtils.isNotEmpty(control.getLabelAttribute())) {
168                     controlMap.set("labelAttribute", control.getLabelAttribute());
169                 }
170                 if (control.getIncludeKeyInLabel() != null) {
171                     controlMap.set("includeKeyInLabel", control.getIncludeKeyInLabel().toString());
172                 }
173             }
174             else if (control.isSelect()) {
175                 controlMap.set("select", "true");
176                 if (control.getValuesFinderClass() != null) {
177                     controlMap.set("valuesFinder", control.getValuesFinderClass());
178                 }
179                 if (control.getBusinessObjectClass() != null) {
180                     controlMap.set("businessObject", control.getBusinessObjectClass());
181                 }
182                 if (StringUtils.isNotEmpty(control.getKeyAttribute())) {
183                     controlMap.set("keyAttribute", control.getKeyAttribute());
184                 }
185                 if (StringUtils.isNotEmpty(control.getLabelAttribute())) {
186                     controlMap.set("labelAttribute", control.getLabelAttribute());
187                 }
188                 if (control.getIncludeBlankRow() != null) {
189                     controlMap.set("includeBlankRow", control.getIncludeBlankRow().toString());
190                 }
191                 if (control.getIncludeKeyInLabel() != null) {
192                     controlMap.set("includeKeyInLabel", control.getIncludeKeyInLabel().toString());
193                 }
194             }
195             else if (control.isMultiselect()) {
196                 controlMap.set("multiselect", "true");
197                 if (control.getValuesFinderClass() != null) {
198                     controlMap.set("valuesFinder", control.getValuesFinderClass());
199                 }
200                 if (control.getBusinessObjectClass() != null) {
201                     controlMap.set("businessObject", control.getBusinessObjectClass());
202                 }
203                 if (StringUtils.isNotEmpty(control.getKeyAttribute())) {
204                     controlMap.set("keyAttribute", control.getKeyAttribute());
205                 }
206                 if (StringUtils.isNotEmpty(control.getLabelAttribute())) {
207                     controlMap.set("labelAttribute", control.getLabelAttribute());
208                 }
209                 if (control.getIncludeKeyInLabel() != null) {
210                     controlMap.set("includeKeyInLabel", control.getIncludeKeyInLabel().toString());
211                 }
212                 if (control.getSize() != null) {
213                 	controlMap.set("size", control.getSize().toString());
214                 }
215             }
216             else if (control.isText()) {
217                 controlMap.set("text", "true");
218                 if (control.getSize() != null) {
219                     controlMap.set("size", control.getSize().toString());
220                 }
221                 controlMap.set("datePicker", Boolean.valueOf(control.isDatePicker()).toString());
222                 controlMap.set("ranged", Boolean.valueOf(control.isRanged()).toString());
223             }
224             else if (control.isTextarea()) {
225                 controlMap.set("textarea", "true");
226                 controlMap.set("rows", control.getRows().toString());
227                 controlMap.set("cols", control.getCols().toString());
228                 controlMap.set("expandedTextArea", Boolean.valueOf(control.isExpandedTextArea()).toString());
229             }
230             else if (control.isCurrency()) {
231                 controlMap.set("currency", "true");
232                 if (control.getSize() != null) {
233                     controlMap.set("size", control.getSize().toString());
234                 }
235                 controlMap.set("formattedMaxLength", ((CurrencyControlDefinition) control).getFormattedMaxLength().toString());
236             }
237             else if (control.isLookupHidden()) {
238                 controlMap.set("lookupHidden", "true");
239             }
240             else if (control.isLookupReadonly()) {
241                 controlMap.set("lookupReadonly", "true");
242             }else if (control.isButton()) {
243                 controlMap.set("button", "true");
244                 if (StringUtils.isNotEmpty(((ButtonControlDefinition) control).getImageSrc())) {
245                 	controlMap.set("imageSrc", ((ButtonControlDefinition) control).getImageSrc());
246                 }
247                 if (StringUtils.isNotEmpty(((ButtonControlDefinition) control).getStyleClass())) {
248                 	controlMap.set("styleClass", ((ButtonControlDefinition) control).getStyleClass() );
249                 }
250             }else if (control.isLink()) {
251                 controlMap.set("link", "true");
252                 if (StringUtils.isNotEmpty(((LinkControlDefinition) control).getTarget())) {
253                 	controlMap.set("target", ((LinkControlDefinition) control).getTarget());
254                 }
255                 if (StringUtils.isNotEmpty(((LinkControlDefinition) control).getStyleClass())) {
256                 	controlMap.set("styleClass", ((LinkControlDefinition) control).getStyleClass() );
257                 }
258                 if (StringUtils.isNotEmpty(((LinkControlDefinition) control).getHrefText())) {
259                 	controlMap.set("hrefText", ((LinkControlDefinition) control).getHrefText());
260                 }
261             }
262         } else {
263             controlMap.set("text", "true");
264             controlMap.set("size", "40");
265         }
266 
267         return controlMap;
268     }
269 }