1
|
package eu.dnetlib.data.mapreduce.hbase.oai.config;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.util.List;
|
5
|
|
6
|
import org.apache.commons.logging.Log;
|
7
|
import org.apache.commons.logging.LogFactory;
|
8
|
|
9
|
import com.google.common.base.Function;
|
10
|
import com.google.common.base.Predicate;
|
11
|
import com.google.common.collect.Iterables;
|
12
|
import com.google.common.collect.Lists;
|
13
|
|
14
|
import eu.dnetlib.data.mapreduce.hbase.oai.utils.MDFInfo;
|
15
|
import eu.dnetlib.data.mapreduce.hbase.oai.utils.OaiPublisherRuntimeException;
|
16
|
import eu.dnetlib.data.mapreduce.hbase.oai.utils.PublisherField;
|
17
|
import eu.dnetlib.data.mapreduce.hbase.oai.utils.SetInfo;
|
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
|
*/
|
25
|
public class OAIConfigurationStringReader implements OAIConfigurationReader {
|
26
|
|
27
|
private static final Log log = LogFactory.getLog(OAIConfigurationStringReader.class); // NOPMD by marko on 11/24/08 5:02 PM
|
28
|
|
29
|
private OAIConfiguration oaiConfiguration;
|
30
|
|
31
|
private OAIConfigurationParser configurationParser = new OAIConfigurationParser();
|
32
|
|
33
|
public OAIConfigurationStringReader() {
|
34
|
oaiConfiguration = null;
|
35
|
}
|
36
|
|
37
|
public OAIConfigurationStringReader(final String profile) {
|
38
|
this.readConfiguration(profile);
|
39
|
}
|
40
|
|
41
|
public void readConfiguration(final String profile) {
|
42
|
log.info("Reading OAI configuration profile");
|
43
|
log.debug(profile);
|
44
|
try {
|
45
|
oaiConfiguration = configurationParser.getConfiguration(profile);
|
46
|
} catch (IOException e) {
|
47
|
throw new OaiPublisherRuntimeException("Could not read OAI configuration profile", e);
|
48
|
}
|
49
|
}
|
50
|
|
51
|
public boolean isConfigurationLoaded() {
|
52
|
return this.oaiConfiguration != null;
|
53
|
}
|
54
|
|
55
|
@Override
|
56
|
public List<SetInfo> getSets() {
|
57
|
if (isConfigurationLoaded()) return Lists.newArrayList(this.oaiConfiguration.getSetsMap().values());
|
58
|
else throw new OaiPublisherRuntimeException("Configuration is not loaded");
|
59
|
}
|
60
|
|
61
|
@Override
|
62
|
public List<SetInfo> getSets(final boolean onlyEnabled) {
|
63
|
if (isConfigurationLoaded()) return Lists.newArrayList(Iterables.filter(this.oaiConfiguration.getSetsMap().values(), new Predicate<SetInfo>() {
|
64
|
|
65
|
/**
|
66
|
* {@inheritDoc}
|
67
|
*
|
68
|
* @see com.google.common.base.Predicate#apply(java.lang.Object)
|
69
|
*/
|
70
|
@Override
|
71
|
public boolean apply(final SetInfo setInfo) {
|
72
|
if (onlyEnabled) return setInfo.isEnabled();
|
73
|
else return true;
|
74
|
}
|
75
|
|
76
|
}));
|
77
|
else throw new OaiPublisherRuntimeException("Configuration is not loaded");
|
78
|
}
|
79
|
|
80
|
@Override
|
81
|
public List<String> getSetSpecs() {
|
82
|
if (isConfigurationLoaded()) return Lists.newArrayList(this.oaiConfiguration.getSetsMap().keySet());
|
83
|
else throw new OaiPublisherRuntimeException("Configuration is not loaded");
|
84
|
}
|
85
|
|
86
|
@Override
|
87
|
public List<String> getSetSpecs(final boolean onlyEnabled) {
|
88
|
if (isConfigurationLoaded()) {
|
89
|
if (!onlyEnabled) return this.getSetSpecs();
|
90
|
List<SetInfo> enabled = this.getSets(true);
|
91
|
return Lists.newArrayList(Iterables.transform(enabled, new Function<SetInfo, String>() {
|
92
|
|
93
|
@Override
|
94
|
public String apply(final SetInfo setInfo) {
|
95
|
return setInfo.getSetSpec();
|
96
|
}
|
97
|
|
98
|
}));
|
99
|
} else throw new OaiPublisherRuntimeException("Configuration is not loaded");
|
100
|
}
|
101
|
|
102
|
@Override
|
103
|
public List<MDFInfo> getSourceMetadataFormats() {
|
104
|
if (isConfigurationLoaded()) return Lists.newArrayList(this.oaiConfiguration.getSourcesMDF());
|
105
|
else throw new OaiPublisherRuntimeException("Configuration is not loaded");
|
106
|
}
|
107
|
|
108
|
@Override
|
109
|
public SetInfo getSetInfo(final String setSpec) {
|
110
|
if (isConfigurationLoaded()) return this.oaiConfiguration.getSetsMap().get(setSpec);
|
111
|
else throw new OaiPublisherRuntimeException("Configuration is not loaded");
|
112
|
}
|
113
|
|
114
|
@Override
|
115
|
public List<MDFInfo> getMetadataFormatInfo() {
|
116
|
if (isConfigurationLoaded()) return Lists.newArrayList(this.oaiConfiguration.getMdFormatsMap().values());
|
117
|
else throw new OaiPublisherRuntimeException("Configuration is not loaded");
|
118
|
}
|
119
|
|
120
|
@Override
|
121
|
public List<MDFInfo> getMetadataFormatInfo(final boolean onlyEnabled) {
|
122
|
if (isConfigurationLoaded()) {
|
123
|
if (!onlyEnabled) return this.getMetadataFormatInfo();
|
124
|
else return Lists.newArrayList(Iterables.filter(this.oaiConfiguration.getMdFormatsMap().values(), new Predicate<MDFInfo>() {
|
125
|
|
126
|
/**
|
127
|
* {@inheritDoc}
|
128
|
*
|
129
|
* @see com.google.common.base.Predicate#apply(java.lang.Object)
|
130
|
*/
|
131
|
@Override
|
132
|
public boolean apply(final MDFInfo mdfInfo) {
|
133
|
if (onlyEnabled) return mdfInfo.isEnabled();
|
134
|
else return true;
|
135
|
}
|
136
|
|
137
|
}));
|
138
|
|
139
|
} else throw new OaiPublisherRuntimeException("Configuration is not loaded");
|
140
|
}
|
141
|
|
142
|
@Override
|
143
|
public MDFInfo getMetadataFormatInfo(final String mdPrefix) {
|
144
|
if (isConfigurationLoaded()) return this.oaiConfiguration.getMdFormatsMap().get(mdPrefix);
|
145
|
else throw new OaiPublisherRuntimeException("Configuration is not loaded");
|
146
|
}
|
147
|
|
148
|
@Override
|
149
|
public List<PublisherField> getFields() {
|
150
|
if (isConfigurationLoaded()) return this.oaiConfiguration.getFields();
|
151
|
else throw new OaiPublisherRuntimeException("Configuration is not loaded");
|
152
|
}
|
153
|
|
154
|
@Override
|
155
|
public List<PublisherField> getFields(final String format, final String interpretation, final String layout) {
|
156
|
if (isConfigurationLoaded()) return Lists.newArrayList(this.oaiConfiguration.getFieldsFor(format, layout, interpretation));
|
157
|
else throw new OaiPublisherRuntimeException("Configuration is not loaded");
|
158
|
}
|
159
|
|
160
|
@Override
|
161
|
public List<String> getFieldNames() {
|
162
|
if (isConfigurationLoaded()) return this.oaiConfiguration.getFieldNames();
|
163
|
else throw new OaiPublisherRuntimeException("Configuration is not loaded");
|
164
|
}
|
165
|
|
166
|
public OAIConfiguration getOaiConfiguration() {
|
167
|
return oaiConfiguration;
|
168
|
}
|
169
|
|
170
|
public void setOaiConfiguration(final OAIConfiguration oaiConfiguration) {
|
171
|
this.oaiConfiguration = oaiConfiguration;
|
172
|
}
|
173
|
|
174
|
public OAIConfigurationParser getConfigurationParser() {
|
175
|
return configurationParser;
|
176
|
}
|
177
|
|
178
|
public void setConfigurationParser(final OAIConfigurationParser configurationParser) {
|
179
|
this.configurationParser = configurationParser;
|
180
|
}
|
181
|
|
182
|
}
|