Example usage for org.hibernate Session flush

List of usage examples for org.hibernate Session flush

Introduction

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

Prototype

void flush() throws HibernateException;

Source Link

Document

Force this session to flush.

Usage

From source file:ca.mcgill.cs.swevo.qualyzer.model.Facade.java

License:Open Source License

/**
 * Try to delete the memo.//from www .j  av  a 2 s  . c om
 * 
 * @param memo
 */
public void deleteMemo(Memo memo) {
    Object project = null;
    HibernateDBManager manager = QualyzerActivator.getDefault().getHibernateDBManagers()
            .get(memo.getProject().getFolderName());
    Session session = null;
    Transaction t = null;
    try {
        session = manager.openSession();
        t = session.beginTransaction();

        /*
         * The following is ALL required in order to delete the object from the database. Don't ask me why, I don't
         * really understand it myself -JF.
         */
        project = session.get(Project.class, memo.getProject().getPersistenceId());
        Memo lMemo = (Memo) session.get(Memo.class, memo.getPersistenceId());

        ((Project) project).getMemos().remove(lMemo);

        session.delete(lMemo);
        session.saveOrUpdate(project);
        session.flush();
        t.commit();
        fListenerManager.notifyMemoListeners(ChangeType.DELETE, new Memo[] { memo }, this);
    } catch (HibernateException e) {
        HibernateUtil.quietRollback(t);
        String key = "model.Facade.Memo.cannotDelete"; //$NON-NLS-1$
        String errorMessage = Messages.getString(key);
        fLogger.error(key, e);
        throw new QualyzerException(errorMessage, e);
    } finally {
        HibernateUtil.quietClose(session);
    }
}

From source file:ca.mcgill.cs.swevo.qualyzer.model.Facade.java

License:Open Source License

/**
 * Delete a code.// w w w . j ava  2s . c o m
 * @param code
 */
public void deleteCode(Code code) {
    Object project = null;
    HibernateDBManager manager = QualyzerActivator.getDefault().getHibernateDBManagers()
            .get(code.getProject().getFolderName());
    Session session = null;
    Transaction t = null;

    try {
        session = manager.openSession();
        t = session.beginTransaction();

        /*
         * The following is ALL required in order to delete the object from the database. Don't ask me why, I don't
         * really understand it myself -JF.
         */
        project = session.get(Project.class, code.getProject().getPersistenceId());
        Object lCode = session.get(Code.class, code.getPersistenceId());

        ((Project) project).getCodes().remove(lCode);

        session.delete(lCode);
        session.saveOrUpdate(project);
        session.flush();
        t.commit();

        fListenerManager.notifyCodeListeners(ChangeType.DELETE, new Code[] { (Code) lCode }, this);
    } catch (HibernateException e) {
        HibernateUtil.quietRollback(t);
        String errorMessage = Messages.getString("model.Facade.code.cannotDelete"); //$NON-NLS-1$
        fLogger.error(errorMessage, e);
        throw new QualyzerException(errorMessage, e);
    } finally {
        HibernateUtil.quietClose(session);
    }

}

From source file:ca.mcgill.cs.swevo.qualyzer.model.Facade.java

License:Open Source License

/**
 * Delete a fragment./*from   w  w w . j a  v  a 2s.  c o  m*/
 * @param fragment
 */
public void deleteFragment(Fragment fragment) {
    IAnnotatedDocument document = fragment.getDocument();
    HibernateDBManager manager = QualyzerActivator.getDefault().getHibernateDBManagers()
            .get(document.getProject().getFolderName());
    Session session = null;
    Transaction t = null;

    try {
        session = manager.openSession();
        t = session.beginTransaction();

        /*
         * The following is ALL required in order to delete the object from the database. Don't ask me why, I don't
         * really understand it myself -JF.
         */

        document.getFragments().remove(fragment.getOffset());
        session.delete(fragment);
        session.saveOrUpdate(document);
        session.flush();
        t.commit();

        if (document instanceof Transcript) {

            fListenerManager.notifyTranscriptListeners(ChangeType.MODIFY,
                    new Transcript[] { (Transcript) document }, this);
        } else {
            fListenerManager.notifyMemoListeners(ChangeType.MODIFY, new Memo[] { (Memo) document }, this);
        }
    } catch (HibernateException e) {
        HibernateUtil.quietRollback(t);
        String errorMessage = Messages.getString("model.Facade.fragment.cannotDelete"); //$NON-NLS-1$
        fLogger.error(errorMessage, e);
        throw new QualyzerException(errorMessage, e);
    } finally {
        HibernateUtil.quietClose(session);
    }
}

From source file:ca.mcgill.cs.swevo.qualyzer.model.Facade.java

License:Open Source License

/**
 * Delete a timestamp.//from   w w w  .  j a v  a2  s.  com
 * @param timestamp
 */
public void deleteTimestamp(Timestamp timestamp) {
    Transcript transcript = timestamp.getTranscript();
    HibernateDBManager manager = QualyzerActivator.getDefault().getHibernateDBManagers()
            .get(transcript.getProject().getFolderName());
    Session session = null;
    Transaction t = null;

    try {
        session = manager.openSession();
        t = session.beginTransaction();

        /*
         * The following is ALL required in order to delete the object from the database. Don't ask me why, I don't
         * really understand it myself -JF.
         */
        transcript.getTimestamps().remove(timestamp.getLineNumber());
        session.delete(timestamp);
        session.saveOrUpdate(transcript);
        session.flush();
        t.commit();

        fListenerManager.notifyTranscriptListeners(ChangeType.MODIFY, new Transcript[] { transcript }, this);
    } catch (HibernateException e) {
        HibernateUtil.quietRollback(t);
        String errorMessage = Messages.getString("model.Facade.Timestamp.cannotDelete"); //$NON-NLS-1$
        fLogger.error(errorMessage, e);
        throw new QualyzerException(errorMessage, e);
    } finally {
        HibernateUtil.quietClose(session);
    }
}

From source file:ca.mcgill.cs.swevo.qualyzer.util.HibernateUtil.java

License:Open Source License

/**
 * Save an object./*from w w  w . j  av  a2s.co  m*/
 * @param manager
 * @param object
 */
public static void quietSave(HibernateDBManager manager, Object object) {
    Transaction t = null;
    Session session = null;
    try {
        session = manager.openSession();
        t = session.beginTransaction();
        session.saveOrUpdate(object);
        session.flush();
        t.commit();
    } catch (HibernateException e) {
        quietRollback(t);
    } finally {
        quietClose(session);
    }
}

From source file:ca.mcgill.cs.swevo.qualyzer.util.HibernateUtil.java

License:Open Source License

/**
 * Save many objects./*from   w  w  w  .j a v  a2  s .  c  om*/
 * @param manager
 * @param objects
 */
public static void quietSave(HibernateDBManager manager, Object[] objects) {
    Transaction t = null;
    Session session = null;
    try {
        session = manager.openSession();
        t = session.beginTransaction();
        for (Object object : objects) {
            session.saveOrUpdate(object);
        }
        session.flush();
        t.commit();
    } catch (HibernateException e) {
        quietRollback(t);
    } finally {
        quietClose(session);
    }
}

From source file:ca.myewb.build.CreateDb.java

License:Open Source License

public static void main(String[] args) {
    String postfix = args[0];//from  w  w w .ja  v  a  2 s. c  om
    String action = args[1];
    System.out.println("Using database: " + postfix);

    Logger.getLogger("ca.myewb").setLevel(Level.WARN);

    try {
        // Set up the database connection
        Class.forName("com.mysql.jdbc.Driver");

        // Drop & re-create the database if needed
        if (action.equals("new")) {
            createDb(args[0]);
        }

        // Prepare for data manipulation
        HibernateUtil.createFactory(postfix);

        Session session = HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();

        // Do what's needed
        if (action.equals("new")) {
            insertData();
        }

        controllerData();

        // Finish up
        session.flush();
        tx.commit();
        HibernateUtil.closeSession();
    } catch (Exception e) {
        System.err.print("Exception: " + e);
        e.printStackTrace();
        System.exit(1);
    }

    Logger.getLogger("ca.myewb").setLevel(Level.DEBUG);
}

From source file:ca.myewb.build.CreateDb.java

License:Open Source License

private static void insertData()
        throws Exception, InstantiationException, IllegalAccessException, ClassNotFoundException {
    System.out.println("Inserting data");

    Session session = HibernateUtil.currentSession();

    // BASIC GROUPS 

    {/* w w  w.j a  v a 2s  .com*/
        //must be first group!
        GroupModel g1 = GroupModel.newGroup();
        g1.setName("Org");
        g1.setShortname("Org");
        g1.setDescription("The main announcement mailing list.");
        g1.setPostName("Anyone (on public front page)");
        g1.setAdmin(true);
        g1.setVisible(true);
        g1.setPublic(false);
        session.save(g1);
    }

    {
        GroupModel g10 = GroupModel.newGroup();
        g10.setName("Guest");
        g10.setShortname("Guest");
        g10.setDescription("The guest user");
        g10.setAdmin(true);
        g10.setVisible(false);
        g10.setPublic(false);
        session.save(g10);
    }

    {
        GroupModel g2 = GroupModel.newGroup();
        g2.setName("Users");
        g2.setShortname("Users");
        g2.setDescription("All registered users");
        g2.setAdmin(true);
        g2.setVisible(false);
        g2.setPublic(false);
        session.save(g2);
    }

    //ADMIN LAYER

    {
        GroupModel g4 = GroupModel.newGroup();
        g4.setName("Admin");
        g4.setShortname("Admin");
        g4.setDescription("System administrators");
        g4.setPostName("All system administrators");
        g4.setAdmin(true);
        g4.setVisible(false);
        g4.setPublic(false);
        session.save(g4);
    }

    {
        GroupModel g3 = GroupModel.newGroup();
        g3.setName("NMT");
        g3.setShortname("NMT");
        g3.setDescription("NMT peoples");
        g3.setPostName("All National Management Team members");
        g3.setAdmin(true);
        g3.setVisible(false);
        g3.setPublic(false);
        session.save(g3);
    }

    //REGULAR MEMBERSHIP

    {
        GroupModel g6 = GroupModel.newGroup();
        g6.setName("Regular");
        g6.setShortname("Regular");
        g6.setDescription("Global group for regular members");
        g6.setPostName("All Regular Members");
        g6.setAdmin(true);
        g6.setVisible(false);
        g6.setPublic(false);
        session.save(g6);
    }

    {
        GroupModel g7 = GroupModel.newGroup();
        g7.setName("Associate");
        g7.setShortname("Associate");
        g7.setDescription("Global group for associate members");
        g7.setPostName("All Associate Members");
        g7.setAdmin(true);
        g7.setVisible(false);
        g7.setPublic(false);
        session.save(g7);
    }

    //CHAPTER MEMBERSHIP

    {
        GroupModel g8 = GroupModel.newGroup();
        g8.setName("Chapter");
        g8.setShortname("Chapter");
        g8.setDescription("Everyone in a chapter");
        g8.setPostName("Anyone in a chapter");
        g8.setAdmin(true);
        g8.setVisible(false);
        g8.setPublic(false);
        session.save(g8);
    }

    {
        GroupModel g9 = GroupModel.newGroup();
        g9.setName("NoChapter");
        g9.setShortname("NoChapter");
        g9.setDescription("Everyone not in a chapter");
        g9.setPostName("Anyone not in a chapter");
        g9.setAdmin(true);
        g9.setVisible(false);
        g9.setPublic(false);
        session.save(g9);
    }

    //DELETED STATES

    {
        GroupModel g11 = GroupModel.newGroup();
        g11.setName("Deleted");
        g11.setShortname("Deleted");
        g11.setDescription("All deleted users");
        g11.setAdmin(true);
        g11.setVisible(false);
        g11.setPublic(false);
        session.save(g11);
    }

    {
        GroupModel g14 = GroupModel.newGroup();
        g14.setName("DeletedPosts");
        g14.setShortname("DeletedPosts");
        g14.setDescription("Group for deleted posts");
        g14.setPostName("No one");
        g14.setAdmin(true);
        g14.setVisible(false);
        g14.setPublic(false);
        session.save(g14);
    }

    //EXEC/NATL REP SYSTEM

    {
        GroupModel g5 = GroupModel.newGroup();
        g5.setName("Chapter Executive Members (all chapters)");
        g5.setShortname("Exec");
        g5.setDescription("General executive mailing list (all chapters).");
        g5.setPostName("All chapter executive members (any chapter)");
        g5.setAdmin(true);
        g5.setVisible(true);
        g5.setPublic(false);
        session.save(g5);
    }

    {
        GroupModel g14 = GroupModel.newGroup();
        g14.setName("Chapter National Reps (all chapters)");
        g14.setShortname("NatlRep");
        g14.setDescription("National Reps mailing list (all chapters).");
        g14.setPostName("All chapter national reps (any chapter)");
        g14.setAdmin(true);
        g14.setVisible(false);
        g14.setPublic(false);
        session.save(g14);
    }

    {
        GroupModel g15 = GroupModel.newGroup();
        g15.setName("Student Chapter Exec");
        g15.setShortname("UniChaptersExec");
        g15.setDescription("General executive mailing list (student chapters).");
        g15.setPostName("All student chapter executive members (any student chapter)");
        g15.setAdmin(true);
        g15.setVisible(true);
        g15.setPublic(false);
        session.save(g15);
    }

    {
        GroupModel g16 = GroupModel.newGroup();
        g16.setName("Professional Chapter Exec");
        g16.setShortname("ProChaptersExec");
        g16.setDescription("General executive mailing list (professional chapters).");
        g16.setPostName("All professional chapter executive members (any professional chapter)");
        g16.setAdmin(true);
        g16.setVisible(true);
        g16.setPublic(false);
        session.save(g16);
    }

    //APPLICATIONS SYSTEM

    {
        GroupModel g15 = GroupModel.newGroup();
        g15.setName("OVs");
        g15.setShortname("OVs");
        g15.setDescription("All overseas volunteers");
        g15.setAdmin(true);
        g15.setVisible(false);
        g15.setPublic(false);
        session.save(g15);
    }

    //NATIONAL REP LISTS

    {
        GroupModel g12 = GroupModel.newGroup();
        g12.setName("Student Chapter Presidents");
        g12.setShortname("UniPresidents");
        g12.setDescription("Student Chapter Presidents' mailing list.");
        g12.setPostName("All student chapter presidents");
        g12.setAdmin(true);
        g12.setVisible(true);
        g12.setPublic(false);
        g12.setNationalRepType('s');
        session.save(g12);
    }

    {
        GroupModel g16 = GroupModel.newGroup();
        g16.setName("Professional Chapter Presidents");
        g16.setShortname("ProPresidents");
        g16.setDescription("Professional Chapter Presidents' mailing list");
        g16.setPostName("All professional chapter presidents");
        g16.setAdmin(true);
        g16.setVisible(true);
        g16.setPublic(false);
        g16.setNationalRepType('p');
        session.save(g16);
    }
    {
        GroupModel g13 = GroupModel.newGroup();
        g13.setName("Finance National Reps");
        g13.setShortname("Finance");
        g13.setDescription("Finance National Reps' mailing list.");
        g13.setPostName("Any Finance National Rep");
        g13.setAdmin(true);
        g13.setVisible(true);
        g13.setPublic(false);
        g13.setNationalRepType('b');
        session.save(g13);
    }

    session.flush();

    Calendar cal = Calendar.getInstance();

    for (int i = 0; i < 30; i++) {
        DailyStatsModel.newDailyStats(cal.getTime());
        cal.add(Calendar.DAY_OF_YEAR, 1);
    }

    /*
     *
     * USERS and ROLES
     *
     */

    //guest user must be first!
    UserModel.createGuestUser();

    // first admin user
    UserModel admin = UserModel.newAssociateSignUp(null, "sample@email.com", "Anonymous", "Administrator",
            "admin");
    admin.upgradeToAdmin();

    session.flush();
}

From source file:ca.myewb.build.CreateDb.java

License:Open Source License

private static void controllerData()
        throws InstantiationException, IllegalAccessException, ClassNotFoundException {
    /* Dynamically find & add all pages (controllers)
     * This is done by directory listing... a bit of a hack,
     *  but good enough.//  ww  w. j  a  v  a2s .c  om
     *
     * This assumes the only files in controllers that aren't
     * pages are the CVS directories, and that directories are only
     * one level deep.
     */
    System.out.println("Inserting controller data");

    Session session = HibernateUtil.currentSession();

    // Clear the perms table first.
    Iterator pages = session.createQuery("FROM PageModel").list().iterator();

    while (pages.hasNext()) {
        ((PageModel) pages.next()).clearGroups();
    }

    session.flush();
    session.createQuery("DELETE PageModel").executeUpdate();
    session.flush();
    session.clear();

    // And re-populate it
    File f = new File("WEB-INF/src/ca/myewb/controllers");
    String[] dirs = f.list();

    System.out.println("Using controllers directory " + f.getAbsolutePath());

    for (String pkgName : dirs) {
        if (!pkgName.equals(".svn") && !pkgName.equals("common")) {
            String[] files = new File("WEB-INF/src/ca/myewb/controllers/" + pkgName).list();
            for (String fileName : files) {
                if (!fileName.substring(0, 1).equals(".")) {
                    File theFile = new File("WEB-INF/src/ca/myewb/controllers/" + pkgName + "/" + fileName);
                    if (!theFile.isDirectory()) {
                        fileName = fileName.substring(0, fileName.length() - 5);
                        putPageInDatabase(session, pkgName, fileName);
                    } else {
                        String[] files2 = new File(
                                "WEB-INF/src/ca/myewb/controllers/" + pkgName + "/" + fileName).list();
                        for (String fileName2 : files2) {
                            if (!fileName2.substring(0, 1).equals(".")) {
                                fileName2 = fileName2.substring(0, fileName2.length() - 5);
                                putPageInDatabase(session, pkgName, fileName + "." + fileName2);
                            }
                        }
                    }
                }
            }
        }
    }
}

From source file:ca.myewb.build.CreateDb.java

License:Open Source License

private static void putPageInDatabase(Session session, String thePackage, String file)
        throws InstantiationException, IllegalAccessException, ClassNotFoundException {
    Controller theController = (Controller) Class.forName("ca.myewb.controllers." + thePackage + "." + file)
            .newInstance();/*from  ww w .j ava 2s. c o  m*/

    PageModel p = PageModel.newPage();
    p.setName(file);
    p.setOldName(theController.oldName());
    p.setArea(thePackage);
    p.setDisplayName(theController.displayName());
    p.setWeight(theController.weight());
    session.save(p);

    Set groups = theController.defaultGroups();
    Iterator it;

    if (groups != null) {
        it = groups.iterator();

        boolean shouldAddAdmin = false;
        boolean shouldAddNMT = false;

        while (it.hasNext()) {
            String s = (String) it.next();

            if (!s.equals("Guest") && !s.equals("Admin")) {
                shouldAddAdmin = true;
            }

            if (!s.equals("Guest") && !s.equals("NMT")) {
                shouldAddNMT = true;
            }

            List r = session.createQuery("FROM GroupModel WHERE shortname=?").setString(0, s).list();
            GroupModel g = (GroupModel) r.get(0);
            g.addPage(p);
            session.flush();
        }

        if (shouldAddAdmin) {
            List r = session.createQuery("FROM GroupModel WHERE shortname=?").setString(0, "Admin").list();
            GroupModel g = (GroupModel) r.get(0);
            g.addPage(p);
            session.flush();
        }

        if (shouldAddNMT) {
            List r = session.createQuery("FROM GroupModel WHERE shortname=?").setString(0, "NMT").list();
            GroupModel g = (GroupModel) r.get(0);
            g.addPage(p);
            session.flush();
        }
    }

    groups = theController.invisibleGroups();

    if (groups != null) {
        it = groups.iterator();

        boolean shouldAddAdmin = false;
        boolean shouldAddNMT = false;

        while (it.hasNext()) {
            String s = (String) it.next();

            if (!s.equals("Guest") && !s.equals("Admin")) {
                shouldAddAdmin = true;
            }

            if (!s.equals("Guest") && !s.equals("NMT")) {
                shouldAddNMT = true;
            }

            List r = session.createQuery("FROM GroupModel WHERE shortname=?").setString(0, s).list();
            GroupModel g = (GroupModel) r.get(0);
            g.addInvisiblePage(p);
            session.flush();
        }

        if (shouldAddAdmin) {
            List r = session.createQuery("FROM GroupModel WHERE shortname=?").setString(0, "Admin").list();
            GroupModel g = (GroupModel) r.get(0);
            g.addInvisiblePage(p);
            session.flush();
        }

        if (shouldAddNMT) {
            List r = session.createQuery("FROM GroupModel WHERE shortname=?").setString(0, "NMT").list();
            GroupModel g = (GroupModel) r.get(0);
            g.addInvisiblePage(p);
            session.flush();
        }
    }
}