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