001    /**
002     * Copyright 2005-2012 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     */
016    package org.kuali.rice.kns.datadictionary.exporter;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.kns.datadictionary.control.ButtonControlDefinition;
020    import org.kuali.rice.kns.datadictionary.control.CurrencyControlDefinition;
021    import org.kuali.rice.kns.datadictionary.control.LinkControlDefinition;
022    import org.kuali.rice.krad.datadictionary.AttributeDefinition;
023    import org.kuali.rice.krad.datadictionary.DataDictionaryEntryBase;
024    import org.kuali.rice.krad.datadictionary.control.ControlDefinition;
025    import org.kuali.rice.krad.datadictionary.exporter.ExportMap;
026    
027    import java.util.ArrayList;
028    import java.util.Collections;
029    import java.util.Comparator;
030    import java.util.List;
031    import java.util.Map;
032    
033    /**
034     * AttributesMapBuilder
035     *
036     *
037     */
038    @Deprecated
039    public 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            attributeMap.set("maxLength", attribute.getMaxLength().toString());
072            String exclusiveMin = attribute.getExclusiveMin();
073            if (exclusiveMin != null) {
074                attributeMap.set("exclusiveMin", exclusiveMin/*.toString()*/);
075            }
076            String exclusiveMax = attribute.getInclusiveMax();
077            if (exclusiveMax != null) {
078                attributeMap.set("exclusiveMax", exclusiveMax/*.toString()*/);
079            }
080    
081            attributeMap.set("required", attribute.isRequired().toString());
082            if (attribute.getSummary() != null) {
083                attributeMap.set("summary", attribute.getSummary());
084            }
085            if (attribute.getDescription() != null) {
086                attributeMap.set("description", attribute.getDescription());
087            }
088            if (attribute.hasFormatterClass()) {
089                attributeMap.set("formatterClass", attribute.getFormatterClass());
090            }
091    
092            // complex properties
093            if (attribute.hasValidationPattern()) {
094                attributeMap.set(attribute.getValidationPattern().buildExportMap("validationPattern"));
095            }
096    
097            if(attribute.hasAttributeSecurity()){
098                    attributeMap.set("attributeSecurityMask", String.valueOf(attribute.getAttributeSecurity().isMask()));
099                    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                if (control.getSize() != null) {
210                    controlMap.set("size", control.getSize().toString());
211                }
212                controlMap.set("datePicker", Boolean.valueOf(control.isDatePicker()).toString());
213                controlMap.set("ranged", Boolean.valueOf(control.isRanged()).toString());
214            }
215            else if (control.isTextarea()) {
216                controlMap.set("textarea", "true");
217                controlMap.set("rows", control.getRows().toString());
218                controlMap.set("cols", control.getCols().toString());
219                controlMap.set("expandedTextArea", Boolean.valueOf(control.isExpandedTextArea()).toString());
220            }
221            else if (control.isCurrency()) {
222                controlMap.set("currency", "true");
223                if (control.getSize() != null) {
224                    controlMap.set("size", control.getSize().toString());
225                }
226                controlMap.set("formattedMaxLength", ((CurrencyControlDefinition) control).getFormattedMaxLength().toString());
227            }
228            else if (control.isLookupHidden()) {
229                controlMap.set("lookupHidden", "true");
230            }
231            else if (control.isLookupReadonly()) {
232                controlMap.set("lookupReadonly", "true");
233            }else if (control.isButton()) {
234                controlMap.set("button", "true");
235                if (StringUtils.isNotEmpty(((ButtonControlDefinition) control).getImageSrc())) {
236                    controlMap.set("imageSrc", ((ButtonControlDefinition) control).getImageSrc());
237                }
238                if (StringUtils.isNotEmpty(((ButtonControlDefinition) control).getStyleClass())) {
239                    controlMap.set("styleClass", ((ButtonControlDefinition) control).getStyleClass() );
240                }
241            }else if (control.isLink()) {
242                controlMap.set("link", "true");
243                if (StringUtils.isNotEmpty(((LinkControlDefinition) control).getTarget())) {
244                    controlMap.set("target", ((LinkControlDefinition) control).getTarget());
245                }
246                if (StringUtils.isNotEmpty(((LinkControlDefinition) control).getStyleClass())) {
247                    controlMap.set("styleClass", ((LinkControlDefinition) control).getStyleClass() );
248                }
249                if (StringUtils.isNotEmpty(((LinkControlDefinition) control).getHrefText())) {
250                    controlMap.set("hrefText", ((LinkControlDefinition) control).getHrefText());
251                }
252            }
253    
254            return controlMap;
255        }
256    }