DS-3406: Ordering sub communities and collections

This commit is contained in:
Yana De Pauw
2017-01-12 12:54:47 +01:00
committed by Tim Donohue
parent 821678dae4
commit a588d42f5a
6 changed files with 55 additions and 38 deletions

View File

@@ -12,13 +12,14 @@ import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.CollectionService; import org.dspace.content.service.CollectionService;
import org.dspace.core.*; import org.dspace.core.*;
import org.dspace.eperson.Group; 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 org.hibernate.proxy.HibernateProxyHelper;
import javax.persistence.*; import javax.persistence.*;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.List;
/** /**
* Class representing a collection. * Class representing a collection.
@@ -85,7 +86,8 @@ public class Collection extends DSpaceObject implements DSpaceObjectLegacySuppor
joinColumns = {@JoinColumn(name = "collection_id") }, joinColumns = {@JoinColumn(name = "collection_id") },
inverseJoinColumns = {@JoinColumn(name = "community_id") } inverseJoinColumns = {@JoinColumn(name = "community_id") }
) )
private final List<Community> communities = new ArrayList<>(); @Sort(type = SortType.COMPARATOR, comparator = NameAscendingComparator.class)
private Set<Community> communities = new TreeSet<>(new NameAscendingComparator());
@Transient @Transient
private transient CollectionService collectionService; private transient CollectionService collectionService;
@@ -265,8 +267,8 @@ public class Collection extends DSpaceObject implements DSpaceObjectLegacySuppor
*/ */
public List<Community> getCommunities() throws SQLException public List<Community> getCommunities() throws SQLException
{ {
Collections.sort(communities, new NameAscendingComparator()); // We return a copy because we do not want people to add elements to this collection directly.
return communities; return Arrays.asList(communities.toArray(new Community[]{}));
} }
void addCommunity(Community community) { void addCommunity(Community community) {

View File

@@ -750,8 +750,8 @@ public class CollectionServiceImpl extends DSpaceObjectServiceImpl<Collection> i
while (owningCommunities.hasNext()) while (owningCommunities.hasNext())
{ {
Community owningCommunity = owningCommunities.next(); Community owningCommunity = owningCommunities.next();
owningCommunities.remove(); collection.removeCommunity(owningCommunity);
owningCommunity.getCollections().remove(collection); owningCommunity.removeCollection(collection);
} }
collectionDAO.delete(context, collection); collectionDAO.delete(context, collection);

View File

@@ -14,6 +14,9 @@ import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.CommunityService; import org.dspace.content.service.CommunityService;
import org.dspace.core.*; import org.dspace.core.*;
import org.dspace.eperson.Group; 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 org.hibernate.proxy.HibernateProxyHelper;
import javax.persistence.*; import javax.persistence.*;
@@ -45,13 +48,16 @@ public class Community extends DSpaceObject implements DSpaceObjectLegacySupport
joinColumns = {@JoinColumn(name = "parent_comm_id") }, joinColumns = {@JoinColumn(name = "parent_comm_id") },
inverseJoinColumns = {@JoinColumn(name = "child_comm_id") } inverseJoinColumns = {@JoinColumn(name = "child_comm_id") }
) )
private final List<Community> subCommunities = new ArrayList<>(); @Sort(type = SortType.COMPARATOR, comparator = NameAscendingComparator.class)
private Set<Community> subCommunities = new TreeSet<Community>(new NameAscendingComparator());
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "subCommunities") @ManyToMany(fetch = FetchType.LAZY, mappedBy = "subCommunities")
private List<Community> parentCommunities = new ArrayList<>(); @Sort(type = SortType.COMPARATOR, comparator = NameAscendingComparator.class)
private Set<Community> parentCommunities = new TreeSet<Community>(new NameAscendingComparator());;
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "communities", cascade = {CascadeType.PERSIST}) @ManyToMany(fetch = FetchType.LAZY, mappedBy = "communities", cascade = {CascadeType.PERSIST})
private final List<Collection> collections = new ArrayList<>(); @Sort(type = SortType.COMPARATOR, comparator = NameAscendingComparator.class)
private Set<Collection> collections =new TreeSet<Collection>(new NameAscendingComparator());;
@OneToOne @OneToOne
@JoinColumn(name = "admin") @JoinColumn(name = "admin")
@@ -86,13 +92,13 @@ public class Community extends DSpaceObject implements DSpaceObjectLegacySupport
void addSubCommunity(Community subCommunity) void addSubCommunity(Community subCommunity)
{ {
getSubcommunities().add(subCommunity); subCommunities.add(subCommunity);
setModified(); setModified();
} }
void removeSubCommunity(Community subCommunity) void removeSubCommunity(Community subCommunity)
{ {
getSubcommunities().remove(subCommunity); subCommunities.remove(subCommunity);
setModified(); setModified();
} }
@@ -141,18 +147,18 @@ public class Community extends DSpaceObject implements DSpaceObjectLegacySupport
*/ */
public List<Collection> getCollections() public List<Collection> getCollections()
{ {
Collections.sort(collections, new NameAscendingComparator()); // We return a copy because we do not want people to add elements to this collection directly.
return collections; return Arrays.asList(collections.toArray(new Collection[]{}));
} }
void addCollection(Collection collection) void addCollection(Collection collection)
{ {
getCollections().add(collection); collections.add(collection);
} }
void removeCollection(Collection collection) void removeCollection(Collection collection)
{ {
getCollections().remove(collection); collections.remove(collection);
} }
/** /**
@@ -164,8 +170,8 @@ public class Community extends DSpaceObject implements DSpaceObjectLegacySupport
*/ */
public List<Community> getSubcommunities() public List<Community> getSubcommunities()
{ {
Collections.sort(subCommunities, new NameAscendingComparator()); // We return a copy because we do not want people to add elements to this collection directly.
return subCommunities; return Arrays.asList(subCommunities.toArray(new Community[]{}));
} }
/** /**
@@ -176,12 +182,12 @@ public class Community extends DSpaceObject implements DSpaceObjectLegacySupport
*/ */
public List<Community> getParentCommunities() public List<Community> getParentCommunities()
{ {
Collections.sort(parentCommunities, new NameAscendingComparator()); // We return a copy because we do not want people to add elements to this collection directly.
return parentCommunities; return Arrays.asList(parentCommunities.toArray(new Community[]{}));
} }
void addParentCommunity(Community parentCommunity) { void addParentCommunity(Community parentCommunity) {
getParentCommunities().add(parentCommunity); parentCommunities.add(parentCommunity);
} }
void clearParentCommunities(){ void clearParentCommunities(){
@@ -189,6 +195,11 @@ public class Community extends DSpaceObject implements DSpaceObjectLegacySupport
this.parentCommunities = null; this.parentCommunities = null;
} }
public void removeParentCommunity(Community parentCommunity)
{
this.parentCommunities.remove(parentCommunity);
}
/** /**
* Return <code>true</code> if <code>other</code> is the same Community * Return <code>true</code> if <code>other</code> is the same Community
* as this object, <code>false</code> otherwise * as this object, <code>false</code> otherwise

View File

@@ -455,7 +455,7 @@ public class CommunityServiceImpl extends DSpaceObjectServiceImpl<Community> imp
rawDelete(context, childCommunity); rawDelete(context, childCommunity);
childCommunity.getParentCommunities().remove(parentCommunity); childCommunity.removeParentCommunity(parentCommunity);
parentCommunity.removeSubCommunity(childCommunity); parentCommunity.removeSubCommunity(childCommunity);
log.info(LogManager.getHeader(context, "remove_subcommunity", log.info(LogManager.getHeader(context, "remove_subcommunity",
@@ -492,7 +492,7 @@ public class CommunityServiceImpl extends DSpaceObjectServiceImpl<Community> imp
Iterator<Community> subcommunities = community.getSubcommunities().iterator(); Iterator<Community> subcommunities = community.getSubcommunities().iterator();
while (subcommunities.hasNext()) { while (subcommunities.hasNext()) {
Community subCommunity = subcommunities.next(); Community subCommunity = subcommunities.next();
subcommunities.remove(); community.removeSubCommunity(subCommunity);
delete(context, subCommunity); delete(context, subCommunity);
} }
// now let the parent remove the community // now let the parent remove the community
@@ -535,7 +535,7 @@ public class CommunityServiceImpl extends DSpaceObjectServiceImpl<Community> imp
while (collections.hasNext()) while (collections.hasNext())
{ {
Collection collection = collections.next(); Collection collection = collections.next();
collections.remove(); community.removeCollection(collection);
removeCollection(context, community, collection); removeCollection(context, community, collection);
} }
// delete subcommunities // delete subcommunities
@@ -544,7 +544,7 @@ public class CommunityServiceImpl extends DSpaceObjectServiceImpl<Community> imp
while (subCommunities.hasNext()) while (subCommunities.hasNext())
{ {
Community subComm = subCommunities.next(); Community subComm = subCommunities.next();
subCommunities.remove(); community.removeSubCommunity(subComm);
delete(context, subComm); delete(context, subComm);
} }

View File

@@ -13,13 +13,12 @@ import org.dspace.content.service.ItemService;
import org.dspace.core.Constants; import org.dspace.core.Constants;
import org.dspace.core.Context; import org.dspace.core.Context;
import org.dspace.eperson.EPerson; import org.dspace.eperson.EPerson;
import org.hibernate.annotations.Sort;
import org.hibernate.annotations.SortType;
import org.hibernate.proxy.HibernateProxyHelper; import org.hibernate.proxy.HibernateProxyHelper;
import javax.persistence.*; import javax.persistence.*;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.Date;
import java.util.List;
/** /**
* Class representing an item in DSpace. * Class representing an item in DSpace.
@@ -80,7 +79,8 @@ public class Item extends DSpaceObject implements DSpaceObjectLegacySupport
joinColumns = {@JoinColumn(name = "item_id") }, joinColumns = {@JoinColumn(name = "item_id") },
inverseJoinColumns = {@JoinColumn(name = "collection_id") } inverseJoinColumns = {@JoinColumn(name = "collection_id") }
) )
private final List<Collection> collections = new ArrayList<>(); @Sort(type = SortType.COMPARATOR, comparator = NameAscendingComparator.class)
private final Set<Collection> collections = new TreeSet<>(new NameAscendingComparator());
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "items") @ManyToMany(fetch = FetchType.LAZY, mappedBy = "items")
private final List<Bundle> bundles = new ArrayList<>(); private final List<Bundle> bundles = new ArrayList<>();
@@ -232,18 +232,22 @@ public class Item extends DSpaceObject implements DSpaceObjectLegacySupport
*/ */
public List<Collection> getCollections() public List<Collection> getCollections()
{ {
Collections.sort(collections, new NameAscendingComparator()); // We return a copy because we do not want people to add elements to this collection directly.
return collections; return Arrays.asList(collections.toArray(new Collection[]{}));
} }
void addCollection(Collection collection) void addCollection(Collection collection)
{ {
getCollections().add(collection); collections.add(collection);
} }
void removeCollection(Collection collection) void removeCollection(Collection collection)
{ {
getCollections().remove(collection); collections.remove(collection);
}
public void clearCollections(){
collections.clear();
} }
public Collection getTemplateItemOf() { public Collection getTemplateItemOf() {

View File

@@ -656,7 +656,7 @@ public class ItemServiceImpl extends DSpaceObjectServiceImpl<Item> implements It
} }
//Only clear collections after we have removed everything else from the item //Only clear collections after we have removed everything else from the item
item.getCollections().clear(); item.clearCollections();
item.setOwningCollection(null); item.setOwningCollection(null);
// Finally remove item row // Finally remove item row