Example usage for org.springframework.data.mapping PersistentEntity getIdProperty

List of usage examples for org.springframework.data.mapping PersistentEntity getIdProperty

Introduction

In this page you can find the example usage for org.springframework.data.mapping PersistentEntity getIdProperty.

Prototype

@Nullable
P getIdProperty();

Source Link

Document

Returns the id property of the PersistentEntity .

Usage

From source file:org.lightadmin.core.util.NamingUtils.java

public static String entityId(DomainTypeAdministrationConfiguration domainTypeAdministrationConfiguration,
        Object entity) {//from   www  .  j  a v a2  s .c o m
    if (entity == null) {
        return null;
    }

    PersistentEntity persistentEntity = domainTypeAdministrationConfiguration.getPersistentEntity();
    PersistentProperty idProperty = persistentEntity.getIdProperty();

    BeanWrapper beanWrapper = new DirectFieldAccessFallbackBeanWrapper(entity);

    return String.valueOf(beanWrapper.getPropertyValue(idProperty.getName()));
}

From source file:org.lightadmin.core.web.support.DomainEntityLinks.java

private Serializable idAttributeValue(Object entity, PersistentEntity persistentEntity) {
    return (Serializable) new DirectFieldAccessFallbackBeanWrapper(entity)
            .getPropertyValue(persistentEntity.getIdProperty().getName());
}

From source file:org.lightadmin.core.web.support.DynamicRepositoryEntityLinks.java

private Serializable idValue(Object instance, PersistentEntity persistentEntity) {
    return (Serializable) new DirectFieldAccessFallbackBeanWrapper(instance)
            .getPropertyValue(persistentEntity.getIdProperty().getName());
}

From source file:org.lightadmin.core.web.support.DynamicPersistentEntityResourceProcessor.java

private String primaryKey(PersistentEntity persistentEntity) {
    return persistentEntity.getIdProperty().getName();
}

From source file:org.lightadmin.core.persistence.support.DynamicDomainObjectMerger.java

private boolean mathesAny(Collection<Object> collection, final Object item) {
    final PersistentEntity<?, ?> persistentEntity = configuration.forManagedDomainType(item.getClass())
            .getPersistentEntity();/*from w  w w  . ja v  a  2 s. com*/
    final PersistentProperty<?> idProperty = persistentEntity.getIdProperty();

    return Iterables.any(collection, new Predicate<Object>() {
        @Override
        public boolean apply(Object object) {
            return itemsEqual(object, item, idProperty);
        }
    });
}

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

private Object findEntityOfDomain(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());

    return repository.findOne(id);
}

From source file:org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.java

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
        NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

    RootResourceInformation resourceInformation = resourceInformationResolver.resolveArgument(parameter,
            mavContainer, webRequest, binderFactory);

    HttpServletRequest nativeRequest = webRequest.getNativeRequest(HttpServletRequest.class);
    ServletServerHttpRequest request = new ServletServerHttpRequest(nativeRequest);
    IncomingRequest incoming = new IncomingRequest(request);

    Class<?> domainType = resourceInformation.getDomainType();
    MediaType contentType = request.getHeaders().getContentType();

    for (HttpMessageConverter converter : messageConverters) {

        if (!converter.canRead(PersistentEntityResource.class, contentType)) {
            continue;
        }//from   www  .j  a v a  2s.c  o m

        Serializable id = idResolver.resolveArgument(parameter, mavContainer, webRequest, binderFactory);
        Object objectToUpdate = getObjectToUpdate(id, resourceInformation);

        boolean forUpdate = false;
        Object entityIdentifier = null;
        PersistentEntity<?, ?> entity = resourceInformation.getPersistentEntity();

        if (objectToUpdate != null) {
            forUpdate = true;
            entityIdentifier = entity.getIdentifierAccessor(objectToUpdate).getIdentifier();
        }

        Object obj = read(resourceInformation, incoming, converter, objectToUpdate);

        if (obj == null) {
            throw new HttpMessageNotReadableException(String.format(ERROR_MESSAGE, domainType));
        }

        if (entityIdentifier != null) {
            entity.getPropertyAccessor(obj).setProperty(entity.getIdProperty(), entityIdentifier);
        }

        Builder build = PersistentEntityResource.build(obj, entity);
        return forUpdate ? build.build() : build.forCreation();
    }

    throw new HttpMessageNotReadableException(String.format(NO_CONVERTER_FOUND, domainType, contentType));
}

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 {/*  ww  w.java 2 s . c om*/
            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;
    }
}