Search in sources :

Example 6 with PrivateMessage

use of net.jforum.entities.PrivateMessage in project jforum2 by rafaelsteil.

the class GenericPrivateMessageDAO method delete.

/**
	 * @see net.jforum.dao.PrivateMessageDAO#delete(net.jforum.entities.PrivateMessage[], int)
	 */
public void delete(PrivateMessage[] pm, int userId) {
    PreparedStatement deleteMessage = null;
    PreparedStatement deleteText = null;
    PreparedStatement isDeleteAllowed = null;
    try {
        Connection connection = JForumExecutionContext.getConnection();
        deleteMessage = connection.prepareStatement(SystemGlobals.getSql("PrivateMessageModel.delete"));
        deleteText = connection.prepareStatement(SystemGlobals.getSql("PrivateMessagesModel.deleteText"));
        isDeleteAllowed = connection.prepareStatement(SystemGlobals.getSql("PrivateMessagesModel.isDeleteAllowed"));
        isDeleteAllowed.setInt(2, userId);
        isDeleteAllowed.setInt(3, userId);
        for (int i = 0; i < pm.length; i++) {
            PrivateMessage currentMessage = pm[i];
            isDeleteAllowed.setInt(1, currentMessage.getId());
            ResultSet rs = null;
            try {
                rs = isDeleteAllowed.executeQuery();
                if (rs.next()) {
                    deleteText.setInt(1, currentMessage.getId());
                    deleteText.executeUpdate();
                    deleteMessage.setInt(1, currentMessage.getId());
                    deleteMessage.executeUpdate();
                }
            } finally {
                DbUtils.close(rs);
            }
        }
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(deleteMessage);
        DbUtils.close(deleteText);
        DbUtils.close(isDeleteAllowed);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) PrivateMessage(net.jforum.entities.PrivateMessage) DatabaseException(net.jforum.exceptions.DatabaseException)

Example 7 with PrivateMessage

use of net.jforum.entities.PrivateMessage in project jforum2 by rafaelsteil.

the class GenericPrivateMessageDAO method selectFromInbox.

/**
	 * @see net.jforum.dao.PrivateMessageDAO#selectFromInbox(net.jforum.entities.User)
	 */
public List selectFromInbox(User user) {
    String query = SystemGlobals.getSql("PrivateMessageModel.baseListing");
    query = query.replaceAll("#FILTER#", SystemGlobals.getSql("PrivateMessageModel.inbox"));
    PreparedStatement p = null;
    ResultSet rs = null;
    try {
        p = JForumExecutionContext.getConnection().prepareStatement(query);
        p.setInt(1, user.getId());
        List pmList = new ArrayList();
        rs = p.executeQuery();
        while (rs.next()) {
            PrivateMessage pm = this.getPm(rs, false);
            User fromUser = new User();
            fromUser.setId(rs.getInt("user_id"));
            fromUser.setUsername(rs.getString("username"));
            pm.setFromUser(fromUser);
            pmList.add(pm);
        }
        return pmList;
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(rs, p);
    }
}
Also used : User(net.jforum.entities.User) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) ArrayList(java.util.ArrayList) List(java.util.List) PrivateMessage(net.jforum.entities.PrivateMessage) DatabaseException(net.jforum.exceptions.DatabaseException)

Example 8 with PrivateMessage

use of net.jforum.entities.PrivateMessage in project jforum2 by rafaelsteil.

the class PrivateMessageAction method sendSave.

public void sendSave() {
    if (!SessionFacade.isLogged()) {
        this.setTemplateName(ViewCommon.contextToLogin());
        return;
    }
    UserDAO userDao = DataAccessDriver.getInstance().newUserDAO();
    String toUserIdStr = this.request.getParameter("toUserId");
    String toUsername = this.request.getParameter("toUsername");
    int toUserId = -1;
    // inserted the username by hand in the form's field
    if (toUserIdStr == null || "".equals(toUserIdStr.trim())) {
        List l = userDao.findByName(toUsername, true);
        if (l.size() > 0) {
            User u = (User) l.get(0);
            toUserId = u.getId();
        }
    } else {
        toUserId = Integer.parseInt(toUserIdStr);
    }
    // We failed to get the user id?
    if (toUserId == -1) {
        this.setTemplateName(TemplateKeys.PM_SENDSAVE_USER_NOTFOUND);
        this.context.put("message", I18n.getMessage("PrivateMessage.userIdNotFound"));
        return;
    }
    PrivateMessage pm = new PrivateMessage();
    pm.setPost(PostCommon.fillPostFromRequest());
    // Sender
    User fromUser = new User();
    fromUser.setId(SessionFacade.getUserSession().getUserId());
    pm.setFromUser(fromUser);
    // Recipient
    User toUser = userDao.selectById(toUserId);
    pm.setToUser(toUser);
    boolean preview = ("1".equals(this.request.getParameter("preview")));
    if (!preview) {
        DataAccessDriver.getInstance().newPrivateMessageDAO().send(pm);
        this.setTemplateName(TemplateKeys.PM_SENDSAVE);
        this.context.put("message", I18n.getMessage("PrivateMessage.messageSent", new String[] { this.request.getContextPath() + "/pm/inbox" + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION) }));
        // If the target user if in the forum, then increments its 
        // private messate count
        String sid = SessionFacade.isUserInSession(toUserId);
        if (sid != null) {
            UserSession us = SessionFacade.getUserSession(sid);
            us.setPrivateMessages(us.getPrivateMessages() + 1);
        }
        if (toUser.getEmail() != null && toUser.getEmail().trim().length() > 0 && SystemGlobals.getBoolValue(ConfigKeys.MAIL_NOTIFY_ANSWERS)) {
            Executor.execute(new EmailSenderTask(new PrivateMessageSpammer(toUser)));
        }
    } else {
        this.context.put("preview", true);
        this.context.put("post", pm.getPost());
        Post postPreview = new Post(pm.getPost());
        this.context.put("postPreview", PostCommon.preparePostForDisplay(postPreview));
        this.context.put("pm", pm);
        this.send();
    }
}
Also used : EmailSenderTask(net.jforum.util.mail.EmailSenderTask) User(net.jforum.entities.User) UserDAO(net.jforum.dao.UserDAO) Post(net.jforum.entities.Post) UserSession(net.jforum.entities.UserSession) List(java.util.List) PrivateMessageSpammer(net.jforum.util.mail.PrivateMessageSpammer) PrivateMessage(net.jforum.entities.PrivateMessage)

Example 9 with PrivateMessage

use of net.jforum.entities.PrivateMessage in project jforum2 by rafaelsteil.

the class PrivateMessageAction method quote.

public void quote() {
    if (!SessionFacade.isLogged()) {
        this.setTemplateName(ViewCommon.contextToLogin());
        return;
    }
    int id = this.request.getIntParameter("id");
    PrivateMessage pm = new PrivateMessage();
    pm.setId(id);
    pm = DataAccessDriver.getInstance().newPrivateMessageDAO().selectById(pm);
    int userId = SessionFacade.getUserSession().getUserId();
    if (pm.getToUser().getId() != userId && pm.getFromUser().getId() != userId) {
        this.setTemplateName(TemplateKeys.PM_READ_DENIED);
        this.context.put("message", I18n.getMessage("PrivateMessage.readDenied"));
        return;
    }
    pm.getPost().setSubject(I18n.getMessage("PrivateMessage.replyPrefix") + pm.getPost().getSubject());
    this.sendFormCommon(DataAccessDriver.getInstance().newUserDAO().selectById(userId));
    this.context.put("quote", "true");
    this.context.put("quoteUser", pm.getFromUser().getUsername());
    this.context.put("post", pm.getPost());
    this.context.put("pm", pm);
}
Also used : PrivateMessage(net.jforum.entities.PrivateMessage)

Aggregations

PrivateMessage (net.jforum.entities.PrivateMessage)9 User (net.jforum.entities.User)4 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 List (java.util.List)3 UserSession (net.jforum.entities.UserSession)3 DatabaseException (net.jforum.exceptions.DatabaseException)3 ArrayList (java.util.ArrayList)2 UserDAO (net.jforum.dao.UserDAO)2 Post (net.jforum.entities.Post)2 Connection (java.sql.Connection)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 PrivateMessageDAO (net.jforum.dao.PrivateMessageDAO)1 EmailSenderTask (net.jforum.util.mail.EmailSenderTask)1 PrivateMessageSpammer (net.jforum.util.mail.PrivateMessageSpammer)1