*/
package mircoders.localizer.basic;
-import java.util.Arrays;
-import java.util.*;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
-import java.util.Vector;
+import java.util.Map;
import mir.entity.Entity;
import mir.session.Request;
import mir.session.Session;
import mir.session.SessionExc;
import mir.session.SessionFailure;
-import mir.session.UploadedFile;
-import mir.util.ExceptionFunctions;
+import mir.session.ValidationHelper;
+import mir.util.EntityUtility;
import mircoders.entity.EntityComment;
import mircoders.global.MirGlobal;
import mircoders.media.MediaUploadProcessor;
-import mircoders.module.*;
+import mircoders.module.ModuleCommentStatus;
+import mircoders.module.ModuleMediafolder;
import mircoders.storage.DatabaseComment;
import mircoders.storage.DatabaseCommentToMedia;
import mircoders.storage.DatabaseContent;
-import mircoders.media.*;
/**
- *
- * <p>Title: Experimental session handler for comment postings </p>
- * <p>Description: </p>
- * <p>Copyright: Copyright (c) 2003</p>
- * <p>Company: </p>
+ * Session handler for comment postings
+ *
* @author Zapata
- * @version 1.0
*/
public class MirBasicCommentPostingHandler extends MirBasicPostingSessionHandler {
- protected ModuleComment commentModule = new ModuleComment(DatabaseComment.getInstance());
+ protected DatabaseComment commentDatabase = DatabaseComment.getInstance();
protected DatabaseCommentToMedia commentToMedia = DatabaseCommentToMedia.getInstance();
+
+ public MirBasicCommentPostingHandler() {
+ this(false);
+ }
+
+
+ public MirBasicCommentPostingHandler(boolean aPersistentUploadedFiles) {
+ super(aPersistentUploadedFiles);
+
+ setResponseGenerators(
+ configuration.getString("Localizer.OpenSession.comment.EditTemplate"),
+ configuration.getString("Localizer.OpenSession.comment.DupeTemplate"),
+ configuration.getString("Localizer.OpenSession.comment.UnsupportedMediaTemplate"),
+ configuration.getString("Localizer.OpenSession.comment.DoneTemplate"));
+ }
+
protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
super.initializeResponseData(aRequest, aSession, aResponse);
- Iterator i = DatabaseComment.getInstance().getFields().iterator();
+ Iterator i = DatabaseComment.getInstance().getFieldNames().iterator();
while (i.hasNext()) {
String field = (String) i.next();
aResponse.setResponseValue(field, aRequest.getParameter(field));
}
public void validate(List aResults, Request aRequest, Session aSession) throws SessionExc, SessionFailure {
- testFieldEntered(aRequest, "title", "validationerror.missing", aResults);
- testFieldEntered(aRequest, "description", "validationerror.missing", aResults);
- testFieldEntered(aRequest, "creator", "validationerror.missing", aResults);
+ super.validate(aResults, aRequest, aSession);
+
+ ValidationHelper.testFieldEntered(aRequest, "title", "validationerror.missing", aResults);
+ ValidationHelper.testFieldEntered(aRequest, "description", "validationerror.missing", aResults);
+ ValidationHelper.testFieldEntered(aRequest, "creator", "validationerror.missing", aResults);
}
protected void initializeSession(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
String articleId = aRequest.getParameter("to_media");
if (articleId==null)
- throw new SessionExc("initializeCommentPosting: article id not set!");
+ throw new SessionExc("initializeSession: article id not set!");
aSession.setAttribute("to_media", articleId);
- };
+ }
public void finalizeComment(Request aRequest, Session aSession, EntityComment aComment) throws SessionExc, SessionFailure {
- aComment.setValueForProperty("is_published", "1");
- aComment.setValueForProperty("to_comment_status", "1");
- aComment.setValueForProperty("is_html","0");
+ try {
+ aComment.setFieldValue("is_published", "1");
+ ModuleCommentStatus module = new ModuleCommentStatus();
+ aComment.setFieldValue("to_comment_status", module.commentStatusIdForName(configuration.getString("Localizer.OpenSession.comment.DefaultCommentStatus")));
+ aComment.setFieldValue("is_html", "0");
+ aComment.setFieldValue("to_media", (String) aSession.getAttribute("to_media"));
+ }
+ catch (Throwable t) {
+ throw new SessionFailure(t);
+ }
}
public void preProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
String id;
Map values = getIntersectingValues(aRequest, DatabaseComment.getInstance());
- EntityComment comment = (EntityComment) commentModule.createNew();
- comment.setValues(values);
- comment.setValueForProperty("to_media", (String) aSession.getAttribute("to_media"));
+ EntityComment comment = (EntityComment) commentDatabase.createNewEntity();
+ comment.setFieldValues(values);
finalizeComment(aRequest, aSession, comment);
id = comment.insert();
if (id == null) {
}
}
- public void processUploadedFile(Request aRequest, Session aSession, UploadedFile aFile) throws SessionExc, SessionFailure {
+ public void processAttachment(Request aRequest, Session aSession, Attachment aFile) throws SessionExc, SessionFailure {
try {
Map values = new HashMap();
- values.put("title", aRequest.getParameter(aFile.getFieldName()+"_title"));
values.put("creator", aRequest.getParameter("creator"));
+ values.putAll(aFile.getAllAttributes());
values.put("to_publisher", "0");
values.put("is_published", "1");
+ ModuleMediafolder module = new ModuleMediafolder();
+ values.put("to_media_folder", module.mediaFolderIdForName(configuration.getString("Localizer.OpenSession.comment.DefaultMediaFolder")));
+
Entity mediaItem = MediaUploadProcessor.processMediaUpload(aFile, values);
mediaItem.update();
commentToMedia.addMedia(((EntityComment) aSession.getAttribute("comment")).getId(), mediaItem.getId());
}
}
+ public void processAttachmentError(Request aRequest, Session aSession, Attachment aFile, Throwable anError) {
+ EntityUtility.appendLineToField( ((EntityComment) aSession.getAttribute("comment")), "comment",
+ "error with attachment: " + anError.toString());
+
+ ((EntityComment) aSession.getAttribute("comment")).update();
+ }
+
public void postProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
EntityComment comment = (EntityComment) aSession.getAttribute("comment");
catch (Throwable t) {
throw new SessionFailure(t);
}
- DatabaseContent.getInstance().setUnproduced("id=" + comment.getValue("to_media"));
+ DatabaseContent.getInstance().setUnproduced("id=" + comment.getFieldValue("to_media"));
logger.info("Comment posted");
- };
-
- protected void makeInitialResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
- aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.EditTemplate"));
- };
-
- protected void makeResponse(Request aRequest, Session aSession, Response aResponse, List anErrors) throws SessionExc, SessionFailure {
- aResponse.setResponseValue("errors", anErrors);
- aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.EditTemplate"));
- };
-
- protected void makeFinalResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
- aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.DoneTemplate"));
- };
-
- protected void makeErrorResponse(Request aRequest, Session aSession, Response aResponse, Throwable anError) throws SessionExc, SessionFailure {
- anError.printStackTrace();
- Throwable rootCause = ExceptionFunctions.traceCauseException(anError);
-
- if (rootCause instanceof DuplicateCommentExc)
- aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.DupeTemplate"));
- if (rootCause instanceof ModuleMediaType.UnsupportedMimeTypeExc) {
- aResponse.setResponseValue("mimetype", ((ModuleMediaType.UnsupportedMimeTypeExc) rootCause).getMimeType());
- aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.UnsupportedMediaTemplate"));
- }
- else
- super.makeErrorResponse(aRequest, aSession, aResponse, anError);
- };
+ }
protected static class DuplicateCommentExc extends SessionExc {
public DuplicateCommentExc(String aMessage) {
super(aMessage);
}
}
-}
\ No newline at end of file
+}