Formated source and better method for finding item.

All source is formated by DSpace code formater. Better method for
searching item by metadata field. Changed to SQL select, because is more
faster.
This commit is contained in:
ctu-developers
2014-10-02 12:56:44 +02:00
parent ca7f6b4dd9
commit 52469fb9a1
7 changed files with 1086 additions and 888 deletions

View File

@@ -117,8 +117,8 @@ public class CollectionsResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
log.trace("Collection(id=" + collectionId + ") has been successfully read."); log.trace("Collection(id=" + collectionId + ") has been successfully read.");
return collection; return collection;
@@ -175,10 +175,9 @@ public class CollectionsResource extends Resource
offset = 0; offset = 0;
} }
// dspaceCollections = // TODO Repair it in DSpace api
// org.dspace.content.Collection.findAll(context, limit, offset); // // dspaceCollections = org.dspace.content.Collection.findAll(context, limit, offset);
// Message:java.sql.SQLSyntaxErrorException: ORA-00933: SQL command // Message:java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended
// not properly ended
dspaceCollections = org.dspace.content.Collection.findAll(context); dspaceCollections = org.dspace.content.Collection.findAll(context);
for (int i = offset; i < limit + offset; i++) for (int i = offset; i < limit + offset; i++)
@@ -206,8 +205,8 @@ public class CollectionsResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
log.trace("All collections were successfully read."); log.trace("All collections were successfully read.");
return collections.toArray(new org.dspace.rest.common.Collection[0]); return collections.toArray(new org.dspace.rest.common.Collection[0]);
@@ -294,8 +293,8 @@ public class CollectionsResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
log.trace("All items in collection(id=" + collectionId + ") were successfully read."); log.trace("All items in collection(id=" + collectionId + ") were successfully read.");
return null; return null;
@@ -312,10 +311,10 @@ public class CollectionsResource extends Resource
* If you want to access to collection under logged user into * If you want to access to collection under logged user into
* context. In headers must be set header "rest-dspace-token" * context. In headers must be set header "rest-dspace-token"
* with passed token from login method. * with passed token from login method.
* @return Return status code with item. Return status * @return Return status code with item. Return status (OK)200 if item was
* (OK)200 if item was created. NOT_FOUND(404) if id of collection * created. NOT_FOUND(404) if id of collection does not exists.
* does not exists. UNAUTHORIZED(401) if user have not permission to * UNAUTHORIZED(401) if user have not permission to write items in
* write items in collection. * collection.
* @throws WebApplicationException * @throws WebApplicationException
* It is thrown when was problem with database reading or * It is thrown when was problem with database reading or
* writing (SQLException) or problem with creating * writing (SQLException) or problem with creating
@@ -369,9 +368,9 @@ public class CollectionsResource extends Resource
log.trace("Installing item to collection(id=" + collectionId + ")."); log.trace("Installing item to collection(id=" + collectionId + ").");
dspaceItem = org.dspace.content.InstallItem.installItem(context, workspaceItem); dspaceItem = org.dspace.content.InstallItem.installItem(context, workspaceItem);
returnItem = new Item(dspaceItem, "", context); returnItem = new Item(dspaceItem, "", context);
context.complete(); context.complete();
} }
@@ -400,8 +399,8 @@ public class CollectionsResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
log.info("Item successfully created in collection(id=" + collectionId + "). Item handle=" + returnItem.getHandle()); log.info("Item successfully created in collection(id=" + collectionId + "). Item handle=" + returnItem.getHandle());
return returnItem; return returnItem;
@@ -449,8 +448,7 @@ public class CollectionsResource extends Resource
dspaceCollection.setMetadata("name", collection.getName()); dspaceCollection.setMetadata("name", collection.getName());
dspaceCollection.setLicense(collection.getLicense()); dspaceCollection.setLicense(collection.getLicense());
// dspaceCollection.setLogo(collection.getLogo()); // TODO Add this // dspaceCollection.setLogo(collection.getLogo()); // TODO Add this option.
// option.
dspaceCollection.setMetadata(org.dspace.content.Collection.COPYRIGHT_TEXT, collection.getCopyrightText()); dspaceCollection.setMetadata(org.dspace.content.Collection.COPYRIGHT_TEXT, collection.getCopyrightText());
dspaceCollection.setMetadata(org.dspace.content.Collection.INTRODUCTORY_TEXT, collection.getIntroductoryText()); dspaceCollection.setMetadata(org.dspace.content.Collection.INTRODUCTORY_TEXT, collection.getIntroductoryText());
dspaceCollection.setMetadata(org.dspace.content.Collection.SHORT_DESCRIPTION, collection.getShortDescription()); dspaceCollection.setMetadata(org.dspace.content.Collection.SHORT_DESCRIPTION, collection.getShortDescription());
@@ -475,8 +473,8 @@ public class CollectionsResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
log.info("Collection(id=" + collectionId + ") successfully updated."); log.info("Collection(id=" + collectionId + ") successfully updated.");
return Response.ok().build(); return Response.ok().build();
@@ -545,8 +543,8 @@ public class CollectionsResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
log.info("Collection(id=" + collectionId + ") was successfully deleted."); log.info("Collection(id=" + collectionId + ") was successfully deleted.");
return Response.ok().build(); return Response.ok().build();
@@ -651,66 +649,83 @@ public class CollectionsResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
log.info("Item(id=" + itemId + ") in collection(id=" + collectionId + ") was successfully deleted."); log.info("Item(id=" + itemId + ") in collection(id=" + collectionId + ") was successfully deleted.");
return Response.ok().build(); return Response.ok().build();
} }
/** /**
* Search for first collection with passed name. * Search for first collection with passed name.
* @param name Name of collection. *
* @param name
* Name of collection.
* @param headers * @param headers
* If you want to access to collection under logged user into * If you want to access to collection under logged user into
* context. In headers must be set header "rest-dspace-token" * context. In headers must be set header "rest-dspace-token"
* with passed token from login method. * with passed token from login method.
* @return It returns null if collection was not found. Otherwise returns first founded collection. * @return It returns null if collection was not found. Otherwise returns
* first founded collection.
* @throws WebApplicationException * @throws WebApplicationException
*/ */
@POST @POST
@Path("/find-collection") @Path("/find-collection")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Collection findCollectionByName(String name, @Context HttpHeaders headers) throws WebApplicationException { public Collection findCollectionByName(String name, @Context HttpHeaders headers) throws WebApplicationException
log.info("Searching for first collection with name=" + name + "."); {
org.dspace.core.Context context = null; log.info("Searching for first collection with name=" + name + ".");
Collection collection = null; org.dspace.core.Context context = null;
Collection collection = null;
try { try
context = createContext(getUser(headers));
org.dspace.content.Collection[] dspaceCollections;
dspaceCollections = org.dspace.content.Collection.findAll(context);
for (org.dspace.content.Collection dspaceCollection : dspaceCollections) {
if (AuthorizeManager.authorizeActionBoolean(context, dspaceCollection, org.dspace.core.Constants.READ)) {
if (dspaceCollection.getName().equals(name)) {
collection = new Collection(dspaceCollection, "", context, 100, 0);
break;
}
}
}
context.complete();
} catch (SQLException e) {
processException("Something went wrong while searching for collection(name=" + name + ") from database. Message: " + e, context);
} catch (ContextException e) {
processException("Something went wrong while searching for collection(name=" + name + "), ContextError. Message: " + e.getMessage(), context);
}
finally
{ {
context.abort(); context = createContext(getUser(headers));
} org.dspace.content.Collection[] dspaceCollections;
if (collection == null) { dspaceCollections = org.dspace.content.Collection.findAll(context);
log.info("Collection was not found.");
} else { for (org.dspace.content.Collection dspaceCollection : dspaceCollections)
log.info("Collection was found with id(" + collection.getId() + ")."); {
} if (AuthorizeManager.authorizeActionBoolean(context, dspaceCollection, org.dspace.core.Constants.READ))
return collection; {
} if (dspaceCollection.getName().equals(name))
{
collection = new Collection(dspaceCollection, "", context, 100, 0);
break;
}
}
}
context.complete();
}
catch (SQLException e)
{
processException("Something went wrong while searching for collection(name=" + name + ") from database. Message: "
+ e, context);
}
catch (ContextException e)
{
processException("Something went wrong while searching for collection(name=" + name + "), ContextError. Message: "
+ e.getMessage(), context);
}
finally
{
context.abort();
}
if (collection == null)
{
log.info("Collection was not found.");
}
else
{
log.info("Collection was found with id(" + collection.getId() + ").");
}
return collection;
}
/** /**
* Find collection from DSpace database. It is encapsulation of method * Find collection from DSpace database. It is encapsulation of method
@@ -764,8 +779,8 @@ public class CollectionsResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
return collection; return collection;
} }
} }

View File

@@ -10,7 +10,6 @@ package org.dspace.rest;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
@@ -36,8 +35,6 @@ import org.dspace.core.Constants;
import org.dspace.rest.common.Collection; import org.dspace.rest.common.Collection;
import org.dspace.rest.common.Community; import org.dspace.rest.common.Community;
import org.dspace.rest.exceptions.ContextException; import org.dspace.rest.exceptions.ContextException;
import org.dspace.storage.rdbms.TableRow;
import org.dspace.storage.rdbms.TableRowIterator;
import org.dspace.usage.UsageEvent; import org.dspace.usage.UsageEvent;
/** /**
@@ -108,8 +105,8 @@ public class CommunitiesResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
log.trace("Community(id=" + communityId + ") was successfully read."); log.trace("Community(id=" + communityId + ") was successfully read.");
return community; return community;
@@ -190,8 +187,8 @@ public class CommunitiesResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
return null; return null;
} }
@@ -274,8 +271,8 @@ public class CommunitiesResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
return null; return null;
} }
@@ -362,8 +359,8 @@ public class CommunitiesResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
return null; return null;
} }
@@ -451,8 +448,8 @@ public class CommunitiesResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
return null; return null;
} }
@@ -509,7 +506,7 @@ public class CommunitiesResource extends Resource
dspaceCommunity.setMetadata(org.dspace.content.Community.SHORT_DESCRIPTION, community.getShortDescription()); dspaceCommunity.setMetadata(org.dspace.content.Community.SHORT_DESCRIPTION, community.getShortDescription());
dspaceCommunity.setMetadata(org.dspace.content.Community.SIDEBAR_TEXT, community.getSidebarText()); dspaceCommunity.setMetadata(org.dspace.content.Community.SIDEBAR_TEXT, community.getSidebarText());
dspaceCommunity.update(); dspaceCommunity.update();
retCommunity = new Community(dspaceCommunity, "", context); retCommunity = new Community(dspaceCommunity, "", context);
context.complete(); context.complete();
@@ -528,8 +525,8 @@ public class CommunitiesResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
log.info("Community at top level has been successfully created. Handle:" + retCommunity.getHandle()); log.info("Community at top level has been successfully created. Handle:" + retCommunity.getHandle());
return retCommunity; return retCommunity;
@@ -587,7 +584,7 @@ public class CommunitiesResource extends Resource
dspaceCollection.update(); dspaceCollection.update();
dspaceCommunity.update(); dspaceCommunity.update();
retCollection = new Collection(dspaceCollection, "", context, 100, 0); retCollection = new Collection(dspaceCollection, "", context, 100, 0);
context.complete(); context.complete();
} }
@@ -609,10 +606,11 @@ public class CommunitiesResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
log.info("Collection was successfully added into community(id=" + communityId + "). Collection handle=" + retCollection.getHandle()); log.info("Collection was successfully added into community(id=" + communityId + "). Collection handle="
+ retCollection.getHandle());
return retCollection; return retCollection;
} }
@@ -688,8 +686,8 @@ public class CommunitiesResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
log.info("Subcommunity was successfully added in community(id=" + communityId + ")."); log.info("Subcommunity was successfully added in community(id=" + communityId + ").");
return retCommunity; return retCommunity;
@@ -760,8 +758,8 @@ public class CommunitiesResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
log.info("Community(id=" + communityId + ") has been successfully updated."); log.info("Community(id=" + communityId + ") has been successfully updated.");
return Response.ok().build(); return Response.ok().build();
@@ -825,8 +823,8 @@ public class CommunitiesResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
log.info("Community(id=" + communityId + ") was successfully deleted."); log.info("Community(id=" + communityId + ") was successfully deleted.");
return Response.status(Response.Status.OK).build(); return Response.status(Response.Status.OK).build();
@@ -930,8 +928,8 @@ public class CommunitiesResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
log.info("Collection(id=" + collectionId + ") in community(id=" + communityId + ") was successfully deleted."); log.info("Collection(id=" + collectionId + ") in community(id=" + communityId + ") was successfully deleted.");
return Response.status(Response.Status.OK).build(); return Response.status(Response.Status.OK).build();
@@ -1035,13 +1033,13 @@ public class CommunitiesResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
log.info("Subcommunity(id=" + subcommunityId + ") from community(id=" + parentCommunityId + ") was successfully deleted."); log.info("Subcommunity(id=" + subcommunityId + ") from community(id=" + parentCommunityId + ") was successfully deleted.");
return Response.status(Response.Status.OK).build(); return Response.status(Response.Status.OK).build();
} }
/** /**
* Find community from DSpace database. It is encapsulation of method * Find community from DSpace database. It is encapsulation of method
* org.dspace.content.Community.find with checking if item exist and if user * org.dspace.content.Community.find with checking if item exist and if user
@@ -1094,8 +1092,8 @@ public class CommunitiesResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
return community; return community;
} }
} }

View File

@@ -45,6 +45,8 @@ import org.dspace.rest.common.Bitstream;
import org.dspace.rest.common.Item; import org.dspace.rest.common.Item;
import org.dspace.rest.common.MetadataEntry; import org.dspace.rest.common.MetadataEntry;
import org.dspace.rest.exceptions.ContextException; import org.dspace.rest.exceptions.ContextException;
import org.dspace.storage.rdbms.TableRow;
import org.dspace.storage.rdbms.TableRowIterator;
import org.dspace.usage.UsageEvent; import org.dspace.usage.UsageEvent;
/** /**
@@ -53,8 +55,7 @@ import org.dspace.usage.UsageEvent;
* @author Rostislav Novak (Computing and Information Centre, CTU in Prague) * @author Rostislav Novak (Computing and Information Centre, CTU in Prague)
* *
*/ */
// Every DSpace class used without namespace is from package // Every DSpace class used without namespace is from package org.dspace.rest.common.*. Otherwise namespace is defined.
// org.dspace.rest.common.*. Otherwise namespace is defined.
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Path("/items") @Path("/items")
public class ItemsResource extends Resource public class ItemsResource extends Resource
@@ -121,8 +122,8 @@ public class ItemsResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
return item; return item;
} }
@@ -202,8 +203,8 @@ public class ItemsResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
return null; return null;
} }
@@ -261,8 +262,8 @@ public class ItemsResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
return null; return null;
} }
@@ -334,8 +335,8 @@ public class ItemsResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
return null; return null;
} }
@@ -383,10 +384,8 @@ public class ItemsResource extends Resource
for (MetadataEntry entry : metadata) for (MetadataEntry entry : metadata)
{ {
String data[] = mySplit(entry.getKey()); // Done by my split, // TODO Test with Java split
// because of java String data[] = mySplit(entry.getKey()); // Done by my split, because of java split was not function.
// split was not
// function.
if ((data.length >= 2) && (data.length <= 3)) if ((data.length >= 2) && (data.length <= 3))
{ {
dspaceItem.addMetadata(data[0], data[1], data[2], entry.getLanguage(), entry.getValue()); dspaceItem.addMetadata(data[0], data[1], data[2], entry.getLanguage(), entry.getValue());
@@ -411,8 +410,8 @@ public class ItemsResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
log.info("Metadata to item(id=" + itemId + ") were successfully added."); log.info("Metadata to item(id=" + itemId + ") were successfully added.");
return Response.status(Status.OK).build(); return Response.status(Status.OK).build();
@@ -429,9 +428,9 @@ public class ItemsResource extends Resource
* If you want to access to item under logged user into context. * If you want to access to item under logged user into context.
* In headers must be set header "rest-dspace-token" with passed * In headers must be set header "rest-dspace-token" with passed
* token from login method. * token from login method.
* @return Returns bitstream with status code OK(200). If id of item * @return Returns bitstream with status code OK(200). If id of item is
* is invalid , it returns status code NOT_FOUND(404). If user is * invalid , it returns status code NOT_FOUND(404). If user is not
* not allowed to write to item, UNAUTHORIZED(401). * allowed to write to item, UNAUTHORIZED(401).
* @throws WebApplicationException * @throws WebApplicationException
* It is thrown by these exceptions: SQLException, when was * It is thrown by these exceptions: SQLException, when was
* problem with reading/writing from/to database. * problem with reading/writing from/to database.
@@ -442,127 +441,151 @@ public class ItemsResource extends Resource
* DSpace. * DSpace.
*/ */
// TODO Add option to add bitsream by URI.(for very big files) // TODO Add option to add bitsream by URI.(for very big files)
@POST @POST
@Path("/{item_id}/bitstreams") @Path("/{item_id}/bitstreams")
public Bitstream addItemBitstream( public Bitstream addItemBitstream(@PathParam("item_id") Integer itemId, InputStream inputStream,
@PathParam("item_id") Integer itemId, InputStream inputStream, @QueryParam("name") String name, @QueryParam("description") String description,
@QueryParam("name") String name, @QueryParam("description") String description, @QueryParam("groupId") Integer groupId, @QueryParam("year") Integer year, @QueryParam("month") Integer month,
@QueryParam("groupId") Integer groupId, @QueryParam("year") Integer year, @QueryParam("month") Integer month, @QueryParam("day") Integer day, @QueryParam("userIP") String user_ip, @QueryParam("userAgent") String user_agent,
@QueryParam("day") Integer day, @QueryParam("userIP") String user_ip, @QueryParam("userAgent") String user_agent, @QueryParam("xforwarderfor") String xforwarderfor, @Context HttpHeaders headers, @Context HttpServletRequest request)
@QueryParam("xforwarderfor") String xforwarderfor, @Context HttpHeaders headers, @Context HttpServletRequest request) throws WebApplicationException
throws WebApplicationException { {
log.info("Adding bitstream to item(id=" + itemId + ")."); log.info("Adding bitstream to item(id=" + itemId + ").");
org.dspace.core.Context context = null; org.dspace.core.Context context = null;
Bitstream bitstream = null; Bitstream bitstream = null;
try { try
context = createContext(getUser(headers));
org.dspace.content.Item dspaceItem = findItem(context, itemId, org.dspace.core.Constants.WRITE);
writeStats(Constants.ITEM, dspaceItem, UsageEvent.Action.UPDATE, user_ip, user_agent, xforwarderfor, headers, request);
// Is better to add bitstream to ORIGINAL bundle or to item own?
log.trace("Creating bitstream in item.");
org.dspace.content.Bundle bundle = null;
org.dspace.content.Bitstream dspaceBitstream = null;
if ((dspaceItem.getBundles().length == 0) || (dspaceItem.getBundles() == null)) {
log.trace("Creating bundle in item.");
dspaceBitstream = dspaceItem.createSingleBitstream(inputStream);
//bundle = dspaceItem.getBundles()[0];
//bundle.setName(dspaceItem.getID() + "-bundle");
//bundle.update();
} else {
log.trace("Getting bundle from item.");
bundle = dspaceItem.getBundles()[0];
dspaceBitstream = bundle.createBitstream(inputStream);
}
dspaceBitstream.setSource("DSpace Rest api");
// Set bitstream name and description
if (name != null) {
if (BitstreamResource.getMimeType(name) == null) {
dspaceBitstream.setFormat(BitstreamFormat.findUnknown(context));
} else {
dspaceBitstream.setFormat(BitstreamFormat.findByMIMEType(context, BitstreamResource.getMimeType(name)));
}
dspaceBitstream.setName(name);
}
if (description != null) {
dspaceBitstream.setDescription(description);
}
dspaceBitstream.update();
// Create policy for bitstream
if (groupId != null) {
Bundle[] bundles = dspaceBitstream.getBundles();
for (Bundle dspaceBundle : bundles) {
List<org.dspace.authorize.ResourcePolicy> bitstreamsPolicies = dspaceBundle.getBitstreamPolicies();
// Remove default bitstream policies
List<org.dspace.authorize.ResourcePolicy> policiesToRemove = new ArrayList<org.dspace.authorize.ResourcePolicy>();
for (org.dspace.authorize.ResourcePolicy policy : bitstreamsPolicies) {
if (policy.getResourceID() == dspaceBitstream.getID()) {
policiesToRemove.add(policy);
}
}
for (org.dspace.authorize.ResourcePolicy policy : policiesToRemove) {
bitstreamsPolicies.remove(policy);
}
org.dspace.authorize.ResourcePolicy dspacePolicy = org.dspace.authorize.ResourcePolicy.create(context);
dspacePolicy.setAction(org.dspace.core.Constants.READ);
dspacePolicy.setGroup(Group.find(context, groupId));
dspacePolicy.setResourceID(dspaceBitstream.getID());
dspacePolicy.setResource(dspaceBitstream);
dspacePolicy.setResourceType(org.dspace.core.Constants.BITSTREAM);
if ((year != null) || (month != null) || (day != null)) {
Date date = new Date();
if (year != null) {
date.setYear(year-1900);
}
if (month != null) {
date.setMonth(month-1);
}
if (day != null) {
date.setDate(day);
}
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
dspacePolicy.setStartDate(date);
}
dspacePolicy.update();
bitstreamsPolicies.add(dspacePolicy);
dspaceBundle.replaceAllBitstreamPolicies(bitstreamsPolicies);
dspaceBundle.update();
}
}
dspaceBitstream = org.dspace.content.Bitstream.find(context, dspaceBitstream.getID());
bitstream = new Bitstream(dspaceBitstream, "");
context.complete();
} catch (SQLException e) {
processException("Could not create bitstream in item(id=" + itemId + "), SQLException. Message: " + e, context);
} catch (AuthorizeException e) {
processException("Could not create bitstream in item(id=" + itemId + "), AuthorizeException. Message: " + e, context);
} catch (IOException e) {
processException("Could not create bitstream in item(id=" + itemId + "), IOException Message: " + e, context);
} catch (ContextException e) {
processException("Could not create bitstream in item(id=" + itemId + "), ContextException Message: " + e.getMessage(), context);
}
finally
{ {
context.abort(); context = createContext(getUser(headers));
} org.dspace.content.Item dspaceItem = findItem(context, itemId, org.dspace.core.Constants.WRITE);
log.info("Bitstream(id=" + bitstream.getId() + ") was successfully added into item(id=" + itemId + ")."); writeStats(Constants.ITEM, dspaceItem, UsageEvent.Action.UPDATE, user_ip, user_agent, xforwarderfor, headers, request);
return bitstream;
// Is better to add bitstream to ORIGINAL bundle or to item own?
log.trace("Creating bitstream in item.");
org.dspace.content.Bundle bundle = null;
org.dspace.content.Bitstream dspaceBitstream = null;
if ((dspaceItem.getBundles().length == 0) || (dspaceItem.getBundles() == null))
{
log.trace("Creating bundle in item.");
dspaceBitstream = dspaceItem.createSingleBitstream(inputStream);
}
else
{
log.trace("Getting bundle from item.");
bundle = dspaceItem.getBundles()[0];
dspaceBitstream = bundle.createBitstream(inputStream);
}
dspaceBitstream.setSource("DSpace Rest api");
// Set bitstream name and description
if (name != null)
{
if (BitstreamResource.getMimeType(name) == null)
{
dspaceBitstream.setFormat(BitstreamFormat.findUnknown(context));
}
else
{
dspaceBitstream.setFormat(BitstreamFormat.findByMIMEType(context, BitstreamResource.getMimeType(name)));
}
dspaceBitstream.setName(name);
}
if (description != null)
{
dspaceBitstream.setDescription(description);
}
dspaceBitstream.update();
// Create policy for bitstream
if (groupId != null)
{
Bundle[] bundles = dspaceBitstream.getBundles();
for (Bundle dspaceBundle : bundles)
{
List<org.dspace.authorize.ResourcePolicy> bitstreamsPolicies = dspaceBundle.getBitstreamPolicies();
// Remove default bitstream policies
List<org.dspace.authorize.ResourcePolicy> policiesToRemove = new ArrayList<org.dspace.authorize.ResourcePolicy>();
for (org.dspace.authorize.ResourcePolicy policy : bitstreamsPolicies)
{
if (policy.getResourceID() == dspaceBitstream.getID())
{
policiesToRemove.add(policy);
}
}
for (org.dspace.authorize.ResourcePolicy policy : policiesToRemove)
{
bitstreamsPolicies.remove(policy);
}
org.dspace.authorize.ResourcePolicy dspacePolicy = org.dspace.authorize.ResourcePolicy.create(context);
dspacePolicy.setAction(org.dspace.core.Constants.READ);
dspacePolicy.setGroup(Group.find(context, groupId));
dspacePolicy.setResourceID(dspaceBitstream.getID());
dspacePolicy.setResource(dspaceBitstream);
dspacePolicy.setResourceType(org.dspace.core.Constants.BITSTREAM);
if ((year != null) || (month != null) || (day != null))
{
Date date = new Date();
if (year != null)
{
date.setYear(year - 1900);
}
if (month != null)
{
date.setMonth(month - 1);
}
if (day != null)
{
date.setDate(day);
}
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
dspacePolicy.setStartDate(date);
}
dspacePolicy.update();
bitstreamsPolicies.add(dspacePolicy);
dspaceBundle.replaceAllBitstreamPolicies(bitstreamsPolicies);
dspaceBundle.update();
}
}
dspaceBitstream = org.dspace.content.Bitstream.find(context, dspaceBitstream.getID());
bitstream = new Bitstream(dspaceBitstream, "");
context.complete();
}
catch (SQLException e)
{
processException("Could not create bitstream in item(id=" + itemId + "), SQLException. Message: " + e, context);
}
catch (AuthorizeException e)
{
processException("Could not create bitstream in item(id=" + itemId + "), AuthorizeException. Message: " + e, context);
}
catch (IOException e)
{
processException("Could not create bitstream in item(id=" + itemId + "), IOException Message: " + e, context);
}
catch (ContextException e)
{
processException(
"Could not create bitstream in item(id=" + itemId + "), ContextException Message: " + e.getMessage(), context);
}
finally
{
context.abort();
}
log.info("Bitstream(id=" + bitstream.getId() + ") was successfully added into item(id=" + itemId + ").");
return bitstream;
} }
/** /**
@@ -644,8 +667,8 @@ public class ItemsResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
log.info("Metadata of item(id=" + itemId + ") were successfully updated."); log.info("Metadata of item(id=" + itemId + ") were successfully updated.");
return Response.status(Status.OK).build(); return Response.status(Status.OK).build();
@@ -713,8 +736,8 @@ public class ItemsResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
log.info("Item(id=" + itemId + ") was successfully deleted."); log.info("Item(id=" + itemId + ") was successfully deleted.");
return Response.status(Status.OK).build(); return Response.status(Status.OK).build();
@@ -757,8 +780,7 @@ public class ItemsResource extends Resource
writeStats(Constants.ITEM, dspaceItem, UsageEvent.Action.UPDATE, user_ip, user_agent, xforwarderfor, headers, request); writeStats(Constants.ITEM, dspaceItem, UsageEvent.Action.UPDATE, user_ip, user_agent, xforwarderfor, headers, request);
log.trace("Deleting metadata."); log.trace("Deleting metadata.");
// TODO Rewrite without deprecated object. // TODO Rewrite without deprecated object. Leave there only generated metadata.
// Leave there only generated metadata.
DCValue[] value = dspaceItem.getMetadata("dc", "date", "accessioned", org.dspace.content.Item.ANY); DCValue[] value = dspaceItem.getMetadata("dc", "date", "accessioned", org.dspace.content.Item.ANY);
DCValue[] value2 = dspaceItem.getMetadata("dc", "date", "available", org.dspace.content.Item.ANY); DCValue[] value2 = dspaceItem.getMetadata("dc", "date", "available", org.dspace.content.Item.ANY);
DCValue[] value3 = dspaceItem.getMetadata("dc", "identifier", "uri", org.dspace.content.Item.ANY); DCValue[] value3 = dspaceItem.getMetadata("dc", "identifier", "uri", org.dspace.content.Item.ANY);
@@ -792,8 +814,8 @@ public class ItemsResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
log.info("Item(id=" + itemId + ") metadata were successfully deleted."); log.info("Item(id=" + itemId + ") metadata were successfully deleted.");
return Response.status(Status.OK).build(); return Response.status(Status.OK).build();
@@ -888,8 +910,8 @@ public class ItemsResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
log.info("Bitstream(id=" + bitstreamId + ") from item(id=" + itemId + ") was successfuly deleted ."); log.info("Bitstream(id=" + bitstreamId + ") from item(id=" + itemId + ") was successfuly deleted .");
return Response.status(Status.OK).build(); return Response.status(Status.OK).build();
@@ -930,7 +952,6 @@ public class ItemsResource extends Resource
org.dspace.core.Context context = null; org.dspace.core.Context context = null;
List<Item> items = new ArrayList<Item>(); List<Item> items = new ArrayList<Item>();
ItemIterator itemIterator = null;
String[] metadata = mySplit(metadataEntry.getKey()); String[] metadata = mySplit(metadataEntry.getKey());
try try
@@ -940,17 +961,21 @@ public class ItemsResource extends Resource
// TODO Repair, it ends by error: // TODO Repair, it ends by error:
// "java.sql.SQLSyntaxErrorException: ORA-00932: inconsistent datatypes: expected - got CLOB" // "java.sql.SQLSyntaxErrorException: ORA-00932: inconsistent datatypes: expected - got CLOB"
/* /*
* if (metadata.length == 3) { itemIterator = * if (metadata.length == 3){
* org.dspace.content.Item.findByMetadataField(context, metadata[0], * itemIterator = org.dspace.content.Item.findByMetadataField(context, metadata[0],
* metadata[1], metadata[2], value); } else if (metadata.length == * metadata[1], metadata[2], value);
* 2) { itemIterator = * } else if (metadata.length == 2){
* org.dspace.content.Item.findByMetadataField(context, metadata[0], * itemIterator = org.dspace.content.Item.findByMetadataField(context, metadata[0],
* metadata[1], null, value); } else { context.abort(); * metadata[1], null, value);
* log.error("Finding failed, bad metadata key."); throw new * } else {
* WebApplicationException(Response.Status.NOT_FOUND); } * context.abort();
* log.error("Finding failed, bad metadata key.");
* throw new WebApplicationException(Response.Status.NOT_FOUND);
* }
* *
* if (itemIterator.hasNext()) { item = new * if (itemIterator.hasNext()) {
* Item(itemIterator.next(), "", context); } * item = new Item(itemIterator.next(), "", context);
* }
*/ */
// Must used own style. // Must used own style.
@@ -961,25 +986,37 @@ public class ItemsResource extends Resource
throw new WebApplicationException(Response.Status.NOT_FOUND); throw new WebApplicationException(Response.Status.NOT_FOUND);
} }
org.dspace.content.Item dspaceItem = null; String sql = "SELECT ITEM_ID, TEXT_VALUE, TEXT_LANG, SHORT_ID, ELEMENT, QUALIFIER " +
itemIterator = org.dspace.content.Item.findAll(context); "FROM METADATAVALUE " +
org.dspace.content.DCValue[] dspaceValue = null; "JOIN METADATAFIELDREGISTRY ON METADATAVALUE.METADATA_FIELD_ID = METADATAFIELDREGISTRY.METADATA_FIELD_ID " +
while (itemIterator.hasNext()) "JOIN METADATASCHEMAREGISTRY ON METADATAFIELDREGISTRY.METADATA_SCHEMA_ID = METADATASCHEMAREGISTRY.METADATA_SCHEMA_ID " +
"WHERE " +
"SHORT_ID='" + metadata[0] + "' AND " +
"ELEMENT='" + metadata[1] + "' AND ";
if (metadata.length > 3)
{ {
dspaceItem = itemIterator.next(); sql += "QUALIFIER='" + metadata[2] + "' AND ";
}
sql += "dbms_lob.substr(TEXT_VALUE, 40)='" + metadataEntry.getValue() + "' AND ";
if (metadataEntry.getLanguage() != null)
{
sql += "TEXT_LANG='" + metadataEntry.getLanguage() + "'";
}
else
{
sql += "TEXT_LANG is null";
}
dspaceValue = dspaceItem.getMetadata(metadata[0], metadata[1], metadata[2], org.dspace.content.Item.ANY); TableRowIterator iterator = org.dspace.storage.rdbms.DatabaseManager.query(context, sql);
while (iterator.hasNext())
for (DCValue dcvalue : dspaceValue) {
{ TableRow row = iterator.next();
if (dcvalue.value.equals(metadataEntry.getValue())) org.dspace.content.Item dspaceItem = this.findItem(context, row.getIntColumn("ITEM_ID"),
{ org.dspace.core.Constants.READ);
items.add(new Item(dspaceItem, expnad, context)); Item item = new Item(dspaceItem, "", context);
writeStats(Constants.ITEM, dspaceItem, UsageEvent.Action.VIEW, user_ip, user_agent, xforwarderfor, writeStats(Constants.ITEM, dspaceItem, UsageEvent.Action.VIEW, user_ip, user_agent, xforwarderfor, headers,
headers, request); request);
break; items.add(item);
}
}
} }
context.complete(); context.complete();
@@ -988,21 +1025,15 @@ public class ItemsResource extends Resource
catch (SQLException e) catch (SQLException e)
{ {
processException("Something get wrong while finding item. SQLException, Message: " + e, context); processException("Something get wrong while finding item. SQLException, Message: " + e, context);
}/* }
* catch (AuthorizeException e) { processException(
* "Something get wrong while finding item. AuthorizeException, Message:"
* + e, context); } catch (IOException e) { processException(
* "Something get wrong while finding item. IOEception Message:" + e,
* context); }
*/
catch (ContextException e) catch (ContextException e)
{ {
processException("Context error:" + e.getMessage(), context); processException("Context error:" + e.getMessage(), context);
} }
finally finally
{ {
context.abort(); context.abort();
} }
if (items.size() == 0) if (items.size() == 0)
{ {
@@ -1067,8 +1098,8 @@ public class ItemsResource extends Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
return item; return item;
} }
} }

View File

@@ -85,8 +85,8 @@ public class Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
} }
/** /**
@@ -148,8 +148,8 @@ public class Resource
} }
finally finally
{ {
context.abort(); context.abort();
} }
} }

View File

@@ -75,6 +75,7 @@ public class RestIndex {
"<li>GET /collections/{collectionId} - Return collection with id.</li>" + "<li>GET /collections/{collectionId} - Return collection with id.</li>" +
"<li>GET /collections/{collectionId}/items - Return all items of collection.</li>" + "<li>GET /collections/{collectionId}/items - Return all items of collection.</li>" +
"<li>POST /collections/{collectionId}/items - Create posted item in collection.</li>" + "<li>POST /collections/{collectionId}/items - Create posted item in collection.</li>" +
"<li>POST /collections/find-collection - Find collection by passed name.</li>" +
"<li>PUT /collections/{collectionId} </li> - Update collection. You muset post collection." + "<li>PUT /collections/{collectionId} </li> - Update collection. You muset post collection." +
"<li>DELETE /collections/{collectionId} - Delete collection from DSpace.</li>" + "<li>DELETE /collections/{collectionId} - Delete collection from DSpace.</li>" +
"<li>DELETE /collections/{collectionId}/items/{itemId} - Delete item in collection. </li>" + "<li>DELETE /collections/{collectionId}/items/{itemId} - Delete item in collection. </li>" +
@@ -97,10 +98,13 @@ public class RestIndex {
"<ul>" + "<ul>" +
"<li>GET /bitstreams - Return all bitstreams in DSpace.</li>" + "<li>GET /bitstreams - Return all bitstreams in DSpace.</li>" +
"<li>GET /bitstreams/{bitstream id} - Return bitstream.</li>" + "<li>GET /bitstreams/{bitstream id} - Return bitstream.</li>" +
"<li>GET /bitstreams/{bitstream id}/policy - Return bitstream policies.</li>" +
"<li>POST /bitstreams/{bitstream id}/retrieve - Return data of bitstream.</li>" + "<li>POST /bitstreams/{bitstream id}/retrieve - Return data of bitstream.</li>" +
"<li>POST /bitstreams/{bitstream id}/policy - Add policy to item.</li>" +
"<li>PUT /bitstreams/{bitstream id}/data - Update data of bitstream.</li>" + "<li>PUT /bitstreams/{bitstream id}/data - Update data of bitstream.</li>" +
"<li>PUT /bitstreams/{bitstream id} - Update metadata of bitstream.</li>" + "<li>PUT /bitstreams/{bitstream id} - Update metadata of bitstream.</li>" +
"<li>DELETE /bitstreams/{bitstream id} - Delete bitstream from DSpace.</li>" + "<li>DELETE /bitstreams/{bitstream id} - Delete bitstream from DSpace.</li>" +
"<li>DELETE /bitstreams/{bitstream id}/policy/{policy_id} - Delete bitstream policy.</li>" +
"</ul>" + "</ul>" +
"</body></html> "; "</body></html> ";
} }

View File

@@ -95,8 +95,8 @@ public class TokenHolder
} }
finally finally
{ {
context.abort(); context.abort();
} }
return token; return token;
} }