ListAction.java :  » Development » ivatamasks » com » ivata » mask » web » struts » Java Open Source

Java Open Source » Development » ivatamasks 
ivatamasks » com » ivata » mask » web » struts » ListAction.java
/*
 * Copyright (c) 2001 - 2005 ivata limited.
 * All rights reserved.
 * -----------------------------------------------------------------------------
 * ivata masks may be redistributed under the GNU General Public
 * License as published by the Free Software Foundation;
 * version 2 of the License.
 *
 * These programs are free software; you can redistribute them and/or
 * modify them under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 2 of the License.
 *
 * These programs are distributed in the hope that they will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the GNU General Public License in the file LICENSE.txt for more
 * details.
 *
 * If you would like a copy of the GNU General Public License write to
 *
 * Free Software Foundation, Inc.
 * 59 Temple Place - Suite 330
 * Boston, MA 02111-1307, USA.
 *
 *
 * To arrange commercial support and licensing, contact ivata at
 *                  http://www.ivata.com/contact.jsp
 * -----------------------------------------------------------------------------
 * $Log: ListAction.java,v $
 * Revision 1.13  2005/10/17 18:16:21  colinmacleod
 * Changed error when there is no form - we always expect a form now
 * (because of changes in MaskRequestProcessorImplementation).
 *
 * Revision 1.12  2005/10/14 14:07:21  colinmacleod
 * Added checking for a null form.
 *
 * Revision 1.11  2005/10/11 18:54:06  colinmacleod
 * Fixed some checkstyle and javadoc issues.
 *
 * Revision 1.10  2005/10/03 10:17:25  colinmacleod
 * Fixed some style and javadoc issues.
 *
 * Revision 1.9  2005/10/02 14:06:34  colinmacleod
 * Added/improved log4j logging.
 *
 * Revision 1.8  2005/09/29 12:18:32  colinmacleod
 * Added session parameter to persistenceManager.openSession().
 * Added new method to create the list form (so you can override it.
 *
 * Revision 1.7  2005/04/11 14:52:45  colinmacleod
 * Added checking for the new button.
 *
 * Revision 1.6  2005/04/09 18:04:18  colinmacleod
 * Changed copyright text to GPL v2 explicitly.
 *
 * Revision 1.5  2005/01/19 13:14:02  colinmacleod
 * Renamed CausedByException to SystemException.
 *
 * Revision 1.4  2005/01/07 08:08:24  colinmacleod
 * Moved up a version number.
 * Changed copyright notices to 2005.
 * Updated the documentation:
 *   - started working on multiproject:site docu.
 *   - changed the logo.
 * Added checkstyle and fixed LOADS of style issues.
 * Added separate thirdparty subproject.
 * Added struts (in web), util and webgui (in webtheme) from ivata op.
 *
 * Revision 1.3  2004/12/29 15:32:19  colinmacleod
 * List/input mask actions are no longer hard-coded.
 * Overrides added via request parameters
 * - defaults via new methods on the factory.
 *
 * Revision 1.2  2004/11/12 15:10:42  colinmacleod
 * Moved persistence classes from ivata op as a replacement for
 * ValueObjectLocator.
 *
 * Revision 1.1.1.1  2004/05/16 20:40:32  colinmacleod
 * Ready for 0.1 release
 * -----------------------------------------------------------------------------
 */
package com.ivata.mask.web.struts;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.sourceforge.clientsession.ClientSession;

import com.ivata.mask.MaskFactory;
import com.ivata.mask.persistence.PersistenceManager;
import com.ivata.mask.persistence.PersistenceSession;
import com.ivata.mask.util.StringHandling;
import com.ivata.mask.util.SystemException;
/**
 * <p>
 * Load the list for a given base class. The base class is specified in a
 * request parameter, or may be specified in the request as a
 * <code>ListForm</code>.
 * </p>
 *
 * @since ivata masks 0.1 (2004-04-30)
 * @author Colin MacLeod
 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
 * @version $Revision: 1.13 $
 */
public class ListAction extends MaskAction {
    /**
     * Logger for this class.
     */
    private static final Logger logger = Logger.getLogger(ListAction.class);
    /**
     * <p>
     * Used to locate the value objects by their shared base class.
     * </p>
     */
    private PersistenceManager persistenceManager;
    /**
     * <p>
     * Create a new list action with the given value object locator.
     * </p>
     *
     * @param persistenceManagerParam
     *            used to locate the value objects by their shared base class.
     * @param maskFactoryParam {@inheritDoc}
     * @param authenticatorParam {@inheritDoc}
     */
    public ListAction(final PersistenceManager persistenceManagerParam,
            final MaskFactory maskFactoryParam,
            final MaskAuthenticator authenticatorParam) {
        super(maskFactoryParam, authenticatorParam);
        this.persistenceManager = persistenceManagerParam;
    }
    /**
     * This method does all the hard work, preparing objects for display in the
     * list.
     *
     * @param mapping {@inheritDoc}
     * @param form {@inheritDoc}
     * @param request {@inheritDoc}
     * @param response {@inheritDoc}
     * @param session {@inheritDoc}
     * @param clientSession {@inheritDoc}
     * @return {@inheritDoc}
     * @throws SystemException {@inheritDoc}
     */
    public String execute(final ActionMapping mapping,
            final ActionForm form, final HttpServletRequest request,
            final HttpServletResponse response,
            final HttpSession session, final ClientSession clientSession)
            throws SystemException {
        if (logger.isDebugEnabled()) {
            logger.debug("execute(ActionMapping mapping = " + mapping
                    + ", ActionForm form = " + form
                    + ", HttpServletRequest request = " + request
                    + ", HttpServletResponse response = " + response
                    + ", HttpSession session = " + session
                    + ", ClientSession clientSession = " + clientSession
                    + ") - start");
        }
        if (form == null) {
            throw new NullPointerException(
                "ERROR in ListAction: a valid list form must be specified.");
        }
        // if the new button was pressed, just return new to go to the new
        // action
        String clear = getFromRequestOrForm("clear", request, form);
        if (!StringHandling.isNullOrEmpty(clear)) {
            if (logger.isDebugEnabled()) {
                logger.debug("execute - end - return value = new");
            }
            return "new";
        }
        assert (form instanceof ListForm);
        ListForm listForm = (ListForm) form;

        // request parameter overrides current list form
        Class baseClass = listForm.getBaseClass();

        // if we changed to the list view from another view, build the list form
        PersistenceSession persistenceSession =
            persistenceManager.openSession(session);
        List valueObjects;
        try {
            valueObjects = persistenceManager.findAll(persistenceSession,
                    baseClass);
        } finally {
            persistenceSession.close();
        }
        listForm.setValueObjects(valueObjects);
        request.setAttribute(ListForm.REQUEST_ATTRIBUTE, listForm);

        if (logger.isDebugEnabled()) {
            logger.debug("execute - end - return value = success");
        }
        return "success";
    }
}

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.