Index: lams_tool_forum/web/WEB-INF/classes/org/lamsfoundation/lams/tool/forum/actions/ForumAction.java =================================================================== diff -u --- lams_tool_forum/web/WEB-INF/classes/org/lamsfoundation/lams/tool/forum/actions/ForumAction.java (revision 0) +++ lams_tool_forum/web/WEB-INF/classes/org/lamsfoundation/lams/tool/forum/actions/ForumAction.java (revision da429cada8bdb32348ca0f8b7ab64ec755f63c8f) @@ -0,0 +1,203 @@ +package org.lamsfoundation.lams.tool.forum.actions; + +import org.apache.log4j.Logger; +import org.apache.struts.action.*; +import org.apache.struts.upload.FormFile; +import org.lamsfoundation.lams.tool.forum.core.GenericObjectFactoryImpl; +import org.lamsfoundation.lams.tool.forum.core.PersistenceException; +import org.lamsfoundation.lams.tool.forum.persistence.Forum; +import org.lamsfoundation.lams.tool.forum.persistence.Message; +import org.lamsfoundation.lams.tool.forum.persistence.Attachment; +import org.lamsfoundation.lams.tool.forum.service.ForumManager; +import org.lamsfoundation.lams.tool.forum.forms.ForumForm; +import org.lamsfoundation.lams.tool.forum.forms.MessageForm; +import org.lamsfoundation.lams.tool.forum.util.ForumConstants; +import org.lamsfoundation.lams.tool.forum.util.FileUtils; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.File; +import java.util.*; + +/** + * Created by IntelliJ IDEA. + * User: conradb + * Date: 10/06/2005 + * Time: 12:18:57 + * To change this template use File | Settings | File Templates. + */ +public class ForumAction extends Action { + private static Logger log = Logger.getLogger(ForumAction.class.getName()); + private ForumManager forumManager; + + public void setForumManager(ForumManager forumManager) { + this.forumManager = forumManager; + } + + public ForumAction() { + this.forumManager = (ForumManager) GenericObjectFactoryImpl.getInstance().lookup("forumManager"); + //GenericObjectFactoryImpl.getInstance().configure(this); + } + public final ActionForward execute(ActionMapping mapping, ActionForm form, + HttpServletRequest request, HttpServletResponse response) throws Exception { + String param = mapping.getParameter(); + if (param.equals("createForum")) { + return createForum(mapping, form, request, response); + } + if (param.equals("editForum")) { + return editForum(mapping, form, request, response); + } + if (param.equals("getForum")) { + return getForum(mapping, form, request, response); + } + if (param.equals("deleteForum")) { + return deleteForum(mapping, form, request, response); + } + if (param.equals("createTopic")) { + return createTopic(mapping, form, request, response); + } + if (param.equals("instructions")) { + return saveInstructions(mapping, form, request, response); + } + if (param.equals("deleteAttachment")) { + return deleteAttachment(mapping, form, request, response); + } + if (param.equals("deleteTopic")) { + return deleteTopic(mapping, form, request, response); + } + if (param.equals("advanced")) { + return mapping.findForward("success"); + } + return mapping.findForward("error"); + } + + public ActionForward createForum(ActionMapping mapping, + ActionForm form, + HttpServletRequest request, + HttpServletResponse response) + throws IOException, ServletException, Exception { + ForumForm forumForm = (ForumForm) form; + Forum forum = forumForm.getForum(); + Map topics = (Map) request.getSession().getAttribute("topics"); + forum = this.forumManager.createForum(forum, forumForm.getAttachments(), topics); + forumForm.setForum(forum); + + //populate topics with new topics + List topicList = this.forumManager.getTopics(forum.getId()); + topics = new HashMap(); + Iterator it = topicList.iterator(); + while (it.hasNext()) { + Message message = (Message) it.next(); + topics.put(message.getSubject(), message); + } + + request.getSession().setAttribute("topics", topics); + request.getSession().setAttribute("topicList", topicList); + return mapping.findForward("success"); + } + + public ActionForward editForum(ActionMapping mapping, + ActionForm form, + HttpServletRequest request, + HttpServletResponse response) + throws IOException, ServletException, Exception { + ForumForm forumForm = (ForumForm) form; + Forum forum = forumForm.getForum(); + Map topics = (Map) request.getSession().getAttribute("topics"); + this.forumManager.editForum(forum, forumForm.getAttachments(), topics); + return mapping.findForward("success"); + } + + public ActionForward getForum(ActionMapping mapping, + ActionForm form, + HttpServletRequest request, + HttpServletResponse response) + throws IOException, ServletException, Exception { + Long forumId = new Long((String) request.getParameter("forumId")); + Forum forum = forumManager.getForum(forumId); + List topicList = this.forumManager.getTopics(forum.getId()); + ForumForm forumForm = new ForumForm(); + forumForm.setForum(forum); + request.getSession().setAttribute("forum", forumForm); + + Map topics = new HashMap(); + Iterator it = topicList.iterator(); + while (it.hasNext()) { + Message message = (Message) it.next(); + topics.put(message.getSubject(), message); + } + + request.getSession().setAttribute("topics", topics); + request.getSession().setAttribute("topicList", topicList); + return mapping.findForward("success"); + } + + public ActionForward deleteForum(ActionMapping mapping, + ActionForm form, + HttpServletRequest request, + HttpServletResponse response) + throws IOException, ServletException, Exception { + Long forumId = new Long((String) request.getParameter("forumId")); + forumManager.deleteForum(forumId); + return (mapping.findForward("success")); + } + + public ActionForward createTopic(ActionMapping mapping, + ActionForm form, + HttpServletRequest request, + HttpServletResponse response) + throws IOException, ServletException, PersistenceException { + MessageForm messageForm = (MessageForm) form; + Message message = messageForm.getMessage(); + Map topics = (Map) request.getSession().getAttribute("topics"); + if (topics == null) { + topics = new HashMap(); + } + topics.put(message.getSubject(), message); + request.getSession().setAttribute("topics", topics); + request.getSession().setAttribute("topicList", new ArrayList(topics.values())); + return mapping.findForward("success"); + } + + public ActionForward saveInstructions(ActionMapping mapping, + ActionForm form, + HttpServletRequest request, + HttpServletResponse response) + throws IOException, ServletException, PersistenceException { + ForumForm forumForm = (ForumForm) form; + Collection entries = forumForm.getAttachments().values(); + List attachmentList = new ArrayList(entries); + request.getSession().setAttribute("attachmentList", attachmentList); + return mapping.findForward("success"); + } + + public ActionForward deleteAttachment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws PersistenceException { + ActionForward forward = new ActionForward(); + forward.setPath(mapping.getInput()); + String fileName = (String) request.getParameter("fileName"); + ForumForm forumForm = (ForumForm) form; + Map attachments = forumForm.getAttachments(); + Attachment attachment = (Attachment) attachments.remove(fileName); + if (attachment.getId() !=null) { + this.forumManager.deleteForumAttachment(attachment.getId()); + } + List attachmentList = new ArrayList(attachments.values()); + request.getSession().setAttribute("attachmentList", attachmentList); + return mapping.findForward("success"); + } + + public ActionForward deleteTopic(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws PersistenceException { + String topicName = (String) request.getParameter("topicName"); + Map topics = (Map) request.getSession().getAttribute("topics"); + Message topic = (Message) topics.remove(topicName); + if (topic.getId() != null) { + this.forumManager.deleteMessage(topic.getId()); + } + request.getSession().setAttribute("topics", topics); + request.getSession().setAttribute("topicList", new ArrayList(topics.values())); + return mapping.findForward("success"); + } + +} Index: lams_tool_forum/web/WEB-INF/classes/org/lamsfoundation/lams/tool/forum/actions/LearningAction.java =================================================================== diff -u --- lams_tool_forum/web/WEB-INF/classes/org/lamsfoundation/lams/tool/forum/actions/LearningAction.java (revision 0) +++ lams_tool_forum/web/WEB-INF/classes/org/lamsfoundation/lams/tool/forum/actions/LearningAction.java (revision da429cada8bdb32348ca0f8b7ab64ec755f63c8f) @@ -0,0 +1,151 @@ +package org.lamsfoundation.lams.tool.forum.actions; + +import org.apache.struts.action.Action; +import org.apache.struts.action.ActionForward; +import org.apache.struts.action.ActionMapping; +import org.apache.struts.action.ActionForm; +import org.apache.log4j.Logger; +import org.lamsfoundation.lams.tool.forum.service.ForumManager; +import org.lamsfoundation.lams.tool.forum.core.GenericObjectFactoryImpl; +import org.lamsfoundation.lams.tool.forum.core.PersistenceException; +import org.lamsfoundation.lams.tool.forum.persistence.Forum; +import org.lamsfoundation.lams.tool.forum.persistence.Message; +import org.lamsfoundation.lams.tool.forum.forms.ForumForm; +import org.lamsfoundation.lams.tool.forum.forms.MessageForm; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.ServletException; +import java.io.IOException; +import java.util.List; + +/** + * Created by IntelliJ IDEA. + * User: conradb + * Date: 24/06/2005 + * Time: 10:54:09 + * To change this template use File | Settings | File Templates. + */ +public class LearningAction extends Action { + private static Logger log = Logger.getLogger(LearningAction.class.getName()); + private ForumManager forumManager; + + public void setForumManager(ForumManager forumManager) { + this.forumManager = forumManager; + } + + public LearningAction() { + this.forumManager = (ForumManager) GenericObjectFactoryImpl.getInstance().lookup("forumManager"); + //GenericObjectFactoryImpl.getInstance().configure(this); + } + public final ActionForward execute(ActionMapping mapping, ActionForm form, + HttpServletRequest request, HttpServletResponse response) throws Exception { + String param = mapping.getParameter(); + if (param.equals("openForum")) { + return getForum(mapping, form, request, response); + } + if (param.equals("editMessage")) { + return editMessage(mapping, form, request, response); + } + if (param.equals("openMessage")) { + return getMessage(mapping, form, request, response); + } + if (param.equals("deleteMessage")) { + return deleteMessage(mapping, form, request, response); + } + if (param.equals("post")) { + return post(mapping, form, request, response); + } + if (param.equals("reply")) { + return replyToMessage(mapping, form, request, response); + } + return mapping.findForward("error"); + } + + public ActionForward post(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) + throws PersistenceException { + Long topicId = new Long((String) request.getParameter("topicId")); + Long parentId = new Long((String) request.getParameter("parentId")); + Message parent = forumManager.getMessage(parentId); + MessageForm messageForm = new MessageForm(); + Message reply = new Message(); + reply.setParent(parent); + reply.setSubject(parent.getSubject()); + messageForm.setTopicId(topicId); + messageForm.setParentId(parentId); + messageForm.setMessage(reply); + request.setAttribute("messageForm", messageForm); + return mapping.findForward("success"); + } + + public ActionForward getForum(ActionMapping mapping, + ActionForm form, + HttpServletRequest request, + HttpServletResponse response) + throws IOException, ServletException, Exception { + Long forumId = new Long((String) request.getParameter("forumId")); + Forum forum = forumManager.getForum(forumId); + List topicList = this.forumManager.getTopics(forum.getId()); + ForumForm forumForm = new ForumForm(); + forumForm.setForum(forum); + request.setAttribute("forum", forumForm); + request.setAttribute("forumTopics", topicList); + return mapping.findForward("success"); + } + + public ActionForward editMessage(ActionMapping mapping, + ActionForm form, + HttpServletRequest request, + HttpServletResponse response) + throws IOException, ServletException, PersistenceException { + MessageForm messageForm = (MessageForm) form; + Message message = messageForm.getMessage(); + this.forumManager.editMessage(message); + return mapping.findForward("success"); + } + + public ActionForward replyToMessage(ActionMapping mapping, + ActionForm form, + HttpServletRequest request, + HttpServletResponse response) + throws IOException, ServletException, PersistenceException { + MessageForm messageForm = (MessageForm) form; + Message message = messageForm.getMessage(); + Long parentId = messageForm.getParentId(); + Message reply = this.forumManager.replyToMessage(parentId, message); + request.setAttribute("messageId", parentId); + request.setAttribute("topicId", messageForm.getTopicId()); + request.setAttribute("message", reply.getParent()); + return mapping.findForward("success"); + } + + public ActionForward deleteMessage(ActionMapping mapping, + ActionForm form, + HttpServletRequest request, + HttpServletResponse response) + throws IOException, ServletException, Exception { + Long messageId = new Long((String) request.getParameter("messageId")); + this.forumManager.deleteMessage(messageId); + return mapping.findForward("success"); + } + + public ActionForward getMessage(ActionMapping mapping, + ActionForm form, + HttpServletRequest request, + HttpServletResponse response) + throws IOException, ServletException, Exception { + Long topicId = new Long((String) request.getParameter("topicId")); + + /* + Long messageId = new Long((String) request.getParameter("parentId")); + Message message = forumManager.getMessage(messageId); + */ + + Message message = forumManager.getMessage(topicId); + request.setAttribute("message", message); + return mapping.findForward("success"); + } + + + +} Index: lams_tool_forum/web/WEB-INF/classes/org/lamsfoundation/lams/tool/forum/forms/ForumForm.java =================================================================== diff -u --- lams_tool_forum/web/WEB-INF/classes/org/lamsfoundation/lams/tool/forum/forms/ForumForm.java (revision 0) +++ lams_tool_forum/web/WEB-INF/classes/org/lamsfoundation/lams/tool/forum/forms/ForumForm.java (revision da429cada8bdb32348ca0f8b7ab64ec755f63c8f) @@ -0,0 +1,118 @@ +package org.lamsfoundation.lams.tool.forum.forms; + +import org.lamsfoundation.lams.tool.forum.persistence.Forum; +import org.lamsfoundation.lams.tool.forum.persistence.Attachment; +import org.lamsfoundation.lams.tool.forum.util.ForumConstants; +import org.lamsfoundation.lams.tool.forum.util.FileUtils; +import org.apache.struts.upload.FormFile; +import org.apache.struts.action.ActionErrors; +import org.apache.struts.action.ActionMapping; +import org.apache.struts.action.ActionError; +import org.apache.struts.validator.ValidatorForm; +import org.apache.log4j.Logger; + +import java.io.File; +import java.util.*; + +/** + * + * Message Form. + * @struts.form name="forumForm" + * Created by IntelliJ IDEA. + * User: conradb + * Date: 10/06/2005 + * Time: 15:44:36 + * To change this template use File | Settings | File Templates. + */ +public class ForumForm extends ValidatorForm { + protected Forum forum; + private FormFile offlineFile; + private FormFile onlineFile; + protected Map attachments; + private static Logger logger = Logger.getLogger(ForumForm.class.getName()); + + public ForumForm() { + this.forum = new Forum(); + this.forum.setTitle("New Forum"); + this.attachments = new HashMap(); + } + + public void setForum(Forum forum) { + this.forum = forum; + } + + public Forum getForum() { + return forum; + } + + public void setOnlineFile(FormFile onlineFile) { + this.onlineFile = onlineFile; + } + + public FormFile getOnlineFile() { + return onlineFile; + } + + public void setOfflineFile(FormFile offlineFile) { + this.offlineFile = offlineFile; + } + + public FormFile getOfflineFile() { + return offlineFile; + } + + public void setAttachments(Map attachments) { + this.attachments = attachments; + } + + public Map getAttachments() { + return this.attachments; + } + + public ActionErrors validate(ActionMapping mapping, + javax.servlet.http.HttpServletRequest request) { + ActionErrors errors = super.validate(mapping, request); + try{ + // if ("".equals(forum.getTitle())) { + // ActionError error = new ActionError("error.valueReqd"); + // errors.add("forum.title", error); + // } + if (onlineFile != null && !(onlineFile.getFileName().trim().equals(""))) { + if (onlineFile.getFileSize() > ForumConstants.MAX_FILE_SIZE) { + ActionError ae = new ActionError("error.inputFileTooLarge"); + errors.add("onlineFile", ae); + } else { + String fileName = onlineFile.getFileName(); + Attachment attachment = new Attachment(); + File afile = FileUtils.getFile(fileName, onlineFile.getInputStream()); + attachment.setName(fileName); + attachment.setContentType(onlineFile.getContentType()); + attachment.setData(FileUtils.getBytes(afile)); + attachment.setType(Attachment.TYPE_ONLINE); + attachments.put(fileName, attachment); + onlineFile = null; + } + } + if (offlineFile != null && !(offlineFile.getFileName().trim().equals(""))) { + if (offlineFile.getFileSize() > ForumConstants.MAX_FILE_SIZE) { + ActionError ae = new ActionError("error.inputFileTooLarge"); + errors.add("offlineFile", ae); + } else { + String fileName = offlineFile.getFileName(); + Attachment attachment = new Attachment(); + File afile = FileUtils.getFile(fileName, offlineFile.getInputStream()); + attachment.setName(fileName); + attachment.setContentType(offlineFile.getContentType()); + attachment.setData(FileUtils.getBytes(afile)); + attachment.setType(Attachment.TYPE_OFFLINE); + attachments.put(fileName, attachment); + offlineFile = null; + } + } + } catch (Exception e) { + logger.error("", e); + } + return errors; + } + +} Index: lams_tool_forum/web/WEB-INF/classes/org/lamsfoundation/lams/tool/forum/forms/MessageForm.java =================================================================== diff -u --- lams_tool_forum/web/WEB-INF/classes/org/lamsfoundation/lams/tool/forum/forms/MessageForm.java (revision 0) +++ lams_tool_forum/web/WEB-INF/classes/org/lamsfoundation/lams/tool/forum/forms/MessageForm.java (revision da429cada8bdb32348ca0f8b7ab64ec755f63c8f) @@ -0,0 +1,58 @@ +package org.lamsfoundation.lams.tool.forum.forms; + +import org.lamsfoundation.lams.tool.forum.persistence.Message; +import org.apache.struts.validator.ValidatorForm; + +/** + * + * Message Form. + * @struts.form name="messageForm" + * + * Created by IntelliJ IDEA. + * User: conradb + * Date: 10/06/2005 + * Time: 15:44:47 + * To change this template use File | Settings | File Templates. + */ +public class MessageForm extends ValidatorForm { + protected Message message; + protected Long forumId; + protected Long parentId; + protected Long topicId; + + public MessageForm() { + this.message = new Message(); + } + + public void setMessage(Message message) { + this.message = message; + } + + public Message getMessage() { + return message; + } + + public void setForumId(Long forumId) { + this.forumId = forumId; + } + + public Long getForumId() { + return this.forumId; + } + + public void setParentId(Long parentId) { + this.parentId = parentId; + } + + public Long getParentId() { + return this.parentId; + } + + public void setTopicId(Long topicId) { + this.topicId = topicId; + } + + public Long getTopicId() { + return this.topicId; + } +}