001    /**
002     * Copyright 2005-2013 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.krms.impl.ui;
017    
018    import org.apache.commons.collections.CollectionUtils;
019    import org.kuali.rice.krad.maintenance.MaintenanceDocument;
020    import org.kuali.rice.krad.maintenance.MaintainableImpl;
021    import org.kuali.rice.krad.service.BusinessObjectService;
022    import org.kuali.rice.krad.service.KRADServiceLocator;
023    import org.kuali.rice.krad.util.KRADConstants;
024    import org.kuali.rice.krms.api.repository.context.ContextDefinition;
025    import org.kuali.rice.krms.impl.repository.ContextBo;
026    import org.kuali.rice.krms.impl.repository.ContextValidTermBo;
027    import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
028    import org.kuali.rice.krms.impl.repository.TermSpecificationBo;
029    
030    import java.util.ArrayList;
031    import java.util.Collection;
032    import java.util.Collections;
033    import java.util.Map;
034    
035    /**
036     * {@link org.kuali.rice.krad.maintenance.Maintainable} for the {@link AgendaEditor}
037     *
038     * @author Kuali Rice Team (rice.collab@kuali.org)
039     *
040     */
041    public class TermSpecificationMaintainable extends MaintainableImpl {
042            
043            private static final long serialVersionUID = 1L;
044    
045        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(TermSpecificationMaintainable.class);
046    
047            /**
048             * @return the boService
049             */
050            public BusinessObjectService getBoService() {
051                    return KRADServiceLocator.getBusinessObjectService();
052            }
053    
054        @Override
055        public Object retrieveObjectForEditOrCopy(MaintenanceDocument document, Map<String, String> dataObjectKeys) {
056    
057            TermSpecificationBo termSpecificationBo = (TermSpecificationBo) super.retrieveObjectForEditOrCopy(document,
058                    dataObjectKeys);
059    
060            if (!CollectionUtils.isEmpty(termSpecificationBo.getContextIds())) {
061                for (String contextId : termSpecificationBo.getContextIds()) {
062                    ContextDefinition context =
063                            KrmsRepositoryServiceLocator.getContextBoService().getContextByContextId(contextId);
064                    if (context != null) {
065                        termSpecificationBo.getContexts().add(ContextBo.from(context));
066                    }
067                }
068            }
069    
070            if (KRADConstants.MAINTENANCE_COPY_ACTION.equals(getMaintenanceAction())) {
071                document.getDocumentHeader().setDocumentDescription("New Term Specification Document");
072            }
073    
074            return termSpecificationBo;
075        }
076    
077        /**
078         * Add the Term Specification's Context to the given termSpecificationBo.  Note that there is no check for the Context
079         * already having been added to the Term Specification.
080         * @param termSpecificationBo with
081         */
082        private void findContexts(TermSpecificationBo termSpecificationBo) {
083            Collection<ContextValidTermBo> validContextMappings =
084                getBoService().findMatching(ContextValidTermBo.class,
085                        Collections.singletonMap("termSpecificationId", termSpecificationBo.getId()));
086    
087            if (!CollectionUtils.isEmpty(validContextMappings)) for (ContextValidTermBo validContextMapping : validContextMappings) {
088                termSpecificationBo.getContextIds().add(validContextMapping.getContextId());
089            }
090        }
091    
092        /**
093             * {@inheritDoc}
094             */
095            @Override
096            public void processAfterNew(MaintenanceDocument document,
097                    Map<String, String[]> requestParameters) {
098    
099                    super.processAfterNew(document, requestParameters);
100            document.getDocumentHeader().setDocumentDescription("New Term Specification Document");
101    
102            }
103    
104        /**
105         * {@inheritDoc}
106         */
107        @Override
108        public void processAfterEdit(MaintenanceDocument document, Map<String, String[]> requestParameters) {
109            super.processAfterEdit(document, requestParameters);
110            copyContextsOldToNewBo(document);
111            document.getDocumentHeader().setDocumentDescription("Edited Term Specification Document");
112        }
113    
114        @Override
115        public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> requestParameters) {
116            super.processAfterCopy(document, requestParameters);
117            copyContextsOldToNewBo(document);
118        }
119    
120        /**
121         * Copy the contexts from the old TermSpecificationBo to the newTermSpecificationBo of the maintenance document.
122         * <p>
123         * Since the contexts is a transient field it doesn't get copied by the deepCopy in
124         * MaintenanceDocumentServiceImpl.setupMaintenanceObject, we manually need to copy the values over.
125         * For performance reasons a shallow copy is done since the ContextBo themselves are never changed.
126         * </p>
127         * @param document that contains the old and new TermSpecificationBos
128         */
129        private void copyContextsOldToNewBo(MaintenanceDocument document) {
130            TermSpecificationBo oldTermSpec = (TermSpecificationBo) document.getOldMaintainableObject().getDataObject();
131            TermSpecificationBo newTermSpec = (TermSpecificationBo) document.getNewMaintainableObject().getDataObject();
132            newTermSpec.setContexts(new ArrayList<ContextBo>());
133            for (ContextBo contextBo : oldTermSpec.getContexts()) {
134                newTermSpec.getContexts().add(contextBo);
135            }
136        }
137    
138        /**
139         * Store contexts in contextIds (needed for serialization)
140         *
141         * @see org.kuali.rice.krad.maintenance.Maintainable#prepareForSave
142         */
143        @Override
144        public void prepareForSave() {
145            super.prepareForSave();
146    
147            TermSpecificationBo termSpec = (TermSpecificationBo) getDataObject();
148            termSpec.setContextIds(new ArrayList<String>());
149            for (ContextBo context : termSpec.getContexts()) {
150                termSpec.getContextIds().add(context.getId());
151            }
152        }
153    
154        @Override
155        public void saveDataObject() {
156            TermSpecificationBo termSpec = (TermSpecificationBo) getDataObject();
157    
158            super.saveDataObject();    // save it, it should get an id assigned
159    
160            if (termSpec.getId() != null) {
161                // clear all context valid term mappings
162                getBoService().deleteMatching(ContextValidTermBo.class,
163                        Collections.singletonMap("termSpecificationId", termSpec.getId()));
164    
165                // add a new mapping for each context in the collection
166                for (String contextId : termSpec.getContextIds()) {
167                    ContextValidTermBo contextValidTerm = new ContextValidTermBo();
168                    contextValidTerm.setContextId(contextId);
169                    contextValidTerm.setTermSpecificationId(termSpec.getId());
170                    getBoService().save(contextValidTerm);
171                }
172            }
173        }
174    
175        @Override
176        public Class getDataObjectClass() {
177            return TermSpecificationBo.class;
178        }
179    
180        /**
181         * Recreate the contexts from the contextIDs (needed due to serialization)
182         *
183         * @see org.kuali.rice.krad.maintenance.Maintainable#processAfterRetrieve
184         */
185        @Override
186        public void processAfterRetrieve() {
187            super.processAfterRetrieve();
188    
189            TermSpecificationBo termSpec = (TermSpecificationBo) getDataObject();
190            termSpec.setContexts(new ArrayList<ContextBo>());
191            for (String contextId : termSpec.getContextIds()) {
192                ContextDefinition context = KrmsRepositoryServiceLocator.getContextBoService().getContextByContextId(contextId);
193                termSpec.getContexts().add(ContextBo.from(context));
194            }
195        }
196    }