jp.co.opentone.bsol.framework.web.view.PagePropertyUtil.java Source code

Java tutorial

Introduction

Here is the source code for jp.co.opentone.bsol.framework.web.view.PagePropertyUtil.java

Source

/*
 * Copyright 2016 OPEN TONE Inc.
 *
 * 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.co.opentone.bsol.framework.web.view;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;

import jp.co.opentone.bsol.framework.core.exception.ReflectionRuntimeException;
import jp.co.opentone.bsol.framework.web.extension.jsf.annotation.Transfer;

/**
 * Page??????.
 * @author opentone
 */
public class PagePropertyUtil {

    /**
     * ???.
     * ???.
     */
    private PagePropertyUtil() {
    }

    /**
     * page??{@link Transfer}????????.
     * <p>
     * ???????????????.
     * </p>
     * @param page
     *            page
     * @return {@link Transfer}????
     */
    public static Map<String, Object> collectTransferValues(Page page) {
        Map<String, Object> values = new HashMap<String, Object>();

        for (Class<?> c = page.getClass(); c != null; c = c.getSuperclass()) {
            for (Field field : c.getDeclaredFields()) {
                if (field.isAnnotationPresent(Transfer.class)) {
                    String name = field.getName();
                    Object value;
                    try {
                        value = PropertyUtils.getProperty(page, name);
                        if (value != null) {
                            values.put(name, value);
                        }
                    } catch (IllegalAccessException e) {
                        throw new ReflectionRuntimeException(e);
                    } catch (InvocationTargetException e) {
                        throw new ReflectionRuntimeException(e);
                    } catch (NoSuchMethodException e) {
                        throw new ReflectionRuntimeException(e);
                    }
                }
            }
        }
        return values;
    }

    /**
     * page???values??.
     * <p>
     * ???????????????.
     * </p>
     * @param page
     *            page
     * @param values
     *            ?
     */
    public static void copyProperties(Page page, Map<String, Object> values) {
        if (values != null && !values.isEmpty()) {
            try {
                PropertyUtils.copyProperties(page, values);
            } catch (IllegalAccessException e) {
                throw new ReflectionRuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new ReflectionRuntimeException(e);
            } catch (NoSuchMethodException e) {
                throw new ReflectionRuntimeException(e);
            }
        }
    }
}