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

📄 hasgeneratedidbo.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.util.List;

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

import net.sf.irunninglog.canonical.HasGeneratedId;
import net.sf.irunninglog.util.ConstantValues;
import net.sf.irunninglog.util.ConversionException;
import net.sf.irunninglog.util.DTO;
import net.sf.irunninglog.util.Utilities;
import net.sf.irunninglog.validation.ValidationUtilities;

/**
 * Business object (BO) representaion of any object containing a
 * generated <em>id</em> field.  This abstract class provides all of the
 * funtionality needed to access, update, and validate the value of the
 * <em>id</em> field.
 *
 * @author <a href="mailto:allan_e_lewis@yahoo.com">Allan Lewis</a>
 * @version $Revision: 1.1.1.1 $ $Date: 2005/06/23 01:48:54 $
 * @since iRunningLog 1.0
 */
public abstract class HasGeneratedIdBO extends HasRunnerIdBO {

    /** <code>Log</code> instance for this class. */
    private static final Log LOG = LogFactory.getLog(HasGeneratedIdBO.class);

    /** The BO's <em>id</em> field. */
    private String mId;

    /**
     * Get the value of the BO's <em>id</em> field.
     *
     * @return The current value of the <em>id</em> field
     */
    public String getId() {
        return mId;
    }

    /**
     * Set the value of the BO's <em>id</em> field.
     *
     * @param value The value to be set onto the <em>id</em> field
     */
    public void setId(String value) {
        mId = value;
    }

    /**
     * Get a string representing the primary key value of this business object.
     *
     * @return The primary key of the business object
     */
    public String getPrimaryKey() {
        return getId();
    }

    /**
     * Update the object's primary key value based on the values contained
     * in a transfer object.
     *
     * @param valueObject The transfer object containing values to be used in
     *                    formulating the primary key value
     */
    public void setPrimaryKey(DTO valueObject) {
        String id = (valueObject == null) ? null
                                : valueObject.getValue(HasGeneratedId.FIELD_ID);
        setId(id);
    }

    /**
     * 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);
        }

        value = getId();
        valueObject.setValue(HasGeneratedId.FIELD_ID, 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);
        }

        // Default id for now - it will be generated later
        value = valueObject.getValue(HasGeneratedId.FIELD_ID);
        if (Utilities.isBlank(value)) {
            valueObject.setValue(HasGeneratedId.FIELD_ID,
                                 ConstantValues.STRING_UNSAVED_VALUE);
        }

        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;

        value = valueObject.getValue(HasGeneratedId.FIELD_ID);
        setId(value);
    }

    /**
     * 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)");
        }

        validateIdValue(valueObject, errors);

        if (LOG.isDebugEnabled()) {
            LOG.debug("validateValues: After validations, error list contains"
                      + errors.size() + " error(s)");
        }

        return errors;
    }

    /**
     * Validate the <code>id</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 validateIdValue(DTO valueObject, List errors) {
        String fieldName = HasGeneratedId.FIELD_ID;

        ValidationUtilities.executeRequiredFieldValidation(valueObject,
                                                           fieldName,
                                                           errors);

        ValidationUtilities.executeMaxLengthValidation(valueObject,
                                                       fieldName,
                                                       errors);
    }

}

⌨️ 快捷键说明

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