Merge pull request #1319 from KevinVdV/DS-2996-fix-hiearchical-community-retrieval

[DS-2996] Fix retrieval of hierarchical of communities from a collection
This commit is contained in:
Tim Donohue
2016-08-08 12:07:20 -05:00
committed by GitHub
20 changed files with 65 additions and 38 deletions

View File

@@ -12,7 +12,10 @@ import java.util.*;
import org.dspace.content.Collection; import org.dspace.content.Collection;
import org.dspace.content.Community; import org.dspace.content.Community;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.CommunityService;
import org.dspace.core.ConfigurationManager; import org.dspace.core.ConfigurationManager;
import org.dspace.core.Context;
/** /**
* Utility class for lists of collections. * Utility class for lists of collections.
@@ -20,6 +23,9 @@ import org.dspace.core.ConfigurationManager;
public class CollectionDropDown { public class CollectionDropDown {
private static final CommunityService communityService = ContentServiceFactory.getInstance().getCommunityService();
/** /**
* Get full path starting from a top-level community via subcommunities down to a collection. * Get full path starting from a top-level community via subcommunities down to a collection.
* The full path will not be truncated. * The full path will not be truncated.
@@ -29,9 +35,9 @@ public class CollectionDropDown {
* @return Full path to the collection * @return Full path to the collection
* @throws SQLException if database error * @throws SQLException if database error
*/ */
public static String collectionPath(Collection col) throws SQLException public static String collectionPath(Context context, Collection col) throws SQLException
{ {
return CollectionDropDown.collectionPath(col, 0); return CollectionDropDown.collectionPath(context, col, 0);
} }
/** /**
@@ -45,7 +51,7 @@ public class CollectionDropDown {
* @return Full path to the collection (truncated) * @return Full path to the collection (truncated)
* @throws SQLException if database error * @throws SQLException if database error
*/ */
public static String collectionPath(Collection col, int maxchars) throws SQLException public static String collectionPath(Context context, Collection col, int maxchars) throws SQLException
{ {
String separator = ConfigurationManager.getProperty("subcommunity.separator"); String separator = ConfigurationManager.getProperty("subcommunity.separator");
if (separator == null) if (separator == null)
@@ -55,7 +61,7 @@ public class CollectionDropDown {
List<Community> getCom = null; List<Community> getCom = null;
StringBuffer name = new StringBuffer(""); StringBuffer name = new StringBuffer("");
getCom = col.getCommunities(); // all communities containing given collection getCom = communityService.getAllParents(context, col); // all communities containing given collection
for (Community com : getCom) for (Community com : getCom)
{ {
name.insert(0, com.getName() + separator); name.insert(0, com.getName() + separator);
@@ -83,13 +89,13 @@ public class CollectionDropDown {
* @return A sorted array of collection path entries (essentially collection/path pairs). * @return A sorted array of collection path entries (essentially collection/path pairs).
* @throws SQLException In case there are problems annotating a collection with its path. * @throws SQLException In case there are problems annotating a collection with its path.
*/ */
public static CollectionPathEntry[] annotateWithPaths(List<Collection> collections) throws SQLException public static CollectionPathEntry[] annotateWithPaths(Context context, List<Collection> collections) throws SQLException
{ {
CollectionPathEntry[] result = new CollectionPathEntry[collections.size()]; CollectionPathEntry[] result = new CollectionPathEntry[collections.size()];
for (int i = 0; i < collections.size(); i++) for (int i = 0; i < collections.size(); i++)
{ {
Collection collection = collections.get(i); Collection collection = collections.get(i);
CollectionPathEntry entry = new CollectionPathEntry(collection, collectionPath(collection)); CollectionPathEntry entry = new CollectionPathEntry(collection, collectionPath(context, collection));
result[i] = entry; result[i] = entry;
} }
Arrays.sort(result); Arrays.sort(result);

View File

@@ -306,7 +306,7 @@ public class BundleServiceImpl extends DSpaceObjectServiceImpl<Bundle> implement
collection = item.getOwningCollection(); collection = item.getOwningCollection();
if (collection != null) if (collection != null)
{ {
community = collection.getCommunities().iterator().next(); community = collection.getCommunities().get(0);
} }
} }
switch (action) switch (action)

View File

@@ -628,7 +628,7 @@ public class CollectionServiceImpl extends DSpaceObjectServiceImpl<Collection> i
@Override @Override
public void canEdit(Context context, Collection collection, boolean useInheritance) throws SQLException, AuthorizeException { public void canEdit(Context context, Collection collection, boolean useInheritance) throws SQLException, AuthorizeException {
List<Community> parents = collection.getCommunities(); List<Community> parents = communityService.getAllParents(context, collection);
for (Community parent : parents) { for (Community parent : parents) {
if (authorizeService.authorizeActionBoolean(context, parent, if (authorizeService.authorizeActionBoolean(context, parent,
Constants.WRITE, useInheritance)) { Constants.WRITE, useInheritance)) {
@@ -803,7 +803,7 @@ public class CollectionServiceImpl extends DSpaceObjectServiceImpl<Collection> i
List<Community> communities = collection.getCommunities(); List<Community> communities = collection.getCommunities();
if (CollectionUtils.isNotEmpty(communities)) if (CollectionUtils.isNotEmpty(communities))
{ {
community = communities.iterator().next(); community = communities.get(0);
} }
switch (action) switch (action)
@@ -836,7 +836,7 @@ public class CollectionServiceImpl extends DSpaceObjectServiceImpl<Collection> i
public DSpaceObject getParentObject(Context context, Collection collection) throws SQLException { public DSpaceObject getParentObject(Context context, Collection collection) throws SQLException {
List<Community> communities = collection.getCommunities(); List<Community> communities = collection.getCommunities();
if(CollectionUtils.isNotEmpty(communities)){ if(CollectionUtils.isNotEmpty(communities)){
return communities.iterator().next(); return communities.get(0);
}else{ }else{
return null; return null;
} }

View File

@@ -323,6 +323,17 @@ public class CommunityServiceImpl extends DSpaceObjectServiceImpl<Community> imp
return parentList; return parentList;
} }
@Override
public List<Community> getAllParents(Context context, Collection collection) throws SQLException {
List<Community> result = new ArrayList<>();
List<Community> communities = collection.getCommunities();
result.addAll(communities);
for (Community community : communities) {
result.addAll(getAllParents(context, community));
}
return result;
}
@Override @Override
public List<Collection> getAllCollections(Context context, Community community) throws SQLException { public List<Collection> getAllCollections(Context context, Community community) throws SQLException {
List<Collection> collectionList = new ArrayList<Collection>(); List<Collection> collectionList = new ArrayList<Collection>();

View File

@@ -241,11 +241,7 @@ public class ItemServiceImpl extends DSpaceObjectServiceImpl<Item> implements It
List<Community> result = new ArrayList<>(); List<Community> result = new ArrayList<>();
List<Collection> collections = item.getCollections(); List<Collection> collections = item.getCollections();
for (Collection collection : collections) { for (Collection collection : collections) {
List<Community> owningCommunities = collection.getCommunities(); result.addAll(communityService.getAllParents(context, collection));
for (Community community : owningCommunities) {
result.add(community);
result.addAll(communityService.getAllParents(context, community));
}
} }
return result; return result;

View File

@@ -189,6 +189,13 @@ public interface CommunityService extends DSpaceObjectService<Community>, DSpace
*/ */
public List<Community> getAllParents(Context context, Community community) throws SQLException; public List<Community> getAllParents(Context context, Community community) throws SQLException;
/**
* Return an array of parent communities of this collection.
*
* @return an array of parent communities
*/
public List<Community> getAllParents(Context context, Collection collection) throws SQLException;
/** /**
* Return an array of collections of this community and its subcommunities * Return an array of collections of this community and its subcommunities
* *

View File

@@ -677,10 +677,10 @@ public class SolrServiceImpl implements SearchService, IndexingService {
return locations; return locations;
} }
protected List<String> getCollectionLocations(Collection target) throws SQLException { protected List<String> getCollectionLocations(Context context, Collection target) throws SQLException {
List<String> locations = new Vector<String>(); List<String> locations = new Vector<String>();
// build list of community ids // build list of community ids
List<Community> communities = target.getCommunities(); List<Community> communities = communityService.getAllParents(context, target);
// now put those into strings // now put those into strings
for (Community community : communities) for (Community community : communities)
@@ -798,7 +798,7 @@ public class SolrServiceImpl implements SearchService, IndexingService {
*/ */
protected void buildDocument(Context context, Collection collection) protected void buildDocument(Context context, Collection collection)
throws SQLException, IOException { throws SQLException, IOException {
List<String> locations = getCollectionLocations(collection); List<String> locations = getCollectionLocations(context, collection);
// Create Lucene Document // Create Lucene Document
SolrInputDocument doc = buildDocument(Constants.COLLECTION, collection.getID(), SolrInputDocument doc = buildDocument(Constants.COLLECTION, collection.getID(),

View File

@@ -128,7 +128,7 @@ public class ItemCheck extends Check {
return sb.toString(); return sb.toString();
} }
public String getCollectionSizesInfo(Context context) throws SQLException { public String getCollectionSizesInfo(final Context context) throws SQLException {
final StringBuffer ret = new StringBuffer(); final StringBuffer ret = new StringBuffer();
List<Map.Entry<Collection, Long>> colBitSizes = collectionService.getCollectionsWithBitstreamSizesTotal(context); List<Map.Entry<Collection, Long>> colBitSizes = collectionService.getCollectionsWithBitstreamSizesTotal(context);
long total_size = 0; long total_size = 0;
@@ -137,8 +137,8 @@ public class ItemCheck extends Check {
@Override @Override
public int compare(Map.Entry<Collection, Long> o1, Map.Entry<Collection, Long> o2) { public int compare(Map.Entry<Collection, Long> o1, Map.Entry<Collection, Long> o2) {
try { try {
return CollectionDropDown.collectionPath(o1.getKey()).compareTo( return CollectionDropDown.collectionPath(context, o1.getKey()).compareTo(
CollectionDropDown.collectionPath(o2.getKey()) CollectionDropDown.collectionPath(context, o2.getKey())
); );
} catch (Exception e) { } catch (Exception e) {
ret.append(e.getMessage()); ret.append(e.getMessage());
@@ -151,7 +151,7 @@ public class ItemCheck extends Check {
total_size += size; total_size += size;
Collection col = row.getKey(); Collection col = row.getKey();
ret.append(String.format( ret.append(String.format(
"\t%s: %s\n", CollectionDropDown.collectionPath(col), FileUtils.byteCountToDisplaySize((long) size))); "\t%s: %s\n", CollectionDropDown.collectionPath(context, col), FileUtils.byteCountToDisplaySize((long) size)));
} }
ret.append(String.format( ret.append(String.format(
"Total size: %s\n", FileUtils.byteCountToDisplaySize(total_size))); "Total size: %s\n", FileUtils.byteCountToDisplaySize(total_size)));

View File

@@ -377,7 +377,7 @@ implements ConverterPlugin
} }
// add all parents // add all parents
for (DSpaceObject parent : collection.getCommunities()) for (DSpaceObject parent : communityService.getAllParents(context, collection))
{ {
if (!RDFUtil.isPublicBoolean(context, parent)) if (!RDFUtil.isPublicBoolean(context, parent))
{ {

View File

@@ -88,7 +88,7 @@ public class SelectCollectionTag extends TagSupport
{ {
sb.append(" selected=\"selected\""); sb.append(" selected=\"selected\"");
} }
sb.append(">").append(CollectionDropDown.collectionPath(coll)).append("</option>\n"); sb.append(">").append(CollectionDropDown.collectionPath(context, coll)).append("</option>\n");
} }
sb.append("</select>\n"); sb.append("</select>\n");

View File

@@ -30,6 +30,8 @@
<%@ page import="org.dspace.app.util.CollectionDropDown" %> <%@ page import="org.dspace.app.util.CollectionDropDown" %>
<%@ page import="org.dspace.eperson.Subscription" %> <%@ page import="org.dspace.eperson.Subscription" %>
<%@ page import="java.util.List" %> <%@ page import="java.util.List" %>
<%@ page import="org.dspace.app.webui.util.UIUtil" %>
<%@ page import="org.dspace.core.Context" %>
<% <%
List<Collection> availableSubscriptions = List<Collection> availableSubscriptions =
@@ -38,6 +40,7 @@
(List<Subscription>) request.getAttribute("subscriptions"); (List<Subscription>) request.getAttribute("subscriptions");
boolean updated = boolean updated =
((Boolean) request.getAttribute("updated")).booleanValue(); ((Boolean) request.getAttribute("updated")).booleanValue();
Context context = UIUtil.obtainContext(request);
%> %>
<dspace:layout style="submission" locbar="link" <dspace:layout style="submission" locbar="link"
@@ -72,7 +75,7 @@ for (int i = 0; i < availableSubscriptions.size(); i++)
{ {
%> %>
<option value="<%= availableSubscriptions.get(i).getID() %>"> <option value="<%= availableSubscriptions.get(i).getID() %>">
<%= CollectionDropDown.collectionPath(availableSubscriptions.get(i), 0) %> <%= CollectionDropDown.collectionPath(context, availableSubscriptions.get(i), 0) %>
</option> </option>
<% <%
} }
@@ -110,7 +113,7 @@ for (int i = 0; i < availableSubscriptions.size(); i++)
<td class="<%= row %>RowOddCol"> <td class="<%= row %>RowOddCol">
<a href="<%= request.getContextPath() %>/handle/<%= subscriptions.get(i).getCollection().getHandle() %>"> <a href="<%= request.getContextPath() %>/handle/<%= subscriptions.get(i).getCollection().getHandle() %>">
<%= CollectionDropDown.collectionPath(subscriptions.get(i).getCollection(),0) %> <%= CollectionDropDown.collectionPath(context, subscriptions.get(i).getCollection(),0) %>
</a> </a>
</td> </td>
<td class="<%= row %>RowEvenCol"> <td class="<%= row %>RowEvenCol">

View File

@@ -237,7 +237,7 @@ public class XOAI {
for (Collection col : item.getCollections()) for (Collection col : item.getCollections())
doc.addField("item.collections", doc.addField("item.collections",
"col_" + col.getHandle().replace("/", "_")); "col_" + col.getHandle().replace("/", "_"));
for (Community com : collectionsService.flatParentCommunities(item)) for (Community com : collectionsService.flatParentCommunities(context, item))
doc.addField("item.communities", doc.addField("item.communities",
"com_" + com.getHandle().replace("/", "_")); "com_" + com.getHandle().replace("/", "_"));

View File

@@ -10,6 +10,7 @@ package org.dspace.xoai.services.api;
import org.dspace.content.Collection; import org.dspace.content.Collection;
import org.dspace.content.Community; import org.dspace.content.Community;
import org.dspace.content.Item; import org.dspace.content.Item;
import org.dspace.core.Context;
import org.dspace.xoai.services.api.context.ContextService; import org.dspace.xoai.services.api.context.ContextService;
import java.sql.SQLException; import java.sql.SQLException;
@@ -20,5 +21,5 @@ public interface CollectionsService {
List<UUID> getAllSubCollections(ContextService contextService, UUID communityId) throws SQLException; List<UUID> getAllSubCollections(ContextService contextService, UUID communityId) throws SQLException;
List<Community> flatParentCommunities(Collection collection) throws SQLException; List<Community> flatParentCommunities(Collection collection) throws SQLException;
List<Community> flatParentCommunities(Community community) throws SQLException; List<Community> flatParentCommunities(Community community) throws SQLException;
List<Community> flatParentCommunities(Item item) throws SQLException; List<Community> flatParentCommunities(Context context, Item item) throws SQLException;
} }

View File

@@ -10,6 +10,7 @@ package org.dspace.xoai.services.impl;
import org.dspace.content.Collection; import org.dspace.content.Collection;
import org.dspace.content.Community; import org.dspace.content.Community;
import org.dspace.content.Item; import org.dspace.content.Item;
import org.dspace.core.Context;
import org.dspace.xoai.services.api.context.ContextService; import org.dspace.xoai.services.api.context.ContextService;
import org.dspace.xoai.services.api.context.ContextServiceException; import org.dspace.xoai.services.api.context.ContextServiceException;
import org.dspace.xoai.services.api.CollectionsService; import org.dspace.xoai.services.api.CollectionsService;
@@ -96,14 +97,14 @@ public class DSpaceCollectionsService implements CollectionsService {
} }
@Override @Override
public List<Community> flatParentCommunities(Item c) public List<Community> flatParentCommunities(Context context, Item c)
throws SQLException throws SQLException
{ {
Queue<Community> queue = new LinkedList<>(); Queue<Community> queue = new LinkedList<>();
List<Community> result = new ArrayList<>(); List<Community> result = new ArrayList<>();
for (Collection com : c.getCollections()) for (Collection collection : c.getCollections())
queue.addAll(com.getCommunities()); queue.addAll(communityService.getAllParents(context, collection));
while (!queue.isEmpty()) while (!queue.isEmpty())
{ {

View File

@@ -10,6 +10,7 @@ package org.dspace.rest.common;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.dspace.content.factory.ContentServiceFactory; import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.CollectionService; import org.dspace.content.service.CollectionService;
import org.dspace.content.service.CommunityService;
import org.dspace.content.service.ItemService; import org.dspace.content.service.ItemService;
import org.dspace.core.Context; import org.dspace.core.Context;
@@ -31,6 +32,7 @@ import java.util.List;
*/ */
@XmlRootElement(name = "collection") @XmlRootElement(name = "collection")
public class Collection extends DSpaceObject { public class Collection extends DSpaceObject {
protected CommunityService communityService = ContentServiceFactory.getInstance().getCommunityService();
protected CollectionService collectionService = ContentServiceFactory.getInstance().getCollectionService(); protected CollectionService collectionService = ContentServiceFactory.getInstance().getCollectionService();
protected ItemService itemService = ContentServiceFactory.getInstance().getItemService(); protected ItemService itemService = ContentServiceFactory.getInstance().getItemService();
@@ -69,7 +71,7 @@ public class Collection extends DSpaceObject {
this.setSidebarText(collectionService.getMetadata(collection, org.dspace.content.Collection.SIDEBAR_TEXT)); this.setSidebarText(collectionService.getMetadata(collection, org.dspace.content.Collection.SIDEBAR_TEXT));
if(expandFields.contains("parentCommunityList") || expandFields.contains("all")) { if(expandFields.contains("parentCommunityList") || expandFields.contains("all")) {
List<org.dspace.content.Community> parentCommunities = collection.getCommunities(); List<org.dspace.content.Community> parentCommunities = communityService.getAllParents(context, collection);
for(org.dspace.content.Community parentCommunity : parentCommunities) { for(org.dspace.content.Community parentCommunity : parentCommunities) {
this.addParentCommunityList(new Community(parentCommunity, servletContext, null, context)); this.addParentCommunityList(new Community(parentCommunity, servletContext, null, context));
} }

View File

@@ -77,7 +77,7 @@ public class FilteredCollection extends DSpaceObject {
} }
if(expandFields.contains("parentCommunityList") || expandFields.contains("all")) { if(expandFields.contains("parentCommunityList") || expandFields.contains("all")) {
List<org.dspace.content.Community> parentCommunities = collection.getCommunities(); List<org.dspace.content.Community> parentCommunities = communityService.getAllParents(context, collection);
List<Community> parentCommunityList = new ArrayList<Community>(); List<Community> parentCommunityList = new ArrayList<Community>();
for(org.dspace.content.Community parentCommunity : parentCommunities) { for(org.dspace.content.Community parentCommunity : parentCommunities) {
parentCommunityList.add(new Community(parentCommunity, servletContext, null, context)); parentCommunityList.add(new Community(parentCommunity, servletContext, null, context));
@@ -95,7 +95,7 @@ public class FilteredCollection extends DSpaceObject {
} }
if(expandFields.contains("topCommunity") | expandFields.contains("all")) { if(expandFields.contains("topCommunity") | expandFields.contains("all")) {
List<org.dspace.content.Community> parentCommunities = collection.getCommunities(); List<org.dspace.content.Community> parentCommunities = communityService.getAllParents(context, collection);
if (parentCommunities.size() > 0) { if (parentCommunities.size() > 0) {
org.dspace.content.Community topCommunity = parentCommunities.get(parentCommunities.size()-1); org.dspace.content.Community topCommunity = parentCommunities.get(parentCommunities.size()-1);
this.setTopCommunity(new Community(topCommunity, servletContext, null, context)); this.setTopCommunity(new Community(topCommunity, servletContext, null, context));

View File

@@ -72,7 +72,7 @@ public class BatchImportMain extends AbstractDSpaceTransformer {
select.addOption("",T_collection_default); select.addOption("",T_collection_default);
for (Collection collection : collections) for (Collection collection : collections)
{ {
select.addOption(collection.getHandle(), CollectionDropDown.collectionPath(collection)); select.addOption(collection.getHandle(), CollectionDropDown.collectionPath(context, collection));
} }
//Zip File Upload //Zip File Upload

View File

@@ -97,7 +97,7 @@ public class MoveItemForm extends AbstractDSpaceTransformer {
// Only add the item if it isn't already the owner // Only add the item if it isn't already the owner
if (!itemService.isOwningCollection(item, collection)) if (!itemService.isOwningCollection(item, collection))
{ {
select.addOption(collection.equals(owningCollection), collection.getID().toString(), CollectionDropDown.collectionPath(collection)); select.addOption(collection.equals(owningCollection), collection.getID().toString(), CollectionDropDown.collectionPath(context, collection));
} }
} }

View File

@@ -385,7 +385,7 @@ public class EditProfile extends AbstractDSpaceTransformer
subscriptions.enableDeleteOperation(); subscriptions.enableDeleteOperation();
subscriptions.addOption(-1,T_select_collection); subscriptions.addOption(-1,T_select_collection);
CollectionDropDown.CollectionPathEntry[] possibleEntries = CollectionDropDown.annotateWithPaths(possibleList); CollectionDropDown.CollectionPathEntry[] possibleEntries = CollectionDropDown.annotateWithPaths(context, possibleList);
for (CollectionDropDown.CollectionPathEntry possible : possibleEntries) for (CollectionDropDown.CollectionPathEntry possible : possibleEntries)
{ {
subscriptions.addOption(possible.collection.getID().toString(), possible.path); subscriptions.addOption(possible.collection.getID().toString(), possible.path);

View File

@@ -105,7 +105,7 @@ public class SelectCollectionStep extends AbstractSubmissionStep
select.setHelp(T_collection_help); select.setHelp(T_collection_help);
select.addOption("",T_collection_default); select.addOption("",T_collection_default);
CollectionDropDown.CollectionPathEntry[] collectionPaths = CollectionDropDown.annotateWithPaths(collections); CollectionDropDown.CollectionPathEntry[] collectionPaths = CollectionDropDown.annotateWithPaths(context, collections);
for (CollectionDropDown.CollectionPathEntry entry : collectionPaths) for (CollectionDropDown.CollectionPathEntry entry : collectionPaths)
{ {
select.addOption(entry.collection.getHandle(), entry.path); select.addOption(entry.collection.getHandle(), entry.path);