예제 #1
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;
  }
예제 #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();
   }
 }
예제 #3
0
  public List<Team> getTeams() {
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    List result = null;
    try {
      Transaction transaction = session.beginTransaction();
      result = session.createQuery("from Team").list();
      session.flush();
      transaction.commit();
    } catch (HibernateException e) {
      e.printStackTrace();
    } finally {
      if (session.isOpen()) {
        session.close();
      }
    }

    List<Team> teams = new ArrayList<Team>();
    for (Object o : result) {
      if (o instanceof Team) {
        teams.add((Team) o);
      }
    }

    return teams;
  }
예제 #4
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;
  }
예제 #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;
  }
예제 #6
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;
 }
 @SuppressWarnings("unchecked")
 public List<MessageBundleProperty> getModifiedProperties(
     int sortOrder, int sortField, int startingIndex, int pageSize) {
   String orderBy = "asc";
   if (sortOrder == SORT_ORDER_DESCENDING) {
     orderBy = "desc";
   }
   String sortFieldName = "id";
   if (sortField == SORT_FIELD_MODULE) {
     sortFieldName = "moduleName";
   }
   if (sortField == SORT_FIELD_PROPERTY) {
     sortFieldName = "propertyName";
   }
   if (sortField == SORT_FIELD_LOCALE) {
     sortFieldName = "locale";
   }
   if (sortField == SORT_FIELD_BASENAME) {
     sortFieldName = "baseName";
   }
   org.hibernate.Query query = null;
   String queryString =
       "from MessageBundleProperty where value != null order by " + sortFieldName + " " + orderBy;
   try {
     query = getSession().createQuery(queryString);
     query.setFirstResult(startingIndex);
     query.setMaxResults(pageSize);
     return query.list();
   } catch (HibernateException e) {
     throw new RuntimeException(e.getMessage(), e);
   }
 }
예제 #8
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();
    }
  }
  public static GroovyAwareJavassistProxyFactory buildProxyFactory(
      PersistentClass persistentClass) {
    GroovyAwareJavassistProxyFactory proxyFactory = new GroovyAwareJavassistProxyFactory();

    @SuppressWarnings("unchecked")
    Set<Class<HibernateProxy>> proxyInterfaces = new HashSet<Class<HibernateProxy>>();
    proxyInterfaces.add(HibernateProxy.class);

    final Class<?> javaClass = persistentClass.getMappedClass();
    final Property identifierProperty = persistentClass.getIdentifierProperty();
    final Method idGetterMethod =
        identifierProperty != null ? identifierProperty.getGetter(javaClass).getMethod() : null;
    final Method idSetterMethod =
        identifierProperty != null ? identifierProperty.getSetter(javaClass).getMethod() : null;
    final Type identifierType =
        persistentClass.hasEmbeddedIdentifier() ? persistentClass.getIdentifier().getType() : null;

    try {
      proxyFactory.postInstantiate(
          persistentClass.getEntityName(),
          javaClass,
          proxyInterfaces,
          idGetterMethod,
          idSetterMethod,
          identifierType instanceof CompositeType ? (CompositeType) identifierType : null);
    } catch (HibernateException e) {
      LOG.warn("Cannot instantiate proxy factory: " + e.getMessage());
      return null;
    }

    return proxyFactory;
  }
예제 #10
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();
      }
    }
  }
예제 #11
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;
  }
예제 #12
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;
 }
 protected int executeCountQuery(String query) {
   Integer count = null;
   try {
     count = (Integer) getSession().createQuery(query).uniqueResult();
   } catch (HibernateException e) {
     throw new RuntimeException(e.getMessage(), e);
   }
   return count.intValue();
 }
예제 #14
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();
     }
   }
 }
예제 #15
0
  public Boolean addOrUpdateMedia(Session session, Media media) {
    try {
      // Need to save the loans before saving the media
      Map<Integer, Loan> tabEx = media.getTabExemplaries();
      for (Iterator<Loan> i = tabEx.values().iterator(); i.hasNext(); )
        session.saveOrUpdate(i.next());

      // Persist the media
      session.saveOrUpdate(media);

    } catch (HibernateException pe) {
      System.err.println("Problème dans la sauvegarde ");
      pe.printStackTrace();
      return false;
    }
    return true;
  }
예제 #16
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();
   }
 }
예제 #17
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();
   }
 }
예제 #18
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;
 }
예제 #19
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;
 }
예제 #20
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;
 }
예제 #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;
 }
예제 #22
0
  @SuppressWarnings("unchecked")
  public Boolean delMedia(Session session, Media media) {
    try {
      // Need to delete the loans before deleting the media
      Map<Integer, Loan> tabEx = media.getTabExemplaries();
      for (Iterator<Loan> i = tabEx.values().iterator(); i.hasNext(); ) session.delete(i.next());

      // Also, all the bookings of the media need to be deleted
      List<Booking> bookings =
          (List<Booking>)
              session.createQuery("from Booking where media=:m").setParameter("m", media).list();
      for (Booking b : bookings) session.delete(b);

      session.delete(media);

    } catch (HibernateException pe) {
      System.err.println("Problème dans la suppression ");
      pe.printStackTrace();
      return false;
    }
    return true;
  }
예제 #23
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;
  }
예제 #24
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;
 }
예제 #25
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;
 }
예제 #26
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;
  }
예제 #27
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;
 }
예제 #28
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;
 }
  public int getSearchCount(String searchQuery, String module, String baseName, String locale) {
    List<String> values = new ArrayList<String>();
    List<NullableType> types = new ArrayList<NullableType>();
    StringBuffer queryString = new StringBuffer("");

    try {
      if (searchQuery != null && searchQuery.length() > 0) {
        queryString.append("(defaultValue like ? OR value like ? OR propertyName = ?)");
        values.add("%" + searchQuery + "%");
        values.add("%" + searchQuery + "%");
        values.add(searchQuery);
        types.add(Hibernate.STRING);
        types.add(Hibernate.STRING);
        types.add(Hibernate.STRING);
      }
      if (module != null && module.length() > 0) {
        if (queryString.length() > 0) {
          queryString.append(" AND ");
        }
        queryString.append("moduleName = ? ");
        values.add(module);
        types.add(Hibernate.STRING);
      }
      if (baseName != null && baseName.length() > 0) {
        if (queryString.length() > 0) {
          queryString.append(" AND ");
        }
        queryString.append("baseName = ?");
        values.add(baseName);
        types.add(Hibernate.STRING);
      }
      if (locale != null && locale.length() > 0) {
        if (queryString.length() > 0) {
          queryString.append(" AND ");
        }
        queryString.append("locale = ?");
        values.add(locale);
        types.add(Hibernate.STRING);
      }

      if (queryString.length() > 0) {
        queryString.insert(0, "select count(*) from MessageBundleProperty where ");
      } else {
        queryString.insert(0, "select count(*) from MessageBundleProperty");
      }

      Integer count = null;
      try {
        Query query = getSession().createQuery(queryString.toString());
        query.setParameters(values.toArray(), (Type[]) types.toArray(new Type[types.size()]));
        count = (Integer) query.uniqueResult();
      } catch (HibernateException e) {
        throw new RuntimeException(e.getMessage(), e);
      }
      return count.intValue();

    } catch (Exception e) {
      logger.error("problem searching the message bundle data", e);
    }
    return 0;
  }