Example usage for org.apache.commons.beanutils PropertyUtils getPropertyDescriptor

List of usage examples for org.apache.commons.beanutils PropertyUtils getPropertyDescriptor

Introduction

In this page you can find the example usage for org.apache.commons.beanutils PropertyUtils getPropertyDescriptor.

Prototype

public static PropertyDescriptor getPropertyDescriptor(Object bean, String name)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Retrieve the property descriptor for the specified property of the specified bean, or return null if there is no such descriptor.

For more details see PropertyUtilsBean.

Usage

From source file:com.puppycrawl.tools.checkstyle.internal.XdocsPagesTest.java

private static void validatePropertySectionProperties(String fileName, String sectionName, Node subSection,
        Object instance, Set<String> properties) throws Exception {
    boolean skip = true;
    boolean didJavadocTokens = false;
    boolean didTokens = false;

    for (Node row : XmlUtil.getChildrenElements(XmlUtil.getFirstChildElement(subSection))) {
        if (skip) {
            skip = false;/*from  ww w .  jav  a 2 s .c o m*/
            continue;
        }
        Assert.assertFalse(fileName + " section '" + sectionName + "' should have token properties last",
                didTokens);

        final List<Node> columns = new ArrayList<>(XmlUtil.getChildrenElements(row));

        final String propertyName = columns.get(0).getTextContent();
        Assert.assertTrue(
                fileName + " section '" + sectionName + "' should not contain the property: " + propertyName,
                properties.remove(propertyName));

        if ("tokens".equals(propertyName)) {
            final AbstractCheck check = (AbstractCheck) instance;
            validatePropertySectionPropertyTokens(fileName, sectionName, check, columns);
            didTokens = true;
        } else if ("javadocTokens".equals(propertyName)) {
            final AbstractJavadocCheck check = (AbstractJavadocCheck) instance;
            validatePropertySectionPropertyJavadocTokens(fileName, sectionName, check, columns);
            didJavadocTokens = true;
        } else {
            Assert.assertFalse(
                    fileName + " section '" + sectionName
                            + "' should have javadoc token properties next to last, before tokens",
                    didJavadocTokens);
            Assert.assertFalse(
                    fileName + " section '" + sectionName + "' should have a description for " + propertyName,
                    columns.get(1).getTextContent().trim().isEmpty());

            final String actualTypeName = columns.get(2).getTextContent().replace("\n", "").replace("\r", "")
                    .replaceAll(" +", " ").trim();
            final String actualValue = columns.get(3).getTextContent().replace("\n", "").replace("\r", "")
                    .replaceAll(" +", " ").trim();

            Assert.assertFalse(
                    fileName + " section '" + sectionName + "' should have a type for " + propertyName,
                    actualTypeName.isEmpty());

            final PropertyDescriptor descriptor = PropertyUtils.getPropertyDescriptor(instance, propertyName);
            final Class<?> clss = descriptor.getPropertyType();
            final String expectedTypeName = getModulePropertyExpectedTypeName(clss, instance, propertyName);
            final String expectedValue = getModulePropertyExpectedValue(clss, instance, propertyName);

            if (expectedTypeName != null) {
                Assert.assertEquals(
                        fileName + " section '" + sectionName + "' should have the type for " + propertyName,
                        expectedTypeName, actualTypeName);
                if (expectedValue != null) {
                    Assert.assertEquals(fileName + " section '" + sectionName + "' should have the value for "
                            + propertyName, expectedValue, actualValue);
                }
            }
        }
    }
}

From source file:ca.sqlpower.dao.SPPersisterListener.java

public void propertyChanged(PropertyChangeEvent evt) {
    SPObject source = (SPObject) evt.getSource();
    String uuid = source.getUUID();
    String propertyName = evt.getPropertyName();
    Object oldValue = evt.getOldValue();
    Object newValue = evt.getNewValue();

    try {//from ww  w  . j  av  a 2  s  .co  m
        if (!PersisterHelperFinder.findPersister(source.getClass()).getPersistedProperties()
                .contains(propertyName)) {
            logger.debug(
                    "Tried to persist a property that shouldn't be. Ignoring the property: " + propertyName);
            return;
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    if (!((SPObject) evt.getSource()).getRunnableDispatcher().isForegroundThread()) {
        throw new RuntimeException("Property change " + evt + " not fired on the foreground.");
    }

    Object oldBasicType = converter.convertToBasicType(oldValue);
    Object newBasicType = converter.convertToBasicType(newValue);

    PersistedSPOProperty property = null;
    for (PersistedSPOProperty p : persistedProperties.get(uuid)) {
        if (p.getPropertyName().equals(propertyName)) {
            property = p;
            break;
        }
    }
    if (property != null) {
        boolean valuesMatch;
        if (property.getNewValue() == null) {
            valuesMatch = oldBasicType == null;
        } else {
            // Check that the old property's new value is equal to the new change's old value.
            // Also, accept the change if it is the same as the last one.
            valuesMatch = property.getNewValue().equals(oldBasicType)
                    || (property.getOldValue().equals(oldBasicType)
                            && property.getNewValue().equals(newBasicType));
        }
        if (!valuesMatch) {
            try {
                throw new RuntimeException("Multiple property changes do not follow after each other properly. "
                        + "Property " + property.getPropertyName() + ", on object " + source + " of type "
                        + source.getClass() + ", Old " + oldBasicType + ", new " + property.getNewValue());
            } finally {
                this.rollback();
            }
        }
    }

    if (wouldEcho()) {
        //The persisted property was changed by a persist call received from the server.
        //The property is removed from the persist calls as it now matchs what is
        //in the server.
        persistedProperties.remove(uuid, property);
        return;
    }

    transactionStarted(TransactionEvent.createStartTransactionEvent(this,
            "Creating start transaction event from propertyChange on object "
                    + evt.getSource().getClass().getSimpleName() + " and property name "
                    + evt.getPropertyName()));

    //Not persisting non-settable properties.
    //TODO A method in the persister helpers would make more sense than
    //using reflection here.
    PropertyDescriptor propertyDescriptor;
    try {
        propertyDescriptor = PropertyUtils.getPropertyDescriptor(source, propertyName);
    } catch (Exception ex) {
        this.rollback();
        throw new RuntimeException(ex);
    }

    if (propertyDescriptor == null || propertyDescriptor.getWriteMethod() == null) {
        transactionEnded(TransactionEvent.createEndTransactionEvent(this));
        return;
    }

    DataType typeForClass = PersisterUtils.getDataType(newValue == null ? Void.class : newValue.getClass());

    boolean unconditional = false;
    if (property != null) {
        // Hang on to the old value
        oldBasicType = property.getOldValue();
        // If an object was created, and unconditional properties are being sent,
        // this will maintain that flag in the event of additional property changes
        // to the same property in the same transaction.
        unconditional = property.isUnconditional();
        persistedProperties.remove(uuid, property);
    }
    logger.debug("persistProperty(" + uuid + ", " + propertyName + ", " + typeForClass.name() + ", " + oldValue
            + ", " + newValue + ")");
    persistedProperties.put(uuid, new PersistedSPOProperty(uuid, propertyName, typeForClass, oldBasicType,
            newBasicType, unconditional));

    this.transactionEnded(TransactionEvent.createEndTransactionEvent(this));
}

From source file:com.xwtec.xwserver.util.json.JSONObject.java

/**
 * Creates a bean from a JSONObject, with the specific configuration.
 *//*from   www .j  a va 2 s  .  c  o m*/
public static Object toBean(JSONObject jsonObject, Object root, JsonConfig jsonConfig) {
    if (jsonObject == null || jsonObject.isNullObject() || root == null) {
        return root;
    }

    Class rootClass = root.getClass();
    if (rootClass.isInterface()) {
        throw new JSONException("Root bean is an interface. " + rootClass);
    }

    Map classMap = jsonConfig.getClassMap();
    if (classMap == null) {
        classMap = Collections.EMPTY_MAP;
    }

    Map props = JSONUtils.getProperties(jsonObject);
    PropertyFilter javaPropertyFilter = jsonConfig.getJavaPropertyFilter();
    for (Iterator entries = jsonObject.names(jsonConfig).iterator(); entries.hasNext();) {
        String name = (String) entries.next();
        Class type = (Class) props.get(name);
        Object value = jsonObject.get(name);
        if (javaPropertyFilter != null && javaPropertyFilter.apply(root, name, value)) {
            continue;
        }
        String key = JSONUtils.convertToJavaIdentifier(name, jsonConfig);
        try {
            PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(root, key);
            if (pd != null && pd.getWriteMethod() == null) {
                log.info("Property '" + key + "' of " + root.getClass() + " has no write method. SKIPPED.");
                continue;
            }

            if (!JSONUtils.isNull(value)) {
                if (value instanceof JSONArray) {
                    if (pd == null || List.class.isAssignableFrom(pd.getPropertyType())) {
                        Class targetClass = findTargetClass(key, classMap);
                        targetClass = targetClass == null ? findTargetClass(name, classMap) : targetClass;
                        Object newRoot = jsonConfig.getNewBeanInstanceStrategy().newInstance(targetClass, null);
                        List list = JSONArray.toList((JSONArray) value, newRoot, jsonConfig);
                        setProperty(root, key, list, jsonConfig);
                    } else {
                        Class innerType = JSONUtils.getInnerComponentType(pd.getPropertyType());
                        Class targetInnerType = findTargetClass(key, classMap);
                        if (innerType.equals(Object.class) && targetInnerType != null
                                && !targetInnerType.equals(Object.class)) {
                            innerType = targetInnerType;
                        }
                        Object newRoot = jsonConfig.getNewBeanInstanceStrategy().newInstance(innerType, null);
                        Object array = JSONArray.toArray((JSONArray) value, newRoot, jsonConfig);
                        if (innerType.isPrimitive() || JSONUtils.isNumber(innerType)
                                || Boolean.class.isAssignableFrom(innerType) || JSONUtils.isString(innerType)) {
                            array = JSONUtils.getMorpherRegistry()
                                    .morph(Array.newInstance(innerType, 0).getClass(), array);
                        } else if (!array.getClass().equals(pd.getPropertyType())) {
                            if (!pd.getPropertyType().equals(Object.class)) {
                                Morpher morpher = JSONUtils.getMorpherRegistry()
                                        .getMorpherFor(Array.newInstance(innerType, 0).getClass());
                                if (IdentityObjectMorpher.getInstance().equals(morpher)) {
                                    ObjectArrayMorpher beanMorpher = new ObjectArrayMorpher(
                                            new BeanMorpher(innerType, JSONUtils.getMorpherRegistry()));
                                    JSONUtils.getMorpherRegistry().registerMorpher(beanMorpher);
                                }
                                array = JSONUtils.getMorpherRegistry()
                                        .morph(Array.newInstance(innerType, 0).getClass(), array);
                            }
                        }
                        setProperty(root, key, array, jsonConfig);
                    }
                } else if (String.class.isAssignableFrom(type) || JSONUtils.isBoolean(type)
                        || JSONUtils.isNumber(type) || JSONUtils.isString(type)
                        || JSONFunction.class.isAssignableFrom(type)) {
                    if (pd != null) {
                        if (jsonConfig.isHandleJettisonEmptyElement() && "".equals(value)) {
                            setProperty(root, key, null, jsonConfig);
                        } else if (!pd.getPropertyType().isInstance(value)) {
                            Morpher morpher = JSONUtils.getMorpherRegistry()
                                    .getMorpherFor(pd.getPropertyType());
                            if (IdentityObjectMorpher.getInstance().equals(morpher)) {
                                log.warn("Can't transform property '" + key + "' from " + type.getName()
                                        + " into " + pd.getPropertyType().getName()
                                        + ". Will register a default BeanMorpher");
                                JSONUtils.getMorpherRegistry().registerMorpher(
                                        new BeanMorpher(pd.getPropertyType(), JSONUtils.getMorpherRegistry()));
                            }
                            setProperty(root, key,
                                    JSONUtils.getMorpherRegistry().morph(pd.getPropertyType(), value),
                                    jsonConfig);
                        } else {
                            setProperty(root, key, value, jsonConfig);
                        }
                    } else if (root instanceof Map) {
                        setProperty(root, key, value, jsonConfig);
                    } else {
                        log.warn("Tried to assign property " + key + ":" + type.getName() + " to bean of class "
                                + root.getClass().getName());
                    }
                } else {
                    if (pd != null) {
                        Class targetClass = pd.getPropertyType();
                        if (jsonConfig.isHandleJettisonSingleElementArray()) {
                            JSONArray array = new JSONArray().element(value, jsonConfig);
                            Class newTargetClass = findTargetClass(key, classMap);
                            newTargetClass = newTargetClass == null ? findTargetClass(name, classMap)
                                    : newTargetClass;
                            Object newRoot = jsonConfig.getNewBeanInstanceStrategy().newInstance(newTargetClass,
                                    null);
                            if (targetClass.isArray()) {
                                setProperty(root, key, JSONArray.toArray(array, newRoot, jsonConfig),
                                        jsonConfig);
                            } else if (Collection.class.isAssignableFrom(targetClass)) {
                                setProperty(root, key, JSONArray.toList(array, newRoot, jsonConfig),
                                        jsonConfig);
                            } else if (JSONArray.class.isAssignableFrom(targetClass)) {
                                setProperty(root, key, array, jsonConfig);
                            } else {
                                setProperty(root, key, toBean((JSONObject) value, newRoot, jsonConfig),
                                        jsonConfig);
                            }
                        } else {
                            if (targetClass == Object.class) {
                                targetClass = findTargetClass(key, classMap);
                                targetClass = targetClass == null ? findTargetClass(name, classMap)
                                        : targetClass;
                            }
                            Object newRoot = jsonConfig.getNewBeanInstanceStrategy().newInstance(targetClass,
                                    null);
                            setProperty(root, key, toBean((JSONObject) value, newRoot, jsonConfig), jsonConfig);
                        }
                    } else if (root instanceof Map) {
                        Class targetClass = findTargetClass(key, classMap);
                        targetClass = targetClass == null ? findTargetClass(name, classMap) : targetClass;
                        Object newRoot = jsonConfig.getNewBeanInstanceStrategy().newInstance(targetClass, null);
                        setProperty(root, key, toBean((JSONObject) value, newRoot, jsonConfig), jsonConfig);
                    } else {
                        log.warn("Tried to assign property " + key + ":" + type.getName() + " to bean of class "
                                + rootClass.getName());
                    }
                }
            } else {
                if (type.isPrimitive()) {
                    // assume assigned default value
                    log.warn("Tried to assign null value to " + key + ":" + type.getName());
                    setProperty(root, key, JSONUtils.getMorpherRegistry().morph(type, null), jsonConfig);
                } else {
                    setProperty(root, key, null, jsonConfig);
                }
            }
        } catch (JSONException jsone) {
            throw jsone;
        } catch (Exception e) {
            throw new JSONException("Error while setting property=" + name + " type " + type, e);
        }
    }

    return root;
}

From source file:net.sf.json.JSONObject.java

/**
 * Creates a bean from a JSONObject, with the specific configuration.
 *///from  ww w. ja va2s .com
public static Object toBean(JSONObject jsonObject, Object root, JsonConfig jsonConfig) {
    if (jsonObject == null || jsonObject.isNullObject() || root == null) {
        return root;
    }

    Class rootClass = root.getClass();
    if (rootClass.isInterface()) {
        throw new JSONException("Root bean is an interface. " + rootClass);
    }

    Map classMap = jsonConfig.getClassMap();
    if (classMap == null) {
        classMap = Collections.EMPTY_MAP;
    }

    Map props = JSONUtils.getProperties(jsonObject);
    PropertyFilter javaPropertyFilter = jsonConfig.getJavaPropertyFilter();
    for (Iterator entries = jsonObject.names(jsonConfig).iterator(); entries.hasNext();) {
        String name = (String) entries.next();
        Class type = (Class) props.get(name);
        Object value = jsonObject.get(name);
        if (javaPropertyFilter != null && javaPropertyFilter.apply(root, name, value)) {
            continue;
        }
        String key = JSONUtils.convertToJavaIdentifier(name, jsonConfig);
        try {
            PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(root, key);
            if (pd != null && pd.getWriteMethod() == null) {
                log.info("Property '" + key + "' of " + root.getClass() + " has no write method. SKIPPED.");
                continue;
            }

            if (!JSONUtils.isNull(value)) {
                if (value instanceof JSONArray) {
                    if (pd == null || List.class.isAssignableFrom(pd.getPropertyType())) {
                        Class targetClass = resolveClass(classMap, key, name, type);
                        Object newRoot = jsonConfig.getNewBeanInstanceStrategy().newInstance(targetClass, null);
                        List list = JSONArray.toList((JSONArray) value, newRoot, jsonConfig);
                        setProperty(root, key, list, jsonConfig);
                    } else {
                        Class innerType = JSONUtils.getInnerComponentType(pd.getPropertyType());
                        Class targetInnerType = findTargetClass(key, classMap);
                        if (innerType.equals(Object.class) && targetInnerType != null
                                && !targetInnerType.equals(Object.class)) {
                            innerType = targetInnerType;
                        }
                        Object newRoot = jsonConfig.getNewBeanInstanceStrategy().newInstance(innerType, null);
                        Object array = JSONArray.toArray((JSONArray) value, newRoot, jsonConfig);
                        if (innerType.isPrimitive() || JSONUtils.isNumber(innerType)
                                || Boolean.class.isAssignableFrom(innerType) || JSONUtils.isString(innerType)) {
                            array = JSONUtils.getMorpherRegistry()
                                    .morph(Array.newInstance(innerType, 0).getClass(), array);
                        } else if (!array.getClass().equals(pd.getPropertyType())) {
                            if (!pd.getPropertyType().equals(Object.class)) {
                                Morpher morpher = JSONUtils.getMorpherRegistry()
                                        .getMorpherFor(Array.newInstance(innerType, 0).getClass());
                                if (IdentityObjectMorpher.getInstance().equals(morpher)) {
                                    ObjectArrayMorpher beanMorpher = new ObjectArrayMorpher(
                                            new BeanMorpher(innerType, JSONUtils.getMorpherRegistry()));
                                    JSONUtils.getMorpherRegistry().registerMorpher(beanMorpher);
                                }
                                array = JSONUtils.getMorpherRegistry()
                                        .morph(Array.newInstance(innerType, 0).getClass(), array);
                            }
                        }
                        setProperty(root, key, array, jsonConfig);
                    }
                } else if (String.class.isAssignableFrom(type) || JSONUtils.isBoolean(type)
                        || JSONUtils.isNumber(type) || JSONUtils.isString(type)
                        || JSONFunction.class.isAssignableFrom(type)) {
                    if (pd != null) {
                        if (jsonConfig.isHandleJettisonEmptyElement() && "".equals(value)) {
                            setProperty(root, key, null, jsonConfig);
                        } else if (!pd.getPropertyType().isInstance(value)) {
                            Morpher morpher = JSONUtils.getMorpherRegistry()
                                    .getMorpherFor(pd.getPropertyType());
                            if (IdentityObjectMorpher.getInstance().equals(morpher)) {
                                log.warn("Can't transform property '" + key + "' from " + type.getName()
                                        + " into " + pd.getPropertyType().getName()
                                        + ". Will register a default BeanMorpher");
                                JSONUtils.getMorpherRegistry().registerMorpher(
                                        new BeanMorpher(pd.getPropertyType(), JSONUtils.getMorpherRegistry()));
                            }
                            setProperty(root, key,
                                    JSONUtils.getMorpherRegistry().morph(pd.getPropertyType(), value),
                                    jsonConfig);
                        } else {
                            setProperty(root, key, value, jsonConfig);
                        }
                    } else if (root instanceof Map) {
                        setProperty(root, key, value, jsonConfig);
                    } else {
                        log.warn("Tried to assign property " + key + ":" + type.getName() + " to bean of class "
                                + root.getClass().getName());
                    }
                } else {
                    if (pd != null) {
                        Class targetClass = pd.getPropertyType();
                        if (jsonConfig.isHandleJettisonSingleElementArray()) {
                            JSONArray array = new JSONArray().element(value, jsonConfig);
                            Class newTargetClass = resolveClass(classMap, key, name, type);
                            Object newRoot = jsonConfig.getNewBeanInstanceStrategy().newInstance(newTargetClass,
                                    (JSONObject) value);
                            if (targetClass.isArray()) {
                                setProperty(root, key, JSONArray.toArray(array, newRoot, jsonConfig),
                                        jsonConfig);
                            } else if (Collection.class.isAssignableFrom(targetClass)) {
                                setProperty(root, key, JSONArray.toList(array, newRoot, jsonConfig),
                                        jsonConfig);
                            } else if (JSONArray.class.isAssignableFrom(targetClass)) {
                                setProperty(root, key, array, jsonConfig);
                            } else {
                                setProperty(root, key, toBean((JSONObject) value, newRoot, jsonConfig),
                                        jsonConfig);
                            }
                        } else {
                            if (targetClass == Object.class) {
                                targetClass = resolveClass(classMap, key, name, type);
                                if (targetClass == null) {
                                    targetClass = Object.class;
                                }
                            }
                            Object newRoot = jsonConfig.getNewBeanInstanceStrategy().newInstance(targetClass,
                                    (JSONObject) value);
                            setProperty(root, key, toBean((JSONObject) value, newRoot, jsonConfig), jsonConfig);
                        }
                    } else if (root instanceof Map) {
                        Class targetClass = findTargetClass(key, classMap);
                        targetClass = targetClass == null ? findTargetClass(name, classMap) : targetClass;
                        Object newRoot = jsonConfig.getNewBeanInstanceStrategy().newInstance(targetClass, null);
                        setProperty(root, key, toBean((JSONObject) value, newRoot, jsonConfig), jsonConfig);
                    } else {
                        log.warn("Tried to assign property " + key + ":" + type.getName() + " to bean of class "
                                + rootClass.getName());
                    }
                }
            } else {
                if (type.isPrimitive()) {
                    // assume assigned default value
                    log.warn("Tried to assign null value to " + key + ":" + type.getName());
                    setProperty(root, key, JSONUtils.getMorpherRegistry().morph(type, null), jsonConfig);
                } else {
                    setProperty(root, key, null, jsonConfig);
                }
            }
        } catch (JSONException jsone) {
            throw jsone;
        } catch (Exception e) {
            throw new JSONException("Error while setting property=" + name + " type " + type, e);
        }
    }

    return root;
}

From source file:io.uengine.util.ReflectionUtils.java

/**
 *  ?? ?  Setter ./*  www . j a  va2  s.c  om*/
 *
 * @param instance  ?
 * @param fieldName ? 
 * @return Setter
 * @throws io.uengine.common.exception.ServiceException Setter    
 */
public static Method getSetterMethod(Object instance, String fieldName) throws ServiceException {
    try {
        PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(instance, fieldName);
        return propertyDescriptor.getWriteMethod();
    } catch (Exception e) {
        String message = MessageFormatter
                .format("Cannot find setter method of '{}' in '{}'.", fieldName, instance.getClass().getName())
                .getMessage();
        throw new ServiceException(message, e);
    }
}

From source file:io.uengine.util.ReflectionUtils.java

/**
 *  ?? ?  Getter .//from w w w  . jav  a2s.c o  m
 *
 * @param instance  ?
 * @param fieldName ? 
 * @return Getter
 * @throws io.uengine.common.exception.ServiceException Getter    
 */
public static Method getGetterMethod(Object instance, String fieldName) throws ServiceException {
    try {
        PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(instance, fieldName);
        return propertyDescriptor.getReadMethod();
    } catch (Exception e) {
        String message = MessageFormatter
                .format("Cannot find getter method of '{}' in '{}'.", fieldName, instance.getClass().getName())
                .getMessage();
        throw new ServiceException(message, e);
    }
}

From source file:net.firejack.platform.core.store.BaseStore.java

private <T extends Annotation> T getMethodAnnotation(Class<T> clazz, Object example, String name) {
    T annotation = null;/* w w w.ja  v  a 2  s  . c o m*/
    try {
        PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(example, name);
        Method method = PropertyUtils.getReadMethod(propertyDescriptor);
        annotation = method.getAnnotation(clazz);
    } catch (Exception e) {
        logger.warn("Can't find " + clazz.getName() + " annotation.", e);
    }
    return annotation;
}

From source file:ca.sqlpower.wabit.dao.session.WorkspacePersisterListener.java

public void propertyChanged(PropertyChangeEvent evt) {

    if (wouldEcho())
        return;//from  w ww  . j a va  2 s .  co  m

    this.transactionStarted(TransactionEvent.createStartTransactionEvent(this,
            "Creating start transaction event from propertyChange on object "
                    + evt.getSource().getClass().getSimpleName() + " and property name "
                    + evt.getPropertyName()));

    SPObject source = (SPObject) evt.getSource();
    String uuid = source.getUUID();
    String propertyName = evt.getPropertyName();
    Object oldValue = evt.getOldValue();
    Object newValue = evt.getNewValue();

    PropertyDescriptor propertyDescriptor;
    try {
        propertyDescriptor = PropertyUtils.getPropertyDescriptor(source, propertyName);
    } catch (Exception ex) {
        this.rollback();
        throw new RuntimeException(ex);
    }

    //Not persisting non-settable properties
    if (propertyDescriptor == null || propertyDescriptor.getWriteMethod() == null) {
        this.transactionEnded(TransactionEvent.createEndTransactionEvent(this));
        return;
    }

    for (PropertyToIgnore ignoreProperty : ignoreList) {
        if (ignoreProperty.getPropertyName().equals(propertyName)
                && ignoreProperty.getClassType().isAssignableFrom(source.getClass())) {
            this.transactionEnded(TransactionEvent.createEndTransactionEvent(this));
            return;
        }
    }

    //XXX special case that I want to remove even though I'm implementing it
    List<Object> additionalParams = new ArrayList<Object>();
    if (source instanceof OlapQuery && propertyName.equals("currentCube")) {
        additionalParams.add(((OlapQuery) source).getOlapDataSource());
    }

    DataType typeForClass = PersisterUtils.getDataType(newValue == null ? Void.class : newValue.getClass());
    Object oldBasicType;
    Object newBasicType;
    oldBasicType = converter.convertToBasicType(oldValue, additionalParams.toArray());
    newBasicType = converter.convertToBasicType(newValue, additionalParams.toArray());

    logger.debug("Calling persistProperty on propertyChange");
    this.persistProperty(uuid, propertyName, typeForClass, oldBasicType, newBasicType);

    this.transactionEnded(TransactionEvent.createEndTransactionEvent(this));
}

From source file:edu.ku.brc.af.ui.forms.FormViewObj.java

@Override
public void getDataFromUI() {
    if (isEditing) {
        // This should only happen when the user created a new object
        // in a form in a dialog and then they pressed Cancel without it being saved.
        /*if (isNewlyCreatedDataObj)
        {/*from w  ww .  j  ava2s . c o  m*/
        if (parentDataObj instanceof FormDataObjIFace)
        {
            ((FormDataObjIFace)parentDataObj).addReference((FormDataObjIFace)dataObj, cellName);
                    
        } else
        {
            FormHelper.addToParent(parentDataObj, dataObj);
        }
        isNewlyCreatedDataObj = false;
        }*/

        if (formValidator != null && formValidator.getState() != UIValidatable.ErrorType.Valid) {
            if (isNewlyCreatedDataObj) {
                if (list != null) {
                    list.remove(dataObj);
                }

                if (origDataSet != null) {
                    origDataSet.remove(dataObj);
                }
                formValidator.setFormValidationState(UIValidatable.ErrorType.Valid);
                formValidator.reset(true);
                formValidator.validateRoot();
            }
            return;
        }

        DataObjectSettable ds = formViewDef.getDataSettable();
        DataObjectGettable dg = formViewDef.getDataGettable();
        if (ds != null) {

            // Get Data From Selector
            if (selectorCBX != null) {
                String selectorName = altView.getSelectorName();
                if (StringUtils.isNotEmpty(selectorName)) {
                    try {
                        PropertyDescriptor descr = PropertyUtils.getPropertyDescriptor(dataObj, selectorName);
                        Object selectorValObj = UIHelper.convertDataFromString(altView.getSelectorValue(),
                                descr.getPropertyType());

                        FormHelper.setFieldValue(selectorName, dataObj, selectorValObj, dg, ds);

                    } catch (Exception ex) {
                        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
                        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(FormViewObj.class, ex);
                        log.error(ex);
                        // XXX TODO Show error dialog here
                    }
                }
            }

            for (FVOFieldInfo fieldInfo : controlsById.values()) {
                //String nm = fieldInfo.getFormCell().getName();
                //System.out.println(nm);

                FormCellIFace fc = fieldInfo.getFormCell();
                boolean isInoreGetSet = fc.isIgnoreSetGet();
                boolean isReadOnly;
                boolean useThisData; // meaning the control is using the same data object as what is in the form
                                     // so we don't need to go get the data (skip it)
                if (fc instanceof FormCellField) {
                    FormCellFieldIFace fcf = (FormCellFieldIFace) fc;
                    isReadOnly = fcf.isReadOnly();// || fcf.isEditOnCreate();
                    useThisData = fcf.useThisData();

                } else {
                    useThisData = false;
                    isReadOnly = false;
                }

                String id = fieldInfo.getFormCell().getIdent();
                boolean hasFormControlChanged = hasFormControlChanged(id);
                //log.debug(fieldInfo.getName()+"\t"+fieldInfo.getFormCell().getName()+"\t   hasChanged: "+(!isReadOnly && hasFormControlChanged));

                if (!isReadOnly && !isInoreGetSet && (hasFormControlChanged || isAlwaysGetDataFromUI)) {
                    // this ends up calling the getData on the GetSetValueIFace 
                    // which enables the control to set data into the data object
                    if (useThisData) {
                        getDataFromUIComp(id);
                        continue;
                    }

                    boolean isSubView = fieldInfo.getFormCell() instanceof FormCellSubViewIFace;
                    if (isSubView && fieldInfo.getComp() instanceof MultiView) {
                        MultiView mv = (MultiView) fieldInfo.getComp();
                        mv.getDataFromUI();
                    }

                    //log.debug(fieldInfo.getName()+"  "+fieldInfo.getFormCell().getName() +"  HAS CHANGED!");
                    Object uiData = getDataFromUIComp(id); // if ID is null then we have huge problems
                    // if (uiData != null && dataObj != null) Changed for Bug 4994
                    if (dataObj != null) {
                        if (isSubView) {
                            log.debug(fieldInfo.getFormCell().getName());
                            if (uiData != null) {
                                FormHelper.setFieldValue(fieldInfo.getFormCell().getName(), dataObj, uiData, dg,
                                        ds);
                            }
                        } else {
                            //log.info(fieldInfo.getFormCell().getName()+" "+(dataObj != null ? dataObj.getClass().getSimpleName() : "dataObj was null"));
                            FormHelper.setFieldValue(fieldInfo.getFormCell().getName(), dataObj, uiData, dg,
                                    ds);
                        }
                    }
                }
            }
        } else {
            throw new RuntimeException(
                    "Calling getDataFromUI when the DataObjectSettable is null for the form.");
        }
    }
}

From source file:nl.nn.adapterframework.configuration.AbstractSpringPoweredDigesterFactory.java

protected void checkAttribute(Object currObj, String beanName, String attributeName, String value,
        Map<String, String> attrs) throws Exception {
    PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(currObj, attributeName);
    if (pd != null) {
        Method rm = PropertyUtils.getReadMethod(pd);
        if (rm != null) {
            try {
                Object dv = rm.invoke(currObj, new Object[0]);
                if (currObj instanceof HasSpecialDefaultValues) {
                    dv = ((HasSpecialDefaultValues) currObj).getSpecialDefaultValue(attributeName, dv, attrs);
                }//from   ww  w.  j a  va2  s.c  o  m
                if (dv != null) {
                    if (dv instanceof String) {
                        if (value.equals(dv)) {
                            addSetToDefaultConfigWarning(currObj, beanName, attributeName, value);
                        }
                    } else {
                        if (value.length() == 0) {
                            addConfigWarning(currObj, beanName, "attribute [" + attributeName + "] with type ["
                                    + dv.getClass().getName() + "] has no value");
                        } else {
                            if (dv instanceof Boolean) {
                                if (Boolean.valueOf(value).equals(dv)) {
                                    addSetToDefaultConfigWarning(currObj, beanName, attributeName, value);
                                }
                            } else {
                                if (dv instanceof Integer) {
                                    try {
                                        if (Integer.valueOf(value).equals(dv)) {
                                            addSetToDefaultConfigWarning(currObj, beanName, attributeName,
                                                    value);
                                        }
                                    } catch (NumberFormatException e) {
                                        addConfigWarning(currObj, beanName,
                                                "attribute [" + attributeName + "] String [" + value
                                                        + "] cannot be converted to Integer: "
                                                        + e.getMessage());
                                    }
                                } else {
                                    if (dv instanceof Long) {
                                        try {
                                            if (Long.valueOf(value).equals(dv)) {
                                                addSetToDefaultConfigWarning(currObj, beanName, attributeName,
                                                        value);
                                            }
                                        } catch (NumberFormatException e) {
                                            addConfigWarning(currObj, beanName,
                                                    "attribute [" + attributeName + "] String [" + value
                                                            + "] cannot be converted to Long: "
                                                            + e.getMessage());
                                        }
                                    } else {
                                        log.warn("Unknown returning type [" + rm.getReturnType()
                                                + "] for getter method [" + rm.getName() + "], object ["
                                                + getObjectName(currObj, beanName) + "]");
                                    }
                                }
                            }
                        }
                    }
                }
            } catch (Throwable t) {
                log.warn("Error on getting default for object [" + getObjectName(currObj, beanName)
                        + "] with method [" + rm.getName() + "]", t);
            }
        }
    }
}