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