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

License:Open Source License

public int saveCourseOfferingOutcomeLink(int courseOfferingId, String outcomeName, int outcomeId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//from  w  w  w  . j a va2 s.com
    try {

        CourseOutcome outcome = new CourseOutcome();
        if (outcomeId > -1) {
            outcome = (CourseOutcome) session.get(CourseOutcome.class, outcomeId);
            outcome.setName(outcomeName);
            session.merge(outcome);
            logger.debug("Saved new value [" + outcomeName + "] for id " + outcomeId);
        } else {
            outcome.setName(outcomeName);
            outcome.setDescription("");
            session.save(outcome);
        }
        CourseOffering courseOffering = (CourseOffering) session.get(CourseOffering.class, courseOfferingId);
        LinkCourseOfferingOutcome lco = getLinkCourseOfferingOutcome(courseOffering, outcome, session);
        if (lco == null) {
            int existingCount = session
                    .createQuery("FROM LinkCourseOfferingOutcome WHERE courseOffering.id=:offeringId")
                    .setParameter("offeringId", courseOfferingId).list().size();
            lco = new LinkCourseOfferingOutcome();
            lco.setCourseOffering(courseOffering);
            lco.setCourseOutcome(outcome);
            lco.setDisplayIndex(existingCount + 1);
            session.save(lco);
        }
        session.getTransaction().commit();
        return outcome.getId();
    } catch (Exception e) {
        HibernateUtil.logException(logger, e);
        try {
            session.getTransaction().rollback();
        } catch (Exception e2) {
            logger.error("Unable to roll back!", e2);
        }
        return -1;
    }
}

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

License:Open Source License

public boolean moveLinkCourseOfferingOutcome(int toMoveId, int courseOfferingId, 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 w  w  w  . ja v a 2  s  . com*/
    try {
        @SuppressWarnings("unchecked")
        List<LinkCourseOfferingOutcome> existing = (List<LinkCourseOfferingOutcome>) session.createQuery(
                "select l from LinkCourseOfferingOutcome l where l.courseOffering.id = :offeringId order by l.displayIndex")
                .setParameter("offeringId", courseOfferingId).list();
        if (direction.equals("up")) {
            LinkCourseOfferingOutcome prev = null;
            for (LinkCourseOfferingOutcome link : existing) {
                if (link.getCourseOutcome().getId() == toMoveId && 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")) {
            LinkCourseOfferingOutcome prev = null;
            for (LinkCourseOfferingOutcome 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.getCourseOutcome().getId() == toMoveId) {
                    prev = link;
                }

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

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

License:Open Source License

public boolean deleteCourseOfferingOutcome(int outcomeId, int courseOfferingId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*from w w w .  j  a va 2 s. c om*/
    try {

        @SuppressWarnings("unchecked")
        List<LinkCourseOfferingOutcome> existing = (List<LinkCourseOfferingOutcome>) session.createQuery(
                "select l from LinkCourseOfferingOutcome l where l.courseOffering.id = :offeringId order by l.displayIndex")
                .setParameter("offeringId", courseOfferingId).list();

        LinkCourseOfferingOutcome toDelete = null;
        for (LinkCourseOfferingOutcome link : existing) {
            if (toDelete != null) {
                link.setDisplayIndex(link.getDisplayIndex() - 1);
                session.merge(link);
            }
            if (link.getCourseOutcome().getId() == outcomeId) {
                toDelete = link;
            }

        }

        if (toDelete != null) {
            Set<LinkCourseOfferingOutcomeCharacteristic> existingCharacteristics = toDelete
                    .getLinkCourseOfferingOutcomeCharacteristics();
            for (LinkCourseOfferingOutcomeCharacteristic linkToDelete : existingCharacteristics) {
                session.delete(linkToDelete);
            }
            List<LinkCourseOutcomeProgramOutcome> existingLinks = ProgramManager.instance()
                    .getProgramOutcomeLinksForCourseOutcome(courseOfferingId, outcomeId, session);
            for (LinkCourseOutcomeProgramOutcome programLinktoDelete : existingLinks) {
                session.delete(programLinktoDelete);
            }
            @SuppressWarnings("unchecked")
            List<LinkAssessmentCourseOutcome> existingAssessmentLinks = (List<LinkAssessmentCourseOutcome>) session
                    .createQuery(
                            "from LinkAssessmentCourseOutcome l where l.courseOffering.id=:courseOfferingId AND l.outcome.id=:outcomeId")
                    .setParameter("courseOfferingId", courseOfferingId).setParameter("outcomeId", outcomeId)
                    .list();
            for (LinkAssessmentCourseOutcome assessmentLinktoDelete : existingAssessmentLinks) {
                session.delete(assessmentLinktoDelete);
            }

            session.refresh(toDelete);
            session.delete(toDelete);
        }

        //   CourseOutcome toDelete = (CourseOutcome)session.get(CourseOutcome.class, outcomeId);
        //   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.OutcomeManager.java

License:Open Source License

public boolean saveProgramOutcome(String outcomeName, int programOutcomeId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*  www. j a va 2  s  .co  m*/
    try {

        ProgramOutcome outcome = (ProgramOutcome) session.get(ProgramOutcome.class, programOutcomeId);
        outcome.setName(outcomeName);
        session.merge(outcome);
        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.PermissionsManager.java

License:Open Source License

public boolean saveOrganizationPermission(int organizationId, String type, String name, String first,
        String last) {/* w ww. ja  v  a2 s.  co  m*/
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    try {

        OrganizationAdmin p = getOrganizationAdminByNameAndType(name, type, organizationId, session);
        boolean existed = true;
        if (p == null) {
            p = new OrganizationAdmin();
            Organization Organization = (Organization) session.get(Organization.class, organizationId);
            p.setOrganization(Organization);
            p.setName(name);
            p.setType(type);
            existed = false;
            if (type.equals("Userid"))
                p.setTypeDisplay("Persons");
            else
                p.setTypeDisplay("Organizations");

        }
        p.setLastName(last);
        p.setFirstName(first);
        if (existed)
            session.merge(p);
        else
            session.save(p);
        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.PermissionsManager.java

License:Open Source License

public boolean saveSystemPermission(String type, String name, String createdUserid, String first, String last) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//from w  ww  .  j  av a 2s.  c  o m
    try {

        SystemAdmin p = getSystemAdminByNameAndType(name, type, session);
        boolean existed = true;
        if (p == null) {
            p = new SystemAdmin();
            p.setName(name);
            p.setType(type);
            if (type.equals("Userid"))
                p.setTypeDisplay("Persons");
            else
                p.setTypeDisplay("Organizations");
            p.setCreatedUserid(createdUserid);
            p.setCreatedOn(Calendar.getInstance().getTime());

            existed = false;
        }
        p.setLastName(last);
        p.setFirstName(first);
        if (existed)
            session.merge(p);
        else
            session.save(p);

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

License:Open Source License

public boolean saveInstructor(int id, String userid, String first, String last) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//from ww w .ja  va 2 s. c  o m
    try {
        boolean createNew = true;
        Instructor i = new Instructor();
        if (id > -1) {
            i = (Instructor) session.get(Instructor.class, id);
            createNew = false;
        } else {
            i = (Instructor) session.createQuery("FROM Instructor where lower(userid) = :userid")
                    .setParameter("userid", userid.toLowerCase()).uniqueResult();
            if (i == null)
                i = new Instructor();
        }
        i.setUserid(userid);
        i.setFirstName(first);
        i.setLastName(last);
        if (createNew)
            session.save(i);
        else
            session.merge(i);

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

License:Open Source License

public boolean saveInstructor(String userid, String first, String last) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//w  ww .  j  a  v  a 2  s  .  c  o  m
    try {
        Instructor i = (Instructor) session.createQuery("FROM Instructor where userid=:userid")
                .setParameter("userid", userid).uniqueResult();
        boolean existed = true;
        if (i == null) {
            i = new Instructor();
            i.setUserid(userid);
            existed = false;
        }
        i.setLastName(last);
        i.setFirstName(first);

        if (existed)
            session.merge(i);
        else
            session.save(i);

        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 saveProgramOutcomeGroupNameById(String value, int programOutcomeGroupId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//from w w w . ja v a 2  s . c  o m
    try {
        ProgramOutcomeGroup o = (ProgramOutcomeGroup) session.get(ProgramOutcomeGroup.class,
                programOutcomeGroupId);
        o.setName(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 saveProgramOutcomeNameById(String value, int programOutcomeId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*from   w  w w.java 2s. c o m*/
    try {
        ProgramOutcome o = (ProgramOutcome) session.get(ProgramOutcome.class, programOutcomeId);
        o.setName(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;
    }
}