Example #1
0
  @Override
  public void saveEventDetails(Event event) {
    // TODO Auto-generated method stub

    // Opening a session to create a database connection
    Session session = this.sessionFactory.openSession();

    // for CRUD operations, we need to create a transaction
    Transaction tx = null;

    // adding a try catch block for maintaining the atomicity of transaction
    try {
      tx = session.beginTransaction();
      System.out.println("DbHelper..saveEventDetails");

      // session save inserts the object into DB
      session.save(event);

      tx.commit();
      System.out.println("Event saved");
    } catch (HibernateException e) {
      if (tx != null) {
        tx.rollback();
        e.printStackTrace();
      }
    } finally {
      session.close();
    }
  }
Example #2
0
 public List<User> getUserSearchResults(int topicId) {
   // TODO Auto-generated method stub
   List<User> userArr = new ArrayList<User>();
   Session session = sessionFactory.openSession();
   Transaction tx = null;
   try {
     tx = session.beginTransaction();
     Query query1 = session.createQuery("select userId from UserTopic where topicId = :topicId");
     query1.setString("topicId", String.valueOf(topicId));
     Query query2 = session.createQuery("from User where userId in (:userList)");
     query2.setParameterList("userList", query1.list());
     userArr = query2.list();
     /*for(Topic topic : topicArr){
     	System.out.println("Topic Description----"+topic.getTopicDescription());
     	System.out.println("Topic Id----"+topic.getTopicId());
     }*/
   } catch (HibernateException e) {
     if (tx != null) {
       tx.rollback();
       e.printStackTrace();
     }
   } finally {
     session.close();
   }
   return userArr;
 }
Example #3
0
  @Override
  public List<Forum> getForumsOfaUser(int userId) {
    // TODO Auto-generated method stub
    List<Forum> forums = new ArrayList<Forum>();
    Session session = sessionFactory.openSession();
    Transaction tx = null;
    try {
      tx = session.beginTransaction();
      Query query = session.createQuery("from Subscription where userId = :userId");
      query.setString("userId", String.valueOf(userId));
      List<Subscription> userSubscribedGroups = query.list();
      for (Subscription subsc : userSubscribedGroups) {
        query = session.createQuery("from Forum where forumId = :forumId");
        query.setParameter("forumId", subsc.getForumId());
        // add this to a list variable query.uniqueResult();
        forums.add((Forum) query.uniqueResult());
      }

    } catch (HibernateException e) {
      if (tx != null) {
        tx.rollback();
        e.printStackTrace();
      }
    } finally {
      session.close();
    }
    return forums;
  }
Example #4
0
 @Override
 public List<Reply> getRepliesToPost(int postId) {
   // TODO Auto-generated method stub
   Session session = sessionFactory.openSession();
   Transaction tx = null;
   List<Reply> replyArr = new ArrayList<Reply>();
   try {
     tx = session.beginTransaction();
     Query query =
         session.createQuery("from Reply where postId = :postId order by createdDate desc");
     query.setParameter("postId", postId);
     replyArr = query.list();
     /*			for(Reply reply : replyArr){
     	System.out.println("Reply - id----"+reply.getReplyId());
     	System.out.println("Reply - Description----"+reply.getDescription());
     	System.out.println("Reply - Post id----"+reply.getPostId());
     }*/
   } catch (HibernateException e) {
     if (tx != null) {
       tx.rollback();
       e.printStackTrace();
     }
   } finally {
     session.close();
   }
   return replyArr;
 }
Example #5
0
  public int getCountOfSubscribers(int forumId, int userId) {
    // TODO Auto-generated method stub
    Session session = this.sessionFactory.openSession();
    Transaction tx = null;
    int countOfSubscribers = 0;
    try {
      tx = session.beginTransaction();
      Query query = null;
      if (userId == 0) {
        query = session.createQuery("select count(*) from Subscription where forumId = :forumId");
        query.setParameter("forumId", forumId);
      } else {
        query =
            session.createQuery(
                "select count(*) from Subscription where forumId = :forumId and userId = :userId");
        query.setParameter("forumId", forumId);
        query.setParameter("userId", userId);
      }

      Long count = (Long) query.uniqueResult();
      countOfSubscribers = count.intValue();
      // System.out.println("No of subscribers.."+countOfSubscribers);
    } catch (HibernateException e) {
      if (tx != null) {
        tx.rollback();
        e.printStackTrace();
      }
    } finally {
      session.close();
    }
    return countOfSubscribers;
  }
Example #6
0
 @Override
 public void deleteSubscription(int userId, int forumId) {
   // TODO Auto-generated method stub
   Session session = this.sessionFactory.openSession();
   Transaction tx = null;
   try {
     tx = session.beginTransaction();
     Query query =
         session.createQuery(
             "delete from Subscription where userId = :userId and forumId = :forumId");
     query.setParameter("userId", userId);
     query.setParameter("forumId", forumId);
     int result = query.executeUpdate();
     System.out.println("Execute query..");
     if (result > 0) {
       tx.commit();
       // System.out.println("Subscription Removed..");
     } else {
       // System.out.println("Subscription does not exist");
       tx.rollback();
     }
   } catch (HibernateException e) {
     if (tx != null) {
       tx.rollback();
       e.printStackTrace();
     }
   } finally {
     session.close();
   }
 }
Example #7
0
 @Override
 public void saveUserTopic(UserTopic userTopic) {
   Session session = this.sessionFactory.openSession();
   Transaction tx = null;
   try {
     tx = session.beginTransaction();
     session.save(userTopic);
     tx.commit();
     // System.out.println("Forum Committed..")
   } catch (HibernateException e) {
     if (tx != null) {
       tx.rollback();
       e.printStackTrace();
     }
   } finally {
     session.close();
   }
 }
Example #8
0
 @Override
 public void savePostDetails(Post post) {
   // TODO Auto-generated method stub
   Session session = this.sessionFactory.openSession();
   Transaction tx = null;
   try {
     tx = session.beginTransaction();
     session.save(post);
     tx.commit();
     // System.out.println("Post Committed..");
   } catch (HibernateException e) {
     if (tx != null) {
       tx.rollback();
       e.printStackTrace();
     }
   } finally {
     session.close();
   }
 }
Example #9
0
 @Override
 public List<Event> getEventDetails(int userId) {
   // TODO Auto-generated method stub
   List<Event> events = new ArrayList<Event>();
   Session session = sessionFactory.openSession();
   Transaction tx = null;
   try {
     tx = session.beginTransaction();
     Query query = session.createQuery("from Event");
     events = query.list();
   } catch (HibernateException e) {
     if (tx != null) {
       tx.rollback();
       e.printStackTrace();
     }
   } finally {
     session.close();
   }
   return events;
 }
Example #10
0
 public Forum getForumById(int forumId) {
   // TODO Auto-generated method stub
   Session session = sessionFactory.openSession();
   Transaction tx = null;
   Forum forum = new Forum();
   try {
     tx = session.beginTransaction();
     forum = (Forum) session.get(Forum.class, forumId);
     // System.out.println("First Name - "+user.getFirstname());
     // System.out.println("Last Name - "+user.getLastname());
   } catch (HibernateException e) {
     if (tx != null) {
       tx.rollback();
       e.printStackTrace();
     }
   } finally {
     session.close();
   }
   return forum;
 }
Example #11
0
 public User getUser(String username) {
   // TODO Auto-generated method stub
   System.out.println("DbHelper->getUser...");
   Session session = sessionFactory.openSession();
   Transaction tx = null;
   User user = new User();
   try {
     tx = session.beginTransaction();
     Query query = session.createQuery("from User where emailId = :emailId");
     query.setString("emailId", username);
     user = (User) query.uniqueResult();
   } catch (HibernateException e) {
     if (tx != null) {
       tx.rollback();
       e.printStackTrace();
     }
   } finally {
     session.close();
   }
   return user;
 }
Example #12
0
 public List<Domain> getDomains() {
   List<Domain> domainArr = new ArrayList<Domain>();
   Session session = sessionFactory.openSession();
   Transaction tx = null;
   try {
     tx = session.beginTransaction();
     Query query = session.createQuery("from Domain");
     domainArr = query.list();
     /*for(Topic topic : topicArr){
     	System.out.println("Topic Description----"+topic.getTopicDescription());
     	System.out.println("Topic Id----"+topic.getTopicId());
     }*/
   } catch (HibernateException e) {
     if (tx != null) {
       tx.rollback();
       e.printStackTrace();
     }
   } finally {
     session.close();
   }
   return domainArr;
 }
Example #13
0
 @Override
 public User getUser(int userId) {
   // TODO Auto-generated method stub
   System.out.println("DbHelper->getUser...");
   Session session = sessionFactory.openSession();
   Transaction tx = null;
   User user = new User();
   try {
     tx = session.beginTransaction();
     user = (User) session.get(User.class, userId);
     // System.out.println("First Name - "+user.getFirstname());
     // System.out.println("Last Name - "+user.getLastname());
   } catch (HibernateException e) {
     if (tx != null) {
       tx.rollback();
       e.printStackTrace();
     }
   } finally {
     session.close();
   }
   return user;
 }
Example #14
0
  @SuppressWarnings("unchecked")
  public List<PostAndReply> getPostsAndReplies(int forumId) {
    // TODO Auto-generated method stub
    Session session = sessionFactory.openSession();
    Transaction tx = null;
    List<Post> postArr = new ArrayList<Post>();
    List<PostAndReply> prArray = new ArrayList<PostAndReply>();

    try {
      tx = session.beginTransaction();
      Query query =
          session.createQuery("from Post where forumId = :forumId order by createdDate desc");
      query.setParameter("forumId", forumId);
      postArr = query.list();
      System.out.println("No of posts.." + postArr.size());
      for (Post post : postArr) {
        int postId = post.getPostId();
        System.out.println("Post id----" + postId);
        List<Reply> replies = getRepliesToPost(postId);
        User user = getUser(post.getCreatedBy());
        String postCreatorName = user.getFirstname() + ' ' + user.getLastname();
        PostAndReply pr = new PostAndReply();
        pr.setPost(post);
        pr.setReplies(replies);
        pr.setPostCreatorName(postCreatorName);
        prArray.add(pr);
        // System.out.println("Post - Description----"+post.getDescription());
        // System.out.println("No of Replies----"+replies.size());
      }
    } catch (HibernateException e) {
      if (tx != null) {
        tx.rollback();
        e.printStackTrace();
      }
    } finally {
      session.close();
    }
    return prArray;
  }
Example #15
0
 public List<Forum> getSearchResults(int topicId) {
   List<Forum> forumArr = new ArrayList<Forum>();
   Session session = sessionFactory.openSession();
   Transaction tx = null;
   try {
     tx = session.beginTransaction();
     Query query = session.createQuery("from Forum where topicId = :topicId");
     query.setString("topicId", String.valueOf(topicId));
     forumArr = query.list();
     /*for(Topic topic : topicArr){
     	System.out.println("Topic Description----"+topic.getTopicDescription());
     	System.out.println("Topic Id----"+topic.getTopicId());
     }*/
   } catch (HibernateException e) {
     if (tx != null) {
       tx.rollback();
       e.printStackTrace();
     }
   } finally {
     session.close();
   }
   return forumArr;
 }
Example #16
0
 @Override
 public List<Topic> getTopics() {
   // TODO Auto-generated method stub
   List<Topic> topicArr = new ArrayList<Topic>();
   Session session = sessionFactory.openSession();
   Transaction tx = null;
   try {
     tx = session.beginTransaction();
     Query query = session.createQuery("from Topic");
     topicArr = query.list();
     /*for(Topic topic : topicArr){
     	System.out.println("Topic Description----"+topic.getTopicDescription());
     	System.out.println("Topic Id----"+topic.getTopicId());
     }*/
   } catch (HibernateException e) {
     if (tx != null) {
       tx.rollback();
       e.printStackTrace();
     }
   } finally {
     session.close();
   }
   return topicArr;
 }
Example #17
0
 @SuppressWarnings("unchecked")
 @Override
 public List<Post> getPostsInForum(int forumId) {
   // TODO Auto-generated method stub
   // System.out.println("DbHelper->getPostsInForum...");
   Session session = sessionFactory.openSession();
   Transaction tx = null;
   List<Post> postArr = new ArrayList<Post>();
   try {
     tx = session.beginTransaction();
     Query query =
         session.createQuery("from Post where forumId = :forumId order by createdDate desc");
     query.setParameter("forumId", forumId);
     postArr = query.list();
   } catch (HibernateException e) {
     if (tx != null) {
       tx.rollback();
       e.printStackTrace();
     }
   } finally {
     session.close();
   }
   return postArr;
 }