Example usage for org.hibernate Session delete

List of usage examples for org.hibernate Session delete

Introduction

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

Prototype

void delete(Object object);

Source Link

Document

Remove a persistent instance from the datastore.

Usage

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

License:Open Source License

public boolean deleteAssessment(int linkId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//from w  w  w .  jav  a2  s  .  c  o  m
    try {
        @SuppressWarnings("unchecked")
        List<LinkCourseAssessmentFeedbackOption> exitsingOptions = session.createQuery(
                "FROM LinkCourseAssessmentFeedbackOption WHERE linkCourseOfferingAssessment.id = :linkId")
                .setParameter("linkId", linkId).list();
        for (LinkCourseAssessmentFeedbackOption toDelete : exitsingOptions) {
            session.delete(toDelete);
        }
        LinkCourseOfferingAssessment link = (LinkCourseOfferingAssessment) session
                .get(LinkCourseOfferingAssessment.class, linkId);
        session.delete(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 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();/*from   w  ww  .  j  a  v a2s  .  co  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();// ww w. jav  a  2s . c o  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

@SuppressWarnings("unchecked")
public boolean removeCourseOfferingFromCourse(int offeringId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//w  ww. jav  a 2  s  .c o  m
    try {
        CourseOffering offering = (CourseOffering) session.get(CourseOffering.class, offeringId);

        List<LinkCourseOfferingOutcome> linkOutcomes = (List<LinkCourseOfferingOutcome>) session
                .createQuery("FROM LinkCourseOfferingOutcome WHERE courseOffering.id=:offeringId")
                .setParameter("offeringId", offeringId).list();
        for (LinkCourseOfferingOutcome link : linkOutcomes) {
            List<LinkCourseOfferingOutcomeCharacteristic> chars = (List<LinkCourseOfferingOutcomeCharacteristic>) session
                    .createQuery(
                            "FROM LinkCourseOfferingOutcomeCharacteristic WHERE linkCourseOfferingOutcome.id =:linkId")
                    .setParameter("linkId", link.getId()).list();
            for (LinkCourseOfferingOutcomeCharacteristic c : chars) {
                session.delete(c);
            }
            session.delete(link);
        }
        List<LinkCourseOfferingAssessment> assessments = (List<LinkCourseOfferingAssessment>) session
                .createQuery("FROM LinkCourseOfferingAssessment WHERE courseOffering.id = :offeringId")
                .setParameter("offeringId", offeringId).list();
        for (LinkCourseOfferingAssessment assessment : assessments) {
            List<LinkCourseAssessmentFeedbackOption> exitsingOptions = session.createQuery(
                    "FROM LinkCourseAssessmentFeedbackOption WHERE linkCourseOfferingAssessment.id = :linkId")
                    .setParameter("linkId", assessment.getId()).list();
            for (LinkCourseAssessmentFeedbackOption toDelete : exitsingOptions) {
                session.delete(toDelete);
            }
            session.delete(assessment);
        }
        List<LinkCourseOfferingTeachingMethod> methods = (List<LinkCourseOfferingTeachingMethod>) session
                .createQuery("FROM LinkCourseOfferingTeachingMethod WHERE courseOffering.id = :offeringId")
                .setParameter("offeringId", offeringId).list();
        for (LinkCourseOfferingTeachingMethod method : methods) {
            session.delete(method);
        }
        List<LinkCourseOfferingInstructor> instructors = (List<LinkCourseOfferingInstructor>) session
                .createQuery("FROM LinkCourseOfferingInstructor WHERE courseOffering.id = :offeringId")
                .setParameter("offeringId", offeringId).list();
        for (LinkCourseOfferingInstructor instructor : instructors) {
            session.delete(instructor);
        }
        List<LinkAssessmentCourseOutcome> existingLinks = OutcomeManager.instance()
                .getLinkAssessmentCourseOutcomes(offeringId, session);
        for (LinkAssessmentCourseOutcome outcomeLink : existingLinks) {
            session.delete(outcomeLink);
        }
        List<LinkCourseOfferingContributionProgramOutcome> contributionLinks = ProgramManager.instance()
                .getCourseOfferingContributionLinks(offering, session);
        for (LinkCourseOfferingContributionProgramOutcome link : contributionLinks) {
            session.delete(link);
        }
        List<LinkCourseOutcomeProgramOutcome> links = ProgramManager.instance().getCourseOutcomeLinks(offering,
                session);
        for (LinkCourseOutcomeProgramOutcome link : links) {
            session.delete(link);
        }
        session.refresh(offering);

        session.delete(offering);
        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.OrganizationManager.java

License:Open Source License

public boolean deleteOrganization(String id) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*from  w w w .j a  v  a 2s . c  om*/
    try {

        Organization o = (Organization) session.get(Organization.class, Integer.parseInt(id));
        session.delete(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.OrganizationManager.java

License:Open Source License

public boolean removeAttribute(int toRemoveId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//from ww  w .j  a  v  a2  s .  c  om
    try {
        @SuppressWarnings("unchecked")
        List<InstructorAttributeValue> existing = (List<InstructorAttributeValue>) session
                .createQuery("FROM InstructorAttributeValue WHERE attribute.id = :attrId")
                .setParameter("attrId", toRemoveId).list();
        for (InstructorAttributeValue toDel : existing) {
            session.delete(toDel);
        }
        InstructorAttribute o = (InstructorAttribute) session.get(InstructorAttribute.class, toRemoveId);
        session.delete(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.OrganizationManager.java

License:Open Source License

public boolean removeAttributeValue(int toRemoveId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();//from w  w w .j  a v  a2  s  .c o  m
    try {
        InstructorAttributeValue o = (InstructorAttributeValue) session.get(InstructorAttributeValue.class,
                toRemoveId);
        session.delete(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.OrganizationManager.java

License:Open Source License

public boolean removeCourseAttribute(int toRemoveId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*from  w ww  .  j a v a  2  s.com*/
    try {
        @SuppressWarnings("unchecked")
        List<CourseAttributeValue> existing = (List<CourseAttributeValue>) session
                .createQuery("FROM CourseAttributeValue WHERE attribute.id = :attrId")
                .setParameter("attrId", toRemoveId).list();
        for (CourseAttributeValue toDel : existing) {
            session.delete(toDel);
        }
        CourseAttribute o = (CourseAttribute) session.get(CourseAttribute.class, toRemoveId);
        session.delete(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.OrganizationManager.java

License:Open Source License

public boolean removeCourseAttributeValue(int toRemoveId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*from ww w .j  a  v  a 2  s .  c om*/
    try {
        CourseAttributeValue o = (CourseAttributeValue) session.get(CourseAttributeValue.class, toRemoveId);
        session.delete(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.OrganizationManager.java

License:Open Source License

public boolean removeOrganizationOutcome(int outcomeLinkId) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*from   w w  w  . j a  va  2s  .co  m*/
    try {
        LinkOrganizationOrganizationOutcome l = (LinkOrganizationOrganizationOutcome) session
                .get(LinkOrganizationOrganizationOutcome.class, outcomeLinkId);
        session.delete(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;
    }
}