View Javadoc

1   /**
2    * Copyright 2005-2012 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  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          if (StringUtils.isBlank(peopleFlowId)) {
45              throw new RiceIllegalArgumentException("peopleFlowId is null or blank");
46          }
47  
48          return PeopleFlowBo.to(getPeopleFlowBo(peopleFlowId));
49      }
50  
51      @Override
52      public PeopleFlowDefinition getPeopleFlowByName(String namespaceCode, String name) {
53          if (StringUtils.isBlank(namespaceCode)) {
54              throw new RiceIllegalArgumentException("namespaceCode is null or blank");
55          }
56  
57          if (StringUtils.isBlank(name)) {
58              throw new RiceIllegalArgumentException("name is null or blank");
59          }
60  
61          return PeopleFlowBo.to(getPeopleFlowBoByName(namespaceCode, name));
62      }
63  
64      @Override
65      public PeopleFlowDefinition createPeopleFlow(PeopleFlowDefinition peopleFlow) {
66          validateForCreate(peopleFlow);
67          KewTypeDefinition kewTypeDefinition = loadKewTypeDefinition(peopleFlow);
68          PeopleFlowBo peopleFlowBo = PeopleFlowBo.from(peopleFlow, kewTypeDefinition);
69          peopleFlowBo = savePeopleFlow(peopleFlowBo);
70          return PeopleFlowBo.to(peopleFlowBo);
71      }
72  
73      @Override
74      public PeopleFlowDefinition updatePeopleFlow(PeopleFlowDefinition peopleFlow) {
75          PeopleFlowBo existingBo = validateForUpdate(peopleFlow);
76          KewTypeDefinition kewTypeDefinition = loadKewTypeDefinition(peopleFlow);
77          PeopleFlowBo peopleFlowBo = PeopleFlowBo.fromAndUpdate(peopleFlow, kewTypeDefinition, existingBo);
78          peopleFlowBo = savePeopleFlow(peopleFlowBo);
79          return PeopleFlowBo.to(peopleFlowBo);
80      }
81  
82      protected KewTypeDefinition loadKewTypeDefinition(PeopleFlowDefinition peopleFlow) {
83          KewTypeDefinition kewTypeDefinition = null;
84          if (peopleFlow.getTypeId() != null) {
85              kewTypeDefinition = getKewTypeRepositoryService().getTypeById(peopleFlow.getTypeId());
86              if (kewTypeDefinition == null) {
87                  throw new RiceIllegalArgumentException("Failed to locate a KewTypeDefinition for the given type id of '" + peopleFlow.getTypeId() + "'");
88              }
89          }
90          return kewTypeDefinition;
91      }
92  
93      protected void validateForCreate(PeopleFlowDefinition peopleFlow) {
94          if (peopleFlow == null) {
95              throw new RiceIllegalArgumentException("peopleFlow is null");
96          }
97          if (StringUtils.isNotBlank(peopleFlow.getId())) {
98              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         if (peopleFlow.getVersionNumber() != null) {
102             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         validatePeopleFlowMembersForCreate(peopleFlow);
106         if (getPeopleFlowBoByName(peopleFlow.getNamespaceCode(), peopleFlow.getName()) != null) {
107             throw new RiceIllegalStateException("A PeopleFlow definition with the namespace code '" + peopleFlow.getNamespaceCode() +
108             "' and name '" + peopleFlow.getName() + "' already exists.");
109         }
110     }
111 
112     protected void validatePeopleFlowMembersForCreate(PeopleFlowDefinition peopleFlowDefinition) {
113         for (PeopleFlowMember member : peopleFlowDefinition.getMembers()) {
114             if (StringUtils.isNotBlank(member.getResponsibilityId())) {
115                 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             for (PeopleFlowDelegate delegate : member.getDelegates()) {
119                 if (StringUtils.isNotBlank(delegate.getResponsibilityId())) {
120                     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     }
126 
127     protected PeopleFlowBo validateForUpdate(PeopleFlowDefinition peopleFlow) {
128         if (peopleFlow == null) {
129             throw new RiceIllegalArgumentException("peopleFlow is null");
130         }
131         if (StringUtils.isBlank(peopleFlow.getId())) {
132             throw new RiceIllegalArgumentException("Attempted to update a PeopleFlow definition without a specified peopleFlowId, the id is required when performing an update.");
133         }
134         if (peopleFlow.getVersionNumber() == null) {
135             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         PeopleFlowBo peopleFlowBo = getPeopleFlowBo(peopleFlow.getId());
138         if (peopleFlowBo == null) {
139             throw new RiceIllegalArgumentException("Failed to locate an existing PeopleFlow definition with the given id of '" + peopleFlow.getId() + "'");
140         }
141         return peopleFlowBo;
142     }
143 
144     protected PeopleFlowBo getPeopleFlowBo(String peopleFlowId) {
145         if (StringUtils.isBlank(peopleFlowId)) {
146             throw new RiceIllegalArgumentException("peopleFlowId was a null or blank value");
147         }
148         return businessObjectService.findBySinglePrimaryKey(PeopleFlowBo.class, peopleFlowId);
149     }
150 
151     protected PeopleFlowBo getPeopleFlowBoByName(String namespaceCode, String name) {
152         if (StringUtils.isBlank(namespaceCode)) {
153             throw new RiceIllegalArgumentException("namespaceCode was a null or blank value");
154         }
155         if (StringUtils.isBlank(name)) {
156             throw new RiceIllegalArgumentException("name was a null or blank value");
157         }
158         Map<String,String> criteria = new HashMap<String,String>();
159 		criteria.put(KewImplConstants.PropertyConstants.NAMESPACE_CODE, namespaceCode);
160         criteria.put(KewImplConstants.PropertyConstants.NAME, name);
161 		Collection<PeopleFlowBo> peopleFlows = businessObjectService.findMatching(PeopleFlowBo.class, criteria);
162         if (CollectionUtils.isEmpty(peopleFlows)) {
163             return null;
164         } else if (peopleFlows.size() > 1) {
165             throw new RiceIllegalStateException("Found more than one PeopleFlow with the given namespace code '" + namespaceCode + "' and name '" + name + "'");
166 		}
167         return peopleFlows.iterator().next();
168     }
169 
170     protected PeopleFlowBo savePeopleFlow(PeopleFlowBo peopleFlowBo) {
171 		if ( peopleFlowBo == null ) {
172 			return null;
173 		}
174         assignResponsibilityIds(peopleFlowBo);
175         return businessObjectService.save(peopleFlowBo);
176     }
177 
178     protected void assignResponsibilityIds(PeopleFlowBo peopleFlowBo) {
179         if (CollectionUtils.isNotEmpty(peopleFlowBo.getMembers())) {
180             for (PeopleFlowMemberBo memberBo : peopleFlowBo.getMembers()) {
181                 if (StringUtils.isBlank(memberBo.getResponsibilityId())) {
182                     memberBo.setResponsibilityId(responsibilityIdService.getNewResponsibilityId());
183                 }
184                 if (CollectionUtils.isNotEmpty(memberBo.getDelegates())) {
185                     for (PeopleFlowDelegateBo delegateBo : memberBo.getDelegates()) {
186                         if (StringUtils.isBlank(delegateBo.getResponsibilityId())) {
187                             delegateBo.setResponsibilityId(responsibilityIdService.getNewResponsibilityId());
188                         }
189                     }
190                 }
191             }
192         }
193     }
194 
195     public BusinessObjectService getBusinessObjectService() {
196         return businessObjectService;
197     }
198 
199     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
200         this.businessObjectService = businessObjectService;
201     }
202 
203     public KewTypeRepositoryService getKewTypeRepositoryService() {
204         return kewTypeRepositoryService;
205     }
206 
207     public void setKewTypeRepositoryService(KewTypeRepositoryService kewTypeRepositoryService) {
208         this.kewTypeRepositoryService = kewTypeRepositoryService;
209     }
210 
211     public ResponsibilityIdService getResponsibilityIdService() {
212         return responsibilityIdService;
213     }
214 
215     public void setResponsibilityIdService(ResponsibilityIdService responsibilityIdService) {
216         this.responsibilityIdService = responsibilityIdService;
217     }
218     
219 }