Revision 42184
Added by Michele Artini about 8 years ago
DNetOAICore.java | ||
---|---|---|
4 | 4 |
import java.util.Iterator; |
5 | 5 |
import java.util.List; |
6 | 6 |
import java.util.function.Function; |
7 |
|
|
7 | 8 |
import javax.annotation.Resource; |
8 | 9 |
|
9 |
import eu.dnetlib.oai.*; |
|
10 |
import org.apache.commons.lang3.StringUtils; |
|
11 |
import org.apache.commons.logging.Log; |
|
12 |
import org.apache.commons.logging.LogFactory; |
|
13 |
|
|
14 |
import eu.dnetlib.oai.BadResumptionTokenException; |
|
15 |
import eu.dnetlib.oai.Cursor; |
|
16 |
import eu.dnetlib.oai.NoRecordsMatchException; |
|
17 |
import eu.dnetlib.oai.OAIError; |
|
18 |
import eu.dnetlib.oai.PublisherStore; |
|
19 |
import eu.dnetlib.oai.PublisherStoreDAO; |
|
20 |
import eu.dnetlib.oai.conf.OAIConfigurationReader; |
|
10 | 21 |
import eu.dnetlib.oai.info.ListDocumentsInfo; |
11 | 22 |
import eu.dnetlib.oai.info.RecordInfo; |
12 | 23 |
import eu.dnetlib.oai.info.ResumptionTokenImpl; |
13 | 24 |
import eu.dnetlib.rmi.provision.MDFInfo; |
14 | 25 |
import eu.dnetlib.rmi.provision.OaiPublisherException; |
15 | 26 |
import eu.dnetlib.rmi.provision.OaiPublisherRuntimeException; |
16 |
import org.apache.commons.lang3.StringUtils; |
|
17 |
import org.apache.commons.logging.Log; |
|
18 |
import org.apache.commons.logging.LogFactory; |
|
19 | 27 |
|
20 | 28 |
public class DNetOAICore extends AbstractOAICore { |
21 | 29 |
|
... | ... | |
28 | 36 |
|
29 | 37 |
@Override |
30 | 38 |
protected RecordInfo getRecordById(final MDFInfo mdf, final String id) throws OaiPublisherException { |
31 |
PublisherStore<Cursor> store = this.publisherStoreDAO.getStoreFor(mdf.getPrefix(), getCurrentDBName()); |
|
32 |
if (store == null) |
|
33 |
throw new OaiPublisherRuntimeException("Missing store for metadata prefix " + mdf.getPrefix() + ". Please check OAI publisher configuration.");
|
|
39 |
final PublisherStore<Cursor> store = this.publisherStoreDAO.getStoreFor(mdf.getPrefix(), getCurrentDBName());
|
|
40 |
if (store == null) { throw new OaiPublisherRuntimeException(
|
|
41 |
"Missing store for metadata prefix " + mdf.getPrefix() + ". Please check OAI publisher configuration."); }
|
|
34 | 42 |
RecordInfo record = null; |
35 | 43 |
if (StringUtils.isBlank(mdf.getTransformationRuleID())) { |
36 | 44 |
record = store.getRecord(id); |
37 | 45 |
} else { |
38 |
Function<String, String> function = getLookupClient().getUnaryFunctionFromTDSRule(mdf.getTransformationRuleID()); |
|
46 |
final Function<String, String> function = getLookupClient().getUnaryFunctionFromTDSRule(mdf.getTransformationRuleID());
|
|
39 | 47 |
record = store.getRecord(id, function); |
40 | 48 |
} |
41 | 49 |
if (record != null) { |
... | ... | |
56 | 64 |
final String from, |
57 | 65 |
final String until) |
58 | 66 |
throws OaiPublisherException { |
59 |
MDFInfo mdf = obtainMDFInfo(metadataPrefix); |
|
60 |
boolean hasDateRange = StringUtils.isNotBlank(from) || StringUtils.isNotBlank(until); |
|
61 |
String query = this.generateQuery(mdf, set, from, until, hasDateRange); |
|
62 |
int total = this.countTotal(hasDateRange, query, set, mdf); |
|
67 |
final MDFInfo mdf = obtainMDFInfo(metadataPrefix);
|
|
68 |
final boolean hasDateRange = StringUtils.isNotBlank(from) || StringUtils.isNotBlank(until);
|
|
69 |
final String query = this.generateQuery(mdf, set, from, until, hasDateRange);
|
|
70 |
final int total = this.countTotal(hasDateRange, query, set, mdf);
|
|
63 | 71 |
log.debug("Total number of records: " + total); |
64 |
Cursor results = this.getCursor(query, onlyIdentifiers, mdf); |
|
65 |
ListDocumentsInfo res = this.prepareListDocumentsInfo(results, mdf, query, set, 0, total, hasDateRange); |
|
72 |
final Cursor results = this.getCursor(query, onlyIdentifiers, mdf);
|
|
73 |
final ListDocumentsInfo res = this.prepareListDocumentsInfo(results, mdf, query, set, 0, total, hasDateRange);
|
|
66 | 74 |
log.debug("Delivering " + res.getDocs().size() + " in a page"); |
67 | 75 |
return res; |
68 | 76 |
} |
69 | 77 |
|
70 | 78 |
@Override |
71 | 79 |
protected ListDocumentsInfo getDocuments(final boolean onlyIdentifiers, final String resumptionToken) throws OaiPublisherException { |
72 |
ResumptionTokenImpl resToken = new ResumptionTokenImpl(); |
|
80 |
final ResumptionTokenImpl resToken = new ResumptionTokenImpl();
|
|
73 | 81 |
resToken.deserialize(resumptionToken); |
74 | 82 |
|
75 | 83 |
log.debug(resToken.toString()); |
76 | 84 |
|
77 |
MDFInfo mdf = obtainMDFInfo(resToken.getMetadataPrefix()); |
|
78 |
String lastID = resToken.getLastObjIdentifier(); |
|
79 |
String query = resToken.getQuery(); |
|
85 |
final MDFInfo mdf = obtainMDFInfo(resToken.getMetadataPrefix());
|
|
86 |
final String lastID = resToken.getLastObjIdentifier();
|
|
87 |
final String query = resToken.getQuery();
|
|
80 | 88 |
String newQuery = ""; |
81 | 89 |
if (StringUtils.isNotBlank(query)) { |
82 | 90 |
newQuery = query + " AND "; |
83 | 91 |
} |
84 | 92 |
newQuery += " _id > \"" + lastID + "\""; |
85 | 93 |
log.debug("NEW QUERY BECAUSE of resumptionToken: " + newQuery); |
86 |
int total = this.countTotal(resToken.hasDateRange(), query, resToken.getRequestedSet(), mdf); |
|
87 |
Cursor results = this.getCursor(newQuery, onlyIdentifiers, mdf); |
|
88 |
int oldCount = resToken.getnMaxElements(); |
|
94 |
final int total = this.countTotal(resToken.hasDateRange(), query, resToken.getRequestedSet(), mdf);
|
|
95 |
final Cursor results = this.getCursor(newQuery, onlyIdentifiers, mdf);
|
|
96 |
final int oldCount = resToken.getnMaxElements();
|
|
89 | 97 |
// if the number of records changed, then for sure we can invalidate the resumption token, unless we have a new total of -1 (date |
90 | 98 |
// range queries can't be counted for performance reasons) |
91 |
if ((total != -1) && (oldCount != total)) throw new BadResumptionTokenException(resumptionToken);
|
|
99 |
if ((total != -1) && (oldCount != total)) { throw new BadResumptionTokenException(resumptionToken); }
|
|
92 | 100 |
|
93 |
ListDocumentsInfo res = this.prepareListDocumentsInfo(results, mdf, query, resToken.getRequestedSet(), resToken.getnRead(), resToken.getnMaxElements(), |
|
94 |
resToken.hasDateRange()); |
|
101 |
final ListDocumentsInfo res = |
|
102 |
this.prepareListDocumentsInfo(results, mdf, query, resToken.getRequestedSet(), resToken.getnRead(), resToken.getnMaxElements(), |
|
103 |
resToken.hasDateRange()); |
|
95 | 104 |
res.setCursor(resToken.getnRead()); |
96 | 105 |
return res; |
97 | 106 |
} |
... | ... | |
103 | 112 |
final int read, |
104 | 113 |
final int totalNumber, |
105 | 114 |
final boolean hasDateRange) throws OaiPublisherException { |
106 |
ListDocumentsInfo documentList = new ListDocumentsInfo(); |
|
115 |
final ListDocumentsInfo documentList = new ListDocumentsInfo();
|
|
107 | 116 |
documentList.setnMaxElements(totalNumber); |
108 | 117 |
documentList.setMetadataPrefix(mdf.getPrefix()); |
109 | 118 |
documentList.setCursor(0); |
110 |
if (documentList.getnMaxElements() == 0) throw new NoRecordsMatchException(OAIError.noRecordsMatch.getMessage());
|
|
119 |
if (documentList.getnMaxElements() == 0) { throw new NoRecordsMatchException(OAIError.noRecordsMatch.getMessage()); }
|
|
111 | 120 |
|
112 |
List<RecordInfo> theRecords = this.generateOAIRecords(mdf, requestedSet, results); |
|
121 |
final List<RecordInfo> theRecords = this.generateOAIRecords(mdf, requestedSet, results);
|
|
113 | 122 |
documentList.setDocs(theRecords); |
114 | 123 |
|
115 |
if ((theRecords == null) || theRecords.isEmpty()) throw new NoRecordsMatchException("noRecordsMatch: 'documents' is null or empty");
|
|
124 |
if ((theRecords == null) || theRecords.isEmpty()) { throw new NoRecordsMatchException("noRecordsMatch: 'documents' is null or empty"); }
|
|
116 | 125 |
|
117 | 126 |
if ((documentList.getnMaxElements() > (read + theRecords.size())) || (documentList.getnMaxElements() == -1)) { |
118 |
String lastID = theRecords.get(theRecords.size() - 1).getInternalId(); |
|
119 |
ResumptionTokenImpl nextToken = new ResumptionTokenImpl(); |
|
127 |
final String lastID = theRecords.get(theRecords.size() - 1).getInternalId();
|
|
128 |
final ResumptionTokenImpl nextToken = new ResumptionTokenImpl();
|
|
120 | 129 |
nextToken.setDateRange(hasDateRange); |
121 | 130 |
nextToken.setLastObjIdentifier(lastID); |
122 | 131 |
nextToken.setMetadataPrefix(mdf.getPrefix()); |
... | ... | |
131 | 140 |
} |
132 | 141 |
|
133 | 142 |
protected Cursor getCursor(final String query, final boolean onlyIdentifiers, final MDFInfo mdfInfo) { |
134 |
PublisherStore<Cursor> store = this.publisherStoreDAO.getStore(mdfInfo.getSourceFormatName(), mdfInfo.getSourceFormatInterpretation(), |
|
143 |
final PublisherStore<Cursor> store = this.publisherStoreDAO.getStore(mdfInfo.getSourceFormatName(), mdfInfo.getSourceFormatInterpretation(),
|
|
135 | 144 |
mdfInfo.getSourceFormatLayout(), getCurrentDBName()); |
136 |
if (store == null) |
|
137 |
throw new OaiPublisherRuntimeException("Missing store for metadata prefix " + mdfInfo.getPrefix() + ". Please check OAI publisher configuration.");
|
|
145 |
if (store == null) { throw new OaiPublisherRuntimeException(
|
|
146 |
"Missing store for metadata prefix " + mdfInfo.getPrefix() + ". Please check OAI publisher configuration."); }
|
|
138 | 147 |
Cursor results = null; |
139 | 148 |
if (StringUtils.isBlank(mdfInfo.getTransformationRuleID())) { |
140 |
results = store.getRecords(query, !onlyIdentifiers, pageSize); |
|
149 |
results = store.getRecords(query, !onlyIdentifiers, this.pageSize);
|
|
141 | 150 |
} else { |
142 |
Function<String, String> function = getLookupClient().getUnaryFunctionFromTDSRule(mdfInfo.getTransformationRuleID()); |
|
143 |
results = store.getRecords(query, function, !onlyIdentifiers, pageSize); |
|
151 |
final Function<String, String> function = getLookupClient().getUnaryFunctionFromTDSRule(mdfInfo.getTransformationRuleID());
|
|
152 |
results = store.getRecords(query, function, !onlyIdentifiers, this.pageSize);
|
|
144 | 153 |
} |
145 | 154 |
return results; |
146 | 155 |
} |
... | ... | |
148 | 157 |
/** |
149 | 158 |
* Generates the List of RecordInfo to be delivered. |
150 | 159 |
* |
151 |
* @param mdf MDFInfo, the requested metadata format information. |
|
152 |
* @param requestedSet set specified in the request. It is blank if no set was requested. |
|
153 |
* @param cursor Cursor instance to use to get the records. |
|
160 |
* @param mdf |
|
161 |
* MDFInfo, the requested metadata format information. |
|
162 |
* @param requestedSet |
|
163 |
* set specified in the request. It is blank if no set was requested. |
|
164 |
* @param cursor |
|
165 |
* Cursor instance to use to get the records. |
|
154 | 166 |
* @return List of RecordInfo instances |
155 | 167 |
*/ |
156 | 168 |
protected List<RecordInfo> generateOAIRecords(final MDFInfo mdf, final String requestedSet, final Cursor cursor) { |
157 | 169 |
final List<RecordInfo> documents = new ArrayList<>(); |
158 |
Iterator<RecordInfo> cursorIterator = cursor.iterator(); |
|
170 |
final Iterator<RecordInfo> cursorIterator = cursor.iterator();
|
|
159 | 171 |
while (cursorIterator.hasNext()) { |
160 |
RecordInfo current = cursorIterator.next(); |
|
172 |
final RecordInfo current = cursorIterator.next();
|
|
161 | 173 |
current.addSetspec(requestedSet); |
162 | 174 |
current.setPrefix(mdf.getPrefix()); |
163 | 175 |
documents.add(current); |
... | ... | |
166 | 178 |
} |
167 | 179 |
|
168 | 180 |
protected String generateQuery(final MDFInfo mdf, final String set, final String from, final String until, final boolean hasDateRange) { |
169 |
String datestampIndexName = OAIConfigurationReader.DATESTAMP_FIELD; |
|
181 |
final String datestampIndexName = OAIConfigurationReader.DATESTAMP_FIELD;
|
|
170 | 182 |
|
171 | 183 |
String query = mdf.getBaseQuery(); |
172 | 184 |
if (!StringUtils.isBlank(set)) { |
... | ... | |
209 | 221 |
} |
210 | 222 |
|
211 | 223 |
public String getDefaultDate() { |
212 |
return defaultDate; |
|
224 |
return this.defaultDate;
|
|
213 | 225 |
} |
214 | 226 |
|
215 | 227 |
public void setDefaultDate(final String defaultDate) { |
... | ... | |
217 | 229 |
} |
218 | 230 |
|
219 | 231 |
public PublisherStoreDAO<PublisherStore<Cursor>, Cursor> getPublisherStoreDAO() { |
220 |
return publisherStoreDAO; |
|
232 |
return this.publisherStoreDAO;
|
|
221 | 233 |
} |
222 | 234 |
|
223 | 235 |
public void setPublisherStoreDAO(final PublisherStoreDAO<PublisherStore<Cursor>, Cursor> publisherStoreDAO) { |
... | ... | |
225 | 237 |
} |
226 | 238 |
|
227 | 239 |
public int getPageSize() { |
228 |
return pageSize; |
|
240 |
return this.pageSize;
|
|
229 | 241 |
} |
230 | 242 |
|
231 | 243 |
public void setPageSize(final int pageSize) { |
Also available in: Unified diff
oai import