View Javadoc
1   /**
2    * Copyright 2005-2015 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   *
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.isCheckbox()) {
147             controlMap.set("checkbox", "true");
148         }
149         else if (control.isHidden()) {
150             controlMap.set("hidden", "true");
151         }
152         else if (control.isKualiUser()) {
153             controlMap.set("kualiUser", "true");
154         }
155         else if (control.isRadio()) {
156             controlMap.set("radio", "true");
157             if (control.getValuesFinderClass() != null) {
158                 controlMap.set("valuesFinder", control.getValuesFinderClass());
159             }
160             if (control.getBusinessObjectClass() != null) {
161                 controlMap.set("businessObject", control.getBusinessObjectClass());
162             }
163             if (StringUtils.isNotEmpty(control.getKeyAttribute())) {
164                 controlMap.set("keyAttribute", control.getKeyAttribute());
165             }
166             if (StringUtils.isNotEmpty(control.getLabelAttribute())) {
167                 controlMap.set("labelAttribute", control.getLabelAttribute());
168             }
169             if (control.getIncludeKeyInLabel() != null) {
170                 controlMap.set("includeKeyInLabel", control.getIncludeKeyInLabel().toString());
171             }
172         }
173         else if (control.isSelect()) {
174             controlMap.set("select", "true");
175             if (control.getValuesFinderClass() != null) {
176                 controlMap.set("valuesFinder", control.getValuesFinderClass());
177             }
178             if (control.getBusinessObjectClass() != null) {
179                 controlMap.set("businessObject", control.getBusinessObjectClass());
180             }
181             if (StringUtils.isNotEmpty(control.getKeyAttribute())) {
182                 controlMap.set("keyAttribute", control.getKeyAttribute());
183             }
184             if (StringUtils.isNotEmpty(control.getLabelAttribute())) {
185                 controlMap.set("labelAttribute", control.getLabelAttribute());
186             }
187             if (control.getIncludeBlankRow() != null) {
188                 controlMap.set("includeBlankRow", control.getIncludeBlankRow().toString());
189             }
190             if (control.getIncludeKeyInLabel() != null) {
191                 controlMap.set("includeKeyInLabel", control.getIncludeKeyInLabel().toString());
192             }
193         }
194         else if (control.isMultiselect()) {
195             controlMap.set("multiselect", "true");
196             if (control.getValuesFinderClass() != null) {
197                 controlMap.set("valuesFinder", control.getValuesFinderClass());
198             }
199             if (control.getBusinessObjectClass() != null) {
200                 controlMap.set("businessObject", control.getBusinessObjectClass());
201             }
202             if (StringUtils.isNotEmpty(control.getKeyAttribute())) {
203                 controlMap.set("keyAttribute", control.getKeyAttribute());
204             }
205             if (StringUtils.isNotEmpty(control.getLabelAttribute())) {
206                 controlMap.set("labelAttribute", control.getLabelAttribute());
207             }
208             if (control.getIncludeKeyInLabel() != null) {
209                 controlMap.set("includeKeyInLabel", control.getIncludeKeyInLabel().toString());
210             }
211             if (control.getSize() != null) {
212             	controlMap.set("size", control.getSize().toString());
213             }
214         }
215         else if (control.isText()) {
216             controlMap.set("text", "true");
217             if (control.getSize() != null) {
218                 controlMap.set("size", control.getSize().toString());
219             }
220             controlMap.set("datePicker", Boolean.valueOf(control.isDatePicker()).toString());
221             controlMap.set("ranged", Boolean.valueOf(control.isRanged()).toString());
222         }
223         else if (control.isTextarea()) {
224             controlMap.set("textarea", "true");
225             controlMap.set("rows", control.getRows().toString());
226             controlMap.set("cols", control.getCols().toString());
227             controlMap.set("expandedTextArea", Boolean.valueOf(control.isExpandedTextArea()).toString());
228         }
229         else if (control.isCurrency()) {
230             controlMap.set("currency", "true");
231             if (control.getSize() != null) {
232                 controlMap.set("size", control.getSize().toString());
233             }
234             controlMap.set("formattedMaxLength", ((CurrencyControlDefinition) control).getFormattedMaxLength().toString());
235         }
236         else if (control.isLookupHidden()) {
237             controlMap.set("lookupHidden", "true");
238         }
239         else if (control.isLookupReadonly()) {
240             controlMap.set("lookupReadonly", "true");
241         }else if (control.isButton()) {
242             controlMap.set("button", "true");
243             if (StringUtils.isNotEmpty(((ButtonControlDefinition) control).getImageSrc())) {
244             	controlMap.set("imageSrc", ((ButtonControlDefinition) control).getImageSrc());
245             }
246             if (StringUtils.isNotEmpty(((ButtonControlDefinition) control).getStyleClass())) {
247             	controlMap.set("styleClass", ((ButtonControlDefinition) control).getStyleClass() );
248             }
249         }else if (control.isLink()) {
250             controlMap.set("link", "true");
251             if (StringUtils.isNotEmpty(((LinkControlDefinition) control).getTarget())) {
252             	controlMap.set("target", ((LinkControlDefinition) control).getTarget());
253             }
254             if (StringUtils.isNotEmpty(((LinkControlDefinition) control).getStyleClass())) {
255             	controlMap.set("styleClass", ((LinkControlDefinition) control).getStyleClass() );
256             }
257             if (StringUtils.isNotEmpty(((LinkControlDefinition) control).getHrefText())) {
258             	controlMap.set("hrefText", ((LinkControlDefinition) control).getHrefText());
259             }
260         }
261 
262         return controlMap;
263     }
264 }