use of com.remswork.project.alice.exception.SectionException in project classify-system by anverliedoit.
the class SectionDaoImpl method updateSectionById.
@Override
public Section updateSectionById(long id, Section newSection, long departmentId) 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");
if (newSection == null)
newSection = new Section();
if (!(newSection.getName() != null ? newSection.getName() : "").trim().isEmpty())
section.setName(newSection.getName().trim());
if (departmentId > 0) {
Department department = departmentDao.getDepartmentById(departmentId);
if (department.getId() == (section.getDepartment() != null ? section.getDepartment().getId() : 0))
throw new SectionDaoException("Can't update section's department with same department");
section.setDepartment(department);
}
session.getTransaction().commit();
session.close();
return section;
} catch (SectionDaoException | DepartmentException 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 addSection.
@Override
public Section addSection(Section section, long departmentId) throws SectionException {
Session session = sessionFactory.openSession();
session.beginTransaction();
try {
if (section == null)
throw new SectionDaoException("You tried to add section with a null value");
if (section.getName() == null)
throw new SectionDaoException("Section's first name is required");
if (section.getName().trim().equals(""))
throw new SectionDaoException("Section can't have an empty name");
if (departmentId < 1)
throw new SectionDaoException("Query param : departmentId is required");
Department department = departmentDao.getDepartmentById(departmentId);
section.setDepartment(department);
section = (Section) session.merge(section);
session.getTransaction().commit();
session.close();
return section;
} catch (SectionDaoException | DepartmentException 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 updateStudentById.
@Override
public Student updateStudentById(long id, Student newStudent, long sectionId) throws StudentException {
Session session = sessionFactory.openSession();
session.beginTransaction();
try {
Student student = session.get(Student.class, id);
if (student == null)
throw new StudentDaoException("Student with id : " + id + " does not exist.");
if (newStudent == null)
newStudent = new Student();
if (newStudent.getStudentNumber() < 1)
student.setStudentNumber(newStudent.getStudentNumber());
if (!(newStudent.getFirstName() != null ? newStudent.getFirstName() : "").trim().isEmpty())
student.setFirstName(newStudent.getFirstName());
if (!(newStudent.getLastName() != null ? newStudent.getLastName() : "").trim().isEmpty())
student.setLastName(newStudent.getLastName());
if (!(newStudent.getMiddleName() != null ? newStudent.getMiddleName() : "").trim().isEmpty())
student.setMiddleName(newStudent.getMiddleName());
if (!(newStudent.getGender() != null ? newStudent.getGender() : "").trim().isEmpty()) {
if (!(newStudent.getGender().trim().equalsIgnoreCase("Male") || newStudent.getGender().trim().equalsIgnoreCase("Female")))
throw new StudentDaoException("Student's gender is invalid");
student.setGender(newStudent.getGender());
}
if (newStudent.getAge() != 0) {
if (newStudent.getAge() <= 14 || newStudent.getAge() > 60)
throw new StudentDaoException("Student's age is invalid. " + "The minimum age is 14 and the maximum age is 60");
student.setAge(newStudent.getAge());
}
if (sectionId > 0) {
Section section = sectionDao.getSectionById(sectionId);
if (section.getId() == (student.getSection() != null ? student.getSection().getId() : 0))
throw new StudentDaoException("Can't update student's section with same section");
student.setSection(section);
student = (Student) session.merge(student);
}
session.getTransaction().commit();
session.close();
return student;
} catch (StudentDaoException | SectionException e) {
session.close();
throw new StudentException(e.getMessage());
}
}
Aggregations