Project

General

Profile

« Previous | Next » 

Revision 45283

codebase used to migrate to java8 the production system

View differences:

modules/dnet-openaireplus-datasource-manager-service/trunk/deploy.info
1
{"type_source": "SVN", "goal": "package -U -T 4C source:jar", "url": "http://svn-public.driver.research-infrastructures.eu/driver/dnet40/modules/dnet-openaireplus-datasource-manager-service/trunk/", "deploy_repository": "dnet4-snapshots", "version": "4", "mail": "sandro.labruzzo@isti.cnr.it,michele.artini@isti.cnr.it, claudio.atzori@isti.cnr.it, alessia.bardi@isti.cnr.it", "deploy_repository_url": "http://maven.research-infrastructures.eu/nexus/content/repositories/dnet4-snapshots", "name": "dnet-openaireplus-datasource-manager-service"}
modules/dnet-openaireplus-datasource-manager-service/trunk/src/main/java/eu/dnetlib/enabling/datasources/DatasourceFunctions.java
1
package eu.dnetlib.enabling.datasources;
2

  
3
import java.io.IOException;
4
import java.text.ParseException;
5
import java.text.SimpleDateFormat;
6
import java.util.ArrayList;
7
import java.util.Date;
8
import java.util.List;
9
import java.util.Map;
10

  
11
import org.antlr.stringtemplate.StringTemplate;
12
import org.apache.commons.io.IOUtils;
13
import org.apache.commons.lang.StringEscapeUtils;
14
import org.apache.commons.lang.StringUtils;
15
import org.apache.commons.logging.Log;
16
import org.apache.commons.logging.LogFactory;
17
import org.dom4j.Document;
18
import org.dom4j.Node;
19
import org.springframework.core.io.ClassPathResource;
20
import org.springframework.core.io.Resource;
21

  
22
import com.google.common.base.Splitter;
23
import com.google.common.collect.Lists;
24
import com.google.common.collect.Maps;
25

  
26
import eu.dnetlib.enabling.datasources.rmi.DatasourceConstants;
27
import eu.dnetlib.enabling.datasources.rmi.DatasourceDesc;
28
import eu.dnetlib.enabling.datasources.rmi.DatasourceManagerServiceException;
29
import eu.dnetlib.enabling.datasources.rmi.IfaceDesc;
30

  
31
public class DatasourceFunctions {
32

  
33
	private static final Log log = LogFactory.getLog(DatasourceFunctions.class); // NOPMD by marko on 11/24/08 5:02 PM
34

  
35
	private static Resource ifaceTemplate = new ClassPathResource("/eu/dnetlib/enabling/datasources/repo_interface.st");
36

  
37
	public static String buildInterface(final String infopackage) {
38
		final IfaceDesc iface = generateIfaceDesc(infopackage);
39

  
40
		if (iface != null) {
41
			final StringTemplate i = getTemplate(ifaceTemplate);
42
			i.setAttribute("ifc", iface);
43

  
44
			return i.toString();
45
		} else {
46
			return "";
47
		}
48
	}
49

  
50
	public static IfaceDesc generateIfaceDesc(final String infopackage) {
51
		if (infopackage == null || infopackage.trim().isEmpty()) { return null; }
52

  
53
		final IfaceDesc iface = new IfaceDesc();
54
		iface.setId(getValueBetween(infopackage, null, "<==1==>"));
55
		iface.setTypology(getValueBetween(infopackage, "<==1==>", "<==2==>"));
56
		iface.setCompliance(getValueBetween(infopackage, "<==2==>", "<==3==>"));
57
		iface.setContentDescription(getValueBetween(infopackage, "<==3==>", "<==4==>"));
58
		iface.setAccessProtocol(getValueBetween(infopackage, "<==4==>", "<==5==>"));
59
		iface.setActive(Boolean.parseBoolean(getValueBetween(infopackage, "<==5==>", "<==6==>")));
60
		iface.setRemovable(Boolean.parseBoolean(getValueBetween(infopackage, "<==6==>", "<==7==>")));
61

  
62
		final Map<String, String> accessParams = Maps.newHashMap();
63
		final Map<String, String> extraParams = Maps.newHashMap();
64

  
65
		for (String param : parseParams(getValueBetween(infopackage, "<==7==>", null))) {
66

  
67
			final String[] arr = param.split("###");
68

  
69
			if (arr.length == 3) {
70
				final boolean accessParam = Boolean.parseBoolean(arr[0].trim());
71
				final String paramName = arr[1].trim();
72
				final String paramValue = arr.length > 2 ? arr[2].trim() : "";
73

  
74
				if (accessParam) {
75
					if (paramName.equals(DatasourceParams.baseUrl.toString())) {
76
						iface.setBaseUrl(paramValue);
77
					} else {
78
						accessParams.put(paramName, paramValue);
79
					}
80
				} else {
81
					extraParams.put(paramName, paramValue);
82
				}
83
			} else {
84
				log.debug("Invalid Datasource Parameter");
85
			}
86
		}
87

  
88
		iface.setAccessParams(accessParams);
89
		iface.setExtraFields(extraParams);
90

  
91
		return iface;
92
	}
93

  
94
	private static String getValueBetween(final String s, final String pre, final String post) {
95
		if (pre == null && post == null) {
96
			return StringEscapeUtils.escapeXml(s);
97
		} else if (pre == null) {
98
			return StringEscapeUtils.escapeXml(StringUtils.substringBefore(s, post));
99
		} else if (post == null) {
100
			return StringEscapeUtils.escapeXml(StringUtils.substringAfter(s, pre));
101
		} else {
102
			return StringEscapeUtils.escapeXml(StringUtils.substringBetween(s, pre, post));
103
		}
104
	}
105

  
106
	private static Iterable<String> parseParams(final String s) {
107
		return Splitter.on("@@@").omitEmptyStrings().trimResults().split(s);
108
	}
109

  
110
	private static StringTemplate getTemplate(final Resource res) {
111
		try {
112
			return new StringTemplate(IOUtils.toString(res.getInputStream()));
113
		} catch (IOException e) {
114
			log.error("unable to get template", e);
115
			throw new RuntimeException(e);
116
		}
117
	}
118

  
119
	public static String asSqlValue(final String s) {
120
		return s == null ? "NULL" : "'" + StringEscapeUtils.escapeSql(s) + "'";
121
	}
122

  
123
	public static String asSqlValue(final Boolean b) {
124
		return b == null ? "NULL" : b.toString();
125
	}
126

  
127
	private static String asSqlValue(final Double d) {
128
		return d == null ? "NULL" : d.toString();
129
	}
130

  
131
	private static Object asSqlValue(final Date date) {
132
		if (date == null) { return "NULL"; }
133
		final long millis = date.getTime();
134
		final java.sql.Date sqlDate = new java.sql.Date(millis);
135
		return "'" + sqlDate.toString() + "'";
136
	}
137

  
138
	public static boolean verifyCompliance(final Document doc) {
139

  
140
		for (Object o : doc.selectNodes("//INTERFACE")) {
141
			final String compliance = ((Node) o).valueOf("./INTERFACE_EXTRA_FIELD[@name='" + DatasourceConstants.OVERRIDING_COMPLIANCE_FIELD + "']");
142
			if (!StringUtils.isEmpty(compliance)) {
143
				if (!"unknown".equalsIgnoreCase(compliance) && !"notCompatible".equalsIgnoreCase(compliance)) { return true; }
144
			} else {
145
				final String compliance2 = ((Node) o).valueOf("@compliance");
146
				if (!"unknown".equalsIgnoreCase(compliance2) && !"notCompatible".equalsIgnoreCase(compliance2)) { return true; }
147
			}
148
		}
149

  
150
		return false;
151
	}
152

  
153
	@SuppressWarnings("unchecked")
154
	public static DatasourceDesc xmlToDatasourceDesc(final Document doc) throws DatasourceManagerServiceException {
155

  
156
		final DatasourceDesc ds = new DatasourceDesc();
157

  
158
		ds.setId(findValue(doc, "id", String.class));
159
		ds.setOfficialName(findValue(doc, "officialname", String.class));
160
		ds.setEnglishName(findValue(doc, "englishname", String.class));
161
		ds.setWebsiteUrl(findValue(doc, "websiteurl", String.class));
162
		ds.setLogoUrl(findValue(doc, "logourl", String.class));
163
		ds.setCountryCode(findValue(doc, "countrycode", String.class));
164
		ds.setCountryName(findValue(doc, "countryname", String.class));
165
		ds.setOrganization(findValue(doc, "organization", String.class));
166
		ds.setContactEmail(findValue(doc, "contactemail", String.class));
167
		ds.setLatitude(findValue(doc, "latitude", Double.class));
168
		ds.setLongitude(findValue(doc, "longitude", Double.class));
169
		ds.setTimezone(findValue(doc, "timezone", Double.class));
170
		ds.setNamespacePrefix(findValue(doc, "namespaceprefix", String.class));
171
		ds.setOdNumberOfItems(findValue(doc, "od_numberofitems", String.class));
172
		ds.setOdNumberOfItemsDate(findValue(doc, "od_numberofitemsdate", String.class));
173
		ds.setOdPolicies(findValue(doc, "od_policies", String.class));
174
		ds.setOdLanguages(findValue(doc, "od_languages", String.class));
175
		ds.setOdContentTypes(findValue(doc, "od_contenttypes", String.class));
176
		ds.setCollectedFrom(findValue(doc, "collectedfrom", String.class));
177
		ds.setInferred(findValue(doc, "inferred", Boolean.class));
178
		ds.setDeletedByInference(findValue(doc, "deletedbyinference", Boolean.class));
179
		ds.setTrust(findValue(doc, "trust", Double.class));
180
		ds.setInferenceProvenance(findValue(doc, "inferenceprovenance", String.class));
181
		ds.setDateOfValidation(findValue(doc, "dateofvalidation", Date.class));
182
		ds.setRegisteredBy(findValue(doc, "registeredby", String.class));
183
		ds.setDatasourceClass(findValue(doc, "datasourceclass", String.class));
184
		ds.setProvenanceActionClass(findValue(doc, "provenanceactionclass", String.class));
185
		ds.setDateOfCollection(findValue(doc, "dateofcollection", Date.class));
186
		ds.setTypology(findValue(doc, "typology", String.class));
187
		ds.setActivationId(findValue(doc, "activationid", String.class));
188
		ds.setMergehomonyms(findValue(doc, "mergehomonyms", Boolean.class));
189
		ds.setDescription(findValue(doc, "description", String.class));
190
		ds.setReleaseStartDate(findValue(doc, "releasestartdate", Date.class));
191
		ds.setReleaseEndDate(findValue(doc, "releaseenddate", Date.class));
192
		ds.setMissionStatementUrl(findValue(doc, "missionstatementurl", String.class));
193
		ds.setDataProvider(findValue(doc, "dataprovider", Boolean.class));
194
		ds.setServiceProvider(findValue(doc, "serviceprovider", Boolean.class));
195
		ds.setDatabaseAccessType(findValue(doc, "databaseaccesstype", String.class));
196
		ds.setDataUploadType(findValue(doc, "datauploadtype", String.class));
197
		ds.setDatabaseAccessRestriction(findValue(doc, "databaseaccessrestriction", String.class));
198
		ds.setDataUploadRestriction(findValue(doc, "datauploadrestriction", String.class));
199
		ds.setVersioning(findValue(doc, "versioning", Boolean.class));
200
		ds.setCitationGuidelineUrl(findValue(doc, "citationguidelineurl", String.class));
201
		ds.setQualityManagementKind(findValue(doc, "qualitymanagementkind", String.class));
202
		ds.setPidSystems(findValue(doc, "pidsystems", String.class));
203
		ds.setCertificates(findValue(doc, "certificates", String.class));
204
		ds.setAggregator(findValue(doc, "aggregator", String.class));
205
		ds.setIssn(findValue(doc, "issn", String.class));
206
		ds.setEissn(findValue(doc, "eissn", String.class));
207
		ds.setLissn(findValue(doc, "lissn", String.class));
208
		ds.setInterfaces(findValue(doc, "accessinfopackage", List.class));
209

  
210
		return ds;
211
	}
212

  
213
	@SuppressWarnings("unchecked")
214
	private static <T> T findValue(final Document doc, final String elem, final Class<T> clazz) throws DatasourceManagerServiceException {
215

  
216
		if (clazz == List.class) {
217
			if (elem.equalsIgnoreCase("accessinfopackage")) {
218
				final List<IfaceDesc> ifaces = Lists.newArrayList();
219
				for (Object o : doc.selectNodes("//FIELD[@name='accessinfopackage']/ITEM")) {
220
					final IfaceDesc ifaceDesc = generateIfaceDesc(((Node) o).getText());
221
					if (ifaceDesc != null) {
222
						ifaces.add(ifaceDesc);
223
					}
224
				}
225
				return (T) ifaces;
226
			} else {
227
				throw new DatasourceManagerServiceException("Invalid List element " + elem);
228
			}
229
		} else {
230
			final String val = doc.valueOf("//FIELD[@name='" + elem + "' and not(@isNull='true')]");
231

  
232
			if (clazz == String.class) {
233
				return (T) val;
234
			} else if (val == null || val.isEmpty()) {
235
				return null;
236
			} else if (clazz == Boolean.class) {
237
				return (T) new Boolean(val);
238
			} else if (clazz == Double.class) {
239
				return (T) new Double(val);
240
			} else if (clazz == Date.class) {
241
				try {
242
					return (T) new SimpleDateFormat("yyyy-MM-dd").parse(val);
243
				} catch (ParseException e) {
244
					throw new DatasourceManagerServiceException("Invalid date: " + val);
245
				}
246
			} else {
247
				throw new DatasourceManagerServiceException("Invalid type " + clazz + " for element " + elem);
248
			}
249
		}
250

  
251
	}
252

  
253
	public static Map<String, Object> asMapOfSqlValues(final DatasourceDesc ds) {
254
		final Map<String, Object> map = Maps.newHashMap();
255
		map.put("id", asSqlValue(ds.getId()));
256
		map.put("officialName", asSqlValue(ds.getOfficialName()));
257
		map.put("englishName", asSqlValue(ds.getEnglishName()));
258
		map.put("websiteUrl", asSqlValue(ds.getWebsiteUrl()));
259
		map.put("logoUrl", asSqlValue(ds.getLogoUrl()));
260
		map.put("countryCode", asSqlValue(ds.getCountryCode()));
261
		map.put("countryName", asSqlValue(ds.getCountryName()));
262
		map.put("organization", asSqlValue(ds.getOrganization()));
263
		map.put("contactEmail", asSqlValue(ds.getContactEmail()));
264
		map.put("latitude", asSqlValue(ds.getLatitude()));
265
		map.put("longitude", asSqlValue(ds.getLongitude()));
266
		map.put("timezone", asSqlValue(ds.getTimezone()));
267
		map.put("namespacePrefix", asSqlValue(ds.getNamespacePrefix()));
268
		map.put("odNumberOfItems", asSqlValue(ds.getOdNumberOfItems()));
269
		map.put("odNumberOfItemsDate", asSqlValue(ds.getOdNumberOfItemsDate()));
270
		map.put("odPolicies", asSqlValue(ds.getOdPolicies()));
271
		map.put("odLanguages", asSqlValue(ds.getOdLanguages()));
272
		map.put("odContentTypes", asSqlValue(ds.getOdContentTypes()));
273
		map.put("collectedFrom", asSqlValue(ds.getCollectedFrom()));
274
		map.put("inferred", asSqlValue(ds.isInferred()));
275
		map.put("deletedByInference", asSqlValue(ds.isDeletedByInference()));
276
		map.put("trust", asSqlValue(ds.getTrust()));
277
		map.put("inferenceProvenance", asSqlValue(ds.getInferenceProvenance()));
278
		map.put("dateOfValidation", asSqlValue(ds.getDateOfValidation()));
279
		map.put("registeredBy", asSqlValue(ds.getRegisteredBy()));
280
		map.put("datasourceClass", asSqlValue(ds.getDatasourceClass()));
281
		map.put("provenanceActionClass", asSqlValue(ds.getProvenanceActionClass()));
282
		map.put("dateOfCollection", asSqlValue(ds.getDateOfCollection()));
283
		map.put("typology", asSqlValue(ds.getTypology()));
284
		map.put("activationId", asSqlValue(ds.getActivationId()));
285
		map.put("mergehomonyms", asSqlValue(ds.isMergehomonyms()));
286
		map.put("description", asSqlValue(ds.getDescription()));
287
		map.put("releaseStartDate", asSqlValue(ds.getReleaseStartDate()));
288
		map.put("releaseEndDate", asSqlValue(ds.getReleaseEndDate()));
289
		map.put("missionStatementUrl", asSqlValue(ds.getMissionStatementUrl()));
290
		map.put("dataProvider", asSqlValue(ds.isDataProvider()));
291
		map.put("serviceProvider", asSqlValue(ds.isServiceProvider()));
292
		map.put("databaseAccessType", asSqlValue(ds.getDatabaseAccessType()));
293
		map.put("dataUploadType", asSqlValue(ds.getDataUploadType()));
294
		map.put("databaseAccessRestriction", asSqlValue(ds.getDatabaseAccessRestriction()));
295
		map.put("dataUploadRestriction", asSqlValue(ds.getDataUploadRestriction()));
296
		map.put("versioning", asSqlValue(ds.isVersioning()));
297
		map.put("citationGuidelineUrl", asSqlValue(ds.getCitationGuidelineUrl()));
298
		map.put("qualityManagementKind", asSqlValue(ds.getQualityManagementKind()));
299
		map.put("pidSystems", asSqlValue(ds.getPidSystems()));
300
		map.put("certificates", asSqlValue(ds.getCertificates()));
301
		map.put("aggregator", asSqlValue(ds.getAggregator()));
302
		map.put("issn", asSqlValue(ds.getIssn()));
303
		map.put("eissn", asSqlValue(ds.getEissn()));
304
		map.put("lissn", asSqlValue(ds.getLissn()));
305

  
306
		final ArrayList<Map<String, Object>> ifaces = new ArrayList<Map<String, Object>>();
307

  
308
		if (ds.getInterfaces() != null) {
309

  
310
			for (IfaceDesc iface : ds.getInterfaces()) {
311
				if (iface.getId() != null && !iface.getId().trim().isEmpty()) {
312
					final Map<String, Object> mapIface = Maps.newHashMap();
313

  
314
					mapIface.put("id", asSqlValue(iface.getId()));
315
					mapIface.put("typology", asSqlValue(iface.getTypology()));
316
					mapIface.put("compliance", asSqlValue(iface.getCompliance()));
317
					mapIface.put("contentDescription", asSqlValue(iface.getContentDescription()));
318
					mapIface.put("accessProtocol", asSqlValue(iface.getAccessProtocol()));
319
					mapIface.put("baseUrl", asSqlValue(iface.getBaseUrl()));
320

  
321
					final Map<String, String> extraFields = Maps.newHashMap();
322
					if (iface.getExtraFields() != null) {
323
						for (String k : iface.getExtraFields().keySet()) {
324
							if (k != null && !k.trim().isEmpty()) {
325
								extraFields.put(asSqlValue(k), asSqlValue(iface.getExtraFields().get(k)));
326
							}
327
						}
328
					}
329
					mapIface.put("extraFields", extraFields);
330

  
331
					final Map<String, String> accessParams = Maps.newHashMap();
332
					if (iface.getAccessParams() != null) {
333
						for (String k : iface.getAccessParams().keySet()) {
334
							if (k != null && !k.trim().isEmpty()) {
335
								accessParams.put(asSqlValue(k), asSqlValue(iface.getAccessParams().get(k)));
336
							}
337
						}
338
					}
339
					mapIface.put("accessParams", accessParams);
340

  
341
					ifaces.add(mapIface);
342
				}
343
			}
344

  
345
			map.put("interfaces", ifaces);
346
		}
347

  
348
		return map;
349
	}
350

  
351
	public static Map<String, Object> asMapOfSqlValues(final Map<String, String> input) {
352
		final Map<String, Object> map = Maps.newHashMap();
353
		for (Map.Entry<String, String> e : input.entrySet()) {
354
			map.put(asSqlValue(e.getKey()), asSqlValue(e.getValue()));
355
		}
356
		return map;
357
	}
358

  
359
}
modules/dnet-openaireplus-datasource-manager-service/trunk/src/main/java/eu/dnetlib/enabling/datasources/DatasourceManagerClients.java
1
package eu.dnetlib.enabling.datasources;
2

  
3
import java.io.StringReader;
4
import java.text.ParseException;
5
import java.util.Date;
6
import java.util.List;
7
import java.util.Map;
8
import java.util.regex.Pattern;
9

  
10
import javax.xml.ws.wsaddressing.W3CEndpointReference;
11

  
12
import org.antlr.stringtemplate.StringTemplate;
13
import org.apache.commons.io.IOUtils;
14
import org.apache.commons.logging.Log;
15
import org.apache.commons.logging.LogFactory;
16
import org.dom4j.Document;
17
import org.dom4j.io.SAXReader;
18
import org.quartz.CronExpression;
19
import org.springframework.beans.factory.annotation.Required;
20
import org.springframework.core.io.ClassPathResource;
21
import org.springframework.core.io.Resource;
22

  
23
import com.google.common.collect.Lists;
24
import com.google.common.collect.Maps;
25

  
26
import eu.dnetlib.enabling.database.rmi.DatabaseException;
27
import eu.dnetlib.enabling.database.rmi.DatabaseService;
28
import eu.dnetlib.enabling.datasources.rmi.DatasourceDesc;
29
import eu.dnetlib.enabling.datasources.rmi.DatasourceManagerServiceException;
30
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException;
31
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
32
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
33
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
34
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
35
import eu.dnetlib.enabling.resultset.client.ResultSetClientFactory;
36
import eu.dnetlib.miscutils.functional.UnaryFunction;
37
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
38

  
39
public class DatasourceManagerClients {
40

  
41
	private static final Resource xslt = new ClassPathResource("/eu/dnetlib/enabling/datasources/repo_2_is.xslt");
42
	private static final String REPOSITORY_SERVICE_RESOURCE_TYPE = "RepositoryServiceResourceType";
43
	private static final Log log = LogFactory.getLog(DatasourceManagerClients.class);
44
	private static final String TMPLS_BASEDIR = "/eu/dnetlib/enabling/datasources/";
45
	private String db;
46
	private UniqueServiceLocator serviceLocator;
47
	private ResultSetClientFactory resultSetClientFactory;
48

  
49
	public String findDatasourceId(final String profileId) throws DatasourceManagerServiceException {
50
		try {
51
			return serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(
52
					"/*[.//RESOURCE_IDENTIFIER/@value='" + profileId + "']//EXTRA_FIELDS/FIELD[./key='OpenAireDataSourceId']/value/text()");
53
		} catch (final Exception e) {
54
			log.error("Error finding dsId of profile " + profileId, e);
55
			throw new DatasourceManagerServiceException("Error finding dsId of profile " + profileId, e);
56
		}
57
	}
58

  
59
	public String getDatasourceProfile(final String dsId) throws DatasourceManagerServiceException {
60
		try {
61
			return serviceLocator.getService(ISLookUpService.class)
62
					.getResourceProfileByQuery(
63
							"collection('/db/DRIVER/RepositoryServiceResources/RepositoryServiceResourceType')/*[.//EXTRA_FIELDS/FIELD[./key='OpenAireDataSourceId']/value/text() = '"
64
									+ dsId + "']");
65
		} catch (final Exception e) {
66
			return null;
67
		}
68
	}
69

  
70
	public boolean deleteProfile(final String dsId) throws DatasourceManagerServiceException {
71
		try {
72
			final SAXReader reader = new SAXReader();
73

  
74
			final String profile = getDatasourceProfile(dsId);
75

  
76
			if (profile != null) {
77
				final Document docOld = reader.read(new StringReader(profile));
78
				final String profId = docOld.valueOf("//RESOURCE_IDENTIFIER/@value");
79
				serviceLocator.getService(ISRegistryService.class).deleteProfile(profId);
80
			}
81
			return true;
82
		} catch (final Exception e) {
83
			log.error("Error deleting profile", e);
84
			throw new DatasourceManagerServiceException("Error deleting profile", e);
85
		}
86
	}
87

  
88
	public boolean regenerateProfile(final String dsId) throws DatasourceManagerServiceException {
89

  
90
		try {
91
			final SAXReader reader = new SAXReader();
92

  
93
			final List<String> list = getTransformedDatasourcesByCondition("ds.id= '" + dsId + "'", new ApplyXslt(xslt));
94

  
95
			if (list.size() != 1) { throw new DatasourceManagerServiceException("Illegal number of datasource with id " + dsId + ", size: " + list.size()); }
96

  
97
			final Document doc = reader.read(new StringReader(list.get(0)));
98

  
99
			final ISRegistryService registry = serviceLocator.getService(ISRegistryService.class);
100

  
101
			final String profile = getDatasourceProfile(dsId);
102

  
103
			if (profile != null) {
104
				final Document docOld = reader.read(new StringReader(profile));
105
				final String profId = docOld.valueOf("//RESOURCE_IDENTIFIER/@value");
106

  
107
				doc.selectSingleNode("//RESOURCE_IDENTIFIER/@value").setText(profId);
108

  
109
				registry.updateProfile(profId, doc.asXML(), REPOSITORY_SERVICE_RESOURCE_TYPE);
110
				log.info("Profile " + profId + " UPDATED for ds " + dsId);
111
			} else {
112
				final String profId = registry.registerProfile(doc.asXML());
113
				log.info("Valid Profile " + profId + " REGISTERED for ds " + dsId);
114
			}
115
			return true;
116
		} catch (final Exception e) {
117
			log.error("Error saving profile, id: " + dsId, e);
118
			throw new DatasourceManagerServiceException("Error regenerating profile", e);
119
		}
120
	}
121

  
122
	public Iterable<String> searchSQL(final String sql) throws DatabaseException {
123
		final W3CEndpointReference epr = serviceLocator.getService(DatabaseService.class).searchSQL(getDb(), sql);
124
		return resultSetClientFactory.getClient(epr);
125
	}
126

  
127
	public boolean updateSQL(final String dsId, final String sql, final boolean delete, final boolean updateprofile) throws DatasourceManagerServiceException {
128
		log.debug("Executing query SQL: " + sql);
129

  
130
		try {
131
			serviceLocator.getService(DatabaseService.class).updateSQL(getDb(), sql);
132
		} catch (final DatabaseException e) {
133
			throw new DatasourceManagerServiceException(e);
134
		}
135

  
136
		if (updateprofile) {
137
			if (delete) {
138
				return deleteProfile(dsId);
139
			} else {
140
				return regenerateProfile(dsId);
141
			}
142
		}
143
		return false;
144
	}
145

  
146
	public boolean updateSQL(final String dsId, final String sqlTemplate, final Map<String, Object> params, final boolean delete, final boolean updateProfile)
147
			throws DatasourceManagerServiceException {
148

  
149
		verifyParams(params.values());
150
		verifyParams(params.keySet().toArray());
151

  
152
		try {
153
			final Resource resource = new ClassPathResource(TMPLS_BASEDIR + sqlTemplate);
154
			final StringTemplate st = new StringTemplate(IOUtils.toString(resource.getInputStream()));
155
			st.setAttributes(params);
156
			return updateSQL(dsId, st.toString(), delete, updateProfile);
157
		} catch (final Exception e) {
158
			log.error("Error in updateSQL", e);
159
			throw new DatasourceManagerServiceException("Error in updateSQL", e);
160
		}
161
	}
162

  
163
	public List<DatasourceDesc> getDatasourcesByCondition(final String condition) throws DatasourceManagerServiceException {
164
		final SAXReader reader = new SAXReader();
165
		final List<DatasourceDesc> list = Lists.newArrayList();
166
		try {
167
			for (final String s : getXmlDatasourcesByCondition(condition)) {
168
				final Document doc = reader.read(new StringReader(s));
169
				list.add(DatasourceFunctions.xmlToDatasourceDesc(doc));
170
			}
171
		} catch (final Exception e) {
172
			log.error("Error obtaining datasources from db", e);
173
			throw new DatasourceManagerServiceException("Error obtaining datasources from db", e);
174
		}
175
		return list;
176

  
177
	}
178

  
179
	private void verifyParams(final Object... params) throws DatasourceManagerServiceException {
180

  
181
		final Pattern pattern = Pattern.compile("\\n");
182

  
183
		for (final Object p : params) {
184
			log.debug("TESTING SQL PARAM:" + p);
185
			if ((p == null) || p.toString().isEmpty()) {
186
				log.error("Parameter null or empty");
187
				throw new DatasourceManagerServiceException("Parameter null or empty");
188
			} else if (pattern.matcher(p.toString()).matches()) {
189
				log.error("Parameter [" + p + "] contains an invalid character");
190
				throw new DatasourceManagerServiceException("Parameter [" + p + "] contains an invalid character");
191
			} else {
192
				log.debug("TEST OK");
193
			}
194
		}
195
	}
196

  
197
	private List<String> getTransformedDatasourcesByCondition(final String condition, final UnaryFunction<String, String> function)
198
			throws DatasourceManagerServiceException {
199
		final List<String> list = Lists.newArrayList();
200
		try {
201
			for (final String s : getXmlDatasourcesByCondition(condition)) {
202
				list.add(function.evaluate(s));
203
			}
204
		} catch (final Exception e) {
205
			log.error("Error obtaining datasources from db", e);
206
			throw new DatasourceManagerServiceException("Error obtaining datasources from db", e);
207
		}
208
		return list;
209
	}
210

  
211
	private Iterable<String> getXmlDatasourcesByCondition(final String condition) throws DatasourceManagerServiceException {
212
		try {
213
			final Map<String, Object> params = Maps.newHashMap();
214

  
215
			if ((condition != null) && !condition.trim().isEmpty()) {
216
				params.put("condition", condition);
217
			}
218
			return searchSQL("getDatasources.sql.st", params);
219
		} catch (final Exception e) {
220
			log.error("Error obtaining datasources from db", e);
221
			throw new DatasourceManagerServiceException("Error obtaining datasources from db", e);
222
		}
223
	}
224

  
225
	public Iterable<String> searchSQL(final String sqlTemplate, final Map<String, Object> params) throws DatasourceManagerServiceException {
226
		try {
227
			final Resource resource = new ClassPathResource(TMPLS_BASEDIR + sqlTemplate);
228
			final StringTemplate st = new StringTemplate(IOUtils.toString(resource.getInputStream()));
229
			if (params != null) {
230
				st.setAttributes(params);
231
			}
232

  
233
			final String sql = st.toString();
234

  
235
			log.debug("Executing SQL: " + sql);
236

  
237
			return searchSQL(sql);
238
		} catch (final Exception e) {
239
			log.error("Error executing sql", e);
240

  
241
			throw new DatasourceManagerServiceException("Error obtaining datasources from db", e);
242
		}
243
	}
244

  
245
	public boolean isDefinedParam(final String ifaceId, final String field) throws DatasourceManagerServiceException {
246
		final Map<String, Object> params = Maps.newHashMap();
247
		params.put("ifaceId", DatasourceFunctions.asSqlValue(ifaceId));
248
		params.put("field", DatasourceFunctions.asSqlValue(field));
249

  
250
		final List<String> list = Lists.newArrayList(searchSQL("searchApiCollectionParam.sql.st", params));
251

  
252
		return !list.isEmpty();
253
	}
254

  
255
	public Date findNextScheduledExecution(final String dsId, final String ifaceId) throws DatasourceManagerServiceException {
256
		final String xquery = "/*[.//DATAPROVIDER/@interface='" + ifaceId + "' and .//SCHEDULING/@enabled='true']//CRON/text()";
257
		try {
258
			final String cronExpression = serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(xquery);
259
			final CronExpression cron = new CronExpression(cronExpression);
260
			return cron.getNextValidTimeAfter(new Date());
261
		} catch (final ISLookUpDocumentNotFoundException e) {
262
			// When the value is not found a null value must be returned
263
			return null;
264
		} catch (final ISLookUpException e) {
265
			log.error("Error in xquery: " + xquery, e);
266
			throw new DatasourceManagerServiceException("Error in xquery: " + xquery, e);
267
		} catch (final ParseException e) {
268
			log.error("Error parsing cron expression", e);
269
			throw new DatasourceManagerServiceException("Error parsing cron expression", e);
270
		}
271
	}
272

  
273
	public String getDb() {
274
		return db;
275
	}
276

  
277
	@Required
278
	public void setDb(final String db) {
279
		this.db = db;
280
	}
281

  
282
	public ResultSetClientFactory getResultSetClientFactory() {
283
		return resultSetClientFactory;
284
	}
285

  
286
	@Required
287
	public void setResultSetClientFactory(final ResultSetClientFactory resultSetClientFactory) {
288
		this.resultSetClientFactory = resultSetClientFactory;
289
	}
290

  
291
	public UniqueServiceLocator getServiceLocator() {
292
		return serviceLocator;
293
	}
294

  
295
	@Required
296
	public void setServiceLocator(final UniqueServiceLocator serviceLocator) {
297
		this.serviceLocator = serviceLocator;
298
	}
299

  
300
}
modules/dnet-openaireplus-datasource-manager-service/trunk/src/main/java/eu/dnetlib/enabling/datasources/DatasourceManagerServiceImpl.java
1
package eu.dnetlib.enabling.datasources;
2

  
3
import java.io.StringReader;
4
import java.util.Collections;
5
import java.util.Date;
6
import java.util.HashMap;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.Map.Entry;
10

  
11
import org.apache.commons.lang.StringEscapeUtils;
12
import org.apache.commons.lang.StringUtils;
13
import org.apache.commons.lang.math.NumberUtils;
14
import org.apache.commons.logging.Log;
15
import org.apache.commons.logging.LogFactory;
16
import org.dom4j.Document;
17
import org.dom4j.Node;
18
import org.dom4j.io.SAXReader;
19
import org.springframework.beans.factory.annotation.Required;
20

  
21
import com.google.common.base.Function;
22
import com.google.common.collect.Iterables;
23
import com.google.common.collect.Lists;
24
import com.google.common.collect.Maps;
25

  
26
import eu.dnetlib.enabling.datasources.rmi.BrowsableField;
27
import eu.dnetlib.enabling.datasources.rmi.BrowseTerm;
28
import eu.dnetlib.enabling.datasources.rmi.DatasourceConstants;
29
import eu.dnetlib.enabling.datasources.rmi.DatasourceDesc;
30
import eu.dnetlib.enabling.datasources.rmi.DatasourceManagerService;
31
import eu.dnetlib.enabling.datasources.rmi.DatasourceManagerServiceException;
32
import eu.dnetlib.enabling.datasources.rmi.IfaceDesc;
33
import eu.dnetlib.enabling.datasources.rmi.RepositoryMapEntry;
34
import eu.dnetlib.enabling.datasources.rmi.SearchInterfacesEntry;
35
import eu.dnetlib.enabling.datasources.rmi.SimpleDatasourceDesc;
36
import eu.dnetlib.enabling.tools.AbstractBaseService;
37

  
38
public class DatasourceManagerServiceImpl extends AbstractBaseService implements DatasourceManagerService {
39

  
40
	private DatasourceManagerClients datasourceManagerClients;
41

  
42
	private List<DbBrowsableField> browsableFields;
43

  
44
	private static final Log log = LogFactory.getLog(DatasourceManagerServiceImpl.class);
45

  
46
	@Override
47
	public boolean addDatasource(final DatasourceDesc ds) throws DatasourceManagerServiceException {
48
		if (StringUtils.isBlank(ds.getAggregator())) {
49
			ds.setAggregator("OPENAIRE");
50
		}
51

  
52
		final Map<String, Object> params = DatasourceFunctions.asMapOfSqlValues(ds);
53

  
54
		if ((ds.getOrganization() != null) && !ds.getOrganization().trim().isEmpty()) {
55
			params.put("hasOrganization", 1);
56
		}
57
		return datasourceManagerClients.updateSQL(ds.getId(), "addDatasource.sql.st", params, false, true);
58
	}
59

  
60
	@Override
61
	public boolean deleteDatasource(final String dsId) throws DatasourceManagerServiceException {
62
		final Map<String, Object> params = Maps.newHashMap();
63

  
64
		params.put("dsId", DatasourceFunctions.asSqlValue(dsId));
65

  
66
		return datasourceManagerClients.updateSQL(dsId, "deleteDatasource.sql.st", params, true, true);
67
	}
68

  
69
	@Override
70
	public DatasourceDesc getDatasource(final String dsId) throws DatasourceManagerServiceException {
71
		final List<DatasourceDesc> list = datasourceManagerClients.getDatasourcesByCondition("ds.id = " + DatasourceFunctions.asSqlValue(dsId));
72
		if (list.size() != 1) { throw new DatasourceManagerServiceException("Datasource not found, id=" + dsId); }
73

  
74
		return list.get(0);
75
	}
76

  
77
	@Override
78
	public List<DatasourceDesc> listAllDatasources() throws DatasourceManagerServiceException {
79
		return datasourceManagerClients.getDatasourcesByCondition(null);
80
	}
81

  
82
	@Override
83
	public List<DatasourceDesc> listDatasourcesUsingFilter(final String compliance,
84
			final String contentDescription,
85
			final String iisProcessingWorkflow,
86
			final String collectedFrom) throws DatasourceManagerServiceException {
87

  
88
		String cond = "";
89

  
90
		if ((compliance != null) && !compliance.isEmpty()) {
91
			if (!cond.isEmpty()) {
92
				cond += " and ";
93
			}
94
			cond += "ag.compatibilityclass=" + DatasourceFunctions.asSqlValue(compliance);
95
		}
96

  
97
		if ((contentDescription != null) && !contentDescription.isEmpty()) {
98
			if (!cond.isEmpty()) {
99
				cond += " and ";
100
			}
101
			cond += "ag.contentdescriptionclass=" + DatasourceFunctions.asSqlValue(contentDescription);
102
		}
103

  
104
		if ((iisProcessingWorkflow != null) && !iisProcessingWorkflow.isEmpty()) {
105
			if (!cond.isEmpty()) {
106
				cond += " and ";
107
			}
108
			cond += "ag.accessinfopackage LIKE '%###" + DatasourceParams.iis_processing_workflow + "###" + iisProcessingWorkflow + "###%'";
109
		}
110

  
111
		if ((collectedFrom != null) && !collectedFrom.isEmpty()) {
112
			if (!cond.isEmpty()) {
113
				cond += " and ";
114
			}
115
			cond += "ds.collectedfrom=" + DatasourceFunctions.asSqlValue(collectedFrom);
116
		}
117

  
118
		return datasourceManagerClients.getDatasourcesByCondition(cond);
119
	}
120

  
121
	@Override
122
	public boolean updateActivationStatus(final String dsId, final String ifaceId, final boolean active) throws DatasourceManagerServiceException {
123
		final Map<String, Object> params = Maps.newHashMap();
124

  
125
		params.put("active", DatasourceFunctions.asSqlValue(active));
126
		params.put("ifaceId", DatasourceFunctions.asSqlValue(ifaceId));
127
		params.put("dsId", DatasourceFunctions.asSqlValue(dsId));
128

  
129
		return datasourceManagerClients.updateSQL(dsId, "updateActivationStatus.sql.st", params, false, true);
130
	}
131

  
132
	@Override
133
	public boolean updateLevelOfCompliance(final String dsId, final String ifaceId, final String level) throws DatasourceManagerServiceException {
134
		final Map<String, Object> params = Maps.newHashMap();
135

  
136
		params.put("level", DatasourceFunctions.asSqlValue(level));
137
		params.put("ifaceId", DatasourceFunctions.asSqlValue(ifaceId));
138
		params.put("dsId", DatasourceFunctions.asSqlValue(dsId));
139

  
140
		return datasourceManagerClients.updateSQL(dsId, "updateLevelOfCompliance.sql.st", params, false, true);
141
	}
142

  
143
	@Override
144
	public boolean updateBaseUrl(final String dsId, final String ifaceId, final String baseUrl) throws DatasourceManagerServiceException {
145
		return updateAccessParam(dsId, ifaceId, DatasourceParams.baseUrl.toString(), baseUrl, true);
146
	}
147

  
148
	@Override
149
	public boolean updateContentDescription(final String dsId, final String ifaceId, final String desc) throws DatasourceManagerServiceException {
150
		final Map<String, Object> params = Maps.newHashMap();
151
		params.put("desc", DatasourceFunctions.asSqlValue(desc));
152
		params.put("ifaceId", DatasourceFunctions.asSqlValue(ifaceId));
153
		params.put("dsId", DatasourceFunctions.asSqlValue(dsId));
154

  
155
		return datasourceManagerClients.updateSQL(dsId, "updateContentDescription.sql.st", params, false, true);
156
	}
157

  
158
	@Override
159
	public boolean setIisProcessingWorkflow(final String dsId, final String ifaceId, final String wf) throws DatasourceManagerServiceException {
160
		return updateExtraField(dsId, ifaceId, DatasourceParams.iis_processing_workflow.toString(), wf, false);
161
	}
162

  
163
	@Override
164
	public boolean addInterface(final String dsId, final IfaceDesc iface) throws DatasourceManagerServiceException {
165
		final Map<String, Object> params = Maps.newHashMap();
166

  
167
		params.put("datasource", DatasourceFunctions.asSqlValue(dsId));
168
		params.put("id", DatasourceFunctions.asSqlValue(iface.getId()));
169
		params.put("typology", DatasourceFunctions.asSqlValue(iface.getTypology()));
170
		params.put("protocol", DatasourceFunctions.asSqlValue(iface.getAccessProtocol()));
171
		params.put("baseUrl", DatasourceFunctions.asSqlValue(iface.getBaseUrl()));
172
		params.put("description", DatasourceFunctions.asSqlValue(iface.getContentDescription()));
173
		params.put("compliance", DatasourceFunctions.asSqlValue(iface.getCompliance()));
174

  
175
		final Map<String, String> accessParams = new HashMap<String, String>();
176
		if (iface.getAccessParams() != null) {
177
			for (final Entry<String, String> e : iface.getAccessParams().entrySet()) {
178
				accessParams.put(DatasourceFunctions.asSqlValue(e.getKey()), DatasourceFunctions.asSqlValue(e.getValue()));
179
			}
180
		}
181
		params.put("accessParams", accessParams);
182

  
183
		final Map<String, String> extraFields = new HashMap<String, String>();
184
		if (iface.getExtraFields() != null) {
185
			for (final Entry<String, String> e : iface.getExtraFields().entrySet()) {
186
				extraFields.put(DatasourceFunctions.asSqlValue(e.getKey()), DatasourceFunctions.asSqlValue(e.getValue()));
187
			}
188
		}
189
		params.put("extraFields", extraFields);
190

  
191
		return datasourceManagerClients.updateSQL(dsId, "insertInterface.sql.st", params, false, true);
192
	}
193

  
194
	@Override
195
	public boolean deleteInterface(final String dsId, final String ifcId) throws DatasourceManagerServiceException {
196
		final Map<String, Object> params = Maps.newHashMap();
197

  
198
		params.put("datasource", DatasourceFunctions.asSqlValue(dsId));
199
		params.put("id", DatasourceFunctions.asSqlValue(ifcId));
200

  
201
		return datasourceManagerClients.updateSQL(dsId, "deleteInterface.sql.st", params, false, true);
202
	}
203

  
204
	@Override
205
	public boolean updateExtraField(final String dsId, final String ifaceId, final String field, final String value, final boolean preserveOriginal)
206
			throws DatasourceManagerServiceException {
207
		return updateApiCollectionsRow(dsId, ifaceId, field, value, preserveOriginal, false);
208
	}
209

  
210
	@Override
211
	public boolean updateAccessParam(final String dsId, final String ifaceId, final String field, final String value, final boolean preserveOriginal)
212
			throws DatasourceManagerServiceException {
213
		return updateApiCollectionsRow(dsId, ifaceId, field, value, preserveOriginal, true);
214
	}
215

  
216
	private boolean updateApiCollectionsRow(final String dsId,
217
			final String ifaceId,
218
			final String field,
219
			final String value,
220
			final boolean preserveOriginal,
221
			final boolean accessParam) throws DatasourceManagerServiceException {
222

  
223
		final String openaireDsId = datasourceManagerClients.findDatasourceId(dsId);
224

  
225
		final Map<String, Object> params = Maps.newHashMap();
226

  
227
		params.put("dsId", DatasourceFunctions.asSqlValue(openaireDsId));
228
		params.put("ifaceId", DatasourceFunctions.asSqlValue(ifaceId));
229
		params.put("field", DatasourceFunctions.asSqlValue(field));
230
		params.put("accessParam", accessParam);
231

  
232
		if (value != null) {
233
			params.put("value", DatasourceFunctions.asSqlValue(value));
234
		}
235

  
236
		if (datasourceManagerClients.isDefinedParam(ifaceId, field)) {
237
			params.put("update", 1);
238
		}
239
		if (preserveOriginal) {
240
			params.put("preserveOriginal", 1);
241
		}
242

  
243
		return datasourceManagerClients.updateSQL(openaireDsId, "updateApiCollectionsRow.sql.st", params, false, true);
244
	}
245

  
246
	@Override
247
	public boolean deleteAccessParamOrExtraField(final String dsId, final String ifaceId, final String field) throws DatasourceManagerServiceException {
248
		final Map<String, Object> params = Maps.newHashMap();
249

  
250
		params.put("dsId", DatasourceFunctions.asSqlValue(dsId));
251
		params.put("ifaceId", DatasourceFunctions.asSqlValue(ifaceId));
252
		params.put("field", DatasourceFunctions.asSqlValue(field));
253

  
254
		return datasourceManagerClients.updateSQL(dsId, "deleteApiCollectionsRow.sql.st", params, false, true);
255
	}
256

  
257
	@Override
258
	public boolean updateSQL(final String dsId, final String sql, final boolean delete) throws DatasourceManagerServiceException {
259
		return datasourceManagerClients.updateSQL(dsId, sql, delete, true);
260
	}
261

  
262
	@Override
263
	public Date findNextScheduledExecution(final String dsId, final String ifaceId)
264
			throws DatasourceManagerServiceException {
265
		return datasourceManagerClients.findNextScheduledExecution(dsId, ifaceId);
266
	}
267

  
268
	@Override
269
	public boolean bulkUpdateApiExtraFields(final String repoId, final String ifaceId, final Map<String, String> fields)
270
			throws DatasourceManagerServiceException {
271
		return performUpdate(repoId, ifaceId, fields, false);
272
	}
273

  
274
	@Override
275
	public boolean bulkUpdateApiAccessParams(final String repoId, final String ifaceId, final Map<String, String> params)
276
			throws DatasourceManagerServiceException {
277
		return performUpdate(repoId, ifaceId, params, true);
278
	}
279

  
280
	private boolean performUpdate(final String repoId, final String ifaceId, final Map<String, String> params, final boolean accessParam)
281
			throws DatasourceManagerServiceException {
282
		final String dsId = datasourceManagerClients.findDatasourceId(repoId);
283

  
284
		if (!accessParam) {
285
			deleteOldExtraFields(dsId, ifaceId);
286
		}
287

  
288
		for (final Map.Entry<String, String> e : params.entrySet()) {
289
			performSingleUpdate(dsId, ifaceId, e.getKey(), e.getValue(), accessParam);
290
		}
291
		datasourceManagerClients.regenerateProfile(dsId);
292
		return true;
293
	}
294

  
295
	private boolean deleteOldExtraFields(final String dsId, final String ifaceId) throws DatasourceManagerServiceException {
296
		final Map<String, Object> params = Maps.newHashMap();
297

  
298
		params.put("dsId", DatasourceFunctions.asSqlValue(dsId));
299
		params.put("ifaceId", DatasourceFunctions.asSqlValue(ifaceId));
300

  
301
		return datasourceManagerClients.updateSQL(dsId, "deleteOldExtraFields.sql.st", params, false, false);
302
	}
303

  
304
	private boolean performSingleUpdate(final String dsId, final String ifaceId, final String field, final String value, final boolean accessParam)
305
			throws DatasourceManagerServiceException {
306
		final Map<String, Object> params = Maps.newHashMap();
307

  
308
		params.put("dsId", DatasourceFunctions.asSqlValue(dsId));
309
		params.put("ifaceId", DatasourceFunctions.asSqlValue(ifaceId));
310
		params.put("field", DatasourceFunctions.asSqlValue(field));
311
		params.put("accessParam", accessParam);
312

  
313
		if (value != null) {
314
			params.put("value", DatasourceFunctions.asSqlValue(value));
315
		}
316

  
317
		if (accessParam && datasourceManagerClients.isDefinedParam(ifaceId, field)) {
318
			params.put("update", 1);
319
			params.put("preserveOriginal", 1);
320
		}
321

  
322
		return datasourceManagerClients.updateSQL(dsId, "updateApiCollectionsRow.sql.st", params, false, false);
323
	}
324

  
325
	public DatasourceManagerClients getDatasourceManagerClients() {
326
		return datasourceManagerClients;
327
	}
328

  
329
	@Required
330
	public void setDatasourceManagerClients(final DatasourceManagerClients datasourceManagerClients) {
331
		this.datasourceManagerClients = datasourceManagerClients;
332
	}
333

  
334
	@Override
335
	public boolean overrideCompliance(final String repoId, final String ifaceId, final String compliance) throws DatasourceManagerServiceException {
336

  
337
		final String dsId = datasourceManagerClients.findDatasourceId(repoId);
338

  
339
		final Map<String, Object> params = Maps.newHashMap();
340
		params.put("dsId", DatasourceFunctions.asSqlValue(dsId));
341
		params.put("ifaceId", DatasourceFunctions.asSqlValue(ifaceId));
342
		params.put("field", DatasourceFunctions.asSqlValue(DatasourceConstants.OVERRIDING_COMPLIANCE_FIELD));
343

  
344
		if (StringUtils.isEmpty(compliance)) {
345
			params.put("delete", true);
346
			log.debug("Removing compliance");
347
		} else {
348
			params.put("value", DatasourceFunctions.asSqlValue(compliance));
349
			if (datasourceManagerClients.isDefinedParam(ifaceId, DatasourceConstants.OVERRIDING_COMPLIANCE_FIELD)) {
350
				params.put("update", true);
351
				log.debug("Updating compliance: " + compliance);
352
			} else {
353
				params.put("insert", true);
354
				log.debug("Adding compliance: " + compliance);
355
			}
356
		}
357
		return datasourceManagerClients.updateSQL(dsId, "overrideCompliance.sql.st", params, false, true);
358
	}
359

  
360
	@Override
361
	public List<BrowsableField> listBrowsableFields() throws DatasourceManagerServiceException {
362
		return Lists.transform(getBrowsableFields(), new Function<DbBrowsableField, BrowsableField>() {
363

  
364
			@Override
365
			public BrowsableField apply(final DbBrowsableField f) {
366
				return new BrowsableField(f.getId(), f.getLabel());
367
			}
368
		});
369
	}
370

  
371
	@Override
372
	public List<BrowseTerm> browseField(final String f) throws DatasourceManagerServiceException {
373
		final List<BrowseTerm> list = Lists.newArrayList();
374

  
375
		try {
376
			final DbBrowsableField field = findBrowseField(f);
377
			if (field != null) {
378
				final SAXReader reader = new SAXReader();
379
				for (final String s : datasourceManagerClients.searchSQL(field.getSql())) {
380
					final Document doc = reader.read(new StringReader(s));
381
					final String id = doc.valueOf("/ROW/FIELD[@name='id']");
382
					final String name = doc.valueOf("/ROW/FIELD[@name='name']");
383
					final int value = NumberUtils.toInt(doc.valueOf("/ROW/FIELD[@name='count']"), 0);
384
					if (StringUtils.isNotEmpty(id)) {
385
						list.add(new BrowseTerm(id, name, value));
386
					}
387
				}
388
			}
389
		} catch (final Exception e) {
390
			log.error("Error browsing field " + f, e);
391
		}
392

  
393
		return list;
394
	}
395

  
396
	@Override
397
	public List<SearchInterfacesEntry> searchInterface(final String field, final String value) throws DatasourceManagerServiceException {
398
		try {
399
			final Map<String, Object> params = Maps.newHashMap();
400
			params.put("value", value);
401

  
402
			if (!field.equalsIgnoreCase("__search__")) {
403
				params.put("field", field);
404
			}
405

  
406
			final DbBrowsableField f = findBrowseField(field);
407
			if ((f == null) || f.isText()) {
408
				params.put("delimeter", "'");
409
			}
410

  
411
			final SAXReader reader = new SAXReader();
412
			final Iterable<String> iter = datasourceManagerClients.searchSQL("searchRepoInterfaces.sql.st", params);
413
			return Lists.newArrayList(Iterables.transform(iter, new Function<String, SearchInterfacesEntry>() {
414

  
415
				@Override
416
				public SearchInterfacesEntry apply(final String s) {
417
					final SearchInterfacesEntry iface = new SearchInterfacesEntry();
418
					try {
419
						final Document doc = reader.read(new StringReader(s));
420
						final String country = doc.valueOf("/ROW/FIELD[@name='country']");
421

  
422
						iface.setRepoId(doc.valueOf("/ROW/FIELD[@name='repoid']"));
423
						iface.setRepoCountry(StringUtils.isEmpty(country) ? "-" : country.toUpperCase());
424
						iface.setRepoName(StringEscapeUtils.unescapeXml(doc.valueOf("/ROW/FIELD[@name='reponame']")));
425
						iface.setRepoPrefix(doc.valueOf("/ROW/FIELD[@name='repoprefix']"));
426

  
427
						iface.setId(doc.valueOf("/ROW/FIELD[@name='id']"));
428
						iface.setActive(Boolean.valueOf(doc.valueOf("/ROW/FIELD[@name='active']")));
429
						iface.setProtocol(doc.valueOf("/ROW/FIELD[@name='protocol']"));
430
						iface.setCompliance(doc.valueOf("/ROW/FIELD[@name='compliance']"));
431
						iface.setAggrDate(doc.valueOf("/ROW/FIELD[@name='aggrdate']"));
432
						iface.setAggrTotal(NumberUtils.toInt(doc.valueOf("/ROW/FIELD[@name='aggrtotal']"), 0));
433
					} catch (final Exception e) {
434
						log.error(e);
435
					}
436
					return iface;
437
				}
438
			}));
439
		} catch (final Exception e) {
440
			log.error("Error searching field " + field + " - value: " + value, e);
441
		}
442
		return Lists.newArrayList();
443
	}
444

  
445
	private DbBrowsableField findBrowseField(final String id) {
446
		for (final DbBrowsableField f : getBrowsableFields()) {
447
			if (f.getId().equals(id)) { return f; }
448
		}
449
		return null;
450
	}
451

  
452
	@Override
453
	public List<RepositoryMapEntry> getRepositoryMap() throws DatasourceManagerServiceException {
454
		final SAXReader reader = new SAXReader();
455

  
456
		try {
457
			final Iterable<String> iter = datasourceManagerClients.searchSQL("findReposMap.sql.st", null);
458
			return Lists.newArrayList(Iterables.transform(iter, new Function<String, RepositoryMapEntry>() {
459

  
460
				@Override
461
				public RepositoryMapEntry apply(final String s) {
462
					final RepositoryMapEntry r = new RepositoryMapEntry();
463
					try {
464
						final Document doc = reader.read(new StringReader(s));
465
						r.setId(doc.valueOf("/ROW/FIELD[@name='id']"));
466
						r.setName(StringEscapeUtils.unescapeXml(doc.valueOf("/ROW/FIELD[@name='name']")));
467
						r.setLat(NumberUtils.toFloat(doc.valueOf("/ROW/FIELD[@name='lat']"), 0));
468
						r.setLng(NumberUtils.toFloat(doc.valueOf("/ROW/FIELD[@name='lng']"), 0));
469
					} catch (final Exception e) {
470
						log.error(e);
471
					}
472
					return r;
473
				}
474
			}));
475
		} catch (final Exception e) {
476
			log.error("Error obtaing repo map entries", e);
477
		}
478
		return Lists.newArrayList();
479
	}
480

  
481
	@Override
482
	public List<SimpleDatasourceDesc> simpleListDatasourcesByType(final String type) throws DatasourceManagerServiceException {
483
		final Map<String, Object> params = Maps.newHashMap();
484
		params.put("type", type);
485
		final List<SimpleDatasourceDesc> list = Lists.newArrayList();
486
		try {
487
			final SAXReader reader = new SAXReader();
488
			for (final String s : datasourceManagerClients.searchSQL("simpleFindRepos.sql.st", params)) {
489
				final Document doc = reader.read(new StringReader(s));
490
				final SimpleDatasourceDesc r = new SimpleDatasourceDesc();
491
				r.setId(doc.valueOf("/ROW/FIELD[@name='id']"));
492
				r.setOrigId(doc.valueOf("/ROW/FIELD[@name='id']"));
493
				r.setName(StringEscapeUtils.unescapeXml(doc.valueOf("/ROW/FIELD[@name='name']")));
494
				for (final Object o : doc.selectNodes("/ROW/FIELD[@name='apis']/ITEM")) {
495
					r.getApis().add(((Node) o).getText());
496
				}
497
				r.setValid(true); // TODO
498
				r.setTypology(type);
499
				list.add(r);
500
			}
501
			Collections.sort(list);
502
		} catch (final Exception e) {
503
			log.error("Error listing repos", e);
504
		}
505
		return list;
506
	}
507

  
508
	public List<DbBrowsableField> getBrowsableFields() {
509
		return browsableFields;
510
	}
511

  
512
	@Required
513
	public void setBrowsableFields(final List<DbBrowsableField> browsableFields) {
514
		this.browsableFields = browsableFields;
515
	}
516
}
modules/dnet-openaireplus-datasource-manager-service/trunk/src/main/java/eu/dnetlib/enabling/datasources/DbBrowsableField.java
1
package eu.dnetlib.enabling.datasources;
2

  
3
import eu.dnetlib.enabling.datasources.rmi.BrowsableField;
4

  
5
public class DbBrowsableField extends BrowsableField {
6

  
7
	private String sql;
8

  
9
	private boolean text = true;
10

  
11
	public DbBrowsableField() {
12
		super();
13
	}
14

  
15
	public DbBrowsableField(final String id, final String label) {
16
		super(id, label);
17
	}
18

  
19
	public DbBrowsableField(final String id, final String label, final String sql) {
20
		super(id, label);
21
		this.sql = sql;
22
	}
23

  
24
	public String getSql() {
25
		return sql;
26
	}
27

  
28
	public void setSql(final String sql) {
29
		this.sql = sql;
30
	}
31

  
32
	public boolean isText() {
33
		return text;
34
	}
35

  
36
	public void setText(boolean text) {
37
		this.text = text;
38
	}
39

  
40
}
modules/dnet-openaireplus-datasource-manager-service/trunk/src/main/java/eu/dnetlib/enabling/datasources/DatasourceParams.java
1
package eu.dnetlib.enabling.datasources;
2

  
3
public enum DatasourceParams {
4
	baseUrl, iis_processing_workflow, metadata_identifier_path
5
}
modules/dnet-openaireplus-datasource-manager-service/trunk/src/main/resources/eu/dnetlib/enabling/datasources/searchApiCollectionParam.sql.st
1
SELECT * FROM apicollections WHERE api = $ifaceId$ AND lower(param) = lower($field$)
modules/dnet-openaireplus-datasource-manager-service/trunk/src/main/resources/eu/dnetlib/enabling/datasources/deleteOldExtraFields.sql.st
1
DELETE FROM apicollections WHERE api = $ifaceId$ AND accessparam = false;
modules/dnet-openaireplus-datasource-manager-service/trunk/src/main/resources/eu/dnetlib/enabling/datasources/searchRepoInterfaces.sql.st
1
SELECT * FROM (SELECT
2
	(array_agg(DISTINCT o.countryclass))[1] AS country,
3
	(array_agg(DISTINCT o.legalname))[1]    AS organization,
4
	d.id                                    AS repoid,
5
	d.officialname                          AS reponame,
6
	d.englishname                           AS othername,
7
	d.namespaceprefix                       AS repoprefix,
8
	d.websiteurl                            AS repourl,
9
	a.id                                    AS id,
10
	a.active                                AS active,
11
	a.protocolclass                         AS protocol,
12
	a.typologyclass                         AS type,
13
	CASE 
14
		WHEN (accomp.edited IS NOT NULL) THEN accomp.edited 
15
		WHEN (accomp.original IS NOT NULL) THEN accomp.original
16
		ELSE a.compatibilityclass
17
	END AS compliance,
18
	CASE 
19
		WHEN (acaggrdate.edited   IS NOT NULL) THEN acaggrdate.edited 
20
		WHEN (acaggrdate.original IS NOT NULL) THEN acaggrdate.original
21
		WHEN (acdowndate.edited   IS NOT NULL) THEN acdowndate.edited 
22
		WHEN (acdowndate.original IS NOT NULL) THEN acdowndate.original
23
		ELSE ''
24
	END AS aggrdate,
25
	CASE 
26
		WHEN (acaggrtotal.edited   IS NOT NULL) THEN acaggrtotal.edited 
27
		WHEN (acaggrtotal.original IS NOT NULL) THEN acaggrtotal.original
28
		WHEN (acdowntotal.edited   IS NOT NULL) THEN acdowntotal.edited 
29
		WHEN (acdowntotal.original IS NOT NULL) THEN acdowntotal.original
30
		ELSE '0'
31
	END AS aggrtotal
32
FROM
33
	api a 
34
	LEFT OUTER JOIN datasources d               ON (a.datasource = d.id)
35
	LEFT OUTER JOIN datasource_organization dao ON (d.id = dao.datasource)
36
	LEFT OUTER JOIN organizations o             ON (dao.organization = o.id)
37
	LEFT OUTER JOIN apicollections accomp       ON (a.id = accomp.api      AND accomp.param = 'overriding_compliance')
38
	LEFT OUTER JOIN apicollections acaggrdate   ON (a.id = acaggrdate.api  AND acaggrdate.param = 'last_aggregation_date')
39
	LEFT OUTER JOIN apicollections acaggrtotal  ON (a.id = acaggrtotal.api AND acaggrtotal.param = 'last_aggregation_total')
40
	LEFT OUTER JOIN apicollections acdowndate   ON (a.id = acdowndate.api  AND acdowndate.param = 'last_download_date')
41
	LEFT OUTER JOIN apicollections acdowntotal  ON (a.id = acdowntotal.api AND acdowntotal.param = 'last_download_total')
42
	GROUP BY
43
		d.id, d.officialname, d.englishname, d.namespaceprefix, d.websiteurl, 
44
		a.id, a.active, a.protocolclass, a.typologyclass, a.compatibilityclass,
45
		accomp.edited, accomp.original,
46
		acaggrdate.edited, acaggrdate.original, 
47
		acdowndate.edited, acdowndate.original,
48
		acaggrtotal.edited, acaggrtotal.original,
49
		acdowntotal.edited, acdowntotal.original
50
) AS t WHERE
51
	$if(field)$
52
		$field$ = $delimeter$$value$$delimeter$
53
	$else$
54
		repoid ILIKE '%$value$%' 
55
		OR reponame ILIKE '%$value$%'
56
		OR othername ILIKE '%$value$%'
57
		OR repoprefix ILIKE '%$value$%' 
58
		OR repourl ILIKE '%$value$%' 
59
		OR organization ILIKE '%$value$%' 
60
	$endif$
61

  
62

  
modules/dnet-openaireplus-datasource-manager-service/trunk/src/main/resources/eu/dnetlib/enabling/datasources/simpleFindRepos.sql.st
1
SELECT 
2
	d.id                     AS id,
3
	d.officialname           AS name,
4
	array_agg(DISTINCT a.id) AS apis
5
FROM
6
	datasources d
7
	LEFT OUTER JOIN api a ON (d.id = a.datasource)
8
WHERE
9
	datasourceclass = '$type$'
10
GROUP BY 
11
	d.id,
12
	d.officialname
13
ORDER BY
14
	d.officialname
15
	
modules/dnet-openaireplus-datasource-manager-service/trunk/src/main/resources/eu/dnetlib/enabling/datasources/addDatasource.sql.st
1
BEGIN;
2

  
3
INSERT INTO datasources (
4
	_dnet_resource_identifier_,
5
	id,
6
	officialName,
7
	englishName,
8
	websiteUrl,
9
	logoUrl,
10
	contactEmail,
11
	latitude,
12
	longitude,
13
	timezone,
14
	namespacePrefix,
15
	od_NumberOfItems,
16
	od_NumberOfItemsDate,
17
	od_Policies,
18
	od_Languages,
19
	od_ContentTypes,
20
	collectedFrom,
21
	inferred,
22
	deletedByInference,
23
	trust,
24
	inferenceProvenance,
25
	dateOfValidation,
26
	registeredby,
27
	datasourceClass,
28
	provenanceActionClass,
29
	dateOfCollection,
30
	typology,
31
	activationId,
32
	mergehomonyms,
33
	description,
34
	releaseStartDate,
35
	releaseEndDate,
36
	missionStatementUrl,
37
	dataProvider,
38
	serviceProvider,
39
	databaseAccessType,
40
	dataUploadType,
41
	databaseAccessRestriction,
42
	dataUploadRestriction,
43
	versioning,
44
	citationGuidelineUrl,
45
	qualityManagementKind,
46
	pidSystems,
47
	certificates,
48
	aggregator,
49
	issn,
50
	eissn,
51
	lissn
52
) VALUES (
53
	$id$,
54
	$id$,
55
	$officialName$,
56
	$englishName$,
57
	$websiteUrl$,
58
	$logoUrl$,
59
	$contactEmail$,
60
	$latitude$,
61
	$longitude$,
62
	$timezone$,
63
	$namespacePrefix$,
64
	$odNumberOfItems$,
65
	$odNumberOfItemsDate$,
66
	$odPolicies$,
67
	$odLanguages$,
68
	$odContentTypes$,
69
	$collectedFrom$,
70
	$inferred$,
71
	$deletedByInference$,
72
	$trust$,
73
	$inferenceProvenance$,
74
	$dateOfValidation$,
75
	$registeredBy$,
76
	$datasourceClass$,
77
	$provenanceActionClass$,
78
	$dateOfCollection$,
79
	$typology$,
80
	$activationId$,
81
	$mergehomonyms$,
82
	$description$,
83
	$releaseStartDate$,
84
	$releaseEndDate$,
85
	$missionStatementUrl$,
86
	$dataProvider$,
87
	$serviceProvider$,
88
	$databaseAccessType$,
89
	$dataUploadType$,
90
	$databaseAccessRestriction$,
91
	$dataUploadRestriction$,
92
	$versioning$,
93
	$citationGuidelineUrl$,
94
	$qualityManagementKind$,
95
	$pidSystems$,
96
	$certificates$,
97
	$aggregator$,
98
	$issn$,
99
	$eissn$,
100
	$lissn$
101
);
102

  
103
$if(hasOrganization)$
104
	INSERT INTO organizations (
105
		_dnet_resource_identifier_,
106
		id,
107
		legalname,
108
		countryclass
109
	) VALUES (
110
		$id$||'::'||$organization$,
111
		$id$||'::'||$organization$,
112
		$organization$,
113
		$countryCode$
114
	);
115
	
116
	INSERT INTO datasource_organization (
117
		_dnet_resource_identifier_,
118
		datasource,
119
		organization
120
	) VALUES (
121
		$id$||'@@'||$id$||'::'||$organization$,
122
		$id$,
123
		$id$||'::'||$organization$
124
	);
125
$endif$
126

  
127

  
128

  
129
$interfaces:{ifc|
130
	INSERT INTO api (
131
		_dnet_resource_identifier_,
132
		id,
133
		datasource, 
134
		protocolclass, 
135
		contentdescriptionclass,
136
		removable,
137
		typologyclass,
138
		compatibilityclass
139
	) VALUES (
140
		$ifc.id$,
141
		$ifc.id$,
142
		$id$,
143
		$ifc.accessProtocol$,
144
		$ifc.contentDescription$,
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff