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

    
7
import javax.annotation.Resource;
8

    
9
import org.apache.commons.lang.StringUtils;
10
import org.apache.commons.logging.Log;
11
import org.apache.commons.logging.LogFactory;
12

    
13
import com.google.common.base.Function;
14
import com.google.common.collect.Iterables;
15
import com.google.common.collect.Lists;
16

    
17
import eu.dnetlib.data.provision.index.rmi.IndexServiceException;
18
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException;
19
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
20
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
21
import eu.dnetlib.enabling.is.registry.ISRegistryDocumentNotFoundException;
22
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryException;
23
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
24
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
25
import eu.dnetlib.functionality.index.client.IndexClientException;
26

    
27
public class ServiceTools {
28

    
29
	private static final Log log = LogFactory.getLog(ServiceTools.class);
30

    
31
	@Resource
32
	private UniqueServiceLocator serviceLocator;
33

    
34
	@Resource
35
	private MetadataReferenceFactory mdFactory;
36

    
37
	public List<MetadataReference> listMDRefs() throws IndexClientException {
38
		return Lists.newArrayList(Iterables.transform(listMDRefsAsString(), new Function<String, MetadataReference>() {
39

    
40
			@Override
41
			public MetadataReference apply(final String s) {
42
				return mdFactory.decodeMetadata(s);
43
			}
44
		}));
45
	}
46

    
47
	private List<String> quickSearchProfile(final String xquery) throws IndexClientException {
48
		try {
49
			return serviceLocator.getService(ISLookUpService.class).quickSearchProfile(xquery);
50
		} catch (ISLookUpException e) {
51
			throw new IndexClientException(e);
52
		}
53
	}
54

    
55
	public MetadataReference getMetadataRef(final String dsId) throws IndexServiceException {
56

    
57
		final String xquery = "for $x in //RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value='" + dsId + "']//CONFIGURATION " + "return concat("
58
				+ "$x/METADATA_FORMAT/text(),'-'," + "$x/METADATA_FORMAT_LAYOUT/text(),'-'," + "$x/METADATA_FORMAT_INTERPRETATION/text())";
59
		return mdFactory.decodeMetadata(getResourceProfileByQuery(xquery));
60
	}
61

    
62
	private String getResourceProfileByQuery(final String xquery) throws IndexServiceException {
63
		try {
64
			return serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(xquery);
65
		} catch (ISLookUpDocumentNotFoundException e) {
66
			throw new IndexServiceException(e);
67
		} catch (ISLookUpException e) {
68
			throw new IndexServiceException(e);
69
		}
70
	}
71

    
72
	public String getIndexFields(final String dsId) throws IndexServiceException {
73

    
74
		return getIndexFields(getMetadataRef(dsId));
75
	}
76

    
77
	public String getIndexFields(final MetadataReference mdRef) {
78

    
79
		final String xquery = "for $x in collection('')/RESOURCE_PROFILE/BODY[CONFIGURATION/NAME='" + mdRef.getFormat()
80
				+ "'] return $x/STATUS/LAYOUTS/LAYOUT[@name='" + mdRef.getLayout() + "']/FIELDS";
81
		try {
82
			return getResourceProfileByQuery(xquery);
83
		} catch (IndexServiceException e) {
84
			log.warn("couldn't find Metadata format profile matching specs: " + mdRef.toString());
85
			throw new RuntimeException(e);
86
		}
87
	}
88

    
89
	public List<String> listDsIds() throws IndexClientException {
90
		final String xquery = "//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='IndexDSResourceType']//RESOURCE_IDENTIFIER/@value/string()";
91
		return quickSearchProfile(xquery);
92
	}
93

    
94
	private List<String> listMDRefsAsString() throws IndexClientException {
95
		final String xquery = "for $x in //RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='MDFormatDSResourceType'] \n"
96
				+ "let $format:= $x//CONFIGURATION/NAME/string() \n"
97
				+ "let $interpretation:= $x//CONFIGURATION/INTERPRETATION/text() \n"
98
				+ "for $y in $x//LAYOUTS/LAYOUT \n"
99
				+ "  let $layout:= $y/@name/string() \n"
100
				+ "  return concat($format,'-',$layout,'-',$interpretation) ";
101
		return quickSearchProfile(xquery);
102
	}
103

    
104
	public Map<String, String> getIndexProperties(final String backendId) throws IndexClientException {
105

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

    
129
	public String registerProfile(final String resourceProfile) throws IndexServiceException {
130
		try {
131
			return serviceLocator.getService(ISRegistryService.class).registerProfile(resourceProfile);
132
		} catch (ISRegistryException e) {
133
			throw new IndexServiceException(e);
134
		}
135
	}
136

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

    
142
		log.info("performing increment of HANDLED_DATASTRUCTURE");
143
		return executeXUpdate(xquery);
144
	}
145

    
146
	private boolean executeXUpdate(final String xquery) throws IndexServiceException {
147
		try {
148
			return serviceLocator.getService(ISRegistryService.class).executeXUpdate(xquery);
149
		} catch (ISRegistryException e) {
150
			throw new IndexServiceException(e);
151
		}
152
	}
153

    
154
	public String getServiceAddress(final String backendId) {
155
		final String xquery = "let $x := //RESOURCE_PROFILE[HEADER/PROTOCOLS/PROTOCOL/@name='" + backendId + "']"
156
				+ "return $x//PROTOCOL[./@name='SOAP']/@address/string()";
157
		try {
158
			return getResourceProfileByQuery(xquery);
159
		} catch (IndexServiceException e) {
160
			log.warn("couldn't find service Address for index Service with protocol: " + backendId);
161
			return "";
162
		}
163
	}
164

    
165
	public boolean deleteIndexDS(final String dsId) throws IndexServiceException {
166
		try {
167
			return serviceLocator.getService(ISRegistryService.class).deleteProfile(dsId);
168
		} catch (ISRegistryDocumentNotFoundException e) {
169
			throw new IndexServiceException(e);
170
		} catch (ISRegistryException e) {
171
			throw new IndexServiceException(e);
172
		}
173
	}
174

    
175
	public List<String> getBackendIds(final MetadataReference mdRef) throws IndexServiceException {
176
		String query = "distinct-values(//RESOURCE_PROFILE[.//METADATA_FORMAT='%s' and .//METADATA_FORMAT_LAYOUT='%s' and .//METADATA_FORMAT_INTERPRETATION='%s']//BACKEND/@ID/string())";
177
		try {
178
			String instanceQuery = String.format(query, mdRef.getFormat(), mdRef.getLayout(), mdRef.getInterpretation());
179
			log.debug("Executing query to IS: " + instanceQuery);
180
			return serviceLocator.getService(ISLookUpService.class).quickSearchProfile(instanceQuery);
181
		} catch (ISLookUpException e) {
182
			throw new IndexServiceException(e);
183
		}
184
	}
185
}
(5-5/5)