View Javadoc

1   /**
2    * Copyright 2005-2012 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          attributeMap.set("maxLength", attribute.getMaxLength().toString());
72          String exclusiveMin = attribute.getExclusiveMin();
73          if (exclusiveMin != null) {
74              attributeMap.set("exclusiveMin", exclusiveMin/*.toString()*/);
75          }
76          String exclusiveMax = attribute.getInclusiveMax();
77          if (exclusiveMax != null) {
78              attributeMap.set("exclusiveMax", exclusiveMax/*.toString()*/);
79          }
80  
81          attributeMap.set("required", attribute.isRequired().toString());
82          if (attribute.getSummary() != null) {
83              attributeMap.set("summary", attribute.getSummary());
84          }
85          if (attribute.getDescription() != null) {
86              attributeMap.set("description", attribute.getDescription());
87          }
88          if (attribute.hasFormatterClass()) {
89              attributeMap.set("formatterClass", attribute.getFormatterClass());
90          }
91  
92          // complex properties
93          if (attribute.hasValidationPattern()) {
94              attributeMap.set(attribute.getValidationPattern().buildExportMap("validationPattern"));
95          }
96  
97          if(attribute.hasAttributeSecurity()){
98          	attributeMap.set("attributeSecurityMask", String.valueOf(attribute.getAttributeSecurity().isMask()));
99          	attributeMap.set("attributeSecurityPartialMask", String.valueOf(attribute.getAttributeSecurity().isPartialMask()));
100         	attributeMap.set("attributeSecurityHide", String.valueOf(attribute.getAttributeSecurity().isHide()));
101         	attributeMap.set("attributeSecurityReadOnly", String.valueOf(attribute.getAttributeSecurity().isReadOnly()));
102   	
103         	// TODO: consider whether to export class names from the attribute security
104         }
105 
106         attributeMap.set(buildControlMap(attribute));
107         if (attribute.getOptionsFinder() != null) {
108             attributeMap.set(buildKeyLabelMap(attribute));
109         }
110         if (StringUtils.isNotBlank(fullClassName)) {
111             attributeMap.set("fullClassName", fullClassName);
112         }
113 
114         return attributeMap;
115     }
116 
117     private ExportMap buildKeyLabelMap(AttributeDefinition attribute) {
118 
119         ExportMap keyLabelMap = new ExportMap("keyLabelMap");
120 
121         List<Map.Entry<String, String>> keyLabelList = new ArrayList<Map.Entry<String, String>>(attribute.getOptionsFinder().getKeyLabelMap().entrySet());
122         Collections.sort(keyLabelList, new Comparator<Map.Entry<String, String>>() {
123             @Override
124             public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
125                  return o1.getValue().compareTo(o2.getValue());
126             }
127         });
128         for (Map.Entry<String, String> entry : keyLabelList) {
129             keyLabelMap.set(entry.getKey(), entry.getValue());
130         }
131         return keyLabelMap;
132     }
133 
134     private ExportMap buildControlMap(AttributeDefinition attribute) {
135         ControlDefinition control = attribute.getControl();
136         ExportMap controlMap = new ExportMap("control");
137 
138         if (control.isCheckbox()) {
139             controlMap.set("checkbox", "true");
140         }
141         else if (control.isHidden()) {
142             controlMap.set("hidden", "true");
143         }
144         else if (control.isKualiUser()) {
145             controlMap.set("kualiUser", "true");
146         }
147         else if (control.isRadio()) {
148             controlMap.set("radio", "true");
149             if (control.getValuesFinderClass() != null) {
150                 controlMap.set("valuesFinder", control.getValuesFinderClass());
151             }
152             if (control.getBusinessObjectClass() != null) {
153                 controlMap.set("businessObject", control.getBusinessObjectClass());
154             }
155             if (StringUtils.isNotEmpty(control.getKeyAttribute())) {
156                 controlMap.set("keyAttribute", control.getKeyAttribute());
157             }
158             if (StringUtils.isNotEmpty(control.getLabelAttribute())) {
159                 controlMap.set("labelAttribute", control.getLabelAttribute());
160             }
161             if (control.getIncludeKeyInLabel() != null) {
162                 controlMap.set("includeKeyInLabel", control.getIncludeKeyInLabel().toString());
163             }
164         }
165         else if (control.isSelect()) {
166             controlMap.set("select", "true");
167             if (control.getValuesFinderClass() != null) {
168                 controlMap.set("valuesFinder", control.getValuesFinderClass());
169             }
170             if (control.getBusinessObjectClass() != null) {
171                 controlMap.set("businessObject", control.getBusinessObjectClass());
172             }
173             if (StringUtils.isNotEmpty(control.getKeyAttribute())) {
174                 controlMap.set("keyAttribute", control.getKeyAttribute());
175             }
176             if (StringUtils.isNotEmpty(control.getLabelAttribute())) {
177                 controlMap.set("labelAttribute", control.getLabelAttribute());
178             }
179             if (control.getIncludeBlankRow() != null) {
180                 controlMap.set("includeBlankRow", control.getIncludeBlankRow().toString());
181             }
182             if (control.getIncludeKeyInLabel() != null) {
183                 controlMap.set("includeKeyInLabel", control.getIncludeKeyInLabel().toString());
184             }
185         }
186         else if (control.isMultiselect()) {
187             controlMap.set("multiselect", "true");
188             if (control.getValuesFinderClass() != null) {
189                 controlMap.set("valuesFinder", control.getValuesFinderClass());
190             }
191             if (control.getBusinessObjectClass() != null) {
192                 controlMap.set("businessObject", control.getBusinessObjectClass());
193             }
194             if (StringUtils.isNotEmpty(control.getKeyAttribute())) {
195                 controlMap.set("keyAttribute", control.getKeyAttribute());
196             }
197             if (StringUtils.isNotEmpty(control.getLabelAttribute())) {
198                 controlMap.set("labelAttribute", control.getLabelAttribute());
199             }
200             if (control.getIncludeKeyInLabel() != null) {
201                 controlMap.set("includeKeyInLabel", control.getIncludeKeyInLabel().toString());
202             }
203             if (control.getSize() != null) {
204             	controlMap.set("size", control.getSize().toString());
205             }
206         }
207         else if (control.isText()) {
208             controlMap.set("text", "true");
209             controlMap.set("size", control.getSize().toString());
210             controlMap.set("datePicker", Boolean.valueOf(control.isDatePicker()).toString());
211             controlMap.set("ranged", Boolean.valueOf(control.isRanged()).toString());
212         }
213         else if (control.isTextarea()) {
214             controlMap.set("textarea", "true");
215             controlMap.set("rows", control.getRows().toString());
216             controlMap.set("cols", control.getCols().toString());
217             controlMap.set("expandedTextArea", Boolean.valueOf(control.isExpandedTextArea()).toString());
218         }
219         else if (control.isCurrency()) {
220             controlMap.set("currency", "true");
221             controlMap.set("size", control.getSize().toString());
222             controlMap.set("formattedMaxLength", ((CurrencyControlDefinition) control).getFormattedMaxLength().toString());
223         }
224         else if (control.isLookupHidden()) {
225             controlMap.set("lookupHidden", "true");
226         }
227         else if (control.isLookupReadonly()) {
228             controlMap.set("lookupReadonly", "true");
229         }else if (control.isButton()) {
230             controlMap.set("button", "true");
231             if (StringUtils.isNotEmpty(((ButtonControlDefinition) control).getImageSrc())) {
232             	controlMap.set("imageSrc", ((ButtonControlDefinition) control).getImageSrc());
233             }
234             if (StringUtils.isNotEmpty(((ButtonControlDefinition) control).getStyleClass())) {
235             	controlMap.set("styleClass", ((ButtonControlDefinition) control).getStyleClass() );
236             }
237         }else if (control.isLink()) {
238             controlMap.set("link", "true");
239             if (StringUtils.isNotEmpty(((LinkControlDefinition) control).getTarget())) {
240             	controlMap.set("target", ((LinkControlDefinition) control).getTarget());
241             }
242             if (StringUtils.isNotEmpty(((LinkControlDefinition) control).getStyleClass())) {
243             	controlMap.set("styleClass", ((LinkControlDefinition) control).getStyleClass() );
244             }
245             if (StringUtils.isNotEmpty(((LinkControlDefinition) control).getHrefText())) {
246             	controlMap.set("hrefText", ((LinkControlDefinition) control).getHrefText());
247             }
248         }
249 
250         return controlMap;
251     }
252 }