diff --git a/dspace-api/src/main/java/org/dspace/content/Collection.java b/dspace-api/src/main/java/org/dspace/content/Collection.java index 8964b6767f..d4998156f0 100644 --- a/dspace-api/src/main/java/org/dspace/content/Collection.java +++ b/dspace-api/src/main/java/org/dspace/content/Collection.java @@ -12,13 +12,14 @@ import org.dspace.content.factory.ContentServiceFactory; import org.dspace.content.service.CollectionService; import org.dspace.core.*; import org.dspace.eperson.Group; +import org.hibernate.annotations.CacheConcurrencyStrategy; +import org.hibernate.annotations.Sort; +import org.hibernate.annotations.SortType; import org.hibernate.proxy.HibernateProxyHelper; import javax.persistence.*; import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import java.util.*; /** * Class representing a collection. @@ -85,7 +86,8 @@ public class Collection extends DSpaceObject implements DSpaceObjectLegacySuppor joinColumns = {@JoinColumn(name = "collection_id") }, inverseJoinColumns = {@JoinColumn(name = "community_id") } ) - private final List communities = new ArrayList<>(); + @Sort(type = SortType.COMPARATOR, comparator = NameAscendingComparator.class) + private Set communities = new TreeSet<>(new NameAscendingComparator()); @Transient private transient CollectionService collectionService; @@ -265,8 +267,8 @@ public class Collection extends DSpaceObject implements DSpaceObjectLegacySuppor */ public List getCommunities() throws SQLException { - Collections.sort(communities, new NameAscendingComparator()); - return communities; + // We return a copy because we do not want people to add elements to this collection directly. + return Arrays.asList(communities.toArray(new Community[]{})); } void addCommunity(Community community) { @@ -348,4 +350,4 @@ public class Collection extends DSpaceObject implements DSpaceObjectLegacySuppor } return collectionService; } -} \ No newline at end of file +} diff --git a/dspace-api/src/main/java/org/dspace/content/CollectionServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/CollectionServiceImpl.java index dfa8f8e9b6..1e4b35e654 100644 --- a/dspace-api/src/main/java/org/dspace/content/CollectionServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/CollectionServiceImpl.java @@ -750,8 +750,8 @@ public class CollectionServiceImpl extends DSpaceObjectServiceImpl i while (owningCommunities.hasNext()) { Community owningCommunity = owningCommunities.next(); - owningCommunities.remove(); - owningCommunity.getCollections().remove(collection); + collection.removeCommunity(owningCommunity); + owningCommunity.removeCollection(collection); } collectionDAO.delete(context, collection); diff --git a/dspace-api/src/main/java/org/dspace/content/Community.java b/dspace-api/src/main/java/org/dspace/content/Community.java index 376a9283a5..9ecc5d7774 100644 --- a/dspace-api/src/main/java/org/dspace/content/Community.java +++ b/dspace-api/src/main/java/org/dspace/content/Community.java @@ -14,6 +14,9 @@ import org.dspace.content.factory.ContentServiceFactory; import org.dspace.content.service.CommunityService; import org.dspace.core.*; import org.dspace.eperson.Group; +import org.hibernate.annotations.CacheConcurrencyStrategy; +import org.hibernate.annotations.Sort; +import org.hibernate.annotations.SortType; import org.hibernate.proxy.HibernateProxyHelper; import javax.persistence.*; @@ -45,13 +48,16 @@ public class Community extends DSpaceObject implements DSpaceObjectLegacySupport joinColumns = {@JoinColumn(name = "parent_comm_id") }, inverseJoinColumns = {@JoinColumn(name = "child_comm_id") } ) - private final List subCommunities = new ArrayList<>(); + @Sort(type = SortType.COMPARATOR, comparator = NameAscendingComparator.class) + private Set subCommunities = new TreeSet(new NameAscendingComparator()); @ManyToMany(fetch = FetchType.LAZY, mappedBy = "subCommunities") - private List parentCommunities = new ArrayList<>(); + @Sort(type = SortType.COMPARATOR, comparator = NameAscendingComparator.class) + private Set parentCommunities = new TreeSet(new NameAscendingComparator());; @ManyToMany(fetch = FetchType.LAZY, mappedBy = "communities", cascade = {CascadeType.PERSIST}) - private final List collections = new ArrayList<>(); + @Sort(type = SortType.COMPARATOR, comparator = NameAscendingComparator.class) + private Set collections =new TreeSet(new NameAscendingComparator());; @OneToOne @JoinColumn(name = "admin") @@ -86,13 +92,13 @@ public class Community extends DSpaceObject implements DSpaceObjectLegacySupport void addSubCommunity(Community subCommunity) { - getSubcommunities().add(subCommunity); + subCommunities.add(subCommunity); setModified(); } void removeSubCommunity(Community subCommunity) { - getSubcommunities().remove(subCommunity); + subCommunities.remove(subCommunity); setModified(); } @@ -141,18 +147,18 @@ public class Community extends DSpaceObject implements DSpaceObjectLegacySupport */ public List getCollections() { - Collections.sort(collections, new NameAscendingComparator()); - return collections; + // We return a copy because we do not want people to add elements to this collection directly. + return Arrays.asList(collections.toArray(new Collection[]{})); } void addCollection(Collection collection) { - getCollections().add(collection); + collections.add(collection); } void removeCollection(Collection collection) { - getCollections().remove(collection); + collections.remove(collection); } /** @@ -164,8 +170,8 @@ public class Community extends DSpaceObject implements DSpaceObjectLegacySupport */ public List getSubcommunities() { - Collections.sort(subCommunities, new NameAscendingComparator()); - return subCommunities; + // We return a copy because we do not want people to add elements to this collection directly. + return Arrays.asList(subCommunities.toArray(new Community[]{})); } /** @@ -176,12 +182,12 @@ public class Community extends DSpaceObject implements DSpaceObjectLegacySupport */ public List getParentCommunities() { - Collections.sort(parentCommunities, new NameAscendingComparator()); - return parentCommunities; + // We return a copy because we do not want people to add elements to this collection directly. + return Arrays.asList(parentCommunities.toArray(new Community[]{})); } void addParentCommunity(Community parentCommunity) { - getParentCommunities().add(parentCommunity); + parentCommunities.add(parentCommunity); } void clearParentCommunities(){ @@ -189,6 +195,11 @@ public class Community extends DSpaceObject implements DSpaceObjectLegacySupport this.parentCommunities = null; } + public void removeParentCommunity(Community parentCommunity) + { + this.parentCommunities.remove(parentCommunity); + } + /** * Return true if other is the same Community * as this object, false otherwise @@ -254,4 +265,4 @@ public class Community extends DSpaceObject implements DSpaceObjectLegacySupport } return communityService; } -} \ No newline at end of file +} diff --git a/dspace-api/src/main/java/org/dspace/content/CommunityServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/CommunityServiceImpl.java index 0002871a0a..f93e6d441c 100644 --- a/dspace-api/src/main/java/org/dspace/content/CommunityServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/CommunityServiceImpl.java @@ -455,7 +455,7 @@ public class CommunityServiceImpl extends DSpaceObjectServiceImpl imp rawDelete(context, childCommunity); - childCommunity.getParentCommunities().remove(parentCommunity); + childCommunity.removeParentCommunity(parentCommunity); parentCommunity.removeSubCommunity(childCommunity); log.info(LogManager.getHeader(context, "remove_subcommunity", @@ -492,7 +492,7 @@ public class CommunityServiceImpl extends DSpaceObjectServiceImpl imp Iterator subcommunities = community.getSubcommunities().iterator(); while (subcommunities.hasNext()) { Community subCommunity = subcommunities.next(); - subcommunities.remove(); + community.removeSubCommunity(subCommunity); delete(context, subCommunity); } // now let the parent remove the community @@ -535,7 +535,7 @@ public class CommunityServiceImpl extends DSpaceObjectServiceImpl imp while (collections.hasNext()) { Collection collection = collections.next(); - collections.remove(); + community.removeCollection(collection); removeCollection(context, community, collection); } // delete subcommunities @@ -544,7 +544,7 @@ public class CommunityServiceImpl extends DSpaceObjectServiceImpl imp while (subCommunities.hasNext()) { Community subComm = subCommunities.next(); - subCommunities.remove(); + community.removeSubCommunity(subComm); delete(context, subComm); } diff --git a/dspace-api/src/main/java/org/dspace/content/Item.java b/dspace-api/src/main/java/org/dspace/content/Item.java index e7a0a00a1e..aca2738d54 100644 --- a/dspace-api/src/main/java/org/dspace/content/Item.java +++ b/dspace-api/src/main/java/org/dspace/content/Item.java @@ -13,13 +13,12 @@ import org.dspace.content.service.ItemService; import org.dspace.core.Constants; import org.dspace.core.Context; import org.dspace.eperson.EPerson; +import org.hibernate.annotations.Sort; +import org.hibernate.annotations.SortType; import org.hibernate.proxy.HibernateProxyHelper; import javax.persistence.*; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; -import java.util.List; +import java.util.*; /** * Class representing an item in DSpace. @@ -80,7 +79,8 @@ public class Item extends DSpaceObject implements DSpaceObjectLegacySupport joinColumns = {@JoinColumn(name = "item_id") }, inverseJoinColumns = {@JoinColumn(name = "collection_id") } ) - private final List collections = new ArrayList<>(); + @Sort(type = SortType.COMPARATOR, comparator = NameAscendingComparator.class) + private final Set collections = new TreeSet<>(new NameAscendingComparator()); @ManyToMany(fetch = FetchType.LAZY, mappedBy = "items") private final List bundles = new ArrayList<>(); @@ -232,18 +232,22 @@ public class Item extends DSpaceObject implements DSpaceObjectLegacySupport */ public List getCollections() { - Collections.sort(collections, new NameAscendingComparator()); - return collections; + // We return a copy because we do not want people to add elements to this collection directly. + return Arrays.asList(collections.toArray(new Collection[]{})); } void addCollection(Collection collection) { - getCollections().add(collection); + collections.add(collection); } void removeCollection(Collection collection) { - getCollections().remove(collection); + collections.remove(collection); + } + + public void clearCollections(){ + collections.clear(); } public Collection getTemplateItemOf() { diff --git a/dspace-api/src/main/java/org/dspace/content/ItemServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/ItemServiceImpl.java index 7ad0126236..c13ad27303 100644 --- a/dspace-api/src/main/java/org/dspace/content/ItemServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/ItemServiceImpl.java @@ -656,7 +656,7 @@ public class ItemServiceImpl extends DSpaceObjectServiceImpl implements It } //Only clear collections after we have removed everything else from the item - item.getCollections().clear(); + item.clearCollections(); item.setOwningCollection(null); // Finally remove item row