88051: Support filtering non-latest relationships

This commit is contained in:
Bruno Roemers
2022-03-02 00:58:32 +01:00
parent d0aab90ffc
commit a5f0c03a27
5 changed files with 368 additions and 136 deletions

View File

@@ -293,14 +293,22 @@ public class RelationshipServiceImpl implements RelationshipService {
} }
@Override @Override
public List<Relationship> findByItem(Context context, Item item, Integer limit, Integer offset, public List<Relationship> findByItem(
boolean excludeTilted) throws SQLException { Context context, Item item, Integer limit, Integer offset, boolean excludeTilted
) throws SQLException {
return findByItem(context, item, limit, offset, excludeTilted, true);
}
List<Relationship> list = relationshipDAO.findByItem(context, item, limit, offset, excludeTilted); @Override
public List<Relationship> findByItem(
Context context, Item item, Integer limit, Integer offset, boolean excludeTilted, boolean excludeNonLinked
) throws SQLException {
List<Relationship> list =
relationshipDAO.findByItem(context, item, limit, offset, excludeTilted, excludeNonLinked);
list.sort((o1, o2) -> { list.sort((o1, o2) -> {
int relationshipType = o1.getRelationshipType().getLeftwardType() int relationshipType = o1.getRelationshipType().getLeftwardType()
.compareTo(o2.getRelationshipType().getLeftwardType()); .compareTo(o2.getRelationshipType().getLeftwardType());
if (relationshipType != 0) { if (relationshipType != 0) {
return relationshipType; return relationshipType;
} else { } else {
@@ -652,22 +660,38 @@ public class RelationshipServiceImpl implements RelationshipService {
public List<Relationship> findByItemAndRelationshipType(Context context, Item item, public List<Relationship> findByItemAndRelationshipType(Context context, Item item,
RelationshipType relationshipType) RelationshipType relationshipType)
throws SQLException { throws SQLException {
return relationshipDAO.findByItemAndRelationshipType(context, item, relationshipType, -1, -1); return findByItemAndRelationshipType(context, item, relationshipType, -1, -1, true);
} }
@Override @Override
public List<Relationship> findByItemAndRelationshipType(Context context, Item item, public List<Relationship> findByItemAndRelationshipType(Context context, Item item,
RelationshipType relationshipType, int limit, int offset) RelationshipType relationshipType, int limit, int offset)
throws SQLException { throws SQLException {
return relationshipDAO.findByItemAndRelationshipType(context, item, relationshipType, limit, offset); return findByItemAndRelationshipType(context, item, relationshipType, limit, offset, true);
} }
@Override @Override
public List<Relationship> findByItemAndRelationshipType(Context context, Item item, public List<Relationship> findByItemAndRelationshipType(
RelationshipType relationshipType, boolean isLeft, Context context, Item item, RelationshipType relationshipType, int limit, int offset, boolean excludeNonLatest
int limit, int offset) ) throws SQLException {
throws SQLException { return relationshipDAO
return relationshipDAO.findByItemAndRelationshipType(context, item, relationshipType, isLeft, limit, offset); .findByItemAndRelationshipType(context, item, relationshipType, limit, offset, excludeNonLatest);
}
@Override
public List<Relationship> findByItemAndRelationshipType(
Context context, Item item, RelationshipType relationshipType, boolean isLeft, int limit, int offset
) throws SQLException {
return findByItemAndRelationshipType(context, item, relationshipType, isLeft, limit, offset, true);
}
@Override
public List<Relationship> findByItemAndRelationshipType(
Context context, Item item, RelationshipType relationshipType, boolean isLeft, int limit, int offset,
boolean excludeNonLatest
) throws SQLException {
return relationshipDAO
.findByItemAndRelationshipType(context, item, relationshipType, isLeft, limit, offset, excludeNonLatest);
} }
@Override @Override
@@ -704,7 +728,12 @@ public class RelationshipServiceImpl implements RelationshipService {
@Override @Override
public int countByItem(Context context, Item item) throws SQLException { public int countByItem(Context context, Item item) throws SQLException {
return relationshipDAO.countByItem(context, item); return countByItem(context, item, true);
}
@Override
public int countByItem(Context context, Item item, boolean excludeNonLatest) throws SQLException {
return relationshipDAO.countByItem(context, item, excludeNonLatest);
} }
@Override @Override
@@ -713,9 +742,18 @@ public class RelationshipServiceImpl implements RelationshipService {
} }
@Override @Override
public int countByItemAndRelationshipType(Context context, Item item, RelationshipType relationshipType, public int countByItemAndRelationshipType(
boolean isLeft) throws SQLException { Context context, Item item, RelationshipType relationshipType, boolean isLeft
return relationshipDAO.countByItemAndRelationshipType(context, item, relationshipType, isLeft); ) throws SQLException {
return countByItemAndRelationshipType(context, item, relationshipType, isLeft, true);
}
@Override
public int countByItemAndRelationshipType(
Context context, Item item, RelationshipType relationshipType, boolean isLeft, boolean excludeNonLatest
) throws SQLException {
return relationshipDAO
.countByItemAndRelationshipType(context, item, relationshipType, isLeft, excludeNonLatest);
} }
@Override @Override

View File

@@ -28,31 +28,38 @@ public interface RelationshipDAO extends GenericDAO<Relationship> {
/** /**
* This method returns a list of Relationship objects that have the given Item object * This method returns a list of Relationship objects that have the given Item object
* as a leftItem or a rightItem * as a leftItem or a rightItem
* @param context The relevant DSpace context * @param context The relevant DSpace context
* @param item The item that should be either a leftItem or a rightItem of all * @param item The item that should be either a leftItem or a rightItem of all
* the Relationship objects in the returned list * the Relationship objects in the returned list
* @param excludeTilted If true, excludes tilted relationships * @param excludeTilted If true, excludes tilted relationships
* @return The list of Relationship objects that contain either a left or a * @param excludeNonLatest If true, excludes all relationships for which the other item has a more recent version
* right item that is equal to the given item * that is relevant for this relationship
* @throws SQLException If something goes wrong * @return The list of Relationship objects that contain either a left or a
* right item that is equal to the given item
* @throws SQLException If something goes wrong
*/ */
List<Relationship> findByItem(Context context, Item item, boolean excludeTilted) throws SQLException; List<Relationship> findByItem(
Context context, Item item, boolean excludeTilted, boolean excludeNonLatest
) throws SQLException;
/** /**
* This method returns a list of Relationship objects that have the given Item object * This method returns a list of Relationship objects that have the given Item object
* as a leftItem or a rightItem * as a leftItem or a rightItem
* @param context The relevant DSpace context * @param context The relevant DSpace context
* @param item The item that should be either a leftItem or a rightItem of all * @param item The item that should be either a leftItem or a rightItem of all
* the Relationship objects in the returned list * the Relationship objects in the returned list
* @param limit paging limit * @param limit paging limit
* @param offset paging offset * @param offset paging offset
* @param excludeTilted If true, excludes tilted relationships * @param excludeTilted If true, excludes tilted relationships
* @return The list of Relationship objects that contain either a left or a * @param excludeNonLatest If true, excludes all relationships for which the other item has a more recent version
* right item that is equal to the given item * that is relevant for this relationship
* @throws SQLException If something goes wrong * @return The list of Relationship objects that contain either a left or a
* right item that is equal to the given item
* @throws SQLException If something goes wrong
*/ */
List<Relationship> findByItem(Context context, Item item, Integer limit, Integer offset, boolean excludeTilted) List<Relationship> findByItem(
throws SQLException; Context context, Item item, Integer limit, Integer offset, boolean excludeTilted, boolean excludeNonLatest
) throws SQLException;
/** /**
* This method returns the next leftplace integer to use for a relationship with this item as the leftItem * This method returns the next leftplace integer to use for a relationship with this item as the leftItem
@@ -108,34 +115,41 @@ public interface RelationshipDAO extends GenericDAO<Relationship> {
* It will construct a list of all Relationship objects that have the given RelationshipType object * It will construct a list of all Relationship objects that have the given RelationshipType object
* as the relationshipType property * as the relationshipType property
* @param context The relevant DSpace context * @param context The relevant DSpace context
* @param item item to filter by
* @param relationshipType The RelationshipType object to be checked on * @param relationshipType The RelationshipType object to be checked on
* @param limit paging limit * @param limit paging limit
* @param offset paging offset * @param offset paging offset
* @param item item to filter by * @param excludeNonLatest If true, excludes all relationships for which the other item has a more recent version
* that is relevant for this relationship
* @return A list of Relationship objects that have the given RelationshipType object as the * @return A list of Relationship objects that have the given RelationshipType object as the
* relationshipType property * relationshipType property
* @throws SQLException If something goes wrong * @throws SQLException If something goes wrong
*/ */
List<Relationship> findByItemAndRelationshipType(Context context, Item item, RelationshipType relationshipType, List<Relationship> findByItemAndRelationshipType(
Integer limit, Integer offset) throws SQLException; Context context, Item item, RelationshipType relationshipType, Integer limit, Integer offset,
boolean excludeNonLatest
) throws SQLException;
/** /**
* This method returns a list of Relationship objects for the given RelationshipType object. * This method returns a list of Relationship objects for the given RelationshipType object.
* It will construct a list of all Relationship objects that have the given RelationshipType object * It will construct a list of all Relationship objects that have the given RelationshipType object
* as the relationshipType property * as the relationshipType property
* @param context The relevant DSpace context * @param context The relevant DSpace context
* @param item item to filter by
* @param relationshipType The RelationshipType object to be checked on * @param relationshipType The RelationshipType object to be checked on
* @param isLeft Is item left or right
* @param limit paging limit * @param limit paging limit
* @param offset paging offset * @param offset paging offset
* @param item item to filter by * @param excludeNonLatest If true, excludes all relationships for which the other item has a more recent version
* @param isLeft Is item left or right * that is relevant for this relationship
* @return A list of Relationship objects that have the given RelationshipType object as the * @return A list of Relationship objects that have the given RelationshipType object as the
* relationshipType property * relationshipType property
* @throws SQLException If something goes wrong * @throws SQLException If something goes wrong
*/ */
List<Relationship> findByItemAndRelationshipType(Context context, Item item, RelationshipType relationshipType, List<Relationship> findByItemAndRelationshipType(
boolean isLeft, Integer limit, Integer offset) Context context, Item item, RelationshipType relationshipType, boolean isLeft, Integer limit, Integer offset,
throws SQLException; boolean excludeNonLatest
) throws SQLException;
/** /**
* This method returns a list of Relationship objects for the given typeName * This method returns a list of Relationship objects for the given typeName
@@ -183,28 +197,33 @@ public interface RelationshipDAO extends GenericDAO<Relationship> {
/** /**
* This method returns a count of Relationship objects that have the given Item object * This method returns a count of Relationship objects that have the given Item object
* as a leftItem or a rightItem * as a leftItem or a rightItem
* @param context The relevant DSpace context * @param context The relevant DSpace context
* @param item The item that should be either a leftItem or a rightItem of all * @param item The item that should be either a leftItem or a rightItem of all
* the Relationship objects in the returned list * the Relationship objects in the returned list
* @param excludeNonLatest if true, exclude relationships for which the opposite item is not the latest version
* that is relevant
* @return The list of Relationship objects that contain either a left or a * @return The list of Relationship objects that contain either a left or a
* right item that is equal to the given item * right item that is equal to the given item
* @throws SQLException If something goes wrong * @throws SQLException If something goes wrong
*/ */
int countByItem(Context context, Item item) throws SQLException; int countByItem(Context context, Item item, boolean excludeNonLatest) throws SQLException;
/** /**
* Count total number of relationships (rows in relationship table) by an item and a relationship type and a boolean * 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 * 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 * @param isLeft indicating whether the counted Relationships should have the given Item on the left side
* @param excludeNonLatest if true, exclude relationships for which the opposite item is not the latest version
* that is relevant
* @return total count * @return total count
* @throws SQLException if database error * @throws SQLException if database error
*/ */
int countByItemAndRelationshipType(Context context, Item item, RelationshipType relationshipType, boolean isLeft) int countByItemAndRelationshipType(
throws SQLException; Context context, Item item, RelationshipType relationshipType, boolean isLeft, boolean excludeNonLatest
) throws SQLException;
/** /**
* Count total number of relationships (rows in relationship table) given a typeName * Count total number of relationships (rows in relationship table) given a typeName

View File

@@ -14,6 +14,7 @@ import java.util.UUID;
import javax.persistence.Query; import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root; import javax.persistence.criteria.Root;
import org.dspace.content.Item; import org.dspace.content.Item;
@@ -30,60 +31,147 @@ import org.dspace.core.Context;
public class RelationshipDAOImpl extends AbstractHibernateDAO<Relationship> implements RelationshipDAO { public class RelationshipDAOImpl extends AbstractHibernateDAO<Relationship> implements RelationshipDAO {
@Override @Override
public List<Relationship> findByItem(Context context, Item item, boolean excludeTilted) throws SQLException { public List<Relationship> findByItem(
return findByItem(context, item, -1, -1, excludeTilted); Context context, Item item, boolean excludeTilted, boolean excludeNonLatest
) throws SQLException {
return findByItem(context, item, -1, -1, excludeTilted, excludeNonLatest);
} }
@Override @Override
public List<Relationship> findByItem(Context context, Item item, Integer limit, Integer offset, public List<Relationship> findByItem(
boolean excludeTilted) throws SQLException { Context context, Item item, Integer limit, Integer offset, boolean excludeTilted, boolean excludeNonLatest
) throws SQLException {
CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context); CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context);
CriteriaQuery criteriaQuery = getCriteriaQuery(criteriaBuilder, Relationship.class); CriteriaQuery<Relationship> 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 (excludeTilted) {
// If this item is the left item, criteriaQuery.where(
// return relationships for types which are not tilted right (tilted is either left nor null) criteriaBuilder.or(
// If this item is the right item, getLeftItemPredicate(criteriaBuilder, relationshipRoot, item, excludeTilted, excludeNonLatest),
// return relationships for types which are not tilted left (tilted is either right nor null) getRightItemPredicate(criteriaBuilder, relationshipRoot, item, excludeTilted, excludeNonLatest)
criteriaQuery )
.where(criteriaBuilder.or( );
criteriaBuilder.and(
criteriaBuilder.equal(relationshipRoot.get(Relationship_.leftItem), item),
criteriaBuilder.or(
criteriaBuilder.isNull(relationshipRoot.get(Relationship_.relationshipType)
.get(RelationshipType_.tilted)),
criteriaBuilder.notEqual(relationshipRoot
.get(Relationship_.relationshipType)
.get(RelationshipType_.tilted), RelationshipType.Tilted.RIGHT))),
criteriaBuilder.and(
criteriaBuilder.equal(relationshipRoot.get(Relationship_.rightItem), item),
criteriaBuilder.or(
criteriaBuilder.isNull(relationshipRoot.get(Relationship_.relationshipType)
.get(RelationshipType_.tilted)),
criteriaBuilder.notEqual(relationshipRoot
.get(Relationship_.relationshipType)
.get(RelationshipType_.tilted), RelationshipType.Tilted.LEFT)))));
} else {
criteriaQuery
.where(criteriaBuilder.or(criteriaBuilder.equal(relationshipRoot.get(Relationship_.leftItem), item),
criteriaBuilder.equal(relationshipRoot.get(Relationship_.rightItem), item)));
}
return list(context, criteriaQuery, false, Relationship.class, limit, offset); return list(context, criteriaQuery, false, Relationship.class, limit, offset);
} }
@Override /**
public int countByItem(Context context, Item item) * Get the predicate for a criteria query that selects relationships by their left item.
throws SQLException { * @param criteriaBuilder the criteria builder.
* @param relationshipRoot the relationship root.
* @param item the item that is being searched for.
* @param excludeTilted if true, exclude tilted relationships.
* @param excludeNonLatest if true, exclude relationships for which the opposite item is not the latest version
* that is relevant.
* @return a predicate that satisfies the given restrictions.
*/
protected Predicate getLeftItemPredicate(
CriteriaBuilder criteriaBuilder, Root<Relationship> relationshipRoot, Item item,
boolean excludeTilted, boolean excludeNonLatest
) {
List<Predicate> predicates = new ArrayList<>();
// match relationships based on the left item
predicates.add(
criteriaBuilder.equal(relationshipRoot.get(Relationship_.leftItem), item)
);
if (excludeTilted) {
// if this item is the left item,
// return relationships for types which are NOT tilted right (tilted is either left nor null)
predicates.add(
criteriaBuilder.or(
criteriaBuilder.isNull(
relationshipRoot.get(Relationship_.relationshipType).get(RelationshipType_.tilted)
),
criteriaBuilder.notEqual(
relationshipRoot.get(Relationship_.relationshipType).get(RelationshipType_.tilted),
RelationshipType.Tilted.RIGHT
)
)
);
}
if (excludeNonLatest) {
// if this item is the left item,
// return relationships for which the right item is the "latest" version that is relevant.
predicates.add(
criteriaBuilder.notEqual(
relationshipRoot.get(Relationship_.LATEST_VERSION_STATUS),
Relationship.LatestVersionStatus.LEFT_ONLY
)
);
}
return criteriaBuilder.and(predicates.toArray(new Predicate[]{}));
}
/**
* Get the predicate for a criteria query that selects relationships by their right item.
* @param criteriaBuilder the criteria builder.
* @param relationshipRoot the relationship root.
* @param item the item that is being searched for.
* @param excludeTilted if true, exclude tilted relationships.
* @param excludeNonLatest if true, exclude relationships for which the opposite item is not the latest version
* that is relevant.
* @return a predicate that satisfies the given restrictions.
*/
protected Predicate getRightItemPredicate(
CriteriaBuilder criteriaBuilder, Root<Relationship> relationshipRoot, Item item,
boolean excludeTilted, boolean excludeNonLatest
) {
List<Predicate> predicates = new ArrayList<>();
// match relationships based on the right item
predicates.add(
criteriaBuilder.equal(relationshipRoot.get(Relationship_.rightItem), item)
);
if (excludeTilted) {
// if this item is the right item,
// return relationships for types which are NOT tilted left (tilted is either right nor null)
predicates.add(
criteriaBuilder.or(
criteriaBuilder.isNull(
relationshipRoot.get(Relationship_.relationshipType).get(RelationshipType_.tilted)
),
criteriaBuilder.notEqual(
relationshipRoot.get(Relationship_.relationshipType).get(RelationshipType_.tilted),
RelationshipType.Tilted.LEFT
)
)
);
}
if (excludeNonLatest) {
// if this item is the right item,
// return relationships for which the left item is the "latest" version that is relevant.
predicates.add(
criteriaBuilder.notEqual(
relationshipRoot.get(Relationship_.LATEST_VERSION_STATUS),
Relationship.LatestVersionStatus.RIGHT_ONLY
)
);
}
return criteriaBuilder.and(predicates.toArray(new Predicate[]{}));
}
@Override
public int countByItem(Context context, Item item, boolean excludeNonLatest) 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);
criteriaQuery
.where(criteriaBuilder.or(criteriaBuilder.equal(relationshipRoot.get(Relationship_.leftItem), item), criteriaQuery.where(
criteriaBuilder.equal(relationshipRoot.get(Relationship_.rightItem), item))); criteriaBuilder.or(
getLeftItemPredicate(criteriaBuilder, relationshipRoot, item, false, excludeNonLatest),
getRightItemPredicate(criteriaBuilder, relationshipRoot, item, false, excludeNonLatest)
)
);
return count(context, criteriaQuery, criteriaBuilder, relationshipRoot); return count(context, criteriaQuery, criteriaBuilder, relationshipRoot);
} }
@@ -140,46 +228,50 @@ public class RelationshipDAOImpl extends AbstractHibernateDAO<Relationship> impl
} }
@Override @Override
public List<Relationship> findByItemAndRelationshipType(Context context, Item item, public List<Relationship> findByItemAndRelationshipType(
RelationshipType relationshipType, Integer limit, Context context, Item item, RelationshipType relationshipType, Integer limit, Integer offset,
Integer offset) boolean excludeNonLatest
throws SQLException { ) 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);
criteriaQuery
.where(criteriaBuilder.equal(relationshipRoot.get(Relationship_.relationshipType), criteriaQuery.where(
relationshipType), criteriaBuilder.or criteriaBuilder.equal(relationshipRoot.get(Relationship_.relationshipType), relationshipType),
(criteriaBuilder.equal(relationshipRoot.get(Relationship_.leftItem), item), criteriaBuilder.or(
criteriaBuilder.equal(relationshipRoot.get(Relationship_.rightItem), item))); getLeftItemPredicate(criteriaBuilder, relationshipRoot, item, false, excludeNonLatest),
getRightItemPredicate(criteriaBuilder, relationshipRoot, item, false, excludeNonLatest)
)
);
return list(context, criteriaQuery, true, Relationship.class, limit, offset); return list(context, criteriaQuery, true, Relationship.class, limit, offset);
} }
@Override @Override
public List<Relationship> findByItemAndRelationshipType(Context context, Item item, public List<Relationship> findByItemAndRelationshipType(
RelationshipType relationshipType, boolean isLeft, Context context, Item item, RelationshipType relationshipType, boolean isLeft, Integer limit, Integer offset,
Integer limit, Integer offset) boolean excludeNonLatest
throws SQLException { ) 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) { if (isLeft) {
criteriaQuery criteriaQuery.where(
.where(criteriaBuilder.equal(relationshipRoot.get(Relationship_.relationshipType), criteriaBuilder.equal(relationshipRoot.get(Relationship_.relationshipType), relationshipType),
relationshipType), getLeftItemPredicate(criteriaBuilder, relationshipRoot, item, false, excludeNonLatest)
criteriaBuilder.equal(relationshipRoot.get(Relationship_.leftItem), item)); );
criteriaQuery.orderBy(criteriaBuilder.asc(relationshipRoot.get(Relationship_.leftPlace))); criteriaQuery.orderBy(criteriaBuilder.asc(relationshipRoot.get(Relationship_.leftPlace)));
} else { } else {
criteriaQuery criteriaQuery.where(
.where(criteriaBuilder.equal(relationshipRoot.get(Relationship_.relationshipType), criteriaBuilder.equal(relationshipRoot.get(Relationship_.relationshipType), relationshipType),
relationshipType), getRightItemPredicate(criteriaBuilder, relationshipRoot, item, false, excludeNonLatest)
criteriaBuilder.equal(relationshipRoot.get(Relationship_.rightItem), item)); );
criteriaQuery.orderBy(criteriaBuilder.asc(relationshipRoot.get(Relationship_.rightPlace))); criteriaQuery.orderBy(criteriaBuilder.asc(relationshipRoot.get(Relationship_.rightPlace)));
} }
return list(context, criteriaQuery, true, Relationship.class, limit, offset); return list(context, criteriaQuery, true, Relationship.class, limit, offset);
} }
@@ -228,24 +320,26 @@ public class RelationshipDAOImpl extends AbstractHibernateDAO<Relationship> impl
} }
@Override @Override
public int countByItemAndRelationshipType(Context context, Item item, RelationshipType relationshipType, public int countByItemAndRelationshipType(
boolean isLeft) throws SQLException { Context context, Item item, RelationshipType relationshipType, boolean isLeft, boolean excludeNonLatest
) 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) { if (isLeft) {
criteriaQuery criteriaQuery.where(
.where(criteriaBuilder.equal(relationshipRoot.get(Relationship_.relationshipType), criteriaBuilder.equal(relationshipRoot.get(Relationship_.relationshipType), relationshipType),
relationshipType), getLeftItemPredicate(criteriaBuilder, relationshipRoot, item, false, excludeNonLatest)
criteriaBuilder.equal(relationshipRoot.get(Relationship_.leftItem), item)); );
} else { } else {
criteriaQuery criteriaQuery.where(
.where(criteriaBuilder.equal(relationshipRoot.get(Relationship_.relationshipType), criteriaBuilder.equal(relationshipRoot.get(Relationship_.relationshipType), relationshipType),
relationshipType), getRightItemPredicate(criteriaBuilder, relationshipRoot, item, false, excludeNonLatest)
criteriaBuilder.equal(relationshipRoot.get(Relationship_.rightItem), item)); );
} }
return count(context, criteriaQuery, criteriaBuilder, relationshipRoot); return count(context, criteriaQuery, criteriaBuilder, relationshipRoot);
} }

View File

@@ -50,6 +50,25 @@ public interface RelationshipService extends DSpaceCRUDService<Relationship> {
List<Relationship> findByItem(Context context, Item item, Integer limit, Integer offset, boolean excludeTilted) List<Relationship> findByItem(Context context, Item item, Integer limit, Integer offset, boolean excludeTilted)
throws SQLException; throws SQLException;
/**
* Retrieves the list of Relationships currently in the system for which the given Item is either
* a leftItem or a rightItem object
* @param context The relevant DSpace context
* @param item The Item that has to be the left or right item for the relationship to be
* included in the list
* @param limit paging limit
* @param offset paging offset
* @param excludeTilted If true, excludes tilted relationships
* @param excludeNonLatest If true, excludes all relationships for which the other item has a more recent version
* that is relevant for this relationship
* @return The list of relationships for which each relationship adheres to the above
* listed constraint
* @throws SQLException If something goes wrong
*/
List<Relationship> findByItem(
Context context, Item item, Integer limit, Integer offset, boolean excludeTilted, boolean excludeNonLatest
) throws SQLException;
/** /**
* Retrieves the full list of relationships currently in the system * Retrieves the full list of relationships currently in the system
* @param context The relevant DSpace context * @param context The relevant DSpace context
@@ -129,6 +148,22 @@ public interface RelationshipService extends DSpaceCRUDService<Relationship> {
RelationshipType relationshipType, int limit, int offset) RelationshipType relationshipType, int limit, int offset)
throws SQLException; throws SQLException;
/**
* This method returns a list of Relationships for which the leftItem or rightItem is equal to the given
* Item object and for which the RelationshipType object is equal to the relationshipType property
* @param context The relevant DSpace context
* @param item The Item object to be matched on the leftItem or rightItem for the relationship
* @param relationshipType The RelationshipType object that will be used to check the Relationship on
* @param excludeNonLatest If true, excludes all relationships for which the other item has a more recent version
* that is relevant for this relationship
* @return The list of Relationship objects that have the given Item object as leftItem or rightItem and
* for which the relationshipType property is equal to the given RelationshipType
* @throws SQLException If something goes wrong
*/
public List<Relationship> findByItemAndRelationshipType(
Context context, Item item, RelationshipType relationshipType, int limit, int offset, boolean excludeNonLatest
) throws SQLException;
/** /**
* This method returns a list of Relationships for which the leftItem or rightItem is equal to the given * This method returns a list of Relationships for which the leftItem or rightItem is equal to the given
* Item object and for which the RelationshipType object is equal to the relationshipType property * Item object and for which the RelationshipType object is equal to the relationshipType property
@@ -145,6 +180,24 @@ public interface RelationshipService extends DSpaceCRUDService<Relationship> {
int limit, int offset) int limit, int offset)
throws SQLException; throws SQLException;
/**
* This method returns a list of Relationships for which the leftItem or rightItem is equal to the given
* Item object and for which the RelationshipType object is equal to the relationshipType property
* @param context The relevant DSpace context
* @param item The Item object to be matched on the leftItem or rightItem for the relationship
* @param relationshipType The RelationshipType object that will be used to check the Relationship on
* @param isLeft Is the item left or right
* @param excludeNonLatest If true, excludes all relationships for which the other item has a more recent version
* that is relevant for this relationship
* @return The list of Relationship objects that have the given Item object as leftItem or rightItem and
* for which the relationshipType property is equal to the given RelationshipType
* @throws SQLException If something goes wrong
*/
public List<Relationship> findByItemAndRelationshipType(
Context context, Item item, RelationshipType relationshipType, boolean isLeft, int limit, int offset,
boolean excludeNonLatest
) throws SQLException;
/** /**
* This method will update the place for the Relationship and all other relationships found by the items and * This method will update the place for the Relationship and all other relationships found by the items and
* relationship type of the given Relationship. It will give this Relationship the last place in both the * relationship type of the given Relationship. It will give this Relationship the last place in both the
@@ -310,6 +363,20 @@ public interface RelationshipService extends DSpaceCRUDService<Relationship> {
*/ */
int countByItem(Context context, Item item) throws SQLException; int countByItem(Context context, Item item) throws SQLException;
/**
* This method returns a count of Relationship objects that have the given Item object
* as a leftItem or a rightItem
* @param context The relevant DSpace context
* @param item The item that should be either a leftItem or a rightItem of all
* the Relationship objects in the returned list
* @param excludeNonLatest if true, exclude relationships for which the opposite item is not the latest version
* that is relevant
* @return The list of Relationship objects that contain either a left or a
* right item that is equal to the given item
* @throws SQLException If something goes wrong
*/
int countByItem(Context context, Item item, boolean excludeNonLatest) throws SQLException;
/** /**
* Count total number of relationships (rows in relationship table) by a relationship type and a boolean indicating * 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 * whether the relationship should contain the item on the left side or not
@@ -323,6 +390,21 @@ public interface RelationshipService extends DSpaceCRUDService<Relationship> {
int countByItemAndRelationshipType(Context context, Item item, RelationshipType relationshipType, boolean isLeft) int countByItemAndRelationshipType(Context context, Item item, RelationshipType relationshipType, boolean isLeft)
throws SQLException; throws SQLException;
/**
* 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 relationshipType relationship type to filter by
* @param isLeft Indicating whether the counted Relationships should have the given Item on the left side
* @param excludeNonLatest If true, excludes all relationships for which the other item has a more recent version
* that is relevant for this relationship
* @return total count with the given parameters
* @throws SQLException if database error
*/
int countByItemAndRelationshipType(
Context context, Item item, RelationshipType relationshipType, boolean isLeft, boolean excludeNonLatest
) throws SQLException;
/** /**
* Count total number of relationships (rows in relationship table) * Count total number of relationships (rows in relationship table)
* by a relationship leftward or rightward typeName * by a relationship leftward or rightward typeName

View File

@@ -155,12 +155,11 @@ public abstract class AbstractHibernateDAO<T> implements GenericDAO<T> {
* @return A list of distinct results as depicted by the CriteriaQuery and parameters * @return A list of distinct results as depicted by the CriteriaQuery and parameters
* @throws SQLException * @throws SQLException
*/ */
public List<T> list(Context context, CriteriaQuery criteriaQuery, boolean cacheable, Class<T> clazz, int maxResults, public List<T> list(
int offset) throws SQLException { Context context, CriteriaQuery<T> criteriaQuery, boolean cacheable, Class<T> clazz, int maxResults, int offset
) throws SQLException {
criteriaQuery.distinct(true); criteriaQuery.distinct(true);
@SuppressWarnings("unchecked") return executeCriteriaQuery(context, criteriaQuery, cacheable, maxResults, offset);
List<T> result = (List<T>) executeCriteriaQuery(context, criteriaQuery, cacheable, maxResults, offset);
return result;
} }
/** /**
@@ -183,12 +182,12 @@ public abstract class AbstractHibernateDAO<T> implements GenericDAO<T> {
* @return A list of results determined by the CriteriaQuery and parameters * @return A list of results determined by the CriteriaQuery and parameters
* @throws SQLException * @throws SQLException
*/ */
public List<T> list(Context context, CriteriaQuery criteriaQuery, boolean cacheable, Class<T> clazz, int maxResults, public List<T> list(
int offset, boolean distinct) throws SQLException { Context context, CriteriaQuery<T> criteriaQuery, boolean cacheable, Class<T> clazz, int maxResults, int offset,
boolean distinct
) throws SQLException {
criteriaQuery.distinct(distinct); criteriaQuery.distinct(distinct);
@SuppressWarnings("unchecked") return executeCriteriaQuery(context, criteriaQuery, cacheable, maxResults, offset);
List<T> result = (List<T>) executeCriteriaQuery(context, criteriaQuery, cacheable, maxResults, offset);
return result;
} }
/** /**