Commit df9deb94 by Jan Hrabal

w

parent c2bde5c5
package com.jh.boot.jpa; package com.jh.boot.jpa;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashMap;
import java.util.Set; import java.util.Iterator;
import java.util.function.Function; import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.NoResultException; import javax.persistence.NoResultException;
...@@ -84,75 +89,45 @@ public abstract class AbstractHibernateRepository { ...@@ -84,75 +89,45 @@ public abstract class AbstractHibernateRepository {
} }
} }
/**
* Sync references. protected <T extends AbstractIdEntity> void synchronizeCollections(Collection<T> currentCollection, Collection<T> newCollection, BiConsumer<T, T> modifyFn, Consumer<T> modifyRelationship, boolean deleteOrphans) {
* Objects.requireNonNull(currentCollection);
* @param <T> the generic type Objects.requireNonNull(newCollection);
* @param <S> the generic type Objects.requireNonNull(modifyFn);
* @param entity the entity
* @param referencedClass the referenced class
* @param referencedField the referenced field
* @param references the references
*/
//FIXME??
protected <T extends AbstractIdEntity, S extends AbstractIdEntity> void syncReferences(T entity, String referencedClass, String referencedField, Collection<S> references) {
if (entity.getId() == null) {
//nothing to do
return;
}
//filter only already stored references List<T> toBeAdded = new ArrayList<>();
Set<S> elements = new HashSet<>(); Map<Long, T> map = new HashMap<>();
if (references != null) { if (newCollection != null) {
references.stream().filter(e -> e.getId() != null).forEach(e -> elements.add(e)); for (T nc : newCollection) {
if (nc.getId() == null) {
toBeAdded.add(nc);
} else {
map.put(nc.getId(), nc);
}
}
} }
StringBuilder sb = new StringBuilder("delete from "); Map<Long, T> newMap = map;
sb.append(referencedClass).append(" ref");
sb.append(" where ref.").append(referencedField).append(" = :root");
if (!elements.isEmpty()) {
sb.append(" and ref not in (:refs)");
}
Query q = entityManager.createQuery(sb.toString()); Iterator<T> it = currentCollection.iterator();
q.setParameter("root", entity); while (it.hasNext()) {
if (!elements.isEmpty()) { T t = it.next();
q.setParameter("refs", elements); T nc = newMap.get(t.getId());
} if (nc == null) {
it.remove();
if (deleteOrphans) {
delete(t);
}
continue;
}
q.executeUpdate(); modifyFn.accept(t, nc);
if (references != null) {
for (S s : references) {
save(s);
}
}
}
/**
* Sync references.
*
* @param <T> the generic type
* @param <S> the generic type
* @param entities the entities
* @param referencedClass the referenced class
* @param referencedField the referenced field
* @param valueProviders the value providers
*/
protected <T extends AbstractIdEntity, S extends AbstractIdEntity> void syncReferences(Collection<T> entities, String referencedClass, String referencedField, Function<T, ? extends Collection<S>> valueProviders) {
if (entities == null || entities.isEmpty()) {
//nothing to do
return;
} }
for (T entity : entities) { for (T tba : toBeAdded) {
if (entity.getId() == null) { save(tba);
continue;
}
syncReferences(entity, referencedClass, referencedField, valueProviders.apply(entity));
} }
} }
} }
...@@ -4,6 +4,7 @@ import java.util.ArrayList; ...@@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -23,7 +24,14 @@ public class Converter { ...@@ -23,7 +24,14 @@ public class Converter {
return mappingFunction.apply(source); return mappingFunction.apply(source);
} }
public static <S, D> List<D> convertList(List<S> list, Function<S, D> mappingFunction) { public static <S, D> Set<D> convertSet(Collection<S> list, Function<S, D> mappingFunction) {
if (list == null) {
return null;
}
return list.stream().map(mappingFunction).collect(Collectors.toSet());
}
public static <S, D> List<D> convertList(Collection<S> list, Function<S, D> mappingFunction) {
if (list == null) { if (list == null) {
return null; return null;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment