org.sylvani.bot.util.SessionPropertyModel.java Source code

Java tutorial

Introduction

Here is the source code for org.sylvani.bot.util.SessionPropertyModel.java

Source

/**
 *    Copyright (C) 2016-2017 Harald Kuhn
 *
 *    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 org.sylvani.bot.util;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.PropertyUtils;
import org.sylvani.bot.ISession;

/**
 * @author hkuhn
 */
public class SessionPropertyModel<T extends Serializable> implements ISessionModel<T> {

    private static final long serialVersionUID = 1L;

    private String name;

    public SessionPropertyModel(String name) {
        this.name = name;
    }

    @Override
    public void setObject(T object, ISession session) {
        Object target = session.getAttribute(name);
        try {
            PropertyUtils.setProperty(target, name, object);
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public T getObject(ISession session) {
        Object target = session.getAttribute(name);
        try {
            return (T) PropertyUtils.getProperty(target, name);
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

}