001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.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/ecl2.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 */
016package org.kuali.rice.kns.datadictionary.exporter;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.kns.datadictionary.control.ButtonControlDefinition;
020import org.kuali.rice.kns.datadictionary.control.CurrencyControlDefinition;
021import org.kuali.rice.kns.datadictionary.control.LinkControlDefinition;
022import org.kuali.rice.krad.datadictionary.AttributeDefinition;
023import org.kuali.rice.krad.datadictionary.DataDictionaryEntryBase;
024import org.kuali.rice.krad.datadictionary.control.ControlDefinition;
025import org.kuali.rice.krad.datadictionary.exporter.ExportMap;
026
027import java.util.ArrayList;
028import java.util.Collections;
029import java.util.Comparator;
030import java.util.List;
031import java.util.Map;
032
033/**
034 * AttributesMapBuilder
035 *
036 *
037 */
038@Deprecated
039public class AttributesMapBuilder {
040
041    /**
042     * Default constructor
043     */
044    public AttributesMapBuilder() {
045    }
046
047
048    /**
049     * @param entry
050     * @return ExportMap containing the standard entries for the entry's AttributesDefinition
051     */
052    public ExportMap buildAttributesMap(DataDictionaryEntryBase entry) {
053        ExportMap attributesMap = new ExportMap("attributes");
054
055        for ( AttributeDefinition attribute : entry.getAttributes() ) {
056            attributesMap.set(buildAttributeMap(attribute, entry.getFullClassName()));
057        }
058
059        return attributesMap;
060    }
061
062    public ExportMap buildAttributeMap(AttributeDefinition attribute, String fullClassName) {
063        ExportMap attributeMap = new ExportMap(attribute.getName());
064
065        // simple properties
066        attributeMap.set("name", attribute.getName());
067        attributeMap.set("forceUppercase", attribute.getForceUppercase().toString());
068        attributeMap.set("label", attribute.getLabel());
069        attributeMap.set("shortLabel", attribute.getShortLabel());
070
071        //KULRICE-9144 remove maxLength non null assumption
072        Integer maxLength = attribute.getMaxLength();
073        if (maxLength != null) {
074            attributeMap.set("maxLength", maxLength.toString());
075        }
076        String exclusiveMin = attribute.getExclusiveMin();
077        if (exclusiveMin != null) {
078            attributeMap.set("exclusiveMin", exclusiveMin/*.toString()*/);
079        }
080        String exclusiveMax = attribute.getInclusiveMax();
081        if (exclusiveMax != null) {
082            attributeMap.set("exclusiveMax", exclusiveMax/*.toString()*/);
083        }
084
085        attributeMap.set("required", attribute.isRequired().toString());
086        if (attribute.getSummary() != null) {
087            attributeMap.set("summary", attribute.getSummary());
088        }
089        if (attribute.getDescription() != null) {
090            attributeMap.set("description", attribute.getDescription());
091        }
092        if (attribute.hasFormatterClass()) {
093            attributeMap.set("formatterClass", attribute.getFormatterClass());
094        }
095
096        // complex properties
097        if (attribute.hasValidationPattern()) {
098            attributeMap.set(attribute.getValidationPattern().buildExportMap("validationPattern"));
099        }
100
101        if(attribute.hasAttributeSecurity()){
102                attributeMap.set("attributeSecurityMask", String.valueOf(attribute.getAttributeSecurity().isMask()));
103                attributeMap.set("attributeSecurityPartialMask", String.valueOf(attribute.getAttributeSecurity().isPartialMask()));
104                attributeMap.set("attributeSecurityHide", String.valueOf(attribute.getAttributeSecurity().isHide()));
105                attributeMap.set("attributeSecurityReadOnly", String.valueOf(attribute.getAttributeSecurity().isReadOnly()));
106        
107                // TODO: consider whether to export class names from the attribute security
108        }
109
110        attributeMap.set(buildControlMap(attribute));
111        if (attribute.getOptionsFinder() != null) {
112            attributeMap.set(buildKeyLabelMap(attribute));
113        }
114        if (StringUtils.isNotBlank(fullClassName)) {
115            attributeMap.set("fullClassName", fullClassName);
116        }
117
118        return attributeMap;
119    }
120
121    private ExportMap buildKeyLabelMap(AttributeDefinition attribute) {
122
123        ExportMap keyLabelMap = new ExportMap("keyLabelMap");
124
125        List<Map.Entry<String, String>> keyLabelList = new ArrayList<Map.Entry<String, String>>(attribute.getOptionsFinder().getKeyLabelMap().entrySet());
126        Collections.sort(keyLabelList, new Comparator<Map.Entry<String, String>>() {
127            @Override
128            public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
129                 return o1.getValue().compareTo(o2.getValue());
130            }
131        });
132        for (Map.Entry<String, String> entry : keyLabelList) {
133            keyLabelMap.set(entry.getKey(), entry.getValue());
134        }
135        return keyLabelMap;
136    }
137
138    private ExportMap buildControlMap(AttributeDefinition attribute) {
139        ControlDefinition control = attribute.getControl();
140        ExportMap controlMap = new ExportMap("control");
141
142        if (control.isCheckbox()) {
143            controlMap.set("checkbox", "true");
144        }
145        else if (control.isHidden()) {
146            controlMap.set("hidden", "true");
147        }
148        else if (control.isKualiUser()) {
149            controlMap.set("kualiUser", "true");
150        }
151        else if (control.isRadio()) {
152            controlMap.set("radio", "true");
153            if (control.getValuesFinderClass() != null) {
154                controlMap.set("valuesFinder", control.getValuesFinderClass());
155            }
156            if (control.getBusinessObjectClass() != null) {
157                controlMap.set("businessObject", control.getBusinessObjectClass());
158            }
159            if (StringUtils.isNotEmpty(control.getKeyAttribute())) {
160                controlMap.set("keyAttribute", control.getKeyAttribute());
161            }
162            if (StringUtils.isNotEmpty(control.getLabelAttribute())) {
163                controlMap.set("labelAttribute", control.getLabelAttribute());
164            }
165            if (control.getIncludeKeyInLabel() != null) {
166                controlMap.set("includeKeyInLabel", control.getIncludeKeyInLabel().toString());
167            }
168        }
169        else if (control.isSelect()) {
170            controlMap.set("select", "true");
171            if (control.getValuesFinderClass() != null) {
172                controlMap.set("valuesFinder", control.getValuesFinderClass());
173            }
174            if (control.getBusinessObjectClass() != null) {
175                controlMap.set("businessObject", control.getBusinessObjectClass());
176            }
177            if (StringUtils.isNotEmpty(control.getKeyAttribute())) {
178                controlMap.set("keyAttribute", control.getKeyAttribute());
179            }
180            if (StringUtils.isNotEmpty(control.getLabelAttribute())) {
181                controlMap.set("labelAttribute", control.getLabelAttribute());
182            }
183            if (control.getIncludeBlankRow() != null) {
184                controlMap.set("includeBlankRow", control.getIncludeBlankRow().toString());
185            }
186            if (control.getIncludeKeyInLabel() != null) {
187                controlMap.set("includeKeyInLabel", control.getIncludeKeyInLabel().toString());
188            }
189        }
190        else if (control.isMultiselect()) {
191            controlMap.set("multiselect", "true");
192            if (control.getValuesFinderClass() != null) {
193                controlMap.set("valuesFinder", control.getValuesFinderClass());
194            }
195            if (control.getBusinessObjectClass() != null) {
196                controlMap.set("businessObject", control.getBusinessObjectClass());
197            }
198            if (StringUtils.isNotEmpty(control.getKeyAttribute())) {
199                controlMap.set("keyAttribute", control.getKeyAttribute());
200            }
201            if (StringUtils.isNotEmpty(control.getLabelAttribute())) {
202                controlMap.set("labelAttribute", control.getLabelAttribute());
203            }
204            if (control.getIncludeKeyInLabel() != null) {
205                controlMap.set("includeKeyInLabel", control.getIncludeKeyInLabel().toString());
206            }
207            if (control.getSize() != null) {
208                controlMap.set("size", control.getSize().toString());
209            }
210        }
211        else if (control.isText()) {
212            controlMap.set("text", "true");
213            if (control.getSize() != null) {
214                controlMap.set("size", control.getSize().toString());
215            }
216            controlMap.set("datePicker", Boolean.valueOf(control.isDatePicker()).toString());
217            controlMap.set("ranged", Boolean.valueOf(control.isRanged()).toString());
218        }
219        else if (control.isTextarea()) {
220            controlMap.set("textarea", "true");
221            controlMap.set("rows", control.getRows().toString());
222            controlMap.set("cols", control.getCols().toString());
223            controlMap.set("expandedTextArea", Boolean.valueOf(control.isExpandedTextArea()).toString());
224        }
225        else if (control.isCurrency()) {
226            controlMap.set("currency", "true");
227            if (control.getSize() != null) {
228                controlMap.set("size", control.getSize().toString());
229            }
230            controlMap.set("formattedMaxLength", ((CurrencyControlDefinition) control).getFormattedMaxLength().toString());
231        }
232        else if (control.isLookupHidden()) {
233            controlMap.set("lookupHidden", "true");
234        }
235        else if (control.isLookupReadonly()) {
236            controlMap.set("lookupReadonly", "true");
237        }else if (control.isButton()) {
238            controlMap.set("button", "true");
239            if (StringUtils.isNotEmpty(((ButtonControlDefinition) control).getImageSrc())) {
240                controlMap.set("imageSrc", ((ButtonControlDefinition) control).getImageSrc());
241            }
242            if (StringUtils.isNotEmpty(((ButtonControlDefinition) control).getStyleClass())) {
243                controlMap.set("styleClass", ((ButtonControlDefinition) control).getStyleClass() );
244            }
245        }else if (control.isLink()) {
246            controlMap.set("link", "true");
247            if (StringUtils.isNotEmpty(((LinkControlDefinition) control).getTarget())) {
248                controlMap.set("target", ((LinkControlDefinition) control).getTarget());
249            }
250            if (StringUtils.isNotEmpty(((LinkControlDefinition) control).getStyleClass())) {
251                controlMap.set("styleClass", ((LinkControlDefinition) control).getStyleClass() );
252            }
253            if (StringUtils.isNotEmpty(((LinkControlDefinition) control).getHrefText())) {
254                controlMap.set("hrefText", ((LinkControlDefinition) control).getHrefText());
255            }
256        }
257
258        return controlMap;
259    }
260}