Merge pull request #19 from atmire/w2p-70401_count-queries-circular-entities

W2p 70401 count queries circular entities
This commit is contained in:
benbosman
2020-04-17 12:02:31 +02:00
committed by GitHub
6 changed files with 88 additions and 15 deletions

View File

@@ -537,9 +537,9 @@ public class RelationshipServiceImpl implements RelationshipService {
} }
@Override @Override
public int countByItemAndRelationshipType(Context context, Item item, RelationshipType relationshipType) public int countByItemAndRelationshipType(Context context, Item item, RelationshipType relationshipType,
throws SQLException { boolean isLeft) throws SQLException {
return relationshipDAO.countByItemAndRelationshipType(context, item, relationshipType); return relationshipDAO.countByItemAndRelationshipType(context, item, relationshipType, isLeft);
} }
@Override @Override

View File

@@ -189,15 +189,17 @@ public interface RelationshipDAO extends GenericDAO<Relationship> {
int countByItem(Context context, Item item) throws SQLException; int countByItem(Context context, Item item) throws SQLException;
/** /**
* Count total number of relationships (rows in relationship table) by an item and a relationship type * Count total number of relationships (rows in relationship table) by an item and a relationship type and a boolean
* indicating whether the item should be the leftItem or the rightItem
* *
* @param context context * @param context context
* @param relationshipType relationship type to filter by * @param relationshipType relationship type to filter by
* @param item item to filter by * @param item item to filter by
* @param isLeft Indicating whether the counted Relationships should have the given Item on the left side or not
* @return total count * @return total count
* @throws SQLException if database error * @throws SQLException if database error
*/ */
int countByItemAndRelationshipType(Context context, Item item, RelationshipType relationshipType) int countByItemAndRelationshipType(Context context, Item item, RelationshipType relationshipType, boolean isLeft)
throws SQLException; throws SQLException;
/** /**

View File

@@ -201,18 +201,24 @@ public class RelationshipDAOImpl extends AbstractHibernateDAO<Relationship> impl
} }
@Override @Override
public int countByItemAndRelationshipType(Context context, Item item, RelationshipType relationshipType) public int countByItemAndRelationshipType(Context context, Item item, RelationshipType relationshipType,
throws SQLException { boolean isLeft) throws SQLException {
CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context); CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context);
CriteriaQuery criteriaQuery = getCriteriaQuery(criteriaBuilder, Relationship.class); CriteriaQuery criteriaQuery = getCriteriaQuery(criteriaBuilder, Relationship.class);
Root<Relationship> relationshipRoot = criteriaQuery.from(Relationship.class); Root<Relationship> relationshipRoot = criteriaQuery.from(Relationship.class);
criteriaQuery.select(relationshipRoot); criteriaQuery.select(relationshipRoot);
if (isLeft) {
criteriaQuery criteriaQuery
.where(criteriaBuilder.equal(relationshipRoot.get(Relationship_.relationshipType), .where(criteriaBuilder.equal(relationshipRoot.get(Relationship_.relationshipType),
relationshipType), criteriaBuilder.or relationshipType),
(criteriaBuilder.equal(relationshipRoot.get(Relationship_.leftItem), item), criteriaBuilder.equal(relationshipRoot.get(Relationship_.leftItem), item));
criteriaBuilder.equal(relationshipRoot.get(Relationship_.rightItem), item))); } else {
criteriaQuery
.where(criteriaBuilder.equal(relationshipRoot.get(Relationship_.relationshipType),
relationshipType),
criteriaBuilder.equal(relationshipRoot.get(Relationship_.rightItem), item));
}
return count(context, criteriaQuery, criteriaBuilder, relationshipRoot); return count(context, criteriaQuery, criteriaBuilder, relationshipRoot);
} }

View File

@@ -283,14 +283,16 @@ public interface RelationshipService extends DSpaceCRUDService<Relationship> {
int countByItem(Context context, Item item) throws SQLException; int countByItem(Context context, Item item) throws SQLException;
/** /**
* Count total number of relationships (rows in relationship table) by a relationship type * Count total number of relationships (rows in relationship table) by a relationship type and a boolean indicating
* whether the relationship should contain the item on the left side or not
* *
* @param context context * @param context context
* @param relationshipType relationship type to filter by * @param relationshipType relationship type to filter by
* @return total count * @param isLeft Indicating whether the counted Relationships should have the given Item on the left side or not
* @return total count with the given parameters
* @throws SQLException if database error * @throws SQLException if database error
*/ */
int countByItemAndRelationshipType(Context context, Item item, RelationshipType relationshipType) int countByItemAndRelationshipType(Context context, Item item, RelationshipType relationshipType, boolean isLeft)
throws SQLException; throws SQLException;
/** /**

View File

@@ -354,7 +354,7 @@ public class RelationshipRestRepository extends DSpaceRestRepository<Relationshi
if (relationshipType.getLeftwardType().equalsIgnoreCase(label)) { if (relationshipType.getLeftwardType().equalsIgnoreCase(label)) {
isLeft = true; isLeft = true;
} }
total += relationshipService.countByItemAndRelationshipType(context, item, relationshipType); total += relationshipService.countByItemAndRelationshipType(context, item, relationshipType, isLeft);
relationships.addAll(relationshipService.findByItemAndRelationshipType(context, item, relationshipType, relationships.addAll(relationshipService.findByItemAndRelationshipType(context, item, relationshipType,
isLeft, pageable.getPageSize(), Math.toIntExact(pageable.getOffset()))); isLeft, pageable.getPageSize(), Math.toIntExact(pageable.getOffset())));
} }

View File

@@ -97,6 +97,7 @@ public class RelationshipRestRepositoryIT extends AbstractEntityIntegrationTest
private Item orgUnit1; private Item orgUnit1;
private Item orgUnit2; private Item orgUnit2;
private Item orgUnit3;
private Item project1; private Item project1;
private Item publication1; private Item publication1;
@@ -175,6 +176,13 @@ public class RelationshipRestRepositoryIT extends AbstractEntityIntegrationTest
.withRelationshipType("OrgUnit") .withRelationshipType("OrgUnit")
.build(); .build();
orgUnit3 = ItemBuilder.createItem(context, col3)
.withTitle("OrgUnit3")
.withAuthor("Test, Testy")
.withIssueDate("2015-02-01")
.withRelationshipType("OrgUnit")
.build();
project1 = ItemBuilder.createItem(context, col3) project1 = ItemBuilder.createItem(context, col3)
.withTitle("Project1") .withTitle("Project1")
.withAuthor("Testy, TEst") .withAuthor("Testy, TEst")
@@ -2566,5 +2574,60 @@ public class RelationshipRestRepositoryIT extends AbstractEntityIntegrationTest
}
@Test
public void orgUnitFindByLabelParentChildOfCountTest() throws Exception {
context.turnOffAuthorisationSystem();
EntityType orgUnit = entityTypeService.findByEntityType(context, "OrgUnit");
RelationshipType isParentOrgUnitOf = relationshipTypeService
.findbyTypesAndTypeName(context, orgUnit, orgUnit, "isParentOrgUnitOf", "isChildOrgUnitOf");
MetadataSchema metadataSchema = metadataSchemaService.find(context, "relation");
MetadataFieldBuilder.createMetadataField(context, metadataSchema, "isParentOrgUnitOf", null, null).build();
MetadataFieldBuilder.createMetadataField(context, metadataSchema, "isChildOrgUnitOf", null, null).build();
String adminToken = getAuthToken(admin.getEmail(), password);
context.restoreAuthSystemState();
// Here we create our first Relationship to the Publication to give it a dc.contributor.author virtual
// metadata field.
getClient(adminToken).perform(post("/api/core/relationships")
.param("relationshipType",
isParentOrgUnitOf.getID().toString())
.contentType(MediaType.parseMediaType
(org.springframework.data.rest.webmvc.RestMediaTypes
.TEXT_URI_LIST_VALUE))
.content(
"https://localhost:8080/server/api/core/items/" + orgUnit1
.getID() + "\n" +
"https://localhost:8080/server/api/core/items" +
"/" + orgUnit2
.getID()))
.andExpect(status().isCreated());
getClient(adminToken).perform(post("/api/core/relationships")
.param("relationshipType",
isParentOrgUnitOf.getID().toString())
.contentType(MediaType.parseMediaType
(org.springframework.data.rest.webmvc.RestMediaTypes
.TEXT_URI_LIST_VALUE))
.content(
"https://localhost:8080/server/api/core/items/" + orgUnit2
.getID() + "\n" +
"https://localhost:8080/server/api/core/items" +
"/" + orgUnit3
.getID()))
.andExpect(status().isCreated());
getClient().perform(get("/api/core/relationships/search/byLabel")
.param("label", "isChildOrgUnitOf")
.param("dso", String.valueOf(orgUnit2.getID()))
.param("page", "0")
.param("size", "1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.page", PageMatcher.pageEntryWithTotalPagesAndElements(0, 1, 1, 1)));
} }
} }