Project

General

Profile

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
import org.springframework.beans.factory.annotation.Autowired;
18

    
19
public class OAIConfigurationWriter {
20

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

    
23
	@Autowired
24
	private UniqueServiceLocator serviceLocator;
25

    
26
	@Resource(name = "oaiConfigurationExistReader")
27
	private OAIConfigurationReader configuration;
28

    
29
	private boolean execute(final String xUpdate) throws ISRegistryException {
30
		log.debug("Running XUpdate:\n" + xUpdate);
31
		boolean done = this.serviceLocator.getService(ISRegistryService.class).executeXUpdate(xUpdate);
32
		if (!done) {
33
			log.fatal("Could not perform the following Xupdate:\n" + xUpdate);
34
		}
35
		return done;
36
	}
37

    
38
	public boolean updateMetadataFormat(final String mdPrefix, final MDFInfo newInfo) throws ISRegistryException {
39

    
40
		final String xUpdate = "update replace //RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='OAIPublisherConfigurationDSResourceType']"
41
				+ "//CONFIGURATION//METADATAFORMAT[./@metadataPrefix/string()='" + mdPrefix + "'] with <METADATAFORMAT exportable=\"" + newInfo.isEnabled()
42
				+ "\" metadataPrefix=\"" + newInfo.getPrefix() + "\">" + "<NAMESPACE>" + newInfo.getNamespace() + "</NAMESPACE><SCHEMA>" + newInfo.getSchema()
43
				+ "</SCHEMA><SOURCE_METADATA_FORMAT interpretation=\"" + newInfo.getSourceInterpretation() + "\" layout=\""
44
				+ newInfo.getSourceLayout() + "\" name=\"" + newInfo.getSourceFormat() + "\"/><TRANSFORMATION_RULE>"
45
				+ newInfo.getTransformationRuleID() + "</TRANSFORMATION_RULE><BASE_QUERY>" + StringEscapeUtils.escapeXml11(newInfo.getBaseQuery())
46
				+ "</BASE_QUERY></METADATAFORMAT>";
47
		return this.execute(xUpdate);
48
	}
49

    
50
	public boolean addMetadataFormat(final MDFInfo newInfo) throws ISRegistryException {
51

    
52
		String action = "update insert ";
53
		String newNode = "<METADATAFORMAT metadataPrefix='" + newInfo.getPrefix() + "' exportable='" + newInfo.isEnabled() + "' ><NAMESPACE>"
54
				+ newInfo.getNamespace() + "</NAMESPACE><SCHEMA>" + newInfo.getSchema() + "</SCHEMA>" + "<SOURCE_METADATA_FORMAT interpretation='"
55
				+ newInfo.getSourceInterpretation() + "' layout='" + newInfo.getSourceLayout() + "' name='" + newInfo.getSourceFormat()
56
				+ "' /><TRANSFORMATION_RULE>" + newInfo.getTransformationRuleID() + "</TRANSFORMATION_RULE><BASE_QUERY>"
57
				+ StringEscapeUtils.escapeXml11(newInfo.getBaseQuery()) + "</BASE_QUERY></METADATAFORMAT>";
58
		String targetNode = "//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='OAIPublisherConfigurationDSResourceType']//CONFIGURATION/METADATAFORMATS";
59

    
60
		String xUpdate = action + newNode + " into " + targetNode;
61
		return this.execute(xUpdate);
62
	}
63

    
64
	public boolean deleteMetadataFormat(final String mdPrefix) throws ISRegistryException {
65
		String xUpdate =
66
				"update delete //RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='OAIPublisherConfigurationDSResourceType']//CONFIGURATION//METADATAFORMATS/METADATAFORMAT[./@metadataPrefix/string()='"
67
						+ mdPrefix + "']";
68
		return this.execute(xUpdate);
69
	}
70

    
71
	public boolean updateOAISet(final String setSpec, final SetInfo newInfo) throws ISRegistryException {
72
		final String xUpdate = "update replace //RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='OAIPublisherConfigurationDSResourceType']"
73
				+ "//CONFIGURATION//OAISET[spec/text()='" + setSpec + "'] with " + "<OAISET enabled=\"" + newInfo.isEnabled() + "\">" + "<spec>"
74
				+ newInfo.getSetSpec() + "</spec>" + "<name>" + newInfo.getSetName() + "</name>" + "<description>" + newInfo.getSetDescription()
75
				+ "</description>" + "<query>" + StringEscapeUtils.escapeXml11(newInfo.getQuery()) + "</query></OAISET>";
76

    
77
		return this.execute(xUpdate);
78
	}
79

    
80
	public boolean addOAISet(final SetInfo newInfo) throws ISRegistryException {
81
		String action = "update insert ";
82
		String newNode = "<OAISET enabled=\"" + newInfo.isEnabled() + "\">" + "<spec>" + newInfo.getSetSpec() + "</spec>" + "<name>" + newInfo.getSetName()
83
				+ "</name>" + "<description>" + newInfo.getSetDescription() + "</description>" + "<query>" + StringEscapeUtils.escapeXml11(newInfo.getQuery())
84
				+ "</query></OAISET>";
85
		String targetNode = "//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='OAIPublisherConfigurationDSResourceType']//CONFIGURATION/OAISETS";
86

    
87
		String xUpdate = action + newNode + " into " + targetNode;
88
		return this.execute(xUpdate);
89
	}
90

    
91
	public boolean deleteOAISet(final String setSpec) throws ISRegistryException {
92
		String xUpdate =
93
				"update delete //RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='OAIPublisherConfigurationDSResourceType']//CONFIGURATION//OAISET[spec/text()='"
94
						+ setSpec + "']";
95
		return this.execute(xUpdate);
96
	}
97

    
98
	public boolean updateIndices(final String format, final String layout, final String interpretation, final List<PublisherField> indexes)
99
			throws ISRegistryException {
100
		boolean dropped = this.dropIndexesFor(format, layout, interpretation);
101
		String xUpdateAction = "update insert ";
102
		String nodeTemplate = " <SOURCE name='" + format + "' layout='" + layout + "' interpretation='" + interpretation + "' ";
103
		String targetTemplate = "//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='OAIPublisherConfigurationDSResourceType']//CONFIGURATION/INDICES/INDEX";
104
		if (!dropped) { return false; }
105
		for (PublisherField field : indexes) {
106
			String targetNode = targetTemplate + "[@name='" + field.getFieldName() + "']";
107
			Collection<String> paths = field.getSources().get(format + "-" + layout + "-" + interpretation);
108
			for (String p : paths) {
109
				// insert one SOURCE node at a time, otherwise Exist complaints.
110
				String node = nodeTemplate + " path=\"" + p + "\" />";
111
				String xUpdate = xUpdateAction + node + " into " + targetNode;
112
				boolean done = this.execute(xUpdate);
113
				if (!done) { return false; }
114
			}
115
			// finally let's update the repeatability
116
			String updateRepeatability = "update value " + targetNode + "/@repeatable with '" + field.isRepeatable() + "'";
117
			boolean done = this.execute(updateRepeatability);
118
			if (!done) { return false; }
119
		}
120
		return true;
121
	}
122

    
123
	private boolean dropIndexesFor(final String format, final String layout, final String interpretation) throws ISRegistryException {
124
		String xUpdate =
125
				"update delete //RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='OAIPublisherConfigurationDSResourceType']//CONFIGURATION//INDEX/SOURCE[./@interpretation/string()='"
126
						+ interpretation + "' and ./@name/string()='" + format + "' and ./@layout/string()='" + layout + "'] ";
127
		return this.execute(xUpdate);
128
	}
129

    
130
	public boolean addNewIndex(final String format,
131
			final String layout,
132
			final String interpretation,
133
			final String indexName,
134
			final boolean isRepeatable,
135
			final String[] paths) throws ISLookUpException, ISRegistryException {
136
		String action = "update insert ";
137
		String nodeTemplate = " <SOURCE name='" + format + "' layout='" + layout + "' interpretation='" + interpretation + "' ";
138
		String targetTemplate = "//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='OAIPublisherConfigurationDSResourceType']//CONFIGURATION/INDICES";
139
		if (this.configuration.getFieldNames().contains(indexName)) {
140
			String targetNode = targetTemplate + "/INDEX[@name/string()='" + indexName + "']";
141
			for (String p : paths) {
142
				// insert one SOURCE node at a time, otherwise Exist complaints.
143
				String node = nodeTemplate + " path=\"" + p + "\" />";
144
				String xUpdate = action + node + " into " + targetNode;
145
				boolean done = this.execute(xUpdate);
146
				if (!done) { return false; }
147
			}
148
		} else {
149
			String targetNode = targetTemplate;
150
			String node = "<INDEX name='" + indexName + "' repeatable='" + isRepeatable + "'>";
151
			for (String p : paths) {
152
				node += nodeTemplate + " path=\"" + p + "\" />";
153
			}
154
			node += "</INDEX>";
155
			String xUpdate = action + node + " into " + targetNode;
156
			boolean done = this.execute(xUpdate);
157
			if (!done) { return false; }
158
		}
159
		return true;
160
	}
161

    
162
	public OAIConfigurationReader getConfiguration() {
163
		return configuration;
164
	}
165

    
166
	public void setConfiguration(final OAIConfigurationReader configuration) {
167
		this.configuration = configuration;
168
	}
169

    
170
}
(6-6/6)