Example usage for org.springframework.beans PropertyAccessor getPropertyValue

List of usage examples for org.springframework.beans PropertyAccessor getPropertyValue

Introduction

In this page you can find the example usage for org.springframework.beans PropertyAccessor getPropertyValue.

Prototype

@Nullable
Object getPropertyValue(String propertyName) throws BeansException;

Source Link

Document

Get the current value of the specified property.

Usage

From source file:org.uimafit.spring.util.ResourceInitializationUtil.java

/**
 * Handle Spring-initialization of resoures produced by the UIMA framework.
 */// ww  w. j ava2s.com
public static Resource initResource(Resource aResource, ApplicationContext aApplicationContext) {
    AutowireCapableBeanFactory beanFactory = aApplicationContext.getAutowireCapableBeanFactory();

    if (aResource instanceof PrimitiveAnalysisEngine_impl) {
        PropertyAccessor pa = PropertyAccessorFactory.forDirectFieldAccess(aResource);

        // Access the actual AnalysisComponent and initialize it
        AnalysisComponent analysisComponent = (AnalysisComponent) pa.getPropertyValue("mAnalysisComponent");
        initializeBean(beanFactory, analysisComponent, aResource.getMetaData().getName());
        pa.setPropertyValue("mAnalysisComponent", analysisComponent);

        return aResource;
    } else {
        return (Resource) beanFactory.initializeBean(aResource, aResource.getMetaData().getName());
    }
}

From source file:org.jdal.dao.jpa.JpaUtils.java

/**
 * Initialize collection// w  w  w . ja v  a 2  s  .c  om
 * @param em
 * @param entity
 * @param a
 * @param i
 */
@SuppressWarnings("rawtypes")
private static void intializeCollection(EntityManager em, Object entity, Object attached, Attribute a,
        int depth) {
    PropertyAccessor accessor = PropertyAccessorFactory.forDirectFieldAccess(attached);
    Collection c = (Collection) accessor.getPropertyValue(a.getName());

    for (Object o : c)
        initialize(em, o, depth - 1);

    PropertyAccessorFactory.forDirectFieldAccess(entity).setPropertyValue(a.getName(), c);
}

From source file:ru.mystamps.web.validation.jsr303.NotNullIfFirstFieldValidator.java

@Override
public boolean isValid(Object value, ConstraintValidatorContext ctx) {

    if (value == null) {
        return true;
    }//w  w  w. j  av  a 2 s. c o  m

    PropertyAccessor bean = new BeanWrapperImpl(value);

    Object firstField = bean.getPropertyValue(firstFieldName);
    String firstFieldValue = Objects.toString(firstField, "");
    if (StringUtils.isEmpty(firstFieldValue)) {
        return true;
    }

    Object secondField = bean.getPropertyValue(secondFieldName);
    String secondFieldValue = Objects.toString(secondField, "");
    if (StringUtils.isNotEmpty(secondFieldValue)) {
        return true;
    }

    // bind error message to 2nd field
    ConstraintViolationUtils.recreate(ctx, secondFieldName, ctx.getDefaultConstraintMessageTemplate());

    return false;
}

From source file:ru.mystamps.web.validation.jsr303.FieldsMatchValidator.java

@Override
public boolean isValid(Object value, ConstraintValidatorContext ctx) {

    if (value == null) {
        return true;
    }// w ww. j  a  v  a  2s.  c om

    PropertyAccessor bean = new BeanWrapperImpl(value);

    Object firstField = bean.getPropertyValue(firstFieldName);
    String firstFieldValue = Objects.toString(firstField, "");
    if (StringUtils.isEmpty(firstFieldValue)) {
        return true;
    }

    Object secondField = bean.getPropertyValue(secondFieldName);
    String secondFieldValue = Objects.toString(secondField, "");
    if (StringUtils.isEmpty(secondFieldValue)) {
        return true;
    }

    // TODO: check fields only when both fields are equals

    if (!firstFieldValue.equals(secondFieldValue)) {
        // bind error message to 2nd field
        ConstraintViolationUtils.recreate(ctx, secondFieldName, ctx.getDefaultConstraintMessageTemplate());
        return false;
    }

    return true;
}

From source file:ru.mystamps.web.validation.jsr303.FieldsMismatchValidator.java

@Override
public boolean isValid(Object value, ConstraintValidatorContext ctx) {

    if (value == null) {
        return true;
    }// w  ww  .j a v a 2s .  c o  m

    PropertyAccessor bean = new BeanWrapperImpl(value);

    Object firstField = bean.getPropertyValue(firstFieldName);
    String firstFieldValue = Objects.toString(firstField, "");
    if (StringUtils.isEmpty(firstFieldValue)) {
        return true;
    }

    Object secondField = bean.getPropertyValue(secondFieldName);
    String secondFieldValue = Objects.toString(secondField, "");
    if (StringUtils.isEmpty(secondFieldValue)) {
        return true;
    }

    // TODO: check fields only when both fields are equals

    if (firstFieldValue.equals(secondFieldValue)) {
        // bind error message to 2nd field
        ConstraintViolationUtils.recreate(ctx, secondFieldName, ctx.getDefaultConstraintMessageTemplate());
        return false;
    }

    return true;
}

From source file:io.syndesis.dao.validation.UniquePropertyValidator.java

@Override
public boolean isValid(final WithId<?> value, final ConstraintValidatorContext context) {
    if (value == null) {
        return true;
    }//ww w .  java 2  s  .co m

    final PropertyAccessor bean = new BeanWrapperImpl(value);

    final String propertyValue = String.valueOf(bean.getPropertyValue(property));

    @SuppressWarnings({ "rawtypes", "unchecked" })
    final Class<WithId> modelClass = (Class) value.getKind().modelClass;

    @SuppressWarnings("unchecked")
    final Set<String> ids = dataManager.fetchIdsByPropertyValue(modelClass, property, propertyValue);

    final boolean isUnique = ids.isEmpty() || value.getId().map(id -> ids.contains(id)).orElse(false);

    if (!isUnique) {
        if (ids.stream().allMatch(id -> consideredValidByException(modelClass, id))) {
            return true;
        }

        context.disableDefaultConstraintViolation();
        context.unwrap(HibernateConstraintValidatorContext.class)
                .addExpressionVariable("nonUnique", propertyValue)
                .buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate())
                .addPropertyNode(property).addConstraintViolation();
    }

    return isUnique;
}

From source file:net.solarnetwork.central.dras.domain.CapableObject.java

@Override
@SerializeIgnore//from ww w .j  a  va  2s .co  m
public String getName() {
    if (object == null) {
        return null;
    }
    PropertyAccessor bean = PropertyAccessorFactory.forBeanPropertyAccess(object);
    if (bean.isReadableProperty("name")) {
        Object o = bean.getPropertyValue("name");
        if (o != null) {
            return o.toString();
        }
    }
    return null;
}

From source file:net.solarnetwork.node.rfxcom.RFXCOMTransceiver.java

public void updateModeSetting(String name, Object value) {
    if (this.status == null) {
        updateStatus();/* w w w  .j  a  v  a2  s.  c  o m*/
    }
    if (this.status != null) {
        SetModeMessage msg = new SetModeMessage(mf.incrementAndGetSequenceNumber(), this.status);
        PropertyAccessor bean = PropertyAccessorFactory.forBeanPropertyAccess(msg);
        Object currValue = bean.getPropertyValue(name);
        if (value != null && !value.equals(currValue)) {
            bean.setPropertyValue(name, value);
            setMode(msg);
        }
    }
}

From source file:net.solarnetwork.node.rfxcom.RFXCOMTransceiver.java

private void addToggleSetting(List<SettingSpecifier> results, PropertyAccessor bean, String name) {
    results.add(new BasicToggleSettingSpecifier(name,
            (bean == null ? Boolean.FALSE : bean.getPropertyValue(name)), true));
}

From source file:org.jdal.dao.jpa.JpaDao.java

/**
 * Test if entity is New/*  w w w  .j  a va 2 s.  c  om*/
 * @param entity
 * @return true if entity is new, ie not detached
 */
@SuppressWarnings("unchecked")
protected boolean isNew(T entity) {
    SingularAttribute<?, ?> id = getIdAttribute(entity.getClass());
    // try field
    PropertyAccessor pa = PropertyAccessorFactory.forDirectFieldAccess(entity);
    PK key = (PK) pa.getPropertyValue(id.getName());
    if (key == null)
        key = (PK) PropertyAccessorFactory.forBeanPropertyAccess(entity).getPropertyValue(id.getName());

    return key == null || !exists(key, entity.getClass());
}