View Javadoc
1   /**
2    * Copyright 2005-2015 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.kim.impl.responsibility;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.commons.lang.exception.ExceptionUtils;
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.kuali.rice.core.api.criteria.Predicate;
23  import org.kuali.rice.core.api.criteria.QueryByCriteria;
24  import org.kuali.rice.core.api.criteria.QueryResults;
25  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
26  import org.kuali.rice.core.api.exception.RiceIllegalStateException;
27  import org.kuali.rice.core.api.membership.MemberType;
28  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
29  import org.kuali.rice.kim.api.common.delegate.DelegateType;
30  import org.kuali.rice.kim.api.common.template.Template;
31  import org.kuali.rice.kim.api.common.template.TemplateQueryResults;
32  import org.kuali.rice.kim.api.responsibility.Responsibility;
33  import org.kuali.rice.kim.api.responsibility.ResponsibilityAction;
34  import org.kuali.rice.kim.api.responsibility.ResponsibilityQueryResults;
35  import org.kuali.rice.kim.api.responsibility.ResponsibilityService;
36  import org.kuali.rice.kim.api.role.RoleMembership;
37  import org.kuali.rice.kim.api.role.RoleResponsibilityAction;
38  import org.kuali.rice.kim.api.role.RoleService;
39  import org.kuali.rice.kim.api.type.KimType;
40  import org.kuali.rice.kim.api.type.KimTypeInfoService;
41  import org.kuali.rice.kim.framework.responsibility.ResponsibilityTypeService;
42  import org.kuali.rice.kim.impl.KIMPropertyConstants;
43  import org.kuali.rice.kim.impl.common.attribute.AttributeTransform;
44  import org.kuali.rice.kim.impl.common.attribute.KimAttributeDataBo;
45  import org.kuali.rice.kim.impl.role.RoleResponsibilityActionBo;
46  import org.kuali.rice.kim.impl.role.RoleResponsibilityBo;
47  import org.kuali.rice.krad.data.DataObjectService;
48  import org.springframework.util.CollectionUtils;
49  
50  import javax.xml.namespace.QName;
51  import java.util.ArrayList;
52  import java.util.Collection;
53  import java.util.Collections;
54  import java.util.HashMap;
55  import java.util.Iterator;
56  import java.util.List;
57  import java.util.Map;
58  
59  import static org.kuali.rice.core.api.criteria.PredicateFactory.*;
60  
61  public class ResponsibilityServiceImpl implements ResponsibilityService {
62  
63      private static final Integer DEFAULT_PRIORITY_NUMBER = Integer.valueOf(1);
64      private static final Log LOG = LogFactory.getLog(ResponsibilityServiceImpl.class);
65  
66      private ResponsibilityTypeService defaultResponsibilityTypeService;
67      private KimTypeInfoService kimTypeInfoService;
68      private RoleService roleService;
69  
70      private DataObjectService dataObjectService;
71  
72      @Override
73      public Responsibility createResponsibility(final Responsibility responsibility)
74              throws RiceIllegalArgumentException, RiceIllegalStateException {
75          incomingParamCheck(responsibility, "responsibility");
76  
77          if (StringUtils.isNotBlank(responsibility.getId()) && getResponsibility(responsibility.getId()) != null) {
78              throw new RiceIllegalStateException("the responsibility to create already exists: " + responsibility);
79          }
80          List<ResponsibilityAttributeBo> attrBos = Collections.emptyList();
81          if (responsibility.getTemplate() != null) {
82              attrBos = KimAttributeDataBo.createFrom(ResponsibilityAttributeBo.class, responsibility.getAttributes(), responsibility.getTemplate().getKimTypeId());
83          }
84          ResponsibilityBo bo = ResponsibilityBo.from(responsibility);
85          bo.setAttributeDetails(attrBos);
86          return ResponsibilityBo.to(getDataObjectService().save(bo));
87      }
88  
89      @Override
90      public Responsibility updateResponsibility(final Responsibility responsibility)
91              throws RiceIllegalArgumentException, RiceIllegalStateException {
92          incomingParamCheck(responsibility, "responsibility");
93  
94          if (StringUtils.isBlank(responsibility.getId()) || getResponsibility(responsibility.getId()) == null) {
95              throw new RiceIllegalStateException("the responsibility does not exist: " + responsibility);
96          }
97  
98         List<ResponsibilityAttributeBo> attrBos = Collections.emptyList();
99          if (responsibility.getTemplate() != null) {
100             attrBos = KimAttributeDataBo.createFrom(ResponsibilityAttributeBo.class, responsibility.getAttributes(), responsibility.getTemplate().getKimTypeId());
101         }
102         ResponsibilityBo bo = ResponsibilityBo.from(responsibility);
103 
104         if (bo.getAttributeDetails() != null) {
105             bo.getAttributeDetails().clear();
106             bo.setAttributeDetails(attrBos);
107         }
108 
109         return ResponsibilityBo.to(getDataObjectService().save(bo));
110     }
111 
112     @Override
113     public Responsibility getResponsibility(final String id) throws RiceIllegalArgumentException {
114         incomingParamCheck(id, "id");
115 
116         return ResponsibilityBo.to(getDataObjectService().find(ResponsibilityBo.class, id));
117     }
118 
119     @Override
120     public Responsibility findRespByNamespaceCodeAndName(final String namespaceCode, final String name)
121             throws RiceIllegalArgumentException {
122         incomingParamCheck(namespaceCode, "namespaceCode");
123         incomingParamCheck(name, "name");
124 
125         final Map<String, Object> criteria = new HashMap<String, Object>();
126         criteria.put("namespaceCode", namespaceCode);
127         criteria.put("name", name);
128         criteria.put("active", Boolean.TRUE);
129 
130         QueryResults<ResponsibilityBo> bos =
131                 getDataObjectService().findMatching(ResponsibilityBo.class,
132                         QueryByCriteria.Builder.andAttributes(criteria).build());
133 
134         if (bos != null && bos.getResults() != null) {
135             if (bos.getResults().size() > 1) {
136                 throw new RiceIllegalStateException("more than one Responsibility found with namespace code: " + namespaceCode + " and name: " + name);
137             }
138 
139             final Iterator<ResponsibilityBo> i = bos.getResults().iterator();
140             return i.hasNext() ? ResponsibilityBo.to(i.next()) : null;
141         }
142         return null;
143     }
144 
145     @Override
146     public Template getResponsibilityTemplate(final String id) throws RiceIllegalArgumentException {
147         incomingParamCheck(id, "id");
148 
149         return ResponsibilityTemplateBo.to(getDataObjectService().find(ResponsibilityTemplateBo.class, id));
150     }
151 
152     @Override
153     public Template findRespTemplateByNamespaceCodeAndName(final String namespaceCode, final String name) throws RiceIllegalArgumentException {
154         incomingParamCheck(namespaceCode, "namespaceCode");
155         incomingParamCheck(name, "name");
156 
157         final Map<String, Object> criteria = new HashMap<String, Object>();
158         criteria.put("namespaceCode", namespaceCode);
159         criteria.put("name", name);
160         criteria.put("active", Boolean.TRUE);
161 
162         QueryResults<ResponsibilityTemplateBo> bos =
163                 getDataObjectService().findMatching(ResponsibilityTemplateBo.class,
164                         QueryByCriteria.Builder.andAttributes(criteria).build());
165         if (bos != null && bos.getResults() != null) {
166             if (bos.getResults().size() > 1) {
167                 throw new RiceIllegalStateException("more than one Responsibility Template found with namespace code: " + namespaceCode + " and name: " + name);
168             }
169 
170             final Iterator<ResponsibilityTemplateBo> i = bos.getResults().iterator();
171             return i.hasNext() ? ResponsibilityTemplateBo.to(i.next()) : null;
172         }
173         return null;
174     }
175 
176     @Override
177     public boolean hasResponsibility(final String principalId, final String namespaceCode,
178             final String respName, final Map<String, String> qualification) throws RiceIllegalArgumentException {
179         incomingParamCheck(principalId, "principalId");
180         incomingParamCheck(namespaceCode, "namespaceCode");
181         incomingParamCheck(respName, "respName");
182         incomingParamCheck(qualification, "qualification");
183 
184         // get all the responsibility objects whose name match that requested
185         final List<Responsibility> responsibilities = Collections.singletonList(findRespByNamespaceCodeAndName(namespaceCode, respName));
186         return hasResp(principalId, namespaceCode, responsibilities, qualification, null);
187     }
188 
189     @Override
190     public boolean hasResponsibilityByTemplate(final String principalId, final String namespaceCode,
191             final String respTemplateName, final Map<String, String> qualification,
192             final Map<String, String> responsibilityDetails) throws RiceIllegalArgumentException {
193         incomingParamCheck(principalId, "principalId");
194         incomingParamCheck(namespaceCode, "namespaceCode");
195         incomingParamCheck(respTemplateName, "respTemplateName");
196         incomingParamCheck(qualification, "qualification");
197         incomingParamCheck(responsibilityDetails, "responsibilityDetails");
198 
199 
200         // get all the responsibility objects whose name match that requested
201         final List<Responsibility> responsibilities = findResponsibilitiesByTemplate(namespaceCode, respTemplateName);
202         return hasResp(principalId, namespaceCode, responsibilities, qualification, responsibilityDetails);
203     }
204 
205     private boolean hasResp(final String principalId, final String namespaceCode,
206             final List<Responsibility> responsibilities,
207             final Map<String, String> qualification,
208             final Map<String, String> responsibilityDetails) throws RiceIllegalArgumentException {
209         // now, filter the full list by the detail passed
210         final List<String> ids = new ArrayList<String>();
211         for (Responsibility r : getMatchingResponsibilities(responsibilities, responsibilityDetails)) {
212             ids.add(r.getId());
213         }
214         final List<String> roleIds = getRoleIdsForResponsibilities(ids);
215         return roleService.principalHasRole(principalId, roleIds, qualification);
216     }
217 
218     @Override
219     public List<ResponsibilityAction> getResponsibilityActions(final String namespaceCode,
220             final String responsibilityName, final Map<String, String> qualification) throws RiceIllegalArgumentException {
221         incomingParamCheck(namespaceCode, "namespaceCode");
222         incomingParamCheck(responsibilityName, "responsibilityName");
223         incomingParamCheck(qualification, "qualification");
224         
225         if ( LOG.isDebugEnabled() ) {
226             logResponsibilityCheck( namespaceCode, responsibilityName, qualification, Collections.<String, String>emptyMap() );
227         }
228 
229         // get all the responsibility objects whose name match that requested
230         List<Responsibility> responsibilities = Collections.singletonList(findRespByNamespaceCodeAndName(namespaceCode, responsibilityName));
231         return getRespActions(namespaceCode, responsibilities, qualification, null);
232     }
233 
234     @Override
235     public List<ResponsibilityAction> getResponsibilityActionsByTemplate(final String namespaceCode,
236             final String respTemplateName, final Map<String, String> qualification,
237             final Map<String, String> responsibilityDetails) throws RiceIllegalArgumentException {
238         incomingParamCheck(namespaceCode, "namespaceCode");
239         incomingParamCheck(respTemplateName, "respTemplateName");
240         incomingParamCheck(qualification, "qualification");
241         
242         if ( LOG.isDebugEnabled() ) {
243             logResponsibilityCheck( namespaceCode, respTemplateName, qualification, responsibilityDetails );
244         }
245 
246         // get all the responsibility objects whose name match that requested
247         List<Responsibility> responsibilities = findResponsibilitiesByTemplate(namespaceCode, respTemplateName);
248         return getRespActions(namespaceCode, responsibilities, qualification, responsibilityDetails);
249     }
250 
251     private List<ResponsibilityAction> getRespActions(final String namespaceCode,
252             final List<Responsibility> responsibilities,
253             final Map<String, String> qualification,
254             final Map<String, String> responsibilityDetails) {
255         // now, filter the full list by the detail passed
256         List<Responsibility> applicableResponsibilities = getMatchingResponsibilities(responsibilities, responsibilityDetails);
257         List<ResponsibilityAction> results = new ArrayList<ResponsibilityAction>();
258         for (Responsibility r : applicableResponsibilities) {
259             List<String> roleIds = getRoleIdsForResponsibility(r.getId());
260             results.addAll(getActionsForResponsibilityRoles(r, roleIds, qualification));
261         }
262         if ( LOG.isDebugEnabled() ) {
263             LOG.debug("Found " + results.size() + " matching ResponsibilityAction objects");
264             if ( LOG.isTraceEnabled() ) {
265                 LOG.trace( results );
266             }
267         }
268         return results;
269     }
270 
271     private List<ResponsibilityAction> getActionsForResponsibilityRoles(Responsibility responsibility, List<String> roleIds, Map<String, String> qualification) {
272         List<ResponsibilityAction> results = new ArrayList<ResponsibilityAction>();
273         Collection<RoleMembership> roleMembers = roleService.getRoleMembers(roleIds,qualification);
274         for (RoleMembership rm : roleMembers) {
275             // only add them to the list if the member ID has been populated
276             if (StringUtils.isNotBlank(rm.getMemberId())) {
277                 List<RoleResponsibilityAction> roleResponsibilityActions
278                         = getResponsibilityActions(rm.getRoleId(), responsibility.getId(), rm.getId());
279                 for (RoleResponsibilityAction roleResponsibilityAction : roleResponsibilityActions) {
280                     final ResponsibilityAction.Builder rai = ResponsibilityAction.Builder.create();
281                     rai.setMemberRoleId((rm.getEmbeddedRoleId() == null) ? rm.getRoleId() : rm.getEmbeddedRoleId());
282                     rai.setRoleId(rm.getRoleId());
283                     rai.setQualifier(rm.getQualifier());
284                     final List<DelegateType.Builder> bs = new ArrayList<DelegateType.Builder>();
285                     for (DelegateType d : rm.getDelegates()) {
286                         bs.add(DelegateType.Builder.create(d));
287                     }
288                     rai.setDelegates(bs);
289                     rai.setResponsibilityId(responsibility.getId());
290                     rai.setResponsibilityName(responsibility.getName());
291                     rai.setResponsibilityNamespaceCode(responsibility.getNamespaceCode());
292 
293                     if (MemberType.PRINCIPAL.equals(rm.getType())) {
294                         rai.setPrincipalId(rm.getMemberId());
295                     } else {
296                         rai.setGroupId(rm.getMemberId());
297                     }
298                     // add the data to the ResponsibilityActionInfo objects
299                     rai.setActionTypeCode(roleResponsibilityAction.getActionTypeCode());
300                     rai.setActionPolicyCode(roleResponsibilityAction.getActionPolicyCode());
301                     rai.setPriorityNumber(roleResponsibilityAction.getPriorityNumber() == null ? DEFAULT_PRIORITY_NUMBER : roleResponsibilityAction.getPriorityNumber());
302                     rai.setForceAction(roleResponsibilityAction.isForceAction());
303                     rai.setParallelRoutingGroupingCode((rm.getRoleSortingCode() == null) ? "" : rm.getRoleSortingCode());
304                     rai.setRoleResponsibilityActionId(roleResponsibilityAction.getId());
305                     results.add(rai.build());
306                 }
307             }
308         }
309         return Collections.unmodifiableList(results);
310     }
311 
312     private List<RoleResponsibilityAction> getResponsibilityActions(String roleId, String responsibilityId, String roleMemberId) {
313         List<RoleResponsibilityAction> responsibilityActions = new ArrayList<RoleResponsibilityAction>();
314 
315         // KULRICE-7459: Requisition, PO and its subtype documents are going to final status where they should not.
316         //
317         // need to do in 2 steps due to "*" wildcard convention in column data for role member id and role
318         // responsibility id.  Well, we could do in 1 step w/ straight SQL, but not w/ Criteria API due to the
319         // INNER JOIN automatically created between RoleResponsibility and RoleResponsibilityAction tables.
320 
321         final Predicate roleResponsibilityPredicate =
322                 and(
323                         equal("responsibilityId", responsibilityId),
324                         equal("roleId", roleId),
325                         equal("active", Boolean.TRUE)
326                 );
327 
328         // First get RoleResponsibilityBos
329         final QueryByCriteria.Builder roleResponsibilityQueryBuilder = QueryByCriteria.Builder.create();
330         roleResponsibilityQueryBuilder.setPredicates(roleResponsibilityPredicate);
331 
332         final QueryResults<RoleResponsibilityBo> roleResponsibilityResults = getDataObjectService().
333                 findMatching(RoleResponsibilityBo.class, roleResponsibilityQueryBuilder.build());
334         final List<RoleResponsibilityBo> roleResponsibilityBos = roleResponsibilityResults.getResults();
335 
336         if (!CollectionUtils.isEmpty(roleResponsibilityBos)) { // if there are any...
337             // Then query RoleResponsibilityActionBos based on them
338 
339             List<String> roleResponsibilityIds = new ArrayList<String>(roleResponsibilityBos.size());
340             for (RoleResponsibilityBo roleResponsibilityBo : roleResponsibilityBos) {
341                 roleResponsibilityIds.add(roleResponsibilityBo.getRoleResponsibilityId());
342             }
343 
344             final Predicate roleResponsibilityActionPredicate =
345                     or(
346                             and(
347                                     in("roleResponsibilityId", roleResponsibilityIds.toArray()),
348                                     or(
349                                             equal(KIMPropertyConstants.RoleMember.ROLE_MEMBER_ID, roleMemberId),
350                                             equal(KIMPropertyConstants.RoleMember.ROLE_MEMBER_ID, "*")
351                                     )
352                             ),
353                             and(
354                                     equal("roleResponsibilityId", "*"),
355                                     equal(KIMPropertyConstants.RoleMember.ROLE_MEMBER_ID, roleMemberId)
356                             )
357                     );
358 
359             final QueryByCriteria.Builder roleResponsibilityActionQueryBuilder = QueryByCriteria.Builder.create();
360             roleResponsibilityActionQueryBuilder.setPredicates(roleResponsibilityActionPredicate);
361 
362             final QueryResults<RoleResponsibilityActionBo> roleResponsibilityActionResults =
363                     getDataObjectService().findMatching(RoleResponsibilityActionBo.class,
364                             roleResponsibilityActionQueryBuilder.build());
365 
366             final List<RoleResponsibilityActionBo> roleResponsibilityActionBos = roleResponsibilityActionResults.getResults();
367             for (RoleResponsibilityActionBo roleResponsibilityActionBo : roleResponsibilityActionBos) {
368                 responsibilityActions.add(RoleResponsibilityActionBo.to(roleResponsibilityActionBo));
369             };
370         }
371 
372         return Collections.unmodifiableList(responsibilityActions);
373     }
374 
375     @Override
376     public List<String> getRoleIdsForResponsibility(String id) throws RiceIllegalArgumentException {
377         incomingParamCheck(id, "id");
378 
379         final List<String> roleIds = getRoleIdsForPredicate(and(equal("responsibilityId", id), equal("active", Boolean.TRUE)));
380 
381         return Collections.unmodifiableList(roleIds);
382     }
383 
384     @Override
385     public ResponsibilityQueryResults findResponsibilities(final QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
386         incomingParamCheck(queryByCriteria, "queryByCriteria");
387 
388         QueryResults<ResponsibilityBo> results = getDataObjectService().findMatching(ResponsibilityBo.class,
389                 AttributeTransform.getInstance().apply(queryByCriteria));
390 
391         ResponsibilityQueryResults.Builder builder = ResponsibilityQueryResults.Builder.create();
392         builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
393         builder.setTotalRowCount(results.getTotalRowCount());
394 
395         final List<Responsibility.Builder> ims = new ArrayList<Responsibility.Builder>();
396         for (ResponsibilityBo bo : results.getResults()) {
397             ims.add(Responsibility.Builder.create(bo));
398         }
399 
400         builder.setResults(ims);
401         return builder.build();
402     }
403 
404     @Override
405     public TemplateQueryResults findResponsibilityTemplates(final QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
406         incomingParamCheck(queryByCriteria, "queryByCriteria");
407 
408         QueryResults<ResponsibilityTemplateBo> results = getDataObjectService().
409                 findMatching(ResponsibilityTemplateBo.class, queryByCriteria);
410 
411         TemplateQueryResults.Builder builder = TemplateQueryResults.Builder.create();
412         builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
413         builder.setTotalRowCount(results.getTotalRowCount());
414 
415         final List<Template.Builder> ims = new ArrayList<Template.Builder>();
416         for (ResponsibilityTemplateBo bo : results.getResults()) {
417             ims.add(Template.Builder.create(bo));
418         }
419 
420         builder.setResults(ims);
421         return builder.build();
422     }
423 
424     /**
425      * Compare each of the passed in responsibilities with the given responsibilityDetails.  Those that
426      * match are added to the result list.
427      */
428     private List<Responsibility> getMatchingResponsibilities(List<Responsibility> responsibilities, Map<String, String> responsibilityDetails) {
429         // if no details passed, assume that all match
430         if (responsibilityDetails == null || responsibilityDetails.isEmpty()) {
431             return responsibilities;
432         }
433 
434         final List<Responsibility> applicableResponsibilities = new ArrayList<Responsibility>();
435         // otherwise, attempt to match the permission details
436         // build a map of the template IDs to the type services
437         Map<String, ResponsibilityTypeService> responsibilityTypeServices = getResponsibilityTypeServicesByTemplateId(responsibilities);
438         // build a map of permissions by template ID
439         Map<String, List<Responsibility>> responsibilityMap = groupResponsibilitiesByTemplate(responsibilities);
440         // loop over the different templates, matching all of the same template against the type
441         // service at once
442         for (Map.Entry<String, List<Responsibility>> respEntry : responsibilityMap.entrySet()) {
443             ResponsibilityTypeService responsibilityTypeService = responsibilityTypeServices.get(respEntry.getKey());
444             List<Responsibility> responsibilityInfos = respEntry.getValue();
445             if (responsibilityTypeService == null) {
446                 responsibilityTypeService = defaultResponsibilityTypeService;
447             }
448             applicableResponsibilities.addAll(responsibilityTypeService.getMatchingResponsibilities(responsibilityDetails, responsibilityInfos));
449         }
450         return Collections.unmodifiableList(applicableResponsibilities);
451     }
452 
453     private Map<String, ResponsibilityTypeService> getResponsibilityTypeServicesByTemplateId(Collection<Responsibility> responsibilities) {
454         Map<String, ResponsibilityTypeService> responsibilityTypeServices = new HashMap<String, ResponsibilityTypeService>(responsibilities.size());
455         for (Responsibility responsibility : responsibilities) {
456             final Template t = responsibility.getTemplate();
457             final KimType type = kimTypeInfoService.getKimType(t.getKimTypeId());
458 
459             final String serviceName = type.getServiceName();
460             if (serviceName != null) {
461                 ResponsibilityTypeService responsibiltyTypeService = GlobalResourceLoader.getService(QName.valueOf(serviceName));
462                 if (responsibiltyTypeService != null) {
463                     responsibilityTypeServices.put(responsibility.getTemplate().getId(), responsibiltyTypeService);
464                 } else {
465                     responsibilityTypeServices.put(responsibility.getTemplate().getId(), defaultResponsibilityTypeService);
466                 }
467             }
468         }
469         return Collections.unmodifiableMap(responsibilityTypeServices);
470     }
471 
472     private Map<String, List<Responsibility>> groupResponsibilitiesByTemplate(Collection<Responsibility> responsibilities) {
473         final Map<String, List<Responsibility>> results = new HashMap<String, List<Responsibility>>();
474         for (Responsibility responsibility : responsibilities) {
475             List<Responsibility> responsibilityInfos = results.get(responsibility.getTemplate().getId());
476             if (responsibilityInfos == null) {
477                 responsibilityInfos = new ArrayList<Responsibility>();
478                 results.put(responsibility.getTemplate().getId(), responsibilityInfos);
479             }
480             responsibilityInfos.add(responsibility);
481         }
482         return Collections.unmodifiableMap(results);
483     }
484 
485     private List<String> getRoleIdsForResponsibilities(Collection<String> ids) {
486         final List<String> roleIds = getRoleIdsForPredicate(and(in("responsibilityId", ids.toArray()), equal("active", Boolean.TRUE)));
487 
488         return Collections.unmodifiableList(roleIds);
489     }
490 
491     private List<String> getRoleIdsForPredicate(Predicate p) {
492         QueryResults<RoleResponsibilityBo> qr =
493                 getDataObjectService().findMatching(RoleResponsibilityBo.class,
494                         QueryByCriteria.Builder.fromPredicates(p));
495         final List<String> roleIds = new ArrayList<String>();
496         for (RoleResponsibilityBo bo : qr.getResults()) {
497             roleIds.add(bo.getRoleId());
498         }
499         return Collections.unmodifiableList(roleIds);
500     }
501 
502 
503     @Override
504     public List<Responsibility> findResponsibilitiesByTemplate(String namespaceCode, String templateName) {
505 
506         final Map<String, Object> criteria = new HashMap<String, Object>();
507         criteria.put("template.namespaceCode", namespaceCode); 
508         criteria.put("template.name", templateName); 
509         criteria.put("active", Boolean.TRUE);
510 
511         QueryResults<ResponsibilityBo> bos =
512                 getDataObjectService().findMatching(ResponsibilityBo.class,
513                         QueryByCriteria.Builder.andAttributes(criteria).build());
514         final List<Responsibility> ims = new ArrayList<Responsibility>();
515         if (bos != null && bos.getResults() != null) {
516             for (ResponsibilityBo bo : bos.getResults()) {
517                 if (bo != null) {
518                     ims.add(ResponsibilityBo.to(bo));
519                 }
520             }
521         }
522 
523         return Collections.unmodifiableList(ims);
524     }
525 
526     public void setDefaultResponsibilityTypeService(final ResponsibilityTypeService defaultResponsibilityTypeService) {
527         this.defaultResponsibilityTypeService = defaultResponsibilityTypeService;
528     }
529 
530     public void setKimTypeInfoService(final KimTypeInfoService kimTypeInfoService) {
531         this.kimTypeInfoService = kimTypeInfoService;
532     }
533 
534     public void setRoleService(final RoleService roleService) {
535         this.roleService = roleService;
536     }
537 
538     protected void logResponsibilityCheck(String namespaceCode, String responsibilityName, 
539                          Map<String, String> responsibilityDetails, Map<String, String> qualification ) {
540         StringBuilder sb = new StringBuilder();
541         sb.append(  '\n' );
542         sb.append( "Get Resp Actions: " ).append( namespaceCode ).append( "/" ).append( responsibilityName ).append( '\n' );
543         sb.append( "             Details:\n" );
544         if ( responsibilityDetails != null ) {
545             sb.append( responsibilityDetails );
546         } else {
547             sb.append( "                         [null]\n" );
548         }
549         sb.append( "             Qualifiers:\n" );
550         if ( qualification != null ) {
551             sb.append( qualification );
552         } else {
553             sb.append( "                         [null]\n" );
554         }
555         if (LOG.isTraceEnabled()) { 
556             LOG.trace( sb.append(ExceptionUtils.getStackTrace(new Throwable())));
557         } else {
558             LOG.debug(sb.toString());
559         }
560     }
561     
562     private void incomingParamCheck(Object object, String name) {
563         if (object == null) {
564             throw new RiceIllegalArgumentException(name + " was null");
565         } else if (object instanceof String
566                 && StringUtils.isBlank((String) object)) {
567             throw new RiceIllegalArgumentException(name + " was blank");
568         }
569     }
570 
571 
572     public DataObjectService getDataObjectService() {
573         return dataObjectService;
574     }
575 
576     public void setDataObjectService(DataObjectService dataObjectService) {
577         this.dataObjectService = dataObjectService;
578     }
579 }