001 /**
002 * Copyright 2005-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.kim.service.dao.impl;
017
018 import org.kuali.rice.core.api.membership.MemberType;
019 import org.kuali.rice.kew.api.KewApiConstants;
020 import org.kuali.rice.kim.api.KimConstants;
021 import org.kuali.rice.kim.api.group.Group;
022 import org.kuali.rice.kim.api.identity.name.EntityName;
023 import org.kuali.rice.krad.util.KRADConstants;
024 import org.springframework.dao.DataAccessException;
025 import org.springframework.jdbc.core.JdbcTemplate;
026 import org.springframework.jdbc.core.PreparedStatementCallback;
027 import org.springframework.jdbc.core.PreparedStatementCreator;
028 import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
029 import org.kuali.rice.kim.service.dao.UiDocumentServiceDAO;
030
031 import javax.sql.DataSource;
032 import java.sql.Connection;
033 import java.sql.PreparedStatement;
034 import java.sql.ResultSet;
035 import java.sql.SQLException;
036 import java.util.HashMap;
037 import java.util.Map;
038
039 /**
040 * Spring JdbcTemplate implementation of UiDocumentServiceDAO
041 *
042 * @author Kuali Rice Team (rice.collab@kuali.org)
043 *
044 */
045 public class UiDocumentServiceDAOJdbcImpl implements UiDocumentServiceDAO {
046
047 public static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(UiDocumentServiceDAOJdbcImpl.class);
048
049 private DataSource dataSource;
050
051 public void setDataSource(DataSource dataSource) {
052 this.dataSource = new TransactionAwareDataSourceProxy(dataSource);
053 }
054
055 @Override
056 public Map<String, Group> findGroupsForRole(final String roleId) {
057 final Map<String, Group> roleGroupMembers = new HashMap<String, Group>();
058
059 JdbcTemplate template = new JdbcTemplate(dataSource);
060 Map<String, Group> results = template.execute(new PreparedStatementCreator() {
061
062 public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
063
064 String sql = " SELECT GRP_ID, GRP_NM, NMSPC_CD, KIM_TYP_ID, ACTV_IND" +
065 " FROM KRIM_GRP_T G, KRIM_ROLE_MBR_T RM WHERE" +
066 " G.GRP_ID = RM.MBR_ID AND GRP_ID IN" +
067 " (SELECT MBR_ID FROM KRIM_ROLE_MBR_T WHERE MBR_TYP_CD = '" +
068 MemberType.GROUP.getCode() + "' AND ROLE_ID = '" + roleId + "')";
069
070 LOG.debug("Query to find Entity Names for role " + roleId + ":" + sql);
071
072 PreparedStatement statement = connection.prepareStatement(sql);
073 return statement;
074 }
075 }, new PreparedStatementCallback<Map<String,Group>>() {
076 public Map<String,Group> doInPreparedStatement(
077 PreparedStatement statement) throws SQLException, DataAccessException {
078 ResultSet rs = statement.executeQuery();
079 try {
080 while (rs.next()) {
081
082 String groupId = rs.getString(1);
083 String groupName = rs.getString(2);
084 String groupNameSpace = rs.getString(3);
085 String kimTypeId = rs.getString(4);
086 String activeInd = rs.getString(5);
087 Group.Builder builder = Group.Builder.create(groupNameSpace, groupName, kimTypeId);
088 builder.setId(groupId);
089 builder.build();
090 if (activeInd.equalsIgnoreCase(KewApiConstants.ACTIVE_CD)) {
091 builder.setActive(true);
092 }
093 roleGroupMembers.put(groupId, builder.build());
094 }
095 } finally {
096 if (rs != null) {
097 rs.close();
098 }
099 }
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 }