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.BusinessObjectService;
25  import org.kuali.rice.krad.service.KRADServiceLocator;
26  import org.kuali.rice.krad.util.GlobalVariables;
27  
28  import java.util.Collection;
29  import java.util.HashMap;
30  import java.util.Map;
31  
32  public class PeopleFlowBusRule extends MaintenanceDocumentRuleBase {
33      private BusinessObjectService businessObjectService;
34  
35      @Override
36      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
37          boolean result = super.processCustomSaveDocumentBusinessRules(document);
38          PeopleFlowBo peopleFlowDoc = (PeopleFlowBo)document.getNewMaintainableObject().getDataObject();
39          if (StringUtils.isBlank(peopleFlowDoc.getId())) {
40              result &= checkIfDuplicatePeopleFlow(peopleFlowDoc);
41          }
42          return result;
43      }
44  
45      protected boolean checkIfDuplicatePeopleFlow(PeopleFlowBo peopleFlowBo){
46          boolean rulePassed = true;
47          if (!(StringUtils.isBlank(peopleFlowBo.getName()) ||
48                StringUtils.isBlank(peopleFlowBo.getNamespaceCode()))) {
49                  Map<String,String> criteria = new HashMap<String,String>();
50                  criteria.put(KewImplConstants.PropertyConstants.NAMESPACE_CODE, peopleFlowBo.getNamespaceCode());
51                  criteria.put(KewImplConstants.PropertyConstants.NAME, peopleFlowBo.getName());
52                  Collection<PeopleFlowBo> peopleFlows = getBusinessObjectService().findMatching(PeopleFlowBo.class,criteria);
53                  if (CollectionUtils.isEmpty(peopleFlows)) {
54                      rulePassed = true;
55                  } else {
56                      GlobalVariables.getMessageMap().putError("document.peopleFlow.duplicate",
57                              RiceKeyConstants.PEOPLEFLOW_DUPLICATE, peopleFlowBo.getName().toString(), peopleFlowBo.getNamespaceCode().toString());
58                      rulePassed = false;
59                  }
60          }
61          return rulePassed;
62      }
63  
64      public BusinessObjectService getBusinessObjectService() {
65          if(businessObjectService == null){
66              businessObjectService = KRADServiceLocator.getBusinessObjectService();
67          }
68          return businessObjectService;
69      }
70  
71      public void setBusinessObjectService(BusinessObjectService businessObjectService) {
72          this.businessObjectService = businessObjectService;
73      }
74  }