Project

General

Profile

1
package eu.dnetlib.enabling.database;
2

    
3
import java.util.Date;
4
import java.util.concurrent.ExecutorService;
5
import java.util.concurrent.Executors;
6
import javax.xml.ws.wsaddressing.W3CEndpointReference;
7

    
8
import eu.dnetlib.enabling.database.rmi.DatabaseException;
9
import eu.dnetlib.enabling.database.rmi.DatabaseService;
10
import eu.dnetlib.enabling.resultset.XSLTMappedResultSetFactory;
11
import eu.dnetlib.enabling.tools.AbstractBaseService;
12
import org.apache.commons.logging.Log;
13
import org.apache.commons.logging.LogFactory;
14
import org.springframework.beans.factory.annotation.Required;
15

    
16
public class DatabaseServiceImpl extends AbstractBaseService implements DatabaseService {
17

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

    
20
	private DatabaseServiceCore core;
21

    
22
	private XSLTMappedResultSetFactory xsltResultsetFactory;
23
	
24
	private DatabaseBlackBoardNotificationHandler blackBoardNotificationHandler; 
25

    
26
	private ExecutorService threadPool = Executors.newCachedThreadPool();
27

    
28
	@Override
29
	public W3CEndpointReference dumpTable(final String db, final String table) throws DatabaseException {
30
		try {
31
			return core.generateResultSet(db, table, null);
32
		} catch (Throwable e) {
33
			throw new DatabaseException(e);
34
		}
35
	}
36

    
37
	@Override
38
	public W3CEndpointReference dumpTableAndLogs(final String db, final String table, final Date from, final Date until) throws DatabaseException {
39
		try {
40
			return core.generateResultSet(db, table, from, until);
41
		} catch (Throwable e) {
42
			throw new DatabaseException(e);
43
		}
44
	}
45

    
46
	@Override
47
	public void notify(final String subscriptionId, final String topic, final String isId, final String message) {
48
		log.info("Received notification " + subscriptionId + ", TOPIC: " + topic);
49
		
50
		if (topic.contains("BLACKBOARD")) {
51
			blackBoardNotificationHandler.notified(subscriptionId, topic, isId, message);
52
			return;
53
		}
54
	}
55

    
56
	@Override
57
	public void importFromEPR(final String db, final W3CEndpointReference epr, final String xslt) throws DatabaseException {
58

    
59
		final W3CEndpointReference mappedEpr = (xslt == null || xslt.isEmpty()) ?
60
				epr : xsltResultsetFactory.createMappedResultSet(epr, xslt) ;
61

    
62
		try {
63
			threadPool.execute(() -> {
64
				try {
65
					core.importFromResultset(db, mappedEpr);
66
				} catch (final Exception e) {
67
					log.error("Error in thread when importing from epr", e);
68
					throw new RuntimeException(e);
69
				}
70
			});
71
		} catch (RuntimeException e) {
72
			throw new DatabaseException(e);
73
		}
74
	}
75

    
76
	@Required
77
	public void setCore(final DatabaseServiceCore core) {
78
		this.core = core;
79
	}
80

    
81
	@Override
82
	public W3CEndpointReference searchSQL(final String db, final String sql) throws DatabaseException {
83
		try {
84
		return core.generateResultSet(db, sql);
85
		} catch (Throwable e) {
86
			throw new DatabaseException(e);
87
		}
88
	}
89
	
90
	@Override
91
	public W3CEndpointReference alternativeSearchSQL(String db, String sql, String sqlForSize) throws DatabaseException {
92
		try {
93
			return core.generateResultsetWithSize(db, sql, sqlForSize);
94
		} catch (Throwable e) {
95
			throw new DatabaseException(e);
96
		}
97
	}
98
	
99
	@Override
100
	public void updateSQL(final String db, final String sql) throws DatabaseException {
101
		core.getDbUtils().executeSql(db, sql);
102
	}	
103

    
104
	@Override
105
	public W3CEndpointReference xsltSearchSQL(final String db, final String sql, final String xslt) throws DatabaseException {
106
		try {
107
			return xsltResultsetFactory.createMappedResultSet(searchSQL(db, sql), xslt);
108
		} catch (Throwable e) {
109
			throw new DatabaseException("Error returning a transformed resultSet", e);
110
		}
111
	}
112

    
113
	@Override
114
	public W3CEndpointReference alternativeXsltSearchSQL(String db, String sql, String sqlForSize, String xslt) throws DatabaseException {
115
		try {
116
			return xsltResultsetFactory.createMappedResultSet(alternativeSearchSQL(db, sql, sqlForSize), xslt);
117
		} catch (Throwable e) {
118
			throw new DatabaseException("Error returning a transformed resultSet", e);
119
		}
120
	}
121
	
122
	
123
	@Override
124
	public boolean contains(final String db, final String table, final String column, final String value) throws DatabaseException {
125
		return core.getDbUtils().contains(db, table, column, value);
126
	}	
127

    
128
	public XSLTMappedResultSetFactory getXsltResultsetFactory() {
129
		return xsltResultsetFactory;
130
	}
131

    
132
	@Required
133
	public void setXsltResultsetFactory(final XSLTMappedResultSetFactory xsltResultsetFactory) {
134
		this.xsltResultsetFactory = xsltResultsetFactory;
135
	}
136

    
137
	public ExecutorService getThreadPool() {
138
		return threadPool;
139
	}
140

    
141
	public void setThreadPool(ExecutorService threadPool) {
142
		this.threadPool = threadPool;
143
	}
144

    
145
	public DatabaseBlackBoardNotificationHandler getBlackBoardNotificationHandler() {
146
		return blackBoardNotificationHandler;
147
	}
148

    
149
	@Required
150
	public void setBlackBoardNotificationHandler(DatabaseBlackBoardNotificationHandler blackBoardNotificationHandler) {
151
		this.blackBoardNotificationHandler = blackBoardNotificationHandler;
152
	}
153

    
154
}
(5-5/7)