Commit 7f98b693 by Jan Hrabal

attachments

parent 6b5784b6
package com.jh.boot.attachment; package com.jh.boot.attachment;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
...@@ -60,5 +65,47 @@ public interface AttachmentService { ...@@ -60,5 +65,47 @@ public interface AttachmentService {
} }
public default <C extends Collection<Attachment>> C syncAttachments(String objectType, Long objectId, C currentAttachments, C newAttachments, MultipartFile[] files, Class<C> clazz) {
return syncAttachments(null, objectType, objectId, currentAttachments, newAttachments, files, clazz);
}
public default <C extends Collection<Attachment>> C syncAttachments(Long unitId, String objectType, Long objectId, C currentAttachments, C newAttachments, MultipartFile[] files, Class<C> clazz) {
//decide whether Set or List
if (currentAttachments == null) {
if (Set.class.isAssignableFrom(clazz)) {
currentAttachments = (C) new HashSet<Attachment>();
} else if (List.class.isAssignableFrom(clazz)) {
currentAttachments = (C) new ArrayList<Attachment>();
} else {
throw new IllegalArgumentException("Invalid collection type");
}
}
if (newAttachments == null || newAttachments.isEmpty()) {
currentAttachments.clear();
} else {
Iterator<Attachment> it = currentAttachments.iterator();
while (it.hasNext()) {
Attachment a = it.next();
if (!newAttachments.contains(a)) {
it.remove();
}
}
}
//TODO add attachments?
if (files != null && files.length > 0) {
for (MultipartFile f : files) {
currentAttachments.add(addAttachment(unitId, objectType, objectId, f));
}
}
return currentAttachments;
}
} }
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