jp.terasoluna.fw.web.struts.form.ValidatorActionFormEx.java Source code

Java tutorial

Introduction

Here is the source code for jp.terasoluna.fw.web.struts.form.ValidatorActionFormEx.java

Source

/*
 * Copyright (c) 2007 NTT DATA Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jp.terasoluna.fw.web.struts.form;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import jp.terasoluna.fw.util.BeanUtil;
import jp.terasoluna.fw.util.PropertyAccessException;
import jp.terasoluna.fw.web.struts.ModuleUtil;
import jp.terasoluna.fw.web.struts.reset.Resetter;

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

/**
 * Validatort?[??[NANVtH?[NX?B
 *
 * <p>
 * Struts ValidatorActionForm p?
 * ANVtH?[NX?B<br>
 * L?AStruts?t@C(struts-config.xml) ?ANXp?
 * ANVtH?[??B
 * <p>
 * <strong> Struts?t@C(struts-config.xml) ANVtH?[?</strong>
 * </p>
 * <code><pre>
 * &lt;form-beans&gt;
 *  <b>&lt;form-bean
 *    name="_validateSampleForm"
 *    type="jp.terasoluna.sample.xxxx.SampleValidatorActionFormEx"
 *  &gt;</b>
 *  &lt;/form-bean&gt;
 * &lt;/form-beans&gt;
 * </pre></code>
 * &lt;form-bean&gt;^O name ?
 * tH?[_?w?Atype ?NXw?B<br>
 * tH?[_?? &quot;_&quot t?ARg??[
 * RequestProcessorExw?A
 * ZbV?tH?[B???B<br>
 * tH?[L?B<br>
 * <p>
 * <strong>tH?[</strong>
 * </p>
 * <code><pre>
 * public class SampleValidatorActionForm extends ValidatorActionFormEx {
 *
 *    // ID
 *    private String companyId = null;
 *    // ?[UID
 *    private String userId = null;
 *    // pX??[h
 *    private String password = null;
 *    ?E?E?E
 *    // IDsetter
 *    public void setCompanyId(String companyId) {
 *        this.companyId = companyId;
 *    }
 *
 *    // IDgetter
 *    public String getCompanyId() {
 *        return companyId;
 *    }
 *    ?E?E?E
 * }
 * </pre></code>
 * tH?[??tB?[h???AtB?[h
 * getter/setter L?q?B
 *
 * <p>
 * <strong>g_@\</strong>
 * </p>
 * <ul>
 * <li>Zbg@\?B<br>
 * T?[ubgReLXg? ResetterImpl NX
 *  reset() ?\bhN?B<br></li>
 * <li>CfbNXtv?peB??@\?B<br>
 *  getIndexCount(String fieldName) ?A?tB?[h
 * ???B<br></li>
 * <li>CfbNXtv?peB?@\?B<br>
 * z?A List CfbNXOZb^?[?\bh
 * ICfbNXvf??B<br></li>
 * <li>CfbNXtv?peB@\?B<br>
 * z?A List CfbNXOQb^?[?\bh
 *  null p?B<br></li>
 * <li>tB?[hl?XtO?B<br>
 * BLogicMapperANVtH?[tB?[hl?X?
 * modified?true??A?X?sm?B
 * <br></li>
 * </ul>
 *
 * @\??Q?l?
 * <ul>
 *  <li> Struts?t@C(struts-config.xml) L?q?A
 *   ItH?[??????A
 *    DynaValidatorActionFormEx Q??B</li>
 *  <li>tH?[B??AtB?[hl?XtO???A
 *     RequestProcessorEx Q??B</li>
 *  <li>Zbg?EtB?[hlNA@\???A ResetterImpl ?A
 *     ResetterPlugIn Q??B</li>
 *  <li>gof?[V@\  FieldChecksEx Q?</li>
 * </ul>
 * </p>
 *
 * @see
 *  jp.terasoluna.fw.web.struts.form.DynaValidatorActionFormEx
 * @see jp.terasoluna.fw.web.struts.action.RequestProcessorEx
 * @see jp.terasoluna.fw.web.struts.reset.Resetter
 * @see jp.terasoluna.fw.web.struts.plugins.ResetterPlugIn
 * @see jp.terasoluna.fw.web.struts.form.FieldChecksEx
 * @see jp.terasoluna.fw.service.thin.BLogicMapper
 *
 */
public class ValidatorActionFormEx extends ValidatorActionForm implements FormEx {

    /**
     * VAo?[WID
     */
    private static final long serialVersionUID = -744848917166154997L;

    /**
     * ?ONX?B
     */
    private static Log log = LogFactory.getLog(ValidatorActionFormEx.class);

    /**
     * l?XtO?B
     */
    private boolean modified = false;

    /**
     * wCfbNXv?peBl?B
     *
     * @param name ?v?peB
     * @param index ?CfbNX
     * @return v?peBl
     */
    public Object getIndexedValue(String name, int index) {
        Object field = null;
        try {
            field = BeanUtil.getBeanProperty(this, name);
        } catch (PropertyAccessException e) {
            // DynaValidatorActionFormExdl??A
            // O????s?B
        }
        if (field == null) {
            throw new NullPointerException("No indexed value for '" + name + "[" + index + "]'");
        } else if (field.getClass().isArray()) {
            try {
                return (Array.get(field, index));
            } catch (ArrayIndexOutOfBoundsException e) {
                return null;
            }
        } else if (field instanceof List) {
            try {
                return ((List) field).get(index);
            } catch (IndexOutOfBoundsException e) {
                return null;
            }
        } else {
            throw new IllegalArgumentException("Non-indexed property for '" + name + "[" + index + "]'");
        }
    }

    /**
     * wtB?[h???B
     *
     * @param fieldName ???tB?[h?B
     * @return tB?[hl???B
     * <p>v?peB??0?B</p>
     */
    public int getIndexCount(String fieldName) {
        if (log.isDebugEnabled()) {
            log.debug("getIndexCount(" + fieldName + ") called.");
        }
        int count = 0;
        Object value = null;
        try {
            value = BeanUtil.getBeanProperty(this, fieldName);
        } catch (PropertyAccessException e) {
            // DynaValidatorActionFormExdl??A
            // O????s?B
        }
        if (value == null) {
            count = 0;
        } else if (value.getClass().isArray()) {
            count = ((Object[]) value).length;
        } else if (value instanceof List) {
            count = ((List) value).size();
        } else if (value instanceof Map) {
            count = ((Map) value).size();
        } else {
            count = 1;
        }
        if (log.isDebugEnabled()) {
            log.debug("size = [" + count + "]");
        }
        return count;
    }

    /**
     * wCfbNXuv?peBl??B
     *
     * @param name ??CfbNXtv?peB
     * @param index ??CfbNXu
     * @param value ?v?peBl
     */
    @SuppressWarnings("unchecked")
    public void setIndexedValue(String name, int index, Object value) {
        if (log.isDebugEnabled()) {
            log.debug("set(" + name + ", " + index + ", " + value + ") called.");
        }

        Object prop = null;
        try {
            prop = BeanUtil.getBeanProperty(this, name);
        } catch (PropertyAccessException e) {
            // DynaValidatorActionFormExdl??A
            // O????s?B
        }
        if (prop == null) {
            throw new NullPointerException("No indexed value for '" + name + "[" + index + "]'");
        } else if (prop.getClass().isArray()) {
            if (index < Array.getLength(prop)) {
                Array.set(prop, index, value);
            } else {
                // CfbNX`FbN
                ActionFormUtil.checkIndexLength(index);
                // ?VKz??
                Object newArray = Array.newInstance(prop.getClass().getComponentType(), index + 1);
                // zR|?[lgRs?[
                System.arraycopy(prop, 0, newArray, 0, Array.getLength(prop));
                // R|?[lgZbg
                Array.set(newArray, index, value);
                // Q?Rs?[
                prop = newArray;
            }
            try {
                BeanUtil.setBeanProperty(this, name, prop);
            } catch (PropertyAccessException e) {
                throw new IllegalArgumentException("Cannot set property for '" + name + "[" + index + "]'");
            }
        } else if (prop instanceof List) {
            if (index < ((List) prop).size()) {
                ((List) prop).set(index, value);
            } else {
                // CfbNX`FbN
                ActionFormUtil.checkIndexLength(index);
                Object[] oldValues = ((List) prop).toArray();
                Object[] newValues = (Object[]) Array.newInstance(oldValues.getClass().getComponentType(),
                        index + 1);
                System.arraycopy(oldValues, 0, newValues, 0, oldValues.length);
                newValues[index] = value;
                ((List) prop).clear();
                ((List) prop).addAll(Arrays.asList(newValues));
            }
            try {
                BeanUtil.setBeanProperty(this, name, prop);
            } catch (PropertyAccessException e) {
                throw new IllegalArgumentException("Cannot set property for '" + name + "[" + index + "]'");
            }
        } else {
            throw new IllegalArgumentException("Non-indexed property for '" + name + "[" + index + "]'");
        }

    }

    /**
     * l?XtO?B
     *
     * @return l?XtO
     */
    public boolean isModified() {
        return this.modified;
    }

    /**
     * l?XtO??B
     *
     * @param modified l?XtO
     */
    public void setModified(boolean modified) {
        this.modified = modified;
    }

    /**
     * ANVtH?[tB?[hlZbg?s?B
     *
     * @param mapping }bsO?
     * @param request NGXg?
     */
    @Override
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        if (log.isDebugEnabled()) {
            log.debug("reset() called.");
        }

        //Prefix
        String prefix = ModuleUtil.getPrefix(request);

        try {
            Resetter resetter = (Resetter) getServlet().getServletContext()
                    .getAttribute(Resetter.RESETTER_KEY + prefix);
            if (resetter == null) {
                if (log.isDebugEnabled()) {
                    log.debug("resetter class is not specified.");
                }
                return;
            }
            resetter.reset(this, mapping, request);
        } catch (ClassCastException e) {
            log.error("", e);
            return;
        }
    }

}