Example usage for org.hibernate Session clear

List of usage examples for org.hibernate Session clear

Introduction

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

Prototype

void clear();

Source Link

Document

Completely clear the session.

Usage

From source file:de.lemo.dms.connectors.moodle_2_7.ExtractAndMapMoodle.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public void getLMStables(final DBConfigObject dbConfig, final long readingfromtimestamp, List<Long> courses,
        List<String> logins) {

    // accessing DB by creating a session and a transaction using HibernateUtil
    final Session session = HibernateUtil.getSessionFactory(dbConfig).openSession();
    session.clear();
    final Transaction tx = session.beginTransaction();

    boolean hasCR = false;
    if (courses != null && courses.size() > 0)
        hasCR = true;/*w w  w .  j a v a2 s.  co m*/
    else
        courses = new ArrayList<Long>();

    boolean empty = false;

    //Read Context
    Criteria criteria = session.createCriteria(ContextLMS.class, "obj");
    List<Long> contextLevels = new ArrayList<Long>();
    contextLevels.add(40L);
    contextLevels.add(50L);

    criteria.add(Restrictions.in("obj.contextlevel", contextLevels));
    criteria.addOrder(Property.forName("obj.id").asc());
    this.contextLms = criteria.list();
    logger.info("ContextLMS tables: " + this.contextLms.size());

    if (logins != null && !logins.isEmpty()) {
        List<String> archetypes = new ArrayList<String>();
        List<Long> roleIds = new ArrayList<Long>();
        List<String> userIds = new ArrayList<String>();

        archetypes.add("manager");
        archetypes.add("coursecreator");
        archetypes.add("teacher");
        archetypes.add("editingteacher");

        criteria = session.createCriteria(RoleLMS.class, "obj");
        criteria.add(Restrictions.in("obj.archetype", archetypes));
        for (RoleLMS role : (List<RoleLMS>) criteria.list())
            roleIds.add(role.getId());

        criteria = session.createCriteria(UserLMS.class, "obj");
        criteria.add(Restrictions.in("obj.username", logins));
        for (UserLMS user : (List<UserLMS>) criteria.list())
            userIds.add(user.getId() + "");

        criteria = session.createCriteria(RoleAssignmentsLMS.class, "obj");
        criteria.add(Restrictions.in("obj.userid", userIds));
        criteria.add(Restrictions.in("obj.roleid", roleIds));
        for (ContextLMS c : this.contextLms) {
            for (RoleAssignmentsLMS ra : (List<RoleAssignmentsLMS>) criteria.list()) {
                if (c.getContextlevel() == 50 && c.getId() == ra.getContextid()) {
                    courses.add(c.getInstanceid());
                    hasCR = true;
                }
            }
        }
    }

    // reading the LMS Database, create tables as lists of instances of the DB-table classes
    criteria = session.createCriteria(AssignLMS.class, "obj");
    if (hasCR)
        criteria.add(Restrictions.in("obj.course", courses));
    criteria.addOrder(Property.forName("obj.id").asc());
    this.assignLms = criteria.list();
    logger.info("AssignLMS tables: " + this.assignLms.size());

    //Read RoleAssignments
    criteria = session.createCriteria(RoleAssignmentsLMS.class, "obj");
    if (hasCR) {
        ArrayList<Long> ids = new ArrayList<Long>();
        for (ContextLMS c : this.contextLms) {
            if (c.getContextlevel() == 50 && courses.contains(c.getInstanceid()))
                ids.add(c.getId());
        }
        if (!(empty = ids.isEmpty()))
            criteria.add(Restrictions.in("obj.contextid", ids));
    }

    //criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    if (!(hasCR && empty))
        this.roleAssignmentsLms = criteria.list();
    else
        this.roleAssignmentsLms = new ArrayList<RoleAssignmentsLMS>();
    logger.info("RoleAssignmentsLMS tables: " + this.roleAssignmentsLms.size());

    //Read Assign
    criteria = session.createCriteria(AssignGradesLMS.class, "obj");
    if (hasCR) {
        List<Long> tmp = new ArrayList<Long>();
        for (AssignLMS assign : assignLms) {
            tmp.add(assign.getId());
        }
        if (!(empty = tmp.isEmpty()))
            criteria.add(Restrictions.in("obj.assignment", tmp));
    }
    criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    if (!(hasCR && empty))
        this.assignGradesLms = criteria.list();
    else
        this.assignGradesLms = new ArrayList<AssignGradesLMS>();
    logger.info("AssignGradesLMS tables: " + this.assignGradesLms.size());

    //Read Enrol
    criteria = session.createCriteria(EnrolLMS.class, "obj");
    if (hasCR)
        criteria.add(Restrictions.in("obj.courseid", courses));
    criteria.addOrder(Property.forName("obj.id").asc());
    this.enrolLms = criteria.list();
    logger.info("EnrolLMS tables: " + this.enrolLms.size());

    //Read Modules
    criteria = session.createCriteria(ModulesLMS.class, "obj");
    criteria.addOrder(Property.forName("obj.id").asc());
    this.modulesLms = criteria.list();
    logger.info("ModulesLMS tables: " + this.modulesLms.size());

    //Read UserEnrolments
    criteria = session.createCriteria(UserEnrolmentsLMS.class, "obj");
    if (hasCR) {
        ArrayList<Long> ids = new ArrayList<Long>();
        for (EnrolLMS e : this.enrolLms)
            ids.add(e.getId());
        if (!(empty = ids.isEmpty()))
            criteria.add(Restrictions.in("obj.enrolid", ids));
    }
    criteria.addOrder(Property.forName("obj.id").asc());
    if (!(hasCR && empty))
        this.userEnrolmentsLms = criteria.list();
    else
        this.userEnrolmentsLms = new ArrayList<UserEnrolmentsLMS>();
    logger.info("UserEnrolmentsLMS tables: " + this.userEnrolmentsLms.size());

    //Read CourseModules
    criteria = session.createCriteria(CourseModulesLMS.class, "obj");
    if (hasCR)
        criteria.add(Restrictions.in("obj.course", courses));
    criteria.addOrder(Property.forName("obj.id").asc());
    this.courseModulesLms = criteria.list();
    logger.info("CourseModulesLMS tables: " + this.courseModulesLms.size());

    //Read Log
    criteria = session.createCriteria(LogstoreStandardLogLMS.class, "obj");
    if (hasCR) {
        criteria.add(Restrictions.in("obj.course", courses));
    }
    criteria.add(Restrictions.gt("obj.timecreated", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.timecreated").asc());
    this.logstoreLms = criteria.list();
    logger.info("LogLMS tables: " + this.logstoreLms.size());

    //Read Resource
    criteria = session.createCriteria(ResourceLMS.class, "obj");
    if (hasCR)
        criteria.add(Restrictions.in("obj.course", courses));

    //criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    this.resourceLms = criteria.list();
    logger.info("ResourceLMS tables: " + this.resourceLms.size());

    //Read Urls
    criteria = session.createCriteria(UrlLMS.class, "obj");
    if (hasCR)
        criteria.add(Restrictions.in("obj.course", courses));

    //criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    this.urlLms = criteria.list();
    logger.info("UrlLMS tables: " + this.urlLms.size());

    //Read Pages
    criteria = session.createCriteria(PageLMS.class, "obj");
    if (hasCR)
        criteria.add(Restrictions.in("obj.course", courses));

    //criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    this.pageLms = criteria.list();
    logger.info("PageLMS tables: " + this.pageLms.size());

    //Read Quiz
    criteria = session.createCriteria(QuizLMS.class, "obj");
    if (hasCR)
        criteria.add(Restrictions.in("obj.course", courses));

    //criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    this.quizLms = criteria.list();
    logger.info("QuizLMS tables: " + this.quizLms.size());

    //Read QuizAttempts
    criteria = session.createCriteria(QuizAttemptsLMS.class, "obj");
    if (hasCR)
        if (hasCR) {
            ArrayList<Long> ids = new ArrayList<Long>();
            for (QuizLMS e : this.quizLms)
                ids.add(e.getId());
            if (!(empty = ids.isEmpty()))
                criteria.add(Restrictions.in("obj.quiz", ids));
        }

    criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    if (!(hasCR && empty))
        this.quizAttemptsLms = criteria.list();
    else
        this.quizAttemptsLms = new ArrayList<QuizAttemptsLMS>();
    logger.info("QuizAttemptsLMS tables: " + this.quizAttemptsLms.size());

    //Read Chats
    criteria = session.createCriteria(ChatLMS.class, "obj");
    if (hasCR)
        criteria.add(Restrictions.in("obj.course", courses));

    //criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    this.chatLms = criteria.list();
    logger.info("ChatLMS tables: " + this.chatLms.size());

    //Read ChatLog
    criteria = session.createCriteria(ChatLogLMS.class, "obj");
    if (hasCR) {
        ArrayList<Long> ids = new ArrayList<Long>();
        for (ChatLMS e : this.chatLms)
            ids.add(e.getId());
        if (!(empty = ids.isEmpty()))
            criteria.add(Restrictions.in("obj.chat", ids));
    }
    criteria.add(Restrictions.gt("obj.timestamp", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    if (!(hasCR && empty))
        this.chatLogLms = criteria.list();
    else
        this.chatLogLms = new ArrayList<ChatLogLMS>();
    logger.info("ChatLogLMS tables: " + this.chatLogLms.size());

    criteria = session.createCriteria(CourseCategoriesLMS.class, "obj");

    //criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    this.courseCategoriesLms = criteria.list();
    logger.info("CourseCategoriesLMS tables: " + this.courseCategoriesLms.size());

    criteria = session.createCriteria(CourseLMS.class, "obj");
    if (hasCR)
        criteria.add(Restrictions.in("obj.id", courses));

    criteria.addOrder(Property.forName("obj.id").asc());
    this.courseLms = criteria.list();
    logger.info("CourseLMS tables: " + this.courseLms.size());

    final Query forumPosts;
    if (!hasCR) {
        forumPosts = session
                .createQuery("from ForumPostsLMS x where x.modified>=:readingtimestamp order by x.id asc");
        forumPosts.setParameter("readingtimestamp", readingfromtimestamp);
        this.forumPostsLms = forumPosts.list();
    } else {
        String courseClause = "(";
        for (int i = 0; i < courses.size(); i++) {
            courseClause += courses.get(i);
            if (i < courses.size() - 1)
                courseClause += ",";
            else
                courseClause += ")";
        }
        forumPosts = session.createSQLQuery(
                "SELECT posts.id,posts.userid,posts.created,posts.modified,posts.subject,posts.message,posts.discussion from mdl_forum_posts as posts JOIN mdl_logstore_standard_log as logs ON posts.userid = logs.userid Where logs.courseid in "
                        + courseClause
                        + " and (posts.created = logs.timecreated or posts.modified = logs.timecreated) AND posts.modified>=:readingtimestamp");
        forumPosts.setParameter("readingtimestamp", readingfromtimestamp);
        List<Object[]> tmpl = forumPosts.list();
        this.forumPostsLms = new ArrayList<ForumPostsLMS>();
        for (Object[] obj : tmpl) {
            ForumPostsLMS p = new ForumPostsLMS();
            if (obj[0].getClass().equals(BigInteger.class)) {
                p.setId(((BigInteger) obj[0]).longValue());
            } else {
                p.setId(((Integer) obj[0]).longValue());
            }
            if (obj[0].getClass().equals(BigInteger.class)) {
                p.setUserid(((BigInteger) obj[1]).longValue());
            } else {
                p.setUserid(((Integer) obj[1]).longValue());
            }
            if (obj[0].getClass().equals(BigInteger.class)) {
                p.setCreated(((BigInteger) obj[2]).longValue());
            } else {
                p.setCreated(((Integer) obj[2]).longValue());
            }
            if (obj[0].getClass().equals(BigInteger.class)) {
                p.setModified(((BigInteger) obj[3]).longValue());
            } else {
                p.setModified(((Integer) obj[3]).longValue());
            }
            p.setSubject((String) obj[4]);
            p.setMessage((String) obj[5]);
            if (obj[0].getClass().equals(BigInteger.class)) {
                p.setDiscussion(((BigInteger) obj[6]).longValue());
            } else {
                p.setDiscussion(((Integer) obj[6]).longValue());
            }
            this.forumPostsLms.add(p);

        }
    }
    logger.info("ForumPostsLMS tables: " + this.forumPostsLms.size());

    criteria = session.createCriteria(ForumLMS.class, "obj");
    if (hasCR)
        criteria.add(Restrictions.in("obj.course", courses));

    //criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    this.forumLms = criteria.list();
    logger.info("ForumLMS tables: " + this.forumLms.size());

    criteria = session.createCriteria(GroupsLMS.class, "obj");
    if (hasCR)
        criteria.add(Restrictions.in("obj.courseid", courses));

    //criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    this.groupLms = criteria.list();
    logger.info("GroupsLMS tables: " + this.groupLms.size());

    criteria = session.createCriteria(WikiLMS.class, "obj");
    if (hasCR)
        criteria.add(Restrictions.in("obj.course", courses));

    //criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    this.wikiLms = criteria.list();
    logger.info("WikiLMS tables: " + this.wikiLms.size());

    criteria = session.createCriteria(WikiPagesLMS.class, "obj");
    if (hasCR && !this.wikiLms.isEmpty()) {
        Set<Long> wikiids = new HashSet<Long>();
        for (WikiLMS wiki : this.wikiLms) {
            wikiids.add(wiki.getId());
        }
        criteria.add(Restrictions.in("obj.subwikiid", wikiids));
    }
    //criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    this.wikiPagesLms = criteria.list();
    logger.info("WikiPagesLMS tables: " + this.wikiPagesLms.size());

    criteria = session.createCriteria(GroupsMembersLMS.class, "obj");
    if (hasCR) {
        ArrayList<Long> ids = new ArrayList<Long>();
        for (GroupsLMS e : this.groupLms)
            ids.add(e.getId());
        if (!(empty = ids.isEmpty()))
            criteria.add(Restrictions.in("obj.groupid", ids));
    }
    //criteria.add(Restrictions.gt("obj.timeadded", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    if (!(hasCR && empty))
        this.groupMembersLms = criteria.list();
    else
        this.groupMembersLms = new ArrayList<GroupsMembersLMS>();
    logger.info("GroupsMembersLMS tables: " + this.groupMembersLms.size());

    /*      criteria = session.createCriteria(QuestionStatesLMS.class, "obj");
          if(hasCR)
          {
             ArrayList<Long> ids = new ArrayList<Long>();
              for(QuizQuestionInstancesLMS e : this.quizQuestionInstancesLms)
     ids.add(e.getQuestion());
              if(!(empty = ids.isEmpty()))
     criteria.add(Restrictions.in("obj.question", ids));
          }
          criteria.add(Restrictions.gt("obj.timestamp", readingfromtimestamp));
          criteria.addOrder(Property.forName("obj.id").asc());
          if(!(hasCR && empty))
             this.questionStatesLms = criteria.list();
          else
             this.questionStatesLms = new ArrayList<QuestionStatesLMS>();
          logger.info("QuestionStatesLMS tables: " + this.questionStatesLms.size());
          */

    /*
          criteria = session.createCriteria(QuestionLMS.class, "obj");
          if(hasCR)
          {
             ArrayList<Long> ids = new ArrayList<Long>();
              for(QuizQuestionInstancesLMS e : this.quizQuestionInstancesLms)
     ids.add(e.getQuestion());
              if(!(empty = ids.isEmpty()))
     criteria.add(Restrictions.in("obj.id", ids));
          }
                  
          //criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
          criteria.addOrder(Property.forName("obj.id").asc());
          if(!(hasCR && empty))
             this.questionLms = criteria.list();
          else
             this.questionLms = new ArrayList<QuestionLMS>();
          logger.info("QuestionLMS tables: " + this.questionLms.size());
                  
    */

    criteria = session.createCriteria(UserLMS.class, "obj");
    if (hasCR) {
        ArrayList<Long> ids = new ArrayList<Long>();
        for (RoleAssignmentsLMS e : this.roleAssignmentsLms)
            ids.add(Long.valueOf(e.getUserid()));
        if (!(empty = ids.isEmpty()))
            criteria.add(Restrictions.in("obj.id", ids));
    }
    criteria.addOrder(Property.forName("obj.id").asc());
    if (!(hasCR && empty))
        this.userLms = criteria.list();
    else
        this.userLms = new ArrayList<UserLMS>();
    logger.info("UserLMS tables: " + this.userLms.size());

    criteria = session.createCriteria(RoleLMS.class, "obj");
    criteria.addOrder(Property.forName("obj.id").asc());
    this.roleLms = criteria.list();
    logger.info("RoleLMS tables: " + this.roleLms.size());

    criteria = session.createCriteria(QuizGradesLMS.class, "obj");
    if (hasCR) {
        ArrayList<Long> ids = new ArrayList<Long>();
        for (QuizLMS e : this.quizLms)
            ids.add(e.getId());
        if (!(empty = ids.isEmpty()))
            criteria.add(Restrictions.in("obj.quiz", ids));
    }

    criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    if (!(hasCR && empty))
        this.quizGradesLms = criteria.list();
    else
        this.quizGradesLms = new ArrayList<QuizGradesLMS>();
    logger.info("QuizGradesLMS tables: " + this.quizGradesLms.size());

    criteria = session.createCriteria(ForumDiscussionsLMS.class, "obj");
    if (hasCR) {
        ArrayList<Long> ids = new ArrayList<Long>();
        for (ForumLMS e : this.forumLms)
            ids.add(e.getId());
        if (!(empty = ids.isEmpty()))
            criteria.add(Restrictions.in("obj.forum", ids));
    }

    criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    if (!(hasCR && empty))
        this.forumDiscussionsLms = criteria.list();
    else
        this.forumDiscussionsLms = new ArrayList<ForumDiscussionsLMS>();
    logger.info("ForumDiscussionsLMS tables: " + this.forumDiscussionsLms.size());

    criteria = session.createCriteria(ScormLMS.class, "obj");
    if (hasCR) {
        criteria.add(Restrictions.in("obj.course", courses));
    }

    //criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    this.scormLms = criteria.list();
    logger.info("ScormLMS tables: " + this.scormLms.size());

    criteria = session.createCriteria(GradeItemsLMS.class, "obj");
    if (hasCR) {
        criteria.add(Restrictions.in("obj.courseid", courses));
    }

    //criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    this.gradeItemsLms = criteria.list();
    logger.info("GradeItemsLMS tables: " + this.gradeItemsLms.size());

    criteria = session.createCriteria(GradeGradesLMS.class, "obj");
    if (hasCR) {
        ArrayList<Long> ids = new ArrayList<Long>();
        for (GradeItemsLMS e : this.gradeItemsLms)
            ids.add(e.getId());
        if (!(empty = ids.isEmpty()))
            criteria.add(Restrictions.in("obj.itemid", ids));
    }

    //criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    if (!(hasCR && empty))
        this.gradeGradesLms = criteria.list();
    else
        this.gradeGradesLms = new ArrayList<GradeGradesLMS>();
    logger.info("GradeGradesLMS tables: " + this.gradeGradesLms.size());

    // hibernate session finish and close
    tx.commit();
    session.close();

}

From source file:de.lemo.dms.connectors.moodle_2_7.ExtractAndMapMoodle.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public void getLMStables(final DBConfigObject dbConf, final long readingfromtimestamp,
        final long readingtotimestamp, List<Long> courses, List<String> logins) {

    // accessing DB by creating a session and a transaction using HibernateUtil
    final Session session = HibernateUtil.getSessionFactory(dbConf).openSession();
    // Session session = HibernateUtil.getDynamicSourceDBFactoryMoodle("jdbc:mysql://localhost/moodle19",
    session.clear();
    final Transaction tx = session.beginTransaction();

    // reading the LMS Database, create tables as lists of instances of the DB-table classes
    Criteria criteria;//  w w w  . j a va2 s. co  m
    boolean hasCR = false;
    if (courses != null && courses.size() > 0)
        hasCR = true;

    boolean empty = false;

    if (this.userLms == null) {

        //Read Context
        criteria = session.createCriteria(ContextLMS.class, "obj");
        List<Long> contextLevels = new ArrayList<Long>();
        contextLevels.add(40L);
        contextLevels.add(50L);

        criteria.add(Restrictions.in("obj.contextlevel", contextLevels));
        criteria.addOrder(Property.forName("obj.id").asc());
        this.contextLms = criteria.list();
        logger.info("ContextLMS tables: " + this.contextLms.size());

        if (logins != null && !logins.isEmpty()) {
            List<String> archetypes = new ArrayList<String>();
            List<Long> roleIds = new ArrayList<Long>();
            List<String> userIds = new ArrayList<String>();

            archetypes.add("manager");
            archetypes.add("coursecreator");
            archetypes.add("teacher");
            archetypes.add("editingteacher");

            criteria = session.createCriteria(RoleLMS.class, "obj");
            criteria.add(Restrictions.in("obj.archetype", archetypes));
            for (RoleLMS role : (List<RoleLMS>) criteria.list())
                roleIds.add(role.getId());

            criteria = session.createCriteria(UserLMS.class, "obj");
            criteria.add(Restrictions.in("obj.username", logins));
            for (UserLMS user : (List<UserLMS>) criteria.list())
                userIds.add(user.getId() + "");

            criteria = session.createCriteria(RoleAssignmentsLMS.class, "obj");
            criteria.add(Restrictions.in("obj.userid", userIds));
            criteria.add(Restrictions.in("obj.roleid", roleIds));
            for (ContextLMS c : this.contextLms) {
                for (RoleAssignmentsLMS ra : (List<RoleAssignmentsLMS>) criteria.list()) {
                    if (c.getContextlevel() == 50 && c.getId() == ra.getContextid()) {
                        courses.add(c.getInstanceid());
                        hasCR = true;
                    }
                }
            }

        }

        criteria = session.createCriteria(AssignLMS.class, "obj");
        if (hasCR)
            criteria.add(Restrictions.in("obj.course", courses));
        criteria.addOrder(Property.forName("obj.id").asc());
        this.assignLms = criteria.list();
        logger.info("AssignLMS tables: " + this.assignLms.size());

        criteria = session.createCriteria(EnrolLMS.class, "obj");
        if (hasCR)
            criteria.add(Restrictions.in("obj.courseid", courses));
        criteria.addOrder(Property.forName("obj.id").asc());
        this.enrolLms = criteria.list();
        logger.info("EnrolLMS tables: " + this.enrolLms.size());

        criteria = session.createCriteria(ModulesLMS.class, "obj");
        criteria.addOrder(Property.forName("obj.id").asc());
        this.modulesLms = criteria.list();
        logger.info("ModulesLMS tables: " + this.modulesLms.size());

        criteria = session.createCriteria(UserEnrolmentsLMS.class, "obj");
        if (hasCR) {
            ArrayList<Long> ids = new ArrayList<Long>();
            for (EnrolLMS e : this.enrolLms)
                ids.add(e.getId());
            if (!(empty = ids.isEmpty()))
                criteria.add(Restrictions.in("obj.enrolid", ids));
        }
        criteria.addOrder(Property.forName("obj.id").asc());
        if (!(hasCR && empty))
            this.userEnrolmentsLms = criteria.list();
        else
            this.userEnrolmentsLms = new ArrayList<UserEnrolmentsLMS>();
        logger.info("UserEnrolmentsLMS tables: " + this.userEnrolmentsLms.size());

        criteria = session.createCriteria(CourseModulesLMS.class, "obj");
        if (hasCR)
            criteria.add(Restrictions.in("obj.course", courses));
        criteria.addOrder(Property.forName("obj.id").asc());
        this.courseModulesLms = criteria.list();
        logger.info("CourseModulesLMS tables: " + this.courseModulesLms.size());

        criteria = session.createCriteria(ResourceLMS.class, "obj");
        if (hasCR)
            criteria.add(Restrictions.in("obj.course", courses));
        criteria.addOrder(Property.forName("obj.id").asc());
        this.resourceLms = criteria.list();
        logger.info("ResourceLMS tables: " + this.resourceLms.size());

        //Read Urls
        criteria = session.createCriteria(UrlLMS.class, "obj");
        if (hasCR)
            criteria.add(Restrictions.in("obj.course", courses));

        criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
        criteria.addOrder(Property.forName("obj.id").asc());
        this.urlLms = criteria.list();
        logger.info("UrlLMS tables: " + this.urlLms.size());

        //Read Pages
        criteria = session.createCriteria(PageLMS.class, "obj");
        if (hasCR)
            criteria.add(Restrictions.in("obj.course", courses));

        criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
        criteria.addOrder(Property.forName("obj.id").asc());
        this.pageLms = criteria.list();
        logger.info("UrlLMS tables: " + this.pageLms.size());

        criteria = session.createCriteria(CourseLMS.class, "obj");
        if (hasCR)
            criteria.add(Restrictions.in("obj.id", courses));
        criteria.addOrder(Property.forName("obj.id").asc());
        this.courseLms = criteria.list();
        logger.info("CourseLMS tables: " + this.courseLms.size());

        criteria = session.createCriteria(ChatLMS.class, "obj");
        if (hasCR)
            criteria.add(Restrictions.in("obj.course", courses));
        criteria.addOrder(Property.forName("obj.id").asc());
        this.chatLms = criteria.list();
        logger.info("ChatLMS tables: " + this.chatLms.size());

        criteria = session.createCriteria(CourseCategoriesLMS.class, "obj");
        criteria.addOrder(Property.forName("obj.id").asc());
        this.courseCategoriesLms = criteria.list();
        logger.info("CourseCategoriesLMS tables: " + this.courseCategoriesLms.size());

        criteria = session.createCriteria(ForumLMS.class, "obj");
        if (hasCR)
            criteria.add(Restrictions.in("obj.course", courses));
        criteria.addOrder(Property.forName("obj.id").asc());
        this.forumLms = criteria.list();
        logger.info("ForumLMS tables: " + this.forumLms.size());

        criteria = session.createCriteria(GroupsLMS.class, "obj");
        if (hasCR)
            criteria.add(Restrictions.in("obj.courseid", courses));
        criteria.addOrder(Property.forName("obj.id").asc());
        this.groupLms = criteria.list();
        logger.info("GroupsLMS tables: " + this.groupLms.size());

        criteria = session.createCriteria(QuizLMS.class, "obj");
        if (hasCR)
            criteria.add(Restrictions.in("obj.course", courses));
        criteria.addOrder(Property.forName("obj.id").asc());
        this.quizLms = criteria.list();
        logger.info("QuizLMS tables: " + this.quizLms.size());

        criteria = session.createCriteria(WikiLMS.class, "obj");
        if (hasCR)
            criteria.add(Restrictions.in("obj.course", courses));
        criteria.addOrder(Property.forName("obj.id").asc());
        this.wikiLms = criteria.list();
        logger.info("WikiLMS tables: " + this.wikiLms.size());

        /*
        criteria = session.createCriteria(QuizQuestionInstancesLMS.class, "obj");
        if(hasCR)
        {
           ArrayList<Long> ids = new ArrayList<Long>();
            for(QuizLMS e : this.quizLms)
        ids.add(e.getId());
            if(!(empty = ids.isEmpty()))
        criteria.add(Restrictions.in("obj.quiz", ids));
        }
        criteria.addOrder(Property.forName("obj.id").asc());
        if(!(hasCR && empty))
           this.quizQuestionInstancesLms = criteria.list();
        else
           this.quizQuestionInstancesLms = new ArrayList<QuizQuestionInstancesLMS>();
        logger.info("QuizQuestionInstancesLMS tables: " + this.quizQuestionInstancesLms.size());
        */
        /*
                 criteria = session.createCriteria(QuestionLMS.class, "obj");
                 if(hasCR)
                 {
                    ArrayList<Long> ids = new ArrayList<Long>();
                     for(QuizQuestionInstancesLMS e : this.quizQuestionInstancesLms)
        ids.add(e.getQuestion());
                     if(!(empty = ids.isEmpty()))
        criteria.add(Restrictions.in("obj.id", ids));
                 }
                 criteria.addOrder(Property.forName("obj.id").asc());
                 if(!(hasCR && empty))
                    this.questionLms = criteria.list();
                 else
                    this.questionLms = new ArrayList<QuestionLMS>();
                 logger.info("QuestionLMS tables: " + this.questionLms.size());
        */

        criteria = session.createCriteria(RoleLMS.class, "obj");
        criteria.addOrder(Property.forName("obj.id").asc());
        this.roleLms = criteria.list();
        logger.info("RoleLMS tables: " + this.roleLms.size());

        session.clear();

        /*
        criteria = session.createCriteria(AssignmentLMS.class, "obj");
        if(hasCR)
        {
           criteria.add(Restrictions.in("obj.course", courses));
        }
        criteria.addOrder(Property.forName("obj.id").asc());
        this.assignmentLms = criteria.list();
        logger.info("AssignmentLMS tables: " + this.assignmentLms.size());
        */
        criteria = session.createCriteria(ScormLMS.class, "obj");
        if (hasCR) {
            criteria.add(Restrictions.in("obj.course", courses));
        }
        criteria.addOrder(Property.forName("obj.id").asc());
        this.scormLms = criteria.list();
        logger.info("ScormLMS tables: " + this.scormLms.size());

        criteria = session.createCriteria(GradeItemsLMS.class, "obj");
        if (hasCR) {
            criteria.add(Restrictions.in("obj.courseid", courses));
        }
        criteria.addOrder(Property.forName("obj.id").asc());
        this.gradeItemsLms = criteria.list();
        logger.info("GradeItemsLMS tables: " + this.gradeItemsLms.size());

        criteria = session.createCriteria(RoleAssignmentsLMS.class, "obj");
        if (hasCR) {
            ArrayList<Long> ids = new ArrayList<Long>();
            for (ContextLMS c : this.contextLms) {
                if (c.getContextlevel() == 50 && courses.contains(c.getInstanceid()))
                    ids.add(c.getId());
            }
            if (!(empty = ids.isEmpty()))
                criteria.add(Restrictions.in("obj.contextid", ids));
        }
        criteria.addOrder(Property.forName("obj.id").asc());
        if (!(hasCR && empty))
            this.roleAssignmentsLms = criteria.list();
        else
            this.roleAssignmentsLms = new ArrayList<RoleAssignmentsLMS>();
        logger.info("RoleAssignmentsLMS tables: " + this.roleAssignmentsLms.size());

        criteria = session.createCriteria(UserLMS.class, "obj");
        if (hasCR) {
            ArrayList<Long> ids = new ArrayList<Long>();
            for (RoleAssignmentsLMS e : this.roleAssignmentsLms)
                ids.add(Long.valueOf(e.getUserid()));
            if (!(empty = ids.isEmpty()))
                criteria.add(Restrictions.in("obj.id", ids));
        }
        criteria.addOrder(Property.forName("obj.id").asc());
        if (!(hasCR && empty))
            this.userLms = criteria.list();
        else
            this.userLms = new ArrayList<UserLMS>();
        logger.info("UserLMS tables: " + this.userLms.size());
    }

    criteria = session.createCriteria(QuizAttemptsLMS.class, "obj");
    if (hasCR)
        if (hasCR) {
            ArrayList<Long> ids = new ArrayList<Long>();
            for (QuizLMS e : this.quizLms)
                ids.add(e.getId());
            if (!(empty = ids.isEmpty()))
                criteria.add(Restrictions.in("obj.quiz", ids));
        }
    criteria.add(Restrictions.lt("obj.timemodified", readingtotimestamp));
    criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    if (!(hasCR && empty))
        this.quizAttemptsLms = criteria.list();
    else
        this.quizAttemptsLms = new ArrayList<QuizAttemptsLMS>();
    logger.info("QuizAttemptsLMS tables: " + this.quizAttemptsLms.size());

    criteria = session.createCriteria(LogstoreStandardLogLMS.class, "obj");
    if (hasCR)
        criteria.add(Restrictions.in("obj.course", courses));
    criteria.add(Restrictions.lt("obj.time", readingtotimestamp));
    criteria.add(Restrictions.gt("obj.time", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    this.logstoreLms = criteria.list();
    logger.info("LogLMS tables: " + this.logstoreLms.size());

    criteria = session.createCriteria(ChatLogLMS.class, "obj");
    if (hasCR) {
        ArrayList<Long> ids = new ArrayList<Long>();
        for (ChatLMS e : this.chatLms)
            ids.add(e.getId());
        if (!(empty = ids.isEmpty()))
            criteria.add(Restrictions.in("obj.chat", ids));
    }
    criteria.add(Restrictions.lt("obj.timestamp", readingtotimestamp));
    criteria.add(Restrictions.gt("obj.timestamp", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    if (!(hasCR && empty))
        this.chatLogLms = criteria.list();
    else
        this.chatLogLms = new ArrayList<ChatLogLMS>();
    logger.info("ChatLogLMS tables: " + this.chatLogLms.size());

    final Query forumPosts;
    if (!hasCR) {
        forumPosts = session.createQuery(
                "from ForumPostsLMS x where x.created>=:readingtimestamp and x.created<=:ceiling order by x.id asc");
        forumPosts.setParameter("readingtimestamp", readingfromtimestamp);
        forumPosts.setParameter("ceiling", readingtotimestamp);
        this.forumPostsLms = forumPosts.list();
    } else {
        String courseClause = "(";
        for (int i = 0; i < courses.size(); i++) {
            courseClause += courses.get(i);
            if (i < courses.size() - 1)
                courseClause += ",";
            else
                courseClause += ")";
        }
        forumPosts = session.createSQLQuery(
                "SELECT posts.id,posts.userid,posts.created,posts.modified,posts.subject,posts.message,posts.discussion from forum_posts as posts JOIN logstore_standard_log as logs ON posts.userid = logs.userid Where logs.courseid in "
                        + courseClause
                        + " and (posts.created = logs.timecreated or posts.modified = logs.timecreated) AND posts.created>=:readingtimestamp and posts.created<=:ceiling");
        forumPosts.setParameter("readingtimestamp", readingfromtimestamp);
        forumPosts.setParameter("ceiling", readingtotimestamp);
        List<Object[]> tmpl = forumPosts.list();
        if (this.forumPostsLms == null)
            this.forumPostsLms = new ArrayList<ForumPostsLMS>();
        for (Object[] obj : tmpl) {
            ForumPostsLMS p = new ForumPostsLMS();
            if (obj[0].getClass().equals(BigInteger.class)) {
                p.setId(((BigInteger) obj[0]).longValue());
            } else {
                p.setId(((Integer) obj[0]).longValue());
            }
            if (obj[0].getClass().equals(BigInteger.class)) {
                p.setUserid(((BigInteger) obj[1]).longValue());
            } else {
                p.setUserid(((Integer) obj[1]).longValue());
            }
            if (obj[0].getClass().equals(BigInteger.class)) {
                p.setCreated(((BigInteger) obj[2]).longValue());
            } else {
                p.setCreated(((Integer) obj[2]).longValue());
            }
            if (obj[0].getClass().equals(BigInteger.class)) {
                p.setModified(((BigInteger) obj[3]).longValue());
            } else {
                p.setModified(((Integer) obj[3]).longValue());
            }
            p.setSubject((String) obj[4]);
            p.setMessage((String) obj[5]);
            if (obj[0].getClass().equals(BigInteger.class)) {
                p.setDiscussion(((BigInteger) obj[6]).longValue());
            } else {
                p.setDiscussion(((Integer) obj[6]).longValue());
            }
            this.forumPostsLms.add(p);

        }
    }
    logger.info("ForumPostsLMS tables: " + this.forumPostsLms.size());

    final Query forumPostsModified;
    if (!hasCR) {
        forumPostsModified = session.createQuery(
                "from ForumPostsLMS x where x.modified>=:readingtimestamp and x.modified<=:ceiling order by x.id asc");
        this.forumPostsLms.addAll(forumPostsModified.list());
    } else {
        String courseClause = "(";
        for (int i = 0; i < courses.size(); i++) {
            courseClause += courses.get(i);
            if (i < courses.size() - 1)
                courseClause += ",";
            else
                courseClause += ")";
        }
        forumPostsModified = session.createSQLQuery(
                "SELECT posts.id,posts.userid,posts.created,posts.modified,posts.subject,posts.message from mdl_forum_posts as posts JOIN mdl_log as logs ON posts.userid = logs.userid Where logs.course in "
                        + courseClause
                        + " and (posts.created = logs.time or posts.modified = logs.time) AND posts.modified>=:readingtimestamp and posts.modified<=:ceiling");
        forumPostsModified.setParameter("readingtimestamp", readingfromtimestamp);
        forumPostsModified.setParameter("ceiling", readingtotimestamp);
        List<Object[]> tmpl = forumPostsModified.list();
        if (this.forumPostsLms == null)
            this.forumPostsLms = new ArrayList<ForumPostsLMS>();
        for (Object[] obj : tmpl) {
            ForumPostsLMS p = new ForumPostsLMS();
            p.setId(((Integer) obj[0]).longValue());
            p.setUserid(((Integer) obj[1]).longValue());
            p.setCreated(((Integer) obj[2]).longValue());
            p.setModified(((Integer) obj[3]).longValue());
            p.setSubject((String) obj[4]);
            p.setMessage((String) obj[5]);

            this.forumPostsLms.add(p);

        }
    }
    logger.info("ForumPostsModifiedLMS tables: " + this.forumPostsLms.size());

    session.clear();

    criteria = session.createCriteria(AssignGradesLMS.class, "obj");
    if (hasCR) {
        List<Long> tmp = new ArrayList<Long>();
        for (AssignLMS assign : assignLms) {
            tmp.add(assign.getId());
        }

        if (!(empty = tmp.isEmpty()))
            criteria.add(Restrictions.in("obj.assignment", tmp));
    }
    criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
    criteria.add(Restrictions.lt("obj.timemodified", readingtotimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    if (!(hasCR && empty))
        this.assignGradesLms = criteria.list();
    else
        this.assignGradesLms = new ArrayList<AssignGradesLMS>();
    logger.info("AssignGradesLMS tables: " + this.assignGradesLms.size());

    criteria = session.createCriteria(GroupsMembersLMS.class, "obj");
    if (hasCR) {
        ArrayList<Long> ids = new ArrayList<Long>();
        for (GroupsLMS e : this.groupLms)
            ids.add(e.getId());
        if (!(empty = ids.isEmpty()))
            criteria.add(Restrictions.in("obj.groupid", ids));
    }
    criteria.add(Restrictions.lt("obj.timeadded", readingtotimestamp));
    criteria.add(Restrictions.gt("obj.timeadded", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    if (!(hasCR && empty))
        this.groupMembersLms = criteria.list();
    else
        this.groupMembersLms = new ArrayList<GroupsMembersLMS>();
    logger.info("GroupsMembersLMS tables: " + this.groupMembersLms.size());

    /*
    criteria = session.createCriteria(QuestionStatesLMS.class, "obj");
    if(hasCR)
    {
       ArrayList<Long> ids = new ArrayList<Long>();
        for(QuizQuestionInstancesLMS e : this.quizQuestionInstancesLms)
     ids.add(e.getQuestion());
        if(!(empty = ids.isEmpty()))
     criteria.add(Restrictions.in("obj.question", ids));
    }
    criteria.add(Restrictions.lt("obj.timestamp", readingtotimestamp));
    criteria.add(Restrictions.gt("obj.timestamp", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    if(!(hasCR && empty))
       this.questionStatesLms = criteria.list();
    else
       this.questionStatesLms = new ArrayList<QuestionStatesLMS>();
    logger.info("QuestionStatesLMS tables: " + this.questionStatesLms.size());
    */

    /*
    criteria = session.createCriteria(AssignmentSubmissionsLMS.class, "obj");
    if(hasCR)
    {
       ArrayList<Long> ids = new ArrayList<Long>();
        for(AssignmentLMS e : this.assignmentLms)
     ids.add(e.getId());
        if(!(empty = ids.isEmpty()))
     criteria.add(Restrictions.in("obj.id", ids));
    }
    criteria.add(Restrictions.lt("obj.timecreated", readingtotimestamp));
    criteria.add(Restrictions.gt("obj.timecreated", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    if(!(hasCR && empty))
       this.assignmentSubmissionLms = criteria.list();
    else
       this.assignmentSubmissionLms = new ArrayList<AssignmentSubmissionsLMS>();
    logger.info("AssignmentSubmissionsLMS tables: " + this.userLms.size());
     */

    criteria = session.createCriteria(QuizGradesLMS.class, "obj");
    if (hasCR) {
        ArrayList<Long> ids = new ArrayList<Long>();
        for (QuizLMS e : this.quizLms)
            ids.add(e.getId());
        if (!(empty = ids.isEmpty()))
            criteria.add(Restrictions.in("obj.quiz", ids));
    }
    criteria.add(Restrictions.lt("obj.timemodified", readingtotimestamp));
    criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    if (!(hasCR && empty))
        this.quizGradesLms = criteria.list();
    else
        this.quizGradesLms = new ArrayList<QuizGradesLMS>();
    logger.info("QuizGradesLMS tables: " + this.quizGradesLms.size());

    criteria = session.createCriteria(ForumDiscussionsLMS.class, "obj");
    if (hasCR) {
        ArrayList<Long> ids = new ArrayList<Long>();
        for (ForumLMS e : this.forumLms)
            ids.add(e.getId());
        if (!(empty = ids.isEmpty()))
            criteria.add(Restrictions.in("obj.forum", ids));
    }
    criteria.add(Restrictions.lt("obj.timemodified", readingtotimestamp));
    criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    if (!(hasCR && empty))
        this.forumDiscussionsLms = criteria.list();
    else
        this.forumDiscussionsLms = new ArrayList<ForumDiscussionsLMS>();
    logger.info("ForumDiscussionsLMS tables: " + this.forumDiscussionsLms.size());

    criteria = session.createCriteria(GradeGradesLMS.class, "obj");
    if (hasCR) {
        ArrayList<Long> ids = new ArrayList<Long>();
        for (GradeItemsLMS e : this.gradeItemsLms)
            ids.add(e.getId());
        if (!(empty = ids.isEmpty()))
            criteria.add(Restrictions.in("obj.itemid", ids));
    }
    criteria.add(Restrictions.lt("obj.timemodified", readingtotimestamp));
    criteria.add(Restrictions.gt("obj.timemodified", readingfromtimestamp));
    criteria.addOrder(Property.forName("obj.id").asc());
    if (!(hasCR && empty))
        this.gradeGradesLms = criteria.list();
    else
        this.gradeGradesLms = new ArrayList<GradeGradesLMS>();
    logger.info("GradeGradesLMS tables: " + this.gradeGradesLms.size());

    session.clear();

    // hibernate session finish and close
    tx.commit();
    session.close();

}

From source file:de.lemo.dms.db.hibernate.HibernateDBHandler.java

License:Open Source License

/**
 * Saves a list of generic objects to the database.
 *///from   ww w .  java2s . c  o  m
@Override
public void saveCollectionToDB(final Session session, final List<Collection<?>> data) {

    try {
        int classOb = 0;
        String className = "";
        Transaction tx = session.beginTransaction();
        int i = 0;
        for (int j = 0; j < data.size(); j++) {
            for (final Object obj : data.get(j)) {

                if (!className.equals("") && !className.equals(obj.getClass().getName())) {
                    this.logger.info("Wrote " + classOb + " objects of class " + className);
                    classOb = 0;
                }
                className = obj.getClass().getName();
                i++;
                classOb++;
                session.saveOrUpdate(obj);
                if ((i % HibernateDBHandler.BATCH_SIZE) == 0) {
                    // flush a batch of inserts and release memory:
                    session.flush();
                    session.clear();
                }
            }
        }
        this.logger.info("Wrote " + classOb + " objects of class " + className + " to database.");
        session.flush();
        tx.commit();
        session.clear();
        //this.closeSession(session);
    } catch (final HibernateException e) {
        logger.error(e.getMessage());
    }
}

From source file:de.lemo.dms.db.hibernate.HibernateDBHandler.java

License:Open Source License

/**
 * Save a single object to the database.
 *//*from www.j  a v a  2 s  .  c  o m*/
@Override
public void saveToDB(final Session session, final Object data) {
    final Transaction tx = session.beginTransaction();
    session.saveOrUpdate(data);
    tx.commit();
    session.clear();
}

From source file:de.lemo.dms.db.hibernate.HibernateDBHandler.java

License:Open Source License

/**
 * Closes the database connection./*from   w  ww.  ja v  a 2  s.c o m*/
 */
@Override
public void closeSession(final Session session) {
    try {
        session.clear();
        session.close();
    } catch (final HibernateException he) {
        logger.error(he.getMessage());
    }
}

From source file:de.lemo.dms.test.HibernateDBHandler.java

License:Open Source License

@Override
/**//w  w  w.j  av a  2  s .  co  m
 * Saves a list of generic objects to the database.
 * 
 */
public void saveCollectionToDB(final Session session, final List<Collection<?>> data) {

    final List<Object> objects = new ArrayList<Object>();
    session.clear();
    try {

        for (final Iterator<Collection<?>> iter = data.iterator(); iter.hasNext();) {
            final Collection<?> l = iter.next();
            for (final Iterator<?> iter2 = l.iterator(); iter2.hasNext();) {
                final Object o = iter2.next();

                objects.add(o);
            }
        }
        final Transaction tx = session.beginTransaction();
        int classOb = 0;
        String className = "";
        for (int i = 0; i < objects.size(); i++) {

            if (!className.equals("") && !className.equals(objects.get(i).getClass().getName())) {
                logger.info("Wrote " + classOb + " objects of class " + className);
                classOb = 0;
            }
            className = objects.get(i).getClass().getName();

            classOb++;
            session.save(objects.get(i));

            if ((i % 50) == 0) {
                // flush a batch of inserts and release memory:
                session.flush();
                session.clear();
            }
        }
        logger.info("Wrote " + classOb + " objects of class " + className + " to database.");
        tx.commit();
        session.clear();

    } catch (final HibernateException e) {
        logger.error(e.getMessage());
    }

}

From source file:de.lemo.dms.test.HibernateDBHandler.java

License:Open Source License

@Override
/**//from   ww w.  ja  v  a2s .c  o  m
 * Save a single object to the database.
 */
public void saveToDB(final Session session, final Object data) {
    final Transaction tx = session.beginTransaction();
    session.saveOrUpdate(data);
    tx.commit();
    session.clear();

}

From source file:de.lemo.dms.test.TestDataCreatorChemgapedia.java

License:Open Source License

/**
 * Extracts Mining-data from the Mining-database
 *//*from  w w w.j  a  v a  2 s .c o m*/
@SuppressWarnings("unchecked")
public void getDataFromDB() {
    final IDBHandler dbHandler = ServerConfiguration.getInstance().getMiningDbHandler();

    // accessing DB by creating a session and a transaction using HibernateUtil
    final Session session = dbHandler.getMiningSession();
    session.clear();

    final Query resLogQuery = session.createQuery("from ResourceLogMining x order by x.id asc");
    this.resourceLogList = (ArrayList<ResourceLogMining>) resLogQuery.list();

    final Query resQuery = session.createQuery("from ResourceMining x order by x.id asc");
    this.resourceList = (ArrayList<ResourceMining>) resQuery.list();

    final Query degCouQuery = session.createQuery("from LevelCourseMining x order by x.id asc");
    this.degreeCourseList = (ArrayList<LevelCourseMining>) degCouQuery.list();

    final Query depDegQuery = session.createQuery("from DepartmentDegreeMining x order by x.id asc");
    this.departmentDegreeList = (ArrayList<LevelAssociationMining>) depDegQuery.list();

    final Query couResQuery = session.createQuery("from CourseResourceMining x order by x.id asc");
    this.courseResourceList = (ArrayList<CourseResourceMining>) couResQuery.list();

    for (final CourseResourceMining cr : this.courseResourceList) {
        this.couResMap.put(cr.getResource().getId(), cr.getCourse());
    }
    for (final LevelCourseMining dc : this.degreeCourseList) {
        this.degCouMap.put(dc.getCourse().getId(), dc.getLevel());
    }
    for (final LevelAssociationMining dd : this.departmentDegreeList) {
        this.depDegMap.put(dd.getLower().getId(), dd.getUpper());
    }
}

From source file:de.lemo.dms.test.TestDataCreatorMoodle.java

License:Open Source License

@SuppressWarnings("unchecked")
public void getDataFromDB() {
    final IDBHandler dbHandler = ServerConfiguration.getInstance().getMiningDbHandler();

    // accessing DB by creating a session and a transaction using HibernateUtil
    final Session session = dbHandler.getMiningSession();
    session.clear();

    final Query assQuery = session.createQuery("from AssignmentMining x order by x.id asc");
    this.assignmentList = (ArrayList<AssignmentMining>) assQuery.list();

    final Query assLogQuery = session.createQuery("from AssignmentLogMining x order by x.id asc");
    this.assignmentLogList = (ArrayList<AssignmentLogMining>) assLogQuery.list();

    final Query chaQuery = session.createQuery("from ChatMining x order by x.id asc");
    this.chatList = (ArrayList<ChatMining>) chaQuery.list();

    final Query chaLogQuery = session.createQuery("from ChatLogMining x order by x.id asc");
    this.chatLogList = (ArrayList<ChatLogMining>) chaLogQuery.list();

    final Query couQuery = session.createQuery("from CourseMining x order by x.id asc");
    this.courseList = (ArrayList<CourseMining>) couQuery.list();

    final Query couLogQuery = session.createQuery("from CourseLogMining x order by x.id asc");
    this.courseLogList = (ArrayList<CourseLogMining>) couLogQuery.list();

    final Query couAssQuery = session.createQuery("from CourseAssignmentMining x order by x.id asc");
    this.courseAssignmentList = (ArrayList<CourseAssignmentMining>) couAssQuery.list();

    final Query couForumQuery = session.createQuery("from CourseForumMining x order by x.id asc");
    this.courseForumList = (ArrayList<CourseForumMining>) couForumQuery.list();

    final Query couGroupQuery = session.createQuery("from CourseGroupMining x order by x.id asc");
    this.courseGroupList = (ArrayList<CourseGroupMining>) couGroupQuery.list();

    final Query couQuizQuery = session.createQuery("from CourseQuizMining x order by x.id asc");
    this.courseQuizList = (ArrayList<CourseQuizMining>) couQuizQuery.list();

    final Query couResQuery = session.createQuery("from CourseResourceMining x order by x.id asc");
    this.courseResourceList = (ArrayList<CourseResourceMining>) couResQuery.list();

    final Query couScormQuery = session.createQuery("from CourseScormMining x order by x.id asc");
    this.courseScormList = (ArrayList<CourseScormMining>) couScormQuery.list();

    final Query couUserQuery = session.createQuery("from CourseUserMining x order by x.id asc");
    this.courseUserList = (ArrayList<CourseUserMining>) couUserQuery.list();

    final Query couWikiQuery = session.createQuery("from CourseWikiMining x order by x.id asc");
    this.courseWikiList = (ArrayList<CourseWikiMining>) couWikiQuery.list();

    final Query degQuery = session.createQuery("from DegreeMining x order by x.id asc");
    this.degreeList = (ArrayList<LevelMining>) degQuery.list();

    final Query degCouQuery = session.createQuery("from DegreeCourseMining x order by x.id asc");
    this.degreeCourseList = (ArrayList<LevelCourseMining>) degCouQuery.list();

    final Query depQuery = session.createQuery("from DepartmentMining x order by x.id asc");
    this.departmentList = (ArrayList<LevelMining>) depQuery.list();

    final Query depDegQuery = session.createQuery("from DepartmentDegreeMining x order by x.id asc");
    this.departmentDegreeList = (ArrayList<LevelAssociationMining>) depDegQuery.list();

    final Query forQuery = session.createQuery("from ForumMining x order by x.id asc");
    this.forumList = (ArrayList<ForumMining>) forQuery.list();

    final Query forLogQuery = session.createQuery("from ForumLogMining x order by x.id asc");
    this.forumLogList = (ArrayList<ForumLogMining>) forLogQuery.list();

    final Query groupQuery = session.createQuery("from GroupMining x order by x.id asc");
    this.groupList = (ArrayList<GroupMining>) groupQuery.list();

    final Query groupUserQuery = session.createQuery("from GroupUserMining x order by x.id asc");
    this.groupUserList = (ArrayList<GroupUserMining>) groupUserQuery.list();

    final Query queQuery = session.createQuery("from QuestionMining x order by x.id asc");
    this.questionList = (ArrayList<QuestionMining>) queQuery.list();

    final Query queLogQuery = session.createQuery("from QuestionLogMining x order by x.id asc");
    this.questionLogList = (ArrayList<QuestionLogMining>) queLogQuery.list();

    final Query quiLogQuery = session.createQuery("from QuizLogMining x order by x.id asc");
    this.quizLogList = (ArrayList<QuizLogMining>) quiLogQuery.list();

    final Query quiQuery = session.createQuery("from QuizMining x order by x.id asc");
    this.quizList = (ArrayList<QuizMining>) quiQuery.list();

    final Query quiQuestionQuery = session.createQuery("from QuizQuestionMining x order by x.id asc");
    this.quizQuestionList = (ArrayList<QuizQuestionMining>) quiQuestionQuery.list();

    final Query quiUserQuery = session.createQuery("from QuizUserMining x order by x.id asc");
    this.quizUserList = (ArrayList<QuizUserMining>) quiUserQuery.list();

    final Query resQuery = session.createQuery("from ResourceMining x order by x.id asc");
    this.resourceList = (ArrayList<ResourceMining>) resQuery.list();

    final Query resLogQuery = session.createQuery("from ResourceLogMining x order by x.id asc");
    this.resourceLogList = (ArrayList<ResourceLogMining>) resLogQuery.list();

    final Query roleQuery = session.createQuery("from RoleMining x order by x.id asc");
    this.roleList = (ArrayList<RoleMining>) roleQuery.list();

    final Query scormQuery = session.createQuery("from ScormMining x order by x.id asc");
    this.scormList = (ArrayList<ScormMining>) scormQuery.list();

    final Query scormLogQuery = session.createQuery("from ScormLogMining x order by x.id asc");
    this.scormLogList = (ArrayList<ScormLogMining>) scormLogQuery.list();

    final Query userQuery = session.createQuery("from UserMining x order by x.id asc");
    this.userList = (ArrayList<UserMining>) userQuery.list();

    final Query wikQuery = session.createQuery("from WikiMining x order by x.id asc");
    this.wikiList = (ArrayList<WikiMining>) wikQuery.list();

    final Query wikLogQuery = session.createQuery("from WikiLogMining x order by x.id asc");
    this.wikiLogList = (ArrayList<WikiLogMining>) wikLogQuery.list();

    this.couAssMap = new HashMap<Long, CourseMining>();
    this.couForMap = new HashMap<Long, CourseMining>();
    this.couGroMap = new HashMap<Long, CourseMining>();
    this.couQuiMap = new HashMap<Long, CourseMining>();
    this.couResMap = new HashMap<Long, CourseMining>();
    this.couScoMap = new HashMap<Long, CourseMining>();
    this.couUseMap = new HashMap<Long, CourseMining>();
    this.couWikMap = new HashMap<Long, CourseMining>();

    this.degCouMap = new HashMap<Long, LevelMining>();
    this.depDegMap = new HashMap<Long, LevelMining>();

    for (final CourseAssignmentMining ca : this.courseAssignmentList) {
        this.couAssMap.put(ca.getAssignment().getId(), ca.getCourse());
    }// w  ww .  j av a  2s  .com

    for (final CourseGroupMining ca : this.courseGroupList) {
        this.couGroMap.put(ca.getGroup().getId(), ca.getCourse());
    }

    for (final CourseQuizMining ca : this.courseQuizList) {
        this.couQuiMap.put(ca.getQuiz().getId(), ca.getCourse());
    }

    for (final CourseUserMining ca : this.courseUserList) {
        this.couUseMap.put(ca.getUser().getId(), ca.getCourse());
    }

    for (final CourseForumMining ca : this.courseForumList) {
        this.couForMap.put(ca.getForum().getId(), ca.getCourse());
    }

    for (final CourseResourceMining cr : this.courseResourceList) {
        this.couResMap.put(cr.getResource().getId(), cr.getCourse());
    }

    for (final CourseWikiMining cw : this.courseWikiList) {
        this.couWikMap.put(cw.getWiki().getId(), cw.getCourse());
    }

    for (final CourseScormMining cs : this.courseScormList) {
        this.couScoMap.put(cs.getScorm().getId(), cs.getCourse());
    }

    for (final LevelCourseMining dc : this.degreeCourseList) {
        this.degCouMap.put(dc.getCourse().getId(), dc.getLevel());
    }

    for (final LevelAssociationMining dd : this.departmentDegreeList) {
        this.depDegMap.put(dd.getLower().getId(), dd.getUpper());
    }

    session.clear();
    session.close();

}

From source file:de.nava.informa.utils.manager.hibernate.HibernateUtil.java

License:Open Source License

/**
 * Opens new session or returns currently open session for this thread.
 *
 * @return session object.//from w  ww  .  j  a v  a2 s .  c o m
 * @throws HibernateException if something with session creation goes wrong.
 */
public static Session openSession() throws HibernateException {
    if (!inited)
        init();

    Session s = SESSION.get();
    if (s != null) {
        LOG.log(Level.WARNING, "Openning session more than once from the same thread!", new Exception("Dump"));

        s.clear();

        return s;
    } else {
        try {
            lock.lock();
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted waiting for session.");
        }

        s = sessionHandler.getSession();
        SESSION.set(s);
    }

    return s;
}