Example usage for org.springframework.data.mapping PersistentProperty getGetter

List of usage examples for org.springframework.data.mapping PersistentProperty getGetter

Introduction

In this page you can find the example usage for org.springframework.data.mapping PersistentProperty getGetter.

Prototype

@Nullable
Method getGetter();

Source Link

Document

Returns the getter method to access the property value if available.

Usage

From source file:org.lightadmin.core.web.ApplicationController.java

private Object cloneEntityOfDomain(String entityId, String domainTypeName) {
    DomainTypeAdministrationConfiguration domainTypeConfiguration = configuration.forEntityName(domainTypeName);
    DynamicJpaRepository repository = domainTypeConfiguration.getRepository();

    PersistentEntity persistentEntity = domainTypeConfiguration.getPersistentEntity();
    Serializable id = (Serializable) conversionService.convert(entityId,
            persistentEntity.getIdProperty().getActualType());

    Object found = repository.findOne(id);
    if (found != null) {
        try {//from   w  ww  .  ja  va2 s. com
            Object newInstance = null;
            if (found instanceof CloneableEntity) {
                newInstance = CloneableEntity.class.cast(found).clone();
            } else {
                newInstance = domainTypeConfiguration.getDomainType().newInstance();

                BeanUtils.copyProperties(found, newInstance, persistentEntity.getIdProperty().getName());

                PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(newInstance);

                for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {

                    if (propertyDescriptor.getReadMethod() != null
                            && propertyDescriptor.getWriteMethod() != null) {
                        Object value = propertyDescriptor.getReadMethod().invoke(newInstance);

                        Object newValue = null;
                        try {
                            if (value instanceof SortedSet) {
                                newValue = new TreeSet(SortedSet.class.cast(value));
                            } else if (value instanceof Set) {
                                newValue = new HashSet(Set.class.cast(value));
                            } else if (value instanceof SortedMap) {
                                newValue = new TreeMap(SortedMap.class.cast(value));
                            } else if (value instanceof Collection) {
                                newValue = new ArrayList(Collection.class.cast(value));
                            } else if (value instanceof Map) {
                                newValue = new HashMap(Map.class.cast(value));
                            }
                        } catch (Throwable t) {
                            if (logger.isWarnEnabled()) {
                                logger.warn("Can't clone " + propertyDescriptor.getName(), t);
                            }
                        }

                        if (newValue != null) {
                            propertyDescriptor.getWriteMethod().invoke(newInstance, newValue);
                        }
                    }

                }
            }

            Object saved = repository.saveAndFlush(newInstance);

            PersistentProperty idProperty = persistentEntity.getIdProperty();
            Field idField = idProperty.getField();
            idField.setAccessible(true);
            return idProperty.usePropertyAccess() ? idProperty.getGetter().invoke(saved) : idField.get(saved);
        } catch (Throwable t) {
            throw new RuntimeException(t);
        }
    } else {
        return null;
    }
}