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

📄 rundatabymonthformbean.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.formbean;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.sf.irunninglog.util.ConstantValues;
import net.sf.irunninglog.util.ConversionException;
import net.sf.irunninglog.util.Conversions;

/**
 * Form bean used to organize run data.  This bean represents a particular
 * month/year combination, and is useful for storing run data for days that
 * fall within that month.  Accessor and mutator methods are available to
 * specify the month and year.  The <code>populate</code> method can be used
 * to load a collection of run data objects, and will handle the work of
 * associating the correct run data entries with the appropriate days.
 * This bean stores the run data in <code>DayFormBean</code> objects, which are
 * in turn stored in <code>WeekFormBean</code> objects.  The collection of weeks
 * is accessible using the <code>getWeeks</code> method; from the weeks the
 * various days and their associated run data may be accessed.
 *
 * @author <a href="mailto:allan_e_lewis@yahoo.com">Allan Lewis</a>
 * @version $Revision: 1.1.1.1 $ $Date: 2005/06/23 01:49:01 $
 * @since iRunningLog 1.0
 */
public final class RunDataByMonthFormBean extends FormBean {

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

    /** Default size for a list of days of the month. */
    private static final int DEFAULT_DAY_LIST_SIZE = 35;
    /** Format in which to display the date. */
    private static final String DATE_FORMAT = "MMMM yyyy";

    /** Internal calendar. */
    private Calendar mCalendar;
    /** List of <code>WeekFormBean</code>s for this form bean. */
    private List mWeeks;

    /**
     * Create a new form bean representing the current month and year.
     */
    public RunDataByMonthFormBean() {
        super();

        mCalendar = GregorianCalendar.getInstance();
        // This is needed - otherwise things break on the 29th of any month!
        mCalendar.set(Calendar.DAY_OF_MONTH, 1);

        mWeeks = new ArrayList();
    }

    /**
     * Create a new form bean representing a specified month and year.
     *
     * @param month The month for this form bean
     * @param year The year for this form bean
     */
    public RunDataByMonthFormBean(int month, int year) {
        this();

        setMonth(month);
        setYear(year);
    }

    /**
     * Get the month for this form bean.
     *
     * @return The form bean's month
     */
    public int getMonth() {
        return mCalendar.get(Calendar.MONTH);
    }

    /**
     * Set the value to be used for this form bean's month.
     *
     * @param month The value for the form bean's month
     */
    public void setMonth(int month) {
        mCalendar.set(Calendar.MONTH, month);
    }

    /**
     * Decrement the month of this form bean by 1.  This will automatically
     * adjust the year value for the form bean if needed.
     */
    public void decrementMonth() {
        mCalendar.add(Calendar.MONTH, -1);
    }

    /**
     * Increment the month of this form bean by 1.  This will automatically
     * adjust the year value for the form bean if needed.
     */
    public void incrementMonth() {
        mCalendar.add(Calendar.MONTH, 1);
    }

    /**
     * Get the year for this form bean.
     *
     * @return The form bean's year
     */
    public int getYear() {
        return mCalendar.get(Calendar.YEAR);
    }

    /**
     * Set the value to be used for this form bean's year.
     *
     * @param year The value for the form bean's year
     */
    public void setYear(int year) {
        mCalendar.set(Calendar.YEAR, year);
    }

    /**
     * Populate this form bean with a collection of run data form bean objects.
     * This method will first clear out any existing run data, and then store
     * the run data objects from the parameter collection.
     *
     * @param runData The collection of <code>RunDataFormBean</code> objects
     *                to be stored within this bean
     * @throws ConversionException If there is an error converting any of the
     *                             date information contained in the run
     *                             data collection
     */
    public void populate(Collection runData) throws ConversionException {
        mWeeks.clear();

        if (LOG.isDebugEnabled()) {
            LOG.debug("populate: Populating using this collection " + runData);
        }

        if (runData == null) {
            return;
        }

        List bPlaceholders = getBeginPlaceholders();
        List ePlaceholders = getEndPlaceholders();

        ArrayList days = new ArrayList(DEFAULT_DAY_LIST_SIZE);
        List tempDays = getDays();
        RunDataFormBean day = null;
        Date date = null;
        int index;
        Calendar cal = GregorianCalendar.getInstance();
        for (Iterator i = runData.iterator(); i.hasNext();) {
            day = (RunDataFormBean) i.next();

            if (LOG.isDebugEnabled()) {
                LOG.debug("populate: Adding this form bean " + day);
            }

            date = Conversions.stringToDate(day.getDate());

            // Make sure date is non-null, and that it is within this month
            if (date == null) {
                LOG.error("All run data must have a date.");
                throw new IllegalArgumentException("All run data must have"
                                                   + " a date.");
            }
            cal.setTime(date);
            if (cal.get(Calendar.MONTH) != getMonth()
                                      || cal.get(Calendar.YEAR) != getYear()) {
                LOG.error("Run data does not fall within this bean's month.");
                throw new IllegalArgumentException("Run data does not fall"
                                                   + "within this bean's"
                                                   + " month.");
            }

            index = cal.get(Calendar.DAY_OF_MONTH) - 1;
            ((DayFormBean) tempDays.get(index)).setRunData(day);
        }

        if (LOG.isDebugEnabled()) {
            LOG.debug("populate: Number of begin placeholders "
                      + bPlaceholders.size());
            LOG.debug("populate: Number of days " + tempDays.size());
            LOG.debug("populate: Number of end placeholders "
                      + ePlaceholders.size());
        }

        days.addAll(bPlaceholders);
        days.addAll(tempDays);
        days.addAll(ePlaceholders);

        int numWeeks = days.size() / ConstantValues.INT_DAYS_IN_A_WEEK;
        int count = 0;
        WeekFormBean week = null;
        Iterator iter = days.iterator();

        for (int i = 0; i < numWeeks; i++) {
            week = new WeekFormBean();
            week.setSunday((DayFormBean) iter.next());
            week.setMonday((DayFormBean) iter.next());
            week.setTuesday((DayFormBean) iter.next());
            week.setWednesday((DayFormBean) iter.next());
            week.setThursday((DayFormBean) iter.next());
            week.setFriday((DayFormBean) iter.next());
            week.setSaturday((DayFormBean) iter.next());
            mWeeks.add(week);
        }
    }

    /**
     * Get the collection of week form beans associated with this form bean.
     *
     * @return The collection of <code>WeekFormBean</code>s associated with this
     *         form bean
     */
    public Collection getWeeks() {
        return Collections.unmodifiableList(mWeeks);
    }

    /**
     * Get a formatted representation of the month/year for this form bean.
     *
     * @return The month and year for this form bean, formatted as 'MMMM yyyy'
     */
    public String getDateString() {
        DateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
        return formatter.format(mCalendar.getTime());
    }

    /**
     * Get the parameters to be used by the application's view tier to
     * identifying the previous month.
     *
     * @return The map of parameters used to identify the previous month
     */
    public Map getPreviousParameters() {
        Map params = new HashMap();

        params.put(ConstantValues.STRING_ACTION,
                                                ConstantValues.STRING_PREVIOUS);
        params.put(ConstantValues.STRING_MONTH,
                                 String.valueOf(mCalendar.get(Calendar.MONTH)));
        params.put(ConstantValues.STRING_YEAR,
                                 String.valueOf(mCalendar.get(Calendar.YEAR)));

        return Collections.unmodifiableMap(params);
    }

    /**
     * Get the parameters to be used by the application's view tier to
     * identifying the next month.
     *
     * @return The map of parameters used to identify the next month
     */
    public Map getNextParameters() {
        Map params = new HashMap();

        params.put(ConstantValues.STRING_ACTION, ConstantValues.STRING_NEXT);
        params.put(ConstantValues.STRING_MONTH,
                                 String.valueOf(mCalendar.get(Calendar.MONTH)));
        params.put(ConstantValues.STRING_YEAR,
                                 String.valueOf(mCalendar.get(Calendar.YEAR)));

        return Collections.unmodifiableMap(params);
    }

    /**
     * Get the number of days in the month for this form bean.
     *
     * @return The number of days
     */
    private int getNumberOfDaysInMonth() {
        return mCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * Get a list of placeholder <code>DayFormBean</code>s for the beginning
     * of a month.  If the first of the month is any day other than Sunday,
     * this will return the appropriate number of placeholder form beans.
     *
     * @return A list (possibly empty) of placeholder <code>DayFormBean</code>s
     */
    private List getBeginPlaceholders() {
        Calendar tempCalendar = (Calendar) mCalendar.clone();
        tempCalendar.set(Calendar.DAY_OF_MONTH, 1);

        List placeholders = new ArrayList();
        while (tempCalendar.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
            placeholders.add(new DayFormBean());
            tempCalendar.add(Calendar.DAY_OF_WEEK, -1);
        }

        return placeholders;
    }

    /**
     * Get a list of <code>DayFormBean</code>s.  This will return a list of size
     * <code>getNumberOfDaysInMonth</code> filled with <code>DatFormBean</code>
     * instances, with one form bean representing each day of the month.
     *
     * @return A list <code>DayFormBean</code>s representing the days of the
     *         current month
     */
    private List getDays() {
        int numDays = getNumberOfDaysInMonth();
        List days = new ArrayList();

        for (int i = 1; i <= numDays; i++) {
            DayFormBean day = new DayFormBean(i,
                                              mCalendar.get(Calendar.MONTH),
                                              mCalendar.get(Calendar.YEAR));
            days.add(day);
        }

        return days;
    }

    /**
     * Get a list of placeholder <code>DayFormBean</code>s for the end
     * of a month.  If the last day of the month is any day other than Saturday,
     * this will return the appropriate number of placeholder form beans.
     *
     * @return A list (possibly empty) of placeholder <code>DayFormBean</code>s
     */
    private List getEndPlaceholders() {
        Calendar tempCalendar = (Calendar) mCalendar.clone();
        tempCalendar.set(Calendar.DAY_OF_MONTH, getNumberOfDaysInMonth());

        List placeholders = new ArrayList();
        while (tempCalendar.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) {
            placeholders.add(new DayFormBean());
            tempCalendar.add(Calendar.DAY_OF_WEEK, 1);
        }

        return placeholders;
    }

}

⌨️ 快捷键说明

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