88049: Add latest_version_status column to relationship table

This commit is contained in:
Bruno Roemers
2022-03-01 02:30:41 +01:00
parent 122924af82
commit d0aab90ffc
7 changed files with 315 additions and 7 deletions

View File

@@ -89,6 +89,15 @@ public class Relationship implements ReloadableEntity<Integer> {
@Column(name = "rightward_value") @Column(name = "rightward_value")
private String rightwardValue; private String rightwardValue;
/**
* Whether the left and/or right side of a given relationship are the "latest".
* A side of a relationship is "latest" if the item on that side has either no other versions,
* or the item on that side is the most recent version that is relevant to the given relationship.
* This column affects what version of an item appears on search pages or the relationship listings of other items.
*/
@Column(name = "latest_version_status")
private LatestVersionStatus latestVersionStatus = LatestVersionStatus.BOTH;
/** /**
* Protected constructor, create object using: * Protected constructor, create object using:
* {@link org.dspace.content.service.RelationshipService#create(Context)} } * {@link org.dspace.content.service.RelationshipService#create(Context)} }
@@ -216,6 +225,30 @@ public class Relationship implements ReloadableEntity<Integer> {
this.rightwardValue = rightwardValue; this.rightwardValue = rightwardValue;
} }
/**
* Getter for {@link #latestVersionStatus}.
* @return the latest version status of this relationship.
*/
public LatestVersionStatus getLatestVersionStatus() {
return latestVersionStatus;
}
/**
* Setter for {@link #latestVersionStatus}.
* @param latestVersionStatus the new latest version status for this relationship.
*/
public void setLatestVersionStatus(LatestVersionStatus latestVersionStatus) {
this.latestVersionStatus = latestVersionStatus;
}
public enum LatestVersionStatus {
// NOTE: SQL migration expects BOTH to be the first constant in this enum!
BOTH, // both items in this relationship are the "latest"
LEFT_ONLY, // the left-hand item of this relationship is the "latest", but the right-hand item is not
RIGHT_ONLY // the right-hand item of this relationship is the "latest", but the left-hand item is not
// NOTE: one side of any given relationship should ALWAYS be the "latest"
}
/** /**
* Standard getter for the ID for this Relationship * Standard getter for the ID for this Relationship
* @return The ID of this relationship * @return The ID of this relationship

View File

@@ -20,6 +20,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.dspace.authorize.AuthorizeException; import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.service.AuthorizeService; import org.dspace.authorize.service.AuthorizeService;
import org.dspace.content.Relationship.LatestVersionStatus;
import org.dspace.content.dao.RelationshipDAO; import org.dspace.content.dao.RelationshipDAO;
import org.dspace.content.service.EntityTypeService; import org.dspace.content.service.EntityTypeService;
import org.dspace.content.service.ItemService; import org.dspace.content.service.ItemService;
@@ -76,9 +77,10 @@ public class RelationshipServiceImpl implements RelationshipService {
@Override @Override
public Relationship create(Context c, Item leftItem, Item rightItem, RelationshipType relationshipType, public Relationship create(
int leftPlace, int rightPlace, String leftwardValue, String rightwardValue) Context c, Item leftItem, Item rightItem, RelationshipType relationshipType, int leftPlace, int rightPlace,
throws AuthorizeException, SQLException { String leftwardValue, String rightwardValue, LatestVersionStatus latestVersionStatus
) throws AuthorizeException, SQLException {
Relationship relationship = new Relationship(); Relationship relationship = new Relationship();
relationship.setLeftItem(leftItem); relationship.setLeftItem(leftItem);
relationship.setRightItem(rightItem); relationship.setRightItem(rightItem);
@@ -87,9 +89,21 @@ public class RelationshipServiceImpl implements RelationshipService {
relationship.setRightPlace(rightPlace); relationship.setRightPlace(rightPlace);
relationship.setLeftwardValue(leftwardValue); relationship.setLeftwardValue(leftwardValue);
relationship.setRightwardValue(rightwardValue); relationship.setRightwardValue(rightwardValue);
relationship.setLatestVersionStatus(latestVersionStatus);
return create(c, relationship); return create(c, relationship);
} }
@Override
public Relationship create(
Context c, Item leftItem, Item rightItem, RelationshipType relationshipType, int leftPlace, int rightPlace,
String leftwardValue, String rightwardValue
) throws AuthorizeException, SQLException {
return create(
c, leftItem, rightItem, relationshipType, leftPlace, rightPlace, leftwardValue, rightwardValue,
LatestVersionStatus.BOTH
);
}
@Override @Override
public Relationship create(Context context, Relationship relationship) throws SQLException, AuthorizeException { public Relationship create(Context context, Relationship relationship) throws SQLException, AuthorizeException {
if (isRelationshipValidToCreate(context, relationship)) { if (isRelationshipValidToCreate(context, relationship)) {

View File

@@ -14,6 +14,7 @@ import java.util.UUID;
import org.dspace.authorize.AuthorizeException; import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Item; import org.dspace.content.Item;
import org.dspace.content.Relationship; import org.dspace.content.Relationship;
import org.dspace.content.Relationship.LatestVersionStatus;
import org.dspace.content.RelationshipType; import org.dspace.content.RelationshipType;
import org.dspace.core.Context; import org.dspace.core.Context;
import org.dspace.service.DSpaceCRUDService; import org.dspace.service.DSpaceCRUDService;
@@ -198,6 +199,27 @@ public interface RelationshipService extends DSpaceCRUDService<Relationship> {
/** /**
* This method is used to construct a Relationship object with all it's variables * This method is used to construct a Relationship object with all it's variables
* @param c The relevant DSpace context
* @param leftItem The leftItem Item object for the relationship
* @param rightItem The rightItem Item object for the relationship
* @param relationshipType The RelationshipType object for the relationship
* @param leftPlace The leftPlace integer for the relationship
* @param rightPlace The rightPlace integer for the relationship
* @param leftwardValue The leftwardValue string for the relationship
* @param rightwardValue The rightwardValue string for the relationship
* @param latestVersionStatus The latestVersionStatus value for the relationship
* @return The created Relationship object with the given properties
* @throws AuthorizeException If something goes wrong
* @throws SQLException If something goes wrong
*/
Relationship create(
Context c, Item leftItem, Item rightItem, RelationshipType relationshipType, int leftPlace, int rightPlace,
String leftwardValue, String rightwardValue, LatestVersionStatus latestVersionStatus
) throws AuthorizeException, SQLException;
/**
* This method is used to construct a Relationship object with all it's variables,
* except the latest version status
* @param c The relevant DSpace context * @param c The relevant DSpace context
* @param leftItem The leftItem Item object for the relationship * @param leftItem The leftItem Item object for the relationship
* @param rightItem The rightItem Item object for the relationship * @param rightItem The rightItem Item object for the relationship
@@ -210,14 +232,15 @@ public interface RelationshipService extends DSpaceCRUDService<Relationship> {
* @throws AuthorizeException If something goes wrong * @throws AuthorizeException If something goes wrong
* @throws SQLException If something goes wrong * @throws SQLException If something goes wrong
*/ */
Relationship create(Context c, Item leftItem, Item rightItem, RelationshipType relationshipType, Relationship create(
int leftPlace, int rightPlace, String leftwardValue, String rightwardValue) Context c, Item leftItem, Item rightItem, RelationshipType relationshipType, int leftPlace, int rightPlace,
throws AuthorizeException, SQLException; String leftwardValue, String rightwardValue
) throws AuthorizeException, SQLException;
/** /**
* This method is used to construct a Relationship object with all it's variables, * This method is used to construct a Relationship object with all it's variables,
* except the leftward and rightward labels * except the leftward label, rightward label and latest version status
* @param c The relevant DSpace context * @param c The relevant DSpace context
* @param leftItem The leftItem Item object for the relationship * @param leftItem The leftItem Item object for the relationship
* @param rightItem The rightItem Item object for the relationship * @param rightItem The rightItem Item object for the relationship

View File

@@ -0,0 +1,10 @@
--
-- The contents of this file are subject to the license and copyright
-- detailed in the LICENSE and NOTICE files at the root of the source
-- tree and available online at
--
-- http://www.dspace.org/license/
--
-- NOTE: default 0 ensures that existing relations have "latest_version_status" set to "both" (first constant in enum, see Relationship class)
ALTER TABLE relationship ADD COLUMN IF NOT EXISTS latest_version_status INTEGER DEFAULT 0 NOT NULL;

View File

@@ -0,0 +1,10 @@
--
-- The contents of this file are subject to the license and copyright
-- detailed in the LICENSE and NOTICE files at the root of the source
-- tree and available online at
--
-- http://www.dspace.org/license/
--
-- NOTE: default 0 ensures that existing relations have "latest_version_status" set to "both" (first constant in enum, see Relationship class)
ALTER TABLE relationship ADD latest_version_status INTEGER DEFAULT 0 NOT NULL;

View File

@@ -0,0 +1,10 @@
--
-- The contents of this file are subject to the license and copyright
-- detailed in the LICENSE and NOTICE files at the root of the source
-- tree and available online at
--
-- http://www.dspace.org/license/
--
-- NOTE: default 0 ensures that existing relations have "latest_version_status" set to "both" (first constant in enum, see Relationship class)
ALTER TABLE relationship ADD COLUMN IF NOT EXISTS latest_version_status INTEGER DEFAULT 0 NOT NULL;

View File

@@ -0,0 +1,208 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.content;
import static org.junit.Assert.assertEquals;
import org.dspace.AbstractIntegrationTestWithDatabase;
import org.dspace.builder.CollectionBuilder;
import org.dspace.builder.CommunityBuilder;
import org.dspace.builder.EntityTypeBuilder;
import org.dspace.builder.ItemBuilder;
import org.dspace.builder.RelationshipTypeBuilder;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.RelationshipService;
import org.junit.Before;
import org.junit.Test;
public class RelationshipServiceImplVersioningTest extends AbstractIntegrationTestWithDatabase {
private RelationshipService relationshipService;
protected Community community;
protected Collection collection;
protected EntityType publicationEntityType;
protected EntityType personEntityType;
protected RelationshipType relationshipType;
protected Item publication1;
protected Item publication2;
protected Item publication3;
protected Item person1;
@Override
@Before
public void setUp() throws Exception {
super.setUp();
relationshipService = ContentServiceFactory.getInstance().getRelationshipService();
context.turnOffAuthorisationSystem();
community = CommunityBuilder.createCommunity(context)
.withName("community")
.build();
collection = CollectionBuilder.createCollection(context, community)
.withName("collection")
.build();
publicationEntityType = EntityTypeBuilder.createEntityTypeBuilder(context, "Publication")
.build();
personEntityType = EntityTypeBuilder.createEntityTypeBuilder(context, "Person")
.build();
relationshipType = RelationshipTypeBuilder.createRelationshipTypeBuilder(
context, publicationEntityType, personEntityType,
"isAuthorOfPublication", "isPublicationOfAuthor",
null, null, null, null
)
.withCopyToLeft(false)
.withCopyToRight(false)
.build();
publication1 = ItemBuilder.createItem(context, collection)
.withTitle("publication1")
.withMetadata("dspace", "entity", "type", publicationEntityType.getLabel())
.build();
publication2 = ItemBuilder.createItem(context, collection)
.withTitle("publication2")
.withMetadata("dspace", "entity", "type", publicationEntityType.getLabel())
.build();
publication3 = ItemBuilder.createItem(context, collection)
.withTitle("publication3")
.withMetadata("dspace", "entity", "type", publicationEntityType.getLabel())
.build();
person1 = ItemBuilder.createItem(context, collection)
.withTitle("person1")
.withMetadata("dspace", "entity", "type", personEntityType.getLabel())
.build();
context.restoreAuthSystemState();
}
@Test
public void testRelationshipLatestVersionStatusDefault() throws Exception {
// create method #1
context.turnOffAuthorisationSystem();
Relationship relationship1 = relationshipService.create(
context, publication1, person1, relationshipType, 3, 5, "left", "right"
);
context.restoreAuthSystemState();
assertEquals(Relationship.LatestVersionStatus.BOTH, relationship1.getLatestVersionStatus());
Relationship relationship2 = relationshipService.find(context, relationship1.getID());
assertEquals(Relationship.LatestVersionStatus.BOTH, relationship2.getLatestVersionStatus());
// create method #2
context.turnOffAuthorisationSystem();
Relationship relationship3 = relationshipService.create(
context, publication2, person1, relationshipType, 3, 5
);
context.restoreAuthSystemState();
assertEquals(Relationship.LatestVersionStatus.BOTH, relationship3.getLatestVersionStatus());
Relationship relationship4 = relationshipService.find(context, relationship3.getID());
assertEquals(Relationship.LatestVersionStatus.BOTH, relationship4.getLatestVersionStatus());
// create method #3
Relationship inputRelationship = new Relationship();
inputRelationship.setLeftItem(publication3);
inputRelationship.setRightItem(person1);
inputRelationship.setRelationshipType(relationshipType);
context.turnOffAuthorisationSystem();
Relationship relationship5 = relationshipService.create(context, inputRelationship);
context.restoreAuthSystemState();
assertEquals(Relationship.LatestVersionStatus.BOTH, relationship5.getLatestVersionStatus());
Relationship relationship6 = relationshipService.find(context, relationship5.getID());
assertEquals(Relationship.LatestVersionStatus.BOTH, relationship6.getLatestVersionStatus());
}
@Test
public void testRelationshipLatestVersionStatusBoth() throws Exception {
// create method #1
context.turnOffAuthorisationSystem();
Relationship relationship1 = relationshipService.create(
context, publication1, person1, relationshipType, 3, 5, "left", "right",
Relationship.LatestVersionStatus.BOTH // set latest version status
);
context.restoreAuthSystemState();
assertEquals(Relationship.LatestVersionStatus.BOTH, relationship1.getLatestVersionStatus());
Relationship relationship2 = relationshipService.find(context, relationship1.getID());
assertEquals(Relationship.LatestVersionStatus.BOTH, relationship2.getLatestVersionStatus());
// create method #2
Relationship inputRelationship = new Relationship();
inputRelationship.setLeftItem(publication2);
inputRelationship.setRightItem(person1);
inputRelationship.setRelationshipType(relationshipType);
inputRelationship.setLatestVersionStatus(Relationship.LatestVersionStatus.BOTH); // set latest version status
context.turnOffAuthorisationSystem();
Relationship relationship3 = relationshipService.create(context, inputRelationship);
context.restoreAuthSystemState();
assertEquals(Relationship.LatestVersionStatus.BOTH, relationship3.getLatestVersionStatus());
Relationship relationship4 = relationshipService.find(context, relationship3.getID());
assertEquals(Relationship.LatestVersionStatus.BOTH, relationship4.getLatestVersionStatus());
}
@Test
public void testRelationshipLatestVersionStatusLeftOnly() throws Exception {
// create method #1
context.turnOffAuthorisationSystem();
Relationship relationship1 = relationshipService.create(
context, publication1, person1, relationshipType, 3, 5, "left", "right",
Relationship.LatestVersionStatus.LEFT_ONLY // set latest version status
);
context.restoreAuthSystemState();
assertEquals(Relationship.LatestVersionStatus.LEFT_ONLY, relationship1.getLatestVersionStatus());
Relationship relationship2 = relationshipService.find(context, relationship1.getID());
assertEquals(Relationship.LatestVersionStatus.LEFT_ONLY, relationship2.getLatestVersionStatus());
// create method #2
Relationship inputRelationship = new Relationship();
inputRelationship.setLeftItem(publication2);
inputRelationship.setRightItem(person1);
inputRelationship.setRelationshipType(relationshipType);
inputRelationship.setLatestVersionStatus(Relationship.LatestVersionStatus.LEFT_ONLY); // set LVS
context.turnOffAuthorisationSystem();
Relationship relationship3 = relationshipService.create(context, inputRelationship);
context.restoreAuthSystemState();
assertEquals(Relationship.LatestVersionStatus.LEFT_ONLY, relationship3.getLatestVersionStatus());
Relationship relationship4 = relationshipService.find(context, relationship3.getID());
assertEquals(Relationship.LatestVersionStatus.LEFT_ONLY, relationship4.getLatestVersionStatus());
}
@Test
public void testRelationshipLatestVersionStatusRightOnly() throws Exception {
// create method #1
context.turnOffAuthorisationSystem();
Relationship relationship1 = relationshipService.create(
context, publication1, person1, relationshipType, 3, 5, "left", "right",
Relationship.LatestVersionStatus.RIGHT_ONLY // set latest version status
);
context.restoreAuthSystemState();
assertEquals(Relationship.LatestVersionStatus.RIGHT_ONLY, relationship1.getLatestVersionStatus());
Relationship relationship2 = relationshipService.find(context, relationship1.getID());
assertEquals(Relationship.LatestVersionStatus.RIGHT_ONLY, relationship2.getLatestVersionStatus());
// create method #2
Relationship inputRelationship = new Relationship();
inputRelationship.setLeftItem(publication2);
inputRelationship.setRightItem(person1);
inputRelationship.setRelationshipType(relationshipType);
inputRelationship.setLatestVersionStatus(Relationship.LatestVersionStatus.RIGHT_ONLY); // set LVS
context.turnOffAuthorisationSystem();
Relationship relationship3 = relationshipService.create(context, inputRelationship);
context.restoreAuthSystemState();
assertEquals(Relationship.LatestVersionStatus.RIGHT_ONLY, relationship3.getLatestVersionStatus());
Relationship relationship4 = relationshipService.find(context, relationship3.getID());
assertEquals(Relationship.LatestVersionStatus.RIGHT_ONLY, relationship4.getLatestVersionStatus());
}
}