All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.xma.component.ComponentProperty Maven / Gradle / Ivy

There is a newer version: 6.0.2
Show newest version
/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

/*
 * Created on 30.01.2004
 *
 * 
 * 
 */
package at.spardat.xma.component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import at.spardat.enterprise.exc.SysException;
import at.spardat.xma.exception.Codes;

/**
 * This class serves as a helper for getting and setting propertys in a generic way.
 * For each property of a component in its generated class one instance of this class is
 * created and can be accessed by {@link Component#getPropDes()}.
 * 
 * @author s2877
 */
public class ComponentProperty {
    Component component;
    Method getter;
    Method setter;
    
    /**
     * Constructs a property descriptor.
     * @param name the name of the property
     * @param type the type of the property
     * @param component the component containing the property
     * @param genClass the class of the component in which the property is declared
     */
    public ComponentProperty(String name, Class type, Component component, Class genClass) {
        try {
            this.component=component;
    
            String getterName = "get" + Character.toUpperCase(name.charAt(0)) + name.substring(1);
            getter = genClass.getDeclaredMethod(getterName,new Class[0]);
            getter.setAccessible(true);
            
            String setterName = "set" + Character.toUpperCase(name.charAt(0)) + name.substring(1);
            setter = genClass.getDeclaredMethod(setterName,new Class[]{type});
            setter.setAccessible(true);
        } catch (NoSuchMethodException e) {
            throw new SysException(e); 
        }
    }
    
    /**
     * Gets the name of the property
     * @return the name of the property
     */
    String getName() {
        String name = getter.getName().substring(3);
        return Character.toLowerCase(name.charAt(0)) + name.substring(1);
    }
    
    /**
     * Gets the value of the property as String by calling its get-mehtod.
     * If the property is a date the format "dd.MM.yyyy HH:mm:ss" is used.
     * @return the value of the property
     */
    String getValue() {
        try {
            Object value = getter.invoke(component,new Object[0]);
            if(value instanceof Date) {
                return new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format((Date)value);
            } else if(value!=null) {
                return value.toString();
            } else {
                return null;
            }
        } catch (InvocationTargetException e) {
            throw new SysException(e.getTargetException());
        } catch (Exception exc) {
            throw new SysException(exc);
        }
    }
    
    /**
     * Sets the value of the property by calling its set-method.
     * If the property is a date the format "dd.MM.yyyy HH:mm:ss" has to be used.
     * @param value the new value for the property
     */
    void setValue(String value) {
        try {
            Class type = setter.getParameterTypes()[0];
            if(value==null) {
                setter.invoke(component,new Object[]{null});
            } else if(type.isAssignableFrom(String.class)) {
                setter.invoke(component,new Object[]{value});
            } else if(type.isAssignableFrom(Integer.TYPE) || type.isAssignableFrom(Integer.class)) {
                setter.invoke(component,new Object[]{new Integer(value)});
            } else if(type.isAssignableFrom(Long.TYPE) || type.isAssignableFrom(Long.class)) {
                setter.invoke(component,new Object[]{new Long(value)});
            } else if(type.isAssignableFrom(Float.TYPE) || type.isAssignableFrom(Float.class)) {
                setter.invoke(component,new Object[]{new Float(value)});
            } else if(type.isAssignableFrom(Double.TYPE) || type.isAssignableFrom(Double.class)) {
                setter.invoke(component,new Object[]{new Double(value)});
            } else if(type.isAssignableFrom(Boolean.TYPE) || type.isAssignableFrom(Boolean.class)) {
                setter.invoke(component,new Object[]{new Boolean(value)});
            } else if(type.isAssignableFrom(BigDecimal.class)) {
                setter.invoke(component,new Object[]{new BigDecimal(value)});
            } else if(type.isAssignableFrom(Date.class)) {
                Date date = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").parse(value);
                setter.invoke(component,new Object[]{date});
            } else {
                throw new SysException("unsupported property type "+type.getName()); 
            }
        } catch (InvocationTargetException e) {            
            throw new SysException(e.getTargetException(),Codes.getText(Codes.COMPONENT_PROPERTY_SETTING_EXCEPTION)+ e.getTargetException().toString(),component.getName()+"."+getName(),value).setCode(Codes.COMPONENT_PROPERTY_SETTING_EXCEPTION);
        } catch (IllegalArgumentException e) {            
            throw new SysException(e,Codes.getText(Codes.COMPONENT_PROPERTY_SETTING_EXCEPTION)+ e.toString(),component.getName()+"."+getName(),value).setCode(Codes.COMPONENT_PROPERTY_SETTING_EXCEPTION);
        } catch (IllegalAccessException e) {
            throw new SysException(e);
        } catch (ParseException e) {            
            throw new SysException(e,Codes.getText(Codes.COMPONENT_PROPERTY_DATE_SETTING_EXCEPTION),component.getName()+"."+getName(),value).setCode(Codes.COMPONENT_PROPERTY_SETTING_EXCEPTION);
        }
    }

    /**
     * @return true if the property is an input property or an in-out property
     */
    boolean isIn() {
        return Modifier.isPublic(setter.getModifiers());
    }
    
    /**
     * @return true if the property is an output property or an in-out property
     */
    boolean isOut() {
        return Modifier.isPublic(getter.getModifiers());
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy