Project

General

Profile

1
package eu.dnetlib.oai.conf;
2

    
3
import java.io.IOException;
4
import java.util.List;
5
import java.util.stream.Collectors;
6

    
7
import javax.annotation.Resource;
8

    
9
import org.apache.commons.logging.Log;
10
import org.apache.commons.logging.LogFactory;
11

    
12
import com.google.common.collect.Lists;
13

    
14
import eu.dnetlib.oai.PublisherField;
15
import eu.dnetlib.oai.info.SetInfo;
16
import eu.dnetlib.rmi.provision.MDFInfo;
17
import eu.dnetlib.rmi.provision.OaiPublisherRuntimeException;
18

    
19
/**
20
 * Instances of this class reads the OAI configuration from a string, which is the configuration profile passed in as a string.
21
 *
22
 * @author alessia
23
 */
24
public class OAIConfigurationStringReader implements OAIConfigurationReader {
25

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

    
28
	private OAIConfiguration oaiConfiguration;
29
	@Resource
30
	private OAIConfigurationParser configurationParser;
31

    
32
	public OAIConfigurationStringReader() {
33
		this.oaiConfiguration = null;
34
	}
35

    
36
	public OAIConfigurationStringReader(final String profile) {
37
		this.readConfiguration(profile);
38
	}
39

    
40
	public void readConfiguration(final String profile) {
41
		log.debug(profile);
42
		try {
43
			this.oaiConfiguration = this.configurationParser.getConfiguration(profile);
44
		} catch (final IOException e) {
45
			throw new OaiPublisherRuntimeException("Could not read OAI configuration profile", e);
46
		}
47
	}
48

    
49
	public boolean isConfigurationLoaded() {
50
		return this.oaiConfiguration != null;
51
	}
52

    
53
	@Override
54
	public List<SetInfo> getSets() {
55
		if (isConfigurationLoaded()) {
56
			return Lists.newArrayList(this.oaiConfiguration.getSetsMap().values());
57
		} else {
58
			throw new OaiPublisherRuntimeException("Configuration is not loaded");
59
		}
60
	}
61

    
62
	@Override
63
	public List<SetInfo> getSets(final boolean onlyEnabled) {
64

    
65
		if (isConfigurationLoaded()) {
66
			return this.oaiConfiguration.getSetsMap().values().stream().filter(it -> onlyEnabled ? it.isEnabled() : true).collect(
67
					Collectors.toList());
68
		} else {
69
			throw new OaiPublisherRuntimeException("Configuration is not loaded");
70
		}
71
	}
72

    
73
	@Override
74
	public List<String> getSetSpecs() {
75
		if (isConfigurationLoaded()) {
76
			return Lists.newArrayList(this.oaiConfiguration.getSetsMap().keySet());
77
		} else {
78
			throw new OaiPublisherRuntimeException("Configuration is not loaded");
79
		}
80
	}
81

    
82
	@Override
83
	public List<String> getSetSpecs(final boolean onlyEnabled) {
84
		if (isConfigurationLoaded()) {
85
			if (!onlyEnabled) { return this.getSetSpecs(); }
86
			final List<SetInfo> enabled = this.getSets(true);
87
			return enabled.stream().map(it -> it.getSetSpec()).collect(Collectors.toList());
88

    
89
		} else {
90
			throw new OaiPublisherRuntimeException("Configuration is not loaded");
91
		}
92
	}
93

    
94
	@Override
95
	public List<MDFInfo> getSourceMetadataFormats() {
96
		if (isConfigurationLoaded()) {
97
			return this.oaiConfiguration.getSourcesMDF().stream().collect(Collectors.toList());
98
		} else {
99
			throw new OaiPublisherRuntimeException("Configuration is not loaded");
100
		}
101
	}
102

    
103
	@Override
104
	public SetInfo getSetInfo(final String setSpec) {
105
		if (isConfigurationLoaded()) {
106
			return this.oaiConfiguration.getSetsMap().get(setSpec);
107
		} else {
108
			throw new OaiPublisherRuntimeException("Configuration is not loaded");
109
		}
110
	}
111

    
112
	@Override
113
	public List<MDFInfo> getMetadataFormatInfo() {
114
		if (isConfigurationLoaded()) {
115
			return Lists.newArrayList(this.oaiConfiguration.getMdFormatsMap().values());
116
		} else {
117
			throw new OaiPublisherRuntimeException("Configuration is not loaded");
118
		}
119
	}
120

    
121
	@Override
122
	public List<MDFInfo> getMetadataFormatInfo(final boolean onlyEnabled) {
123
		if (isConfigurationLoaded()) {
124
			if (!onlyEnabled) {
125
				return this.getMetadataFormatInfo();
126
			} else {
127
				return this.oaiConfiguration.getMdFormatsMap().values().stream().filter(it -> onlyEnabled ? it.isEnabled() : true).collect(Collectors.toList());
128
			}
129

    
130
		} else {
131
			throw new OaiPublisherRuntimeException("Configuration is not loaded");
132
		}
133
	}
134

    
135
	@Override
136
	public MDFInfo getMetadataFormatInfo(final String mdPrefix) {
137
		if (isConfigurationLoaded()) {
138
			return this.oaiConfiguration.getMdFormatsMap().get(mdPrefix);
139
		} else {
140
			throw new OaiPublisherRuntimeException("Configuration is not loaded");
141
		}
142
	}
143

    
144
	@Override
145
	public List<PublisherField> getFields() {
146
		if (isConfigurationLoaded()) {
147
			return this.oaiConfiguration.getFields();
148
		} else {
149
			throw new OaiPublisherRuntimeException("Configuration is not loaded");
150
		}
151
	}
152

    
153
	@Override
154
	public List<PublisherField> getFields(final String format, final String interpretation, final String layout) {
155
		if (isConfigurationLoaded()) {
156
			return Lists.newArrayList(this.oaiConfiguration.getFieldsFor(format, layout, interpretation));
157
		} else {
158
			throw new OaiPublisherRuntimeException("Configuration is not loaded");
159
		}
160
	}
161

    
162
	@Override
163
	public List<String> getFieldNames() {
164
		if (isConfigurationLoaded()) {
165
			return this.oaiConfiguration.getFieldNames();
166
		} else {
167
			throw new OaiPublisherRuntimeException("Configuration is not loaded");
168
		}
169
	}
170

    
171
	@Override
172
	public List<MDFInfo> getFormatsServedBy(final String sourceFormatName, final String sourceFormatLayout, final String sourceFormatInterpretation) {
173
		if (isConfigurationLoaded()) {
174
			return this.oaiConfiguration.getMdFormatsMap().values().stream()
175
					.filter(mdf -> (mdf.getSourceFormatName() == sourceFormatName) && (mdf.getSourceFormatLayout() == sourceFormatLayout)
176
							&& (mdf.getSourceFormatInterpretation() == sourceFormatInterpretation))
177
					.collect(Collectors.toList());
178
		} else {
179
			throw new OaiPublisherRuntimeException("Configuration is not loaded");
180
		}
181
	}
182

    
183
	@Override
184
	public String getIdScheme() {
185
		if (isConfigurationLoaded()) {
186
			return this.oaiConfiguration.getIdScheme();
187
		} else {
188
			throw new OaiPublisherRuntimeException("Configuration is not loaded");
189
		}
190
	}
191

    
192
	@Override
193
	public String getIdNamespace() {
194
		if (isConfigurationLoaded()) {
195
			return this.oaiConfiguration.getIdNamespace();
196
		} else {
197
			throw new OaiPublisherRuntimeException("Configuration is not loaded");
198
		}
199
	}
200

    
201
	public OAIConfiguration getOaiConfiguration() {
202
		return this.oaiConfiguration;
203
	}
204

    
205
	public void setOaiConfiguration(final OAIConfiguration oaiConfiguration) {
206
		this.oaiConfiguration = oaiConfiguration;
207
	}
208

    
209
	public OAIConfigurationParser getConfigurationParser() {
210
		return this.configurationParser;
211
	}
212

    
213
	public void setConfigurationParser(final OAIConfigurationParser configurationParser) {
214
		this.configurationParser = configurationParser;
215
	}
216

    
217
}
(5-5/9)