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

📄 shoebo.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.businessobject;import java.math.BigDecimal;import java.util.Collection;import java.util.Date;import java.util.Iterator;import java.util.List;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import net.sf.irunninglog.canonical.Shoe;import net.sf.irunninglog.transaction.IUnitOfWork;import net.sf.irunninglog.transaction.TransactionException;import net.sf.irunninglog.transaction.TransactionalSupport;import net.sf.irunninglog.util.ConstantValues;import net.sf.irunninglog.util.ConversionException;import net.sf.irunninglog.util.Conversions;import net.sf.irunninglog.util.DTO;import net.sf.irunninglog.util.FatalRuntimeException;import net.sf.irunninglog.util.Utilities;import net.sf.irunninglog.validation.ValidationError;import net.sf.irunninglog.validation.ValidationUtilities;/** * Business object representing a <em>Shoe</em>.  This is a POJO business * object (BO) implementation that is used to represent <em>Shoe</em> * entities within the application's business layer. * * @author <a href="mailto:allan_e_lewis@yahoo.com">Allan Lewis</a> * @version $Revision: 1.2 $ $Date: 2005/06/25 15:18:19 $ * @since iRunningLog 1.0 */public class ShoeBO extends ReferenceDataBO {    /** <code>Log</code> instance for this class. */    private static final Log LOG = LogFactory.getLog(ShoeBO.class);    /** Query string used to find run data records. */    private static final String FIND_RUN_DATA_FOR_SHOE = "RunDataForShoe";    /** The shoe's <em>start date</em> field. */    private Date mStartDate;    /** The shoe's <em>start mileage</em> field. */    private BigDecimal mStartMileage;    /** The shoe's <em>retired</em> flag. */    private Boolean mRetired;    /**     * Create a new business object instance.     *     * @deprecated Use the <code>BusinessObjectFactory</code> to obtain new     *             instances     * @see BusinessObjectFactory#newInstance(String)     */    public ShoeBO() {        super();    }    /**     * Get the value of the BO's <em>start date</em> field.     *     * @return The current value of the <em>start date</em> field     */    public Date getStartDate() {        return mStartDate;    }    /**     * Set the value of the BO's <em>start date</em> field.     *     * @param value The value to be set onto the <em>start date</em> field     */    public void setStartDate(Date value) {        mStartDate = value;    }    /**     * Get the value of the BO's <em>start mileage</em> field.     *     * @return The current value of the <em>start mileage</em> field     */    public BigDecimal getStartMileage() {        return mStartMileage;    }    /**     * Set the value of the BO's <em>start mileage</em> field.     *     * @param value The value to be set onto the <em>start mileage</em> field     */    public void setStartMileage(BigDecimal value) {        mStartMileage = value;    }    /**     * Get the value of the BO's <em>mileage</em> field.     *     * @return The current value of the <em>mileage</em> field     */    public BigDecimal getMileage() {        Collection runData = null;        BigDecimal mileage = ConstantValues.BD_ZERO;        BigDecimal temp = null;        RunDataBO record = null;        String units = null;        try {            IUnitOfWork unitOfWork = TransactionalSupport.getUnitOfWork();            runData = unitOfWork.find(FIND_RUN_DATA_FOR_SHOE,                                      new Object [] {getRunnerId(), getId()});        } catch (TransactionException ex) {            throw new FatalRuntimeException(ex);        }        if (runData != null) {            for (Iterator i = runData.iterator(); i.hasNext();) {                record = (RunDataBO) i.next();                units = record.getUnits();                if (units != null                                 && units.equals(ConstantValues.STRING_MILES)) {                    mileage = mileage.add(record.getDistance());                } else if (units != null                            && units.equals(ConstantValues.STRING_KILOMETERS)) {                    temp = record.getDistance();                    temp = temp.multiply(ConstantValues.BD_KM_TO_MI);                    temp = temp.setScale(2, BigDecimal.ROUND_HALF_UP);                    mileage = mileage.add(temp);                }            }        }        return mileage;    }    /**     * Get the value of the BO's <em>total mileage</em> field.     *     * @return The current value of the <em>total mileage</em> field     */    public BigDecimal getTotalMileage() {        return (getStartMileage() == null) ? null                                          : getStartMileage().add(getMileage());    }    /**     * Get the value of the BO's <em>retired</em> flag.     *     * @return The current value of the <em>retired</em> flag     */    public Boolean isRetired() {        return mRetired;    }    /**     * Set the value of the BO's <em>retired</em> flag.     *     * @param value The value to be set onto the <em>retired</em> flag     */    public void setRetired(Boolean value) {        mRetired = value;    }    /**     * Get a data transfer objects containing values that represent the current     * state of this BO.  This is a template method that is called from the     * final method <code>getValues</code> in <code>BaseBO</code>.     *     * @return The data transfer object, populated with the appropriate values     * @see BaseBO#getValues()     */    protected DTO getValuesInternal() {        DTO valueObject = super.getValuesInternal();        String value = null;        if (LOG.isDebugEnabled()) {            LOG.debug("getValuesInternal: Contents of the value object "                      + " (before) " + valueObject);        }        valueObject.setCanonicalId(Shoe.CANONICAL_ID);        value = Conversions.dateToString(getStartDate());        valueObject.setValue(Shoe.FIELD_START_DATE, value);        value = Conversions.decimalToString(getStartMileage());        valueObject.setValue(Shoe.FIELD_START_MILEAGE, value);        value = Conversions.decimalToString(getMileage());        valueObject.setValue(Shoe.FIELD_MILEAGE, value);        value = Conversions.decimalToString(getTotalMileage());        valueObject.setValue(Shoe.FIELD_TOTAL_MILEAGE, value);        value = Conversions.booleanToString(isRetired());        valueObject.setValue(Shoe.FIELD_RETIRED, value);        if (LOG.isDebugEnabled()) {            LOG.debug("getValuesInternal: Contents of the value object (after) "                      + valueObject);        }        return valueObject;    }    /**     * Default the values of a transfer object.     *     * @param valueObject The transfer object whose values are to be defaulted     */    protected void defaultValues(DTO valueObject) {        super.defaultValues(valueObject);        String value = null;        if (LOG.isDebugEnabled()) {            LOG.debug("defaultValues: Contents of the value object (before) "                      + valueObject);        }        value = valueObject.getValue(Shoe.FIELD_START_MILEAGE);        if (Utilities.isBlank(value)) {            valueObject.setValue(Shoe.FIELD_START_MILEAGE,                                 ConstantValues.STRING_ZERO);        }        value = valueObject.getValue(Shoe.FIELD_RETIRED);        if (Utilities.isBlank(value)) {            valueObject.setValue(Shoe.FIELD_RETIRED,                                 ConstantValues.STRING_FALSE);        }        if (LOG.isDebugEnabled()) {            LOG.debug("defaultValues: Contents of the value object (before) "                      + valueObject);        }    }    /**     * Update the BO's fields with the values provided in a data transfer     * object.  This is a template method that is called from the final     * <code>setValue</code> method in <code>BaseBO</code>.     *     * @param valueObject The data transfer object containing values to be     *                    used in updating this BO     * @throws ConversionException If a conversion error occurrs while setting     *                             the fields     * @see BaseBO#setValues(DTO)     */    protected void setValuesInternal(DTO valueObject)                                                   throws ConversionException {        if (LOG.isDebugEnabled()) {            LOG.debug("setValuesInternal: Contents of the value object "                      + valueObject);        }        super.setValuesInternal(valueObject);        String value = null;        Boolean oBool = null;        value = valueObject.getValue(Shoe.FIELD_START_DATE);        setStartDate(Conversions.stringToDate(value));        value = valueObject.getValue(Shoe.FIELD_START_MILEAGE);        setStartMileage(Conversions.stringToDecimal(value));        value = valueObject.getValue(Shoe.FIELD_RETIRED);        oBool = Conversions.stringToBoolean(value);        setRetired(oBool);    }    /**     * Validate the values contained in a data transfer object.  This method     * is reponsible for perofrming all validations on the transfer object, and     * should be called before updating the BO's fields to avoid     * bad data and/or errors.     *     * @param valueObject The data transfer object whose fields are to be     *                    validated     * @return A list consisting of any errors found while validating the     *         transfer object     */    protected List validateValues(DTO valueObject) {        List errors = super.validateValues(valueObject);        if (LOG.isDebugEnabled()) {            LOG.debug("validateValues: Before validations, error list contains"                      + errors.size() + " error(s)");        }        validateStartDateValue(valueObject, errors);        validateStartMileageValue(valueObject, errors);        validateRetiredValue(valueObject, errors);        if (LOG.isDebugEnabled()) {            LOG.debug("validateValues: After validations, error list contains"                      + errors.size() + " error(s)");        }        return errors;    }    /**     * Validate the <code>start date</code> field value on a data transfer     * object.     *     * @param valueObject The data transfer object containing the value(s) to     *                    be validated     * @param errors An error list, which this method may add to if there are     *               any validation errors     */    private void validateStartDateValue(DTO valueObject, List errors) {        ValidationUtilities.executeDateValidation(valueObject,                                                  Shoe.FIELD_START_DATE,                                                  errors);    }    /**     * Validate the <code>start mileage</code> field value on a data transfer     * object.     *     * @param valueObject The data transfer object containing the value(s) to     *                    be validated     * @param errors An error list, which this method may add to if there are     *               any validation errors     */    private void validateStartMileageValue(DTO valueObject, List errors) {        String fieldName = Shoe.FIELD_START_MILEAGE;        ValidationUtilities.executeRequiredFieldValidation(valueObject,                                                           fieldName,                                                           errors);        boolean validNumber =                      ValidationUtilities.executeDecimalValidation(valueObject,                                                                   fieldName,                                                                   errors);        if (!Utilities.isBlank(valueObject.getValue(fieldName))            && validNumber) {            ValidationUtilities.executeMinValueValidation(valueObject,                                                          fieldName,                                                          errors);            ValidationUtilities.executeMaxValueValidation(valueObject,                                                          fieldName,                                                          errors);        }    }    /**     * Validate the <code>retired</code> field value on a data transfer     * object.     *     * @param valueObject The data transfer object containing the value(s) to     *                    be validated     * @param errors An error list, which this method may add to if there are     *               any validation errors     */    private void validateRetiredValue(DTO valueObject, List errors) {        String fieldName = Shoe.FIELD_RETIRED;        ValidationUtilities.executeRequiredFieldValidation(valueObject,                                                           fieldName,                                                           errors);        ValidationUtilities.executeBooleanValidation(valueObject,                                                     fieldName,                                                     errors);        String defaultValue = valueObject.getValue(Shoe.FIELD_DEFAULT);        String retiredValue = valueObject.getValue(Shoe.FIELD_RETIRED);        try {            Boolean bDefaultValue = Conversions.stringToBoolean(defaultValue);            Boolean bRetiredValue = Conversions.stringToBoolean(retiredValue);            if (bDefaultValue != null && bRetiredValue != null                                      && bDefaultValue.booleanValue()                                      && bRetiredValue.booleanValue()) {                String errorNum = ValidationUtilities.ERROR_INVALID_SHOE_FLAGS;                errors.add(new ValidationError(errorNum));            }        } catch (ConversionException ex) {            if (LOG.isDebugEnabled()) {                LOG.debug("validateRetiredValue: Could not convert flag(s)");            }        }    }}

⌨️ 快捷键说明

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