Пример #1
0
  private void removePosts(List posts) {
    PreparedStatement post = null;
    PreparedStatement text = null;

    try {
      post =
          JForumExecutionContext.getConnection()
              .prepareStatement(SystemGlobals.getSql("PostModel.deletePost"));

      text =
          JForumExecutionContext.getConnection()
              .prepareStatement(SystemGlobals.getSql("PostModel.deletePostText"));

      for (Iterator iter = posts.iterator(); iter.hasNext(); ) {
        Post p = (Post) iter.next();

        post.setInt(1, p.getId());
        text.setInt(1, p.getId());

        text.executeUpdate();
        post.executeUpdate();

        SearchFacade.delete(p);
      }
    } catch (SQLException e) {
      throw new DatabaseException(e);
    } finally {
      DbUtils.close(post);
      DbUtils.close(text);
    }
  }
Пример #2
0
  /**
   * Setup optios and values for the user's session if authentication was ok.
   *
   * @param userSession The UserSession instance of the user
   * @param user The User instance of the authenticated user
   */
  protected void configureUserSession(UserSession userSession, User user) {

    LOG.trace("configureUserSession");
    userSession.dataToUser(user);

    // As an user may come back to the forum before its
    // last visit's session expires, we should check for
    // existent user information and then, if found, store
    // it to the database before getting his information back.
    String sessionId = SessionFacade.isUserInSession(user.getId());

    UserSession tmpUs;
    if (sessionId != null) {
      SessionFacade.storeSessionData(sessionId, JForumExecutionContext.getConnection());
      tmpUs = SessionFacade.getUserSession(sessionId);
      SessionFacade.remove(sessionId);
    } else {
      UserSessionDAO sm = DataAccessDriver.getInstance().newUserSessionDAO();
      tmpUs = sm.selectById(userSession, JForumExecutionContext.getConnection());
    }

    if (tmpUs == null) {
      userSession.setLastVisit(new Date(System.currentTimeMillis()));
    } else {
      // Update last visit and session start time
      userSession.setLastVisit(new Date(tmpUs.getStartTime().getTime() + tmpUs.getSessionTime()));
    }

    // If the execution point gets here, then the user
    // has chosen "autoLogin"
    userSession.setAutoLogin(true);
    SessionFacade.makeLogged();

    I18n.load(user.getLang());
  }
Пример #3
0
  /** @see net.jforum.model.PostModel#countPreviousPosts(int) */
  public int countPreviousPosts(int postId) {
    int total = 0;

    PreparedStatement p = null;
    ResultSet rs = null;
    try {
      p =
          JForumExecutionContext.getConnection()
              .prepareStatement(SystemGlobals.getSql("PostModel.countPreviousPosts"));
      p.setInt(1, postId);
      p.setInt(2, postId);

      rs = p.executeQuery();

      if (rs.next()) {
        total = rs.getInt(1);
      }

      return total;
    } catch (SQLException e) {
      throw new DatabaseException(e);
    } finally {
      DbUtils.close(rs, p);
    }
  }
Пример #4
0
  public int countUserPosts(int userId) {
    int total = 0;

    PreparedStatement p = null;
    ResultSet rs = null;
    try {
      p =
          JForumExecutionContext.getConnection()
              .prepareStatement(
                  SystemGlobals.getSql("PostModel.countUserPosts")
                      .replaceAll(":fids:", ForumRepository.getListAllowedForums()));
      p.setInt(1, userId);

      rs = p.executeQuery();

      if (rs.next()) {
        total = rs.getInt(1);
      }

      return total;
    } catch (SQLException e) {
      throw new DatabaseException(e);
    } finally {
      DbUtils.close(rs, p);
    }
  }
Пример #5
0
  /** @see net.jforum.dao.PostDAO#selectByUserByLimit(int, int, int) */
  public List selectByUserByLimit(int userId, int startFrom, int count) {
    String sql = SystemGlobals.getSql("PostModel.selectByUserByLimit");
    sql = sql.replaceAll(":fids:", ForumRepository.getListAllowedForums());

    PreparedStatement p = null;
    ResultSet rs = null;
    try {
      p = JForumExecutionContext.getConnection().prepareStatement(sql);

      p.setInt(1, userId);
      p.setInt(2, startFrom);
      p.setInt(3, count);

      rs = p.executeQuery();
      List l = new ArrayList();

      while (rs.next()) {
        l.add(this.makePost(rs));
      }

      return l;
    } catch (SQLException e) {
      throw new DatabaseException(e);
    } finally {
      DbUtils.close(rs, p);
    }
  }
Пример #6
0
  /** @see net.jforum.dao.PostDAO#selectAllBytTopicByLimit(int, int, int) */
  public List selectAllByTopicByLimit(int topicId, int startFrom, int count) {
    List l = new ArrayList();

    String sql = SystemGlobals.getSql("PostModel.selectAllByTopicByLimit");

    PreparedStatement p = null;
    ResultSet rs = null;

    try {
      p = JForumExecutionContext.getConnection().prepareStatement(sql);
      p.setInt(1, topicId);
      p.setInt(2, startFrom);
      p.setInt(3, count);

      rs = p.executeQuery();

      while (rs.next()) {
        l.add(this.makePost(rs));
      }

      return l;
    } catch (SQLException e) {
      throw new DatabaseException(e);
    } finally {
      DbUtils.close(rs, p);
    }
  }
Пример #7
0
  protected void updatePostsTable(Post post) {
    PreparedStatement p = null;

    try {
      p =
          JForumExecutionContext.getConnection()
              .prepareStatement(SystemGlobals.getSql("PostModel.updatePost"));
      p.setInt(1, post.getTopicId());
      p.setInt(2, post.getForumId());
      p.setInt(3, post.isBbCodeEnabled() ? 1 : 0);
      p.setInt(4, post.isHtmlEnabled() ? 1 : 0);
      p.setInt(5, post.isSmiliesEnabled() ? 1 : 0);
      p.setInt(6, post.isSignatureEnabled() ? 1 : 0);
      p.setTimestamp(7, new Timestamp(System.currentTimeMillis()));
      p.setInt(8, post.getEditCount() + 1);
      p.setString(9, post.getUserIp());
      p.setInt(10, post.getId());

      p.executeUpdate();
    } catch (SQLException e) {
      throw new DatabaseException(e);
    } finally {
      DbUtils.close(p);
    }
  }
Пример #8
0
  /** @see net.jforum.model.PostModel#deleteByTopic(int) */
  public void deleteByTopic(int topicId) {
    PreparedStatement p = null;
    ResultSet rs = null;

    try {
      p =
          JForumExecutionContext.getConnection()
              .prepareStatement(SystemGlobals.getSql("PostModel.deleteByTopic"));
      p.setInt(1, topicId);
      rs = p.executeQuery();

      List posts = new ArrayList();

      while (rs.next()) {
        Post post = new Post();
        post.setId(rs.getInt("post_id"));
        post.setUserId(rs.getInt("user_id"));

        posts.add(post);
      }

      this.removePosts(posts);
    } catch (SQLException e) {
      throw new DatabaseException(e);
    } finally {
      DbUtils.close(rs, p);
    }
  }
Пример #9
0
  public void deleteForumRoles(int forumId) {
    PreparedStatement p = null;

    List roleIds = this.selectForumRoles(forumId);

    try {
      StringBuffer ids = new StringBuffer();

      for (Iterator iterator = roleIds.iterator(); iterator.hasNext(); ) {
        Integer id = (Integer) iterator.next();
        ids.append(id).append(',');
      }

      ids.append("-1");

      // Role values
      String sql = SystemGlobals.getSql("PermissionControl.deleteRoleValues");
      sql = StringUtils.replace(sql, "#IDS#", ids.toString());

      p = JForumExecutionContext.getConnection().prepareStatement(sql);
      p.setString(1, String.valueOf(forumId));
      p.executeUpdate();
    } catch (SQLException e) {
      throw new DatabaseException(e);
    } finally {
      DbUtils.close(p);
    }
  }
Пример #10
0
  public List selectHotForRSS(int limit) {
    List l = new ArrayList();

    PreparedStatement p = null;
    ResultSet rs = null;

    try {
      p =
          JForumExecutionContext.getConnection()
              .prepareStatement(SystemGlobals.getSql("PostModel.selectHotForRSS"));
      p.setInt(1, limit);

      rs = p.executeQuery();

      while (rs.next()) {
        Post post = this.buildPostForRSS(rs);
        l.add(post);
      }

    } catch (SQLException e) {
      throw new DatabaseException(e);
    } finally {
      DbUtils.close(rs, p);
    }

    return l;
  }
Пример #11
0
  private List selectForumRoles(int forumId) {
    List l = new ArrayList();

    PreparedStatement p = null;
    ResultSet rs = null;

    try {
      p =
          JForumExecutionContext.getConnection()
              .prepareStatement(SystemGlobals.getSql("PermissionControl.selectForumRoles"));
      p.setString(1, String.valueOf(forumId));

      rs = p.executeQuery();

      while (rs.next()) {
        l.add(new Integer(rs.getInt("role_id")));
      }
    } catch (SQLException e) {
      throw new DatabaseException(e);
    } finally {
      DbUtils.close(rs, p);
    }

    return l;
  }
Пример #12
0
  protected RoleCollection loadRoles(int[] groupIds) {
    String sql = SystemGlobals.getSql("PermissionControl.loadGroupRoles");
    String groupIdAsString = SecurityCommon.groupIdAsString(groupIds);

    if ("".equals(groupIdAsString)) {
      // We suppose there is no "negative" group ids
      sql = sql.replaceAll("#IN#", "-1");
    } else {
      sql = sql.replaceAll("#IN#", groupIdAsString);
    }

    RoleCollection roles = new RoleCollection();

    PreparedStatement p = null;
    ResultSet rs = null;

    try {
      p = JForumExecutionContext.getConnection().prepareStatement(sql);
      rs = p.executeQuery();

      roles = SecurityCommon.loadRoles(rs);
    } catch (Exception e) {
      throw new DatabaseException(e);
    } finally {
      DbUtils.close(rs, p);
    }

    return roles;
  }
  /** Finishes the execution context */
  public static void finish() {
    Connection conn = JForumExecutionContext.getConnection(false);

    if (conn != null) {
      if (SystemGlobals.getBoolValue(ConfigKeys.DATABASE_USE_TRANSACTIONS)) {
        if (JForumExecutionContext.shouldRollback()) {
          try {
            conn.rollback();
          } catch (Exception e) {
            logger.error("Error while rolling back a transaction", e);
          }
        } else {
          try {
            conn.commit();
          } catch (Exception e) {
            logger.error("Error while commiting a transaction", e);
          }
        }
      }

      try {
        DBConnection.getImplementation().releaseConnection(conn);
      } catch (Exception e) {
        logger.error("Error while releasing the connection : " + e, e);
      }
    }

    userData.set(null);
  }
Пример #14
0
  /**
   * @see net.jforum.dao.GroupSecurityDAO#addRoleValue(int, net.jforum.security.Role,
   *     net.jforum.security.RoleValueCollection)
   */
  public void addRoleValue(int groupId, Role role, RoleValueCollection rvc) {
    PreparedStatement p = null;
    ResultSet rs = null;

    try {
      p =
          JForumExecutionContext.getConnection()
              .prepareStatement(SystemGlobals.getSql("PermissionControl.getRoleIdByName"));
      p.setString(1, role.getName());
      p.setInt(2, groupId);

      int roleId = -1;

      rs = p.executeQuery();
      if (rs.next()) {
        roleId = rs.getInt("role_id");
      }

      rs.close();
      rs = null;
      p.close();
      p = null;

      if (roleId == -1) {
        this.addRole(groupId, role, rvc);
      } else {
        p =
            JForumExecutionContext.getConnection()
                .prepareStatement(SystemGlobals.getSql("PermissionControl.addRoleValues"));
        p.setInt(1, roleId);

        for (Iterator iter = rvc.iterator(); iter.hasNext(); ) {
          RoleValue rv = (RoleValue) iter.next();
          p.setString(2, rv.getValue());
          p.executeUpdate();
        }
      }
    } catch (SQLException e) {
      throw new DatabaseException(e);
    } finally {
      DbUtils.close(rs, p);
    }
  }
Пример #15
0
  /** @see net.jforum.dao.security.SecurityDAO#deleteAllRoles(int) */
  public void deleteAllRoles(int groupId) {
    PreparedStatement p = null;

    try {
      p =
          JForumExecutionContext.getConnection()
              .prepareStatement(SystemGlobals.getSql("PermissionControl.deleteAllRoleValues"));
      p.setInt(1, groupId);
      p.executeUpdate();
      p.close();

      p =
          JForumExecutionContext.getConnection()
              .prepareStatement(SystemGlobals.getSql("PermissionControl.deleteAllGroupRoles"));
      p.setInt(1, groupId);
      p.executeUpdate();
    } catch (SQLException e) {
      throw new DatabaseException(e);
    } finally {
      DbUtils.close(p);
    }
  }
Пример #16
0
 /** @see net.jforum.dao.SmilieDAO#delete(int) */
 public void delete(int id) {
   PreparedStatement pstmt = null;
   try {
     pstmt =
         JForumExecutionContext.getConnection()
             .prepareStatement(SystemGlobals.getSql("SmiliesModel.delete"));
     pstmt.setInt(1, id);
     pstmt.executeUpdate();
   } catch (SQLException e) {
     throw new DatabaseException(e);
   } finally {
     DbUtils.close(pstmt);
   }
 }
Пример #17
0
  protected void addNewPostText(Post post) throws Exception {
    PreparedStatement p = null;
    try {
      p =
          JForumExecutionContext.getConnection()
              .prepareStatement(SystemGlobals.getSql("PostModel.addNewPostText"));
      int cIndex = 1;
      p.setInt(cIndex++, post.getId());
      p.setString(cIndex++, post.getText());
      p.setString(cIndex++, post.getSubject());

      p.executeUpdate();
    } finally {
      DbUtils.close(p);
    }
  }
Пример #18
0
  /** @see net.jforum.dao.SmilieDAO#update(net.jforum.entities.Smilie) */
  public void update(Smilie smilie) {
    PreparedStatement pstmt = null;
    try {
      pstmt =
          JForumExecutionContext.getConnection()
              .prepareStatement(SystemGlobals.getSql("SmiliesModel.update"));
      pstmt.setString(1, smilie.getCode());
      pstmt.setString(2, smilie.getUrl());
      pstmt.setString(3, smilie.getDiskName());
      pstmt.setInt(4, smilie.getId());

      pstmt.executeUpdate();
    } catch (SQLException e) {
      throw new DatabaseException(e);
    } finally {
      DbUtils.close(pstmt);
    }
  }
Пример #19
0
  protected void updatePostsTextTable(Post post) {
    PreparedStatement p = null;

    try {
      p =
          JForumExecutionContext.getConnection()
              .prepareStatement(SystemGlobals.getSql("PostModel.updatePostText"));
      p.setString(1, post.getText());
      p.setString(2, post.getSubject());
      p.setInt(3, post.getId());

      p.executeUpdate();
    } catch (SQLException e) {
      throw new DatabaseException(e);
    } finally {
      DbUtils.close(p);
    }
  }
Пример #20
0
  /** @see net.jforum.dao.SmilieDAO#selectAll() */
  public List<Smilie> selectAll() {
    List<Smilie> l = new ArrayList<Smilie>();

    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      pstmt =
          JForumExecutionContext.getConnection()
              .prepareStatement(SystemGlobals.getSql("SmiliesModel.selectAll"));
      rs = pstmt.executeQuery();
      while (rs.next()) {
        l.add(this.getSmilie(rs));
      }

      return l;
    } catch (SQLException e) {
      throw new DatabaseException(e);
    } finally {
      DbUtils.close(rs, pstmt);
    }
  }
Пример #21
0
  /** @see net.jforum.dao.ApiDAO#isValid(java.lang.String) */
  public boolean isValid(final String apiKey) {
    boolean status = false;

    PreparedStatement pstmt = null;
    ResultSet resultSet = null;

    try {
      pstmt =
          JForumExecutionContext.getConnection()
              .prepareStatement(SystemGlobals.getSql("ApiModel.isValid"));
      pstmt.setString(1, apiKey);

      resultSet = pstmt.executeQuery();
      status = resultSet.next();
    } catch (SQLException e) {
      throw new DatabaseException(e);
    } finally {
      DbUtils.close(resultSet, pstmt);
    }

    return status;
  }
Пример #22
0
  /** @see net.jforum.dao.SmilieDAO#selectById(int) */
  public Smilie selectById(int id) {
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      pstmt =
          JForumExecutionContext.getConnection()
              .prepareStatement(SystemGlobals.getSql("SmiliesModel.selectById"));
      pstmt.setInt(1, id);

      Smilie s = new Smilie();

      rs = pstmt.executeQuery();
      if (rs.next()) {
        s = this.getSmilie(rs);
      }

      return s;
    } catch (SQLException e) {
      throw new DatabaseException(e);
    } finally {
      DbUtils.close(rs, pstmt);
    }
  }
Пример #23
0
  /** @see net.jforum.dao.PostDAO#selectById(int) */
  public Post selectById(int postId) {
    PreparedStatement p = null;
    ResultSet rs = null;
    try {
      p =
          JForumExecutionContext.getConnection()
              .prepareStatement(SystemGlobals.getSql("PostModel.selectById"));
      p.setInt(1, postId);

      rs = p.executeQuery();

      Post post = new Post();

      if (rs.next()) {
        post = this.makePost(rs);
      }

      return post;
    } catch (SQLException e) {
      throw new DatabaseException(e);
    } finally {
      DbUtils.close(rs, p);
    }
  }