jp.terasoluna.fw.service.thin.AbstractBLogicMapper.java Source code

Java tutorial

Introduction

Here is the source code for jp.terasoluna.fw.service.thin.AbstractBLogicMapper.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.service.thin;

import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jp.terasoluna.fw.exception.SystemException;
import jp.terasoluna.fw.util.BeanUtil;
import jp.terasoluna.fw.util.ClassLoadException;
import jp.terasoluna.fw.util.ClassUtil;
import jp.terasoluna.fw.util.PropertyAccessException;
import jp.terasoluna.fw.util.StringUtil;
import jp.terasoluna.fw.web.struts.actions.AbstractDownloadObject;

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

/**
 * rWlX?WbN?o?f?NX?B
 *
 * <p>
 *  rWlX?WbN?o??BLogicResources?A
 *  WebwIuWFNg?ArWlX?WbNf?[^}bsO?s@\
 *  ?W?NX?BSrWlX?WbN?o?fNX
 *  NXp??B
 *  AbstractBLogicMapper?AQ
 *  <ul>
 *   <li>BLogicResources??AANV
 *    rWlX?WbN?NXJavaBean
 *    ??@\</li>
 *   <li>rWlX?WbN?o?NX
 *    BLogicResultp?ABLogicResources??A
 *    WebwIuWFNgf@\</li>
 *  </ul>
 * </p>
 * <p>
 *  AbstractBLogicMapperTuNX?A
 *  ftHgBLogicMapper?A
 *  @\u?B
 *  ??AAbstractBLogicMapperBLogicMapper
 *  p?rWlX?WbN?o?fNX??Kv?B
 *  grWlX?WbN?o?fNX?A
 *  blogic-io.xmlsource?request?Asession?Aapplication?A
 *  dest?request?AsessionOC
 *  w??l???A?olf???B<br>
 *  l???\bh?A
 *  "getValueFrom" + source?w?B
 *  source?"factory"w???A?\bhgetValueFromFactory?B
 *  ?Sl?\bh?A
 *  getValueFromForm()?\bh??B<br>
 *  ?olf???\bh?A
 *  "setValueTo" + dest?w?B
 *  dest?"factory"w???A?\bhgetValueToFactory?B
 *  ?S?olf?\bh?A
 *  getValueToForm()?\bh??B<br>
 *  ??AftHgBLogicMapperT|?[grequest?Asession?Aapplication
 *  ?ANGXg??AZbV??AT?[ubgReLXg?
 *  ??B
 * </p>
 * <p>
 *  Strutsgp??rWlX?WbN?o?fNX?E
 *  struts-config.xmlL?q@?ABLogicIOPlugInQ??B
 * </p>
 *
 * @see jp.terasoluna.fw.service.thin.BLogicMapper
 * @see jp.terasoluna.fw.web.struts.plugins.BLogicIOPlugIn
 * @see jp.terasoluna.fw.service.thin.BLogicResult
 * @see jp.terasoluna.fw.service.thin.BLogicIO
 * @see jp.terasoluna.fw.service.thin.BLogicProperty
 * @see jp.terasoluna.fw.service.thin.BLogicResources
 * @see jp.terasoluna.fw.web.struts.actions.AbstractBLogicAction
 * @see jp.terasoluna.fw.web.struts.actions.BLogicAction
 *
 */
public abstract class AbstractBLogicMapper {

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

    /**
     * BLogicResultnull??G?[R?[h?B
     */
    private static final String NULL_RESULT_KEY = "errors.blogic.mapper.result";

    /**
     * lw??G?[R?[h?B
     */
    private static final String ERROR_SOURCE = "errors.blogic.mapper.source";

    /**
     * ?ol??w??G?[R?[h?B
     */
    private static final String ERROR_DEST = "errors.blogic.mapper.dest";

    /**
     * rWlX?WbNJavaBean??s??G?[R?[h?B
     */
    private static final String ERROR_BEAN_CREATE = "errors.blogic.mapper.create";

    /**
     * v?peBlJavaBean??G?[R?[h?B
     */
    private static final String ERROR_GETPROPERTY = "errors.blogic.mapper.getproperty";

    /**
     * v?peBlJavaBean???G?[R?[h?B
     */
    private static final String ERROR_SETPROPERTY = "errors.blogic.mapper.setproperty";

    /**
     * lCX^X??G?[R?[h?B
     */
    private static final String ERROR_GETVALUE = "errors.blogic.mapper.getvalue";

    /**
     * l?o?CX^Xf??G?[R?[h?B
     */
    private static final String ERROR_SETVALUE = "errors.blogic.mapper.setvalue";

    /**
     * fionullresult.getResultObject()lnull
     * ??G?[R?[h?B
     */
    private static final String ERROR_BEAN_NOTNULL = "errors.blogic.mapper.notnull";

    /**
     * WebwIuWFNgi[lJavaBean}bsO?B
     *
     * @param request HTTPNGXg
     * @param response HTTPX|X
     * @param io rWlX?WbN?o?
     * @return Webwi[Kvp??[^Si[JavaBean?Aio
     * NXw??nullp?B
     */
    public Object mapBLogicParams(HttpServletRequest request, HttpServletResponse response, BLogicIO io) {
        if (io == null) {
            // ionull???Anullp
            return null;
        }

        if (io.getInputBeanName() == null) {
            // Beanw??nullp?B
            if (log.isDebugEnabled()) {
                log.debug("blogic-io.inputBeanName is null.");
            }
            return null;
        }

        // JavaBean}bsO
        return setParams(request, response, io);
    }

    /**
     * rWlX?WbN?JavaBean?A
     * WebwIuWFNgi[lJavaBean}bsO?B
     * blogic-ioblogic-params}bsO?B
     * 
     * @param request
     * @param response
     * @param io
     * @return rWlX?WbN?JavaBean
     */
    protected Object setParams(HttpServletRequest request, HttpServletResponse response, BLogicIO io) {

        // Ce?[^?[??
        Iterator it = io.getBLogicParams().iterator();

        // lJavaBean??
        Object bean = null;
        // blogic-ioblogic-params???CX^X??
        if (it.hasNext()) {
            try {
                bean = ClassUtil.create(io.getInputBeanName());
            } catch (ClassLoadException e) {
                log.error("bean creation failure.");
                throw new SystemException(e, ERROR_BEAN_CREATE);
            }
        } else {
            return null;
        }

        // ListSBLogicPropertyCX^X?A???s
        while (it.hasNext()) {

            // BLogicPropertyCX^X
            BLogicProperty property = (BLogicProperty) it.next();
            // v?peB
            String propertyName = property.getProperty();

            // rWlX?WbNgpv?peB
            String blogicPropertyName = property.getBLogicProperty();

            // blogicPropertyName????ApropertyName
            if (blogicPropertyName == null) {
                blogicPropertyName = propertyName;
            }

            // li[?s?AJavaBeani[
            if (!"".equals(property.getSource()) && (property.getSource() != null)) {
                // Nl??\bh?z
                Object[] methodParams = new Object[] { propertyName, request, response };
                Class[] parameterTypes = new Class[] { String.class, HttpServletRequest.class,
                        HttpServletResponse.class };
                Object value = null;
                try {
                    // l???s
                    value = MethodUtils.invokeMethod(this,
                            "getValueFrom" + StringUtil.capitalizeInitial(property.getSource()), methodParams,
                            parameterTypes);
                } catch (NoSuchMethodException e) {
                    log.error("no such method.");
                    throw new SystemException(new BLogicMapperException(e), ERROR_GETVALUE,
                            new String[] { propertyName });
                } catch (IllegalAccessException e) {
                    log.error("illegal access to the method.");
                    throw new SystemException(new BLogicMapperException(e), ERROR_GETVALUE,
                            new String[] { propertyName });
                } catch (InvocationTargetException e) {
                    log.error("exception is thrown out by invokeMethod.");
                    throw new SystemException(new BLogicMapperException(e), ERROR_GETVALUE,
                            new String[] { propertyName });
                }

                try {
                    // l??\bh?s
                    BeanUtil.setBeanProperty(bean, blogicPropertyName, value);
                } catch (PropertyAccessException e) {
                    log.error("setBeanProperty failure.");
                    throw new SystemException(new BLogicMapperException(e), ERROR_SETPROPERTY,
                            new String[] { blogicPropertyName });
                }

            } else {
                // i[wOX??[
                log.error("source is illegal.");
                throw new SystemException(new BLogicMapperException(), ERROR_SOURCE);
            }
        }
        return bean;
    }

    /**
     * NGXgwv?peBl?B
     *
     * @param propName v?peB
     * @param request HTTPNGXg
     * @param response HTTPX|X
     * @return v?peBl
     */
    public abstract Object getValueFromRequest(String propName, HttpServletRequest request,
            HttpServletResponse response);

    /**
     * ZbVwv?peBL?[l?B
     *
     * @param propName v?peB
     * @param request HTTPNGXg
     * @param response HTTPX|X
     * @return v?peBl
     */
    public abstract Object getValueFromSession(String propName, HttpServletRequest request,
            HttpServletResponse response);

    /**
     * BLogicIO?]?AWebwIuWFNgli[?B
     *
     * @param request HTTPNGXg
     * @param response HTTPX|X
     * @param io rWlX?WbN?o?
     * @param result rWlX?WbN?o?
     */
    public void mapBLogicResult(HttpServletRequest request, HttpServletResponse response, BLogicIO io,
            BLogicResult result) {
        // BLogicResultnull`FbN
        if (result == null) {
            log.error("BLogicResult is null.");
            throw new SystemException(new BLogicMapperException(new NullPointerException()), NULL_RESULT_KEY);
        }

        // resultJavaBean
        Object bean = result.getResultObject();

        // beannull???A?~???s      
        if (bean == null) {

            return;
        }

        // beanAbstractDownloadObjectp????A
        // ?~???s      
        if (bean instanceof AbstractDownloadObject) {

            return;
        }

        // io??Abeani[??`FbN
        if (io == null) {
            if (bean != null) {
                if (log.isDebugEnabled()) {
                    log.debug("The bean should be null.");
                }
                log.error("bean is not null.");
                throw new SystemException(new BLogicMapperException(), ERROR_BEAN_NOTNULL);
            }
        }

        getResults(request, response, io, bean);
    }

    /**
     * blogic-ioblogic-resultMapping?s?B
     * 
     * @param request
     * @param response
     * @param io
     * @param bean
     */
    protected void getResults(HttpServletRequest request, HttpServletResponse response, BLogicIO io, Object bean) {

        // Ce?[^?[??
        Iterator it = io.getBLogicResults().iterator();

        // blogic-result??Abeani[??`FbN
        if (!it.hasNext()) {
            if (bean != null) {
                if (log.isDebugEnabled()) {
                    log.debug("The bean should be null.");
                }
                log.error("bean is not null.");
                throw new SystemException(new BLogicMapperException(), ERROR_BEAN_NOTNULL);
            }
        }

        // ListSBLogicPropertyNXCX^X?A???s
        while (it.hasNext()) {

            // BLogicPropertyCX^X
            BLogicProperty property = (BLogicProperty) it.next();

            // v?peB
            String propertyName = property.getProperty();

            // rWlX?WbNgpv?peB
            String blogicPropertyName = property.getBLogicProperty();

            // blogicPropertyName????ApropertyName
            if (blogicPropertyName == null) {
                blogicPropertyName = propertyName;
            }

            if (!"".equals(property.getDest()) && (property.getDest() != null)) {

                Object value = null;
                try {
                    // ?ol?\bh?s
                    value = BeanUtil.getBeanProperty(bean, blogicPropertyName);
                } catch (PropertyAccessException e) {
                    log.error("getBeanProperty failure.");
                    throw new SystemException(new BLogicMapperException(e), ERROR_GETPROPERTY,
                            new String[] { blogicPropertyName });
                }

                // i[??s?A?oli[
                Object[] methodParams = new Object[] { value, propertyName, request, response };
                Class[] parameterTypes = new Class[] { Object.class, String.class, HttpServletRequest.class,
                        HttpServletResponse.class };
                try {
                    MethodUtils.invokeMethod(this,
                            "setValueTo" + StringUtil.capitalizeInitial(property.getSource()), methodParams,
                            parameterTypes);
                } catch (NoSuchMethodException e) {
                    log.error("no such method.");
                    throw new SystemException(new BLogicMapperException(e), ERROR_SETVALUE,
                            new String[] { propertyName });
                } catch (IllegalAccessException e) {
                    log.error("illegal access to the method.");
                    throw new SystemException(new BLogicMapperException(e), ERROR_SETVALUE,
                            new String[] { propertyName });
                } catch (InvocationTargetException e) {
                    log.error("exception is thrown out by invokeMethod.");
                    throw new SystemException(new BLogicMapperException(e), ERROR_SETVALUE,
                            new String[] { propertyName });
                }
            } else {
                // i[?w?AOX??[
                log.error("dest is illegal.");
                throw new SystemException(new BLogicMapperException(), ERROR_DEST);
            }
        }
    }

    /**
     * NGXgwv?peBli[?B
     *
     * @param value ?ol
     * @param propName v?peB
     * @param request HTTPNGXg
     * @param response HTTPX|X
     */
    public abstract void setValueToRequest(Object value, String propName, HttpServletRequest request,
            HttpServletResponse response);

    /**
     * ZbVwv?peBL?[li[?B
     *
     * @param value ?ol
     * @param propName v?peB
     * @param request HTTPNGXg
     * @param response HTTPX|X
     */
    public abstract void setValueToSession(Object value, String propName, HttpServletRequest request,
            HttpServletResponse response);

    /**
     * T?[ubgReLXgwv?peBl?B
     *
     * @param propName v?peB
     * @param request HTTPNGXg
     * @param response HTTPX|X
     * @return v?peBl
     */
    public abstract Object getValueFromApplication(String propName, HttpServletRequest request,
            HttpServletResponse response);

}