Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
fixes #2 - extends PaperclipRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
danhaywood committed Nov 11, 2016
1 parent 958a14d commit cd76579
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ To use "out-of-the-box":
<dependency>
<groupId>org.incode.module.document</groupId>
<artifactId>incode-module-document-dom</artifactId>
<version>1.13.5</version>
<version>1.13.6</version>
</dependency>
----

Expand Down Expand Up @@ -476,7 +476,8 @@ public class EmailHeader {

== Change Log

* `1.13.5` - released against Isis 1.13.0. This release is *not* backward compatible with the previous release.
* `1.13.6` - released against Isis 1.13.0. Fixes https://github.com/incodehq/incode-module-document/issues/2[#2]
* `1.13.5` - released against Isis 1.13.0. Fixes https://github.com/incodehq/incode-module-document/issues/1[#1], with various additional extensions to functionality. NB: this release is *not* backward compatible with the previous release.
* `1.13.0` - released against Isis 1.13.0


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,44 @@ public void delete(final Paperclip paperclip) {
repositoryService.remove(paperclip);
}

public enum Policy {
/**
* Delete the paperclips
*/
PAPERCLIPS_ONLY,
/**
* Delete the paperclips, and also delete the documents if they are no longer attached to any objects
*/
PAPERCLIPS_AND_DOCUMENTS_IF_ORPHANED
}

@Programmatic
public void deleteIfAttachedTo(final Object domainObject) {
deleteIfAttachedTo(domainObject, Policy.PAPERCLIPS_ONLY);
}
@Programmatic
public void deleteIfAttachedTo(final Object domainObject, final Policy policy) {
final List<Paperclip> paperclips = findByAttachedTo(domainObject);
for (Paperclip paperclip : paperclips) {
delete(paperclip);
if(policy == Policy.PAPERCLIPS_AND_DOCUMENTS_IF_ORPHANED) {
final DocumentAbstract document = paperclip.getDocument();
if(orphaned(document, domainObject)) {
repositoryService.remove(document);
}
}
}
}

private boolean orphaned(final DocumentAbstract document, final Object attachedTo) {
final List<Paperclip> paperclips = findByDocument(document);
for (Paperclip paperclip : paperclips) {
if(paperclip.getAttachedTo() != attachedTo) {
// found a paperclip for this document attached to some other object
return false;
}
}
return true;
}
//endregion

Expand Down

0 comments on commit cd76579

Please sign in to comment.