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.krms.impl.ui;
017
018import org.apache.commons.collections.CollectionUtils;
019import org.kuali.rice.krad.maintenance.MaintenanceDocument;
020import org.kuali.rice.krad.maintenance.MaintainableImpl;
021import org.kuali.rice.krad.service.BusinessObjectService;
022import org.kuali.rice.krad.service.KRADServiceLocator;
023import org.kuali.rice.krad.uif.container.CollectionGroup;
024import org.kuali.rice.krad.uif.view.View;
025import org.kuali.rice.krad.util.KRADConstants;
026import org.kuali.rice.krms.impl.repository.ContextBo;
027import org.kuali.rice.krms.impl.repository.ContextValidTermBo;
028import org.kuali.rice.krms.impl.repository.TermSpecificationBo;
029
030import java.util.Collection;
031import java.util.Collections;
032import java.util.Map;
033
034/**
035 * {@link org.kuali.rice.krad.maintenance.Maintainable} for the {@link AgendaEditor}
036 *
037 * @author Kuali Rice Team (rice.collab@kuali.org)
038 *
039 */
040public class TermSpecificationMaintainable extends MaintainableImpl {
041        
042        private static final long serialVersionUID = 1L;
043
044    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(TermSpecificationMaintainable.class);
045
046        /**
047         * @return the boService
048         */
049        public BusinessObjectService getBoService() {
050                return KRADServiceLocator.getBusinessObjectService();
051        }
052
053    @Override
054    public Object retrieveObjectForEditOrCopy(MaintenanceDocument document, Map<String, String> dataObjectKeys) {
055
056        TermSpecificationBo termSpecificationBo = (TermSpecificationBo) super.retrieveObjectForEditOrCopy(document,
057                dataObjectKeys);
058
059        if (KRADConstants.MAINTENANCE_COPY_ACTION.equals(getMaintenanceAction())) {
060            document.getDocumentHeader().setDocumentDescription("New Term Specification Document");
061        }
062
063        return termSpecificationBo;
064    }
065
066    /**
067     * Add the Term Specification's Context to the given termSpecificationBo.  Note that there is no check for the Context
068     * already having been added to the Term Specification.
069     * @param termSpecificationBo with
070     */
071    private void findContexts(TermSpecificationBo termSpecificationBo) {
072        Collection<ContextValidTermBo> validContextMappings =
073            getBoService().findMatching(ContextValidTermBo.class,
074                    Collections.singletonMap("termSpecificationId", termSpecificationBo.getId()));
075
076        if (!CollectionUtils.isEmpty(validContextMappings)) for (ContextValidTermBo validContextMapping : validContextMappings) {
077            ContextBo context = getBoService().findBySinglePrimaryKey(ContextBo.class, validContextMapping.getContextId());
078            termSpecificationBo.getContexts().add(context);
079        }
080    }
081
082    /**
083         * {@inheritDoc}
084         */
085        @Override
086        public void processAfterNew(MaintenanceDocument document,
087                Map<String, String[]> requestParameters) {
088
089                super.processAfterNew(document, requestParameters);
090        document.getDocumentHeader().setDocumentDescription("New Term Specification Document");
091
092        }
093
094    /**
095     * {@inheritDoc}
096     */
097    @Override
098    public void processAfterEdit(MaintenanceDocument document, Map<String, String[]> requestParameters) {
099
100
101        super.processAfterEdit(document,
102                requestParameters);
103
104        document.getDocumentHeader().setDocumentDescription("Edited Term Specification Document");
105    }
106
107    @Override
108    public void saveDataObject() {
109        TermSpecificationBo termSpec = (TermSpecificationBo) getDataObject();
110
111        super.saveDataObject();    // save it, it should get an id assigned
112
113        if (termSpec.getId() != null) {
114            // clear all context valid term mappings
115            getBoService().deleteMatching(ContextValidTermBo.class,
116                    Collections.singletonMap("termSpecificationId", termSpec.getId()));
117
118            // add a new mapping for each context in the collection
119            if (!CollectionUtils.isEmpty(termSpec.getContexts())) for (ContextBo context : termSpec.getContexts()) {
120                ContextValidTermBo contextValidTerm = new ContextValidTermBo();
121                contextValidTerm.setContextId(context.getId());
122                contextValidTerm.setTermSpecificationId(termSpec.getId());
123                getBoService().save(contextValidTerm);
124            }
125        }
126
127    }
128
129    @Override
130    public Class getDataObjectClass() {
131        return TermSpecificationBo.class;
132    }
133
134    @Override
135    protected void processBeforeAddLine(View view, CollectionGroup collectionGroup, Object model, Object addLine) {
136        super.processBeforeAddLine(view, collectionGroup, model, addLine);
137    }
138
139
140}