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.kew.impl.peopleflow;
017
018import org.apache.commons.collections.CollectionUtils;
019import org.apache.commons.lang.StringUtils;
020import org.kuali.rice.core.api.util.RiceKeyConstants;
021import org.kuali.rice.kew.impl.KewImplConstants;
022import org.kuali.rice.krad.maintenance.MaintenanceDocument;
023import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
024import org.kuali.rice.krad.service.BusinessObjectService;
025import org.kuali.rice.krad.service.KRADServiceLocator;
026import org.kuali.rice.krad.util.GlobalVariables;
027
028import java.util.Collection;
029import java.util.HashMap;
030import java.util.Map;
031
032public class PeopleFlowBusRule extends MaintenanceDocumentRuleBase {
033    private BusinessObjectService businessObjectService;
034
035    @Override
036    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
037        boolean result = super.processCustomSaveDocumentBusinessRules(document);
038        PeopleFlowBo peopleFlowDoc = (PeopleFlowBo)document.getNewMaintainableObject().getDataObject();
039        if (StringUtils.isBlank(peopleFlowDoc.getId())) {
040            result &= checkIfDuplicatePeopleFlow(peopleFlowDoc);
041        }
042        return result;
043    }
044
045    protected boolean checkIfDuplicatePeopleFlow(PeopleFlowBo peopleFlowBo){
046        boolean rulePassed = true;
047        if (!(StringUtils.isBlank(peopleFlowBo.getName()) ||
048              StringUtils.isBlank(peopleFlowBo.getNamespaceCode()))) {
049                Map<String,String> criteria = new HashMap<String,String>();
050                criteria.put(KewImplConstants.PropertyConstants.NAMESPACE_CODE, peopleFlowBo.getNamespaceCode());
051                criteria.put(KewImplConstants.PropertyConstants.NAME, peopleFlowBo.getName());
052                Collection<PeopleFlowBo> peopleFlows = getBusinessObjectService().findMatching(PeopleFlowBo.class,criteria);
053                if (CollectionUtils.isEmpty(peopleFlows)) {
054                    rulePassed = true;
055                } else {
056                    GlobalVariables.getMessageMap().putError("document.peopleFlow.duplicate",
057                            RiceKeyConstants.PEOPLEFLOW_DUPLICATE, peopleFlowBo.getName().toString(), peopleFlowBo.getNamespaceCode().toString());
058                    rulePassed = false;
059                }
060        }
061        return rulePassed;
062    }
063
064    public BusinessObjectService getBusinessObjectService() {
065        if(businessObjectService == null){
066            businessObjectService = KRADServiceLocator.getBusinessObjectService();
067        }
068        return businessObjectService;
069    }
070
071    public void setBusinessObjectService(BusinessObjectService businessObjectService) {
072        this.businessObjectService = businessObjectService;
073    }
074}