Project

General

Profile

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

    
3
import java.util.HashMap;
4
import java.util.List;
5
import java.util.Map;
6
import javax.annotation.Resource;
7

    
8
import com.google.common.collect.Iterables;
9
import com.google.common.collect.Lists;
10
import eu.dnetlib.data.provision.index.rmi.IndexServiceException;
11
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException;
12
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
13
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
14
import eu.dnetlib.enabling.is.registry.ISRegistryDocumentNotFoundException;
15
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryException;
16
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
17
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
18
import eu.dnetlib.functionality.index.client.IndexClientException;
19
import org.apache.commons.lang3.StringUtils;
20
import org.apache.commons.logging.Log;
21
import org.apache.commons.logging.LogFactory;
22

    
23
public class ServiceTools {
24

    
25
	private static final Log log = LogFactory.getLog(ServiceTools.class);
26

    
27
	@Resource
28
	private UniqueServiceLocator serviceLocator;
29

    
30
	@Resource
31
	private MetadataReferenceFactory mdFactory;
32

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

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

    
45
	public MetadataReference getMetadataRef(final String dsId) throws IndexServiceException {
46

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

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

    
62
	public String getIndexFields(final String dsId) throws IndexServiceException {
63

    
64
		return getIndexFields(getMetadataRef(dsId));
65
	}
66

    
67
	public String getIndexFields(final MetadataReference mdRef) {
68

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

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

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

    
94
	public Map<String, String> getIndexProperties(final String backendId) throws IndexClientException {
95

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

    
119
	public String registerProfile(final String resourceProfile) throws IndexServiceException {
120
		try {
121
			return serviceLocator.getService(ISRegistryService.class).registerProfile(resourceProfile);
122
		} catch (ISRegistryException e) {
123
			throw new IndexServiceException(e);
124
		}
125
	}
126

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

    
132
		log.info("performing increment of HANDLED_DATASTRUCTURE");
133
		return executeXUpdate(xquery);
134
	}
135

    
136
	private boolean executeXUpdate(final String xquery) throws IndexServiceException {
137
		try {
138
			return serviceLocator.getService(ISRegistryService.class).executeXUpdate(xquery);
139
		} catch (ISRegistryException e) {
140
			throw new IndexServiceException(e);
141
		}
142
	}
143

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

    
155
	public boolean deleteIndexDS(final String dsId) throws IndexServiceException {
156
		try {
157
			return serviceLocator.getService(ISRegistryService.class).deleteProfile(dsId);
158
		} catch (ISRegistryDocumentNotFoundException e) {
159
			throw new IndexServiceException(e);
160
		} catch (ISRegistryException e) {
161
			throw new IndexServiceException(e);
162
		}
163
	}
164

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