Search in sources :

Example 41 with SubjectException

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

the class SubjectDaoImpl method updateSubjectById.

@Override
public Subject updateSubjectById(long id, Subject newSubject) throws SubjectException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        Subject subject = session.get(Subject.class, id);
        if (newSubject == null)
            throw new SubjectDaoException("You tried to update subject with a null value");
        if (subject == null)
            throw new SubjectDaoException("SubjectDto with id : " + id + " does not exist");
        if (!(newSubject.getName() != null ? newSubject.getName() : "").trim().isEmpty())
            subject.setName(newSubject.getName());
        if (!(newSubject.getDescription() != null ? newSubject.getDescription() : "").trim().isEmpty())
            subject.setDescription(newSubject.getDescription());
        if (!(newSubject.getCode() != null ? newSubject.getCode() : "").trim().isEmpty())
            subject.setCode(newSubject.getCode());
        if (newSubject.getUnit() != 0) {
            if (newSubject.getUnit() < 0)
                throw new SubjectDaoException("SubjectDto's unit is invalid");
            subject.setUnit(newSubject.getUnit());
        }
        session.getTransaction().commit();
        session.close();
        return subject;
    } catch (SubjectDaoException e) {
        session.close();
        throw new SubjectException(e.getMessage());
    }
}
Also used : SubjectDaoException(com.remswork.project.alice.dao.exception.SubjectDaoException) SubjectException(com.remswork.project.alice.exception.SubjectException) Session(org.hibernate.Session)

Example 42 with SubjectException

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

the class SubjectDaoImpl method getSubjectByClassAndTeacherId.

@Override
public Subject getSubjectByClassAndTeacherId(long classId, long teacherId) throws SubjectException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        String hql = "from Class as c join c.subject as su join c.teacher as t where c.id = :classId and " + "t.id = :teacherId";
        Query query = session.createQuery(hql);
        query.setParameter("classId", classId);
        query.setParameter("teacherId", teacherId);
        if (query.list().size() < 1)
            throw new SubjectDaoException("No subject found with teacherId : " + teacherId);
        Subject subject = (Subject) ((Object[]) query.list().get(0))[1];
        session.getTransaction().commit();
        session.close();
        return subject;
    } catch (SubjectDaoException e) {
        session.close();
        throw new SubjectException(e.getMessage());
    }
}
Also used : Query(org.hibernate.Query) SubjectDaoException(com.remswork.project.alice.dao.exception.SubjectDaoException) SubjectException(com.remswork.project.alice.exception.SubjectException) Session(org.hibernate.Session)

Example 43 with SubjectException

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

the class SubjectDaoImpl method getSubjectByScheduleId.

@Override
public Subject getSubjectByScheduleId(long scheduleId) throws SubjectException {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    try {
        String hql = "from Class as c join c.subject as su join c.scheduleList as sc where sc.id = :scheduleId";
        Query query = session.createQuery(hql);
        query.setParameter("scheduleId", scheduleId);
        if (query.list().size() < 1)
            throw new SubjectDaoException("No subject found with scheduleId : " + scheduleId);
        Subject subject = (Subject) ((Object[]) query.list().get(0))[1];
        session.getTransaction().commit();
        session.close();
        return subject;
    } catch (SubjectDaoException e) {
        session.close();
        throw new SubjectException(e.getMessage());
    }
}
Also used : Query(org.hibernate.Query) SubjectDaoException(com.remswork.project.alice.dao.exception.SubjectDaoException) SubjectException(com.remswork.project.alice.exception.SubjectException) Session(org.hibernate.Session)

Example 44 with SubjectException

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

the class SubjectDaoImpl method deleteSubjectById.

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

Aggregations

SubjectException (com.remswork.project.alice.exception.SubjectException)44 Message (com.remswork.project.alice.model.support.Message)27 Subject (com.remswork.project.alice.model.Subject)23 AsyncTask (android.os.AsyncTask)10 Gson (com.google.gson.Gson)10 SubjectDaoException (com.remswork.project.alice.dao.exception.SubjectDaoException)10 IOException (java.io.IOException)10 InputStream (java.io.InputStream)10 HttpURLConnection (java.net.HttpURLConnection)10 URL (java.net.URL)10 ArrayList (java.util.ArrayList)10 ExecutionException (java.util.concurrent.ExecutionException)10 Session (org.hibernate.Session)10 SubjectServiceException (com.remswork.project.alice.web.service.exception.SubjectServiceException)9 Client (javax.ws.rs.client.Client)9 WebTarget (javax.ws.rs.client.WebTarget)9 Response (javax.ws.rs.core.Response)9 SubjectResourceLinks (com.remswork.project.alice.resource.links.SubjectResourceLinks)8 Query (org.hibernate.Query)7 List (java.util.List)6