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 java.util.Iterator;
019
020import org.apache.commons.lang.StringUtils;
021import org.kuali.rice.kew.api.doctype.DocumentType;
022import org.kuali.rice.kns.datadictionary.MaintainableCollectionDefinition;
023import org.kuali.rice.kns.datadictionary.MaintainableFieldDefinition;
024import org.kuali.rice.kns.datadictionary.MaintainableItemDefinition;
025import org.kuali.rice.kns.datadictionary.MaintainableSectionDefinition;
026import org.kuali.rice.kns.datadictionary.MaintainableSubSectionHeaderDefinition;
027import org.kuali.rice.kns.datadictionary.MaintenanceDocumentEntry;
028import org.kuali.rice.kns.service.DocumentHelperService;
029import org.kuali.rice.kns.service.KNSServiceLocator;
030import org.kuali.rice.krad.datadictionary.exporter.ExportMap;
031import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
032
033/**
034 * MaintenanceDocumentEntryMapper
035 * 
036 * 
037 */
038@Deprecated
039public class MaintenanceDocumentEntryMapper extends DocumentEntryMapper {
040
041    /**
042     * Default constructor
043     */
044    public MaintenanceDocumentEntryMapper() {
045    }
046
047
048    /**
049     * @param entry
050     * @return Map containing a String- and Map-based representation of the given entry
051     */
052    public ExportMap mapEntry(MaintenanceDocumentEntry entry) {
053         // simple properties
054        ExportMap entryMap = new ExportMap(entry.getJstlKey());
055
056        Class businessRulesClass = entry.getBusinessRulesClass();
057        if (businessRulesClass != null) {
058            entryMap.set("businessRulesClass", businessRulesClass.getName());
059        }
060
061        entryMap.set("documentTypeName", entry.getDocumentTypeName());
062
063        DocumentType docType = getDocumentType(entry.getDocumentTypeName());
064        entryMap.set("label", docType.getLabel());
065
066        if (docType.getDescription() != null) {
067            entryMap.set("description", docType.getDescription());
068        }
069
070        DocumentHelperService documentHelperService = KNSServiceLocator.getDocumentHelperService();
071        entryMap.set("documentAuthorizerClass", documentHelperService.getDocumentAuthorizer(entry.getDocumentTypeName()).getClass().getName());
072        entryMap.set("documentPresentationControllerClass", documentHelperService.getDocumentPresentationController(entry.getDocumentTypeName()).getClass().getName());
073
074        entryMap.set("allowsNoteAttachments", Boolean.toString(entry.getAllowsNoteAttachments()));
075
076        entryMap.set("allowsNoteFYI", Boolean.toString(entry.getAllowsNoteFYI()));
077
078        if (entry.getAttachmentTypesValuesFinderClass() != null) {
079            entryMap.set("attachmentTypesValuesFinderClass", entry.getAttachmentTypesValuesFinderClass().getName());
080        }
081
082        entryMap.set("displayTopicFieldInNotes", Boolean.toString(entry.getDisplayTopicFieldInNotes()));
083
084        entryMap.set("usePessimisticLocking", Boolean.toString(entry.getUsePessimisticLocking()));
085        entryMap.set("useWorkflowPessimisticLocking", Boolean.toString(entry.getUseWorkflowPessimisticLocking()));
086        entryMap.set("sessionDocument", Boolean.toString(entry.isSessionDocument()));
087
088        entryMap.set(new AttributesMapBuilder().buildAttributesMap(entry));
089        entryMap.set(new CollectionsMapBuilder().buildCollectionsMap(entry));
090
091        // simple properties
092        entryMap.set("maintenanceDocument", "true");
093        entryMap.set("dataObjectClass", entry.getBusinessObjectClass().getName());
094        entryMap.set("maintainableClass", entry.getMaintainableClass().getName());
095
096        // complex properties
097        entryMap.set(buildMaintainableSectionsMap(entry));
098
099        return entryMap;
100    }
101
102    private ExportMap buildMaintainableSectionsMap(MaintenanceDocumentEntry entry) {
103        ExportMap maintainableSectionsMap = new ExportMap("maintainableSections");
104
105        int index = 0;
106        for (Iterator i = entry.getMaintainableSections().iterator(); i.hasNext();) {
107            MaintainableSectionDefinition section = (MaintainableSectionDefinition) i.next();
108
109            maintainableSectionsMap.set(buildMaintainableSectionMap(section, index++));
110        }
111
112        return maintainableSectionsMap;
113    }
114
115    private ExportMap buildMaintainableSectionMap(MaintainableSectionDefinition section, int index) {
116        ExportMap sectionMap = new ExportMap(Integer.toString(index));
117
118        sectionMap.set("index", Integer.toString(index));
119        sectionMap.set("title", section.getTitle());
120
121        sectionMap.set(buildMaintainableItemsMap(section));
122
123        return sectionMap;
124    }
125
126    private ExportMap buildMaintainableItemsMap(MaintainableSectionDefinition section) {
127        ExportMap itemsMap = new ExportMap("maintainableItems");
128
129        for (Iterator i = section.getMaintainableItems().iterator(); i.hasNext();) {
130            MaintainableItemDefinition item = (MaintainableItemDefinition) i.next();
131            itemsMap.set(buildMaintainableItemMap(item));
132        }
133
134        return itemsMap;
135    }
136
137    private ExportMap buildMaintainableItemMap(MaintainableItemDefinition item) {
138        ExportMap itemMap = new ExportMap(item.getName());
139
140        if (item instanceof MaintainableFieldDefinition) {
141            MaintainableFieldDefinition field = (MaintainableFieldDefinition) item;
142
143            itemMap.set("field", "true");
144            itemMap.set("name", field.getName());
145            itemMap.set("required", Boolean.toString(field.isRequired()));
146                        if (StringUtils.isNotBlank(field.getAlternateDisplayAttributeName())) {
147                                itemMap.set("alternateDisplayAttributeName", field.getAlternateDisplayAttributeName());
148                        }
149                        if (StringUtils.isNotBlank(field.getAdditionalDisplayAttributeName())) {
150                                itemMap.set("additionalDisplayAttributeName", field.getAdditionalDisplayAttributeName());
151                        }
152        }
153        else if (item instanceof MaintainableCollectionDefinition) {
154            MaintainableCollectionDefinition collection = (MaintainableCollectionDefinition) item;
155
156            itemMap.set("collection", "true");
157            itemMap.set("name", collection.getName());
158            itemMap.set("dataObjectClass", collection.getBusinessObjectClass().getName());
159        }
160        else if (item instanceof MaintainableSubSectionHeaderDefinition) {
161            MaintainableSubSectionHeaderDefinition subSectionHeader = (MaintainableSubSectionHeaderDefinition) item;
162            itemMap.set("name", subSectionHeader.getName());
163        }
164        else {
165            throw new IllegalStateException("unable to create itemMap for unknown MaintainableItem subclass '" + item.getClass().getName() + "'");
166        }
167
168        return itemMap;
169    }
170}