use of com.remswork.project.alice.exception.SectionException in project classify-system by anverliedoit.
the class SectionDaoImpl method getSectionList.
@Override
public List<Section> getSectionList() throws SectionException {
Session session = sessionFactory.openSession();
session.beginTransaction();
try {
List<Section> sectionList = new ArrayList<>();
Query query = session.createQuery("from Section");
for (Object sectionObj : query.list()) sectionList.add((Section) sectionObj);
session.getTransaction().commit();
session.close();
return sectionList;
} catch (SectionDaoException e) {
session.close();
throw new SectionException(e.getMessage());
}
}
use of com.remswork.project.alice.exception.SectionException in project classify-system by anverliedoit.
the class SectionDaoImpl method deleteSectionById.
@Override
public Section deleteSectionById(long id) throws SectionException {
Session session = sessionFactory.openSession();
session.beginTransaction();
try {
Section section = session.get(Section.class, id);
if (section == null)
throw new SectionDaoException("Section with id : " + id + " does not exist");
// to avoid the constraints restriction we meed to remove section from the student that having the section
Query studentQuery = session.createQuery("from Student");
for (Object studentObj : studentQuery.list()) {
Student student = (Student) studentObj;
if (student.getSection() == null)
continue;
if (student.getSection().equals(section) || (student.getSection() != null ? student.getSection().getId() : 0) == section.getId()) {
student.setSection(null);
}
}
// to avoid the constraints restriction we meed to remove section from the class that having the section
Query classQuery = session.createQuery("from Class");
for (Object classObj : classQuery.list()) {
Class _class = (Class) classObj;
if (_class.getSection() == null)
continue;
if (_class.getSection().equals(section) || (_class.getSection() != null ? _class.getSection().getId() : 0) == section.getId()) {
_class.setSection(null);
}
}
section.setDepartment(null);
session.delete(section);
session.getTransaction().commit();
session.close();
return section;
} catch (SectionDaoException e) {
session.close();
throw new SectionException(e.getMessage());
}
}
use of com.remswork.project.alice.exception.SectionException in project classify-system by anverliedoit.
the class StudentDaoImpl method addStudent.
@Override
public Student addStudent(Student student, long sectionId) throws StudentException {
Session session = sessionFactory.openSession();
session.beginTransaction();
try {
if (student == null)
throw new StudentDaoException("You tried to add student with a null value");
if (student.getStudentNumber() == 0)
throw new StudentDaoException("Student's studentnumber is required");
if (student.getFirstName() == null)
throw new StudentDaoException("Student's first name is required");
if (student.getFirstName().trim().equals(""))
throw new StudentDaoException("Student can't have an empty first name");
if (student.getLastName() == null)
throw new StudentDaoException("Student's last name is required");
if (student.getLastName().trim().equals(""))
throw new StudentDaoException("Student can't have an empty last name");
if (student.getMiddleName() == null)
throw new StudentDaoException("Student's middle name is required");
if (student.getMiddleName().trim().equals(""))
throw new StudentDaoException("Student can't have an empty middle name");
if (student.getGender() == null)
throw new StudentDaoException("Student's gender is required");
if (student.getGender().trim().equals(""))
throw new StudentDaoException("Student can't have an empty gender");
if (!(student.getGender().trim().equalsIgnoreCase("Male") || student.getGender().trim().equalsIgnoreCase("Female")))
throw new StudentDaoException("Student's gender is invalid");
if (student.getAge() == 0)
throw new StudentDaoException("Student's age is required");
if (student.getAge() <= 14 || student.getAge() > 60)
throw new StudentDaoException("Student's age is invalid. " + "The minimum age is 14 and the maximum age is 60");
if (sectionId > 0) {
Section section = sectionDao.getSectionById(sectionId);
student.setSection(section);
} else
student.setSection(null);
student = (Student) session.merge(student);
session.getTransaction().commit();
session.close();
return student;
} catch (StudentDaoException | SectionException e) {
session.close();
throw new StudentException(e.getMessage());
}
}
use of com.remswork.project.alice.exception.SectionException in project classify-system by anverliedoit.
the class SectionController method getSection.
@RequestMapping(value = "get", method = RequestMethod.POST)
public String getSection(@RequestParam("query") String query, ModelMap modelMap) {
try {
List<Department> departmentList = departmentService.getDepartmentList();
List<Section> sectionList = new ArrayList<>();
Section section = null;
if (!query.trim().isEmpty()) {
try {
long id = Long.parseLong(query.trim());
section = sectionService.getSectionById(id);
} catch (NumberFormatException e) {
e.printStackTrace();
section = null;
} catch (SectionException e) {
e.printStackTrace();
section = null;
}
if (section != null)
sectionList.add(section);
else {
for (Section t : sectionService.getSectionList()) {
if (t.getName().equals(query.trim())) {
sectionList.add(t);
continue;
}
}
}
}
if (sectionList.size() < 1) {
sectionList = sectionService.getSectionList();
modelMap.put("responseMessage", "No result found.");
} else {
modelMap.put("responseMessage", sectionList.size() + " Result found.");
}
modelMap.put("sectionList", sectionList);
modelMap.put("departmentList", departmentList);
return "section-table";
} catch (SectionException | DepartmentException e) {
e.printStackTrace();
return "error";
}
}
use of com.remswork.project.alice.exception.SectionException in project classify-system by anverliedoit.
the class StudentController method updateStudentById.
@RequestMapping(value = "update", method = RequestMethod.POST)
public String updateStudentById(@RequestParam("id") long id, @RequestParam("studentNumber") long studentNumber, @RequestParam("firstName") String firstName, @RequestParam("middleName") String middleName, @RequestParam("lastName") String lastName, @RequestParam("gender") String gender, @RequestParam("age") int age, @RequestParam("sectionId") long sectionId, ModelMap modelMap) {
try {
Student newStudent = new Student(studentNumber, firstName, lastName, middleName, gender, age, "");
try {
studentService.updateStudentById(id, newStudent, sectionId);
} catch (StudentException e) {
try {
e.printStackTrace();
sectionId = 0;
studentService.updateStudentById(id, newStudent, sectionId);
} catch (StudentException e2) {
e2.printStackTrace();
}
}
List<Student> studentList = studentService.getStudentList();
List<Section> sectionList = sectionService.getSectionList();
modelMap.put("studentList", studentList);
modelMap.put("sectionList", sectionList);
return "student";
} catch (StudentException | SectionException e) {
e.printStackTrace();
return "error";
}
}
Aggregations