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.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
019    import org.kuali.rice.core.impl.cache.DistributedCacheManagerDecorator;
020    import org.kuali.rice.krad.maintenance.MaintainableImpl;
021    import org.kuali.rice.krad.maintenance.MaintenanceDocument;
022    import org.kuali.rice.krad.service.KRADServiceLocator;
023    import org.kuali.rice.krad.service.SequenceAccessorService;
024    import org.kuali.rice.krad.util.KRADConstants;
025    import org.kuali.rice.krms.api.KrmsConstants;
026    import org.kuali.rice.krms.api.repository.context.ContextDefinition;
027    import org.kuali.rice.krms.impl.repository.ContextBo;
028    
029    import java.util.Map;
030    
031    /**
032     * {@link org.kuali.rice.krad.maintenance.Maintainable} for the {@link ContextBo}
033     *
034     * @author Kuali Rice Team (rice.collab@kuali.org)
035     * */
036    public class ContextMaintainable extends MaintainableImpl {
037    
038        private transient SequenceAccessorService sequenceAccessorService;
039    
040        @Override
041        public void processAfterNew(MaintenanceDocument document, Map<String, String[]> requestParameters) {
042            ContextBo newContext = (ContextBo) document.getNewMaintainableObject().getDataObject();
043    
044            String nextId = getSequenceAccessorService().getNextAvailableSequenceNumber(KrmsMaintenanceConstants.Sequences.CONTEXT, ContextBo.class).toString();
045            newContext.setId(nextId);
046    
047            super.processAfterNew(document, requestParameters);    
048        }
049    
050        @Override
051        public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> requestParameters) {
052            ContextBo context = (ContextBo) document.getNewMaintainableObject().getDataObject();
053    
054            String nextId = getSequenceAccessorService().getNextAvailableSequenceNumber(KrmsMaintenanceConstants.Sequences.CONTEXT, ContextBo.class).toString();
055            context.setId(nextId);
056    
057            super.processAfterCopy(document,
058                    requestParameters);
059        }
060    
061        @Override
062        public void saveDataObject() {
063            super.saveDataObject();
064    
065            //flush context cache
066            DistributedCacheManagerDecorator distributedCacheManagerDecorator =
067                    GlobalResourceLoader.getService(KrmsConstants.KRMS_DISTRIBUTED_CACHE);
068            distributedCacheManagerDecorator.getCache(ContextDefinition.Cache.NAME).clear();
069        }
070    
071        @Override
072        public Object retrieveObjectForEditOrCopy(MaintenanceDocument document, Map<String, String> dataObjectKeys) {
073    
074            ContextBo contextBo = (ContextBo) super.retrieveObjectForEditOrCopy(document, dataObjectKeys);
075    
076            if (KRADConstants.MAINTENANCE_COPY_ACTION.equals(getMaintenanceAction())) {
077                document.getDocumentHeader().setDocumentDescription("New Context Document");
078    
079                contextBo = contextBo.copyContext(" Copy " + System.currentTimeMillis());
080            }
081    
082            return contextBo;
083        }
084    
085        /**
086         *  Returns the sequenceAssessorService
087         * @return {@link SequenceAccessorService}
088         */
089        private SequenceAccessorService getSequenceAccessorService() {
090            if ( sequenceAccessorService == null ) {
091                sequenceAccessorService = KRADServiceLocator.getSequenceAccessorService();
092            }
093            return sequenceAccessorService;
094        }
095    
096    }