Skip to content

Commit

Permalink
la-team#143 Cannot create reference from one object to another
Browse files Browse the repository at this point in the history
  • Loading branch information
max-dev committed Sep 17, 2014
1 parent 193271c commit 43e1e99
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public Repositories repositories() {

@Bean
public DomainObjectMerger domainObjectMerger() throws Exception {
return new DynamicDomainObjectMerger(repositories(), defaultConversionService());
return new DynamicDomainObjectMerger(repositories(), defaultConversionService(), globalAdministrationConfiguration());
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
package org.lightadmin.core.config.domain.configuration.support;

import org.lightadmin.api.config.utils.EntityNameExtractor;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.model.BeanWrapper;
import org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper;

import java.io.Serializable;

Expand Down Expand Up @@ -52,8 +53,8 @@ public PersistentEntityNameExtractor(String entityName, PersistentEntity persist

@Override
public String apply(final Object entity) {
BeanWrapper wrapper = BeanWrapper.create(entity, null);
Object entityId = wrapper.getProperty(persistentEntity.getIdProperty());
BeanWrapper beanWrapper = new DirectFieldAccessFallbackBeanWrapper(entity);
Object entityId = beanWrapper.getPropertyValue(persistentEntity.getIdProperty().getName());

return String.format("%s #%s", entityName, entityId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
import org.lightadmin.api.config.utils.FieldValueRenderer;
import org.lightadmin.core.config.domain.DomainTypeBasicConfiguration;
import org.lightadmin.core.util.Transformer;
import org.springframework.data.mapping.model.BeanWrapper;
import org.springframework.beans.BeanWrapper;
import org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper;

import static java.lang.String.format;

Expand All @@ -31,15 +32,14 @@ private ExceptionAwareTransformer() {
public static Transformer<Object, String> exceptionAwareNameExtractor(final EntityNameExtractor<Object> entityNameExtractor, final DomainTypeBasicConfiguration domainTypeBasicConfiguration) {
return new ExceptionAwareTransformer() {
@Override
public String apply(final Object input) {
public String apply(final Object instance) {
try {
return entityNameExtractor.apply(input);
return entityNameExtractor.apply(instance);
} catch (Exception ex) {
String domainTypeName = domainTypeBasicConfiguration.getDomainTypeName();
BeanWrapper beanWrapper = new DirectFieldAccessFallbackBeanWrapper(instance);

BeanWrapper beanWrapper = BeanWrapper.create(input, null);

Object id = beanWrapper.getProperty(domainTypeBasicConfiguration.getPersistentEntity().getIdProperty());
String domainTypeName = domainTypeBasicConfiguration.getDomainTypeName();
Object id = beanWrapper.getPropertyValue(domainTypeBasicConfiguration.getPersistentEntity().getIdProperty().getName());

return format("%s #%s", domainTypeName, String.valueOf(id));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,36 @@

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import org.lightadmin.core.storage.FileResourceStorage;
import org.lightadmin.core.config.domain.DomainTypeAdministrationConfiguration;
import org.lightadmin.core.config.domain.GlobalAdministrationConfiguration;
import org.lightadmin.core.config.domain.field.PersistentFieldMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanWrapper;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.mapping.*;
import org.springframework.data.mapping.model.BeanWrapper;
import org.springframework.data.repository.support.Repositories;
import org.springframework.data.rest.core.support.DomainObjectMerger;
import org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper;

import java.util.Collection;
import java.util.Iterator;

import static com.google.common.collect.Lists.newArrayList;
import static org.lightadmin.core.config.domain.field.FieldMetadataUtils.getPersistentField;
import static org.springframework.data.rest.core.support.DomainObjectMerger.NullHandlingPolicy.APPLY_NULLS;
import static org.springframework.util.ObjectUtils.nullSafeEquals;

public class DynamicDomainObjectMerger extends DomainObjectMerger {

private static final Logger logger = LoggerFactory.getLogger(DynamicDomainObjectMerger.class);

private final Repositories repositories;
private final ConversionService conversionService;
private final GlobalAdministrationConfiguration configuration;

public DynamicDomainObjectMerger(Repositories repositories, ConversionService conversionService) {
public DynamicDomainObjectMerger(Repositories repositories, ConversionService conversionService, GlobalAdministrationConfiguration configuration) {
super(repositories, conversionService);

this.repositories = repositories;
this.conversionService = conversionService;
this.configuration = configuration;
}

/**
Expand All @@ -60,15 +62,17 @@ public void merge(final Object from, final Object target, final NullHandlingPoli
return;
}

final BeanWrapper<Object> fromWrapper = BeanWrapper.create(from, conversionService);
final BeanWrapper<Object> targetWrapper = BeanWrapper.create(target, conversionService);
final PersistentEntity<?, ?> entity = repositories.getPersistentEntity(target.getClass());
final BeanWrapper fromWrapper = beanWrapper(from);
final BeanWrapper targetWrapper = beanWrapper(target);

final DomainTypeAdministrationConfiguration domainTypeAdministrationConfiguration = configuration.forManagedDomainType(target.getClass());
final PersistentEntity<?, ?> entity = domainTypeAdministrationConfiguration.getPersistentEntity();

entity.doWithProperties(new SimplePropertyHandler() {
@Override
public void doWithPersistentProperty(PersistentProperty<?> persistentProperty) {
Object sourceValue = fromWrapper.getProperty(persistentProperty);
Object targetValue = targetWrapper.getProperty(persistentProperty);
Object sourceValue = fromWrapper.getPropertyValue(persistentProperty.getName());
Object targetValue = targetWrapper.getPropertyValue(persistentProperty.getName());

if (entity.isIdProperty(persistentProperty)) {
return;
Expand All @@ -78,8 +82,12 @@ public void doWithPersistentProperty(PersistentProperty<?> persistentProperty) {
return;
}

if (propertyIsHiddenInFormView(persistentProperty, domainTypeAdministrationConfiguration)) {
return;
}

if (nullPolicy == APPLY_NULLS || sourceValue != null) {
targetWrapper.setProperty(persistentProperty, sourceValue);
targetWrapper.setPropertyValue(persistentProperty.getName(), sourceValue);
}
}
});
Expand All @@ -90,8 +98,16 @@ public void doWithPersistentProperty(PersistentProperty<?> persistentProperty) {
public void doWithAssociation(Association<? extends PersistentProperty<?>> association) {
PersistentProperty<?> persistentProperty = association.getInverse();

Object fromValue = fromWrapper.getProperty(persistentProperty);
Object targetValue = targetWrapper.getProperty(persistentProperty);
Object fromValue = fromWrapper.getPropertyValue(persistentProperty.getName());
Object targetValue = targetWrapper.getPropertyValue(persistentProperty.getName());

if (propertyIsHiddenInFormView(persistentProperty, domainTypeAdministrationConfiguration)) {
return;
}

if ((fromValue == null && nullPolicy == APPLY_NULLS)) {
targetWrapper.setPropertyValue(persistentProperty.getName(), fromValue);
}

if (persistentProperty.isCollectionLike()) {
Collection<Object> sourceCollection = (Collection) fromValue;
Expand All @@ -107,13 +123,19 @@ public void doWithAssociation(Association<? extends PersistentProperty<?>> assoc
return;
}

if ((fromValue == null && nullPolicy == APPLY_NULLS) || !nullSafeEquals(fromValue, targetWrapper.getProperty(persistentProperty))) {
targetWrapper.setProperty(persistentProperty, fromValue);
if (!nullSafeEquals(fromValue, targetWrapper.getPropertyValue(persistentProperty.getName()))) {
targetWrapper.setPropertyValue(persistentProperty.getName(), fromValue);
}
}
});
}

private boolean propertyIsHiddenInFormView(PersistentProperty persistentProperty, DomainTypeAdministrationConfiguration configuration) {
PersistentFieldMetadata persistentField = getPersistentField(configuration.getFormViewFragment().getFields(), persistentProperty.getName());

return persistentField == null;
}

private void addReferencedItems(Collection<Object> targetCollection, Collection<Object> candidatesForAddition) {
for (Object candidateForAddition : candidatesForAddition) {
targetCollection.add(candidateForAddition);
Expand Down Expand Up @@ -151,7 +173,7 @@ private Collection<Object> candidatesForAddition(Collection<Object> sourceCollec
}

private boolean mathesAny(Collection<Object> collection, final Object item) {
final PersistentEntity<?, ?> persistentEntity = repositories.getPersistentEntity(item.getClass());
final PersistentEntity<?, ?> persistentEntity = configuration.forManagedDomainType(item.getClass()).getPersistentEntity();
final PersistentProperty<?> idProperty = persistentEntity.getIdProperty();

return Iterables.any(collection, new Predicate<Object>() {
Expand All @@ -167,9 +189,13 @@ private boolean itemsEqual(Object item1, Object item2, final PersistentProperty<
return true;
}

String sourceItemIdValue = BeanWrapper.create(item1, null).getProperty(idProperty).toString();
String itemIdValue = BeanWrapper.create(item2, null).getProperty(idProperty).toString();
Object sourceItemIdValue = beanWrapper(item1).getPropertyValue(idProperty.getName());
Object itemIdValue = beanWrapper(item2).getPropertyValue(idProperty.getName());

return nullSafeEquals(itemIdValue, sourceItemIdValue);
}

private DirectFieldAccessFallbackBeanWrapper beanWrapper(Object item1) {
return new DirectFieldAccessFallbackBeanWrapper(item1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import org.springframework.data.mapping.SimplePropertyHandler;

/**
* TODO: Document me!
*
* @author Maxim Kharchenko ([email protected])
*/
* TODO: Document me!
*
* @author Maxim Kharchenko ([email protected])
*/
public abstract class FileReferencePropertyHandler implements SimplePropertyHandler {
@Override
public final void doWithPersistentProperty(PersistentProperty<?> property) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

import org.lightadmin.api.config.utils.EntityNameExtractor;
import org.lightadmin.core.config.domain.DomainTypeAdministrationConfiguration;
import org.springframework.beans.BeanWrapper;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.model.BeanWrapper;
import org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper;

import static org.apache.commons.lang3.StringUtils.trim;
import static org.lightadmin.core.config.domain.configuration.support.ExceptionAwareTransformer.exceptionAwareNameExtractor;
Expand All @@ -41,7 +42,9 @@ public static String entityId(DomainTypeAdministrationConfiguration domainTypeAd
PersistentEntity persistentEntity = domainTypeAdministrationConfiguration.getPersistentEntity();
PersistentProperty idProperty = persistentEntity.getIdProperty();

return String.valueOf(BeanWrapper.create(entity, null).getProperty(idProperty));
BeanWrapper beanWrapper = new DirectFieldAccessFallbackBeanWrapper(entity);

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

public static String cutLongText(String text) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
import org.lightadmin.core.config.domain.GlobalAdministrationConfiguration;
import org.lightadmin.core.util.Pair;
import org.lightadmin.core.view.tags.AbstractAutowiredTag;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.model.BeanWrapper;
import org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper;
import org.springframework.util.Assert;

import javax.servlet.jsp.JspContext;
Expand Down Expand Up @@ -63,7 +64,9 @@ public void doTag() throws JspException, IOException {
JspContext jspContext = getJspContext();
JspFragment tagBody = getJspBody();
for (Object element : allElements) {
jspContext.setAttribute(idVar, BeanWrapper.create(element, null).getProperty(idAttribute));
BeanWrapper beanWrapper = new DirectFieldAccessFallbackBeanWrapper(element);

jspContext.setAttribute(idVar, beanWrapper.getPropertyValue(idAttribute.getName()));
jspContext.setAttribute(stringRepresentationVar, exceptionAwareNameExtractor(nameExtractor, domainTypeConfiguration).apply(element));
tagBody.invoke(null);
}
Expand Down

0 comments on commit 43e1e99

Please sign in to comment.