Skip to content

Commit

Permalink
Quick fix for adding extra set names to the ListIdentifiers/ListRecor…
Browse files Browse the repository at this point in the history
…ds headers, for records that occur in multiple sets.
  • Loading branch information
landreev committed May 1, 2017
1 parent 9fc60ad commit a908321
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,26 @@ public List<OAIRecord> findOaiRecordsBySetName(String setName) {
}

public List<OAIRecord> findOaiRecordsBySetName(String setName, Date from, Date until) {
return findOaiRecordsBySetName(setName, from, until, false);
}

public List<OAIRecord> findOaiRecordsNotInThisSet(String setName, Date from, Date until) {
return findOaiRecordsBySetName(setName, from, until, true);
}

public List<OAIRecord> findOaiRecordsBySetName(String setName, Date from, Date until, boolean excludeSet) {

String queryString ="SELECT object(h) from OAIRecord as h where h.id is not null";
queryString += setName != null ? " and h.setName = :setName" : ""; // where h.setName is null";
if (setName != null) {
if (excludeSet) {
queryString += " and h.setName is not null and h.setName != '' and h.setName != :setName";
} else {
queryString += " and h.setName = :setName";
}
}
queryString += from != null ? " and h.lastUpdateTime >= :from" : "";
queryString += until != null ? " and h.lastUpdateTime<=:until" : "";
queryString += " order by h.globalId";

logger.fine("Query: "+queryString);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public class Xitem implements Item {

public Xitem(OAIRecord oaiRecord) {
super();
this.oaiRecord = oaiRecord;
this.oaiRecord = oaiRecord;
oaisets = new ArrayList<>();
if (oaiRecord.getSetName() != null) {
oaisets.add(new Set(oaiRecord.getSetName()));
}
}

private OAIRecord oaiRecord;
Expand Down Expand Up @@ -70,15 +74,13 @@ public String getIdentifier() {
public Date getDatestamp() {
return oaiRecord.getLastUpdateTime();
}

private List<Set> oaisets;

@Override
public List<com.lyncode.xoai.dataprovider.model.Set> getSets() {
List<Set> sets = new ArrayList<>();
if (oaiRecord.getSetName() != null) {
sets.add(new Set(oaiRecord.getSetName()));
}
public List<Set> getSets() {

return sets;
return oaisets;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
import com.lyncode.xoai.dataprovider.handlers.results.ListItemsResults;
import com.lyncode.xoai.dataprovider.model.Item;
import com.lyncode.xoai.dataprovider.model.ItemIdentifier;
import com.lyncode.xoai.dataprovider.model.Set;
import com.lyncode.xoai.dataprovider.repository.ItemRepository;
import edu.harvard.iq.dataverse.Dataset;
import edu.harvard.iq.dataverse.DatasetServiceBean;
import edu.harvard.iq.dataverse.harvest.server.OAIRecord;
import edu.harvard.iq.dataverse.harvest.server.OAIRecordServiceBean;
import edu.harvard.iq.dataverse.util.StringUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -113,6 +115,14 @@ public ListItemIdentifiersResult getItemIdentifiers(List<ScopedFilter> filters,
OAIRecord record = oaiRecords.get(i);
xoaiItems.add(new Xitem(record));
}

// Run a second pass, looking for records in this set that occur
// in *other* sets. Then we'll add these multiple sets to the
// formatted output in the header:
if (!StringUtil.isEmpty(setSpec)) {
addExtraSets(xoaiItems, setSpec, from, until);
}

boolean hasMore = offset + length < oaiRecords.size();
ListItemIdentifiersResult result = new ListItemIdentifiersResult(hasMore, xoaiItems);
logger.fine("returning result with " + xoaiItems.size() + " items.");
Expand Down Expand Up @@ -180,6 +190,11 @@ public ListItemsResults getItems(List<ScopedFilter> filters, int offset, int len
xoaiItems.add(xItem);
}
}

if (!StringUtil.isEmpty(setSpec)) {
addExtraSets(xoaiItems, setSpec, from, until);
}

boolean hasMore = offset + length < oaiRecords.size();
ListItemsResults result = new ListItemsResults(hasMore, xoaiItems);
logger.fine("returning result with " + xoaiItems.size() + " items.");
Expand All @@ -188,4 +203,36 @@ public ListItemsResults getItems(List<ScopedFilter> filters, int offset, int len

return new ListItemsResults(false, xoaiItems);
}

private void addExtraSets(Object xoaiItemsList, String setSpec, Date from, Date until) {

List<Xitem> xoaiItems = (List<Xitem>)xoaiItemsList;

List<OAIRecord> oaiRecords = recordService.findOaiRecordsNotInThisSet(setSpec, from, until);

if (oaiRecords == null && oaiRecords.isEmpty()) {
return;
}

// Make a second pass through the list of xoaiItems already found for this set,
// and add any other sets in which this item occurs:

int j = 0;
for (int i = 0; i < xoaiItems.size(); i++) {
// fast-forward the second list, until we find a record with this identifier,
// or until we are past this record (both lists are sorted alphabetically by
// the identifier:
Xitem xitem = xoaiItems.get(i);

while (j < oaiRecords.size() && xitem.getIdentifier().compareTo(oaiRecords.get(j).getGlobalId()) > 0) {
j++;
}

while (j < oaiRecords.size() && xitem.getIdentifier().equals(oaiRecords.get(j).getGlobalId())) {
xoaiItems.get(i).getSets().add(new Set(oaiRecords.get(j).getSetName()));
j++;
}
}

}
}

0 comments on commit a908321

Please sign in to comment.