Project

General

Profile

1
package eu.dnetlib.data.information.oai.publisher.core;
2

    
3
import java.util.List;
4

    
5
import javax.annotation.Resource;
6

    
7
import org.apache.commons.logging.Log;
8
import org.apache.commons.logging.LogFactory;
9
import org.springframework.beans.factory.annotation.Autowired;
10

    
11
import eu.dnetlib.data.information.oai.publisher.CannotDisseminateFormatException;
12
import eu.dnetlib.data.information.oai.publisher.OaiPublisherException;
13
import eu.dnetlib.data.information.oai.publisher.conf.ISLookUpClient;
14
import eu.dnetlib.data.information.oai.publisher.conf.OAIConfigurationExistReader;
15
import eu.dnetlib.data.information.oai.publisher.info.ListDocumentsInfo;
16
import eu.dnetlib.data.information.oai.publisher.info.ListRecordsInfo;
17
import eu.dnetlib.data.information.oai.publisher.info.MDFInfo;
18
import eu.dnetlib.data.information.oai.publisher.info.RecordInfo;
19
import eu.dnetlib.data.information.oai.publisher.info.ResumptionToken;
20
import eu.dnetlib.data.information.oai.publisher.info.SetInfo;
21
import eu.dnetlib.data.information.oai.sets.SetCollection;
22
import eu.dnetlib.miscutils.cache.EhCache;
23

    
24
/**
25
 * Core OAI servlet implementation.
26
 * 
27
 * @author michele,alessia
28
 * 
29
 */
30

    
31
public abstract class AbstractOAICore {
32

    
33
	private static final Log log = LogFactory.getLog(AbstractOAICore.class); // NOPMD by marko on 11/24/08 5:02 PM
34

    
35
	private String currentDBName;
36

    
37
	@Autowired
38
	private SetCollection setCollection;
39

    
40
	@Autowired
41
	private OAIConfigurationExistReader oaiConfigurationExistReader;
42

    
43
	@Autowired
44
	private ISLookUpClient lookupClient;
45

    
46
	@Resource
47
	private EhCache<String, MDFInfo> mdFormatsCache;
48

    
49
	/**
50
	 * page size.
51
	 */
52
	protected int pageSize = 100;
53

    
54
	/**
55
	 * Returns informations about a record. It contains basic information about a record, such as "prefix", "identifier", "datestamp",
56
	 * "setspec", "metadata".
57
	 * 
58
	 * @param identifier
59
	 *            record identifier
60
	 * @param prefix
61
	 *            metadata prefix
62
	 * @return a recordInfo instance
63
	 * @throws OaiPublisherException
64
	 *             could happen
65
	 */
66
	public RecordInfo getInfoRecord(final String identifier, final String prefix) throws OaiPublisherException {
67
		MDFInfo mdf = obtainMDFInfo(prefix);
68
		return getRecordById(mdf, identifier);
69
	}
70

    
71
	/**
72
	 * List records.
73
	 * 
74
	 * @param onlyIdentifiers
75
	 *            only return record identifiers
76
	 * @param metadataPrefix
77
	 *            metadata prefix
78
	 * @param set
79
	 *            set name
80
	 * @param from
81
	 *            from date
82
	 * @param until
83
	 *            to date
84
	 * @return ListRecordsInfo
85
	 * @throws OaiPublisherException
86
	 *             could happen
87
	 */
88
	public ListRecordsInfo listRecords(final boolean onlyIdentifiers, final String metadataPrefix, final String set, final String from, final String until)
89
			throws OaiPublisherException {
90
		final ListRecordsInfo res = new ListRecordsInfo();
91

    
92
		if (from != null) {
93
			res.setFrom(from);
94
			res.setWithSize(false);
95
		}
96
		if (until != null) {
97
			res.setUntil(until);
98
			res.setWithSize(false);
99
		}
100
		if (metadataPrefix != null) {
101
			res.setMetadataprefix(metadataPrefix);
102
		}
103
		if (set != null) {
104
			res.setSet(set);
105
		}
106

    
107
		ListDocumentsInfo documents = getDocuments(onlyIdentifiers, set, metadataPrefix, from, until);
108
		ResumptionToken resumptionToken = documents.getResumptionToken();
109
		int cursor = documents.getCursor();
110
		int nMaxElements = documents.getnMaxElements();
111

    
112
		if (onlyIdentifiers) {
113
			res.setIdentifiers(documents.getDocs());
114
		} else {
115
			res.setDocuments(documents.getDocs());
116
		}
117

    
118
		if ((resumptionToken != null)) {
119
			res.setResumptiontoken(resumptionToken.serialize());
120
			res.setCursor(cursor);
121
			res.setSize(nMaxElements);
122
		}
123
		return res;
124
	}
125

    
126
	/**
127
	 * List records.
128
	 * 
129
	 * @param onlyIdentifiers
130
	 *            only resource identifiers.
131
	 * @param resumptionToken
132
	 *            resumption token
133
	 * @return ListRecordsInfo
134
	 * @throws OaiPublisherException
135
	 *             could happen
136
	 */
137
	public ListRecordsInfo listRecords(final boolean onlyIdentifiers, final String resumptionToken) throws OaiPublisherException {
138
		ListDocumentsInfo docs = getDocuments(onlyIdentifiers, resumptionToken);
139

    
140
		final ListRecordsInfo res = new ListRecordsInfo();
141

    
142
		if (docs.getMetadataPrefix() != null) {
143
			res.setMetadataprefix(docs.getMetadataPrefix());
144
		}
145

    
146
		if (onlyIdentifiers) {
147
			res.setIdentifiers(docs.getDocs());
148
		} else {
149
			res.setDocuments(docs.getDocs());
150
		}
151

    
152
		ResumptionToken newResumptionToken = docs.getResumptionToken();
153
		if ((newResumptionToken != null)) {
154
			res.setResumptiontoken(newResumptionToken.serialize());
155
			res.setCursor(docs.getCursor());
156
			res.setSize(docs.getnMaxElements());
157
			res.setWithSize(!newResumptionToken.hasDateRange());
158
		}
159
		return res;
160
	}
161

    
162
	public List<? extends SetInfo> listSets() {
163
		return this.setCollection.getAllSets(true, getCurrentDBName());
164
	}
165

    
166
	public List<MDFInfo> listMetadataFormats() {
167
		return this.oaiConfigurationExistReader.getMetadataFormatInfo(true);
168
	}
169

    
170
	public boolean existSet(final String setSpec) {
171
		return this.setCollection.containEnabledSet(setSpec, getCurrentDBName());
172
	}
173

    
174
	protected MDFInfo obtainMDFInfo(final String metadataPrefix) throws OaiPublisherException {
175
		MDFInfo mdf = this.mdFormatsCache.get(metadataPrefix);
176
		if (mdf == null) {
177
			log.fatal("Not using cache for " + metadataPrefix);
178
			mdf = oaiConfigurationExistReader.getMetadataFormatInfo(metadataPrefix);
179
			if (mdf == null) throw new CannotDisseminateFormatException("Invalid metadataPrefix " + metadataPrefix);
180
			this.mdFormatsCache.put(metadataPrefix, mdf);
181
		}
182
		return mdf;
183
	}
184

    
185
	/**
186
	 * Set the current DB from the configuration on the IS.
187
	 */
188
	public void setCurrentDBFromIS() throws OaiPublisherException {
189
		try {
190
			currentDBName = getLookupClient().getCurrentDB();
191
		} catch (Exception e) {
192
			throw new OaiPublisherException(e);
193
		}
194
	}
195

    
196
	protected abstract RecordInfo getRecordById(final MDFInfo mdf, final String id) throws OaiPublisherException;
197

    
198
	protected abstract ListDocumentsInfo getDocuments(final boolean onlyIdentifiers,
199
			final String set,
200
			final String metadataPrefix,
201
			final String from,
202
			final String until) throws OaiPublisherException;
203

    
204
	protected abstract ListDocumentsInfo getDocuments(final boolean onlyIdentifiers, final String resumptionToken) throws OaiPublisherException;
205

    
206
	public String getCurrentDBName() {
207
		return currentDBName;
208
	}
209

    
210
	public void setCurrentDBName(final String currentDBName) {
211
		this.currentDBName = currentDBName;
212
	}
213

    
214
	public SetCollection getSetCollection() {
215
		return setCollection;
216
	}
217

    
218
	public void setSetCollection(final SetCollection setCollection) {
219
		this.setCollection = setCollection;
220
	}
221

    
222
	public OAIConfigurationExistReader getOaiConfigurationExistReader() {
223
		return oaiConfigurationExistReader;
224
	}
225

    
226
	public void setOaiConfigurationExistReader(final OAIConfigurationExistReader oaiConfigurationExistReader) {
227
		this.oaiConfigurationExistReader = oaiConfigurationExistReader;
228
	}
229

    
230
	public ISLookUpClient getLookupClient() {
231
		return lookupClient;
232
	}
233

    
234
	public void setLookupClient(final ISLookUpClient lookupClient) {
235
		this.lookupClient = lookupClient;
236
	}
237

    
238
	public EhCache<String, MDFInfo> getMdFormatsCache() {
239
		return mdFormatsCache;
240
	}
241

    
242
	public void setMdFormatsCache(final EhCache<String, MDFInfo> mdFormatsCache) {
243
		this.mdFormatsCache = mdFormatsCache;
244
	}
245
}
    (1-1/1)