Search in sources :

Example 1 with ScheduleDaoException

use of com.remswork.project.alice.dao.exception.ScheduleDaoException in project classify-system by anverliedoit.

the class ScheduleDaoImpl method deleteScheduleById.

@Override
public Schedule deleteScheduleById(long id) throws ScheduleException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Schedule schedule = session.get(Schedule.class, id);
        if (schedule == null)
            throw new ScheduleDaoException("Schedule with id : " + id + " does not exist");
        Query classQuery = session.createQuery("from Class");
        for (Object classObj : classQuery.list()) {
            Class _class = (Class) classObj;
            _class = session.get(Class.class, _class.getId());
            for (Schedule s : _class.getScheduleList()) {
                if (s == null)
                    continue;
                if (s.getId() == id) {
                    _class.getScheduleList().remove(s);
                    break;
                }
            }
        }
        session.delete(schedule);
        session.getTransaction().commit();
        session.close();
        return schedule;
    } catch (ScheduleDaoException e) {
        session.close();
        throw new ScheduleException(e.getMessage());
    }
}
Also used : Query(org.hibernate.Query) Schedule(com.remswork.project.alice.model.Schedule) Class(com.remswork.project.alice.model.Class) ScheduleException(com.remswork.project.alice.exception.ScheduleException) Session(org.hibernate.Session) ScheduleDaoException(com.remswork.project.alice.dao.exception.ScheduleDaoException)

Example 2 with ScheduleDaoException

use of com.remswork.project.alice.dao.exception.ScheduleDaoException in project classify-system by anverliedoit.

the class ScheduleDaoImpl method updateScheduleById.

@Override
public Schedule updateScheduleById(long id, Schedule newSchedule) throws ScheduleException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Schedule schedule = session.get(Schedule.class, id);
        if (schedule == null)
            throw new ScheduleDaoException("Schedule with id : " + id + " does not exist");
        if (newSchedule == null)
            throw new ScheduleDaoException("You tried to update student with a null value");
        if (!(newSchedule.getDay() != null ? newSchedule.getDay() : "").trim().isEmpty())
            schedule.setDay(newSchedule.getDay());
        if (!(newSchedule.getRoom() != null ? newSchedule.getRoom() : "").trim().isEmpty())
            schedule.setRoom(newSchedule.getRoom());
        if (!(newSchedule.getTime() != null ? newSchedule.getTime() : "").trim().isEmpty()) {
            if (!timeHelperBean.isValid(newSchedule.getTime().trim()))
                throw new ScheduleDaoException("Schedule's time is invalid");
            schedule.setTime(newSchedule.getTime());
        }
        if (!(newSchedule.getPeriod() != null ? newSchedule.getPeriod() : "").trim().isEmpty()) {
            if (!timeHelperBean.isValid(newSchedule.getPeriod().trim()))
                throw new ScheduleDaoException("Schedule's period is invalid");
            schedule.setPeriod(newSchedule.getPeriod());
        }
        session.getTransaction().commit();
        session.close();
        return schedule;
    } catch (ScheduleDaoException e) {
        session.close();
        throw new ScheduleException(e.getMessage());
    }
}
Also used : Schedule(com.remswork.project.alice.model.Schedule) ScheduleException(com.remswork.project.alice.exception.ScheduleException) Session(org.hibernate.Session) ScheduleDaoException(com.remswork.project.alice.dao.exception.ScheduleDaoException)

Example 3 with ScheduleDaoException

use of com.remswork.project.alice.dao.exception.ScheduleDaoException in project classify-system by anverliedoit.

the class ScheduleDaoImpl method getScheduleList.

@Override
public List<Schedule> getScheduleList() throws ScheduleException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        List<Schedule> scheduleList = new ArrayList<>();
        Query query = session.createQuery("from Schedule");
        for (Object scheduleObj : query.list()) scheduleList.add((Schedule) scheduleObj);
        session.getTransaction().commit();
        session.close();
        return scheduleList;
    } catch (ScheduleDaoException e) {
        session.close();
        throw new ScheduleException(e.getMessage());
    }
}
Also used : Query(org.hibernate.Query) Schedule(com.remswork.project.alice.model.Schedule) ArrayList(java.util.ArrayList) ScheduleException(com.remswork.project.alice.exception.ScheduleException) Session(org.hibernate.Session) ScheduleDaoException(com.remswork.project.alice.dao.exception.ScheduleDaoException)

Example 4 with ScheduleDaoException

use of com.remswork.project.alice.dao.exception.ScheduleDaoException in project classify-system by anverliedoit.

the class ScheduleDaoImpl method getScheduleById.

@Override
public Schedule getScheduleById(long id) throws ScheduleException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Schedule schedule = session.get(Schedule.class, id);
        if (schedule == null)
            throw new ScheduleDaoException("Schedule with id : " + id + " does not exist");
        session.getTransaction().commit();
        session.close();
        return schedule;
    } catch (ScheduleDaoException e) {
        session.close();
        throw new ScheduleException(e.getMessage());
    }
}
Also used : Schedule(com.remswork.project.alice.model.Schedule) ScheduleException(com.remswork.project.alice.exception.ScheduleException) Session(org.hibernate.Session) ScheduleDaoException(com.remswork.project.alice.dao.exception.ScheduleDaoException)

Example 5 with ScheduleDaoException

use of com.remswork.project.alice.dao.exception.ScheduleDaoException in project classify-system by anverliedoit.

the class ScheduleDaoImpl method addSchedule.

@Override
public Schedule addSchedule(Schedule schedule) throws ScheduleException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        if (schedule == null)
            throw new ScheduleDaoException("You tried to add schedule with a null value");
        if (schedule.getDay() == null)
            throw new ScheduleDaoException("Schedule's day is required");
        if (schedule.getDay().trim().equals(""))
            throw new ScheduleDaoException("Schedule can't have an empty day");
        if (schedule.getTime() == null)
            throw new ScheduleDaoException("Schedule's time is required");
        if (schedule.getTime().trim().equals(""))
            throw new ScheduleDaoException("Schedule can't have an empty time");
        if (!timeHelperBean.isValid(schedule.getTime().trim()))
            throw new ScheduleDaoException("Schedule's time is invalid");
        if (schedule.getPeriod() == null)
            throw new ScheduleDaoException("Schedule's period is required");
        if (schedule.getPeriod().trim().equals(""))
            throw new ScheduleDaoException("Schedule can't have an empty period");
        if (!timeHelperBean.isValid(schedule.getPeriod().trim()))
            throw new ScheduleDaoException("Schedule's period is invalid");
        session.save(schedule);
        session.getTransaction().commit();
        session.close();
        return schedule;
    } catch (ScheduleDaoException e) {
        session.close();
        throw new ScheduleException(e.getMessage());
    }
}
Also used : ScheduleException(com.remswork.project.alice.exception.ScheduleException) Session(org.hibernate.Session) ScheduleDaoException(com.remswork.project.alice.dao.exception.ScheduleDaoException)

Aggregations

ScheduleDaoException (com.remswork.project.alice.dao.exception.ScheduleDaoException)5 ScheduleException (com.remswork.project.alice.exception.ScheduleException)5 Session (org.hibernate.Session)5 Schedule (com.remswork.project.alice.model.Schedule)4 Query (org.hibernate.Query)2 Class (com.remswork.project.alice.model.Class)1 ArrayList (java.util.ArrayList)1