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
	@Autowired
34
	private SetCollection setCollection;
35
	@Autowired
36
	private OAIConfigurationExistReader oaiConfigurationExistReader;
37
	@Autowired
38
	private OAIISLookUpClient lookupClient;
39
	private Cache mdFormatsCache;
40

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

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

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

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

    
90
		if (onlyIdentifiers) {
91
			res.setIdentifiers(documents.getDocs());
92
		} else {
93
			res.setDocuments(documents.getDocs());
94
		}
95

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

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

    
115
		final ListRecordsInfo res = new ListRecordsInfo();
116

    
117
		if (docs.getMetadataPrefix() != null) {
118
			res.setMetadataprefix(docs.getMetadataPrefix());
119
		}
120

    
121
		if (onlyIdentifiers) {
122
			res.setIdentifiers(docs.getDocs());
123
		} else {
124
			res.setDocuments(docs.getDocs());
125
		}
126

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

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

    
141
	public List<MDFInfo> listMetadataFormats() throws OaiPublisherException {
142
		return this.oaiConfigurationExistReader.getMetadataFormatInfo(true);
143
	}
144

    
145
	public boolean existSet(final String setSpec) {
146
		return this.setCollection.containEnabledSet(setSpec, getCurrentDBName());
147
	}
148

    
149
	protected MDFInfo obtainMDFInfo(final String metadataPrefix) throws OaiPublisherException {
150

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

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

    
175
	protected abstract RecordInfo getRecordById(final MDFInfo mdf, final String id) throws OaiPublisherException;
176

    
177
	protected abstract ListDocumentsInfo getDocuments(final boolean onlyIdentifiers,
178
			final String set,
179
			final String metadataPrefix,
180
			final String from,
181
			final String until) throws OaiPublisherException;
182

    
183
	protected abstract ListDocumentsInfo getDocuments(final boolean onlyIdentifiers, final String resumptionToken) throws OaiPublisherException;
184

    
185
	public String getCurrentDBName() {
186
		return currentDBName;
187
	}
188

    
189
	public void setCurrentDBName(final String currentDBName) {
190
		this.currentDBName = currentDBName;
191
	}
192

    
193
	public SetCollection getSetCollection() {
194
		return setCollection;
195
	}
196

    
197
	public void setSetCollection(final SetCollection setCollection) {
198
		this.setCollection = setCollection;
199
	}
200

    
201
	public OAIConfigurationExistReader getOaiConfigurationExistReader() {
202
		return oaiConfigurationExistReader;
203
	}
204

    
205
	public void setOaiConfigurationExistReader(final OAIConfigurationExistReader oaiConfigurationExistReader) {
206
		this.oaiConfigurationExistReader = oaiConfigurationExistReader;
207
	}
208

    
209
	public OAIISLookUpClient getLookupClient() {
210
		return lookupClient;
211
	}
212

    
213
	public void setLookupClient(final OAIISLookUpClient lookupClient) {
214
		this.lookupClient = lookupClient;
215
	}
216

    
217
	public Cache getMdFormatsCache() {
218
		return mdFormatsCache;
219
	}
220

    
221
	@Required
222
	public void setMdFormatsCache(final Cache mdFormatsCache) {
223
		this.mdFormatsCache = mdFormatsCache;
224
	}
225
}
(1-1/3)