Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -670,9 +670,9 @@ private void writeProperty(final Object source, final VPackBuilder sink, final A

if (property.getRef().isPresent()) {
if (sourceType.isCollectionLike()) {
writeReferences(fieldName, source, sink);
writeReferences(fieldName, source, sink,property.getRef().get());
} else {
writeReference(fieldName, source, sink);
writeReference(fieldName, source, sink,property.getRef().get());
}
}

Expand All @@ -682,7 +682,7 @@ else if (property.getRelations().isPresent()) {

else if (property.getFrom().isPresent() || property.getTo().isPresent()) {
if (!sourceType.isCollectionLike()) {
writeReference(fieldName, source, sink);
writeReference(fieldName, source, sink, null);
}
}

Expand Down Expand Up @@ -745,27 +745,27 @@ private void writeArray(
}
}

private void writeReferences(final String attribute, final Object source, final VPackBuilder sink) {
private void writeReferences(final String attribute, final Object source, final VPackBuilder sink, final Ref annotation) {
sink.add(attribute, ValueType.ARRAY);

if (source.getClass().isArray()) {
for (int i = 0; i < Array.getLength(source); ++i) {
final Object element = Array.get(source, i);
writeReference(null, element, sink);
writeReference(null, element, sink,annotation);
}
}

else {
for (final Object element : asCollection(source)) {
writeReference(null, element, sink);
writeReference(null, element, sink,annotation);
}
}

sink.close();
}

private void writeReference(final String attribute, final Object source, final VPackBuilder sink) {
getRefId(source).ifPresent(id -> sink.add(attribute, id));
private void writeReference(final String attribute, final Object source, final VPackBuilder sink, final Ref annotation) {
getRefId(source, annotation).ifPresent(id -> sink.add(attribute, id));
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -880,18 +880,24 @@ private void writeBaseEdgeDocument(
sink.add(attribute, builder.slice());
}

private Optional<String> getRefId(final Object source) {
return getRefId(source, context.getPersistentEntity(source.getClass()));
private Optional<String> getRefId(final Object source, final Ref annotation) {
return getRefId(source, context.getPersistentEntity(source.getClass()),annotation);
}

private Optional<String> getRefId(final Object source, final ArangoPersistentEntity<?> entity) {
private Optional<String> getRefId(final Object source, final ArangoPersistentEntity<?> entity, final Ref annotation) {
if (source instanceof LazyLoadingProxy) {
return Optional.of(((LazyLoadingProxy) source).getRefId());
}

final Optional<Object> id = Optional.ofNullable(entity.getIdentifierAccessor(source).getIdentifier());
if (id.isPresent()) {
return id.map(key -> MetadataUtils.createIdFromCollectionAndKey(entity.getCollection(), convertId(key)));
if(annotation != null){
final Optional<ReferenceResolver<Annotation>> resolver = resolverFactory.getReferenceResolver(annotation);
return id.map(key -> resolver.get().write(source, entity, convertId(key),annotation));
} else {
return id.map(key -> MetadataUtils.createIdFromCollectionAndKey(entity.getCollection(), convertId(key)));
}

}

return Optional.ofNullable((String) entity.getArangoIdAccessor(source).getIdentifier());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.Collection;
import java.util.stream.Collectors;

import com.arangodb.springframework.core.mapping.ArangoPersistentEntity;
import com.arangodb.springframework.core.util.MetadataUtils;
import org.springframework.data.util.TypeInformation;

import com.arangodb.springframework.annotation.Ref;
Expand Down Expand Up @@ -59,4 +61,10 @@ public Object resolve(final String id, final TypeInformation<?> type, final Ref
return template.find(id, type.getType()).get();
}

@Override
public String write(final Object source, final ArangoPersistentEntity<?> entity, final String id, final Ref annotation) {
return MetadataUtils.createIdFromCollectionAndKey(entity.getCollection(), id);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.lang.annotation.Annotation;
import java.util.Collection;

import com.arangodb.springframework.annotation.Ref;
import com.arangodb.springframework.core.mapping.ArangoPersistentEntity;
import org.springframework.data.util.TypeInformation;

/**
Expand All @@ -35,4 +37,6 @@ public interface ReferenceResolver<A extends Annotation> {

Object resolveMultiple(Collection<String> ids, TypeInformation<?> type, A annotation);

public String write(Object source, ArangoPersistentEntity<?> entity, String id, Ref annotation);

}