Skip to content

Commit f85dc52

Browse files
ChidiebereChidiebere
authored andcommitted
Recent updates have been made to AdminManager class
1 parent 4a5af99 commit f85dc52

8 files changed

Lines changed: 330 additions & 102 deletions

File tree

‎ModualrProject/src/database/managers/AdminManager.java‎

Lines changed: 209 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -4,149 +4,266 @@
44
import java.sql.Connection;
55
import java.sql.ResultSet;
66
import java.sql.SQLException;
7+
import java.sql.SQLIntegrityConstraintViolationException;
8+
import java.util.ArrayList;
9+
10+
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
711

812
import database.bean.Admin;
13+
import exception.InvalidPrimaryKeyException;
914

1015
public class AdminManager
1116
{
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*/
1917
private static final String UPDATE_USERNAME = "{CALL updateAdminUserName(?, ?, ? ) } ";
20-
2118

19+
2220
/**
2321
* Inserts a new {@code Admin } to the database.<br>
2422
* Returns {@code true } only when the insert was successful.
2523
* @param admin the {@code Admin} object to be inserted
2624
* @return {@code true } only when the {@code Admin} was added successfully
25+
* @throws InvalidPrimaryKeyException when the {@code Admin } object is invalid
2726
*/
28-
public static boolean insertAdmin( Admin admin )
27+
public static boolean insertAdmin( Admin admin ) throws InvalidPrimaryKeyException
2928
{
30-
if ( admin != null )
29+
if( !validateAdmin( admin ) )
3130
{
32-
Connection conn = ConnectionManager.getInstance().getConnection();
31+
throw new InvalidPrimaryKeyException ( "The admin object cannot contain spaces and it cannot be null");
32+
}
3333

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();
4036

37+
try(
38+
CallableStatement statement =
39+
conn.prepareCall(
40+
INSERT_CALL,
41+
ResultSet.TYPE_FORWARD_ONLY,
42+
ResultSet.CONCUR_READ_ONLY);
4143

42-
)
43-
{
44-
statement.setString(1, admin.getUsername() );
45-
statement.setString(2, admin.getPassword() );
4644

47-
int affected = statement.executeUpdate();
45+
)
46+
{
47+
statement.setString(1, admin.getUsername() );
48+
statement.setString(2, admin.getPassword() );
4849

49-
if( affected > 0 )
50-
return true;
50+
int affected = statement.executeUpdate();
5151

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;
6254

6355
}
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+
6466
return false;
6567
}
6668

6769
/**
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
7682
{
77-
if ( oldAdmin != null )
83+
if( !validateAdmin( oldAdmin ) )
7884
{
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+
}
9188

92-
int affected = statement.executeUpdate();
89+
final String UPDATE_PASSWORD = "{CALL updateAdminPassword(?, ?, ? ) } ";
90+
91+
Connection conn = ConnectionManager.getInstance().getConnection();
9392

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 );
96103

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;
106109
}
107110

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();
108121
}
122+
109123
return false;
110124
}
111125

112126
/**
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
114131
*/
115-
public static boolean updateAdminUsername( Admin oldAdmin, String newUsername )
132+
public static boolean updateAdminUsername( Admin oldAdmin, String newUsername ) throws InvalidPrimaryKeyException
116133
{
117-
if ( oldAdmin != null )
134+
if( !validateAdmin( oldAdmin ) || newUsername.contains( " " ))
118135
{
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();
131141

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 );
133152

134-
if( affected > 0 )
135-
return true;
153+
int affected = statement.executeUpdate();
136154

137-
}
138-
catch (SQLException e)
155+
if( affected > 0 )
139156
{
140-
// TODO Auto-generated catch block
141-
e.printStackTrace();
142-
return false;
157+
oldAdmin.setUsername( newUsername );
158+
return true;
143159
}
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 );
146201
}
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+
}
147215

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;
148228
}
149229
return false;
150230
}
151231

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+
}
152269
}

0 commit comments

Comments
 (0)