View Javadoc

1   /*
2    * Copyright 2008 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.dao.impl;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.List;
21  
22  import org.apache.ojb.broker.query.Criteria;
23  import org.apache.ojb.broker.query.Query;
24  import org.apache.ojb.broker.query.QueryFactory;
25  import org.kuali.rice.kim.bo.role.KimResponsibility;
26  import org.kuali.rice.kim.bo.role.impl.RoleResponsibilityActionImpl;
27  import org.kuali.rice.kim.bo.role.impl.RoleResponsibilityImpl;
28  import org.kuali.rice.kim.dao.KimResponsibilityDao;
29  import org.kuali.rice.kim.util.KIMPropertyConstants;
30  import org.kuali.rice.kim.util.KimConstants;
31  import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
32  import org.kuali.rice.kns.util.KNSPropertyConstants;
33  
34  /**
35   * This is a description of what this class does - kellerj don't forget to fill this in. 
36   * 
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   *
39   */
40  public class KimResponsibilityDaoOjb extends PlatformAwareDaoBaseOjb implements KimResponsibilityDao {
41  
42  	/**
43  	 * @see org.kuali.rice.kim.dao.KimResponsibilityDao#getRoleIdsForResponsibilities(java.util.Collection)
44  	 */
45  	@SuppressWarnings("unchecked")
46  	public List<String> getRoleIdsForResponsibilities(Collection<? extends KimResponsibility> responsibilities) {
47  		List<String> responsibilityIds = new ArrayList<String>( responsibilities.size() );
48  		for ( KimResponsibility kp : responsibilities ) {
49  			responsibilityIds.add( kp.getResponsibilityId() );
50  		}
51  		Criteria c = new Criteria();
52  		c.addIn( KimConstants.PrimaryKeyConstants.RESPONSIBILITY_ID, responsibilityIds );
53  		c.addEqualTo( KNSPropertyConstants.ACTIVE, true );
54  		
55  		Query query = QueryFactory.newQuery( RoleResponsibilityImpl.class, c, true );
56  		Collection<RoleResponsibilityImpl> coll = getPersistenceBrokerTemplate().getCollectionByQuery(query);
57  		List<String> roleIds = new ArrayList<String>( coll.size() );
58  		for ( RoleResponsibilityImpl rp : coll ) {
59  			roleIds.add( rp.getRoleId() );
60  		}
61  		return roleIds;
62  	}
63  	
64  	/**
65  	 * @see org.kuali.rice.kim.dao.KimResponsibilityDao#getRoleIdsForResponsibility(org.kuali.rice.kim.bo.role.impl.KimResponsibilityImpl)
66  	 */
67  	@SuppressWarnings("unchecked")
68  	public List<String> getRoleIdsForResponsibility( KimResponsibility responsibility) {
69  		Criteria c = new Criteria();
70  		c.addEqualTo( KimConstants.PrimaryKeyConstants.RESPONSIBILITY_ID, responsibility.getResponsibilityId() );
71  		c.addEqualTo( KNSPropertyConstants.ACTIVE, true );
72  		
73  		Query query = QueryFactory.newQuery( RoleResponsibilityImpl.class, c, true );
74  		Collection<RoleResponsibilityImpl> coll = getPersistenceBrokerTemplate().getCollectionByQuery(query);
75  		List<String> roleIds = new ArrayList<String>( coll.size() );
76  		for ( RoleResponsibilityImpl rp : coll ) {
77  			roleIds.add( rp.getRoleId() );
78  		}
79  		return roleIds;
80  	}
81  
82  	/**
83  	 * @see org.kuali.rice.kim.dao.KimResponsibilityDao#getResponsibilityAction(String, String)
84  	 */
85  	@SuppressWarnings("unchecked")
86  	public RoleResponsibilityActionImpl getResponsibilityAction(String roleId, String responsibilityId, String roleMemberId ) {
87  		if ( roleMemberId == null || responsibilityId == null ) {
88  			return null;
89  		}
90  		
91  		Criteria c = new Criteria();
92  		c.addEqualTo( "roleResponsibility.responsibilityId", responsibilityId );
93  		c.addEqualTo( "roleResponsibility.roleId", roleId );
94  		c.addEqualTo( "roleResponsibility.active", true );
95  		Criteria idCriteria = new Criteria();
96  		idCriteria.addEqualTo( KIMPropertyConstants.RoleMember.ROLE_MEMBER_ID, roleMemberId );
97  		// also handle when roleMemberId is "*" in table
98  		Criteria starCrit = new Criteria();
99  		starCrit.addEqualTo( KIMPropertyConstants.RoleMember.ROLE_MEMBER_ID, "*" );
100 		idCriteria.addOrCriteria( starCrit );
101 		c.addAndCriteria( idCriteria );
102 		
103 		Query query = QueryFactory.newQuery( RoleResponsibilityActionImpl.class, c );
104 		Collection<RoleResponsibilityActionImpl> coll = getPersistenceBrokerTemplate().getCollectionByQuery(query);
105 		RoleResponsibilityActionImpl result = null;
106 		if ( coll.size() == 0 ) {
107 			// ok, an exact match failed, attempt to pull where the role_rsp_id is "*"
108 			// but, at this point, we would require an exact match on the role member ID
109 			c = new Criteria();
110 			c.addEqualTo( "roleResponsibilityId", "*" );
111 			c.addEqualTo( KIMPropertyConstants.RoleMember.ROLE_MEMBER_ID, roleMemberId );
112 			query = QueryFactory.newQuery( RoleResponsibilityActionImpl.class, c );
113 			coll = getPersistenceBrokerTemplate().getCollectionByQuery(query);
114 			if ( coll.size() != 0 ) {
115 				result = coll.iterator().next();
116 			}
117 		} else {
118 			result = coll.iterator().next();
119 		}
120 		return result;
121 	}
122 }