Commit 49565093 by Jan Hrabal

syncs

parent df9deb94
......@@ -45,6 +45,7 @@ public abstract class AbstractHibernateAttachmentService implements AttachmentSe
}
}
protected abstract void deleteData(Attachment attachment);
......
......@@ -3,6 +3,8 @@ package com.jh.boot.attachment;
import java.io.InputStream;
import java.util.List;
import org.springframework.web.multipart.MultipartFile;
public interface AttachmentService {
Attachment saveAttachment(Attachment attachment, InputStream content);
......@@ -17,4 +19,46 @@ public interface AttachmentService {
void deleteAttachment(Long attachmentId);
// default methods
public default Attachment addAttachment(String objectType, Long objectId, MultipartFile file) {
return addAttachment(null, objectType, objectId, file);
}
public default Attachment addAttachment(Long unitId, String objectType, Long objectId, MultipartFile file) {
Attachment a = new Attachment();
a.setFilename(file.getOriginalFilename());
a.setSize(file.getSize());
a.setObjectId(objectId);
a.setObjectType(objectType);
try {
saveAttachment(a, file.getInputStream());
} catch (Exception e) {
throw new RuntimeException("Cannot upload attachment", e);
}
return a;
}
public default Attachment syncAttachment(String objectType, Long objectId, Attachment currentAttachment, Attachment newAttachment, MultipartFile file) {
return syncAttachment(null, objectType, objectId, currentAttachment, newAttachment, file);
}
public default Attachment syncAttachment(Long unitId, String objectType, Long objectId, Attachment currentAttachment, Attachment newAttachment, MultipartFile file) {
Attachment a = null;
if (currentAttachment != null && newAttachment == null) {
//delete current attachment
deleteAttachment(currentAttachment.getId());
}
if (file != null) {
a = addAttachment(unitId, objectType, objectId, file);
} else {
a = currentAttachment;
}
return a;
}
}
package com.jh.boot.jpa;
import java.util.*;
//TODO move to jh-boot
public abstract class CollectionSync<R extends AbstractIdEntity, T extends AbstractIdEntity> {
private AbstractHibernateRepository repo;
private boolean deleteOrphans;
protected CollectionSync(AbstractHibernateRepository repo) {
this(repo, false);
}
protected CollectionSync(AbstractHibernateRepository repo, boolean deleteOrphans) {
this.repo = repo;
this.deleteOrphans = deleteOrphans;
}
public <C extends Collection<T>> C synchronize(R root, C existing, Collection<T> fresh, Class<C> clazz) {
//decide whether Set or List
if (existing == null) {
if (Set.class.isAssignableFrom(clazz)) {
existing = (C) new HashSet<T>();
} else if (List.class.isAssignableFrom(clazz)) {
existing = (C) new ArrayList<T>();
} else {
throw new IllegalArgumentException("Invalid collection type");
}
}
List<T> toBeAdded = new ArrayList<>();
Map<Long, T> map = new HashMap();
if (fresh != null) {
for (T t : fresh) {
if (t.getId() == null) {
toBeAdded.add(t);
} else {
map.put(t.getId(), t);
}
}
}
Iterator<T> it = existing.iterator();
while (it.hasNext()) {
T e = it.next();
T f = map.get(e.getId());
if (f == null) {
it.remove();
doRemove(e);
continue;
}
doMerge(e, f);
}
for (T t : toBeAdded) {
setRootToEntity(root, t);
doPersistNew(t);
existing.add(t);
}
return existing;
}
protected void doRemove(T entity) {
if (deleteOrphans) {
repo.delete(entity);
}
}
protected void doPersistNew(T entity) {
repo.save(entity);
}
protected abstract void setRootToEntity(R root, T entity);
protected abstract void doMerge(T existing, T fresh);
protected boolean isDeleteOrphans() {
return deleteOrphans;
}
protected AbstractHibernateRepository getRepo() {
return repo;
}
}
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