View Javadoc

1   /**
2    * Copyright 2005-2013 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.service.dao.impl;
17  
18  import org.kuali.rice.core.api.membership.MemberType;
19  import org.kuali.rice.kew.api.KewApiConstants;
20  import org.kuali.rice.kim.api.KimConstants;
21  import org.kuali.rice.kim.api.group.Group;
22  import org.kuali.rice.kim.api.identity.name.EntityName;
23  import org.kuali.rice.krad.util.KRADConstants;
24  import org.springframework.dao.DataAccessException;
25  import org.springframework.jdbc.core.JdbcTemplate;
26  import org.springframework.jdbc.core.PreparedStatementCallback;
27  import org.springframework.jdbc.core.PreparedStatementCreator;
28  import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
29  import org.kuali.rice.kim.service.dao.UiDocumentServiceDAO;
30  
31  import javax.sql.DataSource;
32  import java.sql.Connection;
33  import java.sql.PreparedStatement;
34  import java.sql.ResultSet;
35  import java.sql.SQLException;
36  import java.util.HashMap;
37  import java.util.Map;
38  
39  /**
40   * Spring JdbcTemplate implementation of UiDocumentServiceDAO
41   *
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   *
44   */
45  public class UiDocumentServiceDAOJdbcImpl implements UiDocumentServiceDAO {
46  
47      public static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(UiDocumentServiceDAOJdbcImpl.class);
48  
49      private DataSource dataSource;
50  
51      public void setDataSource(DataSource dataSource) {
52          this.dataSource = new TransactionAwareDataSourceProxy(dataSource);
53      }
54  
55      @Override
56      public Map<String, Group> findGroupsForRole(final String roleId) {
57          final Map<String, Group> roleGroupMembers = new HashMap<String, Group>();
58  
59          JdbcTemplate template = new JdbcTemplate(dataSource);
60          Map<String, Group> results  = template.execute(new PreparedStatementCreator() {
61  
62              public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
63  
64                  String sql = " SELECT GRP_ID, GRP_NM, NMSPC_CD, KIM_TYP_ID, ACTV_IND" +
65                      " FROM KRIM_GRP_T G, KRIM_ROLE_MBR_T RM WHERE" +
66                      " G.GRP_ID = RM.MBR_ID AND GRP_ID IN" +
67                      " (SELECT MBR_ID FROM KRIM_ROLE_MBR_T WHERE MBR_TYP_CD = '" +
68                      MemberType.GROUP.getCode() + "' AND ROLE_ID = '" + roleId + "')";
69  
70                      LOG.debug("Query to find Entity Names for role " + roleId + ":" + sql);
71  
72                  PreparedStatement statement = connection.prepareStatement(sql);
73                  return statement;
74              }
75          }, new PreparedStatementCallback<Map<String,Group>>() {
76              public Map<String,Group> doInPreparedStatement(
77                  PreparedStatement statement) throws SQLException, DataAccessException {
78                      ResultSet rs = statement.executeQuery();
79                      try {
80                          while (rs.next()) {
81  
82                              String groupId = rs.getString(1);
83                              String groupName = rs.getString(2);
84                              String groupNameSpace = rs.getString(3);
85                              String kimTypeId = rs.getString(4);
86                              String activeInd = rs.getString(5);
87                              Group.Builder builder = Group.Builder.create(groupNameSpace, groupName, kimTypeId);
88                              builder.setId(groupId);
89                              builder.build();
90                              if (activeInd.equalsIgnoreCase(KewApiConstants.ACTIVE_CD)) {
91                                  builder.setActive(true);
92                              }
93                              roleGroupMembers.put(groupId, builder.build());
94                          }
95                      } finally {
96                          if (rs != null) {
97                              rs.close();
98                          }
99                      }
100                     return roleGroupMembers;
101                 }
102             }
103         );
104         return roleGroupMembers;
105     }
106 
107     @Override
108     public Map<String, EntityName> findEntityNamesForRole(final String roleId) {
109         final Map<String, EntityName> entityNamesForPrincipals = new HashMap<String, EntityName>();
110 
111         JdbcTemplate template = new JdbcTemplate(dataSource);
112         Map<String, EntityName> results  = template.execute(new PreparedStatementCreator() {
113             public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
114 
115                 String sql = "SELECT EN.ENTITY_NM_ID, EN.ENTITY_ID, EN.FIRST_NM, EN.LAST_NM, EP.SUPPRESS_NM_IND FROM" +
116                     " KRIM_PRNCPL_T KP, KRIM_ENTITY_NM_T EN LEFT JOIN KRIM_ENTITY_PRIV_PREF_T EP" +
117                     " ON EN.ENTITY_ID = EP.ENTITY_ID" +
118                     " WHERE EN.ACTV_IND='" + KewApiConstants.ACTIVE_CD +
119                     "' AND EN.DFLT_IND='"+ KewApiConstants.ACTIVE_CD +
120                     "' AND EN.NM_TYP_CD='"+ KimConstants.NameTypes.PREFERRED +
121                     "' AND EN.ENTITY_ID = KP.ENTITY_ID " +
122                     " AND KP.PRNCPL_ID IN (SELECT MBR_ID FROM KRIM_ROLE_MBR_T WHERE ROLE_ID = '" + roleId + "')";
123                     LOG.debug("Query to find Entity Names for role " + roleId + ":" + sql);
124 
125                 PreparedStatement statement = connection.prepareStatement(sql);
126                 return statement;
127             }
128         }, new PreparedStatementCallback<Map<String,EntityName>>() {
129             public Map<String,EntityName> doInPreparedStatement(
130                 PreparedStatement statement) throws SQLException, DataAccessException {
131                     ResultSet rs = statement.executeQuery();
132                     try {
133                         while (rs.next()) {
134                             String id = rs.getString(1);
135                             String entityId = rs.getString(2);
136                             String firstName = rs.getString(3);
137                             String lastName = rs.getString(4);
138                             String suppressName = rs.getString(5);
139                             boolean suppressNameBoolean = false;
140                             if (KRADConstants.YES_INDICATOR_VALUE.equalsIgnoreCase(suppressName))  {
141                                 suppressNameBoolean = true;
142                             }
143                             EntityName.Builder builder = EntityName.Builder.create(id, entityId, firstName, lastName, suppressNameBoolean);
144                             builder.setActive(true);
145                             builder.setDefaultValue(true);
146                             EntityName entityName = builder.build();
147                             entityNamesForPrincipals.put(entityName.getEntityId(), entityName);
148                         }
149                     } finally {
150                         if (rs != null) {
151                             rs.close();
152                         }
153                     }
154                         return entityNamesForPrincipals;
155                     }
156                 }
157         );
158         return entityNamesForPrincipals;
159     }
160 }