Use prepareStatement() in RoleStore.deleteRole()
[aaa.git] / aaa-idm-store-h2 / src / main / java / org / opendaylight / aaa / datastore / h2 / RoleStore.java
1 /*
2  * Copyright (c) 2014, 2017 Hewlett-Packard Development Company, L.P. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.aaa.datastore.h2;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.sql.Connection;
13 import java.sql.PreparedStatement;
14 import java.sql.ResultSet;
15 import java.sql.SQLException;
16 import org.opendaylight.aaa.api.IDMStoreUtil;
17 import org.opendaylight.aaa.api.model.Role;
18 import org.opendaylight.aaa.api.model.Roles;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Store for roles.
24  *
25  * @author peter.mellquist@hp.com
26  *
27  */
28 public class RoleStore extends AbstractStore<Role> {
29     private static final Logger LOG = LoggerFactory.getLogger(RoleStore.class);
30
31     public static final String SQL_ID = "roleid";
32     protected static final String SQL_DOMAIN_ID = "domainid";
33     public static final String SQL_NAME = "name";
34     public static final String SQL_DESCR = "description";
35     private static final String TABLE_NAME = "ROLES";
36
37     public RoleStore(final ConnectionProvider dbConnectionFactory) {
38         super(dbConnectionFactory, TABLE_NAME);
39     }
40
41     @Override
42     protected String getTableCreationStatement() {
43         return "CREATE TABLE ROLES " + "(roleid     VARCHAR(128)   PRIMARY KEY,"
44                 + "name        VARCHAR(128)   NOT NULL, " + "domainid    VARCHAR(128)   NOT NULL, "
45                 + "description VARCHAR(128)      NOT NULL)";
46     }
47
48     @Override
49     protected Role fromResultSet(final ResultSet rs) throws SQLException {
50         Role role = new Role();
51         try {
52             role.setRoleid(rs.getString(SQL_ID));
53             role.setDomainid(rs.getString(SQL_DOMAIN_ID));
54             role.setName(rs.getString(SQL_NAME));
55             role.setDescription(rs.getString(SQL_DESCR));
56         } catch (SQLException sqle) {
57             LOG.error("SQL Exception: ", sqle);
58             throw sqle;
59         }
60         return role;
61     }
62
63     public Roles getRoles() throws StoreException {
64         Roles roles = new Roles();
65         roles.setRoles(listAll());
66         return roles;
67     }
68
69     protected Role getRole(final String id) throws StoreException {
70         try (Connection conn = dbConnect();
71                 PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM ROLES WHERE roleid = ? ")) {
72             pstmt.setString(1, id);
73             LOG.debug("query string: {}", pstmt);
74             return firstFromStatement(pstmt);
75         } catch (SQLException s) {
76             throw new StoreException("SQL Exception: " + s);
77         }
78     }
79
80     protected Role createRole(final Role role) throws StoreException {
81         requireNonNull(role);
82         requireNonNull(role.getName());
83         requireNonNull(role.getDomainid());
84         String query = "insert into roles (roleid,domainid,name,description) values(?,?,?,?)";
85         try (Connection conn = dbConnect(); PreparedStatement statement = conn.prepareStatement(query)) {
86             role.setRoleid(IDMStoreUtil.createRoleid(role.getName(), role.getDomainid()));
87             statement.setString(1, role.getRoleid());
88             statement.setString(2, role.getDomainid());
89             statement.setString(3, role.getName());
90             statement.setString(4, role.getDescription());
91             int affectedRows = statement.executeUpdate();
92             if (affectedRows == 0) {
93                 throw new StoreException("Creating role failed, no rows affected.");
94             }
95             return role;
96         } catch (SQLException s) {
97             throw new StoreException("SQL Exception : " + s);
98         }
99     }
100
101     protected Role putRole(final Role role) throws StoreException {
102
103         Role savedRole = getRole(role.getRoleid());
104         if (savedRole == null) {
105             return null;
106         }
107
108         if (role.getDescription() != null) {
109             savedRole.setDescription(role.getDescription());
110         }
111         if (role.getName() != null) {
112             savedRole.setName(role.getName());
113         }
114
115         String query = "UPDATE roles SET description = ? WHERE roleid = ?";
116         try (Connection conn = dbConnect(); PreparedStatement statement = conn.prepareStatement(query)) {
117             statement.setString(1, savedRole.getDescription());
118             statement.setString(2, savedRole.getRoleid());
119             statement.executeUpdate();
120         } catch (SQLException s) {
121             throw new StoreException("SQL Exception : " + s);
122         }
123
124         return savedRole;
125     }
126
127     protected Role deleteRole(final String roleid) throws StoreException {
128         Role savedRole = getRole(roleid);
129         if (savedRole == null) {
130             return null;
131         }
132
133         String query = "DELETE FROM ROLES WHERE roleid = ?";
134         try (Connection conn = dbConnect(); PreparedStatement statement = conn.prepareStatement(query)) {
135             statement.setString(1, roleid);
136             int deleteCount = statement.executeUpdate();
137             LOG.debug("deleted {} records", deleteCount);
138             return savedRole;
139         } catch (SQLException s) {
140             throw new StoreException("SQL Exception : " + s);
141         }
142     }
143 }