use of net.jforum.dao.PostDAO in project jforum2 by rafaelsteil.
the class PostAction method list.
public void list() {
PostDAO postDao = DataAccessDriver.getInstance().newPostDAO();
PollDAO pollDao = DataAccessDriver.getInstance().newPollDAO();
TopicDAO topicDao = DataAccessDriver.getInstance().newTopicDAO();
UserSession us = SessionFacade.getUserSession();
int anonymousUser = SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID);
boolean logged = SessionFacade.isLogged();
int topicId = this.request.getIntParameter("topic_id");
Topic topic = TopicRepository.getTopic(new Topic(topicId));
if (topic == null) {
topic = topicDao.selectById(topicId);
}
// The topic exists?
if (topic.getId() == 0) {
this.topicNotFound();
return;
}
// Shall we proceed?
Forum forum = ForumRepository.getForum(topic.getForumId());
if (!logged) {
if (forum == null || !ForumRepository.isCategoryAccessible(forum.getCategoryId())) {
this.setTemplateName(ViewCommon.contextToLogin());
return;
}
} else if (!TopicsCommon.isTopicAccessible(topic.getForumId())) {
return;
}
int count = SystemGlobals.getIntValue(ConfigKeys.POSTS_PER_PAGE);
int start = ViewCommon.getStartPage();
PermissionControl pc = SecurityRepository.get(us.getUserId());
boolean moderatorCanEdit = false;
if (pc.canAccess(SecurityConstants.PERM_MODERATION_POST_EDIT)) {
moderatorCanEdit = true;
}
List helperList = PostCommon.topicPosts(postDao, moderatorCanEdit, us.getUserId(), topic.getId(), start, count);
// Is moderation pending for the topic?
if (topic.isModerated() && helperList.size() == 0) {
this.notModeratedYet();
return;
}
// Set the topic status as read
if (logged) {
topicDao.updateReadStatus(topic.getId(), us.getUserId(), true);
}
boolean canVoteOnPoll = logged && SecurityRepository.canAccess(SecurityConstants.PERM_VOTE);
Poll poll = null;
if (topic.isVote()) {
// It has a poll associated with the topic
poll = pollDao.selectById(topic.getVoteId());
if (canVoteOnPoll) {
canVoteOnPoll = !pollDao.hasUserVotedOnPoll(topic.getVoteId(), us.getUserId());
}
}
topicDao.incrementTotalViews(topic.getId());
topic.setTotalViews(topic.getTotalViews() + 1);
if (us.getUserId() != anonymousUser) {
SessionFacade.getTopicsReadTime().put(new Integer(topic.getId()), new Long(System.currentTimeMillis()));
}
boolean karmaEnabled = SecurityRepository.canAccess(SecurityConstants.PERM_KARMA_ENABLED);
Map userVotes = new HashMap();
if (logged && karmaEnabled) {
userVotes = DataAccessDriver.getInstance().newKarmaDAO().getUserVotes(topic.getId(), us.getUserId());
}
this.setTemplateName(TemplateKeys.POSTS_LIST);
this.context.put("attachmentsEnabled", pc.canAccess(SecurityConstants.PERM_ATTACHMENTS_ENABLED, Integer.toString(topic.getForumId())));
this.context.put("canDownloadAttachments", pc.canAccess(SecurityConstants.PERM_ATTACHMENTS_DOWNLOAD));
this.context.put("thumbShowBox", SystemGlobals.getBoolValue(ConfigKeys.ATTACHMENTS_IMAGES_THUMB_BOX_SHOW));
this.context.put("am", new AttachmentCommon(this.request, topic.getForumId()));
this.context.put("karmaVotes", userVotes);
this.context.put("rssEnabled", SystemGlobals.getBoolValue(ConfigKeys.RSS_ENABLED));
this.context.put("canRemove", pc.canAccess(SecurityConstants.PERM_MODERATION_POST_REMOVE));
this.context.put("moderatorCanEdit", moderatorCanEdit);
this.context.put("allCategories", ForumCommon.getAllCategoriesAndForums(false));
this.context.put("topic", topic);
this.context.put("poll", poll);
this.context.put("canVoteOnPoll", canVoteOnPoll);
this.context.put("rank", new RankingRepository());
this.context.put("posts", helperList);
this.context.put("forum", forum);
this.context.put("karmaMin", new Integer(SystemGlobals.getValue(ConfigKeys.KARMA_MIN_POINTS)));
this.context.put("karmaMax", new Integer(SystemGlobals.getValue(ConfigKeys.KARMA_MAX_POINTS)));
this.context.put("avatarAllowExternalUrl", SystemGlobals.getBoolValue(ConfigKeys.AVATAR_ALLOW_EXTERNAL_URL));
this.context.put("moderationLoggingEnabled", SystemGlobals.getBoolValue(ConfigKeys.MODERATION_LOGGING_ENABLED));
this.context.put("needCaptcha", SystemGlobals.getBoolValue(ConfigKeys.CAPTCHA_POSTS));
Map topicPosters = topicDao.topicPosters(topic.getId());
for (Iterator iter = topicPosters.values().iterator(); iter.hasNext(); ) {
ViewCommon.prepareUserSignature((User) iter.next());
}
this.context.put("users", topicPosters);
this.context.put("anonymousPosts", pc.canAccess(SecurityConstants.PERM_ANONYMOUS_POST, Integer.toString(topic.getForumId())));
this.context.put("watching", topicDao.isUserSubscribed(topicId, SessionFacade.getUserSession().getUserId()));
this.context.put("pageTitle", topic.getTitle());
this.context.put("isAdmin", pc.canAccess(SecurityConstants.PERM_ADMINISTRATION));
this.context.put("readonly", !pc.canAccess(SecurityConstants.PERM_READ_ONLY_FORUMS, Integer.toString(topic.getForumId())));
this.context.put("replyOnly", !pc.canAccess(SecurityConstants.PERM_REPLY_ONLY, Integer.toString(topic.getForumId())));
this.context.put("isModerator", us.isModerator(topic.getForumId()));
ViewCommon.contextToPagination(start, topic.getTotalReplies() + 1, count);
TopicsCommon.topicListingBase();
TopicRepository.updateTopic(topic);
}
use of net.jforum.dao.PostDAO in project jforum2 by rafaelsteil.
the class PostAction method insertSave.
public void insertSave() {
int forumId = this.request.getIntParameter("forum_id");
boolean firstPost = false;
if (!this.anonymousPost(forumId)) {
return;
}
Topic t = new Topic(-1);
t.setForumId(forumId);
boolean newTopic = (this.request.getParameter("topic_id") == null);
if (!TopicsCommon.isTopicAccessible(t.getForumId()) || this.isForumReadonly(t.getForumId(), newTopic)) {
return;
}
TopicDAO topicDao = DataAccessDriver.getInstance().newTopicDAO();
PostDAO postDao = DataAccessDriver.getInstance().newPostDAO();
PollDAO poolDao = DataAccessDriver.getInstance().newPollDAO();
ForumDAO forumDao = DataAccessDriver.getInstance().newForumDAO();
if (!newTopic) {
int topicId = this.request.getIntParameter("topic_id");
t = TopicRepository.getTopic(new Topic(topicId));
if (t == null) {
t = topicDao.selectById(topicId);
}
// Could not find the topic. The topicId sent was invalid
if (t == null || t.getId() == 0) {
newTopic = true;
} else {
if (!TopicsCommon.isTopicAccessible(t.getForumId())) {
return;
}
// Cannot insert new messages on locked topics
if (t.getStatus() == Topic.STATUS_LOCKED) {
this.topicLocked();
return;
}
}
}
// checking above set the newTopic var to true
if (newTopic) {
if (this.isReplyOnly(forumId)) {
this.replyOnly();
return;
}
if (this.request.getParameter("topic_type") != null) {
t.setType(this.request.getIntParameter("topic_type"));
if (t.getType() != Topic.TYPE_NORMAL && !SecurityRepository.canAccess(SecurityConstants.PERM_CREATE_STICKY_ANNOUNCEMENT_TOPICS)) {
t.setType(Topic.TYPE_NORMAL);
}
}
}
UserSession us = SessionFacade.getUserSession();
User u = DataAccessDriver.getInstance().newUserDAO().selectById(us.getUserId());
if ("1".equals(this.request.getParameter("quick")) && SessionFacade.isLogged()) {
this.request.addParameter("notify", u.isNotifyOnMessagesEnabled() ? "1" : null);
this.request.addParameter("attach_sig", u.getAttachSignatureEnabled() ? "1" : "0");
} else {
u.setId(us.getUserId());
u.setUsername(us.getUsername());
}
// Set the Post
Post p = PostCommon.fillPostFromRequest();
if (p.getText() == null || p.getText().trim().equals("")) {
this.insert();
return;
}
// Check the elapsed time since the last post from the user
int delay = SystemGlobals.getIntValue(ConfigKeys.POSTS_NEW_DELAY);
if (delay > 0) {
Long lastPostTime = (Long) SessionFacade.getAttribute(ConfigKeys.LAST_POST_TIME);
if (lastPostTime != null) {
if (System.currentTimeMillis() < (lastPostTime.longValue() + delay)) {
this.context.put("post", p);
this.context.put("start", this.request.getParameter("start"));
this.context.put("error", I18n.getMessage("PostForm.tooSoon"));
this.insert();
return;
}
}
}
p.setForumId(this.request.getIntParameter("forum_id"));
if (StringUtils.isBlank(p.getSubject())) {
p.setSubject(t.getTitle());
}
boolean needCaptcha = SystemGlobals.getBoolValue(ConfigKeys.CAPTCHA_POSTS) && request.getSessionContext().getAttribute(ConfigKeys.REQUEST_IGNORE_CAPTCHA) == null;
if (needCaptcha) {
if (!us.validateCaptchaResponse(this.request.getParameter("captcha_anwser"))) {
this.context.put("post", p);
this.context.put("start", this.request.getParameter("start"));
this.context.put("error", I18n.getMessage("CaptchaResponseFails"));
this.insert();
return;
}
}
boolean preview = "1".equals(this.request.getParameter("preview"));
if (!preview) {
AttachmentCommon attachments = new AttachmentCommon(this.request, forumId);
try {
attachments.preProcess();
} catch (AttachmentException e) {
JForumExecutionContext.enableRollback();
p.setText(this.request.getParameter("message"));
p.setId(0);
this.context.put("errorMessage", e.getMessage());
this.context.put("post", p);
this.insert();
return;
}
Forum forum = ForumRepository.getForum(forumId);
PermissionControl pc = SecurityRepository.get(us.getUserId());
// Moderators and admins don't need to have their messages moderated
boolean moderate = (forum.isModerated() && !pc.canAccess(SecurityConstants.PERM_MODERATION) && !pc.canAccess(SecurityConstants.PERM_ADMINISTRATION));
if (newTopic) {
t.setTime(new Date());
t.setTitle(this.request.getParameter("subject"));
t.setModerated(moderate);
t.setPostedBy(u);
t.setFirstPostTime(ViewCommon.formatDate(t.getTime()));
int topicId = topicDao.addNew(t);
t.setId(topicId);
firstPost = true;
}
if (!firstPost && pc.canAccess(SecurityConstants.PERM_REPLY_WITHOUT_MODERATION, Integer.toString(t.getForumId()))) {
moderate = false;
}
// Topic watch
if (this.request.getParameter("notify") != null) {
this.watch(topicDao, t.getId(), u.getId());
}
p.setTopicId(t.getId());
// add a poll
Poll poll = PollCommon.fillPollFromRequest();
if (poll != null && newTopic) {
poll.setTopicId(t.getId());
if (poll.getOptions().size() < 2) {
//it is not a valid poll, cancel the post
JForumExecutionContext.enableRollback();
p.setText(this.request.getParameter("message"));
p.setId(0);
this.context.put("errorMessage", I18n.getMessage("PostForm.needMorePollOptions"));
this.context.put("post", p);
this.context.put("poll", poll);
this.insert();
return;
}
poolDao.addNew(poll);
t.setVoteId(poll.getId());
}
// Save the remaining stuff
p.setModerate(moderate);
int postId = postDao.addNew(p);
if (newTopic) {
t.setFirstPostId(postId);
}
if (!moderate) {
t.setLastPostId(postId);
t.setLastPostBy(u);
t.setLastPostDate(p.getTime());
t.setLastPostTime(p.getFormatedTime());
}
topicDao.update(t);
attachments.insertAttachments(p);
if (!moderate) {
StringBuffer path = new StringBuffer(512);
path.append(this.request.getContextPath()).append("/posts/list/");
int start = ViewCommon.getStartPage();
path.append(this.startPage(t, start)).append("/").append(t.getId()).append(SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION)).append('#').append(postId);
JForumExecutionContext.setRedirect(path.toString());
if (newTopic) {
// Notify "forum new topic" users
ForumCommon.notifyUsers(forum, t, p);
} else {
t.setTotalReplies(t.getTotalReplies() + 1);
TopicsCommon.notifyUsers(t, p);
}
// Update forum stats, cache and etc
t.setTotalViews(t.getTotalViews() + 1);
DataAccessDriver.getInstance().newUserDAO().incrementPosts(p.getUserId());
TopicsCommon.updateBoardStatus(t, postId, firstPost, topicDao, forumDao);
ForumRepository.updateForumStats(t, u, p);
int anonymousUser = SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID);
if (u.getId() != anonymousUser) {
SessionFacade.getTopicsReadTime().put(new Integer(t.getId()), new Long(p.getTime().getTime()));
}
if (SystemGlobals.getBoolValue(ConfigKeys.POSTS_CACHE_ENABLED)) {
SimpleDateFormat df = new SimpleDateFormat(SystemGlobals.getValue(ConfigKeys.DATE_TIME_FORMAT));
p.setFormatedTime(df.format(p.getTime()));
PostRepository.append(p.getTopicId(), PostCommon.preparePostForDisplay(p));
}
} else {
JForumExecutionContext.setRedirect(this.request.getContextPath() + "/posts/waitingModeration/" + (firstPost ? 0 : t.getId()) + "/" + t.getForumId() + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
}
if (delay > 0) {
SessionFacade.setAttribute(ConfigKeys.LAST_POST_TIME, new Long(System.currentTimeMillis()));
}
} else {
this.context.put("preview", true);
this.context.put("post", p);
this.context.put("start", this.request.getParameter("start"));
Post postPreview = new Post(p);
this.context.put("postPreview", PostCommon.preparePostForDisplay(postPreview));
this.insert();
}
}
use of net.jforum.dao.PostDAO in project jforum2 by rafaelsteil.
the class PostAction method preList.
/**
* Given a postId, sends the user to the right page
*/
public void preList() {
int postId = this.request.getIntParameter("post_id");
PostDAO dao = DataAccessDriver.getInstance().newPostDAO();
int count = dao.countPreviousPosts(postId);
int postsPerPage = SystemGlobals.getIntValue(ConfigKeys.POSTS_PER_PAGE);
int topicId = 0;
if (this.request.getParameter("topic_id") != null) {
topicId = this.request.getIntParameter("topic_id");
}
if (topicId == 0) {
Post post = dao.selectById(postId);
topicId = post.getTopicId();
}
String page = "";
if (count > postsPerPage) {
page = Integer.toString(postsPerPage * ((count - 1) / postsPerPage)) + "/";
}
JForumExecutionContext.setRedirect(this.request.getContextPath() + "/posts/list/" + page + topicId + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION) + "#" + postId);
}
use of net.jforum.dao.PostDAO in project jforum2 by rafaelsteil.
the class RSSAction method topicPosts.
/**
* RSS for all N first posts for some given topic
*/
public void topicPosts() {
int topicId = this.request.getIntParameter("topic_id");
PostDAO pm = DataAccessDriver.getInstance().newPostDAO();
TopicDAO tm = DataAccessDriver.getInstance().newTopicDAO();
Topic topic = tm.selectById(topicId);
if (!TopicsCommon.isTopicAccessible(topic.getForumId()) || topic.getId() == 0) {
JForumExecutionContext.requestBasicAuthentication();
return;
}
tm.incrementTotalViews(topic.getId());
List posts = pm.selectAllByTopic(topicId);
String[] p = { topic.getTitle() };
String title = I18n.getMessage("RSS.TopicPosts.title", p);
String description = I18n.getMessage("RSS.TopicPosts.description", p);
RSSAware rss = new TopicPostsRSS(title, description, topic.getForumId(), posts);
this.context.put("rssContents", rss.createRSS());
}
use of net.jforum.dao.PostDAO in project jforum2 by rafaelsteil.
the class InstallAction method storeSupportProjectMessage.
private void storeSupportProjectMessage(Connection connection) {
StringBuffer message = new StringBuffer("[color=#3AA315][size=18][b]Support JForum - Help the project[/b][/size][/color]").append("<hr>").append("This project is Open Source, and maintained by at least one full time Senior Developer, [i]which costs US$ 3,000.00 / month[/i]. ").append("If it helped you, please consider helping this project - especially with some [b][url=http://www.jforum.net/contribute.jsp]donation[/url][/b].").append('\n').append('\n').append("[color=#137C9F][size=14][b]Why supporting this project is a good thing[/b][/size][/color]").append("<hr>").append("The JForum Project started four years ago as a completely free and Open Source program, initially entirely developed on my (Rafael Steil) ").append("free time. Today, with the help of some very valuable people, I can spend more time on JForum, to improve it and implement new features ").append("(lots of things, requested either on the [url=http://www.jforum.net/forums/list.page]forums[/url] or registered in the ").append("[url=http://www.jforum.net/jira]bug tracker[/url]).").append('\n').append("That's why I'm asking you to financially support this work. I love Open Source. I love to use good products without having to pay for it too. ").append("But when I see some program that is valuable to my work, that helps me making money, I think it's a good idea to support this project.").append('\n').append('\n').append("[b]Some reasons to support open projects[/b]:").append("<ul><li>Because Open Source is cool? Yes").append("<li>To thank for a great tool? Yes").append("<li>To help the project evolve because this will help my work and my earnings? Yes</ul>").append("Also, as the project grows more and more, it would be great to, sometimes, reward some of the great people who help JForum.").append('\n').append('\n').append("So, that's what I'm asking you: if JForum helps your work, saves your time (time is money, remember?) and increase your earnings, support ").append("this project. The simpler way is to make [url=http://www.jforum.net/contribute.jsp]any donation[/url] via PayPal.").append('\n').append('\n').append("JForum has grown a lot every day, since four years ago, which is a great thing, and initially it wasn't my intention to fully work on this tool. ").append("Lately, I'm spending a lot of time on it, specially to make JForum 3 a reality, to help users, to improve the program, to research about ").append("better solutions. So, your support is very welcome!").append('\n').append('\n').append("Thanks!").append('\n').append('\n').append(":arrow: [size=16][b][url=http://www.jforum.net/contribute.jsp]Click here[/url][/b] to go to the [i][b][url=http://www.jforum.net/contribute.jsp]").append("\"Support JForum\"[/url][/b][/i] page.[/size]").append('\n').append('\n');
try {
ConfigLoader.createLoginAuthenticator();
ConfigLoader.loadDaoImplementation();
SystemGlobals.loadQueries(SystemGlobals.getValue(ConfigKeys.SQL_QUERIES_GENERIC));
SystemGlobals.loadQueries(SystemGlobals.getValue(ConfigKeys.SQL_QUERIES_DRIVER));
SystemGlobals.setValue(ConfigKeys.SEARCH_INDEXING_ENABLED, "false");
JForumExecutionContext ex = JForumExecutionContext.get();
ex.setConnection(connection);
JForumExecutionContext.set(ex);
User user = new User(2);
// Create topic
Topic topic = new Topic();
topic.setPostedBy(user);
topic.setTitle("Support JForum - Please read");
topic.setTime(new Date());
topic.setType(Topic.TYPE_ANNOUNCE);
topic.setForumId(1);
TopicDAO topicDao = DataAccessDriver.getInstance().newTopicDAO();
topicDao.addNew(topic);
// Create post
Post post = new Post();
post.setSubject(topic.getTitle());
post.setTime(topic.getTime());
post.setUserId(user.getId());
post.setText(message.toString());
post.setForumId(topic.getForumId());
post.setSmiliesEnabled(true);
post.setHtmlEnabled(true);
post.setBbCodeEnabled(true);
post.setUserIp("127.0.0.1");
post.setTopicId(topic.getId());
PostDAO postDao = DataAccessDriver.getInstance().newPostDAO();
postDao.addNew(post);
// Update topic
topic.setFirstPostId(post.getId());
topic.setLastPostId(post.getId());
topicDao.update(topic);
// Update forum stats
ForumDAO forumDao = DataAccessDriver.getInstance().newForumDAO();
forumDao.incrementTotalTopics(1, 1);
forumDao.setLastPost(1, post.getId());
} finally {
SystemGlobals.setValue(ConfigKeys.SEARCH_INDEXING_ENABLED, "true");
JForumExecutionContext ex = JForumExecutionContext.get();
ex.setConnection(null);
JForumExecutionContext.set(ex);
}
}
Aggregations