⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 queryrundatabymonth.java

📁 A Java web application, based on Struts and Hibernate, that serves as an online running log. Users m
💻 JAVA
字号:
/* @LICENSE_COPYRIGHT@ */
package net.sf.irunninglog.servlet.action;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import net.sf.irunninglog.service.IQueryService;
import net.sf.irunninglog.service.ServiceException;
import net.sf.irunninglog.servlet.UserContainer;
import net.sf.irunninglog.servlet.formbean.RunDataByMonthFormBean;
import net.sf.irunninglog.util.ConstantValues;
import net.sf.irunninglog.util.ConversionException;
import net.sf.irunninglog.util.FatalRuntimeException;
import net.sf.irunninglog.util.Utilities;

/**
 * Class used to query run data.  This class retrieves all run data for the
 * current runner (for a month/year specified in request parameters).  The
 * results of the query are stored in a <code>RunDataByMonthFormBean</code>.
 *
 * @author <a href="mailto:allan_e_lewis@yahoo.com">Allan Lewis</a>
 * @version $Revision: 1.1.1.1 $ $Date: 2005/06/23 01:49:00 $
 * @since iRunningLog 1.0
 */
public final class QueryRunDataByMonth extends BaseQueryAction {

    /** Log instance for this class. */
    private static final Log LOG = LogFactory.getLog(QueryRunDataByMonth.class);

    /**
     * Perform any work that needs to be done before the actual query
     * happens.  This method is responsible for updating the form bean
     * appropriately based on request parameters in preparation for the actual
     * query.
     *
     * @param mapping The <code>ActionMapping</code> used to select this
     *                instance
     * @param form The <code>RunDataByMonthFormBean</code> bean that will store
     *             results
     * @param request The HTTP request being processed
     * @param container The <code>UserContainer</code> object for the current
     *                  user
     */
    protected void executeBeforeQuery(ActionMapping mapping,
                                      ActionForm form,
                                      HttpServletRequest request,
                                      UserContainer container) {

        super.executeBeforeQuery(mapping, form, request, container);

        RunDataByMonthFormBean data = (RunDataByMonthFormBean) form;
        String action = request.getParameter(ConstantValues.STRING_ACTION);

        if (LOG.isDebugEnabled()) {
            LOG.debug("executeBeforeQuery: Form bean " + data);
            LOG.debug("executeBeforeQuery: Action is '" + action + "'");
        }

        if (!Utilities.isBlank(action)) {
            String sMonth = request.getParameter(ConstantValues.STRING_MONTH);
            String sYear = request.getParameter(ConstantValues.STRING_YEAR);

            if (LOG.isDebugEnabled()) {
                LOG.debug("executeBeforeQuery: Month is " + sMonth);
                LOG.debug("executeBeforeQuery: Year is " + sYear);
            }

            if (Utilities.isBlank(sMonth) || Utilities.isBlank(sYear)) {
                LOG.error("Unable to continue due to null parameter(s)");
                throw new IllegalArgumentException("Unable to continue due to"
                                                   + " null parameter(s)");
            }

            data.setMonth(Integer.parseInt(sMonth));
            data.setYear(Integer.parseInt(sYear));

            if (action.equals(ConstantValues.STRING_NEXT)) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("executeBeforeQuery: Incrementing month");
                }
                data.incrementMonth();
            } else if (action.equals(ConstantValues.STRING_PREVIOUS)) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("executeBeforeQuery: Decrementing month");
                }
                data.decrementMonth();
            } else {
                LOG.error("Unknown action: " + action);
                throw new IllegalArgumentException("Unknown action: " + action);
            }
        }
    }

    /**
     * Execute a query to retrieve all run data for the current runner in a
     * given month.
     *
     * @param mapping The <code>ActionMapping</code> used to select this
     *                instance
     * @param form The optional <code>ActionForm</code> bean for this request
     * @param request The HTTP request being processed
     * @param container The <code>UserContainer</code> object for the current
     *                  user
     * @return The collection of objects found by the query
     * @throws ServiceException If there is an error performing the query
     */
    protected Collection executeQuery(ActionMapping mapping,
                                      ActionForm form,
                                      HttpServletRequest request,
                                      UserContainer container)
                                                       throws ServiceException {

        RunDataByMonthFormBean data = (RunDataByMonthFormBean) form;
        IQueryService service = container.getQueryService();
        String runnerId = container.getUserName();
        int month = data.getMonth();
        int year = data.getYear();

        if (LOG.isDebugEnabled()) {
            LOG.debug("executeQuery: Querying using the runnerId " + runnerId);
            LOG.debug("executeQuery: Querying using the month " + month);
            LOG.debug("executeQuery: Querying using the year " + year);
        }

        Collection results =
                     service.findRunDataForRunnerByMonth(runnerId, month, year);

        if (LOG.isDebugEnabled()) {
            LOG.debug("executeQuery: Found this many results" + results.size());
            LOG.debug("executeQuery: Results are" + results);
        }

        return results;
    }

    /**
     * Perform any work that needs to be done after the actual query
     * happens.  This method will store the results of the query in the
     * <code>RunDataByMonthFormBean</code> parameter.
     *
     * @param mapping The <code>ActionMapping</code> used to select this
     *                instance
     * @param form The <code>RunDataByMonthFormBean</code> used to store
     *             results
     * @param request The HTTP request being processed
     * @param container The <code>UserContainer</code> object for the current
     *                  user
     * @param results The collection of results returned from the query
     */
    protected void executeAfterQuery(ActionMapping mapping,
                                     ActionForm form,
                                     HttpServletRequest request,
                                     UserContainer container,
                                     Collection results) {

        super.executeAfterQuery(mapping, form, request, container, results);
        RunDataByMonthFormBean data = (RunDataByMonthFormBean) form;

        if (LOG.isDebugEnabled()) {
            LOG.debug("executeAfterQuery: Form bean before populating " + data);
        }

        try {
            data.populate(createValueBeans(mapping, results));
        } catch (ConversionException ex) {
            LOG.error("Unable to populate form bean", ex);
            throw new FatalRuntimeException(ex);
        }

        if (LOG.isDebugEnabled()) {
            LOG.debug("executeAfterQuery: Form bean after populating " + data);
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -