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.
*
* @param <T> the generic type
* @param <S> the generic type
* @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
Set<S> elements = new HashSet<>();
if (references != null) {
references.stream().filter(e -> e.getId() != null).forEach(e -> elements.add(e));
}
StringBuilder sb = new StringBuilder("delete from "); protected <T extends AbstractIdEntity> void synchronizeCollections(Collection<T> currentCollection, Collection<T> newCollection, BiConsumer<T, T> modifyFn, Consumer<T> modifyRelationship, boolean deleteOrphans) {
sb.append(referencedClass).append(" ref"); Objects.requireNonNull(currentCollection);
sb.append(" where ref.").append(referencedField).append(" = :root"); Objects.requireNonNull(newCollection);
Objects.requireNonNull(modifyFn);
if (!elements.isEmpty()) { List<T> toBeAdded = new ArrayList<>();
sb.append(" and ref not in (:refs)"); Map<Long, T> map = new HashMap<>();
} if (newCollection != null) {
for (T nc : newCollection) {
Query q = entityManager.createQuery(sb.toString()); if (nc.getId() == null) {
q.setParameter("root", entity); toBeAdded.add(nc);
if (!elements.isEmpty()) { } else {
q.setParameter("refs", elements); map.put(nc.getId(), nc);
}
q.executeUpdate();
if (references != null) {
for (S s : references) {
save(s);
} }
} }
} }
Map<Long, T> newMap = map;
/** Iterator<T> it = currentCollection.iterator();
* Sync references. while (it.hasNext()) {
* T t = it.next();
* @param <T> the generic type T nc = newMap.get(t.getId());
* @param <S> the generic type if (nc == null) {
* @param entities the entities it.remove();
* @param referencedClass the referenced class if (deleteOrphans) {
* @param referencedField the referenced field delete(t);
* @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) {
if (entity.getId() == null) {
continue; continue;
} }
syncReferences(entity, referencedClass, referencedField, valueProviders.apply(entity));
modifyFn.accept(t, nc);
} }
for (T tba : toBeAdded) {
save(tba);
} }
}
} }
...@@ -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