Example usage for org.hibernate Session merge

List of usage examples for org.hibernate Session merge

Introduction

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

Prototype

Object merge(Object object);

Source Link

Document

Copy the state of the given object onto the persistent object with the same identifier.

Usage

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

License:Open Source License

public boolean saveProgramOutcomeDescriptionById(String value, int programOutcomeId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//from w ww . j a  v a  2s.  c o  m
    try {
        ProgramOutcome o = (ProgramOutcome) session.get(ProgramOutcome.class, programOutcomeId);
        o.setDescription(value);
        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 saveCourseOutcomeProgramOutcome(int outcomeId, int programOutcomeId, int courseOfferingId,
        int existingLinkId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*from  w w  w .  j  a v  a2  s. c o  m*/
    try {

        ProgramOutcome pOutcome = (ProgramOutcome) session.get(ProgramOutcome.class, programOutcomeId);
        CourseOffering courseOffering = (CourseOffering) session.get(CourseOffering.class, courseOfferingId);
        CourseOutcome outcome = (CourseOutcome) session.get(CourseOutcome.class, outcomeId);
        logger.debug(outcomeId + " " + (outcome == null));
        LinkCourseOutcomeProgramOutcome o = null;
        if (existingLinkId > -1) // need to update or delete
        {
            o = (LinkCourseOutcomeProgramOutcome) session.get(LinkCourseOutcomeProgramOutcome.class,
                    existingLinkId);
            if (outcomeId > -1) {
                o.setCourseOutcome(outcome);
                session.merge(o);

            } else
                session.delete(o); // contribution is invalid (or 0)  Delete it 

        } else {
            //need to create a new one
            o = new LinkCourseOutcomeProgramOutcome();
            o.setCourseOffering(courseOffering);
            o.setCourseOutcome(outcome);
            o.setProgramOutcome(pOutcome);
            session.save(o);
        }
        session.getTransaction().commit();
        return true;
    } catch (Exception e) {
        HibernateUtil.logException(logger, e);
        logger.error("Oops " + outcomeId + " " + programOutcomeId + " " + courseOfferingId, 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 update(String id, String name, String description) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*from   w  w  w  .ja v a 2 s.c om*/
    try {

        Program o = (Program) session.get(Program.class, Integer.parseInt(id));
        o.setName(name);
        o.setDescription(description);
        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 updateLinkCourseProgram(int id, int classification, int time) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*from w ww .java 2 s. c om*/
    try {
        LinkCourseProgram l = (LinkCourseProgram) session.get(LinkCourseProgram.class, id);
        CourseClassification cl = (CourseClassification) session.get(CourseClassification.class,
                classification);
        Time min = (Time) session.get(Time.class, time);
        l.setCourseClassification(cl);
        l.setTime(min);
        session.merge(l);
        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 moveCharacteristicType(int id, int charTypeId, String direction) {
    //when moving up, find the one to be moved (while keeping track of the previous one) and swap display_index values
    //when moving down, find the one to be moved, swap displayIndex values of it and the next one
    //when deleting, reduce all links following one to be deleted by 1
    boolean done = false;
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//  w w  w. j a va 2 s .c o m
    try {
        @SuppressWarnings("unchecked")
        List<LinkOrganizationCharacteristicType> existing = (List<LinkOrganizationCharacteristicType>) session
                .createQuery(
                        "select l from LinkProgramCharacteristicType l where l.program.id = :programId order by l.displayIndex")
                .setParameter("programId", id).list();
        if (direction.equals("up")) {
            LinkOrganizationCharacteristicType prev = null;
            for (LinkOrganizationCharacteristicType link : existing) {
                if (link.getCharacteristicType().getId() == charTypeId && prev != null) {
                    int swap = prev.getDisplayIndex();
                    prev.setDisplayIndex(link.getDisplayIndex());
                    link.setDisplayIndex(swap);
                    session.merge(prev);
                    session.merge(prev);
                    done = true;
                    break;
                }
                prev = link;
            }
        } else if (direction.equals("down")) {
            LinkOrganizationCharacteristicType prev = null;
            for (LinkOrganizationCharacteristicType link : existing) {
                if (prev != null) {
                    int swap = prev.getDisplayIndex();
                    prev.setDisplayIndex(link.getDisplayIndex());
                    link.setDisplayIndex(swap);
                    session.merge(prev);
                    session.merge(link);
                    done = true;
                    break;
                }
                if (link.getCharacteristicType().getId() == charTypeId) {
                    prev = link;
                }

            }
        } else if (direction.equals("delete")) {
            LinkOrganizationCharacteristicType toDelete = null;
            for (LinkOrganizationCharacteristicType link : existing) {
                if (toDelete != null) {
                    link.setDisplayIndex(link.getDisplayIndex() - 1);
                    session.merge(link);
                }
                if (link.getCharacteristicType().getId() == charTypeId) {
                    toDelete = link;
                }

            }
            if (toDelete != null) {
                session.delete(toDelete);
                done = true;
            }
        }
        session.getTransaction().commit();
        return done;
    } 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 saveCourseOfferingContributionLinksForProgramOutcome(int courseOfferingId,
        int linkProgramOutcomeId, int contributionId, int masteryId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//  ww  w .j  a  v  a  2  s  .co m
    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();// w  w w.java  2  s  .c o  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 saveAnswerOption(int id, String value, String display, int answerSetId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//from  ww w .  ja  v  a2  s. c  o m
    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();// w  w w .ja v  a 2  s.com
    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

@SuppressWarnings("unchecked")
public boolean moveQuestion(int programId, int questionId, String direction) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*from   www  .  j  av  a  2 s.  c o m*/
    try {
        LinkProgramQuestion toMove = (LinkProgramQuestion) session
                .createQuery("FROM LinkProgramQuestion WHERE program.id=:programId AND question.id=:questionId")
                .setParameter("programId", programId).setParameter("questionId", questionId).uniqueResult();

        int toMoveId = toMove.getId();
        List<LinkProgramQuestion> existing = (List<LinkProgramQuestion>) session
                .createQuery("FROM LinkProgramQuestion WHERE program.id=:programId ORDER BY displayIndex")
                .setParameter("programId", toMove.getProgram().getId()).list();
        if (direction.equals("up")) {
            LinkProgramQuestion prev = null;
            for (LinkProgramQuestion 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")) {
            LinkProgramQuestion prev = null;
            for (LinkProgramQuestion 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")) {
            LinkProgramQuestion toDelete = null;
            for (LinkProgramQuestion link : existing) {
                if (toDelete != null) {
                    link.setDisplayIndex(link.getDisplayIndex() - 1);

                    session.merge(link);
                }
                if (link.getId() == toMoveId) {
                    toDelete = link;
                }

            }
            if (toDelete != null) {
                List<QuestionResponse> responsesToQuestion = (List<QuestionResponse>) session
                        .createQuery("FROM QuestionResponse WHERE question.id=:questionId")
                        .setParameter("questionId", questionId).list();
                for (QuestionResponse resp : responsesToQuestion)
                    session.delete(resp);

                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;
    }
}