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 * @deprecated Only used by KNS classes, no replacement.
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 != 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}