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        if (attribute.isRequired() != null) {
086            attributeMap.set("required", attribute.isRequired().toString());
087        } else {
088            attributeMap.set("required", "false");
089        }
090        if (attribute.getSummary() != null) {
091            attributeMap.set("summary", attribute.getSummary());
092        }
093        if (attribute.getDescription() != null) {
094            attributeMap.set("description", attribute.getDescription());
095        }
096        if (attribute.hasFormatterClass()) {
097            attributeMap.set("formatterClass", attribute.getFormatterClass());
098        }
099/**
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}