Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 74153 invoked from network); 1 Apr 2004 03:37:25 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 1 Apr 2004 03:37:25 -0000 Received: (qmail 45955 invoked by uid 500); 1 Apr 2004 03:37:04 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 45724 invoked by uid 500); 1 Apr 2004 03:37:03 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 45711 invoked by uid 500); 1 Apr 2004 03:37:03 -0000 Received: (qmail 45706 invoked from network); 1 Apr 2004 03:37:02 -0000 Received: from unknown (HELO minotaur.apache.org) (209.237.227.194) by daedalus.apache.org with SMTP; 1 Apr 2004 03:37:02 -0000 Received: (qmail 74130 invoked by uid 1237); 1 Apr 2004 03:37:20 -0000 Date: 1 Apr 2004 03:37:20 -0000 Message-ID: <20040401033720.74129.qmail@minotaur.apache.org> From: husted@apache.org To: jakarta-commons-sandbox-cvs@apache.org Subject: cvs commit: jakarta-commons-sandbox/chain/apps/mailreader/src/java/org/apache/commons/chain/mailreader/struts MailReaderAction.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N husted 2004/03/31 19:37:20 Modified: chain/apps/mailreader/src/java/org/apache/commons/chain/mailreader/struts MailReaderAction.java Log: "Simplified" version using conventional Struts signatures Revision Changes Path 1.3 +161 -9 jakarta-commons-sandbox/chain/apps/mailreader/src/java/org/apache/commons/chain/mailreader/struts/MailReaderAction.java Index: MailReaderAction.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/chain/apps/mailreader/src/java/org/apache/commons/chain/mailreader/struts/MailReaderAction.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- MailReaderAction.java 29 Mar 2004 02:34:19 -0000 1.2 +++ MailReaderAction.java 1 Apr 2004 03:37:20 -0000 1.3 @@ -20,27 +20,179 @@ package org.apache.commons.chain.mailreader.struts; import org.apache.commons.chain.Context; +import org.apache.commons.chain.Command; +import org.apache.commons.chain.Catalog; +import org.apache.commons.chain.impl.ContextBase; import org.apache.commons.chain.mailreader.ClientContext; +import org.apache.commons.chain.mailreader.MailReader; import org.apache.commons.chain.mailreader.MailReaderBase; -import org.apache.struts.webapp.example.UserDatabase; +import org.apache.commons.beanutils.BeanUtils; import org.apache.struts.webapp.example.Constants; +import org.apache.struts.webapp.example.User; +import org.apache.struts.webapp.example.UserDatabase; +import org.apache.struts.action.*; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.util.Locale; +import java.util.Map; +import java.util.HashMap; /** *

Process Commands using a {@link org.apache.commons.chain.mailreader.MailReader} * {@link ClientContext}.

*/ -public class MailReaderAction extends CommandAction { +public class MailReaderAction extends Action { + + /** + *

Convert {@link ActionForm} to {@link Context}.

+ * @param form Our ActionForm (conventonal or dynamic) + * @return Context based on ActionForm values + */ + protected Context getInput(ActionForm form) throws Exception { + + Map input; + if (form instanceof DynaActionForm) { + DynaActionForm dyna = (DynaActionForm) form; + input = dyna.getMap(); + } else + try { + input = BeanUtils.describe(form); + } catch (Throwable t) { + input = new HashMap(); // FIXME: Lame resolution + } + return new ContextBase(input); + + } // See interface for JavaDoc - public ClientContext getContext(ActionHelper helper) { + public MailReader getContext( + ActionMapping mapping, + ActionForm form, + HttpServletRequest request) + throws Exception { - Locale locale = helper.getLocale(); - Context input = getInput(helper.getActionForm()); + Locale locale = getLocale(request); + Context input = getInput(form); UserDatabase database = (UserDatabase) - helper.getAttribute(Constants.DATABASE_KEY); + request.getSession().getServletContext().getAttribute(Constants.DATABASE_KEY); return new MailReaderBase(locale, input, database); + + } + + /** + *

+ * Operations to perform prior to executing business command. + * If operations fail, return an appropriate {@link ActionForward}. + * If operations succeed, return null. + *

+ * @return ActionForward to follow, or null + */ + protected ActionForward preExecute(ActionMapping mapping, + ActionForm form, + HttpServletRequest request, + HttpServletResponse response) throws Exception { + + // override to provide functionality + return null; + } + + + /** + *

+ * Return the relevant {@link org.apache.commons.chain.Command} from the default + * {@link org.apache.commons.chain.Catalog}. + *

+ * @return Command for this helper + */ + protected Command getCatalogCommand(ActionMapping mapping, + ActionForm form, + HttpServletRequest request) throws Exception { + + Catalog catalog = (Catalog) request.getSession().getServletContext().getAttribute("catalog"); + String name = mapping.getName(); + return catalog.getCommand(name); + + } + + /** + *

Transfer input properties (back) to ActionForm.

+ */ + protected void conformInput(ActionMapping mapping, + ActionForm form, + HttpServletRequest request, + MailReader mailReader) throws Exception { + + BeanUtils.copyProperties(form,mailReader.getInput()); + + } + + /** + *

Transfer framework properties (back) to framework objects.

+ */ + protected void conformState(ActionMapping mapping, + ActionForm form, + HttpServletRequest request, + MailReader mailReader) throws Exception { + + setLocale(request,mailReader.getLocale()); + User user = mailReader.getUser(); + request.getSession().setAttribute(Constants.USER_KEY, user); + + } + + /** + *

+ * Operations to perform prior to executing business command. + *

+ */ + protected ActionForward postExecute(ActionMapping mapping, + ActionForm form, + HttpServletRequest request, + HttpServletResponse response, + MailReader mailReader) throws Exception { + + conformInput(mapping,form,request,mailReader); + conformState(mapping,form,request,mailReader); + + // TODO: Expose any output + // TODO: Expose any status messages, + // TODO: Expose any error messages and find input + + return null; + + } + + /** + *

Convenience method to return nominal location.

+ * @param mapping Our ActionMapping + * @return ActionForward named "success" or null + */ + protected ActionForward findSuccess(ActionMapping mapping) { + return mapping.findForward("success"); + } + + + // See super class for JavaDoc + public ActionForward execute( + ActionMapping mapping, + ActionForm form, + HttpServletRequest request, + HttpServletResponse response) + throws Exception { + + ActionForward location; + MailReader mailReader = getContext(mapping,form,request); + + location = preExecute(mapping,form,request,response); + if (location != null) return location; + + boolean stop = getCatalogCommand(mapping,form,request).execute(mailReader); + + location = postExecute(mapping,form,request,response,mailReader); + if (location != null) return location; + + return findSuccess(mapping); } --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org