1 |
41872
|
sandro.lab
|
package eu.dnetlib.clients.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.clients.index.client.IndexClientException;
|
11 |
|
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
12 |
|
|
import eu.dnetlib.rmi.enabling.*;
|
13 |
|
|
import eu.dnetlib.rmi.provision.IndexServiceException;
|
14 |
|
|
import org.apache.commons.lang3.StringUtils;
|
15 |
|
|
import org.apache.commons.logging.Log;
|
16 |
|
|
import org.apache.commons.logging.LogFactory;
|
17 |
|
|
|
18 |
|
|
public class ServiceTools {
|
19 |
|
|
|
20 |
|
|
private static final Log log = LogFactory.getLog(ServiceTools.class);
|
21 |
|
|
|
22 |
|
|
@Resource
|
23 |
|
|
private UniqueServiceLocator serviceLocator;
|
24 |
|
|
|
25 |
|
|
@Resource
|
26 |
|
|
private MetadataReferenceFactory mdFactory;
|
27 |
|
|
|
28 |
|
|
public List<MetadataReference> listMDRefs() throws IndexClientException {
|
29 |
|
|
return Lists.newArrayList(Iterables.transform(listMDRefsAsString(), s -> mdFactory.decodeMetadata(s)));
|
30 |
|
|
}
|
31 |
|
|
|
32 |
|
|
private List<String> quickSearchProfile(final String xquery) throws IndexClientException {
|
33 |
|
|
try {
|
34 |
|
|
return serviceLocator.getService(ISLookUpService.class).quickSearchProfile(xquery);
|
35 |
|
|
} catch (ISLookUpException e) {
|
36 |
|
|
throw new IndexClientException(e);
|
37 |
|
|
}
|
38 |
|
|
}
|
39 |
|
|
|
40 |
|
|
public MetadataReference getMetadataRef(final String dsId) throws IndexServiceException {
|
41 |
|
|
|
42 |
|
|
final String xquery = "for $x in //RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value='" + dsId + "']//CONFIGURATION " + "return concat("
|
43 |
|
|
+ "$x/METADATA_FORMAT/text(),'-'," + "$x/METADATA_FORMAT_LAYOUT/text(),'-'," + "$x/METADATA_FORMAT_INTERPRETATION/text())";
|
44 |
|
|
return mdFactory.decodeMetadata(getResourceProfileByQuery(xquery));
|
45 |
|
|
}
|
46 |
|
|
|
47 |
|
|
private String getResourceProfileByQuery(final String xquery) throws IndexServiceException {
|
48 |
|
|
try {
|
49 |
|
|
return serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(xquery);
|
50 |
|
|
} catch (ISLookUpDocumentNotFoundException e) {
|
51 |
|
|
throw new IndexServiceException(e);
|
52 |
|
|
} catch (ISLookUpException e) {
|
53 |
|
|
throw new IndexServiceException(e);
|
54 |
|
|
}
|
55 |
|
|
}
|
56 |
|
|
|
57 |
|
|
public String getIndexFields(final String dsId) throws IndexServiceException {
|
58 |
|
|
|
59 |
|
|
return getIndexFields(getMetadataRef(dsId));
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
public String getIndexFields(final MetadataReference mdRef) {
|
63 |
|
|
|
64 |
|
|
final String xquery = "for $x in collection('')/RESOURCE_PROFILE/BODY[CONFIGURATION/NAME='" + mdRef.getFormat()
|
65 |
|
|
+ "'] return $x/STATUS/LAYOUTS/LAYOUT[@name='" + mdRef.getLayout() + "']/FIELDS";
|
66 |
|
|
try {
|
67 |
|
|
return getResourceProfileByQuery(xquery);
|
68 |
|
|
} catch (IndexServiceException e) {
|
69 |
|
|
log.warn("couldn't find Metadata format profile matching specs: " + mdRef.toString());
|
70 |
|
|
return "";
|
71 |
|
|
}
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
public List<String> listDsIds() throws IndexClientException {
|
75 |
|
|
final String xquery = "//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='IndexDSResourceType']//RESOURCE_IDENTIFIER/@value/string()";
|
76 |
|
|
return quickSearchProfile(xquery);
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
private List<String> listMDRefsAsString() throws IndexClientException {
|
80 |
|
|
final String xquery = "for $x in //RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='MDFormatDSResourceType'] "
|
81 |
|
|
+ "let $format:= $x//CONFIGURATION/NAME/string() " + "for $y in $x//LAYOUTS/LAYOUT " + "let $layout:= $y//LAYOUT/@name/string() "
|
82 |
|
|
+ "let $interpretation:= $x//CONFIGURATION/INTERPRETATION/text() " + "return concat($format,'-',$layout,'-',$interpretation) ";
|
83 |
|
|
return quickSearchProfile(xquery);
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
public Map<String, String> getIndexProperties(final String backendId) throws IndexClientException {
|
87 |
|
|
|
88 |
|
|
String query = "for $x in /RESOURCE_PROFILE[.//RESOURCE_TYPE/@value=\"IndexServiceResourceType\"]//SERVICE_PROPERTIES/PROPERTY"
|
89 |
|
|
+ " return concat($x/@key/string(),\":::\", $x/@value/string())";
|
90 |
|
|
Map<String, String> indexProperties = new HashMap<String, String>();
|
91 |
|
|
try {
|
92 |
|
|
List<String> results = serviceLocator.getService(ISLookUpService.class).quickSearchProfile(query);
|
93 |
|
|
if (results != null) {
|
94 |
|
|
for (String s : results) {
|
95 |
|
|
String[] values = s.split(":::");
|
96 |
|
|
if (values != null && values.length == 2) {
|
97 |
|
|
String key = values[0];
|
98 |
|
|
String value = values[1];
|
99 |
|
|
if (StringUtils.startsWith(key, backendId)) {
|
100 |
|
|
indexProperties.put(StringUtils.substringAfter(key, backendId + ":"), value);
|
101 |
|
|
}
|
102 |
|
|
}
|
103 |
|
|
}
|
104 |
|
|
}
|
105 |
|
|
return indexProperties;
|
106 |
|
|
} catch (ISLookUpException e) {
|
107 |
|
|
throw new IndexClientException();
|
108 |
|
|
}
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
public String registerProfile(final String resourceProfile) throws IndexServiceException {
|
112 |
|
|
try {
|
113 |
|
|
return serviceLocator.getService(ISRegistryService.class).registerProfile(resourceProfile);
|
114 |
|
|
} catch (ISRegistryException e) {
|
115 |
|
|
throw new IndexServiceException(e);
|
116 |
|
|
}
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
public boolean incrementHandledDataStructures(final String backendId) throws IndexServiceException {
|
120 |
|
|
final String xquery = "let $x := //RESOURCE_PROFILE[HEADER/PROTOCOLS/PROTOCOL/@name='" + backendId + "'],"
|
121 |
|
|
+ "$tot := $x//STATUS/HANDLED_DATASTRUCTURE/number() + 1 " + "return update replace $x//STATUS/HANDLED_DATASTRUCTURE with "
|
122 |
|
|
+ "<HANDLED_DATASTRUCTURE>{$tot}</HANDLED_DATASTRUCTURE>";
|
123 |
|
|
|
124 |
|
|
log.info("performing increment of HANDLED_DATASTRUCTURE");
|
125 |
|
|
return executeXUpdate(xquery);
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
private boolean executeXUpdate(final String xquery) throws IndexServiceException {
|
129 |
|
|
try {
|
130 |
|
|
return serviceLocator.getService(ISRegistryService.class).executeXUpdate(xquery);
|
131 |
|
|
} catch (ISRegistryException e) {
|
132 |
|
|
throw new IndexServiceException(e);
|
133 |
|
|
}
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
public String getServiceAddress(final String backendId) {
|
137 |
|
|
final String xquery = "let $x := //RESOURCE_PROFILE[HEADER/PROTOCOLS/PROTOCOL/@name='" + backendId + "']"
|
138 |
|
|
+ "return $x//PROTOCOL[./@name='SOAP']/@address/string()";
|
139 |
|
|
try {
|
140 |
|
|
return getResourceProfileByQuery(xquery);
|
141 |
|
|
} catch (IndexServiceException e) {
|
142 |
|
|
log.warn("couldn't find service Address for index Service with protocol: " + backendId);
|
143 |
|
|
return "";
|
144 |
|
|
}
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
public boolean deleteIndexDS(final String dsId) throws IndexServiceException {
|
148 |
|
|
try {
|
149 |
|
|
return serviceLocator.getService(ISRegistryService.class).deleteProfile(dsId);
|
150 |
|
|
} catch (ISRegistryDocumentNotFoundException e) {
|
151 |
|
|
throw new IndexServiceException(e);
|
152 |
|
|
} catch (ISRegistryException e) {
|
153 |
|
|
throw new IndexServiceException(e);
|
154 |
|
|
}
|
155 |
|
|
}
|
156 |
|
|
|
157 |
|
|
public List<String> getBackendIds(final MetadataReference mdRef) throws IndexServiceException {
|
158 |
|
|
String query =
|
159 |
|
|
"distinct-values(//RESOURCE_PROFILE[.//METADATA_FORMAT='%s' and .//METADATA_FORMAT_LAYOUT='%s' and .//METADATA_FORMAT_INTERPRETATION='%s']//BACKEND/@ID/string())";
|
160 |
|
|
try {
|
161 |
|
|
String instanceQuery = String.format(query, mdRef.getFormat(), mdRef.getLayout(), mdRef.getInterpretation());
|
162 |
|
|
log.debug("Executing query to IS: " + instanceQuery);
|
163 |
|
|
return serviceLocator.getService(ISLookUpService.class).quickSearchProfile(instanceQuery);
|
164 |
|
|
} catch (ISLookUpException e) {
|
165 |
|
|
throw new IndexServiceException(e);
|
166 |
|
|
}
|
167 |
|
|
}
|
168 |
|
|
}
|