Skip to content

Commit

Permalink
Merge pull request DSpace#2501 from atmire/DS-3849
Browse files Browse the repository at this point in the history
[DS-3849] REST API items resource returns items in non-deterministic order (further 6.x changes)
  • Loading branch information
kshepherd committed Feb 19, 2020
2 parents 1d09f30 + 4eafb8c commit 6fc3125
Showing 1 changed file with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
Expand Down Expand Up @@ -50,7 +51,7 @@ protected ItemDAOImpl()

@Override
public Iterator<Item> findAll(Context context, boolean archived) throws SQLException {
Query query = createQuery(context, "FROM Item WHERE inArchive= :in_archive");
Query query = createQuery(context, "FROM Item WHERE inArchive= :in_archive order by id");
query.setParameter("in_archive", archived);
return iterate(query);
}
Expand Down Expand Up @@ -78,6 +79,8 @@ public Iterator<Item> findAll(Context context, boolean archived,
queryStr.append(" AND last_modified > :last_modified");
}

queryStr.append(" order by id");

Query query = createQuery(context, queryStr.toString());
query.setParameter("in_archive", archived);
query.setParameter("withdrawn", withdrawn);
Expand All @@ -91,7 +94,7 @@ public Iterator<Item> findAll(Context context, boolean archived,

@Override
public Iterator<Item> findBySubmitter(Context context, EPerson eperson) throws SQLException {
Query query = createQuery(context, "FROM Item WHERE inArchive= :in_archive and submitter= :submitter");
Query query = createQuery(context, "FROM Item WHERE inArchive= :in_archive and submitter= :submitter order by id");
query.setParameter("in_archive", true);
query.setParameter("submitter", eperson);
return iterate(query);
Expand Down Expand Up @@ -195,14 +198,16 @@ public Iterator<Item> findByMetadataQuery(Context context, List<List<MetadataFie
criteria.add(Subqueries.notExists(subcriteria));
}
}
criteria.addOrder(Order.asc("item.id"));

log.debug(String.format("Running custom query with %d filters", index));

return list(criteria).iterator();
}

@Override
public Iterator<Item> findByAuthorityValue(Context context, MetadataField metadataField, String authority, boolean inArchive) throws SQLException {
Query query = createQuery(context, "SELECT item FROM Item as item join item.metadata metadatavalue WHERE item.inArchive=:in_archive AND metadatavalue.metadataField = :metadata_field AND metadatavalue.authority = :authority");
Query query = createQuery(context, "SELECT item FROM Item as item join item.metadata metadatavalue WHERE item.inArchive=:in_archive AND metadatavalue.metadataField = :metadata_field AND metadatavalue.authority = :authority order by item.id");
query.setParameter("in_archive", inArchive);
query.setParameter("metadata_field", metadataField);
query.setParameter("authority", authority);
Expand All @@ -227,15 +232,15 @@ public Iterator<Item> findArchivedByCollection(Context context, Collection colle

@Override
public Iterator<Item> findAllByCollection(Context context, Collection collection) throws SQLException {
Query query = createQuery(context, "select i from Item i join i.collections c WHERE :collection IN c");
Query query = createQuery(context, "select i from Item i join i.collections c WHERE :collection IN c order by i.id");
query.setParameter("collection", collection);

return iterate(query);
}

@Override
public Iterator<Item> findAllByCollection(Context context, Collection collection, Integer limit, Integer offset) throws SQLException {
Query query = createQuery(context, "select i from Item i join i.collections c WHERE :collection IN c");
Query query = createQuery(context, "select i from Item i join i.collections c WHERE :collection IN c order by i.id");
query.setParameter("collection", collection);

if(offset != null)
Expand Down Expand Up @@ -280,7 +285,7 @@ public int countItems(Context context, List<Collection> collections, boolean inc
public Iterator<Item> findByLastModifiedSince(Context context, Date since)
throws SQLException
{
Query query = createQuery(context, "SELECT i FROM item i WHERE last_modified > :last_modified");
Query query = createQuery(context, "SELECT i FROM item i WHERE last_modified > :last_modified order by id");
query.setTimestamp("last_modified", since);
return iterate(query);
}
Expand Down

0 comments on commit 6fc3125

Please sign in to comment.