Project

General

Profile

1
package eu.dnetlib.oai.core;
2

    
3
import java.util.List;
4

    
5
import eu.dnetlib.oai.CannotDisseminateFormatException;
6
import eu.dnetlib.oai.conf.OAIConfigurationExistReader;
7
import eu.dnetlib.oai.conf.OAIISLookUpClient;
8
import eu.dnetlib.oai.info.*;
9
import eu.dnetlib.oai.sets.SetCollection;
10
import eu.dnetlib.rmi.provision.MDFInfo;
11
import eu.dnetlib.rmi.provision.OaiPublisherException;
12
import net.sf.ehcache.Cache;
13
import net.sf.ehcache.Element;
14
import org.apache.commons.logging.Log;
15
import org.apache.commons.logging.LogFactory;
16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.beans.factory.annotation.Required;
18

    
19
/**
20
 * Core OAI servlet implementation.
21
 *
22
 * @author michele, alessia
23
 */
24

    
25
public abstract class AbstractOAICore {
26

    
27
	private static final Log log = LogFactory.getLog(AbstractOAICore.class); // NOPMD by marko on 11/24/08 5:02 PM
28
	/**
29
	 * page size.
30
	 */
31
	protected int pageSize = 100;
32
	private String currentDBName;
33

    
34
	@Autowired
35
	private SetCollection setCollection;
36
	@Autowired
37
	private OAIConfigurationExistReader oaiConfigurationExistReader;
38

    
39
	@Autowired
40
	private OAIISLookUpClient lookupClient;
41

    
42
	private Cache mdFormatsCache;
43

    
44
	/**
45
	 * Returns informations about a record. It contains basic information about a record, such as "prefix", "identifier", "datestamp",
46
	 * "setspec", "metadata".
47
	 *
48
	 * @param identifier record identifier
49
	 * @param prefix     metadata prefix
50
	 * @return a recordInfo instance
51
	 * @throws OaiPublisherException could happen
52
	 */
53
	public RecordInfo getInfoRecord(final String identifier, final String prefix) throws OaiPublisherException {
54
		MDFInfo mdf = obtainMDFInfo(prefix);
55
		return getRecordById(mdf, identifier);
56
	}
57

    
58
	/**
59
	 * List records.
60
	 *
61
	 * @param onlyIdentifiers only return record identifiers
62
	 * @param metadataPrefix  metadata prefix
63
	 * @param set             set name
64
	 * @param from            from date
65
	 * @param until           to date
66
	 * @return ListRecordsInfo
67
	 * @throws OaiPublisherException could happen
68
	 */
69
	public ListRecordsInfo listRecords(final boolean onlyIdentifiers, final String metadataPrefix, final String set, final String from, final String until)
70
			throws OaiPublisherException {
71
		final ListRecordsInfo res = new ListRecordsInfo();
72

    
73
		if (from != null) {
74
			res.setFrom(from);
75
			res.setWithSize(false);
76
		}
77
		if (until != null) {
78
			res.setUntil(until);
79
			res.setWithSize(false);
80
		}
81
		if (metadataPrefix != null) {
82
			res.setMetadataprefix(metadataPrefix);
83
		}
84
		if (set != null) {
85
			res.setSet(set);
86
		}
87

    
88
		ListDocumentsInfo documents = getDocuments(onlyIdentifiers, set, metadataPrefix, from, until);
89
		ResumptionToken resumptionToken = documents.getResumptionToken();
90
		int cursor = documents.getCursor();
91
		int nMaxElements = documents.getnMaxElements();
92

    
93
		if (onlyIdentifiers) {
94
			res.setIdentifiers(documents.getDocs());
95
		} else {
96
			res.setDocuments(documents.getDocs());
97
		}
98

    
99
		if ((resumptionToken != null)) {
100
			res.setResumptiontoken(resumptionToken.serialize());
101
			res.setCursor(cursor);
102
			res.setSize(nMaxElements);
103
		}
104
		return res;
105
	}
106

    
107
	/**
108
	 * List records.
109
	 *
110
	 * @param onlyIdentifiers only resource identifiers.
111
	 * @param resumptionToken resumption token
112
	 * @return ListRecordsInfo
113
	 * @throws OaiPublisherException could happen
114
	 */
115
	public ListRecordsInfo listRecords(final boolean onlyIdentifiers, final String resumptionToken) throws OaiPublisherException {
116
		ListDocumentsInfo docs = getDocuments(onlyIdentifiers, resumptionToken);
117

    
118
		final ListRecordsInfo res = new ListRecordsInfo();
119

    
120
		if (docs.getMetadataPrefix() != null) {
121
			res.setMetadataprefix(docs.getMetadataPrefix());
122
		}
123

    
124
		if (onlyIdentifiers) {
125
			res.setIdentifiers(docs.getDocs());
126
		} else {
127
			res.setDocuments(docs.getDocs());
128
		}
129

    
130
		ResumptionToken newResumptionToken = docs.getResumptionToken();
131
		if ((newResumptionToken != null)) {
132
			res.setResumptiontoken(newResumptionToken.serialize());
133
			res.setCursor(docs.getCursor());
134
			res.setSize(docs.getnMaxElements());
135
			res.setWithSize(!newResumptionToken.hasDateRange());
136
		}
137
		return res;
138
	}
139

    
140
	public List<? extends SetInfo> listSets() throws OaiPublisherException {
141
		return this.setCollection.getAllSets(true, getCurrentDBName());
142
	}
143

    
144
	public List<MDFInfo> listMetadataFormats() throws OaiPublisherException {
145
		return this.oaiConfigurationExistReader.getMetadataFormatInfo(true);
146
	}
147

    
148
	public boolean existSet(final String setSpec) {
149
		return this.setCollection.containEnabledSet(setSpec, getCurrentDBName());
150
	}
151

    
152
	protected MDFInfo obtainMDFInfo(final String metadataPrefix) throws OaiPublisherException {
153

    
154
		MDFInfo mdf = null;
155
		final Element element = this.mdFormatsCache.get(metadataPrefix);
156
		if (element == null) {
157
			log.fatal("Not using cache for " + metadataPrefix);
158
			mdf = oaiConfigurationExistReader.getMetadataFormatInfo(metadataPrefix);
159
			if (mdf == null) throw new CannotDisseminateFormatException("Invalid metadataPrefix " + metadataPrefix);
160
			this.mdFormatsCache.put(new Element(metadataPrefix, mdf));
161
		} else {
162
			mdf = (MDFInfo) element.getObjectValue();
163
		}
164
		return mdf;
165
	}
166

    
167
	/**
168
	 * Set the current DB from the configuration on the IS.
169
	 */
170
	public void setCurrentDBFromIS() throws OaiPublisherException {
171
		try {
172
			currentDBName = getLookupClient().getCurrentDB();
173
		} catch (Exception e) {
174
			throw new OaiPublisherException(e);
175
		}
176
	}
177

    
178
	protected abstract RecordInfo getRecordById(final MDFInfo mdf, final String id) throws OaiPublisherException;
179

    
180
	protected abstract ListDocumentsInfo getDocuments(final boolean onlyIdentifiers,
181
			final String set,
182
			final String metadataPrefix,
183
			final String from,
184
			final String until) throws OaiPublisherException;
185

    
186
	protected abstract ListDocumentsInfo getDocuments(final boolean onlyIdentifiers, final String resumptionToken) throws OaiPublisherException;
187

    
188
	public String getCurrentDBName() {
189
		return currentDBName;
190
	}
191

    
192
	public void setCurrentDBName(final String currentDBName) {
193
		this.currentDBName = currentDBName;
194
	}
195

    
196
	public SetCollection getSetCollection() {
197
		return setCollection;
198
	}
199

    
200
	public void setSetCollection(final SetCollection setCollection) {
201
		this.setCollection = setCollection;
202
	}
203

    
204
	public OAIConfigurationExistReader getOaiConfigurationExistReader() {
205
		return oaiConfigurationExistReader;
206
	}
207

    
208
	public void setOaiConfigurationExistReader(final OAIConfigurationExistReader oaiConfigurationExistReader) {
209
		this.oaiConfigurationExistReader = oaiConfigurationExistReader;
210
	}
211

    
212
	public OAIISLookUpClient getLookupClient() {
213
		return lookupClient;
214
	}
215

    
216
	public void setLookupClient(final OAIISLookUpClient lookupClient) {
217
		this.lookupClient = lookupClient;
218
	}
219

    
220
	public Cache getMdFormatsCache() {
221
		return mdFormatsCache;
222
	}
223

    
224
	@Required
225
	public void setMdFormatsCache(final Cache mdFormatsCache) {
226
		this.mdFormatsCache = mdFormatsCache;
227
	}
228
}
(1-1/3)