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

Java tutorial

Introduction

Here is the source code for jp.terasoluna.fw.web.struts.form.DynaValidatorActionFormEx.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.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.DynaValidatorActionForm;

/**
 * IANVtH?[NX?B
 *
 * <p>
 * DynaValidatorActionFormEx p?A
 * Struts?t@C(struts-config.xml) ??`?A
 * ANVtH?[I???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.fw.web.thin.form.DynaValidatorActionFormEx"&gt;</b>
 *    &lt;form-property name="companyId" type="java.lang.String" /&gt;
 *    &lt;form-property name="userId"    type="java.lang.String" /&gt;
 *    &lt;form-property name="password"  type="java.lang.String" /&gt;
 *    &lt;form-property name="longName"  type="java.lang.String" /&gt;
 *    &lt;form-property name="codeArray" type="java.util.ArrayList" /&gt;
 *  &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>
 * <br>
 * <strong>g_@\</strong><br>
 * <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?AListCfbNXOZb^?[?\bh
 * ICfbNXvf??B<br></li>
 * <li>CfbNXtv?peB@\?B<br>
 * z?AListCfbNXOQb^?[?\bh
 * nullp?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>tH?[NX?????A
 *   ValidatorActionFormExQ??B</li>
 *  <li>tH?[B??AtB?[hl?XtO???A
 *   RequestProcessorExQ??B</li>
 *  <li>Zbg?EtB?[hlNA@\???AResetterImpl?A
 *    ResetterPlugInQ??B</li>
 *  <li>gof?[V@\ FieldChecksEx Q??B</li>
 * </ul>
 * </p>
 *
 * @see jp.terasoluna.fw.web.struts.form.ValidatorActionFormEx
 * @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 DynaValidatorActionFormEx extends DynaValidatorActionForm implements FormEx {

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

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

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

    /**
     * CfbNXtv?peB?B
     *
     * <p>
     *  CfbNXOnull
     *  StrutsftHgg?B
     * </p>
     *
     * @param name tB?[h
     * @param index CfbNX
     * @return v?peBl
     */
    @Override
    public Object get(String name, int index) {
        if (log.isDebugEnabled()) {
            log.debug("get(" + name + ", " + index + ") called.");
        }

        Object value = dynaValues.get(name);
        if (value == null) {
            throw new NullPointerException("No indexed value for '" + name + "[" + index + "]'");
        } else if (value.getClass().isArray()) {
            try {
                return (Array.get(value, index));
            } catch (ArrayIndexOutOfBoundsException e) {
                return null;
            }
        } else if (value instanceof List) {
            try {
                return ((List) value).get(index);
            } catch (IndexOutOfBoundsException e) {
                return null;
            }
        } else {
            throw new IllegalArgumentException("Non-indexed property for '" + name + "[" + index + "]'");
        }
    }

    /**
     * CfbNXtv?peB?B
     *
     * <p>
     *  CfbNXOnull
     *  StrutsftHgg?B
     * </p>
     *
     * @param name tB?[h
     * @param index CfbNX
     * @return v?peBl
     */
    public Object getIndexedValue(String name, int index) {
        return this.get(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 = dynaValues.get(fieldName);
        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;
    }

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

    /**
     * CfbNXtv?peBZbg?B
     *
     * <p>
     *  StrutsDynaActionFormv?peB^
     *  Listz^gpO??A
     *  TCY?AITCY
     *  ?X?A???g?B
     * </p>
     *
     * @param name Zbg?tB?[h
     * @param index Zbg?CfbNX
     * @param value Zbg?tB?[hl
     */
    public void setIndexedValue(String name, int index, Object value) {

        if (log.isDebugEnabled()) {
            log.debug("setIndexedValue(" + name + ", " + index + ", " + value + ") called.");
        }

        this.set(name, index, value);
    }

    /**
     * CfbNXtv?peBZbg?B
     *
     * <p>
     *  StrutsDynaActionFormv?peB^
     *  Listz^gpO??A
     *  TCY?AITCY
     *  ?X?A???g?B
     * </p>
     *
     * @param name Zbg?tB?[h
     * @param index Zbg?CfbNX
     * @param value Zbg?tB?[hl
     */
    @SuppressWarnings("unchecked")
    @Override
    public void set(String name, int index, Object value) {

        if (log.isDebugEnabled()) {
            log.debug("set(" + name + ", " + index + ", " + value + ") called.");
        }

        Object prop = dynaValues.get(name);
        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;
            }
            dynaValues.put(name, prop);
        } 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));
            }
            dynaValues.put(name, prop);
        } 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;
    }

}