Skip to content

Commit febcf23

Browse files
ChidiebereChidiebere
authored andcommitted
Default image has been added to the database and should thus be ready
for upload
1 parent 0898dda commit febcf23

9 files changed

Lines changed: 243 additions & 71 deletions

File tree

‎ModualrProject/src/database/bean/student/Student.java‎

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package database.bean.student;
22

33
import java.io.File;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
47
import java.sql.Date;
58

69
import database.bean.Bean;
@@ -22,6 +25,8 @@ public class Student implements Bean
2225

2326
private static final long serialVersionUID = 1L;
2427

28+
private static File defaultImage;
29+
2530
/**Foreign key to Modular Class table*/
2631
private String modClass;
2732

@@ -57,7 +62,12 @@ public Student(String firstName, String lastName, String id, String className,
5762
setModClass(className);
5863
setIdCardNumber(id);
5964
setEmailAddress(mail);
60-
setImage( image);
65+
setImage( getDefaultImage());
66+
}
67+
68+
private static File getDefaultImage()
69+
{
70+
return new File("res/defaultImage.jpg");
6171
}
6272

6373
/**
@@ -232,7 +242,7 @@ public File getImage(){
232242
*/
233243
public void setImage(File image)
234244
{
235-
this.image = image;
245+
this.image = image != null ?image : defaultImage;
236246
}
237247

238248
/**
@@ -283,5 +293,58 @@ public boolean validateName()
283293

284294
}
285295

296+
public static void setDefaultImage(InputStream inStream)
297+
{
298+
defaultImage = getImageFromStream("default-image", inStream);
299+
}
300+
301+
/**
302+
* Converts an {@code InputStream} to a file
303+
* @param fileName output file name
304+
* @param inputStream
305+
* @return
306+
*/
307+
public static File getImageFromStream( String fileName , InputStream inputStream)
308+
{
309+
File studentImage = null;
310+
FileOutputStream output = null;
311+
try{
312+
if( inputStream.available() <=0 ) return null;
313+
studentImage = new File( "res\\" + fileName + ".jpg" );
314+
output = new FileOutputStream( studentImage );
315+
studentImage.deleteOnExit();
316+
byte[] buffer = new byte[1024];
317+
while( inputStream.read( buffer) >0 ){
318+
output.write( buffer );
319+
}
320+
}
321+
322+
catch (IOException e)
323+
{
324+
e.printStackTrace();
325+
return null;
326+
}
327+
finally
328+
{
329+
try
330+
{
331+
if( output !=null ) output.close();
332+
if( inputStream != null ) inputStream.close();
333+
}
334+
catch (IOException e)
335+
{
336+
e.printStackTrace();
337+
}
338+
}
339+
return studentImage;
340+
}
341+
342+
public static File getdefaultImage()
343+
{
344+
return defaultImage;
345+
}
346+
347+
348+
286349

287350
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,32 @@ public static boolean resetPassword(String username, String newPassword, String
248248
return false;
249249
}
250250

251+
/**
252+
* Deletes an admin from the database. This operation can only be performed if
253+
* the currently logged in admin has super access else InvalidAdminException is
254+
* thrown.
255+
*
256+
* @param oldAdmin the admin to be removed
257+
* @return true if the operation was successful
258+
* @throws SQLException when a db errror occurs
259+
* @throws InvalidAdminException when the admin that wants to make the change is invalid
260+
*/
261+
public static boolean deleteAdmin(String existingAdminUsername) throws SQLException, InvalidAdminException{
262+
Admin currentAdmin = DatabaseManager.getCurrentAdmin() ;
263+
264+
if(currentAdmin != null &&
265+
!currentAdmin.getUsername().matches(existingAdminUsername ) &&
266+
currentAdmin.isSuper()){
267+
try( CallableStatement stmt =
268+
DatabaseManager.getCallableStatement("removeAdmin(?)", existingAdminUsername); ){
269+
int affected = stmt.executeUpdate();
270+
if(affected > 0 ) return true;
271+
else return false;
272+
}
273+
}
274+
throw new InvalidAdminException("This admin does not havve permission to do this!");
275+
276+
}
251277
public static boolean changeUsername( Admin existingAdmin, String newUsername )
252278
throws SQLException, InvalidAdminException
253279
{

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

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.sql.Connection;
66
import java.sql.Date;
77
import java.sql.DriverManager;
8+
import java.sql.PreparedStatement;
89
import java.sql.ResultSet;
910
import java.sql.SQLException;
1011
import java.sql.Statement;
@@ -23,6 +24,8 @@
2324

2425
import com.sun.net.ssl.internal.ssl.Provider;
2526

27+
import database.bean.student.Student;
28+
2629
/**
2730
* This class contains static methods that would be used to establish
2831
* a connection to the MySql database. <br>
@@ -91,38 +94,38 @@ public static boolean sendMail( String[] recipient, String body, String subject)
9194
//Compose the message
9295
List<InternetAddress> addresses =
9396
Arrays.stream(recipient)
94-
.map(elem -> {
95-
try
96-
{
97-
return new InternetAddress(elem);
98-
}
99-
catch (AddressException e)
100-
{
101-
// TODO Auto-generated catch block
102-
e.printStackTrace();
103-
}
104-
return null;
105-
})
106-
.filter(elem-> elem != null )
107-
.collect(Collectors.toList());
108-
97+
.map(elem -> {
98+
try
99+
{
100+
return new InternetAddress(elem);
101+
}
102+
catch (AddressException e)
103+
{
104+
// TODO Auto-generated catch block
105+
e.printStackTrace();
106+
}
107+
return null;
108+
})
109+
.filter(elem-> elem != null )
110+
.collect(Collectors.toList());
111+
109112
InternetAddress[] addrArray =
110-
addresses.toArray(new InternetAddress[addresses.size()]);
111-
113+
addresses.toArray(new InternetAddress[addresses.size()]);
114+
112115
MimeMessage message = new MimeMessage(mailSession);
113116
message.setFrom(new InternetAddress(user));
114-
117+
115118
message.addRecipients(Message.RecipientType.TO,addrArray);
116119
message.setSubject(subject);
117120
message.setSentDate( new java.util.Date());
118121
message.setText( body);
119-
122+
120123
//send the message
121124
Transport transport = mailSession.getTransport("smtp");
122125
transport.connect(host, user, password);
123-
126+
124127
transport.sendMessage(message, message.getAllRecipients());
125-
128+
126129
transport.close();
127130

128131
}
@@ -145,13 +148,14 @@ protected static boolean openConnection()
145148
try
146149
{
147150
conn = DriverManager.getConnection(MYSQLURL, USERNAME, PASSWORD );
148-
151+
149152
ResultSet result = null;
150153
try( Statement stmt = conn.createStatement();)
151154
{
152155
result = stmt.executeQuery("SELECT NOW();");
153156
if( result.next()){
154157
currentDate = result.getDate(1);
158+
initialiseStudentFile();
155159
System.out.println( "Connected" );
156160
return true;
157161
}
@@ -169,6 +173,40 @@ protected static boolean openConnection()
169173

170174

171175

176+
private static void initialiseStudentFile()
177+
{
178+
String sql = "SELECT DefaultImage FROM resources ";
179+
180+
ResultSet result = null;
181+
try(PreparedStatement stmt = DatabaseManager.getPreparedStatement(sql);){
182+
result = stmt.executeQuery();
183+
if(result.next()){
184+
Student.setDefaultImage(result.getBinaryStream(1));
185+
}
186+
187+
}
188+
catch (SQLException e)
189+
{
190+
// TODO Auto-generated catch block
191+
e.printStackTrace();
192+
}finally{
193+
if( result!=null)
194+
try
195+
{
196+
result.close();
197+
}
198+
catch (SQLException e)
199+
{
200+
// TODO Auto-generated catch block
201+
e.printStackTrace();
202+
}
203+
}
204+
205+
206+
207+
}
208+
209+
172210
/**THis gets the only {@code Connection } object in this project*/
173211
public Connection getConnection()
174212
{

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,26 @@ public static Module[] getModules(int startIndex) throws SQLException, InvalidAd
108108
return list.toArray( new Module[ list.size() ] );
109109

110110
}
111+
112+
/**
113+
* Checks if a MOdule is in the database by checking the moduleName attribute
114+
* @param moduleName the module name to be checked
115+
* @return {@code true } if the module is in the database
116+
* @throws SQLException
117+
*/
118+
public static boolean exists(String moduleName) throws SQLException
119+
{
120+
String sql = "SELECT name FROM module WHERE name = ? ";
121+
ResultSet result = null ;
122+
try( PreparedStatement stmt = DatabaseManager.getPreparedStatement(
123+
sql,moduleName);)
124+
{
125+
126+
result = stmt.executeQuery();
127+
return result.next() ;
128+
129+
}finally{
130+
if( result!=null) result.close();
131+
}
132+
}
111133
}

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

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ public final class ModuleRegisterManager
2424
public static boolean registerForModule(ModuleRegister modReg )
2525
throws SQLException, InvalidAdminException
2626
{
27-
if( modReg.isValid( ValidationType.NEW_BEAN )){
27+
boolean modAndStudentExists =
28+
StudentManager.exists(modReg.getStudentId()) &&
29+
ModuleManager.exists(modReg.getModuleName());
30+
31+
if(modAndStudentExists && modReg.isValid( ValidationType.NEW_BEAN ) &&
32+
canRegister(modReg) ){
2833
try( CallableStatement stmt = DatabaseManager.getCallableStatement
2934
("{call registerForModule(?,?,?)}", modReg.getStudentId(), modReg.getModuleName());)
3035
{
@@ -38,13 +43,46 @@ public static boolean registerForModule(ModuleRegister modReg )
3843
return false;
3944
}
4045

46+
47+
48+
/**
49+
* This checks if a {@code ModuleRegister} can be inputed into the database.
50+
* A {@code ModuleRegister} can be inserted if it does not exists in the database
51+
* or if the {@code Student} failed it the first time.
52+
* @param modReg the {@code ModuleRegister } to be tested
53+
* @return {@code true } if the module Register can be inserted into the database.
54+
* @throws SQLException
55+
* @throws InvalidAdminException
56+
*/
57+
public static boolean canRegister(ModuleRegister modReg) throws SQLException, InvalidAdminException{
58+
String sql =
59+
"Select COUNT(IF(UCASE(result) ='PASS', 1, NULL)) AS numPassed, " +
60+
" COUNT(IF(result,NULL, 1)) AS registeredButNoResult FROM module_Register as reg " +
61+
"WHERE reg.studentId = ? AND reg.moduleName = ? ";
62+
63+
64+
ResultSet result = null;
65+
if( modReg.isValid( ValidationType.NEW_BEAN )){
66+
try( CallableStatement stmt = DatabaseManager.getCallableStatement
67+
(sql, modReg.getStudentId(), modReg.getModuleName());)
68+
{
69+
result = stmt.executeQuery();
70+
71+
if(result.next()) return result.getInt(1) == 0 && result.getInt(2) == 0 ;
72+
73+
}finally{
74+
if(result!=null) result.close();
75+
}
76+
}
77+
return false;
78+
}
4179
public static double getTotalPriceForModule(int regID) throws SQLException
4280
{
4381
ModuleRegister modReg = ModuleRegisterManager.getModRegById(regID);
4482
double totalPrice = modReg.getTotalPriceForModule();
4583
return totalPrice;
4684
}
47-
85+
4886
/**
4987
*
5088
* @param id
@@ -121,7 +159,7 @@ public static boolean setResultForModule( int modRegId, String studId, String mo
121159
ModuleRegister modReg = getModRegById(modRegId);
122160
result = result.toUpperCase().matches("P|PASSED") ? "Pass" : result;
123161
result = result.toUpperCase().matches("F|FAILED") ? "Fail" : result;
124-
162+
125163
if( modReg != null && modReg.hasAttended() &&
126164
(result.toLowerCase().equals("pass") || result.toLowerCase().equals("fail")))
127165
{
@@ -136,7 +174,7 @@ public static boolean setResultForModule( int modRegId, String studId, String mo
136174
}
137175

138176

139-
177+
140178
public static ModuleRegister[] getRegisteredModules( int startIndex ) throws SQLException{
141179
String sql = "SELECT *,isPaymentComplete(id) as Paid FROM module_register " +
142180
"ORDER BY dateRegistered " +

0 commit comments

Comments
 (0)