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 com.google.common.collect.Lists;
8
import eu.dnetlib.oai.PublisherField;
9
import eu.dnetlib.oai.info.SetInfo;
10
import eu.dnetlib.rmi.provision.MDFInfo;
11
import eu.dnetlib.rmi.provision.OaiPublisherRuntimeException;
12
import eu.dnetlib.utils.MetadataReference;
13
import org.apache.commons.logging.Log;
14
import org.apache.commons.logging.LogFactory;
15
import org.springframework.beans.factory.annotation.Autowired;
16

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

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

    
26
	private OAIConfiguration oaiConfiguration;
27
	@Autowired
28
	private OAIConfigurationParser configurationParser;
29

    
30
	public OAIConfigurationStringReader() {
31
		this.oaiConfiguration = null;
32
	}
33

    
34
	public OAIConfigurationStringReader(final String profile) {
35
		this.readConfiguration(profile);
36
	}
37

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

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

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

    
60
	@Override
61
	public List<SetInfo> getSets(final boolean onlyEnabled) {
62

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
199
	public OAIConfiguration getOaiConfiguration() {
200
		return this.oaiConfiguration;
201
	}
202

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

    
207
	public OAIConfigurationParser getConfigurationParser() {
208
		return this.configurationParser;
209
	}
210

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

    
215
}
(5-5/6)