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.CourseManager.java

License:Open Source License

public boolean update(Course c, String subject, String courseNumber, String title, String description) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*  w  w w  . ja  v  a  2  s . c o m*/
    try {
        c.setCourseNumber(Integer.parseInt(courseNumber));
        c.setSubject(subject);
        c.setTitle(title);
        c.setDescription(description);
        session.merge(c);
        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.CourseManager.java

License:Open Source License

public boolean saveTimeItTook(int courseOfferingId, int timeItTookId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*from  w  w w. j a  v a2 s .co m*/
    try {
        CourseOffering c = (CourseOffering) session.get(CourseOffering.class, courseOfferingId);
        TimeItTook time = (TimeItTook) session.get(TimeItTook.class, timeItTookId);
        c.setTimeItTook(time);
        session.merge(c);
        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.CourseManager.java

License:Open Source License

public boolean addTeachingMethod(int courseOfferingId, int teachingMethodId, int howLongId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//w  w  w . j ava  2 s.  c  o  m
    try {
        CourseOffering c = (CourseOffering) session.get(CourseOffering.class, courseOfferingId);
        TeachingMethod tm = (TeachingMethod) session.get(TeachingMethod.class, teachingMethodId);
        TeachingMethodPortionOption howLong = (TeachingMethodPortionOption) session
                .get(TeachingMethodPortionOption.class, howLongId);
        LinkCourseOfferingTeachingMethod link = this.getLinkTeachingMethodByData(c, tm, session);
        if (link == null) {
            link = new LinkCourseOfferingTeachingMethod();
            link.setCourseOffering(c);
            link.setHowLong(howLong);
            link.setTeachingMethod(tm);
            session.save(link);
        } else {
            link.setHowLong(howLong);
            session.merge(link);
        }
        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.CourseManager.java

License:Open Source License

public boolean saveAssessmentMethodName(int id, String newName) {

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/* w ww . j av a2s. co  m*/
    try {
        Assessment o = (Assessment) session.get(Assessment.class, id);
        o.setName(newName);
        session.merge(o);
        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.CourseManager.java

License:Open Source License

public boolean saveAssessmentDescriptionById(int id, String newValue) {

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//from   w w w .  ja  va  2  s .  c om
    try {
        Assessment o = (Assessment) session.get(Assessment.class, id);
        o.setDescription(newValue);
        session.merge(o);
        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.CourseManager.java

License:Open Source License

public boolean saveAssessmentGroupName(int id, String newName) {

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//  w w w.  j ava 2  s  .  com
    try {
        AssessmentGroup o = (AssessmentGroup) session.get(AssessmentGroup.class, id);
        o.setName(newName);
        session.merge(o);
        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.CourseManager.java

License:Open Source License

public boolean saveAssessmentGroupShortName(int id, String newName) {

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*w ww  .j av a2  s  .  c  o  m*/
    try {
        AssessmentGroup o = (AssessmentGroup) session.get(AssessmentGroup.class, id);
        o.setShortName(newName);
        session.merge(o);
        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.CourseManager.java

License:Open Source License

public boolean moveAssessmentMethod(int id, int groupId, 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 .  ja  v a2s .  c o m*/
    try {
        AssessmentGroup group = (AssessmentGroup) session.get(AssessmentGroup.class, groupId);
        List<Assessment> existing = this.getAssessmentsForGroup(group, session);
        if (direction.equals("up")) {
            Assessment prev = null;
            for (Assessment a : existing) {
                if (a.getId() == id && prev != null) {
                    int swap = prev.getDisplayIndex();
                    prev.setDisplayIndex(a.getDisplayIndex());
                    a.setDisplayIndex(swap);
                    session.merge(prev);
                    session.merge(a);
                    done = true;
                    break;
                }
                prev = a;
            }
        } else if (direction.equals("down")) {
            Assessment prev = null;
            for (Assessment a : existing) {
                if (prev != null) {
                    int swap = prev.getDisplayIndex();
                    prev.setDisplayIndex(a.getDisplayIndex());
                    a.setDisplayIndex(swap);
                    session.merge(prev);
                    session.merge(a);
                    done = true;
                    break;
                }
                if (a.getId() == id) {
                    prev = a;
                }

            }
        } else if (direction.equals("delete")) {
            Assessment toDelete = null;
            for (Assessment a : existing) {
                if (toDelete != null) {
                    a.setDisplayIndex(a.getDisplayIndex() - 1);
                    session.merge(a);
                }
                if (a.getId() == id) {
                    toDelete = a;
                }

            }
            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.CourseManager.java

License:Open Source License

public boolean moveAssessmentMethodGroup(int id, 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();//from ww w .j  a va 2 s  .co m
    try {
        List<AssessmentGroup> existing = this.getAssessmentGroups(session);
        if (direction.equals("group_up")) {
            AssessmentGroup prev = null;
            for (AssessmentGroup a : existing) {
                if (a.getId() == id && prev != null) {
                    int swap = prev.getDisplayIndex();
                    prev.setDisplayIndex(a.getDisplayIndex());
                    a.setDisplayIndex(swap);
                    session.merge(prev);
                    session.merge(a);
                    done = true;
                    break;
                }
                prev = a;
            }
        } else if (direction.equals("group_down")) {
            AssessmentGroup prev = null;
            for (AssessmentGroup a : existing) {
                if (prev != null) {
                    int swap = prev.getDisplayIndex();
                    prev.setDisplayIndex(a.getDisplayIndex());
                    a.setDisplayIndex(swap);
                    session.merge(prev);
                    session.merge(a);
                    done = true;
                    break;
                }
                if (a.getId() == id) {
                    prev = a;
                }

            }
        } else if (direction.equals("group_delete")) {
            AssessmentGroup toDelete = null;
            for (AssessmentGroup a : existing) {
                if (toDelete != null) {
                    a.setDisplayIndex(a.getDisplayIndex() - 1);
                    session.merge(a);
                }
                if (a.getId() == id) {
                    toDelete = a;
                }

            }
            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.CourseManager.java

License:Open Source License

public boolean copyDataFromOfferingToOffering(int sourceOffering, int targetOffering, int programId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//from www .  ja  v a2 s. c o  m
    try {

        CourseOffering target = this.getCourseOfferingById(targetOffering, session);
        CourseOffering source = this.getCourseOfferingById(sourceOffering, session);
        Organization org = this.getOrganizationForCourse(source.getCourse(), session).get(0);

        //TeachingMethods
        List<LinkCourseOfferingTeachingMethod> teachingMethods = this.getTeachingMethods(source, session);
        for (LinkCourseOfferingTeachingMethod tm : teachingMethods) {
            LinkCourseOfferingTeachingMethod newLink = new LinkCourseOfferingTeachingMethod();
            newLink.setCourseOffering(target);
            newLink.setHowLong(tm.getHowLong());
            newLink.setTeachingMethod(tm.getTeachingMethod());
            session.save(newLink);
        }
        //assessmentMethods
        List<LinkCourseOfferingAssessment> assessmentMethods = this.getAssessmentsForCourseOffering(source,
                session);
        for (LinkCourseOfferingAssessment am : assessmentMethods) {
            LinkCourseOfferingAssessment newLink = new LinkCourseOfferingAssessment();
            newLink.setCourseOffering(target);
            newLink.setAssessment(am.getAssessment());
            newLink.setWeight(am.getWeight());
            newLink.setWhen(am.getWhen());
            newLink.setCriterionExists(am.getCriterionExists());
            newLink.setCriterionLevel(am.getCriterionLevel());
            newLink.setCriterionCompleted(am.getCriterionCompleted());
            newLink.setCriterionSubmitted(am.getCriterionSubmitted());
            session.save(newLink);
            List<AssessmentFeedbackOption> fbOptions = this
                    .getAssessmentOptionsSelectedForLinkOffering(am.getId(), session);
            for (AssessmentFeedbackOption fbOption : fbOptions) {
                LinkCourseAssessmentFeedbackOption newfbOptionLink = new LinkCourseAssessmentFeedbackOption();
                newfbOptionLink.setLinkCourseOfferingAssessment(newLink);
                newfbOptionLink.setOption(fbOption);
                session.save(newfbOptionLink);
            }
        }
        //outcomes
        List<LinkCourseOfferingOutcome> outcomes = OutcomeManager.instance()
                .getLinkCourseOfferingOutcome(source, session);
        for (LinkCourseOfferingOutcome outcomeLink : outcomes) {
            LinkCourseOfferingOutcome newLink = new LinkCourseOfferingOutcome();
            newLink.setCourseOffering(target);
            newLink.setCourseOutcome(outcomeLink.getCourseOutcome());
            session.save(newLink);
            List<Characteristic> outcomeCharacteristics = OutcomeManager.instance()
                    .getCharacteristicsForCourseOfferingOutcome(source, outcomeLink.getCourseOutcome(), org,
                            session);
            for (Characteristic characteristic : outcomeCharacteristics) {
                LinkCourseOfferingOutcomeCharacteristic newCharLink = new LinkCourseOfferingOutcomeCharacteristic();
                newCharLink.setCharacteristic(characteristic);
                newCharLink.setCreatedByUserid("exported");
                newCharLink.setCreatedOn(Calendar.getInstance().getTime());
                newCharLink.setLinkCourseOfferingOutcome(newLink);
                session.save(newCharLink);
            }
        }

        List<LinkAssessmentCourseOutcome> existingLinks = OutcomeManager.instance()
                .getLinkAssessmentCourseOutcomes(source.getId(), session);
        for (LinkAssessmentCourseOutcome outcomeLink : existingLinks) {
            LinkAssessmentCourseOutcome newLink = new LinkAssessmentCourseOutcome();
            newLink.setCourseOffering(target);
            newLink.setOutcome(outcomeLink.getOutcome());
            newLink.setAssessmentLink(outcomeLink.getAssessmentLink());
            session.save(newLink);
        }

        List<LinkCourseOfferingContributionProgramOutcome> contributionLinks = ProgramManager.instance()
                .getCourseOfferingContributionLinks(source, session);
        for (LinkCourseOfferingContributionProgramOutcome link : contributionLinks) {

            LinkCourseOfferingContributionProgramOutcome newLink = ProgramManager.instance()
                    .getCourseOfferingContributionLinksForProgramOutcome(target, link.getLinkProgramOutcome(),
                            session);
            if (newLink == null)
                newLink = new LinkCourseOfferingContributionProgramOutcome();

            newLink.setLinkProgramOutcome(link.getLinkProgramOutcome());
            newLink.setCourseOffering(target);
            newLink.setContribution(link.getContribution());
            newLink.setMastery(link.getMastery());
            session.save(newLink);
        }
        List<LinkCourseOutcomeProgramOutcome> links = ProgramManager.instance().getCourseOutcomeLinks(source,
                session);
        for (LinkCourseOutcomeProgramOutcome link : links) {
            LinkCourseOutcomeProgramOutcome newLink = new LinkCourseOutcomeProgramOutcome();
            newLink.setCourseOffering(target);
            newLink.setCourseOutcome(link.getCourseOutcome());
            newLink.setProgramOutcome(link.getProgramOutcome());
            session.save(newLink);
        }

        @SuppressWarnings("unchecked")
        List<QuestionResponse> responses = (List<QuestionResponse>) session
                .createQuery("FROM QuestionResponse WHERE courseOffering.id=:courseOfferingId")
                .setParameter("courseOfferingId", source.getId()).list();
        for (QuestionResponse r : responses) {
            QuestionResponse newR = new QuestionResponse();
            newR.setCourseOffering(target);
            newR.setProgram(r.getProgram());
            newR.setQuestion(r.getQuestion());
            newR.setResponse(r.getResponse());
            session.save(newR);
        }

        //comments
        target.setComments(source.getComments());
        target.setContributionComment(source.getContributionComment());
        target.setOutcomeComment(source.getOutcomeComment());
        target.setTeachingComment(source.getTeachingComment());

        session.merge(target);
        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;
    }
}