mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-07 01:54:22 +00:00
[DS-2701] dspace-oai 'the simple bits'
This commit is contained in:
@@ -23,14 +23,11 @@ import org.apache.solr.client.solrj.SolrServer;
|
||||
import org.apache.solr.client.solrj.SolrServerException;
|
||||
import org.apache.solr.common.SolrDocumentList;
|
||||
import org.apache.solr.common.SolrInputDocument;
|
||||
import org.dspace.authorize.AuthorizeException;
|
||||
import org.dspace.authorize.AuthorizeServiceImpl;
|
||||
import org.dspace.authorize.service.AuthorizeService;
|
||||
import org.dspace.content.*;
|
||||
import org.dspace.core.ConfigurationManager;
|
||||
import org.dspace.core.Constants;
|
||||
import org.dspace.core.Context;
|
||||
import org.dspace.storage.rdbms.DatabaseManager;
|
||||
import org.dspace.storage.rdbms.TableRowIterator;
|
||||
import org.dspace.xoai.exceptions.CompilingException;
|
||||
import org.dspace.xoai.services.api.cache.XOAICacheService;
|
||||
import org.dspace.xoai.services.api.cache.XOAIItemCacheService;
|
||||
@@ -55,10 +52,12 @@ import java.text.ParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import javax.inject.Inject;
|
||||
import org.dspace.content.service.ItemService;
|
||||
|
||||
import static com.lyncode.xoai.dataprovider.core.Granularity.Second;
|
||||
import static org.dspace.content.Item.find;
|
||||
import static org.dspace.xoai.util.ItemUtils.retrieveMetadata;
|
||||
|
||||
/**
|
||||
@@ -68,9 +67,9 @@ import static org.dspace.xoai.util.ItemUtils.retrieveMetadata;
|
||||
public class XOAI {
|
||||
private static Logger log = LogManager.getLogger(XOAI.class);
|
||||
|
||||
private Context context;
|
||||
private final Context context;
|
||||
private boolean optimize;
|
||||
private boolean verbose;
|
||||
private final boolean verbose;
|
||||
private boolean clean;
|
||||
|
||||
@Autowired
|
||||
@@ -87,15 +86,19 @@ public class XOAI {
|
||||
private XOAIItemCacheService xoaiItemCacheService;
|
||||
@Autowired
|
||||
private CollectionsService collectionsService;
|
||||
@Inject
|
||||
private AuthorizeService authorizeService;
|
||||
@Inject
|
||||
private ItemService itemService;
|
||||
|
||||
|
||||
private static List<String> getFileFormats(Item item) {
|
||||
List<String> formats = new ArrayList<String>();
|
||||
private List<String> getFileFormats(Item item) {
|
||||
List<String> formats = new ArrayList<>();
|
||||
try {
|
||||
for (Bundle b : item.getBundles("ORIGINAL")) {
|
||||
for (Bitstream bs : b.getBitstreams()) {
|
||||
if (!formats.contains(bs.getFormat().getMIMEType())) {
|
||||
formats.add(bs.getFormat().getMIMEType());
|
||||
for (Bundle b : itemService.getBundles(item, "ORIGINAL")) {
|
||||
for (BundleBitstream bs : b.getBitstreams()) {
|
||||
if (!formats.contains(bs.getBitstream().getFormat(context).getMIMEType())) {
|
||||
formats.add(bs.getBitstream().getFormat(context).getMIMEType());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -154,11 +157,7 @@ public class XOAI {
|
||||
// Set last compilation date
|
||||
xoaiLastCompilationCacheService.put(new Date());
|
||||
return result;
|
||||
} catch (DSpaceSolrException ex) {
|
||||
throw new DSpaceSolrIndexerException(ex.getMessage(), ex);
|
||||
} catch (SolrServerException ex) {
|
||||
throw new DSpaceSolrIndexerException(ex.getMessage(), ex);
|
||||
} catch (IOException ex) {
|
||||
} catch (DSpaceSolrException | SolrServerException | IOException ex) {
|
||||
throw new DSpaceSolrIndexerException(ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
@@ -257,17 +256,20 @@ public class XOAI {
|
||||
doc.addField("item.communities",
|
||||
"com_" + com.getHandle().replace("/", "_"));
|
||||
|
||||
Metadatum[] allData = item.getMetadata(Item.ANY, Item.ANY, Item.ANY,
|
||||
Item.ANY);
|
||||
for (Metadatum dc : allData) {
|
||||
String key = "metadata." + dc.schema + "." + dc.element;
|
||||
if (dc.qualifier != null) {
|
||||
key += "." + dc.qualifier;
|
||||
List<MetadataValue> allData = itemService.getMetadata(item,
|
||||
Item.ANY, Item.ANY, Item.ANY, Item.ANY);
|
||||
for (MetadataValue dc : allData) {
|
||||
MetadataField field = dc.getMetadataField();
|
||||
String key = "metadata."
|
||||
+ field.getMetadataSchema().getName() + "."
|
||||
+ field.getElement();
|
||||
if (field.getQualifier() != null) {
|
||||
key += "." + field.getQualifier();
|
||||
}
|
||||
doc.addField(key, dc.value);
|
||||
if (dc.authority != null) {
|
||||
doc.addField(key + ".authority", dc.authority);
|
||||
doc.addField(key + ".confidence", dc.confidence + "");
|
||||
doc.addField(key, dc.getValue());
|
||||
if (dc.getAuthority() != null) {
|
||||
doc.addField(key + ".authority", dc.getAuthority());
|
||||
doc.addField(key + ".confidence", dc.getConfidence() + "");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -276,10 +278,10 @@ public class XOAI {
|
||||
}
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
XmlOutputContext context = XmlOutputContext.emptyContext(out, Second);
|
||||
retrieveMetadata(item).write(context);
|
||||
context.getWriter().flush();
|
||||
context.getWriter().close();
|
||||
XmlOutputContext xmlContext = XmlOutputContext.emptyContext(out, Second);
|
||||
retrieveMetadata(item).write(xmlContext);
|
||||
xmlContext.getWriter().flush();
|
||||
xmlContext.getWriter().close();
|
||||
doc.addField("item.compile", out.toString());
|
||||
|
||||
if (verbose) {
|
||||
@@ -294,7 +296,7 @@ public class XOAI {
|
||||
boolean pub = false;
|
||||
try {
|
||||
//Check if READ access allowed on this Item
|
||||
pub = AuthorizeManager.authorizeActionBoolean(context, item, Constants.READ);
|
||||
pub = authorizeService.authorizeActionBoolean(context, item, Constants.READ);
|
||||
} catch (SQLException ex) {
|
||||
log.error(ex.getMessage());
|
||||
}
|
||||
@@ -327,9 +329,7 @@ public class XOAI {
|
||||
solrServerResolver.getServer().deleteByQuery("*:*");
|
||||
solrServerResolver.getServer().commit();
|
||||
System.out.println("Index cleared");
|
||||
} catch (SolrServerException ex) {
|
||||
throw new DSpaceSolrIndexerException(ex.getMessage(), ex);
|
||||
} catch (IOException ex) {
|
||||
} catch (SolrServerException | IOException ex) {
|
||||
throw new DSpaceSolrIndexerException(ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
@@ -449,13 +449,13 @@ public class XOAI {
|
||||
}
|
||||
|
||||
private void compile() throws CompilingException {
|
||||
ItemIterator iterator;
|
||||
Iterator<Item> iterator;
|
||||
try {
|
||||
Date last = xoaiLastCompilationCacheService.get();
|
||||
|
||||
if (last == null) {
|
||||
System.out.println("Retrieving all items to be compiled");
|
||||
iterator = Item.findAll(context);
|
||||
iterator = itemService.findAll(context);
|
||||
} else {
|
||||
System.out.println("Retrieving items modified after " + last + " to be compiled");
|
||||
String query = "SELECT * FROM item WHERE last_modified>?";
|
||||
@@ -466,13 +466,10 @@ public class XOAI {
|
||||
Item item = iterator.next();
|
||||
if (verbose) System.out.println("Compiling item with handle: " + item.getHandle());
|
||||
xoaiItemCacheService.put(item, retrieveMetadata(item));
|
||||
context.clearCache();
|
||||
}
|
||||
|
||||
xoaiLastCompilationCacheService.put(new Date());
|
||||
} catch (SQLException e) {
|
||||
throw new CompilingException(e);
|
||||
} catch (IOException e) {
|
||||
} catch (SQLException | IOException e) {
|
||||
throw new CompilingException(e);
|
||||
}
|
||||
System.out.println("Items compiled");
|
||||
|
@@ -14,11 +14,13 @@ import java.util.List;
|
||||
|
||||
import org.apache.log4j.LogManager;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.dspace.authorize.AuthorizeManager;
|
||||
import org.dspace.authorize.factory.AuthorizeServiceFactory;
|
||||
import org.dspace.authorize.service.AuthorizeService;
|
||||
import org.dspace.content.Item;
|
||||
import org.dspace.core.Constants;
|
||||
import org.dspace.core.Context;
|
||||
import org.dspace.handle.HandleServiceImpl;
|
||||
import org.dspace.handle.factory.HandleServiceFactory;
|
||||
import org.dspace.handle.service.HandleService;
|
||||
import org.dspace.xoai.data.DSpaceItem;
|
||||
import org.dspace.xoai.filter.results.DatabaseFilterResult;
|
||||
import org.dspace.xoai.filter.results.SolrFilterResult;
|
||||
@@ -29,12 +31,18 @@ import org.dspace.xoai.filter.results.SolrFilterResult;
|
||||
*/
|
||||
public class DSpaceAuthorizationFilter extends DSpaceFilter
|
||||
{
|
||||
private static Logger log = LogManager.getLogger(DSpaceAuthorizationFilter.class);
|
||||
private static final Logger log = LogManager.getLogger(DSpaceAuthorizationFilter.class);
|
||||
|
||||
private static final AuthorizeService authorizeService
|
||||
= AuthorizeServiceFactory.getInstance().getAuthorizeService();
|
||||
|
||||
private static final HandleService handleService
|
||||
= HandleServiceFactory.getInstance().getHandleService();
|
||||
|
||||
@Override
|
||||
public DatabaseFilterResult buildDatabaseQuery(Context context)
|
||||
{
|
||||
List<Object> params = new ArrayList<Object>();
|
||||
List<Object> params = new ArrayList<>();
|
||||
return new DatabaseFilterResult("EXISTS (SELECT p.action_id FROM "
|
||||
+ "resourcepolicy p, " + "bundle2bitstream b, " + "bundle bu, "
|
||||
+ "item2bundle ib " + "WHERE " + "p.resource_type_id=0 AND "
|
||||
@@ -54,12 +62,12 @@ public class DSpaceAuthorizationFilter extends DSpaceFilter
|
||||
String handle = DSpaceItem.parseHandle(item.getIdentifier());
|
||||
if (handle == null)
|
||||
return false;
|
||||
Item dspaceItem = (Item) HandleManager.resolveToObject(context, handle);
|
||||
Item dspaceItem = (Item) handleService.resolveToObject(context, handle);
|
||||
if (dspaceItem == null)
|
||||
return false;
|
||||
|
||||
// Check if READ access allowed on Item
|
||||
pub = AuthorizeManager.authorizeActionBoolean(context, dspaceItem, Constants.READ);
|
||||
pub = authorizeService.authorizeActionBoolean(context, dspaceItem, Constants.READ);
|
||||
}
|
||||
catch (SQLException ex)
|
||||
{
|
||||
|
@@ -8,6 +8,7 @@
|
||||
package org.dspace.xoai.filter;
|
||||
|
||||
import com.lyncode.xoai.dataprovider.core.ReferenceSet;
|
||||
import java.sql.SQLException;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.log4j.LogManager;
|
||||
import org.apache.log4j.Logger;
|
||||
@@ -21,6 +22,8 @@ import org.dspace.xoai.services.api.database.CollectionsService;
|
||||
import org.dspace.xoai.services.api.database.HandleResolver;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import org.dspace.xoai.services.api.database.HandleResolverException;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -28,11 +31,11 @@ import java.util.List;
|
||||
*/
|
||||
public class DSpaceSetSpecFilter extends DSpaceFilter
|
||||
{
|
||||
private static Logger log = LogManager.getLogger(DSpaceSetSpecFilter.class);
|
||||
private static final Logger log = LogManager.getLogger(DSpaceSetSpecFilter.class);
|
||||
|
||||
private String setSpec;
|
||||
private HandleResolver handleResolver;
|
||||
private CollectionsService collectionsService;
|
||||
private final String setSpec;
|
||||
private final HandleResolver handleResolver;
|
||||
private final CollectionsService collectionsService;
|
||||
|
||||
public DSpaceSetSpecFilter(CollectionsService collectionsService, HandleResolver handleResolver, String spec)
|
||||
{
|
||||
@@ -66,14 +69,14 @@ public class DSpaceSetSpecFilter extends DSpaceFilter
|
||||
{
|
||||
DSpaceObject dso = handleResolver.resolve(setSpec.replace("com_", "").replace("_", "/"));
|
||||
if(dso != null){
|
||||
List<Integer> list = collectionsService.getAllSubCollections(dso.getID());
|
||||
List<UUID> list = collectionsService.getAllSubCollections(dso.getID());
|
||||
String subCollections = StringUtils.join(list.iterator(), ",");
|
||||
return new DatabaseFilterResult(
|
||||
"EXISTS (SELECT tmp.* FROM collection2item tmp WHERE tmp.resource_id=i.item_id AND collection_id IN ("
|
||||
+ subCollections + "))");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
catch (HandleResolverException | SQLException e)
|
||||
{
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
|
@@ -31,7 +31,7 @@ public class DSpaceWithdrawnFilter extends DSpaceFilter {
|
||||
@Override
|
||||
public DatabaseFilterResult buildDatabaseQuery(Context context)
|
||||
{
|
||||
List<Object> params = new ArrayList<Object>();
|
||||
List<Object> params = new ArrayList<>();
|
||||
|
||||
String filter = "i.withdrawn=TRUE";
|
||||
if(DatabaseManager.isOracle())
|
||||
@@ -48,10 +48,7 @@ public class DSpaceWithdrawnFilter extends DSpaceFilter {
|
||||
// we can properly respond with a "deleted" status via OAI-PMH.
|
||||
// Don't worry, this does NOT make the metadata public for withdrawn items,
|
||||
// it merely provides an item "tombstone" via OAI-PMH.
|
||||
if (item.isDeleted())
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
return item.isDeleted();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -13,9 +13,10 @@ import org.dspace.content.Item;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface CollectionsService {
|
||||
List<Integer> getAllSubCollections(int communityId) throws SQLException;
|
||||
List<UUID> getAllSubCollections(UUID communityId) throws SQLException;
|
||||
List<Community> flatParentCommunities(Collection collection) throws SQLException;
|
||||
List<Community> flatParentCommunities(Community community) throws SQLException;
|
||||
List<Community> flatParentCommunities(Item item) throws SQLException;
|
||||
|
@@ -16,11 +16,11 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DatabaseQuery {
|
||||
private static Logger log = Logger.getLogger(DatabaseQuery.class);
|
||||
private static final Logger log = Logger.getLogger(DatabaseQuery.class);
|
||||
|
||||
private String query;
|
||||
private List<Object> parameters;
|
||||
private Context context;
|
||||
private final List<Object> parameters;
|
||||
private final Context context;
|
||||
private Integer total;
|
||||
|
||||
private String countQuery;
|
||||
@@ -28,7 +28,7 @@ public class DatabaseQuery {
|
||||
|
||||
public DatabaseQuery(Context context) {
|
||||
this.context = context;
|
||||
this.parameters = new ArrayList<Object>();
|
||||
this.parameters = new ArrayList<>();
|
||||
}
|
||||
|
||||
public String getQuery() {
|
||||
|
@@ -14,7 +14,6 @@ import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class DSpaceContextService implements ContextService {
|
||||
private static final String OAI_CONTEXT = "OAI_CONTEXT";
|
||||
@@ -24,11 +23,7 @@ public class DSpaceContextService implements ContextService {
|
||||
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
|
||||
Object value = request.getAttribute(OAI_CONTEXT);
|
||||
if (value == null || !(value instanceof Context)) {
|
||||
try {
|
||||
request.setAttribute(OAI_CONTEXT, new Context());
|
||||
} catch (SQLException e) {
|
||||
throw new ContextServiceException(e);
|
||||
}
|
||||
request.setAttribute(OAI_CONTEXT, new Context());
|
||||
}
|
||||
return (Context) request.getAttribute(OAI_CONTEXT);
|
||||
}
|
||||
|
@@ -19,18 +19,24 @@ import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.UUID;
|
||||
import org.dspace.content.factory.ContentServiceFactory;
|
||||
import org.dspace.content.service.CommunityService;
|
||||
|
||||
public class DSpaceCollectionsService implements CollectionsService {
|
||||
|
||||
private ContextService contextService;
|
||||
private static final CommunityService communityService
|
||||
= ContentServiceFactory.getInstance().getCommunityService();
|
||||
|
||||
public List<Integer> getAllSubCollections(int communityId)
|
||||
@Override
|
||||
public List<UUID> getAllSubCollections(UUID communityId)
|
||||
throws SQLException
|
||||
{
|
||||
Queue<Community> comqueue = new LinkedList<Community>();
|
||||
List<Integer> list = new ArrayList<Integer>();
|
||||
Queue<Community> comqueue = new LinkedList<>();
|
||||
List<UUID> list = new ArrayList<>();
|
||||
try {
|
||||
comqueue.add(Community.find(contextService.getContext(), communityId));
|
||||
comqueue.add(communityService.find(contextService.getContext(), communityId));
|
||||
} catch (ContextServiceException e) {
|
||||
throw new SQLException(e);
|
||||
}
|
||||
@@ -40,26 +46,27 @@ public class DSpaceCollectionsService implements CollectionsService {
|
||||
for (Community sub : c.getSubcommunities())
|
||||
comqueue.add(sub);
|
||||
for (Collection col : c.getCollections())
|
||||
if (!list.contains(col))
|
||||
if (!list.contains(col.getID()))
|
||||
list.add(col.getID());
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Community> flatParentCommunities(Collection c)
|
||||
throws SQLException
|
||||
{
|
||||
Queue<Community> queue = new LinkedList<Community>();
|
||||
List<Community> result = new ArrayList<Community>();
|
||||
Queue<Community> queue = new LinkedList<>();
|
||||
List<Community> result = new ArrayList<>();
|
||||
for (Community com : c.getCommunities())
|
||||
queue.add(com);
|
||||
|
||||
while (!queue.isEmpty())
|
||||
{
|
||||
Community p = queue.poll();
|
||||
Community par = p.getParentCommunity();
|
||||
List<Community> par = p.getParentCommunities();
|
||||
if (par != null)
|
||||
queue.add(par);
|
||||
queue.addAll(par);
|
||||
if (!result.contains(p))
|
||||
result.add(p);
|
||||
}
|
||||
@@ -67,20 +74,21 @@ public class DSpaceCollectionsService implements CollectionsService {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Community> flatParentCommunities(Community c)
|
||||
throws SQLException
|
||||
{
|
||||
Queue<Community> queue = new LinkedList<Community>();
|
||||
List<Community> result = new ArrayList<Community>();
|
||||
Queue<Community> queue = new LinkedList<>();
|
||||
List<Community> result = new ArrayList<>();
|
||||
|
||||
queue.add(c);
|
||||
|
||||
while (!queue.isEmpty())
|
||||
{
|
||||
Community p = queue.poll();
|
||||
Community par = p.getParentCommunity();
|
||||
List<Community> par = p.getParentCommunities();
|
||||
if (par != null)
|
||||
queue.add(par);
|
||||
queue.addAll(par);
|
||||
if (!result.contains(p))
|
||||
result.add(p);
|
||||
}
|
||||
@@ -88,21 +96,22 @@ public class DSpaceCollectionsService implements CollectionsService {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Community> flatParentCommunities(Item c)
|
||||
throws SQLException
|
||||
{
|
||||
Queue<Community> queue = new LinkedList<Community>();
|
||||
List<Community> result = new ArrayList<Community>();
|
||||
Queue<Community> queue = new LinkedList<>();
|
||||
List<Community> result = new ArrayList<>();
|
||||
|
||||
for (Community com : c.getCommunities())
|
||||
queue.add(com);
|
||||
for (Collection com : c.getCollections())
|
||||
queue.addAll(com.getCommunities());
|
||||
|
||||
while (!queue.isEmpty())
|
||||
{
|
||||
Community p = queue.poll();
|
||||
Community par = p.getParentCommunity();
|
||||
List<Community> par = p.getParentCommunities();
|
||||
if (par != null)
|
||||
queue.add(par);
|
||||
queue.addAll(par);
|
||||
if (!result.contains(p))
|
||||
result.add(p);
|
||||
}
|
||||
|
@@ -39,8 +39,8 @@ public class DSpaceDatabaseQueryResolver implements DatabaseQueryResolver {
|
||||
|
||||
@Override
|
||||
public DatabaseQuery buildQuery(List<ScopedFilter> filters, int offset, int length) throws DatabaseQueryException {
|
||||
List<Object> parameters = new ArrayList<Object>();
|
||||
List<Object> countParameters = new ArrayList<Object>();
|
||||
List<Object> parameters = new ArrayList<>();
|
||||
List<Object> countParameters = new ArrayList<>();
|
||||
String query = "SELECT i.* FROM item i ";
|
||||
String countQuery = "SELECT COUNT(*) as count FROM item i";
|
||||
|
||||
@@ -98,7 +98,7 @@ public class DSpaceDatabaseQueryResolver implements DatabaseQueryResolver {
|
||||
}
|
||||
|
||||
private String buildCondition (List<ScopedFilter> filters, List<Object> parameters) throws ContextServiceException {
|
||||
List<String> whereCond = new ArrayList<String>();
|
||||
List<String> whereCond = new ArrayList<>();
|
||||
for (ScopedFilter filter : filters)
|
||||
whereCond.add(this.buildQuery(filter.getCondition(), filter.getScope(), parameters));
|
||||
|
||||
|
@@ -7,28 +7,27 @@
|
||||
*/
|
||||
package org.dspace.xoai.services.impl.database;
|
||||
|
||||
|
||||
import java.sql.SQLException;
|
||||
import javax.inject.Inject;
|
||||
import org.dspace.content.DSpaceObject;
|
||||
import org.dspace.handle.HandleServiceImpl;
|
||||
import org.dspace.handle.service.HandleService;
|
||||
import org.dspace.xoai.services.api.context.ContextService;
|
||||
import org.dspace.xoai.services.api.context.ContextServiceException;
|
||||
import org.dspace.xoai.services.api.database.HandleResolver;
|
||||
import org.dspace.xoai.services.api.database.HandleResolverException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class DSpaceHandlerResolver implements HandleResolver {
|
||||
@Autowired
|
||||
@Inject
|
||||
private ContextService contextService;
|
||||
|
||||
@Inject
|
||||
private HandleService handleService;
|
||||
|
||||
@Override
|
||||
public DSpaceObject resolve(String handle) throws HandleResolverException {
|
||||
try {
|
||||
return HandleServiceImpl.resolveToObject(contextService.getContext(), handle);
|
||||
} catch (ContextServiceException e) {
|
||||
throw new HandleResolverException(e);
|
||||
} catch (SQLException e) {
|
||||
return handleService.resolveToObject(contextService.getContext(), handle);
|
||||
} catch (ContextServiceException | SQLException e) {
|
||||
throw new HandleResolverException(e);
|
||||
}
|
||||
}
|
||||
@@ -36,10 +35,8 @@ public class DSpaceHandlerResolver implements HandleResolver {
|
||||
@Override
|
||||
public String getHandle(DSpaceObject object) throws HandleResolverException {
|
||||
try {
|
||||
return HandleServiceImpl.findHandle(contextService.getContext(), object);
|
||||
} catch (SQLException e) {
|
||||
throw new HandleResolverException(e);
|
||||
} catch (ContextServiceException e) {
|
||||
return handleService.findHandle(contextService.getContext(), object);
|
||||
} catch (SQLException | ContextServiceException e) {
|
||||
throw new HandleResolverException(e);
|
||||
}
|
||||
}
|
||||
|
@@ -39,6 +39,8 @@ import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
import org.dspace.handle.factory.HandleServiceFactory;
|
||||
import org.dspace.handle.service.HandleService;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -48,7 +50,10 @@ import java.util.regex.Pattern;
|
||||
public class DSpaceItemDatabaseRepository extends DSpaceItemRepository
|
||||
{
|
||||
|
||||
private static Logger log = LogManager.getLogger(DSpaceItemDatabaseRepository.class);
|
||||
private static final Logger log = LogManager.getLogger(DSpaceItemDatabaseRepository.class);
|
||||
|
||||
private static final HandleService handleService
|
||||
= HandleServiceFactory.getInstance().getHandleService();
|
||||
|
||||
private XOAIItemCacheService cacheService;
|
||||
private boolean useCache;
|
||||
@@ -79,11 +84,11 @@ public class DSpaceItemDatabaseRepository extends DSpaceItemRepository
|
||||
|
||||
private List<ReferenceSet> getSets(org.dspace.content.Item item)
|
||||
{
|
||||
List<ReferenceSet> sets = new ArrayList<ReferenceSet>();
|
||||
List<Community> coms = new ArrayList<Community>();
|
||||
List<ReferenceSet> sets = new ArrayList<>();
|
||||
List<Community> coms = new ArrayList<>();
|
||||
try
|
||||
{
|
||||
Collection[] itemCollections = item.getCollections();
|
||||
List<Collection> itemCollections = item.getCollections();
|
||||
for (Collection col : itemCollections)
|
||||
{
|
||||
ReferenceSet s = new DSpaceSet(col);
|
||||
@@ -113,7 +118,7 @@ public class DSpaceItemDatabaseRepository extends DSpaceItemRepository
|
||||
String parts[] = id.split(Pattern.quote(":"));
|
||||
if (parts.length == 3)
|
||||
{
|
||||
DSpaceObject obj = HandleServiceImpl.resolveToObject(context.getContext(),
|
||||
DSpaceObject obj = handleService.resolveToObject(context.getContext(),
|
||||
parts[2]);
|
||||
if (obj == null)
|
||||
throw new IdDoesNotExistException();
|
||||
@@ -129,13 +134,9 @@ public class DSpaceItemDatabaseRepository extends DSpaceItemRepository
|
||||
log.debug(e.getMessage(), e);
|
||||
throw new IdDoesNotExistException();
|
||||
}
|
||||
catch (SQLException e)
|
||||
catch (SQLException | IOException | ContextServiceException e)
|
||||
{
|
||||
throw new OAIException(e);
|
||||
} catch (IOException e) {
|
||||
throw new OAIException(e);
|
||||
} catch (ContextServiceException e) {
|
||||
throw new OAIException(e);
|
||||
}
|
||||
throw new IdDoesNotExistException();
|
||||
}
|
||||
@@ -145,7 +146,7 @@ public class DSpaceItemDatabaseRepository extends DSpaceItemRepository
|
||||
public ListItemsResults getItems(List<ScopedFilter> filters, int offset,
|
||||
int length) throws OAIException
|
||||
{
|
||||
List<Item> list = new ArrayList<Item>();
|
||||
List<Item> list = new ArrayList<>();
|
||||
try
|
||||
{
|
||||
DatabaseQuery databaseQuery = queryResolver.buildQuery(filters, offset, length);
|
||||
@@ -179,7 +180,7 @@ public class DSpaceItemDatabaseRepository extends DSpaceItemRepository
|
||||
List<ScopedFilter> filters, int offset, int length) throws OAIException
|
||||
{
|
||||
|
||||
List<ItemIdentifier> list = new ArrayList<ItemIdentifier>();
|
||||
List<ItemIdentifier> list = new ArrayList<>();
|
||||
try
|
||||
{
|
||||
DatabaseQuery databaseQuery = queryResolver.buildQuery(filters, offset, length);
|
||||
|
@@ -16,7 +16,6 @@ import org.dspace.content.Collection;
|
||||
import org.dspace.content.Community;
|
||||
import org.dspace.content.DSpaceObject;
|
||||
import org.dspace.core.Context;
|
||||
import org.dspace.handle.HandleServiceImpl;
|
||||
import org.dspace.storage.rdbms.DatabaseManager;
|
||||
import org.dspace.storage.rdbms.TableRow;
|
||||
import org.dspace.storage.rdbms.TableRowIterator;
|
||||
@@ -26,6 +25,11 @@ import java.io.Serializable;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.dspace.content.factory.ContentServiceFactory;
|
||||
import org.dspace.content.service.CollectionService;
|
||||
import org.dspace.content.service.CommunityService;
|
||||
import org.dspace.handle.factory.HandleServiceFactory;
|
||||
import org.dspace.handle.service.HandleService;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -35,6 +39,15 @@ public class DSpaceSetRepository implements SetRepository
|
||||
{
|
||||
private static final Logger log = LogManager.getLogger(DSpaceSetRepository.class);
|
||||
|
||||
private static final CommunityService communityService
|
||||
= ContentServiceFactory.getInstance().getCommunityService();
|
||||
|
||||
private static final CollectionService collectionService
|
||||
= ContentServiceFactory.getInstance().getCollectionService();
|
||||
|
||||
private static final HandleService handleService
|
||||
= HandleServiceFactory.getInstance().getHandleService();
|
||||
|
||||
private final Context _context;
|
||||
|
||||
public DSpaceSetRepository(Context context)
|
||||
@@ -84,9 +97,9 @@ public class DSpaceSetRepository implements SetRepository
|
||||
*/
|
||||
private List<Set> community(int offset, int length)
|
||||
{
|
||||
List<Set> array = new ArrayList<Set>();
|
||||
List<Set> array = new ArrayList<>();
|
||||
StringBuffer query = new StringBuffer("SELECT community_id FROM community ORDER BY community_id");
|
||||
List<Serializable> params = new ArrayList<Serializable>();
|
||||
List<Serializable> params = new ArrayList<>();
|
||||
|
||||
DatabaseManager.applyOffsetAndLimit(query,params,offset,length);
|
||||
|
||||
@@ -99,7 +112,7 @@ public class DSpaceSetRepository implements SetRepository
|
||||
{
|
||||
TableRow row = iterator.next();
|
||||
int communityID = row.getIntColumn("community_id");
|
||||
Community community = Community.find(_context, communityID);
|
||||
Community community = communityService.find(_context, communityID);
|
||||
array.add(DSpaceSet.newDSpaceCommunitySet(
|
||||
community.getHandle(), community.getName()));
|
||||
i++;
|
||||
@@ -122,9 +135,9 @@ public class DSpaceSetRepository implements SetRepository
|
||||
*/
|
||||
private List<Set> collection(int offset, int length)
|
||||
{
|
||||
List<Set> array = new ArrayList<Set>();
|
||||
List<Set> array = new ArrayList<>();
|
||||
StringBuffer query = new StringBuffer("SELECT collection_id FROM collection ORDER BY collection_id");
|
||||
List<Serializable> params = new ArrayList<Serializable>();
|
||||
List<Serializable> params = new ArrayList<>();
|
||||
|
||||
DatabaseManager.applyOffsetAndLimit(query,params,offset,length);
|
||||
|
||||
@@ -137,7 +150,7 @@ public class DSpaceSetRepository implements SetRepository
|
||||
{
|
||||
TableRow row = iterator.next();
|
||||
int collectionID = row.getIntColumn("collection_id");
|
||||
Collection collection = Collection.find(_context, collectionID);
|
||||
Collection collection = collectionService.find(_context, collectionID);
|
||||
array.add(DSpaceSet.newDSpaceCollectionSet(
|
||||
collection.getHandle(),
|
||||
collection.getName()));
|
||||
@@ -156,7 +169,7 @@ public class DSpaceSetRepository implements SetRepository
|
||||
{
|
||||
// Only database sets (virtual sets are added by lyncode common library)
|
||||
log.debug("Querying sets. Offset: " + offset + " - Length: " + length);
|
||||
List<Set> array = new ArrayList<Set>();
|
||||
List<Set> array = new ArrayList<>();
|
||||
int communityCount = this.getCommunityCount();
|
||||
log.debug("Communities: " + communityCount);
|
||||
int collectionCount = this.getCollectionCount();
|
||||
@@ -198,13 +211,11 @@ public class DSpaceSetRepository implements SetRepository
|
||||
{
|
||||
try
|
||||
{
|
||||
DSpaceObject dso = HandleServiceImpl.resolveToObject(_context,
|
||||
DSpaceObject dso = handleService.resolveToObject(_context,
|
||||
setSpec.replace("col_", "").replace("_", "/"));
|
||||
if (dso == null || !(dso instanceof Collection))
|
||||
return false;
|
||||
return true;
|
||||
return !(dso == null || !(dso instanceof Collection));
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch (IllegalStateException | SQLException ex)
|
||||
{
|
||||
log.error(ex.getMessage(), ex);
|
||||
}
|
||||
@@ -213,13 +224,11 @@ public class DSpaceSetRepository implements SetRepository
|
||||
{
|
||||
try
|
||||
{
|
||||
DSpaceObject dso = HandleServiceImpl.resolveToObject(_context,
|
||||
DSpaceObject dso = handleService.resolveToObject(_context,
|
||||
setSpec.replace("com_", "").replace("_", "/"));
|
||||
if (dso == null || !(dso instanceof Community))
|
||||
return false;
|
||||
return true;
|
||||
return !(dso == null || !(dso instanceof Community));
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch (IllegalStateException | SQLException ex)
|
||||
{
|
||||
log.error(ex.getMessage(), ex);
|
||||
}
|
||||
|
@@ -13,7 +13,6 @@ import com.lyncode.xoai.util.Base64Utils;
|
||||
import org.apache.log4j.LogManager;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.dspace.authorize.AuthorizeException;
|
||||
import org.dspace.content.Bitstream;
|
||||
import org.dspace.content.Bundle;
|
||||
import org.dspace.content.Metadatum;
|
||||
import org.dspace.content.Item;
|
||||
@@ -28,6 +27,11 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import org.dspace.content.BundleBitstream;
|
||||
import org.dspace.content.MetadataField;
|
||||
import org.dspace.content.MetadataValue;
|
||||
import org.dspace.content.factory.ContentServiceFactory;
|
||||
import org.dspace.content.service.ItemService;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -36,8 +40,10 @@ import java.util.List;
|
||||
@SuppressWarnings("deprecation")
|
||||
public class ItemUtils
|
||||
{
|
||||
private static Logger log = LogManager
|
||||
.getLogger(ItemUtils.class);
|
||||
private static final Logger log = LogManager.getLogger(ItemUtils.class);
|
||||
|
||||
private static final ItemService itemService
|
||||
= ContentServiceFactory.getInstance().getItemService();
|
||||
|
||||
private static Element getElement(List<Element> list, String name)
|
||||
{
|
||||
@@ -69,38 +75,40 @@ public class ItemUtils
|
||||
|
||||
// read all metadata into Metadata Object
|
||||
metadata = new Metadata();
|
||||
Metadatum[] vals = item.getMetadata(Item.ANY, Item.ANY, Item.ANY, Item.ANY);
|
||||
for (Metadatum val : vals)
|
||||
List<MetadataValue> vals = itemService.getMetadata(item, Item.ANY, Item.ANY, Item.ANY, Item.ANY);
|
||||
for (MetadataValue val : vals)
|
||||
{
|
||||
MetadataField field = val.getMetadataField();
|
||||
|
||||
Element valueElem = null;
|
||||
Element schema = getElement(metadata.getElement(), val.schema);
|
||||
Element schema = getElement(metadata.getElement(), field.getMetadataSchema().getName());
|
||||
if (schema == null)
|
||||
{
|
||||
schema = create(val.schema);
|
||||
schema = create(field.getMetadataSchema().getName());
|
||||
metadata.getElement().add(schema);
|
||||
}
|
||||
valueElem = schema;
|
||||
|
||||
// Has element.. with XOAI one could have only schema and value
|
||||
if (val.element != null && !val.element.equals(""))
|
||||
if (field.getElement() != null && !field.getElement().equals(""))
|
||||
{
|
||||
Element element = getElement(schema.getElement(),
|
||||
val.element);
|
||||
field.getElement());
|
||||
if (element == null)
|
||||
{
|
||||
element = create(val.element);
|
||||
element = create(field.getElement());
|
||||
schema.getElement().add(element);
|
||||
}
|
||||
valueElem = element;
|
||||
|
||||
// Qualified element?
|
||||
if (val.qualifier != null && !val.qualifier.equals(""))
|
||||
if (field.getQualifier() != null && !field.getQualifier().equals(""))
|
||||
{
|
||||
Element qualifier = getElement(element.getElement(),
|
||||
val.qualifier);
|
||||
field.getQualifier());
|
||||
if (qualifier == null)
|
||||
{
|
||||
qualifier = create(val.qualifier);
|
||||
qualifier = create(field.getQualifier());
|
||||
element.getElement().add(qualifier);
|
||||
}
|
||||
valueElem = qualifier;
|
||||
@@ -108,13 +116,13 @@ public class ItemUtils
|
||||
}
|
||||
|
||||
// Language?
|
||||
if (val.language != null && !val.language.equals(""))
|
||||
if (val.getLanguage() != null && !val.getLanguage().equals(""))
|
||||
{
|
||||
Element language = getElement(valueElem.getElement(),
|
||||
val.language);
|
||||
val.getLanguage());
|
||||
if (language == null)
|
||||
{
|
||||
language = create(val.language);
|
||||
language = create(val.getLanguage());
|
||||
valueElem.getElement().add(language);
|
||||
}
|
||||
valueElem = language;
|
||||
@@ -131,11 +139,11 @@ public class ItemUtils
|
||||
valueElem = language;
|
||||
}
|
||||
|
||||
valueElem.getField().add(createValue("value", val.value));
|
||||
if (val.authority != null) {
|
||||
valueElem.getField().add(createValue("authority", val.authority));
|
||||
if (val.confidence != Choices.CF_NOVALUE)
|
||||
valueElem.getField().add(createValue("confidence", val.confidence + ""));
|
||||
valueElem.getField().add(createValue("value", val.getValue()));
|
||||
if (val.getAuthority() != null) {
|
||||
valueElem.getField().add(createValue("authority", val.getAuthority()));
|
||||
if (val.getConfidence() != Choices.CF_NOVALUE)
|
||||
valueElem.getField().add(createValue("confidence", val.getConfidence() + ""));
|
||||
}
|
||||
}
|
||||
// Done! Metadata has been read!
|
||||
@@ -143,7 +151,7 @@ public class ItemUtils
|
||||
Element bundles = create("bundles");
|
||||
metadata.getElement().add(bundles);
|
||||
|
||||
Bundle[] bs;
|
||||
List<Bundle> bs;
|
||||
try
|
||||
{
|
||||
bs = item.getBundles();
|
||||
@@ -156,31 +164,31 @@ public class ItemUtils
|
||||
|
||||
Element bitstreams = create("bitstreams");
|
||||
bundle.getElement().add(bitstreams);
|
||||
Bitstream[] bits = b.getBitstreams();
|
||||
for (Bitstream bit : bits)
|
||||
List<BundleBitstream> bits = b.getBitstreams();
|
||||
for (BundleBitstream bit : bits)
|
||||
{
|
||||
Element bitstream = create("bitstream");
|
||||
bitstreams.getElement().add(bitstream);
|
||||
String url = "";
|
||||
String bsName = bit.getName();
|
||||
String sid = String.valueOf(bit.getSequenceID());
|
||||
String bsName = bit.getBitstream().getName();
|
||||
String sid = String.valueOf(bit.getBitstream().getSequenceID());
|
||||
String baseUrl = ConfigurationManager.getProperty("oai",
|
||||
"bitstream.baseUrl");
|
||||
String handle = null;
|
||||
// get handle of parent Item of this bitstream, if there
|
||||
// is one:
|
||||
Bundle[] bn = bit.getBundles();
|
||||
if (bn.length > 0)
|
||||
List<BundleBitstream> bn = bit.getBitstream().getBundles();
|
||||
if (!bn.isEmpty())
|
||||
{
|
||||
Item bi[] = bn[0].getItems();
|
||||
if (bi.length > 0)
|
||||
List<Item> bi = bn.get(0).getBundle().getItems();
|
||||
if (!bi.isEmpty())
|
||||
{
|
||||
handle = bi[0].getHandle();
|
||||
handle = bi.get(0).getHandle();
|
||||
}
|
||||
}
|
||||
if (bsName == null)
|
||||
{
|
||||
String ext[] = bit.getFormat().getExtensions();
|
||||
String ext[] = bit.getBitstream().getFormat().getExtensions();
|
||||
bsName = "bitstream_" + sid
|
||||
+ (ext.length > 0 ? ext[0] : "");
|
||||
}
|
||||
@@ -196,11 +204,11 @@ public class ItemUtils
|
||||
url = URLUtils.encode(bsName);
|
||||
}
|
||||
|
||||
String cks = bit.getChecksum();
|
||||
String cka = bit.getChecksumAlgorithm();
|
||||
String oname = bit.getSource();
|
||||
String name = bit.getName();
|
||||
String description = bit.getDescription();
|
||||
String cks = bit.getBitstream().getChecksum();
|
||||
String cka = bit.getBitstream().getChecksumAlgorithm();
|
||||
String oname = bit.getBitstream().getSource();
|
||||
String name = bit.getBitstream().getName();
|
||||
String description = bit.getBitstream().getDescription();
|
||||
|
||||
if (name != null)
|
||||
bitstream.getField().add(
|
||||
@@ -212,17 +220,17 @@ public class ItemUtils
|
||||
bitstream.getField().add(
|
||||
createValue("description", description));
|
||||
bitstream.getField().add(
|
||||
createValue("format", bit.getFormat()
|
||||
createValue("format", bit.getBitstream().getFormat()
|
||||
.getMIMEType()));
|
||||
bitstream.getField().add(
|
||||
createValue("size", "" + bit.getSize()));
|
||||
createValue("size", "" + bit.getBitstream().getSize()));
|
||||
bitstream.getField().add(createValue("url", url));
|
||||
bitstream.getField().add(
|
||||
createValue("checksum", cks));
|
||||
bitstream.getField().add(
|
||||
createValue("checksumAlgorithm", cka));
|
||||
bitstream.getField().add(
|
||||
createValue("sid", bit.getSequenceID()
|
||||
createValue("sid", bit.getBitstream().getSequenceID()
|
||||
+ ""));
|
||||
}
|
||||
}
|
||||
@@ -257,21 +265,21 @@ public class ItemUtils
|
||||
|
||||
// Licensing info
|
||||
Element license = create("license");
|
||||
Bundle[] licBundles;
|
||||
List<Bundle> licBundles;
|
||||
try
|
||||
{
|
||||
licBundles = item.getBundles(Constants.LICENSE_BUNDLE_NAME);
|
||||
if (licBundles.length > 0)
|
||||
licBundles = itemService.getBundles(item, Constants.LICENSE_BUNDLE_NAME);
|
||||
if (!licBundles.isEmpty())
|
||||
{
|
||||
Bundle licBundle = licBundles[0];
|
||||
Bitstream[] licBits = licBundle.getBitstreams();
|
||||
if (licBits.length > 0)
|
||||
Bundle licBundle = licBundles.get(0);
|
||||
List<BundleBitstream> licBits = licBundle.getBitstreams();
|
||||
if (!licBits.isEmpty())
|
||||
{
|
||||
Bitstream licBit = licBits[0];
|
||||
BundleBitstream licBit = licBits.get(0);
|
||||
InputStream in;
|
||||
try
|
||||
{
|
||||
in = licBit.retrieve();
|
||||
in = licBit.getBitstream().retrieve();
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Utils.bufferedCopy(in, out);
|
||||
license.getField().add(
|
||||
@@ -279,15 +287,7 @@ public class ItemUtils
|
||||
Base64Utils.encode(out.toString())));
|
||||
metadata.getElement().add(license);
|
||||
}
|
||||
catch (AuthorizeException e)
|
||||
{
|
||||
log.warn(e.getMessage(), e);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
log.warn(e.getMessage(), e);
|
||||
}
|
||||
catch (SQLException e)
|
||||
catch (AuthorizeException | IOException | SQLException e)
|
||||
{
|
||||
log.warn(e.getMessage(), e);
|
||||
}
|
||||
|
Reference in New Issue
Block a user