package org.nemesis.forum.webapp.admin.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.nemesis.forum.Forum;
import org.nemesis.forum.ForumFactory;
import org.nemesis.forum.ForumThread;
import org.nemesis.forum.Message;
import org.nemesis.forum.exception.ForumNotFoundException;
import org.nemesis.forum.exception.ForumThreadNotFoundException;
import org.nemesis.forum.exception.UnauthorizedException;
public class DelMessageAction extends BaseAction {
static protected Log log = LogFactory.getLog(DelMessageAction.class);
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
//check logon
checkUser(request);
ActionErrors errors = new ActionErrors();
boolean isRootmessage=false;
try {
ForumFactory forumFactory = ForumFactory.getInstance(getAuthToken(request));
Forum forum = forumFactory.getForum(Integer.parseInt(request.getParameter("id")));
request.setAttribute("id", request.getParameter("id"));
//check permission
checkPermission(request, OperationConstants.DELETE_MESSAGE, forum);
ForumThread t=forum.getThread( Integer.parseInt(request.getParameter("threadID")));
Message m=t.getMessage( Integer.parseInt(request.getParameter("messageID")));
if(t.getRootMessage().getID()==(m.getID()))isRootmessage=true;
t.deleteMessage(m);
} catch (ForumNotFoundException e) {
errors.add("general", new ActionError("content.forumNotFound"));
} catch (ForumThreadNotFoundException e) {
errors.add("general", new ActionError("content.threadNotFound"));
} catch (UnauthorizedException ue) {
errors.add("general", new ActionError("content.Unauthorized"));
} catch (Exception e) {
String eid = this.getClass().getName() + "_" + System.currentTimeMillis();
log.error("eid:" + eid + "\nsessionID" + request.getSession().getId(), e);
errors.add("general", new ActionError("error.general", "erreur id:" + eid));
}
if (!errors.isEmpty()) {
saveErrors(request, errors);
return mapping.findForward("cancel");
}
if(isRootmessage) return mapping.findForward("success2");
else return mapping.findForward("success");
}
}