Skip to content

Commit 71c7da2

Browse files
ChidiebereChidiebere
authored andcommitted
The Student registration functionality is now complete.
You register a Student by inputing a Student object and a StudentData object. The StudentData object stores information about the Student. It is composed of EducationBackground objects, a Biodata object, ProfessionalExperience objects and MeansOfDiscovery objects. Internally, the StudentManager contains a private class StudentDataManager that does the insertion of the StudentData object. The StudentManager method insert() inserts both the StudentData attributes and the Student object as one atomic operation
1 parent 15d6e31 commit 71c7da2

14 files changed

Lines changed: 563 additions & 91 deletions

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,16 @@ public void setStudentId(String studentId)
217217
public boolean isValid(ValidationType type)
218218
{
219219
if( TEST_DATE != null ){
220-
return getStudentId().length() >0 && getBeginDate() !=null &&
220+
boolean value = getStudentId().length() >0 && getBeginDate() !=null &&
221221
getEndDate() != null && getCourseRead().length() >0 &&
222222
getInstitution().length() >0 && getBeginDate().before(getEndDate()) &&
223223
TEST_DATE.before(getBeginDate()) && TEST_DATE.before(getEndDate()) ;
224+
return value;
224225
}
225-
return getStudentId().length() >0 && getBeginDate() !=null &&
226+
boolean value = getStudentId().length() >0 && getBeginDate() !=null &&
226227
getEndDate() != null && getCourseRead().length() >0 &&
227228
getInstitution().length()>0 && getBeginDate().before(getEndDate()) ;
228-
229+
return value;
229230
}
230231

231232

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

Lines changed: 70 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package database.bean.student;
22

33
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
46

57
import database.bean.Bean;
68
import utils.ValidationType;
79

810
/**
911
* This object is used to seperate a {@code Student}'s data including his biodata, educational
10-
* background etc from the actual {@code Student} object that stores the key details of the
11-
* student. This object would be returned by StudentManager when the Student data is required
12+
* background etc from the actual {@code Student} object that stores the key details of a
13+
* {@code Student}. <br>
14+
* This object is composed of {@link Biodata}, an array of {@link EducationalBackgoround}, an array fo
15+
* {@link MeansOfDiscovery}, an array of {@link Phone} and an array of {@link ProfessionalExperience}.<br>
16+
* The set methods for the arra attruibutes of this object removes any null values from its argument
17+
* before it is set.
1218
* @author Chidiebere
1319
*
1420
*/
@@ -33,12 +39,13 @@ public class StudentData implements Bean
3339
* @param experiences
3440
*/
3541
public StudentData( Biodata data, EducationalBackground[] edu,
36-
Phone[] phoneNumbers, ProfessionalExperience[] experiences)
42+
Phone[] phoneNumbers, ProfessionalExperience[] experiences, MeanOfDiscovery[] meansOfDis)
3743
{
3844
setBiodata(data);
3945
setEducation(edu);
4046
setPhoneNumbers(phoneNumbers);
4147
setExperiences(experiences);
48+
setMeansOfDiscovery(meansOfDis);
4249
}
4350

4451

@@ -55,29 +62,51 @@ public boolean isValid(ValidationType type)
5562
/*This method validates this object by ensuring that all its instances have
5663
* the same student id attribute and that they are all valid
5764
*/
58-
5965
final String studentId = getBiodata().getStudentId();
66+
67+
// This ensures that all the attributes of this object have the same studentId and that the length of the
68+
// array attributes are valid
69+
70+
boolean allSame = areArraysValid() && Arrays.stream( getEducation()).allMatch(edu-> edu.getStudentId().equals(studentId) ) &&
71+
Arrays.stream(getExperiences()).allMatch(exp-> exp.getStudentId().equals(studentId)) &&
72+
Arrays.stream(getMeansOfDiscovery()).allMatch(m-> m.getStudentId().equals(studentId) ) &&
73+
Arrays.stream(getPhoneNumbers()).allMatch(p->p.getStudentId().equals(studentId) );
74+
75+
6076
if( studentId == null || studentId.length() < 1 ) return false;
6177
switch (type)
6278
{
6379

6480
case NEW_BEAN:
65-
return Arrays.stream(getEducation()).allMatch(edu-> edu.equals(studentId) && edu.isValid(ValidationType.NEW_BEAN)) &&
66-
Arrays.stream(getExperiences()).allMatch(exp-> exp.equals(studentId) && exp.isValid(ValidationType.NEW_BEAN)) &&
67-
Arrays.stream(getMeansOfDiscovery()).allMatch(means-> means.equals(studentId) && means.isValid(ValidationType.NEW_BEAN)) &&
68-
Arrays.stream(getPhoneNumbers()).allMatch(phone-> phone.equals(studentId) && phone.isValid( ValidationType.NEW_BEAN));
69-
81+
boolean isvalMean = Arrays.stream(getMeansOfDiscovery()).allMatch(mean-> mean.isValid(ValidationType.NEW_BEAN) );
82+
boolean isvalEdu = Arrays.stream(getEducation()).allMatch(edu->edu.isValid(ValidationType.NEW_BEAN) ) ;
83+
boolean isValExp = Arrays.stream(getExperiences()).allMatch(exp->exp.isValid(ValidationType.NEW_BEAN) ) ;
84+
boolean phoneVal = Arrays.stream(getPhoneNumbers()).allMatch(phone->phone.isValid(ValidationType.NEW_BEAN) );
85+
86+
return allSame && isvalEdu && isvalMean && isValExp && phoneVal;
87+
7088
case EXISTING_BEAN:
71-
return Arrays.stream(getEducation()).allMatch(edu-> edu.equals(studentId) && edu.isValid(ValidationType.EXISTING_BEAN)) &&
72-
Arrays.stream(getExperiences()).allMatch(exp-> exp.equals(studentId) && exp.isValid(ValidationType.EXISTING_BEAN)) &&
73-
Arrays.stream(getMeansOfDiscovery()).allMatch(means-> means.equals(studentId) && means.isValid(ValidationType.EXISTING_BEAN)) &&
74-
Arrays.stream(getPhoneNumbers()).allMatch(phone-> phone.equals(studentId) && phone.isValid( ValidationType.EXISTING_BEAN));
75-
89+
return allSame && Arrays.stream(getEducation()).allMatch(edu->edu.isValid(ValidationType.EXISTING_BEAN) ) &&
90+
Arrays.stream(getExperiences()).allMatch(exp->exp.isValid(ValidationType.EXISTING_BEAN) ) &&
91+
Arrays.stream(getMeansOfDiscovery()).allMatch(mean-> mean.isValid(ValidationType.EXISTING_BEAN) ) &&
92+
Arrays.stream(getPhoneNumbers()).allMatch(phone->phone.isValid(ValidationType.EXISTING_BEAN) );
93+
7694

7795
}
7896
return false;
7997
}
8098

99+
/**
100+
* Checks that none of this object's attributes is {@code null}
101+
* @return true only if there are no {@code null } values
102+
*/
103+
private boolean areArraysValid()
104+
{
105+
return getEducation() != null && getExperiences() != null && getBiodata() != null &&
106+
getMeansOfDiscovery() != null && getPhoneNumbers() != null &&
107+
getEducation().length > 0 && getMeansOfDiscovery().length >0 && getPhoneNumbers().length > 0;
108+
}
109+
81110

82111
public Biodata getBiodata()
83112
{
@@ -99,7 +128,12 @@ public EducationalBackground[] getEducation()
99128

100129
public void setEducation(EducationalBackground[] education)
101130
{
102-
this.education = education;
131+
List<EducationalBackground> list =
132+
Arrays.stream(education)
133+
.filter(e-> e != null )
134+
.collect(Collectors.toList());
135+
136+
this.education = list.toArray(new EducationalBackground[ list.size()]);
103137
}
104138

105139

@@ -111,7 +145,11 @@ public MeanOfDiscovery[] getMeansOfDiscovery()
111145

112146
public void setMeansOfDiscovery(MeanOfDiscovery[] meansOfDiscovery)
113147
{
114-
this.meansOfDiscovery = meansOfDiscovery;
148+
List<MeanOfDiscovery> list =
149+
Arrays.stream(meansOfDiscovery)
150+
.filter(means-> means != null )
151+
.collect(Collectors.toList());
152+
this.meansOfDiscovery = list.toArray(new MeanOfDiscovery[ list.size()]);
115153
}
116154

117155

@@ -123,7 +161,11 @@ public Phone[] getPhoneNumbers()
123161

124162
public void setPhoneNumbers(Phone[] phoneNumbers)
125163
{
126-
this.phoneNumbers = phoneNumbers;
164+
List<Phone> list =
165+
Arrays.stream(phoneNumbers)
166+
.filter(means-> means != null )
167+
.collect(Collectors.toList());
168+
this.phoneNumbers = list.toArray( new Phone[ list.size()] );
127169
}
128170

129171

@@ -132,10 +174,19 @@ public ProfessionalExperience[] getExperiences()
132174
return experiences;
133175
}
134176

135-
177+
/**
178+
* This sets the sets the {@code ProfessionalExperience} stored in this object
179+
* to the argument supplied. It does this by first removing any {@code null} values
180+
* in its argument before setting the {@code ProfessionalExperience} array attribute
181+
* @param experiences the profesional experiences of the Student
182+
*/
136183
public void setExperiences(ProfessionalExperience[] experiences)
137184
{
138-
this.experiences = experiences;
185+
List<ProfessionalExperience> list =
186+
Arrays.stream(experiences)
187+
.filter(exp-> exp != null )
188+
.collect(Collectors.toList());
189+
this.experiences = list.toArray( new ProfessionalExperience[ list.size()] );
139190
}
140191

141192
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import database.bean.student.Student;
1616
import exception.InvalidAdminException;
1717
import exception.InvalidBeanException;
18+
import exception.InvalidBiodataException;
1819
import utils.ValidationType;
1920

2021
public class BiodataManager
@@ -24,10 +25,10 @@ public static boolean insert(Biodata data)
2425
throws SQLException, InvalidAdminException, InvalidBeanException
2526
{
2627
if(!DatabaseManager.validateAdmin() ) throw new InvalidAdminException();
27-
if( !data.isValid(ValidationType.NEW_BEAN )) throw new InvalidBeanException();
28+
if( !data.isValid(ValidationType.NEW_BEAN )) throw new InvalidBiodataException();
2829

2930
try( CallableStatement statement = DatabaseManager.getCallableStatement
30-
("{call insertBiodata(?,?,?,?, ?,?,?, ?, ?,?,?, ?, ?,?) }",
31+
("{call insertBiodata(?,?,?,?, ?,?,?, ?, ?,?,?, ?, ?) }",
3132
data.getStudentId(),data.getTitle(), data.getSurname(),
3233
data.getMiddleName(), data.getLastName(), data.getPermanentAddress(),
3334
data.getCurrentAddress(), data.getReligion(), data.getStateOfOrigin(),
@@ -48,7 +49,7 @@ public static boolean update(Student existingStudent, Biodata data)
4849
if(!DatabaseManager.validateAdmin() ) throw new InvalidAdminException();
4950
if( !(data.isValid(ValidationType.NEW_BEAN ) &&
5051
data.getStudentId().equals(existingStudent.getIdCardNumber()))){
51-
throw new InvalidBeanException();
52+
throw new InvalidBiodataException();
5253
}
5354

5455
try( CallableStatement statement = DatabaseManager.getCallableStatement

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static void setCurrentAdmin(Admin currentAdmin)
5151
}
5252

5353
public static boolean validateAdmin(){
54-
if( currentAdmin.isValid( ValidationType.EXISTING_BEAN) )
54+
if( currentAdmin != null && currentAdmin.isValid( ValidationType.EXISTING_BEAN) )
5555
return AdminManager.validateAdmin( getCurrentAdmin());
5656
return false;
5757
}

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,56 @@
1010
import database.bean.student.Student;
1111
import exception.InvalidAdminException;
1212
import exception.InvalidBeanException;
13+
import exception.InvalidExperienceException;
1314
import utils.ValidationType;
1415

1516
public final class ProfExperienceManager
1617
{
18+
/**
19+
* This method inserts data into the {@code ProfessionalExperience} table an inserts the
20+
* different responsibilities that the {@code Student} had during the job into the
21+
* {@code Responsibility } table in the database.<br>
22+
* This method should be used only in the {@code StudentManager} object when registering a
23+
* {@code Student}. If it to be used outside that scope then a call to the {@code Connection}
24+
* object's commit method should be if this method returns {@code true}.
25+
* @param experience
26+
* @return
27+
* @throws SQLException
28+
* @throws InvalidAdminException
29+
* @throws InvalidBeanException
30+
*/
1731
public static boolean insert(ProfessionalExperience experience)
1832
throws SQLException, InvalidAdminException, InvalidBeanException
1933
{
2034
if( !DatabaseManager.validateAdmin() ) throw new InvalidAdminException();
21-
if( !experience.isValid(ValidationType.NEW_BEAN ) ) throw new InvalidBeanException();
35+
if( !experience.isValid(ValidationType.NEW_BEAN ) ) throw new InvalidExperienceException();
36+
37+
Connection conn = ConnectionManager.getInstance().getConnection();
2238

2339
try(
24-
Connection conn = ConnectionManager.getInstance().getConnection();
2540
CallableStatement statement = DatabaseManager.getCallableStatement
2641
("{call addExperienceRecord(?,?,?,?, ? ,?) }", experience.getStudentId(),
2742
experience.getStartDate(), experience.getEndDate(),
2843
experience.getEmployer(), experience.getJobTitle() ) ; )
2944
{
3045
conn.setAutoCommit(false);
31-
int affected = statement.executeUpdate();
3246
statement.registerOutParameter(6, Types.INTEGER);
47+
int affected = statement.executeUpdate();
3348
JobResponsibility res;
3449
boolean successful = true;
3550
if( affected >0 ){
3651
final int expId =statement.getInt( 6 );
3752
final String[] duties = experience.getDuties();
3853

39-
for( int i =0 ; i< duties.length; i++ ){
40-
res = new JobResponsibility(expId, duties[i] );
54+
for( String duty : duties ){
55+
res = new JobResponsibility(expId, duty );
4156
if( !ResponsibilityManager.insert( res ) ){
4257
successful = false;
4358
break;
4459
}
4560
}
4661

4762
if( successful){
48-
conn.commit();
4963
return true;
5064
}
5165
else{
@@ -65,7 +79,7 @@ public static boolean update(Student existingStudent, ProfessionalExperience exp
6579
{
6680
if(!DatabaseManager.validateAdmin()) throw new InvalidAdminException();
6781
if( !experience.isValid(ValidationType.EXISTING_BEAN) )
68-
throw new InvalidBeanException("The Experience cannot be updated");
82+
throw new InvalidExperienceException("The Experience cannot be updated");
6983
return false;
7084
}
7185

0 commit comments

Comments
 (0)