Coverage Report - org.kuali.rice.kew.impl.peopleflow.PeopleFlowServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
PeopleFlowServiceImpl
0%
0/90
0%
0/58
3.722
 
 1  
 /**
 2  
  * Copyright 2005-2011 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.exception.RiceIllegalArgumentException;
 21  
 import org.kuali.rice.core.api.exception.RiceIllegalStateException;
 22  
 import org.kuali.rice.kew.api.peopleflow.PeopleFlowDefinition;
 23  
 import org.kuali.rice.kew.api.peopleflow.PeopleFlowDelegate;
 24  
 import org.kuali.rice.kew.api.peopleflow.PeopleFlowMember;
 25  
 import org.kuali.rice.kew.api.peopleflow.PeopleFlowService;
 26  
 import org.kuali.rice.kew.api.repository.type.KewTypeDefinition;
 27  
 import org.kuali.rice.kew.api.repository.type.KewTypeRepositoryService;
 28  
 import org.kuali.rice.kew.impl.KewImplConstants;
 29  
 import org.kuali.rice.krad.service.BusinessObjectService;
 30  
 import org.kuali.rice.kew.responsibility.service.ResponsibilityIdService;
 31  
 
 32  
 import java.util.Collection;
 33  
 import java.util.HashMap;
 34  
 import java.util.Map;
 35  
 
 36  0
 public class PeopleFlowServiceImpl implements PeopleFlowService {
 37  
 
 38  
     private BusinessObjectService businessObjectService;
 39  
     private KewTypeRepositoryService kewTypeRepositoryService;
 40  
     private ResponsibilityIdService responsibilityIdService;
 41  
 
 42  
     @Override
 43  
     public PeopleFlowDefinition getPeopleFlow(String peopleFlowId) {
 44  0
         if (StringUtils.isBlank(peopleFlowId)) {
 45  0
             throw new RiceIllegalArgumentException("peopleFlowId is null or blank");
 46  
         }
 47  
 
 48  0
         return PeopleFlowBo.to(getPeopleFlowBo(peopleFlowId));
 49  
     }
 50  
 
 51  
     @Override
 52  
     public PeopleFlowDefinition getPeopleFlowByName(String namespaceCode, String name) {
 53  0
         if (StringUtils.isBlank(namespaceCode)) {
 54  0
             throw new RiceIllegalArgumentException("namespaceCode is null or blank");
 55  
         }
 56  
 
 57  0
         if (StringUtils.isBlank(name)) {
 58  0
             throw new RiceIllegalArgumentException("name is null or blank");
 59  
         }
 60  
 
 61  0
         return PeopleFlowBo.to(getPeopleFlowBoByName(namespaceCode, name));
 62  
     }
 63  
 
 64  
     @Override
 65  
     public PeopleFlowDefinition createPeopleFlow(PeopleFlowDefinition peopleFlow) {
 66  0
         validateForCreate(peopleFlow);
 67  0
         KewTypeDefinition kewTypeDefinition = loadKewTypeDefinition(peopleFlow);
 68  0
         PeopleFlowBo peopleFlowBo = PeopleFlowBo.from(peopleFlow, kewTypeDefinition);
 69  0
         peopleFlowBo = savePeopleFlow(peopleFlowBo);
 70  0
         return PeopleFlowBo.to(peopleFlowBo);
 71  
     }
 72  
 
 73  
     @Override
 74  
     public PeopleFlowDefinition updatePeopleFlow(PeopleFlowDefinition peopleFlow) {
 75  0
         PeopleFlowBo existingBo = validateForUpdate(peopleFlow);
 76  0
         KewTypeDefinition kewTypeDefinition = loadKewTypeDefinition(peopleFlow);
 77  0
         PeopleFlowBo peopleFlowBo = PeopleFlowBo.fromAndUpdate(peopleFlow, kewTypeDefinition, existingBo);
 78  0
         peopleFlowBo = savePeopleFlow(peopleFlowBo);
 79  0
         return PeopleFlowBo.to(peopleFlowBo);
 80  
     }
 81  
 
 82  
     protected KewTypeDefinition loadKewTypeDefinition(PeopleFlowDefinition peopleFlow) {
 83  0
         KewTypeDefinition kewTypeDefinition = null;
 84  0
         if (peopleFlow.getTypeId() != null) {
 85  0
             kewTypeDefinition = getKewTypeRepositoryService().getTypeById(peopleFlow.getTypeId());
 86  0
             if (kewTypeDefinition == null) {
 87  0
                 throw new RiceIllegalArgumentException("Failed to locate a KewTypeDefinition for the given type id of '" + peopleFlow.getTypeId() + "'");
 88  
             }
 89  
         }
 90  0
         return kewTypeDefinition;
 91  
     }
 92  
 
 93  
     protected void validateForCreate(PeopleFlowDefinition peopleFlow) {
 94  0
         if (peopleFlow == null) {
 95  0
             throw new RiceIllegalArgumentException("peopleFlow is null");
 96  
         }
 97  0
         if (StringUtils.isNotBlank(peopleFlow.getId())) {
 98  0
             throw new RiceIllegalArgumentException("Attempted to create a new PeopleFlow definition with a specified peopleFlowId of '"
 99  
                     + peopleFlow.getId() + "'.  This is not allowed, when creating a new PeopleFlow definition, id must be null.");
 100  
         }
 101  0
         if (peopleFlow.getVersionNumber() != null) {
 102  0
             throw new RiceIllegalArgumentException("The version number on the given PeopleFlow definition was not null, value was " + peopleFlow.getVersionNumber() +
 103  
                     "  When creating a new PeopleFlow, the given version number must be null.");
 104  
         }
 105  0
         validatePeopleFlowMembersForCreate(peopleFlow);
 106  0
         if (getPeopleFlowBoByName(peopleFlow.getNamespaceCode(), peopleFlow.getName()) != null) {
 107  0
             throw new RiceIllegalStateException("A PeopleFlow definition with the namespace code '" + peopleFlow.getNamespaceCode() +
 108  
             "' and name '" + peopleFlow.getName() + "' already exists.");
 109  
         }
 110  0
     }
 111  
 
 112  
     protected void validatePeopleFlowMembersForCreate(PeopleFlowDefinition peopleFlowDefinition) {
 113  0
         for (PeopleFlowMember member : peopleFlowDefinition.getMembers()) {
 114  0
             if (StringUtils.isNotBlank(member.getResponsibilityId())) {
 115  0
                 throw new RiceIllegalArgumentException("Attempted to create a new PeopleFlow with a member that already had a responsibility id of '" +
 116  
                         member.getResponsibilityId() + "' specified.  All members must have a null responsibility id upon creation.");
 117  
             }
 118  0
             for (PeopleFlowDelegate delegate : member.getDelegates()) {
 119  0
                 if (StringUtils.isNotBlank(delegate.getResponsibilityId())) {
 120  0
                     throw new RiceIllegalArgumentException("Attempted to create a new PeopleFlow with a delegate that already had a responsibility id of '" +
 121  
                             delegate.getResponsibilityId() + "' specified.  All delegates must have a null responsibility id upon creation.");
 122  
                 }
 123  
             }
 124  
         }
 125  0
     }
 126  
 
 127  
     protected PeopleFlowBo validateForUpdate(PeopleFlowDefinition peopleFlow) {
 128  0
         if (peopleFlow == null) {
 129  0
             throw new RiceIllegalArgumentException("peopleFlow is null");
 130  
         }
 131  0
         if (StringUtils.isBlank(peopleFlow.getId())) {
 132  0
             throw new RiceIllegalArgumentException("Attempted to update a PeopleFlow definition without a specified peopleFlowId, the id is required when performing an update.");
 133  
         }
 134  0
         if (peopleFlow.getVersionNumber() == null) {
 135  0
             throw new RiceIllegalArgumentException("The version number on the given PeopleFlow definition was null, a version number must be supplied when updating a PeopleFlow.");
 136  
         }
 137  0
         PeopleFlowBo peopleFlowBo = getPeopleFlowBo(peopleFlow.getId());
 138  0
         if (peopleFlowBo == null) {
 139  0
             throw new RiceIllegalArgumentException("Failed to locate an existing PeopleFlow definition with the given id of '" + peopleFlow.getId() + "'");
 140  
         }
 141  0
         return peopleFlowBo;
 142  
     }
 143  
 
 144  
     protected PeopleFlowBo getPeopleFlowBo(String peopleFlowId) {
 145  0
         if (StringUtils.isBlank(peopleFlowId)) {
 146  0
             throw new RiceIllegalArgumentException("peopleFlowId was a null or blank value");
 147  
         }
 148  0
         return businessObjectService.findBySinglePrimaryKey(PeopleFlowBo.class, peopleFlowId);
 149  
     }
 150  
 
 151  
     protected PeopleFlowBo getPeopleFlowBoByName(String namespaceCode, String name) {
 152  0
         if (StringUtils.isBlank(namespaceCode)) {
 153  0
             throw new RiceIllegalArgumentException("namespaceCode was a null or blank value");
 154  
         }
 155  0
         if (StringUtils.isBlank(name)) {
 156  0
             throw new RiceIllegalArgumentException("name was a null or blank value");
 157  
         }
 158  0
         Map<String,String> criteria = new HashMap<String,String>();
 159  0
                 criteria.put(KewImplConstants.PropertyConstants.NAMESPACE_CODE, namespaceCode);
 160  0
         criteria.put(KewImplConstants.PropertyConstants.NAME, name);
 161  0
                 Collection<PeopleFlowBo> peopleFlows = businessObjectService.findMatching(PeopleFlowBo.class, criteria);
 162  0
         if (CollectionUtils.isEmpty(peopleFlows)) {
 163  0
             return null;
 164  0
         } else if (peopleFlows.size() > 1) {
 165  0
             throw new RiceIllegalStateException("Found more than one PeopleFlow with the given namespace code '" + namespaceCode + "' and name '" + name + "'");
 166  
                 }
 167  0
         return peopleFlows.iterator().next();
 168  
     }
 169  
 
 170  
     protected PeopleFlowBo savePeopleFlow(PeopleFlowBo peopleFlowBo) {
 171  0
                 if ( peopleFlowBo == null ) {
 172  0
                         return null;
 173  
                 }
 174  0
         assignResponsibilityIds(peopleFlowBo);
 175  0
         return businessObjectService.save(peopleFlowBo);
 176  
     }
 177  
 
 178  
     protected void assignResponsibilityIds(PeopleFlowBo peopleFlowBo) {
 179  0
         if (CollectionUtils.isNotEmpty(peopleFlowBo.getMembers())) {
 180  0
             for (PeopleFlowMemberBo memberBo : peopleFlowBo.getMembers()) {
 181  0
                 if (StringUtils.isBlank(memberBo.getResponsibilityId())) {
 182  0
                     memberBo.setResponsibilityId(responsibilityIdService.getNewResponsibilityId());
 183  
                 }
 184  0
                 if (CollectionUtils.isNotEmpty(memberBo.getDelegates())) {
 185  0
                     for (PeopleFlowDelegateBo delegateBo : memberBo.getDelegates()) {
 186  0
                         if (StringUtils.isBlank(delegateBo.getResponsibilityId())) {
 187  0
                             delegateBo.setResponsibilityId(responsibilityIdService.getNewResponsibilityId());
 188  
                         }
 189  
                     }
 190  
                 }
 191  
             }
 192  
         }
 193  0
     }
 194  
 
 195  
     public BusinessObjectService getBusinessObjectService() {
 196  0
         return businessObjectService;
 197  
     }
 198  
 
 199  
     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
 200  0
         this.businessObjectService = businessObjectService;
 201  0
     }
 202  
 
 203  
     public KewTypeRepositoryService getKewTypeRepositoryService() {
 204  0
         return kewTypeRepositoryService;
 205  
     }
 206  
 
 207  
     public void setKewTypeRepositoryService(KewTypeRepositoryService kewTypeRepositoryService) {
 208  0
         this.kewTypeRepositoryService = kewTypeRepositoryService;
 209  0
     }
 210  
 
 211  
     public ResponsibilityIdService getResponsibilityIdService() {
 212  0
         return responsibilityIdService;
 213  
     }
 214  
 
 215  
     public void setResponsibilityIdService(ResponsibilityIdService responsibilityIdService) {
 216  0
         this.responsibilityIdService = responsibilityIdService;
 217  0
     }
 218  
     
 219  
 }