|
1 |
package eu.dnetlib.enabling.datasources;
|
|
2 |
|
|
3 |
import java.io.StringReader;
|
|
4 |
import java.text.ParseException;
|
|
5 |
import java.util.Collections;
|
|
6 |
import java.util.Date;
|
|
7 |
import java.util.List;
|
|
8 |
import java.util.Map;
|
|
9 |
|
|
10 |
import javax.annotation.Resource;
|
|
11 |
|
|
12 |
import org.antlr.stringtemplate.StringTemplate;
|
|
13 |
import org.apache.commons.io.IOUtils;
|
|
14 |
import org.apache.commons.lang.StringEscapeUtils;
|
|
15 |
import org.apache.commons.lang.StringUtils;
|
|
16 |
import org.apache.commons.lang.math.NumberUtils;
|
|
17 |
import org.apache.commons.logging.Log;
|
|
18 |
import org.apache.commons.logging.LogFactory;
|
|
19 |
import org.dom4j.Document;
|
|
20 |
import org.dom4j.Element;
|
|
21 |
import org.dom4j.Node;
|
|
22 |
import org.dom4j.io.SAXReader;
|
|
23 |
import org.quartz.CronExpression;
|
|
24 |
import org.springframework.beans.factory.annotation.Required;
|
|
25 |
import org.springframework.core.io.ClassPathResource;
|
|
26 |
|
|
27 |
import com.google.common.base.Function;
|
|
28 |
import com.google.common.collect.Lists;
|
|
29 |
import com.google.common.collect.Maps;
|
|
30 |
import com.google.common.collect.Sets;
|
|
31 |
|
|
32 |
import eu.dnetlib.enabling.datasources.rmi.BrowsableField;
|
|
33 |
import eu.dnetlib.enabling.datasources.rmi.BrowseTerm;
|
|
34 |
import eu.dnetlib.enabling.datasources.rmi.DatasourceConstants;
|
|
35 |
import eu.dnetlib.enabling.datasources.rmi.DatasourceDesc;
|
|
36 |
import eu.dnetlib.enabling.datasources.rmi.DatasourceManagerService;
|
|
37 |
import eu.dnetlib.enabling.datasources.rmi.DatasourceManagerServiceException;
|
|
38 |
import eu.dnetlib.enabling.datasources.rmi.IfaceDesc;
|
|
39 |
import eu.dnetlib.enabling.datasources.rmi.RepositoryMapEntry;
|
|
40 |
import eu.dnetlib.enabling.datasources.rmi.SearchInterfacesEntry;
|
|
41 |
import eu.dnetlib.enabling.datasources.rmi.SimpleDatasourceDesc;
|
|
42 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException;
|
|
43 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
|
44 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
|
45 |
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryException;
|
|
46 |
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
|
|
47 |
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
|
48 |
import eu.dnetlib.enabling.tools.AbstractBaseService;
|
|
49 |
|
|
50 |
public class DatasourceManagerServiceImpl extends AbstractBaseService implements DatasourceManagerService {
|
|
51 |
|
|
52 |
private static final Log log = LogFactory.getLog(DatasourceManagerServiceImpl.class);
|
|
53 |
|
|
54 |
private static final String REPOSITORY_RESOURCE_TYPE = "RepositoryServiceResourceType";
|
|
55 |
|
|
56 |
private List<XmlBrowsableField> browsableFields;
|
|
57 |
|
|
58 |
private ClassPathResource findReposQueryTmpl = new ClassPathResource("/eu/dnetlib/enabling/datasources/templates/findRepos.xquery.st");
|
|
59 |
|
|
60 |
private ClassPathResource browseRepoApisQueryTmpl = new ClassPathResource("/eu/dnetlib/enabling/datasources/templates/browseRepoApis.xquery.st");
|
|
61 |
private ClassPathResource findRepoApisQueryTmpl = new ClassPathResource("/eu/dnetlib/enabling/datasources/templates/findRepoApis.xquery.st");
|
|
62 |
private ClassPathResource findReposMapQuery = new ClassPathResource("/eu/dnetlib/enabling/datasources/templates/findReposMap.xquery");
|
|
63 |
private ClassPathResource simpleFindReposQueryTmpl = new ClassPathResource("/eu/dnetlib/enabling/datasources/templates/simpleFindRepos.xquery.st");
|
|
64 |
|
|
65 |
@Resource
|
|
66 |
private UniqueServiceLocator serviceLocator;
|
|
67 |
|
|
68 |
private String fixDsId(final String dsId) throws DatasourceManagerServiceException {
|
|
69 |
if (dsId.endsWith("_UGVuZGluZ1JlcG9zaXRvcnlSZXNvdXJjZXMvUmVwb3NpdG9yeVNlcnZpY2VSZXNvdXJjZVR5cGU=")
|
|
70 |
|| dsId.endsWith("_UmVwb3NpdG9yeVNlcnZpY2VSZXNvdXJjZXMvUmVwb3NpdG9yeVNlcnZpY2VSZXNvdXJjZVR5cGU=")) {
|
|
71 |
return dsId;
|
|
72 |
} else {
|
|
73 |
try {
|
|
74 |
return serviceLocator
|
|
75 |
.getService(ISLookUpService.class)
|
|
76 |
.getResourceProfileByQuery(
|
|
77 |
"for $x in collection("
|
|
78 |
+ "'/db/DRIVER/RepositoryServiceResources/RepositoryServiceResourceType',"
|
|
79 |
+ "'/db/DRIVER/PendingRepositoryResources/RepositoryServiceResourceType') "
|
|
80 |
+ "where $x//DATASOURCE_ORIGINAL_ID = '" + dsId + "' "
|
|
81 |
+ "return $x//RESOURCE_IDENTIFIER/@value/string()");
|
|
82 |
} catch (Exception e) {
|
|
83 |
throw new DatasourceManagerServiceException("Invalid id: " + dsId, e);
|
|
84 |
}
|
|
85 |
}
|
|
86 |
}
|
|
87 |
|
|
88 |
@Override
|
|
89 |
public boolean addDatasource(final DatasourceDesc ds) throws DatasourceManagerServiceException {
|
|
90 |
try {
|
|
91 |
final String profile = DatasourceDescToProfile.convert(ds);
|
|
92 |
serviceLocator.getService(ISRegistryService.class).registerProfile(profile);
|
|
93 |
return true;
|
|
94 |
} catch (Exception e) {
|
|
95 |
log.error("Error registering profile", e);
|
|
96 |
throw new DatasourceManagerServiceException("Error registering profile", e);
|
|
97 |
}
|
|
98 |
}
|
|
99 |
|
|
100 |
@Override
|
|
101 |
public boolean deleteDatasource(final String dsId) throws DatasourceManagerServiceException {
|
|
102 |
return _deleteDatasource(fixDsId(dsId));
|
|
103 |
}
|
|
104 |
|
|
105 |
private boolean _deleteDatasource(final String dsId) throws DatasourceManagerServiceException {
|
|
106 |
try {
|
|
107 |
return serviceLocator.getService(ISRegistryService.class).deleteProfile(dsId);
|
|
108 |
} catch (ISRegistryException e) {
|
|
109 |
log.error("Error deleting profile " + dsId, e);
|
|
110 |
throw new DatasourceManagerServiceException("Error deleting profile " + dsId, e);
|
|
111 |
}
|
|
112 |
}
|
|
113 |
|
|
114 |
@Override
|
|
115 |
public DatasourceDesc getDatasource(final String dsId) throws DatasourceManagerServiceException {
|
|
116 |
return _getDatasource(fixDsId(dsId));
|
|
117 |
}
|
|
118 |
|
|
119 |
private DatasourceDesc _getDatasource(final String dsId) throws DatasourceManagerServiceException {
|
|
120 |
try {
|
|
121 |
final String profile = serviceLocator.getService(ISLookUpService.class).getResourceProfile(dsId);
|
|
122 |
return ProfileToDatasourceDesc.convert(profile);
|
|
123 |
} catch (Exception e) {
|
|
124 |
log.error("Error accessing profile " + dsId, e);
|
|
125 |
throw new DatasourceManagerServiceException("Error accessing profile " + dsId, e);
|
|
126 |
}
|
|
127 |
}
|
|
128 |
|
|
129 |
@Override
|
|
130 |
public List<DatasourceDesc> listAllDatasources() throws DatasourceManagerServiceException {
|
|
131 |
return listDatasourcesUsingFilter(null, null, null, null);
|
|
132 |
}
|
|
133 |
|
|
134 |
@Override
|
|
135 |
public List<DatasourceDesc> listDatasourcesUsingFilter(final String compliance,
|
|
136 |
final String contentDescription,
|
|
137 |
final String iisProcessingWorkflow,
|
|
138 |
final String collectedFrom)
|
|
139 |
throws DatasourceManagerServiceException {
|
|
140 |
try {
|
|
141 |
final StringTemplate st = new StringTemplate();
|
|
142 |
st.setTemplate(IOUtils.toString(findReposQueryTmpl.getInputStream()));
|
|
143 |
|
|
144 |
final Map<String, String> conds = Maps.newHashMap();
|
|
145 |
|
|
146 |
if (StringUtils.isNotBlank(compliance)) {
|
|
147 |
conds.put("//INTERFACE/@compliance", compliance);
|
|
148 |
}
|
|
149 |
if (StringUtils.isNotBlank(contentDescription)) {
|
|
150 |
conds.put("//INTERFACE/@contentDescription", contentDescription);
|
|
151 |
}
|
|
152 |
if (StringUtils.isNotBlank(iisProcessingWorkflow)) {
|
|
153 |
// NOT USED
|
|
154 |
}
|
|
155 |
if (StringUtils.isNotBlank(collectedFrom)) {
|
|
156 |
// NOT USED
|
|
157 |
}
|
|
158 |
|
|
159 |
if (!conds.isEmpty()) {
|
|
160 |
st.setAttribute("conds", conds);
|
|
161 |
}
|
|
162 |
|
|
163 |
final List<String> list = serviceLocator.getService(ISLookUpService.class).quickSearchProfile(st.toString());
|
|
164 |
|
|
165 |
return Lists.transform(list, new ProfileToDatasourceDesc());
|
|
166 |
} catch (Exception e) {
|
|
167 |
log.error("Error listing datasources", e);
|
|
168 |
throw new DatasourceManagerServiceException("Error listing datasources", e);
|
|
169 |
}
|
|
170 |
}
|
|
171 |
|
|
172 |
@Override
|
|
173 |
public boolean updateLevelOfCompliance(final String dsId, final String ifaceId, final String level) throws DatasourceManagerServiceException {
|
|
174 |
return _updateLevelOfCompliance(fixDsId(dsId), ifaceId, level);
|
|
175 |
}
|
|
176 |
|
|
177 |
private boolean _updateLevelOfCompliance(final String dsId, final String ifaceId, final String level) throws DatasourceManagerServiceException {
|
|
178 |
try {
|
|
179 |
return serviceLocator.getService(ISRegistryService.class)
|
|
180 |
.updateProfileNode(dsId, "//INTERFACE[@id = '" + ifaceId + "']/@compliance", "'" + level + "'");
|
|
181 |
} catch (Exception e) {
|
|
182 |
log.error("Error updating profile: " + dsId, e);
|
|
183 |
throw new DatasourceManagerServiceException("Error updating profile: " + dsId, e);
|
|
184 |
}
|
|
185 |
}
|
|
186 |
|
|
187 |
@Override
|
|
188 |
public boolean updateBaseUrl(final String dsId, final String ifaceId, final String baseUrl) throws DatasourceManagerServiceException {
|
|
189 |
return _updateBaseUrl(fixDsId(dsId), ifaceId, baseUrl);
|
|
190 |
}
|
|
191 |
|
|
192 |
private boolean _updateBaseUrl(final String dsId, final String ifaceId, final String baseUrl) throws DatasourceManagerServiceException {
|
|
193 |
try {
|
|
194 |
return serviceLocator.getService(ISRegistryService.class)
|
|
195 |
.updateProfileNode(dsId, "//INTERFACE[@id = '" + ifaceId + "']/BASE_URL", "<BASE_URL>" + baseUrl + "</BASE_URL>");
|
|
196 |
} catch (Exception e) {
|
|
197 |
log.error("Error updating profile: " + dsId, e);
|
|
198 |
throw new DatasourceManagerServiceException("Error updating profile: " + dsId, e);
|
|
199 |
}
|
|
200 |
}
|
|
201 |
|
|
202 |
@Override
|
|
203 |
public boolean updateActivationStatus(final String dsId, final String ifaceId, final boolean active) throws DatasourceManagerServiceException {
|
|
204 |
return _updateActivationStatus(fixDsId(dsId), ifaceId, active);
|
|
205 |
}
|
|
206 |
|
|
207 |
private boolean _updateActivationStatus(final String dsId, final String ifaceId, final boolean active) throws DatasourceManagerServiceException {
|
|
208 |
try {
|
|
209 |
return serviceLocator.getService(ISRegistryService.class)
|
|
210 |
.updateProfileNode(dsId, "//INTERFACE[@id = '" + ifaceId + "']/@active", "'" + active + "'");
|
|
211 |
} catch (Exception e) {
|
|
212 |
log.error("Error updating profile: " + dsId, e);
|
|
213 |
throw new DatasourceManagerServiceException("Error updating profile: " + dsId, e);
|
|
214 |
}
|
|
215 |
}
|
|
216 |
|
|
217 |
@Override
|
|
218 |
public boolean updateContentDescription(final String dsId, final String ifaceId, final String desc) throws DatasourceManagerServiceException {
|
|
219 |
return _updateContentDescription(fixDsId(dsId), ifaceId, desc);
|
|
220 |
}
|
|
221 |
|
|
222 |
private boolean _updateContentDescription(final String dsId, final String ifaceId, final String desc) throws DatasourceManagerServiceException {
|
|
223 |
try {
|
|
224 |
return serviceLocator.getService(ISRegistryService.class)
|
|
225 |
.updateProfileNode(dsId, "//INTERFACE[@id = '" + ifaceId + "']/@contentDescription", "'" + desc + "'");
|
|
226 |
} catch (Exception e) {
|
|
227 |
log.error("Error updating profile: " + dsId, e);
|
|
228 |
throw new DatasourceManagerServiceException("Error updating profile: " + dsId, e);
|
|
229 |
}
|
|
230 |
}
|
|
231 |
|
|
232 |
@Override
|
|
233 |
public boolean setIisProcessingWorkflow(final String dsId, final String ifaceId, final String wf) throws DatasourceManagerServiceException {
|
|
234 |
throw new DatasourceManagerServiceException("NOT IMPLEMENTED");
|
|
235 |
}
|
|
236 |
|
|
237 |
@Override
|
|
238 |
public boolean updateExtraField(final String dsId, final String ifaceId, final String field, final String value, final boolean preserveOriginal)
|
|
239 |
throws DatasourceManagerServiceException {
|
|
240 |
return _updateExtraField(fixDsId(dsId), ifaceId, field, value, preserveOriginal);
|
|
241 |
}
|
|
242 |
|
|
243 |
private boolean _updateExtraField(final String dsId, final String ifaceId, final String field, final String value, final boolean preserveOriginal)
|
|
244 |
throws DatasourceManagerServiceException {
|
|
245 |
try {
|
|
246 |
final String profile = serviceLocator.getService(ISLookUpService.class).getResourceProfile(dsId);
|
|
247 |
final Document doc = new SAXReader().read(new StringReader(profile));
|
|
248 |
|
|
249 |
final Node ifcNode = doc.selectSingleNode("//INTERFACE[@id = '" + ifaceId + "']");
|
|
250 |
final Node node = ifcNode.selectSingleNode("./INTERFACE_EXTRA_FIELD[@name = '" + field + "']");
|
|
251 |
if (node != null) {
|
|
252 |
node.setText(value);
|
|
253 |
} else {
|
|
254 |
final Element e = ((Element) ifcNode).addElement("INTERFACE_EXTRA_FIELD");
|
|
255 |
e.addAttribute("name", field);
|
|
256 |
e.setText(value);
|
|
257 |
}
|
|
258 |
return serviceLocator.getService(ISRegistryService.class).updateProfile(dsId, doc.asXML(), REPOSITORY_RESOURCE_TYPE);
|
|
259 |
} catch (Exception e) {
|
|
260 |
log.error("Error updating profile: " + dsId, e);
|
|
261 |
throw new DatasourceManagerServiceException("Error updating profile: " + dsId, e);
|
|
262 |
}
|
|
263 |
}
|
|
264 |
|
|
265 |
@Override
|
|
266 |
public boolean updateAccessParam(final String dsId, final String ifaceId, final String field, final String value, final boolean preserveOriginal)
|
|
267 |
throws DatasourceManagerServiceException {
|
|
268 |
return _updateAccessParam(fixDsId(dsId), ifaceId, field, value, preserveOriginal);
|
|
269 |
}
|
|
270 |
|
|
271 |
private boolean _updateAccessParam(final String dsId, final String ifaceId, final String field, final String value, final boolean preserveOriginal)
|
|
272 |
throws DatasourceManagerServiceException {
|
|
273 |
try {
|
|
274 |
final String profile = serviceLocator.getService(ISLookUpService.class).getResourceProfile(dsId);
|
|
275 |
final Document doc = new SAXReader().read(new StringReader(profile));
|
|
276 |
|
|
277 |
final Node node = doc.selectSingleNode("//INTERFACE[@id = '" + ifaceId + "']/ACCESS_PROTOCOL");
|
|
278 |
((Element) node).addAttribute(field, value);
|
|
279 |
|
|
280 |
return serviceLocator.getService(ISRegistryService.class).updateProfile(dsId, doc.asXML(), REPOSITORY_RESOURCE_TYPE);
|
|
281 |
} catch (Exception e) {
|
|
282 |
log.error("Error updating profile: " + dsId, e);
|
|
283 |
throw new DatasourceManagerServiceException("Error updating profile: " + dsId, e);
|
|
284 |
}
|
|
285 |
}
|
|
286 |
|
|
287 |
@Override
|
|
288 |
public boolean deleteAccessParamOrExtraField(final String dsId, final String ifaceId, final String field) throws DatasourceManagerServiceException {
|
|
289 |
return _deleteAccessParamOrExtraField(fixDsId(dsId), ifaceId, field);
|
|
290 |
}
|
|
291 |
|
|
292 |
private boolean _deleteAccessParamOrExtraField(final String dsId, final String ifaceId, final String field) throws DatasourceManagerServiceException {
|
|
293 |
try {
|
|
294 |
final String profile = serviceLocator.getService(ISLookUpService.class).getResourceProfile(dsId);
|
|
295 |
final Document doc = new SAXReader().read(new StringReader(profile));
|
|
296 |
|
|
297 |
final Node ef = doc.selectSingleNode("//INTERFACE[@id = '" + ifaceId + "']/INTERFACE_EXTRA_FIELD[@name = '" + field + "']");
|
|
298 |
if (ef != null) {
|
|
299 |
ef.detach();
|
|
300 |
}
|
|
301 |
final Node ap = doc.selectSingleNode("//INTERFACE[@id = '" + ifaceId + "']/ACCESS_PROTOCOL/@" + field);
|
|
302 |
if (ap != null) {
|
|
303 |
ap.detach();
|
|
304 |
|
|
305 |
}
|
|
306 |
return serviceLocator.getService(ISRegistryService.class).updateProfile(dsId, doc.asXML(), REPOSITORY_RESOURCE_TYPE);
|
|
307 |
} catch (Exception e) {
|
|
308 |
log.error("Error updating profile: " + dsId, e);
|
|
309 |
throw new DatasourceManagerServiceException("Error updating profile: " + dsId, e);
|
|
310 |
}
|
|
311 |
}
|
|
312 |
|
|
313 |
@Override
|
|
314 |
public boolean addInterface(final String dsId, final IfaceDesc iface) throws DatasourceManagerServiceException {
|
|
315 |
return _addInterface(fixDsId(dsId), iface);
|
|
316 |
}
|
|
317 |
|
|
318 |
private boolean _addInterface(final String dsId, final IfaceDesc iface) throws DatasourceManagerServiceException {
|
|
319 |
try {
|
|
320 |
final String profile = serviceLocator.getService(ISLookUpService.class).getResourceProfile(dsId);
|
|
321 |
final Document doc = new SAXReader().read(new StringReader(profile));
|
|
322 |
|
|
323 |
final Node node = doc.selectSingleNode("//INTERFACE[@id = '" + iface.getId() + "']");
|
|
324 |
if (node != null) {
|
|
325 |
node.detach();
|
|
326 |
}
|
|
327 |
|
|
328 |
((Element) doc.selectSingleNode("//INTERFACES")).add(IfaceDescToNode.convert(iface));
|
|
329 |
|
|
330 |
return serviceLocator.getService(ISRegistryService.class).updateProfile(dsId, doc.asXML(), REPOSITORY_RESOURCE_TYPE);
|
|
331 |
} catch (Exception e) {
|
|
332 |
log.error("Error updating profile: " + dsId, e);
|
|
333 |
throw new DatasourceManagerServiceException("Error updating profile: " + dsId, e);
|
|
334 |
}
|
|
335 |
}
|
|
336 |
|
|
337 |
@Override
|
|
338 |
public boolean deleteInterface(final String dsId, final String ifaceId) throws DatasourceManagerServiceException {
|
|
339 |
return _deleteInterface(fixDsId(dsId), ifaceId);
|
|
340 |
}
|
|
341 |
|
|
342 |
private boolean _deleteInterface(final String dsId, final String ifaceId) throws DatasourceManagerServiceException {
|
|
343 |
try {
|
|
344 |
final String profile = serviceLocator.getService(ISLookUpService.class).getResourceProfile(dsId);
|
|
345 |
final Document doc = new SAXReader().read(new StringReader(profile));
|
|
346 |
|
|
347 |
final Node node = doc.selectSingleNode("//INTERFACE[@id = '" + ifaceId + "']");
|
|
348 |
if (node != null) {
|
|
349 |
node.detach();
|
|
350 |
}
|
|
351 |
|
|
352 |
return serviceLocator.getService(ISRegistryService.class).updateProfile(dsId, doc.asXML(), REPOSITORY_RESOURCE_TYPE);
|
|
353 |
} catch (Exception e) {
|
|
354 |
log.error("Error updating profile: " + dsId, e);
|
|
355 |
throw new DatasourceManagerServiceException("Error updating profile: " + dsId, e);
|
|
356 |
}
|
|
357 |
}
|
|
358 |
|
|
359 |
@Override
|
|
360 |
public boolean updateSQL(final String dsId, final String sql, final boolean delete) throws DatasourceManagerServiceException {
|
|
361 |
throw new DatasourceManagerServiceException("NOT IMPLEMENTED");
|
|
362 |
}
|
|
363 |
|
|
364 |
@Override
|
|
365 |
public Date findNextScheduledExecution(final String dsId, final String ifaceId) throws DatasourceManagerServiceException {
|
|
366 |
return _findNextScheduledExecution(fixDsId(dsId), ifaceId);
|
|
367 |
}
|
|
368 |
|
|
369 |
private Date _findNextScheduledExecution(final String dsId, final String ifaceId) throws DatasourceManagerServiceException {
|
|
370 |
final String xquery = "/*[.//DATAPROVIDER/@interface='" + ifaceId + "' and .//SCHEDULING/@enabled='true']//CRON/text()";
|
|
371 |
try {
|
|
372 |
final String cronExpression = serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(xquery);
|
|
373 |
final CronExpression cron = new CronExpression(cronExpression);
|
|
374 |
return cron.getNextValidTimeAfter(new Date());
|
|
375 |
} catch (ISLookUpDocumentNotFoundException e) {
|
|
376 |
// When the value is not found a null value must be returned
|
|
377 |
return null;
|
|
378 |
} catch (ISLookUpException e) {
|
|
379 |
log.error("Error in xquery: " + xquery, e);
|
|
380 |
throw new DatasourceManagerServiceException("Error in xquery: " + xquery, e);
|
|
381 |
} catch (ParseException e) {
|
|
382 |
log.error("Error parsing cron expression", e);
|
|
383 |
throw new DatasourceManagerServiceException("Error parsing cron expression", e);
|
|
384 |
}
|
|
385 |
}
|
|
386 |
|
|
387 |
@Override
|
|
388 |
public boolean bulkUpdateApiExtraFields(final String repoId, final String ifaceId, final Map<String, String> fields)
|
|
389 |
throws DatasourceManagerServiceException {
|
|
390 |
try {
|
|
391 |
final String profile = serviceLocator.getService(ISLookUpService.class).getResourceProfile(repoId);
|
|
392 |
|
|
393 |
final SAXReader reader = new SAXReader();
|
|
394 |
final Document doc = reader.read(new StringReader(profile));
|
|
395 |
|
|
396 |
final Element iface = (Element) doc.selectSingleNode("//INTERFACE[@id='" + ifaceId + "']");
|
|
397 |
if (iface != null) {
|
|
398 |
|
|
399 |
while (iface.selectNodes("./INTERFACE_EXTRA_FIELD").size() > 0) {
|
|
400 |
iface.selectSingleNode("./INTERFACE_EXTRA_FIELD").detach();
|
|
401 |
}
|
|
402 |
for (Map.Entry<String, String> e : fields.entrySet()) {
|
|
403 |
if (e.getValue() != null && !e.getValue().isEmpty()) {
|
|
404 |
final Element field = iface.addElement("INTERFACE_EXTRA_FIELD");
|
|
405 |
field.addAttribute("name", e.getKey());
|
|
406 |
field.addText(e.getValue());
|
|
407 |
}
|
|
408 |
}
|
|
409 |
serviceLocator.getService(ISRegistryService.class).updateProfile(repoId, doc.asXML(), REPOSITORY_RESOURCE_TYPE);
|
|
410 |
} else {
|
|
411 |
log.error("Invalid interface: " + ifaceId);
|
|
412 |
throw new DatasourceManagerServiceException("Missing interface: " + ifaceId);
|
|
413 |
}
|
|
414 |
} catch (Exception e) {
|
|
415 |
log.error("Error updating API of profile: " + repoId);
|
|
416 |
throw new DatasourceManagerServiceException("Error updating API of profile: " + repoId, e);
|
|
417 |
}
|
|
418 |
return true;
|
|
419 |
|
|
420 |
}
|
|
421 |
|
|
422 |
@Override
|
|
423 |
public boolean bulkUpdateApiAccessParams(final String dsId, final String ifaceId, final Map<String, String> params)
|
|
424 |
throws DatasourceManagerServiceException {
|
|
425 |
return _bulkUpdateApiAccessParams(fixDsId(dsId), ifaceId, params);
|
|
426 |
}
|
|
427 |
|
|
428 |
private boolean _bulkUpdateApiAccessParams(final String repoId, final String ifaceId, final Map<String, String> params)
|
|
429 |
throws DatasourceManagerServiceException {
|
|
430 |
try {
|
|
431 |
final String profile = serviceLocator.getService(ISLookUpService.class).getResourceProfile(repoId);
|
|
432 |
|
|
433 |
final SAXReader reader = new SAXReader();
|
|
434 |
final Document doc = reader.read(new StringReader(profile));
|
|
435 |
|
|
436 |
final Element accessNode = (Element) doc.selectSingleNode("//INTERFACE[@id='" + ifaceId + "']/ACCESS_PROTOCOL");
|
|
437 |
if (accessNode != null) {
|
|
438 |
while (accessNode.attributes().size() > 0) {
|
|
439 |
accessNode.selectSingleNode("@*").detach();
|
|
440 |
}
|
|
441 |
for (Map.Entry<String, String> e : params.entrySet()) {
|
|
442 |
if (e.getValue() != null && !e.getValue().isEmpty()) {
|
|
443 |
if (e.getKey().equalsIgnoreCase("baseUrl")) {
|
|
444 |
doc.selectSingleNode("//INTERFACE[@id='" + ifaceId + "']/BASE_URL").setText(e.getValue());
|
|
445 |
} else {
|
|
446 |
accessNode.addAttribute(e.getKey(), e.getValue());
|
|
447 |
}
|
|
448 |
}
|
|
449 |
}
|
|
450 |
serviceLocator.getService(ISRegistryService.class).updateProfile(repoId, doc.asXML(), REPOSITORY_RESOURCE_TYPE);
|
|
451 |
} else {
|
|
452 |
log.error("Invalid interface: " + ifaceId);
|
|
453 |
throw new DatasourceManagerServiceException("Missing interface: " + ifaceId);
|
|
454 |
}
|
|
455 |
} catch (Exception e) {
|
|
456 |
log.error("Error updating API of profile: " + repoId);
|
|
457 |
throw new DatasourceManagerServiceException("Error updating API of profile: " + repoId, e);
|
|
458 |
}
|
|
459 |
return true;
|
|
460 |
}
|
|
461 |
|
|
462 |
@Override
|
|
463 |
public boolean overrideCompliance(final String dsId, final String ifaceId, final String level) throws DatasourceManagerServiceException {
|
|
464 |
return _overrideCompliance(fixDsId(dsId), ifaceId, level);
|
|
465 |
}
|
|
466 |
|
|
467 |
private boolean _overrideCompliance(final String repoId, final String ifaceId, final String compliance) throws DatasourceManagerServiceException {
|
|
468 |
try {
|
|
469 |
final String profile = serviceLocator.getService(ISLookUpService.class).getResourceProfile(repoId);
|
|
470 |
|
|
471 |
final SAXReader reader = new SAXReader();
|
|
472 |
final Document doc = reader.read(new StringReader(profile));
|
|
473 |
|
|
474 |
final Element iface = (Element) doc.selectSingleNode("//INTERFACE[@id='" + ifaceId + "']");
|
|
475 |
if (iface != null) {
|
|
476 |
final Map<String, String> fields = Maps.newHashMap();
|
|
477 |
|
|
478 |
if (!StringUtils.isEmpty(compliance)) {
|
|
479 |
fields.put(DatasourceConstants.OVERRIDING_COMPLIANCE_FIELD, compliance);
|
|
480 |
}
|
|
481 |
|
|
482 |
while (iface.selectNodes("./INTERFACE_EXTRA_FIELD").size() > 0) {
|
|
483 |
final Node node = iface.selectSingleNode("./INTERFACE_EXTRA_FIELD");
|
|
484 |
final String name = node.valueOf("@name");
|
|
485 |
|
|
486 |
if (!name.equals(DatasourceConstants.OVERRIDING_COMPLIANCE_FIELD)) {
|
|
487 |
fields.put(node.valueOf("@name"), node.getText());
|
|
488 |
}
|
|
489 |
node.detach();
|
|
490 |
}
|
|
491 |
|
|
492 |
for (Map.Entry<String, String> e : fields.entrySet()) {
|
|
493 |
if (e.getValue() != null && !e.getValue().isEmpty()) {
|
|
494 |
final Element field = iface.addElement("INTERFACE_EXTRA_FIELD");
|
|
495 |
field.addAttribute("name", e.getKey());
|
|
496 |
field.addText(e.getValue());
|
|
497 |
}
|
|
498 |
}
|
|
499 |
serviceLocator.getService(ISRegistryService.class).updateProfile(repoId, doc.asXML(), REPOSITORY_RESOURCE_TYPE);
|
|
500 |
} else {
|
|
501 |
log.error("Invalid interface: " + ifaceId);
|
|
502 |
throw new DatasourceManagerServiceException("Missing interface: " + ifaceId);
|
|
503 |
}
|
|
504 |
} catch (Exception e) {
|
|
505 |
log.error("Error updating API of profile: " + repoId);
|
|
506 |
throw new DatasourceManagerServiceException("Error updating API of profile: " + repoId, e);
|
|
507 |
}
|
|
508 |
|
|
509 |
return true;
|
|
510 |
}
|
|
511 |
|
|
512 |
@Override
|
|
513 |
public List<BrowsableField> listBrowsableFields() throws DatasourceManagerServiceException {
|
|
514 |
return Lists.transform(getBrowsableFields(), new Function<XmlBrowsableField, BrowsableField>() {
|
|
515 |
|
|
516 |
@Override
|
|
517 |
public BrowsableField apply(final XmlBrowsableField f) {
|
|
518 |
return new BrowsableField(f.getId(), f.getLabel());
|
|
519 |
}
|
|
520 |
});
|
|
521 |
}
|
|
522 |
|
|
523 |
@Override
|
|
524 |
public List<BrowseTerm> browseField(final String f) throws DatasourceManagerServiceException {
|
|
525 |
|
|
526 |
final List<BrowseTerm> list = Lists.newArrayList();
|
|
527 |
|
|
528 |
try {
|
|
529 |
final XmlBrowsableField field = findBrowseField(f);
|
|
530 |
if (field != null) {
|
|
531 |
final StringTemplate st = new StringTemplate(IOUtils.toString(browseRepoApisQueryTmpl.getInputStream()));
|
|
532 |
st.setAttribute("xpath", field.getXpath());
|
|
533 |
|
|
534 |
final Map<String, String> terms = fetchVocabularyTerms(field.getVocabulary());
|
|
535 |
|
|
536 |
for (String s : serviceLocator.getService(ISLookUpService.class).quickSearchProfile(st.toString())) {
|
|
537 |
final String[] arr = s.split("@-@-@");
|
|
538 |
final String id = arr[0].trim();
|
|
539 |
final int count = NumberUtils.toInt(arr[1].trim(), 0);
|
|
540 |
list.add(new BrowseTerm(id, findLabel(id, terms), count));
|
|
541 |
}
|
|
542 |
}
|
|
543 |
} catch (Exception e) {
|
|
544 |
log.error("Error browsing field " + f, e);
|
|
545 |
}
|
|
546 |
|
|
547 |
return list;
|
|
548 |
}
|
|
549 |
|
|
550 |
private String findLabel(final String code, final Map<String, String> terms) {
|
|
551 |
return terms.containsKey(code) ? terms.get(code) : code;
|
|
552 |
}
|
|
553 |
|
|
554 |
public Map<String, String> fetchVocabularyTerms(final String voc) throws ISLookUpException {
|
|
555 |
final String xquery = "for $x in collection('/db/DRIVER/VocabularyDSResources/VocabularyDSResourceType')[.//VOCABULARY_NAME/@code = '"
|
|
556 |
+ voc.trim() + "']//TERM return concat($x/@code, ' @@@ ', $x/@english_name)";
|
|
557 |
|
|
558 |
final Map<String, String> map = Maps.newHashMap();
|
|
559 |
for (String s : serviceLocator.getService(ISLookUpService.class).quickSearchProfile(xquery)) {
|
|
560 |
final String[] arr = s.split("@@@");
|
|
561 |
map.put(arr[0].trim(), arr[1].trim());
|
|
562 |
}
|
|
563 |
return map;
|
|
564 |
}
|
|
565 |
|
|
566 |
private XmlBrowsableField findBrowseField(final String id) {
|
|
567 |
for (XmlBrowsableField f : getBrowsableFields()) {
|
|
568 |
if (f.getId().equals(id)) { return f; }
|
|
569 |
}
|
|
570 |
return null;
|
|
571 |
}
|
|
572 |
|
|
573 |
@Override
|
|
574 |
public List<SearchInterfacesEntry> searchInterface(final String field, final String value) throws DatasourceManagerServiceException {
|
|
575 |
try {
|
|
576 |
|
|
577 |
final StringTemplate st = new StringTemplate();
|
|
578 |
st.setTemplate(IOUtils.toString(findRepoApisQueryTmpl.getInputStream()));
|
|
579 |
|
|
580 |
if (field.equalsIgnoreCase("__search__")) {
|
|
581 |
st.setAttribute("cond", "contains(../..//(*|@*)/lower-case(.), '" + StringEscapeUtils.escapeXml(value.toLowerCase()) + "')");
|
|
582 |
} else {
|
|
583 |
final XmlBrowsableField f = findBrowseField(field);
|
|
584 |
if (f != null) {
|
|
585 |
st.setAttribute("cond", f.getXpath() + "='" + StringEscapeUtils.escapeXml(value) + "'");
|
|
586 |
} else {
|
|
587 |
return Lists.newArrayList();
|
|
588 |
}
|
|
589 |
}
|
|
590 |
|
|
591 |
final String query = st.toString();
|
|
592 |
|
|
593 |
final SAXReader reader = new SAXReader();
|
|
594 |
|
|
595 |
final List<String> list = serviceLocator.getService(ISLookUpService.class).quickSearchProfile(query);
|
|
596 |
|
|
597 |
return Lists.transform(list, new Function<String, SearchInterfacesEntry>() {
|
|
598 |
|
|
599 |
@Override
|
|
600 |
public SearchInterfacesEntry apply(final String s) {
|
|
601 |
final SearchInterfacesEntry iface = new SearchInterfacesEntry();
|
|
602 |
try {
|
|
603 |
final Document doc = reader.read(new StringReader(s));
|
|
604 |
final String country = doc.valueOf("//REPO/@country");
|
|
605 |
|
|
606 |
iface.setRepoId(doc.valueOf("//REPO/@id"));
|
|
607 |
iface.setRepoCountry(StringUtils.isEmpty(country) ? "-" : country.toUpperCase());
|
|
608 |
iface.setRepoName(doc.valueOf("//REPO/@name"));
|
|
609 |
iface.setRepoPrefix(doc.valueOf("//REPO/@prefix"));
|
|
610 |
|
|
611 |
final Node ifcNode = doc.selectSingleNode("//INTERFACE");
|
|
612 |
|
|
613 |
iface.setId(ifcNode.valueOf("./@id"));
|
|
614 |
iface.setActive(Boolean.valueOf(ifcNode.valueOf("./@active")));
|
|
615 |
iface.setProtocol(ifcNode.valueOf("./ACCESS_PROTOCOL/text()"));
|
|
616 |
|
|
617 |
final String overCompliance = ifcNode.valueOf("./INTERFACE_EXTRA_FIELD[@name='overriding_compliance']");
|
|
618 |
if (StringUtils.isEmpty(overCompliance)) {
|
|
619 |
iface.setCompliance(ifcNode.valueOf("@compliance"));
|
|
620 |
} else {
|
|
621 |
iface.setCompliance(overCompliance);
|
|
622 |
}
|
|
623 |
|
|
624 |
final String lastAggregationDate = ifcNode.valueOf("./INTERFACE_EXTRA_FIELD[@name='last_aggregation_date']");
|
|
625 |
if (!StringUtils.isEmpty(lastAggregationDate)) {
|
|
626 |
iface.setAggrDate(lastAggregationDate);
|
|
627 |
} else {
|
|
628 |
final String lastDownloadDate = ifcNode.valueOf("./INTERFACE_EXTRA_FIELD[@name='last_download_date']");
|
|
629 |
if (!StringUtils.isEmpty(lastDownloadDate)) {
|
|
630 |
iface.setAggrDate(lastDownloadDate);
|
|
631 |
}
|
|
632 |
}
|
|
633 |
final String lastAggregationTotal = ifcNode.valueOf("./INTERFACE_EXTRA_FIELD[@name='last_aggregation_total']");
|
|
634 |
if (StringUtils.isEmpty(lastAggregationTotal)) {
|
|
635 |
final String lastDownloadTotal = ifcNode.valueOf("./INTERFACE_EXTRA_FIELD[@name='last_download_total']");
|
|
636 |
if (StringUtils.isEmpty(lastDownloadTotal)) {
|
|
637 |
iface.setAggrTotal(0);
|
|
638 |
} else {
|
|
639 |
iface.setAggrTotal(NumberUtils.toInt(lastDownloadTotal, 0));
|
|
640 |
}
|
|
641 |
} else {
|
|
642 |
iface.setAggrTotal(NumberUtils.toInt(lastAggregationTotal, 0));
|
|
643 |
}
|
|
644 |
} catch (Exception e) {
|
|
645 |
log.error(e);
|
|
646 |
}
|
|
647 |
return iface;
|
|
648 |
}
|
|
649 |
});
|
|
650 |
} catch (Exception e) {
|
|
651 |
log.error("Error searching field " + field + " - value: " + value, e);
|
|
652 |
}
|
|
653 |
return Lists.newArrayList();
|
|
654 |
}
|
|
655 |
|
|
656 |
@Override
|
|
657 |
public List<RepositoryMapEntry> getRepositoryMap() throws DatasourceManagerServiceException {
|
|
658 |
try {
|
|
659 |
final SAXReader reader = new SAXReader();
|
|
660 |
|
|
661 |
final String query = IOUtils.toString(findReposMapQuery.getInputStream());
|
|
662 |
final List<String> list = serviceLocator.getService(ISLookUpService.class).quickSearchProfile(query);
|
|
663 |
return Lists.transform(list, new Function<String, RepositoryMapEntry>() {
|
|
664 |
|
|
665 |
@Override
|
|
666 |
public RepositoryMapEntry apply(final String s) {
|
|
667 |
final RepositoryMapEntry r = new RepositoryMapEntry();
|
|
668 |
|
|
669 |
try {
|
|
670 |
final Document doc = reader.read(new StringReader(s));
|
|
671 |
r.setId(doc.valueOf("//dsId"));
|
|
672 |
r.setName(doc.valueOf("//name"));
|
|
673 |
r.setLat(Float.parseFloat(doc.valueOf("//lat")));
|
|
674 |
r.setLng(Float.parseFloat(doc.valueOf("//lng")));
|
|
675 |
} catch (Exception e) {
|
|
676 |
log.error(e);
|
|
677 |
}
|
|
678 |
return r;
|
|
679 |
}
|
|
680 |
});
|
|
681 |
} catch (Exception e) {
|
|
682 |
log.error("Error obtaing repo map entries", e);
|
|
683 |
}
|
|
684 |
return Lists.newArrayList();
|
|
685 |
}
|
|
686 |
|
|
687 |
private List<XmlBrowsableField> getBrowsableFields() {
|
|
688 |
return browsableFields;
|
|
689 |
}
|
|
690 |
|
|
691 |
@Required
|
|
692 |
public void setBrowsableFields(final List<XmlBrowsableField> browsableFields) {
|
|
693 |
this.browsableFields = browsableFields;
|
|
694 |
}
|
|
695 |
|
|
696 |
@Override
|
|
697 |
public List<SimpleDatasourceDesc> simpleListDatasourcesByType(final String type) throws DatasourceManagerServiceException {
|
|
698 |
try {
|
|
699 |
final List<SimpleDatasourceDesc> list = Lists.newArrayList();
|
|
700 |
|
|
701 |
final StringTemplate st = new StringTemplate(IOUtils.toString(simpleFindReposQueryTmpl.getInputStream()));
|
|
702 |
st.setAttribute("type", type);
|
|
703 |
|
|
704 |
for (String s : serviceLocator.getService(ISLookUpService.class).quickSearchProfile(st.toString())) {
|
|
705 |
final SimpleDatasourceDesc r = new SimpleDatasourceDesc();
|
|
706 |
final String[] arr = s.split("@=@");
|
|
707 |
r.setId(arr[0].trim());
|
|
708 |
r.setName(arr[1].trim());
|
|
709 |
r.setOrigId(arr[2].trim());
|
|
710 |
r.setApis(Sets.newHashSet(arr[3].replaceAll("\\s", "").split(",")));
|
|
711 |
r.setValid("true".equals(arr[4].trim()));
|
|
712 |
r.setTypology(type);
|
|
713 |
list.add(r);
|
|
714 |
}
|
|
715 |
|
|
716 |
Collections.sort(list);
|
|
717 |
|
|
718 |
return list;
|
|
719 |
} catch (Exception e) {
|
|
720 |
log.error("Error listing repos", e);
|
|
721 |
}
|
|
722 |
return Lists.newArrayList();
|
|
723 |
}
|
|
724 |
|
|
725 |
}
|
[maven-release-plugin] copy for tag dnet-datasource-manager-service-1.0.3