1
|
package eu.dnetlib.oai.conf;
|
2
|
|
3
|
import java.util.Collection;
|
4
|
import java.util.List;
|
5
|
import javax.annotation.Resource;
|
6
|
|
7
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
8
|
import eu.dnetlib.oai.PublisherField;
|
9
|
import eu.dnetlib.oai.info.SetInfo;
|
10
|
import eu.dnetlib.rmi.enabling.ISLookUpException;
|
11
|
import eu.dnetlib.rmi.enabling.ISRegistryException;
|
12
|
import eu.dnetlib.rmi.enabling.ISRegistryService;
|
13
|
import eu.dnetlib.rmi.provision.MDFInfo;
|
14
|
import org.apache.commons.lang3.StringEscapeUtils;
|
15
|
import org.apache.commons.logging.Log;
|
16
|
import org.apache.commons.logging.LogFactory;
|
17
|
|
18
|
public class OAIConfigurationWriter {
|
19
|
|
20
|
private static final Log log = LogFactory.getLog(OAIConfigurationWriter.class); // NOPMD by marko on 11/24/08 5:02 PM
|
21
|
|
22
|
@Resource
|
23
|
private UniqueServiceLocator serviceLocator;
|
24
|
|
25
|
@Resource(name = "oaiConfigurationExistReader")
|
26
|
private OAIConfigurationReader configuration;
|
27
|
|
28
|
private boolean execute(final String xUpdate) throws ISRegistryException {
|
29
|
log.debug("Running XUpdate:\n" + xUpdate);
|
30
|
boolean done = this.serviceLocator.getService(ISRegistryService.class).executeXUpdate(xUpdate);
|
31
|
if (!done) {
|
32
|
log.fatal("Could not perform the following Xupdate:\n" + xUpdate);
|
33
|
}
|
34
|
return done;
|
35
|
}
|
36
|
|
37
|
public boolean updateMetadataFormat(final String mdPrefix, final MDFInfo newInfo) throws ISRegistryException {
|
38
|
|
39
|
final String xUpdate = "update replace //RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='OAIPublisherConfigurationDSResourceType']"
|
40
|
+ "//CONFIGURATION//METADATAFORMAT[./@metadataPrefix/string()='" + mdPrefix + "'] with <METADATAFORMAT exportable=\"" + newInfo.isEnabled()
|
41
|
+ "\" metadataPrefix=\"" + newInfo.getPrefix() + "\">" + "<NAMESPACE>" + newInfo.getNamespace() + "</NAMESPACE><SCHEMA>" + newInfo.getSchema()
|
42
|
+ "</SCHEMA><SOURCE_METADATA_FORMAT interpretation=\"" + newInfo.getSourceFormatInterpretation() + "\" layout=\""
|
43
|
+ newInfo.getSourceFormatLayout() + "\" name=\"" + newInfo.getSourceFormatName() + "\"/><TRANSFORMATION_RULE>"
|
44
|
+ newInfo.getTransformationRuleID() + "</TRANSFORMATION_RULE><BASE_QUERY>" + StringEscapeUtils.escapeXml11(newInfo.getBaseQuery())
|
45
|
+ "</BASE_QUERY></METADATAFORMAT>";
|
46
|
return this.execute(xUpdate);
|
47
|
}
|
48
|
|
49
|
public boolean addMetadataFormat(final MDFInfo newInfo) throws ISRegistryException {
|
50
|
|
51
|
String action = "update insert ";
|
52
|
String newNode = "<METADATAFORMAT metadataPrefix='" + newInfo.getPrefix() + "' exportable='" + newInfo.isEnabled() + "' ><NAMESPACE>"
|
53
|
+ newInfo.getNamespace() + "</NAMESPACE><SCHEMA>" + newInfo.getSchema() + "</SCHEMA>" + "<SOURCE_METADATA_FORMAT interpretation='"
|
54
|
+ newInfo.getSourceFormatInterpretation() + "' layout='" + newInfo.getSourceFormatLayout() + "' name='" + newInfo.getSourceFormatName()
|
55
|
+ "' /><TRANSFORMATION_RULE>" + newInfo.getTransformationRuleID() + "</TRANSFORMATION_RULE><BASE_QUERY>"
|
56
|
+ StringEscapeUtils.escapeXml11(newInfo.getBaseQuery()) + "</BASE_QUERY></METADATAFORMAT>";
|
57
|
String targetNode = "//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='OAIPublisherConfigurationDSResourceType']//CONFIGURATION/METADATAFORMATS";
|
58
|
|
59
|
String xUpdate = action + newNode + " into " + targetNode;
|
60
|
return this.execute(xUpdate);
|
61
|
}
|
62
|
|
63
|
public boolean deleteMetadataFormat(final String mdPrefix) throws ISRegistryException {
|
64
|
String xUpdate =
|
65
|
"update delete //RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='OAIPublisherConfigurationDSResourceType']//CONFIGURATION//METADATAFORMATS/METADATAFORMAT[./@metadataPrefix/string()='"
|
66
|
+ mdPrefix + "']";
|
67
|
return this.execute(xUpdate);
|
68
|
}
|
69
|
|
70
|
public boolean updateOAISet(final String setSpec, final SetInfo newInfo) throws ISRegistryException {
|
71
|
final String xUpdate = "update replace //RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='OAIPublisherConfigurationDSResourceType']"
|
72
|
+ "//CONFIGURATION//OAISET[spec/text()='" + setSpec + "'] with " + "<OAISET enabled=\"" + newInfo.isEnabled() + "\">" + "<spec>"
|
73
|
+ newInfo.getSetSpec() + "</spec>" + "<name>" + newInfo.getSetName() + "</name>" + "<description>" + newInfo.getSetDescription()
|
74
|
+ "</description>" + "<query>" + StringEscapeUtils.escapeXml11(newInfo.getQuery()) + "</query></OAISET>";
|
75
|
|
76
|
return this.execute(xUpdate);
|
77
|
}
|
78
|
|
79
|
public boolean addOAISet(final SetInfo newInfo) throws ISRegistryException {
|
80
|
String action = "update insert ";
|
81
|
String newNode = "<OAISET enabled=\"" + newInfo.isEnabled() + "\">" + "<spec>" + newInfo.getSetSpec() + "</spec>" + "<name>" + newInfo.getSetName()
|
82
|
+ "</name>" + "<description>" + newInfo.getSetDescription() + "</description>" + "<query>" + StringEscapeUtils.escapeXml11(newInfo.getQuery())
|
83
|
+ "</query></OAISET>";
|
84
|
String targetNode = "//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='OAIPublisherConfigurationDSResourceType']//CONFIGURATION/OAISETS";
|
85
|
|
86
|
String xUpdate = action + newNode + " into " + targetNode;
|
87
|
return this.execute(xUpdate);
|
88
|
}
|
89
|
|
90
|
public boolean deleteOAISet(final String setSpec) throws ISRegistryException {
|
91
|
String xUpdate =
|
92
|
"update delete //RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='OAIPublisherConfigurationDSResourceType']//CONFIGURATION//OAISET[spec/text()='"
|
93
|
+ setSpec + "']";
|
94
|
return this.execute(xUpdate);
|
95
|
}
|
96
|
|
97
|
public boolean updateIndices(final String format, final String layout, final String interpretation, final List<PublisherField> indexes)
|
98
|
throws ISRegistryException {
|
99
|
boolean dropped = this.dropIndexesFor(format, layout, interpretation);
|
100
|
String xUpdateAction = "update insert ";
|
101
|
String nodeTemplate = " <SOURCE name='" + format + "' layout='" + layout + "' interpretation='" + interpretation + "' ";
|
102
|
String targetTemplate = "//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='OAIPublisherConfigurationDSResourceType']//CONFIGURATION/INDICES/INDEX";
|
103
|
if (!dropped) { return false; }
|
104
|
for (PublisherField field : indexes) {
|
105
|
String targetNode = targetTemplate + "[@name='" + field.getFieldName() + "']";
|
106
|
Collection<String> paths = field.getSources().get(format + "-" + layout + "-" + interpretation);
|
107
|
for (String p : paths) {
|
108
|
// insert one SOURCE node at a time, otherwise Exist complaints.
|
109
|
String node = nodeTemplate + " path=\"" + p + "\" />";
|
110
|
String xUpdate = xUpdateAction + node + " into " + targetNode;
|
111
|
boolean done = this.execute(xUpdate);
|
112
|
if (!done) { return false; }
|
113
|
}
|
114
|
// finally let's update the repeatability
|
115
|
String updateRepeatability = "update value " + targetNode + "/@repeatable with '" + field.isRepeatable() + "'";
|
116
|
boolean done = this.execute(updateRepeatability);
|
117
|
if (!done) { return false; }
|
118
|
}
|
119
|
return true;
|
120
|
}
|
121
|
|
122
|
private boolean dropIndexesFor(final String format, final String layout, final String interpretation) throws ISRegistryException {
|
123
|
String xUpdate =
|
124
|
"update delete //RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='OAIPublisherConfigurationDSResourceType']//CONFIGURATION//INDEX/SOURCE[./@interpretation/string()='"
|
125
|
+ interpretation + "' and ./@name/string()='" + format + "' and ./@layout/string()='" + layout + "'] ";
|
126
|
return this.execute(xUpdate);
|
127
|
}
|
128
|
|
129
|
public boolean addNewIndex(final String format,
|
130
|
final String layout,
|
131
|
final String interpretation,
|
132
|
final String indexName,
|
133
|
final boolean isRepeatable,
|
134
|
final String[] paths) throws ISLookUpException, ISRegistryException {
|
135
|
String action = "update insert ";
|
136
|
String nodeTemplate = " <SOURCE name='" + format + "' layout='" + layout + "' interpretation='" + interpretation + "' ";
|
137
|
String targetTemplate = "//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='OAIPublisherConfigurationDSResourceType']//CONFIGURATION/INDICES";
|
138
|
if (this.configuration.getFieldNames().contains(indexName)) {
|
139
|
String targetNode = targetTemplate + "/INDEX[@name/string()='" + indexName + "']";
|
140
|
for (String p : paths) {
|
141
|
// insert one SOURCE node at a time, otherwise Exist complaints.
|
142
|
String node = nodeTemplate + " path=\"" + p + "\" />";
|
143
|
String xUpdate = action + node + " into " + targetNode;
|
144
|
boolean done = this.execute(xUpdate);
|
145
|
if (!done) { return false; }
|
146
|
}
|
147
|
} else {
|
148
|
String targetNode = targetTemplate;
|
149
|
String node = "<INDEX name='" + indexName + "' repeatable='" + isRepeatable + "'>";
|
150
|
for (String p : paths) {
|
151
|
node += nodeTemplate + " path=\"" + p + "\" />";
|
152
|
}
|
153
|
node += "</INDEX>";
|
154
|
String xUpdate = action + node + " into " + targetNode;
|
155
|
boolean done = this.execute(xUpdate);
|
156
|
if (!done) { return false; }
|
157
|
}
|
158
|
return true;
|
159
|
}
|
160
|
|
161
|
public OAIConfigurationReader getConfiguration() {
|
162
|
return configuration;
|
163
|
}
|
164
|
|
165
|
public void setConfiguration(final OAIConfigurationReader configuration) {
|
166
|
this.configuration = configuration;
|
167
|
}
|
168
|
|
169
|
}
|