Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ufal/be-find-item-by-handle #451

Merged
merged 2 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.content.MetadataField;
import org.dspace.content.dao.clarin.ClarinItemDAO;
import org.dspace.content.service.CollectionService;
import org.dspace.content.service.clarin.ClarinItemService;
Expand Down Expand Up @@ -45,6 +46,11 @@ public List<Item> findByBitstreamUUID(Context context, UUID bitstreamUUID) throw
return clarinItemDAO.findByBitstreamUUID(context, bitstreamUUID);
}

@Override
public List<Item> findByHandle(Context context, MetadataField metadataField, String handle) throws SQLException {
return clarinItemDAO.findByHandle(context, metadataField, handle);
}

@Override
public Community getOwningCommunity(Context context, DSpaceObject dso) {
if (Objects.isNull(dso)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
import java.util.UUID;

import org.dspace.content.Item;
import org.dspace.content.MetadataField;
import org.dspace.core.Context;

public interface ClarinItemDAO {
List<Item> findByBitstreamUUID(Context context, UUID bitstreamUUID) throws SQLException;

List<Item> findByHandle(Context context, MetadataField metadataField, String handle) throws SQLException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import javax.persistence.Query;

import org.dspace.content.Item;
import org.dspace.content.MetadataField;
import org.dspace.content.dao.clarin.ClarinItemDAO;
import org.dspace.core.AbstractHibernateDAO;
import org.dspace.core.Context;
Expand All @@ -29,4 +30,16 @@ public List<Item> findByBitstreamUUID(Context context, UUID bitstreamUUID) throw

return list(query);
}

@Override
public List<Item> findByHandle(Context context, MetadataField metadataField, String handle) throws SQLException {
Query query = createQuery(context, "SELECT item FROM Item as item join item.metadata metadata " +
"WHERE metadata.value = :handle AND metadata.metadataField = :metadata_field");

query.setParameter("handle", handle);
query.setParameter("metadata_field", metadataField);
query.setHint("org.hibernate.cacheable", Boolean.TRUE);

return list(query);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.content.MetadataField;
import org.dspace.core.Context;

/**
Expand All @@ -33,6 +34,15 @@ public interface ClarinItemService {
*/
List<Item> findByBitstreamUUID(Context context, UUID bitstreamUUID) throws SQLException;

/**
* Find Item by the Handle
* @param context DSpace context object
* @param handle String of the finding item
* @return found Item or null
* @throws SQLException database error
*/
List<Item> findByHandle(Context context, MetadataField metadataField, String handle) throws SQLException;

/**
* Get item/collection/community's owning community
* @param context DSpace context object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
import org.dspace.content.Bundle;
import org.dspace.content.Collection;
import org.dspace.content.Item;
import org.dspace.content.MetadataField;
import org.dspace.content.Relationship;
import org.dspace.content.RelationshipType;
import org.dspace.content.WorkspaceItem;
import org.dspace.content.service.BundleService;
import org.dspace.content.service.CollectionService;
import org.dspace.content.service.InstallItemService;
import org.dspace.content.service.ItemService;
import org.dspace.content.service.MetadataFieldService;
import org.dspace.content.service.RelationshipService;
import org.dspace.content.service.RelationshipTypeService;
import org.dspace.content.service.WorkspaceItemService;
Expand Down Expand Up @@ -103,6 +105,9 @@ public class ItemRestRepository extends DSpaceObjectRestRepository<Item, ItemRes
@Autowired
private ClarinItemService clarinItemService;

@Autowired
private MetadataFieldService metadataFieldService;

@Autowired
private SolrOAIReindexer solrOAIReindexer;

Expand Down Expand Up @@ -392,4 +397,19 @@ public Page<ItemRest> findByValue(@Parameter(value = "bitstreamUUID", required =

return converter.toRestPage(itemList, pageable, utils.obtainProjection());
}

@SearchRestMethod(name = "byHandle")
public Page<ItemRest> findByHandle(@Parameter(value = "handle", required = true) String
handle,
Pageable pageable) throws SQLException {
Context context = obtainContext();
MetadataField metadataField = metadataFieldService.findByString(context, "dc.identifier.uri", '.');
if (Objects.isNull(metadataField)) {
throw new UnprocessableEntityException("Cannot get item by handle because the metadata field ID for " +
"`dc.identifier.uri` wasn't found.");
}

List<Item> itemList = clarinItemService.findByHandle(context, metadataField, handle);
return converter.toRestPage(itemList, pageable, utils.obtainProjection());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4767,4 +4767,40 @@ public void submitterShouldSeeLocalNoteMetadata() throws Exception {
.andExpect(jsonPath("$", existDescriptionProvenanceMetadataMatcher));
}

/**
* Should find Item by the full handle identifier.
*/
@Test
public void searchByHandle() throws Exception {
context.turnOffAuthorisationSystem();
//** GIVEN **
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
.withName("Sub Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
Collection col2 = CollectionBuilder.createCollection(context, child1).withName("Collection 2").build();

Item publicItem1 = ItemBuilder.createItem(context, col1)
.withTitle("Public item 1")
.withIssueDate("2017-10-17")
.withAuthor("Smith, Donald").withAuthor("Doe, John")
.withSubject("ExtraEntry")
.build();
context.restoreAuthSystemState();

String handlePrefix = configurationService.getProperty("handle.canonical.prefix");
String fullHandleIdentifier = handlePrefix + publicItem1.getHandle();
String token = getAuthToken(admin.getEmail(), password);
getClient(token).perform(get("/api/core/items/search/byHandle")
.param("handle", fullHandleIdentifier))
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$._embedded.items", Matchers.containsInRelativeOrder(
ItemMatcher.matchItemProperties(publicItem1)
)));
}

}
Loading