001/**
002 * Copyright 2005-2017 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.ContextAttributeBo;
026import org.kuali.rice.krms.impl.repository.ContextBo;
027import org.kuali.rice.krms.impl.repository.RepositoryBoIncrementer;
028
029import 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 * */
036public class ContextMaintainable extends MaintainableImpl {
037
038    private static final RepositoryBoIncrementer contextIdIncrementer = new RepositoryBoIncrementer(ContextBo.CONTEXT_SEQ_NAME);
039
040    @Override
041    public void processAfterNew(MaintenanceDocument document, Map<String, String[]> requestParameters) {
042        ContextBo newContext = (ContextBo) document.getNewMaintainableObject().getDataObject();
043
044        newContext.setId(contextIdIncrementer.getNewId());
045
046        super.processAfterNew(document, requestParameters);    
047    }
048
049    @Override
050    public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> requestParameters) {
051        ContextBo context = (ContextBo) document.getNewMaintainableObject().getDataObject();
052
053        if (context.getId()== null) {
054            context.setId(contextIdIncrementer.getNewId());
055        }
056
057        if(context.getName() == null) {
058            context.setName(" Copy " + System.currentTimeMillis());
059        }
060
061        context.setAgendas(null);
062        for (ContextAttributeBo attributeBo : context.getAttributeBos()) {
063            attributeBo.setId(null);
064            attributeBo.setVersionNumber(null);
065        }
066
067        super.processAfterCopy(document,
068                requestParameters);
069    }
070
071    @Override
072    public void saveDataObject() {
073        super.saveDataObject();
074
075        //flush context cache
076        DistributedCacheManagerDecorator distributedCacheManagerDecorator =
077                GlobalResourceLoader.getService(KrmsConstants.KRMS_DISTRIBUTED_CACHE);
078        distributedCacheManagerDecorator.getCache(ContextDefinition.Cache.NAME).clear();
079    }
080
081    @Override
082    public Object retrieveObjectForEditOrCopy(MaintenanceDocument document, Map<String, String> dataObjectKeys) {
083
084        ContextBo contextBo = (ContextBo) super.retrieveObjectForEditOrCopy(document, dataObjectKeys);
085
086        if (KRADConstants.MAINTENANCE_COPY_ACTION.equals(getMaintenanceAction())) {
087            document.getDocumentHeader().setDocumentDescription("New Context Document");
088        }
089
090        return contextBo;
091    }
092}