View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.impl.peopleflow;
17  
18  import org.apache.commons.collections.CollectionUtils;
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.rice.core.api.util.RiceKeyConstants;
21  import org.kuali.rice.kew.impl.KewImplConstants;
22  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
23  import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
24  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
25  import org.kuali.rice.krad.util.GlobalVariables;
26  
27  import java.util.Collection;
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  public class PeopleFlowBusRule extends MaintenanceDocumentRuleBase {
32  
33      @Override
34      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
35          boolean result = super.processCustomSaveDocumentBusinessRules(document);
36          PeopleFlowBo peopleFlowDoc = (PeopleFlowBo)document.getNewMaintainableObject().getDataObject();
37          if (StringUtils.isBlank(peopleFlowDoc.getId())) {
38              result &= checkIfDuplicatePeopleFlow(peopleFlowDoc);
39          }
40          return result;
41      }
42  
43      protected boolean checkIfDuplicatePeopleFlow(PeopleFlowBo peopleFlowBo){
44          boolean rulePassed = true;
45          if (!(StringUtils.isBlank(peopleFlowBo.getName()) ||
46                StringUtils.isBlank(peopleFlowBo.getNamespaceCode()))) {
47                  Map<String,String> criteria = new HashMap<String,String>();
48                  criteria.put(KewImplConstants.PropertyConstants.NAMESPACE_CODE, peopleFlowBo.getNamespaceCode());
49                  criteria.put(KewImplConstants.PropertyConstants.NAME, peopleFlowBo.getName());
50                  Collection<PeopleFlowBo> peopleFlows = KRADServiceLocatorWeb.getLegacyDataAdapter().findMatching(PeopleFlowBo.class, criteria);
51                  if (CollectionUtils.isEmpty(peopleFlows)) {
52                      rulePassed = true;
53                  } else {
54                      GlobalVariables.getMessageMap().putError("document.peopleFlow.duplicate",
55                              RiceKeyConstants.PEOPLEFLOW_DUPLICATE, peopleFlowBo.getName().toString(), peopleFlowBo.getNamespaceCode().toString());
56                      rulePassed = false;
57                  }
58          }
59          return rulePassed;
60      }
61  }