Ejemplo n.º 1
1
  public static Topic createFromDB(int topicID) {
    Topic topic = null;
    Transaction tx = null;
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    try {
      tx = session.getTransaction();
      Query q =
          session.createQuery(
              "select t, count(p.postID) "
                  + "  from Topic as t, Post as p "
                  + " where t.topicID = p.topicID "
                  + "   and t.topicID = :topicid "
                  + " group by t.topicID");
      q.setInteger("topicid", topicID);

      for (Iterator<Object[]> it = q.iterate(); it.hasNext(); ) {
        Object[] el = it.next();
        topic = (Topic) el[0];
        Long replies = (Long) el[1];
        topic.setReplies(replies == null ? 0 : replies.intValue());
      }

    } catch (HibernateException e) {
      e.printStackTrace();
      if (tx != null && tx.isActive()) tx.rollback();
    }
    return topic;
  }
Ejemplo n.º 2
0
 public boolean equals(Object other) {
   if ((this == other)) return true;
   if ((other == null)) return false;
   if (!(other instanceof Topic)) return false;
   Topic t = (Topic) other;
   return (t.getTopicID() == this.getTopicID());
 }
Ejemplo n.º 3
0
  public static boolean needsHighlight(Topic t, User u) {
    boolean highlight = true;
    Transaction tx = null;
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    try {
      tx = session.getTransaction();
      Query q =
          session.createQuery(
              "select ls "
                  + "  from LastseenTopic as ls "
                  + " where ls.userID = :userID "
                  + "   and ls.topicID = :topicID "
                  + "   and ls.lasttime > :lastedit");
      q.setInteger("userID", u.getId());
      q.setInteger("topicID", t.getTopicID());
      q.setDate("lastedit", t.getLastedit());
      List<LastseenTopic> lsts = q.list();
      if (lsts.size() > 0) {
        highlight = lsts.get(0).getLasttime().before(t.getLastedit());
      }

    } catch (HibernateException e) {
      e.printStackTrace();
      if (tx != null && tx.isActive()) tx.rollback();
    }
    return highlight;
  }
Ejemplo n.º 4
0
  public static List<Topic> getTopics(int location, int offset) {

    List<Topic> topics = new Vector<Topic>();

    Transaction tx = null;
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    try {
      tx = session.getTransaction();
      Query q =
          session.createQuery(
              "select u, t, count(p.postID) "
                  + "  from Topic as t, User as u, Post as p "
                  + " where t.starter = u.id "
                  + "   and t.location = :location "
                  + "   and p.topicID = t.topicID "
                  + " group by t.topicID "
                  + " order by t.sticky desc, t.lastedit desc");
      q.setMaxResults(Topic.MAXTOPICS);
      if (offset > 0) {
        q.setFirstResult(offset);
      }
      q.setInteger("location", location);

      for (Iterator<Object[]> it = q.iterate(); it.hasNext(); ) {
        Object[] el = it.next();
        User u = (User) el[0];
        Topic topic = (Topic) el[1];
        Long replies = (Long) el[2];
        topic.setUser(u);
        topic.setReplies(replies == null ? 0 : replies.intValue());
        topics.add(topic);
      }

    } catch (HibernateException e) {
      e.printStackTrace();
      if (tx != null && tx.isActive()) tx.rollback();
    }
    return topics;
  }