Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick fix for adding extra set names to the ListIdentifiers/ListRecords #3806

Merged
merged 3 commits into from
May 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public OAIRecord findOAIRecordBySetNameandGlobalId(String setName, String global
}

public List<OAIRecord> findOaiRecordsByGlobalId(String globalId) {
String query="SELECT h from OAIRecord as h where h.globalId = :globalId";
String query="SELECT object(h) from OAIRecord h where h.globalId = :globalId";
List<OAIRecord> oaiRecords = null;
try {
oaiRecords = em.createQuery(query).setParameter("globalId",globalId).getResultList();
Expand All @@ -303,11 +303,28 @@ 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";
String queryString ="SELECT object(h) from OAIRecord h where h.id is not 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";
}
} else {
queryString += " and h.setName is null";
}
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 @@ -47,12 +49,26 @@ public XitemRepository (OAIRecordServiceBean recordService, DatasetServiceBean d

@Override
public Item getItem(String identifier) throws IdDoesNotExistException, OAIException {
logger.fine("getItem; calling findOAIRecordBySetNameandGlobalId, identifier " + identifier);
OAIRecord oaiRecord = recordService.findOAIRecordBySetNameandGlobalId(null, identifier);
if (oaiRecord != null) {
Dataset dataset = datasetService.findByGlobalId(oaiRecord.getGlobalId());
if (dataset != null) {
return new Xitem(oaiRecord).withDataset(dataset);
logger.fine("getItem; calling findOaiRecordsByGlobalId, identifier " + identifier);
List<OAIRecord> oaiRecords = recordService.findOaiRecordsByGlobalId(identifier);
if (oaiRecords != null && !oaiRecords.isEmpty()) {
Xitem xoaiItem = null;
for (OAIRecord record : oaiRecords) {
if (xoaiItem == null) {
Dataset dataset = datasetService.findByGlobalId(record.getGlobalId());
if (dataset != null) {
xoaiItem = new Xitem(record).withDataset(dataset);
}
} else {
// Adding extra set specs to the XOAI Item, if this record
// is part of multiple sets:
if (!StringUtil.isEmpty(record.getSetName())) {
xoaiItem.getSets().add(new Set(record.getSetName()));
}
}
}
if (xoaiItem != null) {
return xoaiItem;
}
}

Expand Down Expand Up @@ -113,6 +129,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 +204,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 +217,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++;
}
}

}
}