Example #1
0
  public void saveEvent(Event event) {
    Event event1 = getEvent(event);

    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      if (event1 == null) {
        Transaction transaction = session.beginTransaction();
        try {
          session.save(event);
          transaction.commit();
        } catch (HibernateException e) {
          transaction.rollback();
          e.printStackTrace();
        }
      } else {
        Transaction transaction = session.beginTransaction();
        try {
          session.update(event);
          transaction.commit();
        } catch (HibernateException e) {
          transaction.rollback();
          e.printStackTrace();
        }
      }
    } finally {
      if (session.isOpen()) {
        session.flush();
        session.disconnect();
        session.close();
      }
    }
  }
Example #2
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 #3
0
  public boolean createUser(User user) {
    boolean result = false;
    if (user.getEmployeeId() != null) {
      User userById = getUserByEmployeeId(user.getEmployeeId());
      if (userById != null) {
        MessageUtils.createMessage("Brugeren eksisterer allerede.");
        return false;
      }
    }
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    Transaction transaction = session.beginTransaction();
    try {
      session.saveOrUpdate(user);
      result = true;
      session.flush();
      transaction.commit();
    } catch (StaleObjectStateException sose) {
      System.out.println("StaleObjectException");
      transaction.rollback();
    } catch (HibernateException e) {
      e.printStackTrace();
      transaction.rollback();
      result = false;
    } finally {
      if (session.isOpen()) {
        session.close();
      }
    }

    return result;
  }
Example #4
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 #5
0
  public User getUser(String username) {
    User result = null;
    Session session;
    Object user;
    session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      Transaction transaction = null;
      user = null;
      try {
        transaction = session.beginTransaction();
        Query query =
            session.createQuery("from User as user where user.userName='******'");
        user = query.uniqueResult();
        transaction.commit();
      } catch (HibernateException e) {
        transaction.rollback();
        e.printStackTrace();
      }

      if (user != null) {
        result = (User) user;
      }
    } finally {
      if (session.isOpen()) {
        if (session.isOpen()) {
          session.disconnect();
          session.close();
        }
      }
    }
    return result;
  }
Example #6
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 #7
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;
 }
  /** {@inheritDoc} */
  @Override
  public void txEnd(GridCacheTx tx, boolean commit) throws GridException {
    init();

    Session ses = tx.removeMeta(ATTR_SES);

    if (ses != null) {
      Transaction hTx = ses.getTransaction();

      if (hTx != null) {
        try {
          if (commit) {
            ses.flush();

            hTx.commit();
          } else hTx.rollback();

          if (log.isDebugEnabled())
            log.debug("Transaction ended [xid=" + tx.xid() + ", commit=" + commit + ']');
        } catch (HibernateException e) {
          throw new GridException(
              "Failed to end transaction [xid=" + tx.xid() + ", commit=" + commit + ']', e);
        } finally {
          ses.close();
        }
      }
    }
  }
Example #9
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 #10
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;
  }
  /**
   * Rolls back hibernate session.
   *
   * @param ses Hibernate session.
   * @param tx Cache ongoing transaction.
   */
  private void rollback(SharedSessionContract ses, GridCacheTx tx) {
    // Rollback only if there is no cache transaction,
    // otherwise txEnd() will do all required work.
    if (tx == null) {
      Transaction hTx = ses.getTransaction();

      if (hTx != null && hTx.isActive()) hTx.rollback();
    }
  }
Example #12
0
  public boolean saveUser(User user) {
    boolean result = false;

    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    Transaction transaction = session.beginTransaction();
    try {
      session.update(user);
      result = true;
      session.flush();
      transaction.commit();
    } catch (StaleObjectStateException sose) {
      System.out.println("StaleObjectException");
      transaction.rollback();
    } catch (HibernateException e) {
      e.printStackTrace();
      transaction.rollback();
      result = false;
    } finally {
      if (session.isOpen()) {
        session.close();
      }
    }
    return result;
  }
Example #13
0
 public void saveObject(Object entity) {
   Session session = SessionFactoryUtil.getInstance().getCurrentSession();
   Transaction transaction = null;
   try {
     transaction = session.beginTransaction();
     session.saveOrUpdate(entity);
     transaction.commit();
   } catch (HibernateException e) {
     transaction.rollback();
     e.printStackTrace();
   } finally {
     if (session.isOpen()) {
       session.disconnect();
       session.close();
     }
   }
 }
Example #14
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 #15
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 #16
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 #17
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 #18
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 #19
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 #20
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 #21
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 #22
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 #23
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;
 }
Example #24
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;
 }
 private void manejaExcepcion(HibernateException he) throws HibernateException {
   tx.rollback();
   throw new HibernateException("Ocurrio un error en la capa DAO", he);
 }