Search in sources :

Example 21 with TeacherException

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

the class TeacherDaoImpl method deleteTeacherById.

@Override
public Teacher deleteTeacherById(long id) throws TeacherException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Teacher teacher = session.get(Teacher.class, id);
        String[] table = new String[2];
        table[0] = Class.class.getSimpleName();
        table[1] = Formula.class.getSimpleName();
        if (teacher == null)
            throw new TeacherDaoException("Teacher with id : " + id + " does not exist.");
        for (String cell : table) {
            String hql = "delete from ".concat(cell).concat(" where teacher.id = :teacherId");
            Query query = session.createQuery(hql);
            query.setParameter("teacherId", id);
            query.executeUpdate();
        }
        teacher.setDepartment(null);
        session.delete(teacher);
        session.getTransaction().commit();
        session.close();
        return teacher;
    } catch (TeacherDaoException e) {
        session.close();
        throw new TeacherException(e.getMessage());
    }
}
Also used : TeacherException(com.remswork.project.alice.exception.TeacherException) Query(org.hibernate.Query) TeacherDaoException(com.remswork.project.alice.dao.exception.TeacherDaoException) Class(com.remswork.project.alice.model.Class) Session(org.hibernate.Session)

Aggregations

TeacherException (com.remswork.project.alice.exception.TeacherException)21 Message (com.remswork.project.alice.model.support.Message)15 Teacher (com.remswork.project.alice.model.Teacher)14 TeacherDaoException (com.remswork.project.alice.dao.exception.TeacherDaoException)6 TeacherServiceException (com.remswork.project.alice.web.service.exception.TeacherServiceException)6 Client (javax.ws.rs.client.Client)6 WebTarget (javax.ws.rs.client.WebTarget)6 Response (javax.ws.rs.core.Response)6 Session (org.hibernate.Session)6 DepartmentResourceLinks (com.remswork.project.alice.resource.links.DepartmentResourceLinks)5 TeacherResourceLinks (com.remswork.project.alice.resource.links.TeacherResourceLinks)5 Gson (com.google.gson.Gson)4 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 HttpURLConnection (java.net.HttpURLConnection)4 URL (java.net.URL)4 ExecutionException (java.util.concurrent.ExecutionException)4 ClientBuilder (javax.ws.rs.client.ClientBuilder)4 Builder (javax.ws.rs.client.Invocation.Builder)4 AsyncTask (android.os.AsyncTask)2