Commit df9deb94 by Jan Hrabal

w

parent c2bde5c5
package com.jh.boot.jpa;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;
import java.util.HashMap;
import java.util.Iterator;
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.NoResultException;
......@@ -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;
}
protected <T extends AbstractIdEntity> void synchronizeCollections(Collection<T> currentCollection, Collection<T> newCollection, BiConsumer<T, T> modifyFn, Consumer<T> modifyRelationship, boolean deleteOrphans) {
Objects.requireNonNull(currentCollection);
Objects.requireNonNull(newCollection);
Objects.requireNonNull(modifyFn);
//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));
List<T> toBeAdded = new ArrayList<>();
Map<Long, T> map = new HashMap<>();
if (newCollection != null) {
for (T nc : newCollection) {
if (nc.getId() == null) {
toBeAdded.add(nc);
} else {
map.put(nc.getId(), nc);
}
}
}
StringBuilder sb = new StringBuilder("delete from ");
sb.append(referencedClass).append(" ref");
sb.append(" where ref.").append(referencedField).append(" = :root");
if (!elements.isEmpty()) {
sb.append(" and ref not in (:refs)");
}
Map<Long, T> newMap = map;
Query q = entityManager.createQuery(sb.toString());
q.setParameter("root", entity);
if (!elements.isEmpty()) {
q.setParameter("refs", elements);
}
Iterator<T> it = currentCollection.iterator();
while (it.hasNext()) {
T t = it.next();
T nc = newMap.get(t.getId());
if (nc == null) {
it.remove();
if (deleteOrphans) {
delete(t);
}
continue;
}
q.executeUpdate();
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;
modifyFn.accept(t, nc);
}
for (T entity : entities) {
if (entity.getId() == null) {
continue;
}
syncReferences(entity, referencedClass, referencedField, valueProviders.apply(entity));
for (T tba : toBeAdded) {
save(tba);
}
}
}
......@@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
......@@ -23,7 +24,14 @@ public class Converter {
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) {
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