Project

General

Profile

1
package gr.uoa.di.validator.database;
2

    
3
import java.sql.Connection;
4
import java.sql.PreparedStatement;
5
import java.sql.ResultSet;
6
import java.sql.SQLException;
7
import java.util.ArrayList;
8
import java.util.HashMap;
9
import java.util.List;
10
import java.util.Map;
11
import java.util.TreeMap;
12

    
13
import javax.sql.DataSource;
14

    
15
import org.apache.log4j.Logger;
16

    
17
import com.unboundid.util.Base64;
18

    
19
import eu.dnetlib.domain.data.Repository;
20
import eu.dnetlib.domain.data.RepositoryInterface;
21
//import java.io.UnsupportedEncodingException;
22

    
23
public class OpenDoarRepositoryFactoryDirectImpl implements OpenDoarRepositoryFactory {
24
	private static Logger logger = Logger.getLogger(OpenDoarRepositoryFactoryDirectImpl.class);
25
	private DataSource openAireDataSource = null;
26

    
27
	public Repository getRepository(String officialName, String id) throws SQLException {
28
		Connection conn = null;
29
		Repository repo = null;
30
		
31
		try {
32
			logger.debug("getting repository " + officialName + " from openaire db");
33

    
34
			conn = openAireDataSource.getConnection();
35
			PreparedStatement stm = null, stm1 = null;
36
			stm = conn.prepareStatement("SELECT * FROM datasources d WHERE d.id = ?");
37
			stm.setString(1, id);				
38

    
39
			ResultSet rs = stm.executeQuery();
40

    
41
			if (rs.next()) {
42
				repo = new Repository();
43
				repo.setId(rs.getString("Id"));
44
				repo.setOfficialName(officialName);
45
				repo.setEnglishName(rs.getString("englishname"));
46
				repo.setWebsiteUrl(rs.getString("websiteurl"));
47
				repo.setLogoUrl(rs.getString("logourl"));
48
				repo.setContactEmail(rs.getString("contactemail"));
49
				repo.setLatitude(rs.getDouble("latitude"));
50
				repo.setLongitude(rs.getDouble("longitude"));
51
				repo.setTimezone(rs.getDouble("timezone"));
52
				repo.setNamespacePrefix(rs.getString("namespaceprefix"));
53
				repo.setDescription(rs.getString("description"));
54
				repo.setCollectedFrom(rs.getString("collectedfrom"));
55
//				repo.setOptional1(rs.getString("optional1"));
56
//				repo.setOptional2(rs.getString("optional2"));
57
				repo.setActivationId("activationid");
58
				repo.setTypology(rs.getString("typology"));
59
				repo.setDatasourceClass(rs.getString("datasourceClass"));
60
				repo.setProvenanceActionClass(rs.getString("provenanceactionclass"));
61
				repo.setDateOfCollection(rs.getDate("dateofcollection"));
62
				repo.setIssn(rs.getString("issn"));
63
				repo.setEissn(rs.getString("eissn"));
64
				repo.setLissn(rs.getString("lissn"));
65
				
66
				rs.close();
67
				stm.close();
68
				
69
				stm = conn.prepareStatement("SELECT * FROM api a WHERE a.datasource = ? ORDER BY id");
70
				stm.setString(1, repo.getId());
71

    
72
				rs = stm.executeQuery();
73

    
74
				logger.debug("searching api");
75
				while (rs.next()) {
76
					RepositoryInterface iFace = new RepositoryInterface();
77
					if (rs.getBoolean("useedited")) {
78
						iFace.setBaseUrl(rs.getString("edited"));
79
						iFace.setAccessProtocol(rs.getString("editedprotocolclass"));
80
					}
81
					else {
82
						iFace.setBaseUrl(rs.getString("original"));
83
						iFace.setAccessProtocol(rs.getString("originalprotocolclass"));
84
					}
85
//					repo.setApiTypology(rs.getString("typology"));
86
					iFace.setId(rs.getString("id"));
87
					String apiId = rs.getString("id");
88
					logger.debug("apiId: "+ apiId);
89
					
90
					stm1 = conn.prepareStatement("SELECT * FROM apicollections ac WHERE ac.api = ?");
91
					stm1.setString(1, apiId);
92

    
93
					ResultSet rs1 = stm1.executeQuery();
94
					String setEdited = null;
95
					iFace.setAccessSet("none");
96
					logger.debug("searching apicollections");
97
					while (rs1.next()) {				
98
					
99
						if (rs1.getString("accessparam").equals("SET")) {
100
							iFace.setAccessSet(rs1.getString("accessvalue"));
101
						} else if (rs1.getString("accessparam").equals("SET_EDITED")) {
102
							setEdited = rs1.getString("accessvalue");
103
						} else if (rs1.getString("accessparam").equals("FORMAT")) {
104
							iFace.setAccessFormat(rs1.getString("accessvalue"));
105
						}
106
					}
107
					if (setEdited != null)
108
						iFace.setAccessSet(setEdited);
109
					
110
					if (iFace.getAccessSet().equalsIgnoreCase("driver")) {
111
						iFace.setCompliance("Driver");
112
					} else if (iFace.getAccessSet().equalsIgnoreCase("ec_fundedresources")) {
113
						iFace.setCompliance("OpenAIRE 2.0");
114
					} else if (iFace.getAccessSet().equalsIgnoreCase("openaire")) {
115
						iFace.setCompliance("OpenAIRE 3.0");
116
					} else if (iFace.getAccessSet().equalsIgnoreCase("openaire_data")) {
117
						iFace.setCompliance("OpenAIRE 3.0");
118
					} else if (iFace.getAccessSet().equalsIgnoreCase("none")) {
119
						iFace.setCompliance("notCompatible");
120
					} else {
121
						iFace.setCompliance("UNKNOWN");
122
					}
123
						
124
					rs1.close();
125
					stm1.close();
126
					repo.getInterfaces().add(iFace);
127
				}
128
				
129
				rs.close();
130
				stm.close();
131
				
132
//				stm = conn.prepareStatement("select legal_name from datasources, organizations, datasources_organizations where datasourceid=datasource and organizationid=organization and official_name = ?");
133
				stm = conn.prepareStatement("SELECT o.legalname, o.countryclass FROM datasources d, organizations o, datasource_organization dor WHERE dor.datasource=d.id AND o.id=dor.organization AND d.id = ?");
134
				stm.setString(1, id);
135
				rs = stm.executeQuery();
136

    
137
				if (rs.next()) {
138
					repo.setOrganization(rs.getString(1));
139
					repo.setCountryCode(rs.getString(2));
140
				}
141

    
142
				rs.close();
143
				stm.close();
144
			}
145
		} catch (SQLException e) {
146
			logger.error("Error getting repo " + officialName + " from openaire db", e);
147
			throw e;
148
		}  
149
		finally {
150
			if (conn != null)
151
				try {
152
					conn.close();
153
				} catch (SQLException e) {
154
					logger.error("Error closing connection", e);
155
				}
156
		}
157

    
158
		return repo;
159
	}
160

    
161
	@Override
162
	public String storeRepository(Repository repo, String datatype) throws Exception {
163
		String retMessage = null;
164
		Connection conn = null;
165
		try {
166
			conn = openAireDataSource.getConnection();
167
			PreparedStatement stm = null;
168
			ResultSet rs = null;
169
			
170
			logger.debug("storing " + datatype + " repository..");
171
			if (datatype.equalsIgnoreCase("opendoar")) {
172
				
173
				
174
				logger.debug("updating datasources values..");
175
				
176
				stm = conn.prepareStatement("UPDATE datasources SET englishname = ?, logourl = ?, timezone = ?, optional1 = ?, activationid = ?, contactemail = ? WHERE id = ?");
177
				stm.setString(1, repo.getEnglishName());
178
				stm.setString(2, repo.getLogoUrl());
179
				stm.setDouble(3, repo.getTimezone());
180
				stm.setString(4, repo.getRegisteredBy());
181
				stm.setString(5, repo.getActivationId());
182
				stm.setString(6, repo.getContactEmail());
183
				stm.setString(7, repo.getId());
184
				
185
				if (stm.executeUpdate() > 0 ) {
186
					logger.debug("finished updating values in datasources..");
187
					stm.close();
188
					conn.commit();
189
				}
190
				
191
				logger.debug("checking current values in api..");
192
				
193
				stm = conn.prepareStatement("SELECT COUNT(*) FROM api WHERE datasource = ?");
194
				stm.setString(1, repo.getId());
195
				rs = stm.executeQuery();
196
				int noCurrentInApi = 0;
197
				if (rs.next()) {
198
					noCurrentInApi = rs.getInt(1);
199
				}
200
				stm.close();
201
				rs.close();
202
				
203
				
204
				for (RepositoryInterface iFace : repo.getInterfaces()) {
205
					logger.debug("checking if interface : " + iFace.getId() + " already exists in api..");
206
					stm = conn.prepareStatement("SELECT * FROM api WHERE datasource = ? AND id = ?");
207
					stm.setString(1, repo.getId());
208
					stm.setString(2, iFace.getId());
209
					rs = stm.executeQuery();
210
					
211
					if (!rs.next()) {
212
						logger.debug("not exist..");
213
						stm.close();
214
						rs.close();
215
						
216
						stm = conn.prepareStatement("INSERT INTO api (id, typology, originalprotocolclass, original, datasource, _dnet_resource_identifier_)  VALUES (?,?,?,?,?,?)");
217
						iFace.setId("api_________::" + repo.getId() + "::" + noCurrentInApi);
218
						logger.debug("creating new api_id: " + iFace.getId());
219
						noCurrentInApi++;
220
						
221
						String apiTypology = "UNKNOWN";
222
						if (iFace.getDesiredCompatibilityLevel().equalsIgnoreCase("openaire3.0") || iFace.getDesiredCompatibilityLevel().equalsIgnoreCase("openaire3.0_data") || iFace.getDesiredCompatibilityLevel().equalsIgnoreCase("openaire2.0")) {
223
							apiTypology = "openaire";
224
						} else if (iFace.getDesiredCompatibilityLevel().contains("driver")) {
225
							apiTypology = "driver";
226
						}
227
						stm.setString(1, iFace.getId());
228
						stm.setString(2, apiTypology);
229
						stm.setString(3, iFace.getAccessProtocol());
230
						stm.setString(4, iFace.getBaseUrl());
231
						stm.setString(5, repo.getId());
232
						stm.setString(6, iFace.getId());
233
						
234
						if (stm.executeUpdate() > 0) {
235
							logger.debug(iFace.getId() + " inserted in api..");
236
							logger.debug("now inserting in apicollections..");
237
							stm.close();
238
							rs.close();
239
							conn.commit();
240
		
241
							stm = conn.prepareStatement("INSERT INTO apicollections (accessparam, accessvalue, api, _dnet_resource_identifier_)  VALUES (?,?,?,?)");
242
							
243
							stm.setString(1,"FORMAT");
244
							stm.setString(2, iFace.getAccessFormat());
245
							stm.setString(3, iFace.getId());
246
							stm.setString(4,"FORMAT" + "@@" + iFace.getAccessFormat() + "@@" + iFace.getId());
247
							
248
							stm.addBatch();
249
	
250
							stm.setString(1,"SET");
251
							stm.setString(2, iFace.getAccessSet());
252
							stm.setString(3, iFace.getId());
253
							stm.setString(4,"SET" + "@@" + iFace.getAccessSet() + "@@" + iFace.getId());
254
									
255
							stm.addBatch();
256
						
257
							if (stm.executeBatch()[0] > 0) {
258
								logger.debug(iFace.getId() + " inserted in apicollections.");
259
							}
260
							logger.debug("finished updating set + format values in apicollections.");
261
							stm.close();
262
							conn.commit();
263
						}
264
							
265
					} else {
266
						
267
						logger.debug("exists..");
268
						boolean edited = false;
269
						if( rs.getString("original") == null || rs.getString("originalprotocolclass") == null || !rs.getString("original").equalsIgnoreCase(iFace.getBaseUrl()) || !rs.getString("originalprotocolclass").equalsIgnoreCase(iFace.getAccessProtocol()) ) {
270
							edited = true;
271
						}
272
							logger.debug("updating edited values in api..");
273
							stm.close();
274
							rs.close();
275
							
276
							String apiTypology = "UNKNOWN";
277
							if (iFace.getDesiredCompatibilityLevel().equalsIgnoreCase("openaire3.0") || iFace.getDesiredCompatibilityLevel().equalsIgnoreCase("openaire2.0")) {
278
								apiTypology = "openaire";
279
							} else if (iFace.getDesiredCompatibilityLevel().contains("driver")) {
280
								apiTypology = "driver";
281
							}
282
								
283
							stm = conn.prepareStatement("UPDATE api SET typology = ?, edited = ?, editedprotocolclass = ?, useedited = ?  WHERE datasource = ? AND id = ? ");
284
							stm.setString(1, apiTypology);
285
							if (edited) {
286
								stm.setString(2, iFace.getBaseUrl());
287
								stm.setString(3, iFace.getAccessProtocol());
288
								stm.setBoolean(4, true);
289
							} else {
290
								stm.setString(2, null);
291
								stm.setString(3, null);
292
								stm.setBoolean(4, false);
293
							}
294
							stm.setString(5, repo.getId());
295
							stm.setString(6, iFace.getId());
296
							
297
							if (stm.executeUpdate() > 0 ) {
298
								logger.debug("finished updating values in api..");
299
								stm.close();
300
								conn.commit();
301
							}
302
						logger.debug("updating apicollections..");
303
						
304
						logger.debug("deleting current values from apicollections..");
305
						stm = conn.prepareStatement("DELETE FROM apicollections WHERE api = ?");
306
						
307
						stm.setString(1, iFace.getId());
308
						
309
						if (stm.executeUpdate() > 0) {
310
							logger.debug("values for interface " + iFace.getId() + " deleted from api_collections..");
311
							stm.close();
312
							rs.close();
313
							conn.commit();
314
							
315
						}
316
							logger.debug("now inserting in apicollections..");
317
		
318
							stm = conn.prepareStatement("INSERT INTO apicollections (accessparam, accessvalue, api, _dnet_resource_identifier_)  VALUES (?,?,?,?)");
319
							
320
							stm.setString(1,"FORMAT");
321
							stm.setString(2, iFace.getAccessFormat());
322
							stm.setString(3, iFace.getId());
323
							stm.setString(4,"FORMAT" + "@@" + iFace.getAccessFormat() + "@@" + iFace.getId());
324
							
325
							stm.addBatch();
326
	
327
							stm.setString(1,"SET");
328
							stm.setString(2, iFace.getAccessSet());
329
							stm.setString(3, iFace.getId());
330
							stm.setString(4,"SET" + "@@" + iFace.getAccessSet() + "@@" + iFace.getId());
331
								
332
							stm.addBatch();
333
							
334
							if (stm.executeBatch()[0] > 0) {
335
								logger.debug(iFace.getId() + " inserted in apicollections.");
336
							}
337
							logger.debug("finished updating set + format values in apicollections.");
338
							stm.close();
339
							conn.commit();
340
					
341
					}	
342
					logger.debug("interface : " + iFace.getId() + " inserted successfully..");
343
				}			
344
						
345
			} else if (datatype.equalsIgnoreCase("journal")||datatype.equalsIgnoreCase("aggregator")){		
346
	
347
					logger.debug("looking if " + datatype + " " + repo.getOfficialName() + " is already in datasources");
348
					stm = conn.prepareStatement("SELECT * FROM datasources WHERE officialname = ? OR id = ?");
349
					stm.setString(1, repo.getOfficialName());
350
					stm.setString(2, repo.getId());
351
		
352
					rs = stm.executeQuery();
353
		
354
					if (rs.next()) {
355
						retMessage = datatype + " " + repo.getOfficialName() + " is already in datasources.";
356
						logger.debug(retMessage);
357
						
358
						stm.close();
359
						rs.close();
360
					} else {
361
						logger.debug(datatype + " " + repo.getOfficialName() + " is not in datasources. Inserting..");
362
										
363
						stm = conn.prepareStatement("INSERT INTO datasources(id, officialname, englishname, websiteurl, logourl, contactemail, latitude, longitude, timezone, namespaceprefix, collectedfrom, activationid, typology, datasourceclass, provenanceactionclass, _dnet_resource_identifier_, optional1, issn, eissn, lissn, description) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
364
						
365
						stm.setString(1, repo.getId()); //activation id
366
						stm.setString(2, repo.getOfficialName());
367
						stm.setString(3, repo.getEnglishName());
368
						stm.setString(4, repo.getWebsiteUrl());
369
						stm.setString(5, repo.getLogoUrl());
370
						stm.setString(6, repo.getContactEmail());
371
						stm.setDouble(7, repo.getLatitude());
372
						stm.setDouble(8, repo.getLongitude());
373
						stm.setString(9, repo.getTimezone() + "");
374
						stm.setString(10, repo.getNamespacePrefix());
375
						stm.setString(11, repo.getCollectedFrom()); 
376
						stm.setString(12, repo.getActivationId()); 
377
						stm.setString(13, repo.getTypology()); 
378
						stm.setString(14, repo.getDatasourceClass());
379
						stm.setString(15, repo.getProvenanceActionClass());
380
						stm.setString(16,repo.getId());
381
						stm.setString(17, repo.getRegisteredBy());
382
						stm.setString(18, repo.getIssn());
383
						stm.setString(19, repo.getEissn());
384
						stm.setString(20, repo.getLissn());
385
						stm.setString(21, repo.getDescription());
386
						
387
						if (stm.executeUpdate() > 0) {
388
							logger.debug(datatype + " " + repo.getOfficialName() + " inserted in datasources.");
389
							logger.debug("inserting in api..");
390
							
391
							stm.close();
392
							conn.commit();
393
	
394
							
395
							int noCurrentInApi = 0;						
396
							for (RepositoryInterface iFace : repo.getInterfaces()) {
397
									
398
								stm = conn.prepareStatement("INSERT INTO api (id, typology, originalprotocolclass, original, datasource, _dnet_resource_identifier_)  VALUES (?,?,?,?,?,?)");
399
								iFace.setId("api_________::" + repo.getId() + "::" + noCurrentInApi);
400
								logger.debug("creating new api_id: " + iFace.getId());
401
								noCurrentInApi++;
402
								String apiTypology = "UNKNOWN";
403
								if (iFace.getDesiredCompatibilityLevel().equalsIgnoreCase("openaire3.0") || iFace.getDesiredCompatibilityLevel().equalsIgnoreCase("openaire2.0")) {
404
									apiTypology = "openaire";
405
								} else if (iFace.getDesiredCompatibilityLevel().equalsIgnoreCase("driver")) {
406
									apiTypology = "driver";
407
								}
408
								stm.setString(1, iFace.getId());
409
								stm.setString(2, apiTypology);
410
								stm.setString(3, iFace.getAccessProtocol());
411
								stm.setString(4, iFace.getBaseUrl());
412
								stm.setString(5, repo.getId());
413
								stm.setString(6, iFace.getId());
414
								
415
								if (stm.executeUpdate() > 0) {
416
									logger.debug(iFace.getId() + " inserted in api..");
417
									logger.debug("now inserting in apicollections..");
418
									stm.close();
419
									rs.close();
420
									conn.commit();
421
				
422
									stm = conn.prepareStatement("INSERT INTO apicollections (accessparam, accessvalue, api, _dnet_resource_identifier_)  VALUES (?,?,?,?)");
423
									
424
									stm.setString(1,"FORMAT");
425
									stm.setString(2, iFace.getAccessFormat());
426
									stm.setString(3, iFace.getId());
427
									stm.setString(4,"FORMAT" + "@@" + iFace.getAccessFormat() + "@@" + iFace.getId());
428
									
429
									stm.addBatch();
430
	
431
									stm.setString(1,"SET");
432
									stm.setString(2, iFace.getAccessSet());
433
									stm.setString(3, iFace.getId());
434
									stm.setString(4,"SET" + "@@" + iFace.getAccessSet() + "@@" + iFace.getId());
435
										
436
									stm.addBatch();
437
									
438
									if (stm.executeBatch()[0] > 0) {
439
										logger.debug(iFace.getId() + " inserted in apicollections.");
440
									}
441
									logger.debug("finished updating set + format values in apicollections.");
442
									stm.close();
443
									conn.commit();
444
								}
445
									
446
								
447
								logger.debug("interface : " + iFace.getId() + " inserted successfully..");
448
							}			
449
							
450
							logger.debug("inserting in organizations..");
451
									
452
							stm = conn.prepareStatement("SELECT * FROM organizations WHERE id = ?");
453
							stm.setString(1, "openaire____::" + Base64.encode(repo.getOrganization()));
454
							
455
							ResultSet rs1 = stm.executeQuery();
456
							
457
							boolean exists = rs1.next();
458
							
459
							rs1.close();
460
							
461
							if(!exists) {
462
								
463
//								String countryCode = "UNKNOWN";
464
//								stm = conn.prepareStatement("SELECT code FROM class WHERE name = ?");
465
//								stm.setString(1,repo.getCountryName());
466
//								
467
//								rs1 = stm.executeQuery();
468
//								
469
//								if ( rs1.next()) {
470
//									countryCode = rs1.getString(1);
471
//								stm.close();
472
//								}
473
								if (repo.getCountryCode() == null) {
474
									repo.setCountryCode("UNKNOWN");
475
								}
476
								stm = conn.prepareStatement("INSERT INTO organizations (legalname, countryclass, id, legalshortname, collectedfrom, provenanceactionclass, _dnet_resource_identifier_) VALUES (?,?,?,?,?,?,?)");
477
								stm.setString(1, repo.getOrganization());
478
								stm.setString(2, repo.getCountryCode());
479
								stm.setString(3, "openaire____::" + Base64.encode(repo.getOrganization()));
480
								stm.setString(4, repo.getOrganization());
481
								stm.setString(5, repo.getCollectedFrom());
482
								stm.setString(6, repo.getProvenanceActionClass());
483
								stm.setString(7, "openaire____::" + Base64.encode(repo.getOrganization()));
484
								stm.executeUpdate();
485
								logger.debug("organization" + " openaire____::" + Base64.encode(repo.getOrganization()) + " inserted in organizations.");
486
								
487
								stm.close();
488
								conn.commit();
489
							} else {
490
								logger.debug("Organization "+repo.getOrganization()+" already exists");
491
							}
492
							logger.debug("inserting in datasource-organizations..");
493
							stm = conn.prepareStatement("INSERT INTO datasource_organization (organization, datasource, _dnet_resource_identifier_) VALUES (?,?,?)");
494
							stm.setString(1, "openaire____::" + Base64.encode(repo.getOrganization()));
495
							stm.setString(2, repo.getId());
496
							stm.setString(3, repo.getId() + "@@" + "openaire____::" + Base64.encode(repo.getOrganization()) );
497
	
498
							if( stm.executeUpdate()>0) {
499
								logger.debug("organization" + "openaire____::" + Base64.encode(repo.getOrganization()) + " inserted in datasource-organizations.");
500
								stm.close();
501
								conn.commit();
502
							}		
503
						}
504
						logger.debug("Finished storing " + datatype + " repository..");
505
					}
506
	
507
			}
508
		
509
		} catch (SQLException e) {
510
			logger.error("Error storing repo " + repo.getOfficialName() + " in openaire db", e);
511
			throw e;
512
		} finally {
513
			if (conn != null)
514
				try {
515
					conn.close();
516
				} catch (SQLException e) {
517
					logger.error("Error closing connection", e);
518
				}
519
		}
520
		return retMessage;
521
	}
522

    
523
	public TreeMap<String, List<Map<String, String>>> getRepositoriesByCountry(String collectedFrom) throws SQLException {
524
		Connection conn = null;
525
		TreeMap<String, List<Map<String, String>>> res = new TreeMap<String, List<Map<String, String>>>();
526

    
527
		try {
528
			logger.debug("getting repos by country from openaire db");
529
			String collFrom = null;
530
			if (collectedFrom.equalsIgnoreCase("opendoar")) {
531
				logger.debug("populating doar list");
532
				collFrom = "openaire____::opendoar";
533
			} else if (collectedFrom.equalsIgnoreCase("re3data")) {
534
				logger.debug("populating re3data list");
535
				collFrom = "openaire____::re3data";
536
			}
537
			conn = openAireDataSource.getConnection();
538
			PreparedStatement ps = conn.prepareStatement("SELECT DISTINCT d.id, d.officialname, c.name, d.websitEurl FROM datasources d, organizations o, datasource_organization dor, class c WHERE dor.datasource=d.id AND o.id=dor.organization AND o.countryclass=c.code AND d.collectedfrom=? ORDER BY c.name, d.officialname");
539
//			.prepareStatement("SELECT d.official_name, c.name, d.web_site_url FROM countries c, organizations o, datasources d, datasources_organizations dor WHERE dor.datasource=d.datasourceid AND o.organizationid=dor.organization AND o.country_of_origin = c.countryid order by c.name, d.official_name");
540
			ps.setString(1, collFrom);
541
			ResultSet rs = ps.executeQuery();
542

    
543
			while (rs.next()) {
544
				String country = rs.getString(3);
545
				List<Map<String, String>> repos = res.get(country);
546

    
547
				if (repos == null) {
548
					repos = new ArrayList<Map<String, String>>();
549
					res.put(country, repos);
550
				}
551

    
552
				Map<String, String> repo = new HashMap<String, String>();
553
				repo.put("name", rs.getString(2));
554
				repo.put("url", rs.getString(4));
555
				repo.put("id", rs.getString(1));
556
				repos.add(repo);
557
			}
558
		} catch (SQLException e) {
559
			logger.error("Error getting repositories from openaire db", e);
560
			throw e;
561
		} finally {
562
			if (conn != null) {
563
				try {
564
					conn.close();
565
				} catch (SQLException e) {
566
					logger.error("Error closing connection", e);
567
				}
568
			}
569
		}
570

    
571
		return res;
572

    
573
	}
574

    
575
	public DataSource getOpenAireDataSource() {
576
		return openAireDataSource;
577
	}
578

    
579
	public void setOpenAireDataSource(DataSource openAireDataSource) {
580
		this.openAireDataSource = openAireDataSource;
581
	}
582

    
583
	@Override
584
	public List<Repository> getRepos(String user_mail) throws SQLException {				
585
			Connection conn = null;
586
			Repository repo = null;
587
			List<Repository> retRepos = new ArrayList<Repository>();
588
			try {
589
				logger.debug("getting repositories registered by user: " + user_mail + " from openaire db");
590

    
591
				conn = openAireDataSource.getConnection();
592
				PreparedStatement stm = conn.prepareStatement("SELECT * FROM datasources d WHERE contactemail = ? OR optional1 = ?");
593
				stm.setString(1, user_mail);
594
				stm.setString(2, user_mail);
595
				ResultSet rs = stm.executeQuery();
596

    
597
				while (rs.next()) {
598
					repo = new Repository();
599
					repo.setId(rs.getString("id"));
600
					repo.setOfficialName(rs.getString("officialname"));
601
					retRepos.add(repo);
602
				}
603
				rs.close();
604
				stm.close();
605
			} catch (Exception e) {
606
				logger.error("error getting repositories registered by user: " + user_mail + " from openaire db", e);
607
			} finally {
608
				try {
609
					if (conn != null)
610
						conn.close();
611
				} catch (Exception e) {
612
					logger.warn("could not close openaire db connection", e);
613
				}
614
			}
615
			return retRepos;
616
		
617
	}
618

    
619
	@Override
620
	public List<String> getUrlsOfRepos(String user_mail) throws SQLException {				
621
			Connection conn = null;
622
			List<String> urls = new ArrayList<String>();
623
			try {
624
				logger.debug("getting url from repositories registered by user: " + user_mail + " from openaire db");
625

    
626
				conn = openAireDataSource.getConnection();
627
				PreparedStatement stm = conn.prepareStatement("SELECT a.original, a.edited FROM datasources d, api a WHERE a.datasource=d.id AND ( d.optional1=? OR d.contactemail=?)");
628
				stm.setString(1, user_mail);
629
				stm.setString(2, user_mail);
630
				ResultSet rs = stm.executeQuery();
631

    
632
				while (rs.next()) {
633
					if (rs.getString(1) != null && !rs.getString(1).isEmpty() && !urls.contains(rs.getString(1)))
634
						urls.add(rs.getString(1));
635
					if (rs.getString(2) != null && !rs.getString(2).isEmpty() && !urls.contains(rs.getString(2)))
636
						urls.add(rs.getString(2));
637
				}
638
				rs.close();
639
				stm.close();
640
			} catch (Exception e) {
641
				logger.error("error getting url from repositories registered by user: " + user_mail + " from openaire db", e);
642
			} finally {
643
				try {
644
					if (conn != null)
645
						conn.close();
646
				} catch (Exception e) {
647
					logger.warn("could not close openaire db connection", e);
648
				}
649
			}
650
			return urls;
651
		
652
	}
653
	
654
	
655
	
656
	
657
	@Override
658
	public boolean repoIsCompliant(String officialName) throws SQLException {
659
		boolean retCompliant = false;
660
		Connection conn = null;
661
		try {
662
			conn = openAireDataSource.getConnection();
663
			PreparedStatement stm = conn.prepareStatement("SELECT openairecompatibilityclass FROM datasources WHERE officialname = ?");
664

    
665
			stm.setString(1, officialName);
666
			ResultSet rs = stm.executeQuery();
667
			if(rs.next()) {
668
				if (rs.getString(1).equals("UNKNOWN") || rs.getString(1).equals("notCompatible")) {
669
					retCompliant = false;
670
				} else {
671
					retCompliant = true;
672
				}
673
			}
674
			
675

    
676
			stm.close();
677

    
678
			rs.close();
679
			logger.debug(officialName + " set as openaire compliant");
680
		} catch (Exception e) {
681
			logger.error("error connecting to openaire db to get openairecompliant for " + officialName, e);
682
			
683
		} finally {
684
			try {
685
				if (conn != null)
686
					conn.close();
687
			} catch (Exception e) {
688
				logger.warn("could not close openaire db connection", e);
689
			}
690
		}
691
		return retCompliant;
692

    
693
	}
694

    
695
	@Override
696
	public Map<String, String> getRepoCompatibility(String officialName, String datasourceId) throws Exception {
697
		Map<String, String> compMap = null;
698
		Connection conn = null;
699
		
700
		try {
701
			logger.debug("getting repository " + officialName + " compatibility from openaire db");
702

    
703
			conn = openAireDataSource.getConnection();
704
			PreparedStatement stm = null;
705
		
706
			stm = conn.prepareStatement("SELECT openairecompatibilityclass FROM datasources d WHERE d.id = ?");
707
			stm.setString(1, datasourceId);				
708
			ResultSet rs = stm.executeQuery();
709

    
710
			compMap = new HashMap<String, String>();
711
			if (rs.next()) {
712
				String[] comp = rs.getString("openairecompatibilityclass").split("-");
713
				for ( String c : comp) {
714
					if (c.equalsIgnoreCase("driver"))
715
						compMap.put("driver", "driver");
716
					else if (c.equalsIgnoreCase("openaire2.0"))
717
						compMap.put("openaire2.0", "ec_fundedresources");
718
					else if (c.equalsIgnoreCase("openaire3.0")) {
719
						compMap.put("openaire3.0", "openaire");
720
						compMap.put("openaire3.0_data", "openaire_data");
721
					}
722
					else if (c.equalsIgnoreCase("notCompatible"))
723
						compMap.put("notCompatible", "notCompatible");
724
					else if (c.equalsIgnoreCase("UNKNOWN"))
725
						compMap.put("UNKNOWN", "UNKNOWN");
726
				}
727
			}
728

    
729
			rs.close();
730
			stm.close();
731
			
732
		} catch (SQLException e) {
733
			logger.error("Error getting repo " + officialName + " compatibility from openaire db", e);
734
			throw e;
735
		}  
736
		finally {
737
			if (conn != null)
738
				try {
739
					conn.close();
740
				} catch (SQLException e) {
741
					logger.error("Error closing connection", e);
742
				}
743
		}
744
		return compMap;
745
	}
746

    
747

    
748
	@Override
749
	public String editRepository(Repository repo, String officialNameOld, String idOld, String datatype) throws Exception {
750
	String retMessage = null;
751
	Connection conn = openAireDataSource.getConnection();
752
	PreparedStatement stm = null;
753
	ResultSet rs = null;
754
	
755
	logger.debug("editing " + datatype + " repository..");
756
	if (datatype.equalsIgnoreCase("opendoar")) {
757
		
758
		logger.debug("updating datasources values for id:"+repo.getId()+" + name:" + repo.getOfficialName() );
759

    
760
			
761
		stm = conn.prepareStatement("UPDATE datasources SET englishname = ?, logourl = ?, timezone = ? WHERE officialname = ? AND id = ?");
762
		stm.setString(1, repo.getEnglishName());
763
		stm.setString(2, repo.getLogoUrl());
764
		stm.setDouble(3, repo.getTimezone());
765
		stm.setString(4, repo.getOfficialName());
766
		stm.setString(5, repo.getId());
767
		
768
		if (stm.executeUpdate() > 0 ) {
769
			logger.debug("finished updating values in datasources..");
770
			stm.close();
771
			conn.commit();
772
		}
773
		
774
		logger.debug("checking current values in api..");
775
		
776
		stm = conn.prepareStatement("SELECT COUNT(*) FROM api WHERE datasource = ?");
777
		stm.setString(1, repo.getId());
778
		rs = stm.executeQuery();
779
		int noCurrentInApi = 0;
780
		if (rs.next()) {
781
			noCurrentInApi = rs.getInt(1);
782
		}
783
		stm.close();
784
		rs.close();
785
		
786
		
787
		for (RepositoryInterface iFace : repo.getInterfaces()) {
788
			logger.debug("checking if interface : " + iFace.getId() + " already exists in api..");
789
			stm = conn.prepareStatement("SELECT * FROM api WHERE datasource = ? AND id = ?");
790
			stm.setString(1, repo.getId());
791
			stm.setString(2, iFace.getId());
792
			rs = stm.executeQuery();
793
			
794
			if (!rs.next()) {
795
				logger.debug("not exist..");
796
				stm.close();
797
				rs.close();
798
				
799
				stm = conn.prepareStatement("INSERT INTO api (id, typology, originalprotocolclass, original, datasource, _dnet_resource_identifier_)  VALUES (?,?,?,?,?,?)");
800
				iFace.setId("api_________::" + repo.getId() + "::" + noCurrentInApi);
801
				logger.debug("creating new api_id: " + iFace.getId());
802
				noCurrentInApi++;
803
				
804
				String apiTypology = "UNKNOWN";
805
				if (iFace.getDesiredCompatibilityLevel().equalsIgnoreCase("openaire3.0") || iFace.getDesiredCompatibilityLevel().equalsIgnoreCase("openaire2.0")) {
806
					apiTypology = "openaire";
807
				} else if (iFace.getDesiredCompatibilityLevel().equalsIgnoreCase("driver")) {
808
					apiTypology = "driver";
809
				}
810
				
811
				stm.setString(1, iFace.getId());
812
				stm.setString(2, apiTypology);
813
				stm.setString(3, iFace.getAccessProtocol());
814
				stm.setString(4, iFace.getBaseUrl());
815
				stm.setString(5, repo.getId());
816
				stm.setString(6, iFace.getId());
817
				
818
				if (stm.executeUpdate() > 0) {
819
					logger.debug(iFace.getId() + " inserted in api..");
820
					logger.debug("now inserting in apicollections..");
821
					stm.close();
822
					rs.close();
823
					conn.commit();
824

    
825
					stm = conn.prepareStatement("INSERT INTO apicollections (accessparam, accessvalue, api, _dnet_resource_identifier_)  VALUES (?,?,?,?)");
826
					
827
					stm.setString(1,"FORMAT");
828
					stm.setString(2, iFace.getAccessFormat());
829
					stm.setString(3, iFace.getId());
830
					stm.setString(4,"FORMAT" + "@@" + iFace.getAccessFormat() + "@@" + iFace.getId());
831
					
832
					stm.addBatch();
833

    
834
					stm.setString(1,"SET");
835
					stm.setString(2, iFace.getAccessSet());
836
					stm.setString(3, iFace.getId());
837
					stm.setString(4,"SET" + "@@" + iFace.getAccessSet() + "@@" + iFace.getId());
838
						
839
					stm.addBatch();
840
					
841
					if (stm.executeBatch()[0] > 0) {
842
						logger.debug(iFace.getId() + " inserted in apicollections.");
843
					}
844
					logger.debug("finished updating set + format values in apicollections.");
845
					stm.close();
846
					conn.commit();
847
				}
848
					
849
			} else {
850
				
851
				logger.debug("exists..");
852
				boolean edited = false;
853
				if( rs.getString("original") == null || rs.getString("originalprotocolclass") == null || !rs.getString("original").equalsIgnoreCase(iFace.getBaseUrl()) || !rs.getString("originalprotocolclass").equalsIgnoreCase(iFace.getAccessProtocol()) ) {
854
					edited = true;
855
				}
856
					logger.debug("updating edited values in api..");
857
					stm.close();
858
					rs.close();
859
					
860
					String apiTypology = "UNKNOWN";
861
					if (iFace.getDesiredCompatibilityLevel().equalsIgnoreCase("openaire3.0") || iFace.getDesiredCompatibilityLevel().equalsIgnoreCase("openaire2.0")) {
862
						apiTypology = "openaire";
863
					} else if (iFace.getDesiredCompatibilityLevel().equalsIgnoreCase("driver")) {
864
						apiTypology = "driver";
865
					}
866
					
867
						
868
					stm = conn.prepareStatement("UPDATE api SET typology = ?, edited = ?, editedprotocolclass = ?, useedited = ?  WHERE datasource = ? AND id = ? ");
869
					stm.setString(1, apiTypology);
870
					if (edited) {
871
						stm.setString(2, iFace.getBaseUrl());
872
						stm.setString(3, iFace.getAccessProtocol());
873
						stm.setBoolean(4, true);
874
					} else {
875
						stm.setString(2, null);
876
						stm.setString(3, null);
877
						stm.setBoolean(4, false);
878
					}
879
					stm.setString(5, repo.getId());
880
					stm.setString(6, iFace.getId());
881
					
882
					if (stm.executeUpdate() > 0 ) {
883
						logger.debug("finished updating values in api..");
884
						stm.close();
885
						conn.commit();
886
					}
887
				logger.debug("updating apicollections..");
888
				
889
				logger.debug("deleting current values from apicollections..");
890
				stm = conn.prepareStatement("DELETE FROM apicollections WHERE api = ?");
891
				
892
				stm.setString(1, iFace.getId());
893
				
894
				if (stm.executeUpdate() > 0) {
895
					logger.debug("values for interface " + iFace.getId() + " deleted from api_collections..");
896
					stm.close();
897
					rs.close();
898
					conn.commit();
899
					
900
				}
901
					logger.debug("now inserting in apicollections..");
902

    
903
					stm = conn.prepareStatement("INSERT INTO apicollections (accessparam, accessvalue, api, _dnet_resource_identifier_)  VALUES (?,?,?,?)");
904
					
905
					stm.setString(1,"FORMAT");
906
					stm.setString(2, iFace.getAccessFormat());
907
					stm.setString(3, iFace.getId());
908
					stm.setString(4,"FORMAT" + "@@" + iFace.getAccessFormat() + "@@" + iFace.getId());
909
					
910
					stm.addBatch();
911
					
912
					stm.setString(1,"SET");
913
					stm.setString(2, iFace.getAccessSet());
914
					stm.setString(3, iFace.getId());
915
					stm.setString(4,"SET" + "@@" + iFace.getAccessSet() + "@@" + iFace.getId());
916
						
917
					stm.addBatch();
918
					
919
					if (stm.executeBatch()[0] > 0) {
920
						logger.debug(iFace.getId() + " inserted in apicollections.");
921
					}
922
					logger.debug("finished updating set + format values in apicollections.");
923
					stm.close();
924
					conn.commit();
925
			
926
			}	
927
			logger.debug("interface : " + iFace.getId() + " inserted successfully..");
928
		}			
929
			
930
	} else if (datatype.equalsIgnoreCase("journal")||datatype.equalsIgnoreCase("aggregator")){		
931

    
932
		try {								
933
				stm = conn.prepareStatement("UPDATE datasources SET id = ?, officialname = ?, englishname = ?, websiteurl = ?, logourl = ?, contactemail = ?, latitude = ?, longitude = ?, timezone = ?, namespaceprefix = ?, collectedfrom = ?, typology = ?, datasourceclass = ?, provenanceactionclass = ?, _dnet_resource_identifier_ = ? WHERE officialname = ? AND id = ?");
934
				
935
				stm.setString(1, repo.getId()); //activation id
936
				stm.setString(2, repo.getOfficialName());
937
				stm.setString(3, repo.getEnglishName());
938
				stm.setString(4, repo.getWebsiteUrl());
939
				stm.setString(5, repo.getLogoUrl());
940
				stm.setString(6, repo.getContactEmail());
941
				stm.setDouble(7, repo.getLatitude());
942
				stm.setDouble(8, repo.getLongitude());
943
				stm.setString(9, repo.getTimezone() + "");
944
				stm.setString(10, repo.getNamespacePrefix());
945
				stm.setString(11, repo.getCollectedFrom()); 
946
				stm.setString(12, repo.getTypology()); 
947
				stm.setString(13, repo.getDatasourceClass());
948
				stm.setString(14, repo.getProvenanceActionClass());
949
				stm.setString(15,repo.getId());
950
				stm.setString(16,officialNameOld);
951
				stm.setString(17,idOld);
952
	
953
				
954
				stm.executeUpdate();
955
				logger.debug(datatype + " " + repo.getOfficialName() + " inserted in datasources.");
956
				stm.close();
957
				conn.commit();
958

    
959
				logger.debug("checking current values in api..");
960
				
961
				stm = conn.prepareStatement("SELECT COUNT(*) FROM api WHERE datasource = ?");
962
				stm.setString(1, repo.getId());
963
				rs = stm.executeQuery();
964
				int noCurrentInApi = 0;
965
				if (rs.next()) {
966
					noCurrentInApi = rs.getInt(1);
967
				}
968
				stm.close();
969
				rs.close();
970
				
971
				
972
				for (RepositoryInterface iFace : repo.getInterfaces()) {
973
					logger.debug("checking if interface : " + iFace.getId() + " already exists in api..");
974
					stm = conn.prepareStatement("SELECT * FROM api WHERE datasource = ? AND id = ?");
975
					stm.setString(1, repo.getId());
976
					stm.setString(2, iFace.getId());
977
					rs = stm.executeQuery();
978
					
979
					if (!rs.next()) {
980
						logger.debug("not exist..");
981
						stm.close();
982
						rs.close();
983
						
984
						stm = conn.prepareStatement("INSERT INTO api (id, typology, originalprotocolclass, original, datasource, _dnet_resource_identifier_)  VALUES (?,?,?,?,?,?)");
985
						iFace.setId("api_________::" + repo.getId() + "::" + noCurrentInApi);
986
						logger.debug("creating new api_id: " + iFace.getId());
987
						noCurrentInApi++;
988
						
989
						String apiTypology = "UNKNOWN";
990
						if (iFace.getDesiredCompatibilityLevel().equalsIgnoreCase("openaire3.0") || iFace.getDesiredCompatibilityLevel().equalsIgnoreCase("openaire2.0")) {
991
							apiTypology = "openaire";
992
						} else if (iFace.getDesiredCompatibilityLevel().equalsIgnoreCase("driver")) {
993
							apiTypology = "driver";
994
						}
995
						stm.setString(1, iFace.getId());
996
						stm.setString(2, apiTypology);
997
						stm.setString(3, iFace.getAccessProtocol());
998
						stm.setString(4, iFace.getBaseUrl());
999
						stm.setString(5, repo.getId());
1000
						stm.setString(6, iFace.getId());
1001
						
1002
						if (stm.executeUpdate() > 0) {
1003
							logger.debug(iFace.getId() + " inserted in api..");
1004
							logger.debug("now inserting in apicollections..");
1005
							stm.close();
1006
							rs.close();
1007
							conn.commit();
1008
		
1009
							stm = conn.prepareStatement("INSERT INTO apicollections (accessparam, accessvalue, api, _dnet_resource_identifier_)  VALUES (?,?,?,?)");
1010
							
1011
							stm.setString(1,"FORMAT");
1012
							stm.setString(2, iFace.getAccessFormat());
1013
							stm.setString(3, iFace.getId());
1014
							stm.setString(4,"FORMAT" + "@@" + iFace.getAccessFormat() + "@@" + iFace.getId());
1015
							
1016
							stm.addBatch();
1017
	
1018
							stm.setString(1,"SET");
1019
							stm.setString(2, iFace.getAccessSet());
1020
							stm.setString(3, iFace.getId());
1021
							stm.setString(4,"SET" + "@@" + iFace.getAccessSet() + "@@" + iFace.getId());
1022
								
1023
							stm.addBatch();
1024
							
1025
							if (stm.executeBatch()[0] > 0) {
1026
								logger.debug(iFace.getId() + " inserted in apicollections.");
1027
							}
1028
							logger.debug("finished updating set + format values in apicollections.");
1029
							stm.close();
1030
							conn.commit();
1031
						}
1032
							
1033
					} else {
1034
						
1035
						logger.debug("exists..");
1036
						boolean edited = false;
1037
						if( rs.getString("original") == null || rs.getString("originalprotocolclass") == null || !rs.getString("original").equalsIgnoreCase(iFace.getBaseUrl()) || !rs.getString("originalprotocolclass").equalsIgnoreCase(iFace.getAccessProtocol()) ) {
1038
							edited = true;
1039
						}
1040
							logger.debug("updating edited values in api..");
1041
							stm.close();
1042
							rs.close();
1043
							
1044
							String apiTypology = "UNKNOWN";
1045
							if (iFace.getDesiredCompatibilityLevel().equalsIgnoreCase("openaire3.0") || iFace.getDesiredCompatibilityLevel().equalsIgnoreCase("openaire2.0")) {
1046
								apiTypology = "openaire";
1047
							} else if (iFace.getDesiredCompatibilityLevel().equalsIgnoreCase("driver")) {
1048
								apiTypology = "driver";
1049
							}
1050
							
1051
							
1052
							stm = conn.prepareStatement("UPDATE api SET typology = ?, edited = ?, editedprotocolclass = ?, useedited = ?  WHERE datasource = ? AND id = ? ");
1053
							stm.setString(1, apiTypology);
1054
							if (edited) {
1055
								stm.setString(2, iFace.getBaseUrl());
1056
								stm.setString(3, iFace.getAccessProtocol());
1057
								stm.setBoolean(4, true);
1058
							} else {
1059
								stm.setString(2, null);
1060
								stm.setString(3, null);
1061
								stm.setBoolean(4, false);
1062
							}
1063
							stm.setString(5, repo.getId());
1064
							stm.setString(6, iFace.getId());
1065
							
1066
							if (stm.executeUpdate() > 0 ) {
1067
								logger.debug("finished updating values in api..");
1068
								stm.close();
1069
								conn.commit();
1070
							}
1071
						logger.debug("updating apicollections..");
1072
						
1073
						logger.debug("deleting current values from apicollections..");
1074
						stm = conn.prepareStatement("DELETE FROM apicollections WHERE api = ?");
1075
						
1076
						stm.setString(1, iFace.getId());
1077
						
1078
						if (stm.executeUpdate() > 0) {
1079
							logger.debug("values for interface " + iFace.getId() + " deleted from api_collections..");
1080
							stm.close();
1081
							rs.close();
1082
							conn.commit();
1083
							
1084
						}
1085
							logger.debug("now inserting in apicollections..");
1086
		
1087
							stm = conn.prepareStatement("INSERT INTO apicollections (accessparam, accessvalue, api, _dnet_resource_identifier_)  VALUES (?,?,?,?)");
1088
							
1089
							stm.setString(1,"FORMAT");
1090
							stm.setString(2, iFace.getAccessFormat());
1091
							stm.setString(3, iFace.getId());
1092
							stm.setString(4,"FORMAT" + "@@" + iFace.getAccessFormat() + "@@" + iFace.getId());
1093
							
1094
							stm.addBatch();
1095
	
1096
							stm.setString(1,"SET");
1097
							stm.setString(2, iFace.getAccessSet());
1098
							stm.setString(3, iFace.getId());
1099
							stm.setString(4,"SET" + "@@" + iFace.getAccessSet() + "@@" + iFace.getId());
1100
								
1101
							stm.addBatch();
1102
							
1103
							if (stm.executeBatch()[0] > 0) {
1104
								logger.debug(iFace.getId() + " inserted in apicollections.");
1105
							}
1106
							logger.debug("finished updating set + format values in apicollections.");
1107
							stm.close();
1108
							conn.commit();
1109
					
1110
					}	
1111
					logger.debug("interface : " + iFace.getId() + " inserted successfully..");
1112
				}
1113

    
1114
					logger.debug("inserting in organizations..");
1115
					stm.close();
1116
					conn.commit();
1117
					
1118
					stm = conn.prepareStatement("SELECT * FROM organizations WHERE id = ?");
1119
					stm.setString(1, "openaire____::" + Base64.encode(repo.getOrganization()));
1120

    
1121
					ResultSet rs1 = stm.executeQuery();
1122
					boolean exists = rs1.next();
1123
					
1124
					rs1.close();
1125
					
1126
					if(!exists) {
1127
						
1128
						stm = conn.prepareStatement("SELECT code FROM class WHERE name = ?");
1129
						stm.setString(1,repo.getCountryName());
1130
						
1131
						rs1 = stm.executeQuery();
1132
						
1133
						String countryCode = "UNKNOWN";
1134
						if ( rs1.next()) {
1135
							countryCode = rs1.getString(1);
1136
						stm.close();
1137
						}
1138
						stm = conn.prepareStatement("INSERT INTO organizations (legalname, countryclass, id, legalshortname, _dnet_resource_identifier_) VALUES (?,?,?,?,?)");
1139
						stm.setString(1, repo.getOrganization());
1140
						stm.setString(2, countryCode);
1141
						stm.setString(3, "openaire____::" + Base64.encode(repo.getOrganization()));
1142
						stm.setString(4, repo.getOrganization());
1143
						stm.setString(5, "openaire____::" + Base64.encode(repo.getOrganization()));
1144
						stm.executeUpdate();
1145
						logger.debug("organization" + "openaire____::" + Base64.encode(repo.getOrganization()) + " inserted in organizations.");
1146
		
1147
						stm.close();
1148
						conn.commit();
1149
					} else {
1150
						logger.debug("Organization "+repo.getOrganization()+" already exists");
1151
					}
1152
					
1153
						logger.debug("inserting in datasource-organizations..");
1154
						stm = conn.prepareStatement("UPDATE datasource_organization SET organization = ? , datasource = ? , _dnet_resource_identifier_ = ? WHERE datasource = ? ");
1155
						stm.setString(1, "openaire____::" + Base64.encode(repo.getOrganization()));
1156
						stm.setString(2, repo.getId());
1157
						stm.setString(3, repo.getId() + "@@" + "openaire____::" + Base64.encode(repo.getOrganization()) );
1158
						stm.setString(4, idOld);
1159
	
1160
						if( stm.executeUpdate()>0) {
1161
							logger.debug("organization" + "openaire____::" + Base64.encode(repo.getOrganization()) + " inserted in datasource-organizations.");
1162
							stm.close();
1163
							
1164
							conn.commit();
1165
						}		
1166

    
1167
		logger.debug("Finished updating " + datatype + " repository..");
1168
		
1169
		} catch (SQLException e) {
1170
			logger.error("Error updating repo " + repo.getOfficialName() + " from openaire db", e);
1171
			throw e;
1172
		} finally {
1173
			if (conn != null)
1174
				try {
1175
					conn.close();
1176
				} catch (SQLException e) {
1177
					logger.error("Error closing connection", e);
1178
				}
1179
		}
1180
	}
1181
	return retMessage;
1182

    
1183
	}
1184

    
1185
	@Override
1186
	public boolean updateRepositoryInterfaceCompliance(String officialName, String datasourceId,
1187
			String interfaceId, String compliance) throws Exception {
1188
		boolean ret = true;
1189
		Connection conn = null;
1190
		try {
1191
			
1192
			logger.debug("updating repository " + officialName + " compliance to : " + compliance);
1193
			conn = openAireDataSource.getConnection();
1194
			PreparedStatement stm = conn.prepareStatement("UPDATE datasources SET openairecompatibilityclass=?, dateofvalidation=?, activationid = ? , openairecompatibilityscheme = ? WHERE id = ?");
1195

    
1196
			java.util.Date utilDate = new java.util.Date();
1197
			java.sql.Timestamp date = new java.sql.Timestamp(utilDate.getTime());			
1198
			Map<String, String> compMap = this.getRepoCompatibility(officialName, datasourceId);
1199
			
1200
			String newCompliance = ""; 
1201
			if (compliance.equalsIgnoreCase("openaire2.0")) {
1202
				if ( compMap.containsValue("driver") && compMap.containsValue("openaire") )
1203
					newCompliance = "driver-openaire2.0-openaire3.0";
1204
				else if ( compMap.containsValue("driver") )
1205
					newCompliance = "driver-openaire2.0";
1206
				else if ( compMap.containsValue("openaire") )
1207
					newCompliance = "openaire2.0-openaire3.0";
1208
				else 
1209
					newCompliance = "openaire2.0";			
1210
			} else if (compliance.equalsIgnoreCase("driver")) {
1211
				if ( compMap.containsValue("ec_fundedresources") && compMap.containsValue("openaire") )
1212
					newCompliance = "driver-openaire2.0-openaire3.0";
1213
				else if ( compMap.containsValue("ec_fundedresources") )
1214
					newCompliance = "driver-openaire2.0";
1215
				else if ( compMap.containsValue("openaire") )
1216
					newCompliance = "driver-openaire3.0";
1217
				else 
1218
					newCompliance = "driver";	
1219
			} else if (compliance.equalsIgnoreCase("openaire3.0")) {
1220
				if ( compMap.containsValue("ec_fundedresources") && compMap.containsValue("driver") )
1221
					newCompliance = "driver-openaire2.0-openaire3.0";
1222
				else if ( compMap.containsValue("ec_fundedresources") )
1223
					newCompliance = "openaire2.0-openaire3.0";
1224
				else if ( compMap.containsValue("driver") )
1225
					newCompliance = "driver-openaire3.0";
1226
				else 
1227
					newCompliance = "openaire3.0";
1228
			} else if (compliance.equalsIgnoreCase("openaire3.0_data")) {
1229
				newCompliance = "openaire3.0";
1230
			} else if (compliance.equalsIgnoreCase("notCompatible")) {
1231
				if ( compMap.containsValue("ec_fundedresources") && compMap.containsValue("driver") && compMap.containsValue("openaire"))
1232
					newCompliance = "driver-openaire2.0-openaire3.0";
1233
				else if ( compMap.containsValue("ec_fundedresources") && compMap.containsValue("driver") )
1234
					newCompliance = "driver-openaire2.0";
1235
				else if ( compMap.containsValue("openaire") )
1236
					newCompliance = "openaire3.0";
1237
				else if ( compMap.containsValue("ec_fundedresources") )
1238
					newCompliance = "openaire2.0";
1239
				else if ( compMap.containsValue("driver") )
1240
					newCompliance = "driver";
1241
				else 
1242
					newCompliance = "notCompatible";		
1243
			}
1244
			
1245
			stm.setString(1, newCompliance);
1246
			stm.setTimestamp(2,date);
1247
			stm.setString(3, null);
1248
			stm.setString(4, "dnet:compatibilityLevel");
1249
			stm.setString(5, datasourceId);
1250
			
1251
			stm.executeUpdate();
1252
			stm.close();
1253
			conn.commit();
1254
			logger.debug(officialName + " set as openaire compliant: " + compliance);
1255
		} catch (SQLException e) {
1256
			 logger.error("error connecting to openaire db to set a repo as openaire compliant " + officialName, e);
1257
			 	ret = false;
1258
				throw e;
1259
			}  
1260
			finally {
1261
				if (conn != null)
1262
					try {
1263
						conn.close();
1264
					} catch (SQLException e) {
1265
						ret = false;
1266
						logger.error("Error closing connection", e);
1267
					}
1268
			}
1269
		return ret;
1270
	}
1271

    
1272
	@Override
1273
	public boolean insertPubFileInterface(String dsId, RepositoryInterface iFace)
1274
			throws Exception {
1275
		// TODO Auto-generated method stub
1276
		return false;
1277
	}
1278

    
1279
	@Override
1280
	public List<Repository> getRepositories(String collectedFrom)
1281
			throws Exception {
1282
		// TODO Auto-generated method stub
1283
		return null;
1284
	}
1285
}
(2-2/3)