Example usage for org.hibernate Session get

List of usage examples for org.hibernate Session get

Introduction

In this page you can find the example usage for org.hibernate Session get.

Prototype

Object get(String entityName, Serializable id);

Source Link

Document

Return the persistent instance of the given named entity with the given identifier, or null if there is no such persistent instance.

Usage

From source file:ca.usask.gmcte.currimap.action.ProgramManager.java

License:Open Source License

public boolean addCharacteristicToOrganization(int charId, int orgId) {
    boolean createSuccessful = false;
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//from   w w w  .  j ava2s  . c o  m
    try {
        Organization d = (Organization) session.get(Organization.class, orgId);
        CharacteristicType cType = (CharacteristicType) session.get(CharacteristicType.class, charId);
        LinkOrganizationCharacteristicType link = new LinkOrganizationCharacteristicType();
        int max = 0;
        try {
            max = (Integer) session.createQuery(
                    "select max(displayIndex) from LinkOrganizationCharacteristicType l where l.organization.id = :orgId")
                    .setParameter("orgId", d.getId()).uniqueResult();
        } catch (Exception e) {

            logger.error("unable to determine max!", e);
        }

        link.setDisplayIndex(max + 1);
        link.setCharacteristicType(cType);
        link.setOrganization(d);
        //p.getLinkProgramCharacteristicTypes().add(link);
        session.persist(link);
        session.getTransaction().commit();
    } catch (Exception e) {
        HibernateUtil.logException(logger, e);
    }
    createSuccessful = true;
    return createSuccessful;
}

From source file:ca.usask.gmcte.currimap.action.ProgramManager.java

License:Open Source License

public boolean saveCourseOfferingContributionLinksForProgramOutcome(int courseOfferingId,
        int linkProgramOutcomeId, int contributionId, int masteryId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//from w ww . jav a  2s. c  om
    try {
        MasteryOptionValue mastery = (MasteryOptionValue) session.get(MasteryOptionValue.class, masteryId);
        ContributionOptionValue contribution = (ContributionOptionValue) session
                .get(ContributionOptionValue.class, contributionId);
        CourseOffering courseOffering = (CourseOffering) session.get(CourseOffering.class, courseOfferingId);
        LinkProgramProgramOutcome lpo = (LinkProgramProgramOutcome) session.get(LinkProgramProgramOutcome.class,
                linkProgramOutcomeId);

        LinkCourseOfferingContributionProgramOutcome o = getCourseOfferingContributionLinksForProgramOutcome(
                courseOffering, lpo, session);
        if (o == null) {
            o = new LinkCourseOfferingContributionProgramOutcome();
            o.setCourseOffering(courseOffering);
            o.setLinkProgramOutcome(lpo);
            o.setContribution(contribution);
            o.setMastery(mastery);
            session.save(o);
        } else {
            o.setContribution(contribution);
            o.setMastery(mastery);
            session.merge(o);
        }
        session.getTransaction().commit();
        return true;
    } catch (Exception e) {
        HibernateUtil.logException(logger, e);
        try {
            session.getTransaction().rollback();
        } catch (Exception e2) {
            logger.error("Unable to roll back!", e2);
        }
        return false;
    }
}

From source file:ca.usask.gmcte.currimap.action.ProgramManager.java

License:Open Source License

public boolean saveCourseContributionLinksForProgramOutcome(int courseId, int linkProgramOutcomeId,
        int contributionId, int masteryId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*www. j a va  2 s  . co  m*/
    try {

        ContributionOptionValue contribution = (ContributionOptionValue) session
                .get(ContributionOptionValue.class, contributionId);
        MasteryOptionValue mastery = (MasteryOptionValue) session.get(MasteryOptionValue.class, masteryId);
        Course course = (Course) session.get(Course.class, courseId);
        LinkProgramProgramOutcome lpo = (LinkProgramProgramOutcome) session.get(LinkProgramProgramOutcome.class,
                linkProgramOutcomeId);

        LinkCourseContributionProgramOutcome o = getCourseContributionLinksForProgramOutcome(course, lpo,
                session);
        if (o == null) {
            o = new LinkCourseContributionProgramOutcome();
            o.setCourse(course);
            o.setLinkProgramOutcome(lpo);
            o.setContribution(contribution);
            o.setMastery(mastery);
            session.save(o);
        } else {
            o.setContribution(contribution);
            o.setMastery(mastery);
            session.merge(o);
        }
        session.getTransaction().commit();
        return true;
    } catch (Exception e) {
        HibernateUtil.logException(logger, e);
        try {
            session.getTransaction().rollback();
        } catch (Exception e2) {
            logger.error("Unable to roll back!", e2);
        }
        return false;
    }
}

From source file:ca.usask.gmcte.currimap.action.QuestionManager.java

License:Open Source License

public boolean saveQuestion(int id, String display, String questionType, int answerSetId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//ww  w. ja v a 2  s. c  om
    try {
        Question o = new Question();

        if (id > -1) {
            o = (Question) session.get(Question.class, id);
        }
        o.setDisplay(display);
        if (answerSetId > -1) {
            AnswerSet set = (AnswerSet) session.get(AnswerSet.class, answerSetId);
            o.setAnswerSet(set);
        }
        if (questionType != null) {
            QuestionType type = (QuestionType) session.createQuery("FROM QuestionType WHERE name=:name")
                    .setParameter("name", questionType).uniqueResult();
            ;
            o.setQuestionType(type);
        }
        if (id > -1)
            session.update(o);
        else
            session.save(o);
        session.getTransaction().commit();
        return true;
    } catch (Exception e) {
        HibernateUtil.logException(logger, e);
        try {
            session.getTransaction().rollback();
        } catch (Exception e2) {
            logger.error("Unable to roll back!", e2);
        }
        return false;
    }
}

From source file:ca.usask.gmcte.currimap.action.QuestionManager.java

License:Open Source License

public boolean saveAnswerOption(int id, String value, String display, int answerSetId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*from ww w .  ja  v  a2 s. c  om*/
    try {
        AnswerOption o = new AnswerOption();
        if (id > -1) {
            o = (AnswerOption) session.get(AnswerOption.class, id);
            if (!value.equals(o.getValue())) // if the value was already used in question responses, it needs to be updated.
            {
                @SuppressWarnings("unchecked")
                List<QuestionResponse> responsesUsingAnswer = (List<QuestionResponse>) session.createQuery(
                        "FROM QuestionResponse WHERE question in (FROM Question WHERE answerSet.id=:answerSetId) AND response=:responseValue")
                        .setParameter("answerSetId", o.getAnswerSet().getId())
                        .setParameter("responseValue", o.getValue()).list();
                for (QuestionResponse response : responsesUsingAnswer) {
                    response.setResponse(value);
                    session.merge(response);
                }

            }
        } else {
            AnswerSet set = (AnswerSet) session.get(AnswerSet.class, answerSetId);
            o.setAnswerSet(set);
        }
        o.setValue(value);
        o.setDisplay(display);

        if (id < 0) {
            int existingCount = session.createQuery("FROM AnswerOption WHERE answerSet.id=:answerSetId")
                    .setParameter("answerSetId", answerSetId).list().size();
            o.setDisplayIndex(existingCount + 1);
        }
        session.merge(o);
        session.getTransaction().commit();
        return true;
    } catch (Exception e) {
        HibernateUtil.logException(logger, e);
        try {
            session.getTransaction().rollback();
        } catch (Exception e2) {
            logger.error("Unable to roll back!", e2);
        }
        return false;
    }
}

From source file:ca.usask.gmcte.currimap.action.QuestionManager.java

License:Open Source License

@SuppressWarnings("unchecked")
public boolean moveAnswerOption(int toMoveId, String direction) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*  www  .j av  a 2 s  .c om*/
    try {
        AnswerOption toMove = (AnswerOption) session.get(AnswerOption.class, toMoveId);
        List<AnswerOption> existing = (List<AnswerOption>) session
                .createQuery("FROM AnswerOption WHERE answerSet.id=:answerSetId order by displayIndex")
                .setParameter("answerSetId", toMove.getAnswerSet().getId()).list();
        if (direction.equals("up")) {
            AnswerOption prev = null;
            for (AnswerOption link : existing) {
                if (link.getId() == toMoveId && prev != null) {
                    int swap = prev.getDisplayIndex();
                    prev.setDisplayIndex(link.getDisplayIndex());
                    link.setDisplayIndex(swap);
                    session.merge(prev);
                    session.merge(prev);
                    break;
                }
                prev = link;
            }
        } else if (direction.equals("down")) {
            AnswerOption prev = null;
            for (AnswerOption link : existing) {
                if (prev != null) {
                    int swap = prev.getDisplayIndex();
                    prev.setDisplayIndex(link.getDisplayIndex());
                    link.setDisplayIndex(swap);
                    session.merge(prev);
                    session.merge(link);
                    break;
                }
                if (link.getId() == toMoveId) {
                    prev = link;
                }

            }
        } else if (direction.equals("delete")) {
            AnswerOption toDelete = null;
            for (AnswerOption link : existing) {
                if (toDelete != null) {
                    link.setDisplayIndex(link.getDisplayIndex() - 1);
                    List<QuestionResponse> responsesUsingAnswer = (List<QuestionResponse>) session.createQuery(
                            "FROM QuestionResponse WHERE question in (FROM Question WHERE answerSet.id=:answerSetId) AND response=:responseValue")
                            .setParameter("answerSetId", toMove.getAnswerSet().getId())
                            .setParameter("responseValue", toMove.getValue()).list();
                    for (QuestionResponse resp : responsesUsingAnswer) {
                        session.delete(resp);
                    }
                    session.merge(link);
                }
                if (link.getId() == toMoveId) {
                    toDelete = link;
                }

            }
            if (toDelete != null) {
                session.delete(toDelete);
            }
        }
        session.getTransaction().commit();
        return true;
    } catch (Exception e) {
        HibernateUtil.logException(logger, e);
        try {
            session.getTransaction().rollback();
        } catch (Exception e2) {
            logger.error("Unable to roll back!", e2);
        }
        return false;
    }
}

From source file:ca.usask.gmcte.currimap.action.QuestionManager.java

License:Open Source License

public boolean addQuestionToProgram(int questionId, int programId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*  www  . jav  a2  s.co  m*/
    try {
        Question q = (Question) session.get(Question.class, questionId);
        Program p = (Program) session.get(Program.class, programId);
        LinkProgramQuestion newLink = new LinkProgramQuestion();
        newLink.setProgram(p);
        newLink.setQuestion(q);
        int max = 1;
        @SuppressWarnings("unchecked")
        List<LinkProgramQuestion> list = (List<LinkProgramQuestion>) session
                .createQuery("from LinkProgramQuestion where program.id = :programId")
                .setParameter("programId", programId).list();
        if (list != null && !list.isEmpty()) {
            max = (Integer) session
                    .createQuery(
                            "select max(displayIndex) from LinkProgramQuestion where program.id = :programId")
                    .setParameter("programId", programId).uniqueResult();
        }
        newLink.setDisplayIndex(max + 1);
        session.save(newLink);
        session.getTransaction().commit();
        return true;
    } catch (Exception e) {
        HibernateUtil.logException(logger, e);
        try {
            session.getTransaction().rollback();
        } catch (Exception e2) {
            logger.error("Unable to roll back!", e2);
        }
        return false;
    }
}

From source file:ca.usask.gmcte.currimap.action.QuestionManager.java

License:Open Source License

public Question getQuestionById(int id) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*  www.j av  a2s . c o m*/
    Question toReturn = null;
    try {
        toReturn = (Question) session.get(Question.class, id);
        session.getTransaction().commit();
    } catch (Exception e) {
        HibernateUtil.logException(logger, e);
    }
    return toReturn;
}

From source file:ca.usask.gmcte.currimap.action.QuestionManager.java

License:Open Source License

public boolean deleteQuestion(int questionId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*from www  .ja va  2s.c o m*/
    try {
        Question q = (Question) session.get(Question.class, questionId);
        @SuppressWarnings("unchecked")
        List<QuestionResponse> responsesToQuestion = (List<QuestionResponse>) session
                .createQuery("FROM QuestionResponse WHERE question.id=:questionId")
                .setParameter("questionId", q.getId()).list();
        for (QuestionResponse resp : responsesToQuestion)
            session.delete(resp);

        @SuppressWarnings("unchecked")
        List<LinkProgramQuestion> questionLinks = (List<LinkProgramQuestion>) session
                .createQuery("FROM LinkProgramQuestion WHERE question.id=:questionId")
                .setParameter("questionId", q.getId()).list();
        for (LinkProgramQuestion qLink : questionLinks)
            session.delete(qLink);

        session.delete(q);
        session.getTransaction().commit();
        return true;
    } catch (Exception e) {
        HibernateUtil.logException(logger, e);
        try {
            session.getTransaction().rollback();
        } catch (Exception e2) {
            logger.error("Unable to roll back!", e2);
        }
        return false;
    }
}

From source file:ca.usask.gmcte.currimap.action.QuestionManager.java

License:Open Source License

public AnswerSet getAnswerSetById(int id) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/* ww  w .j a  va 2  s .  co  m*/
    AnswerSet toReturn = null;
    try {
        toReturn = (AnswerSet) session.get(AnswerSet.class, id);
        session.getTransaction().commit();
    } catch (Exception e) {
        HibernateUtil.logException(logger, e);
    }
    return toReturn;
}