Project

General

Profile

1
package eu.dnetlib.clients.index.utils;
2

    
3
import java.util.HashMap;
4
import java.util.List;
5
import java.util.Map;
6

    
7
import com.google.common.collect.Iterables;
8
import com.google.common.collect.Lists;
9
import eu.dnetlib.clients.index.client.IndexClientException;
10
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
11
import eu.dnetlib.rmi.enabling.*;
12
import eu.dnetlib.rmi.provision.IndexServiceException;
13
import eu.dnetlib.utils.MetadataReference;
14
import eu.dnetlib.utils.MetadataReferenceFactory;
15
import org.apache.commons.lang3.StringUtils;
16
import org.apache.commons.logging.Log;
17
import org.apache.commons.logging.LogFactory;
18
import org.springframework.beans.factory.annotation.Autowired;
19

    
20
public class ServiceTools {
21

    
22
	private static final Log log = LogFactory.getLog(ServiceTools.class);
23

    
24
	@Autowired
25
	private UniqueServiceLocator serviceLocator;
26

    
27
	@Autowired
28
	private MetadataReferenceFactory mdFactory;
29

    
30
	public List<MetadataReference> listMDRefs() throws IndexClientException {
31
		return Lists.newArrayList(Iterables.transform(listMDRefsAsString(), s -> mdFactory.decodeMetadata(s)));
32
	}
33

    
34
	private List<String> quickSearchProfile(final String xquery) throws IndexClientException {
35
		try {
36
			return serviceLocator.getService(ISLookUpService.class).quickSearchProfile(xquery);
37
		} catch (ISLookUpException e) {
38
			throw new IndexClientException(e);
39
		}
40
	}
41

    
42
	public MetadataReference getMetadataRef(final String dsId) throws IndexServiceException {
43

    
44
		final String xquery = "for $x in //RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value='" + dsId + "']//CONFIGURATION " + "return concat("
45
				+ "$x/METADATA_FORMAT/text(),'@'," + "$x/METADATA_FORMAT_LAYOUT/text(),'@'," + "$x/METADATA_FORMAT_INTERPRETATION/text())";
46
		return mdFactory.decodeMetadata(getResourceProfileByQuery(xquery));
47
	}
48

    
49
	private String getResourceProfileByQuery(final String xquery) throws IndexServiceException {
50
		try {
51
			return serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(xquery);
52
		} catch (ISLookUpDocumentNotFoundException e) {
53
			throw new IndexServiceException(e);
54
		} catch (ISLookUpException e) {
55
			throw new IndexServiceException(e);
56
		}
57
	}
58

    
59
	public String getIndexFields(final String dsId) throws IndexServiceException {
60

    
61
		return getIndexFields(getMetadataRef(dsId));
62
	}
63

    
64
	public String getIndexFields(final MetadataReference mdRef) {
65

    
66
		final String xquery = "for $x in collection('')/RESOURCE_PROFILE/BODY[CONFIGURATION/NAME='" + mdRef.getFormat()
67
				+ "'] return $x/STATUS/LAYOUTS/LAYOUT[@name='" + mdRef.getLayout() + "']/FIELDS";
68
		try {
69
			return getResourceProfileByQuery(xquery);
70
		} catch (IndexServiceException e) {
71
			log.warn("couldn't find Metadata format profile matching specs: " + mdRef.toString());
72
			return "";
73
		}
74
	}
75

    
76
	public List<String> listDsIds() throws IndexClientException {
77
		final String xquery = "//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='IndexDSResourceType']//RESOURCE_IDENTIFIER/@value/string()";
78
		return quickSearchProfile(xquery);
79
	}
80

    
81
	private List<String> listMDRefsAsString() throws IndexClientException {
82
		final String xquery = "for $x in //RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='MDFormatDSResourceType'] "
83
				+ "let $format:= $x//CONFIGURATION/NAME/string() " + "for $y in $x//LAYOUTS/LAYOUT " + "let $layout:= $y/@name/string() "
84
				+ "let $interpretation:= $x//CONFIGURATION/INTERPRETATION/text() " + "return concat($format,'@',$layout,'@',$interpretation) ";
85
		return quickSearchProfile(xquery);
86
	}
87

    
88
	public Map<String, String> getIndexProperties(final String backendId) throws IndexClientException {
89

    
90
		String query = "for $x in /RESOURCE_PROFILE[.//RESOURCE_TYPE/@value=\"IndexServiceResourceType\"]//SERVICE_PROPERTIES/PROPERTY"
91
				+ " return concat($x/@key/string(),\":::\", $x/@value/string())";
92
		Map<String, String> indexProperties = new HashMap<String, String>();
93
		try {
94
			List<String> results = serviceLocator.getService(ISLookUpService.class).quickSearchProfile(query);
95
			if (results != null) {
96
				for (String s : results) {
97
					String[] values = s.split(":::");
98
					if (values != null && values.length == 2) {
99
						String key = values[0];
100
						String value = values[1];
101
						if (StringUtils.startsWith(key, backendId)) {
102
							indexProperties.put(StringUtils.substringAfter(key, backendId + ":"), value);
103
						}
104
					}
105
				}
106
			}
107
			return indexProperties;
108
		} catch (ISLookUpException e) {
109
			throw new IndexClientException();
110
		}
111
	}
112

    
113
	public String registerProfile(final String resourceProfile) throws IndexServiceException {
114
		try {
115
			return serviceLocator.getService(ISRegistryService.class).registerProfile(resourceProfile);
116
		} catch (ISRegistryException e) {
117
			throw new IndexServiceException(e);
118
		}
119
	}
120

    
121
	public boolean incrementHandledDataStructures(final String backendId) throws IndexServiceException {
122
		final String xquery = "let $x := //RESOURCE_PROFILE[HEADER/PROTOCOLS/PROTOCOL/@name='" + backendId + "'],"
123
				+ "$tot := $x//STATUS/HANDLED_DATASTRUCTURE/number() + 1 " + "return update replace $x//STATUS/HANDLED_DATASTRUCTURE with "
124
				+ "<HANDLED_DATASTRUCTURE>{$tot}</HANDLED_DATASTRUCTURE>";
125

    
126
		log.info("performing increment of HANDLED_DATASTRUCTURE");
127
		return executeXUpdate(xquery);
128
	}
129

    
130
	private boolean executeXUpdate(final String xquery) throws IndexServiceException {
131
		try {
132
			return serviceLocator.getService(ISRegistryService.class).executeXUpdate(xquery);
133
		} catch (ISRegistryException e) {
134
			throw new IndexServiceException(e);
135
		}
136
	}
137

    
138
	public String getServiceAddress(final String backendId) {
139
		final String xquery = "let $x := //RESOURCE_PROFILE[HEADER/PROTOCOLS/PROTOCOL/@name='" + backendId + "']"
140
				+ "return $x//PROTOCOL[./@name='SOAP']/@address/string()";
141
		try {
142
			return getResourceProfileByQuery(xquery);
143
		} catch (IndexServiceException e) {
144
			log.warn("couldn't find service Address for index Service with protocol: " + backendId);
145
			return "";
146
		}
147
	}
148

    
149
	public boolean deleteIndexDS(final String dsId) throws IndexServiceException {
150
		try {
151
			return serviceLocator.getService(ISRegistryService.class).deleteProfile(dsId);
152
		} catch (ISRegistryDocumentNotFoundException e) {
153
			throw new IndexServiceException(e);
154
		} catch (ISRegistryException e) {
155
			throw new IndexServiceException(e);
156
		}
157
	}
158

    
159
	public List<String> getBackendIds(final MetadataReference mdRef) throws IndexServiceException {
160
		String query =
161
				"distinct-values(//RESOURCE_PROFILE[.//METADATA_FORMAT='%s' and .//METADATA_FORMAT_LAYOUT='%s' and .//METADATA_FORMAT_INTERPRETATION='%s']//BACKEND/@ID/string())";
162
		try {
163
			String instanceQuery = String.format(query, mdRef.getFormat(), mdRef.getLayout(), mdRef.getInterpretation());
164
			log.debug("Executing query to IS: " + instanceQuery);
165
			return serviceLocator.getService(ISLookUpService.class).quickSearchProfile(instanceQuery);
166
		} catch (ISLookUpException e) {
167
			throw new IndexServiceException(e);
168
		}
169
	}
170
}
(3-3/3)