|
4 | 4 | import java.sql.Connection; |
5 | 5 | import java.sql.ResultSet; |
6 | 6 | import java.sql.SQLException; |
| 7 | +import java.sql.SQLIntegrityConstraintViolationException; |
| 8 | +import java.util.ArrayList; |
| 9 | + |
| 10 | +import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException; |
7 | 11 |
|
8 | 12 | import database.bean.Admin; |
| 13 | +import exception.InvalidPrimaryKeyException; |
9 | 14 |
|
10 | 15 | public class AdminManager |
11 | 16 | { |
12 | | - /**This is the query for that inserts an {@code} Admin to the database*/ |
13 | | - private static final String INSERT_SQL = "{CALL insertAdmin(?, ? ) } "; |
14 | | - |
15 | | - /**This is the query for that update an {@code} Admin to the database*/ |
16 | | - private static final String UPDATE_PASSWORD = "{CALL updateAdminPassword(?, ?, ? ) } "; |
17 | | - |
18 | | - /**This is the query for that update an {@code} Admin to the database*/ |
19 | 17 | private static final String UPDATE_USERNAME = "{CALL updateAdminUserName(?, ?, ? ) } "; |
20 | | - |
21 | 18 |
|
| 19 | + |
22 | 20 | /** |
23 | 21 | * Inserts a new {@code Admin } to the database.<br> |
24 | 22 | * Returns {@code true } only when the insert was successful. |
25 | 23 | * @param admin the {@code Admin} object to be inserted |
26 | 24 | * @return {@code true } only when the {@code Admin} was added successfully |
| 25 | + * @throws InvalidPrimaryKeyException when the {@code Admin } object is invalid |
27 | 26 | */ |
28 | | - public static boolean insertAdmin( Admin admin ) |
| 27 | + public static boolean insertAdmin( Admin admin ) throws InvalidPrimaryKeyException |
29 | 28 | { |
30 | | - if ( admin != null ) |
| 29 | + if( !validateAdmin( admin ) ) |
31 | 30 | { |
32 | | - Connection conn = ConnectionManager.getInstance().getConnection(); |
| 31 | + throw new InvalidPrimaryKeyException ( "The admin object cannot contain spaces and it cannot be null"); |
| 32 | + } |
33 | 33 |
|
34 | | - try( |
35 | | - CallableStatement statement = |
36 | | - conn.prepareCall( |
37 | | - INSERT_SQL, |
38 | | - ResultSet.TYPE_FORWARD_ONLY, |
39 | | - ResultSet.CONCUR_READ_ONLY); |
| 34 | + final String INSERT_CALL = "{CALL insertAdmin(?, ? ) } "; |
| 35 | + Connection conn = ConnectionManager.getInstance().getConnection(); |
40 | 36 |
|
| 37 | + try( |
| 38 | + CallableStatement statement = |
| 39 | + conn.prepareCall( |
| 40 | + INSERT_CALL, |
| 41 | + ResultSet.TYPE_FORWARD_ONLY, |
| 42 | + ResultSet.CONCUR_READ_ONLY); |
41 | 43 |
|
42 | | - ) |
43 | | - { |
44 | | - statement.setString(1, admin.getUsername() ); |
45 | | - statement.setString(2, admin.getPassword() ); |
46 | 44 |
|
47 | | - int affected = statement.executeUpdate(); |
| 45 | + ) |
| 46 | + { |
| 47 | + statement.setString(1, admin.getUsername() ); |
| 48 | + statement.setString(2, admin.getPassword() ); |
48 | 49 |
|
49 | | - if( affected > 0 ) |
50 | | - return true; |
| 50 | + int affected = statement.executeUpdate(); |
51 | 51 |
|
52 | | - } |
53 | | - catch (SQLException e) |
54 | | - { |
55 | | - // TODO Auto-generated catch block |
56 | | - e.printStackTrace(); |
57 | | - return false; |
58 | | - } |
59 | | - finally { |
60 | | - ConnectionManager.close(); |
61 | | - } |
| 52 | + if( affected > 0 ) |
| 53 | + return true; |
62 | 54 |
|
63 | 55 | } |
| 56 | + catch (SQLException e) |
| 57 | + { |
| 58 | + // TODO Auto-generated catch block |
| 59 | + e.printStackTrace(); |
| 60 | + return false; |
| 61 | + } |
| 62 | + finally { |
| 63 | + ConnectionManager.close(); |
| 64 | + } |
| 65 | + |
64 | 66 | return false; |
65 | 67 | } |
66 | 68 |
|
67 | 69 | /** |
68 | | - * Updates the password of an existing {@code Admin }.<br> |
69 | | - * Returns {@code true } only when the update was successful. |
70 | | - * Note that the changes would not be commited if the connection object's |
71 | | - * auto-commiting is set to {@code false} |
72 | | - * @param admin the {@code Admin} object to be inserted |
73 | | - * @return {@code true } only when the {@code Admin} was added successfully |
74 | | - */ |
75 | | - public static boolean updateAdminPassword( Admin oldAdmin, String newPassword ) |
| 70 | + * Updates the password of an existing {@code Admin }.<br> |
| 71 | + * Returns {@code true } only when the update was successful. |
| 72 | + * Note that the changes would not be commited if the connection object's |
| 73 | + * auto-commiting is set to {@code false} <br> |
| 74 | + * This method also updates the {@code Admin } object that is passed as |
| 75 | + * an argument if the update was successful |
| 76 | + * |
| 77 | + * @param admin the {@code Admin} object to be inserted |
| 78 | + * @return {@code true } only when the {@code Admin} was added successfully |
| 79 | + * @throws InvalidPrimaryKeyException when the {@code Admin } is null or its username contians spaces |
| 80 | + */ |
| 81 | + public static boolean updateAdminPassword( Admin oldAdmin, String newPassword ) throws InvalidPrimaryKeyException |
76 | 82 | { |
77 | | - if ( oldAdmin != null ) |
| 83 | + if( !validateAdmin( oldAdmin ) ) |
78 | 84 | { |
79 | | - Connection conn = ConnectionManager.getInstance().getConnection(); |
80 | | - |
81 | | - try( |
82 | | - CallableStatement statement = conn.prepareCall( |
83 | | - UPDATE_PASSWORD, |
84 | | - ResultSet.TYPE_FORWARD_ONLY, |
85 | | - ResultSet.CONCUR_READ_ONLY); |
86 | | - ) |
87 | | - { |
88 | | - statement.setString(1, oldAdmin.getUsername() ); |
89 | | - statement.setString(2, oldAdmin.getPassword() ); |
90 | | - statement.setString(3, newPassword ); |
| 85 | + throw new InvalidPrimaryKeyException |
| 86 | + ( "The admin object cannot contain spaces and it cannot be null"); |
| 87 | + } |
91 | 88 |
|
92 | | - int affected = statement.executeUpdate(); |
| 89 | + final String UPDATE_PASSWORD = "{CALL updateAdminPassword(?, ?, ? ) } "; |
| 90 | + |
| 91 | + Connection conn = ConnectionManager.getInstance().getConnection(); |
93 | 92 |
|
94 | | - if( affected > 0 ) |
95 | | - return true; |
| 93 | + try( |
| 94 | + CallableStatement statement = conn.prepareCall( |
| 95 | + UPDATE_PASSWORD, |
| 96 | + ResultSet.TYPE_FORWARD_ONLY, |
| 97 | + ResultSet.CONCUR_READ_ONLY); |
| 98 | + ) |
| 99 | + { |
| 100 | + statement.setString(1, oldAdmin.getUsername() ); |
| 101 | + statement.setString(2, oldAdmin.getPassword() ); |
| 102 | + statement.setString(3, newPassword ); |
96 | 103 |
|
97 | | - } |
98 | | - catch (SQLException e) |
99 | | - { |
100 | | - // TODO Auto-generated catch block |
101 | | - e.printStackTrace(); |
102 | | - return false; |
103 | | - } |
104 | | - finally { |
105 | | - ConnectionManager.close(); |
| 104 | + int affected = statement.executeUpdate(); |
| 105 | + |
| 106 | + if( affected > 0 ){ |
| 107 | + oldAdmin.setPassword(newPassword); |
| 108 | + return true; |
106 | 109 | } |
107 | 110 |
|
| 111 | + |
| 112 | + } |
| 113 | + catch (SQLException e) |
| 114 | + { |
| 115 | + // TODO Auto-generated catch block |
| 116 | + e.printStackTrace(); |
| 117 | + return false; |
| 118 | + } |
| 119 | + finally { |
| 120 | + ConnectionManager.close(); |
108 | 121 | } |
| 122 | + |
109 | 123 | return false; |
110 | 124 | } |
111 | 125 |
|
112 | 126 | /** |
113 | | - * Updates the username of an existing {@code Admin} |
| 127 | + * Updates the username of an existing {@code Admin}<br> |
| 128 | + * The method also updates the {@code Admin } object that is passed as |
| 129 | + * an argument if the update was successful |
| 130 | + * @throws InvalidPrimaryKeyException |
114 | 131 | */ |
115 | | - public static boolean updateAdminUsername( Admin oldAdmin, String newUsername ) |
| 132 | + public static boolean updateAdminUsername( Admin oldAdmin, String newUsername ) throws InvalidPrimaryKeyException |
116 | 133 | { |
117 | | - if ( oldAdmin != null ) |
| 134 | + if( !validateAdmin( oldAdmin ) || newUsername.contains( " " )) |
118 | 135 | { |
119 | | - Connection conn = ConnectionManager.getInstance().getConnection(); |
120 | | - |
121 | | - try( |
122 | | - CallableStatement statement = conn.prepareCall( |
123 | | - UPDATE_USERNAME, |
124 | | - ResultSet.TYPE_FORWARD_ONLY, |
125 | | - ResultSet.CONCUR_READ_ONLY); |
126 | | - ) |
127 | | - { |
128 | | - statement.setString(1, oldAdmin.getUsername() ); |
129 | | - statement.setString(2, oldAdmin.getPassword() ); |
130 | | - statement.setString(3, newUsername ); |
| 136 | + throw new InvalidPrimaryKeyException |
| 137 | + ( "The admin object cannot contain spaces and it cannot be null"); |
| 138 | + } |
| 139 | + |
| 140 | + Connection conn = ConnectionManager.getInstance().getConnection(); |
131 | 141 |
|
132 | | - int affected = statement.executeUpdate(); |
| 142 | + try( |
| 143 | + CallableStatement statement = conn.prepareCall( |
| 144 | + UPDATE_USERNAME, |
| 145 | + ResultSet.TYPE_FORWARD_ONLY, |
| 146 | + ResultSet.CONCUR_READ_ONLY); |
| 147 | + ) |
| 148 | + { |
| 149 | + statement.setString(1, oldAdmin.getUsername() ); |
| 150 | + statement.setString(2, oldAdmin.getPassword() ); |
| 151 | + statement.setString(3, newUsername ); |
133 | 152 |
|
134 | | - if( affected > 0 ) |
135 | | - return true; |
| 153 | + int affected = statement.executeUpdate(); |
136 | 154 |
|
137 | | - } |
138 | | - catch (SQLException e) |
| 155 | + if( affected > 0 ) |
139 | 156 | { |
140 | | - // TODO Auto-generated catch block |
141 | | - e.printStackTrace(); |
142 | | - return false; |
| 157 | + oldAdmin.setUsername( newUsername ); |
| 158 | + return true; |
143 | 159 | } |
144 | | - finally { |
145 | | - ConnectionManager.close(); |
| 160 | + |
| 161 | + |
| 162 | + } |
| 163 | + catch( SQLIntegrityConstraintViolationException e ){ |
| 164 | + throw new InvalidPrimaryKeyException( e.getMessage() ); |
| 165 | + } |
| 166 | + catch (SQLException e) |
| 167 | + { |
| 168 | + // TODO Auto-generated catch block |
| 169 | + e.printStackTrace(); |
| 170 | + return false; |
| 171 | + } |
| 172 | + finally { |
| 173 | + ConnectionManager.close(); |
| 174 | + } |
| 175 | + |
| 176 | + return false; |
| 177 | + } |
| 178 | + |
| 179 | + public static Admin[] getAllAdmin() |
| 180 | + { |
| 181 | + Connection conn = ConnectionManager.getInstance().getConnection(); |
| 182 | + ResultSet result = null; |
| 183 | + final String GET_ALL_ADMIN = "{Call getAllAdmin() }"; |
| 184 | + |
| 185 | + try( |
| 186 | + CallableStatement statement = conn.prepareCall( |
| 187 | + GET_ALL_ADMIN, |
| 188 | + ResultSet.TYPE_FORWARD_ONLY, |
| 189 | + ResultSet.CONCUR_READ_ONLY); |
| 190 | + ) |
| 191 | + { |
| 192 | + result = statement.executeQuery(); |
| 193 | + ArrayList<Admin> list = new ArrayList<>(); |
| 194 | + Admin admin; |
| 195 | + while( result.next() ) |
| 196 | + { |
| 197 | + admin = new Admin(); |
| 198 | + admin.setUsername( result.getString("username") ); |
| 199 | + admin.setPassword( result.getString( "password" ) ); |
| 200 | + list.add( admin ); |
146 | 201 | } |
| 202 | + return list.toArray( new Admin[ list.size() ] ); |
| 203 | + } |
| 204 | + catch (SQLException e) |
| 205 | + { |
| 206 | + // TODO Auto-generated catch block |
| 207 | + e.printStackTrace(); |
| 208 | + return null; |
| 209 | + } |
| 210 | + finally { |
| 211 | + ConnectionManager.close(); |
| 212 | + } |
| 213 | + |
| 214 | + } |
147 | 215 |
|
| 216 | + /** |
| 217 | + * Returns true if the admin is valid. <br> |
| 218 | + * It checks that the {@code Admin } object is not {@code null } and |
| 219 | + * it does not contain a space |
| 220 | + * @param admin the {@code Admin } object |
| 221 | + * @return {@code true } when the {@code Admin } is valid |
| 222 | + */ |
| 223 | + public static boolean validateAdmin ( Admin admin ){ |
| 224 | + if( admin != null && admin.getUsername() != null && |
| 225 | + admin.getPassword() != null && !admin.getUsername().contains( " " ) ) |
| 226 | + { |
| 227 | + return true; |
148 | 228 | } |
149 | 229 | return false; |
150 | 230 | } |
151 | 231 |
|
| 232 | + /** |
| 233 | + * Returns {@code true } if the {@code Admin } is in the database |
| 234 | + * @param admin the {@code Admin } to check for in the database |
| 235 | + * @return |
| 236 | + */ |
| 237 | + public static boolean exists( Admin admin ){ |
| 238 | + final String GET_ADMIN = "getAdmin(?,?) "; |
| 239 | + |
| 240 | + if( !validateAdmin( admin ) ) return false; |
| 241 | + |
| 242 | + Connection conn = ConnectionManager.getInstance().getConnection(); |
| 243 | + ResultSet result = null ; |
| 244 | + try( |
| 245 | + CallableStatement callable = |
| 246 | + conn.prepareCall( |
| 247 | + GET_ADMIN, |
| 248 | + ResultSet.TYPE_FORWARD_ONLY, |
| 249 | + ResultSet.CONCUR_READ_ONLY); |
| 250 | + |
| 251 | + |
| 252 | + ) |
| 253 | + { |
| 254 | + result = callable.executeQuery(); |
| 255 | + result.last(); |
| 256 | + if( result.getRow() == 0 ) return false; |
| 257 | + |
| 258 | + return true; |
| 259 | + } |
| 260 | + catch (SQLException e) |
| 261 | + { |
| 262 | + // TODO Auto-generated catch block |
| 263 | + e.printStackTrace(); |
| 264 | + } |
| 265 | + return false; |
| 266 | + |
| 267 | + |
| 268 | + } |
152 | 269 | } |
0 commit comments