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