1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.io.common;
|
11
|
|
12
|
import java.lang.reflect.Constructor;
|
13
|
import java.lang.reflect.InvocationTargetException;
|
14
|
import java.util.HashSet;
|
15
|
import java.util.Set;
|
16
|
|
17
|
import org.apache.commons.lang.StringUtils;
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.springframework.transaction.PlatformTransactionManager;
|
20
|
import org.springframework.transaction.TransactionDefinition;
|
21
|
import org.springframework.transaction.TransactionStatus;
|
22
|
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
23
|
|
24
|
import eu.etaxonomy.cdm.api.application.CdmRepository;
|
25
|
import eu.etaxonomy.cdm.common.IoResultBase;
|
26
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
27
|
import eu.etaxonomy.cdm.io.common.events.IIoEvent;
|
28
|
import eu.etaxonomy.cdm.io.common.events.IIoObserver;
|
29
|
import eu.etaxonomy.cdm.io.common.events.IoProblemEvent;
|
30
|
import eu.etaxonomy.cdm.io.common.events.IoProgressEvent;
|
31
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
32
|
import eu.etaxonomy.cdm.persistence.dao.hibernate.common.DaoBase;
|
33
|
|
34
|
/**
|
35
|
* @author a.mueller
|
36
|
* @since 01.07.2008
|
37
|
*/
|
38
|
public abstract class CdmIoBase<STATE extends IoStateBase, RESULT extends IoResultBase>
|
39
|
extends CdmRepository
|
40
|
implements ICdmIO<STATE>, IIoObservable {
|
41
|
|
42
|
private static final long serialVersionUID = -2216451655392574659L;
|
43
|
private static final Logger logger = Logger.getLogger(CdmIoBase.class);
|
44
|
|
45
|
private final Set<IIoObserver> observers = new HashSet<>();
|
46
|
protected String ioName = null;
|
47
|
|
48
|
public static final boolean NO_UNPUBLISHED = DaoBase.NO_UNPUBLISHED;
|
49
|
public static final boolean INCLUDE_UNPUBLISHED = DaoBase.INCLUDE_UNPUBLISHED;
|
50
|
|
51
|
protected CdmIoBase() {
|
52
|
this.ioName = this.getClass().getSimpleName();
|
53
|
}
|
54
|
|
55
|
//******************** Observers *********************************************************
|
56
|
|
57
|
@Override
|
58
|
public boolean addObserver(IIoObserver observer){
|
59
|
return observers.add(observer);
|
60
|
}
|
61
|
|
62
|
@Override
|
63
|
public Set<IIoObserver> getObservers() {
|
64
|
return observers;
|
65
|
}
|
66
|
|
67
|
@Override
|
68
|
public void addObservers(Set<IIoObserver> newObservers) {
|
69
|
for (IIoObserver observer : newObservers){
|
70
|
this.observers.add(observer);
|
71
|
}
|
72
|
}
|
73
|
|
74
|
@Override
|
75
|
public int countObservers(){
|
76
|
return observers.size();
|
77
|
}
|
78
|
|
79
|
@Override
|
80
|
public boolean removeObserver(IIoObserver observer){
|
81
|
return observers.remove(observer);
|
82
|
}
|
83
|
|
84
|
@Override
|
85
|
public void removeObservers(){
|
86
|
observers.removeAll(observers);
|
87
|
}
|
88
|
|
89
|
@Override
|
90
|
public void fire(IIoEvent event){
|
91
|
for (IIoObserver observer: observers){
|
92
|
observer.handleEvent(event);
|
93
|
}
|
94
|
}
|
95
|
|
96
|
//******************** End Observers *********************************************************
|
97
|
|
98
|
public void invoke(STATE state) {
|
99
|
if (isIgnore(state)){
|
100
|
logger.info("No invoke for " + ioName + " (ignored)");
|
101
|
// return getNoDataResult(state);
|
102
|
}else{
|
103
|
updateProgress(state, "Invoking " + ioName);
|
104
|
doInvoke(state);
|
105
|
}
|
106
|
}
|
107
|
|
108
|
protected abstract RESULT getNoDataResult(STATE state);
|
109
|
protected abstract RESULT getDefaultResult(STATE state);
|
110
|
|
111
|
|
112
|
@Override
|
113
|
public long countSteps(STATE state){
|
114
|
return 1;
|
115
|
}
|
116
|
|
117
|
// @Autowired
|
118
|
// private SessionFactory sessionFactory;
|
119
|
|
120
|
// /**
|
121
|
// * flush the current session
|
122
|
// */
|
123
|
// //TODO move into super class CdmApplicationDefaultConfiguration#flush() ?
|
124
|
// public void flush(TransactionStatus txStatus) {
|
125
|
// super.getTransactionManager().getTransaction(txStatus.f).flush();
|
126
|
//// sessionFactory.getCurrentSession().flush();
|
127
|
// }
|
128
|
|
129
|
@Override
|
130
|
//TODO seems to be exact duplicate of CdmApplicationDefaultConfiguration#startTransaction(), remove duplicate
|
131
|
public TransactionStatus startTransaction() {
|
132
|
return startTransaction(false);
|
133
|
}
|
134
|
|
135
|
@Override
|
136
|
//TODO seems to be exact duplicate of CdmApplicationDefaultConfiguration#startTransaction(java.lang.Boolean)
|
137
|
public TransactionStatus startTransaction(Boolean readOnly) {
|
138
|
|
139
|
DefaultTransactionDefinition defaultTxDef = new DefaultTransactionDefinition();
|
140
|
defaultTxDef.setReadOnly(readOnly);
|
141
|
TransactionDefinition txDef = defaultTxDef;
|
142
|
|
143
|
// Log some transaction-related debug information.
|
144
|
if (logger.isDebugEnabled()) {
|
145
|
logger.debug("Transaction name = " + txDef.getName());
|
146
|
logger.debug("Transaction facets:");
|
147
|
logger.debug("Propagation behavior = " + txDef.getPropagationBehavior());
|
148
|
logger.debug("Isolation level = " + txDef.getIsolationLevel());
|
149
|
logger.debug("Timeout = " + txDef.getTimeout());
|
150
|
logger.debug("Read Only = " + txDef.isReadOnly());
|
151
|
// org.springframework.orm.hibernate5.HibernateTransactionManager
|
152
|
// provides more transaction/session-related debug information.
|
153
|
}
|
154
|
|
155
|
TransactionStatus txStatus = super.getTransactionManager().getTransaction(txDef);
|
156
|
return txStatus;
|
157
|
}
|
158
|
|
159
|
@Override
|
160
|
//TODO seems to be exact duplicate of CdmApplicationDefaultConfiguration#startTransaction(java.lang.Boolean), remove duplicate?
|
161
|
public void commitTransaction(TransactionStatus txStatus){
|
162
|
PlatformTransactionManager txManager = super.getTransactionManager();
|
163
|
txManager.commit(txStatus);
|
164
|
return;
|
165
|
}
|
166
|
|
167
|
//TODO move into super class CdmApplicationDefaultConfiguration#startTransaction(java.lang.Boolean)
|
168
|
//==> no
|
169
|
@Override
|
170
|
public void rollbackTransaction(TransactionStatus txStatus){
|
171
|
PlatformTransactionManager txManager = super.getTransactionManager();
|
172
|
txManager.rollback(txStatus);
|
173
|
return;
|
174
|
}
|
175
|
|
176
|
@Override
|
177
|
public boolean check(STATE state) {
|
178
|
if (isIgnore(state)){
|
179
|
logger.info("No check for " + ioName + " (ignored)");
|
180
|
return true;
|
181
|
}else{
|
182
|
return doCheck(state);
|
183
|
}
|
184
|
}
|
185
|
|
186
|
protected abstract boolean doCheck(STATE state);
|
187
|
|
188
|
/**
|
189
|
* invoke method to be implemented by implementing classes
|
190
|
* @param state
|
191
|
* @return
|
192
|
*/
|
193
|
protected abstract void doInvoke(STATE state);
|
194
|
|
195
|
/**
|
196
|
* Returns true if this (IO-)class should be ignored during the import/export process.
|
197
|
* This information is usually stored in the configuration
|
198
|
* @param config
|
199
|
* @return
|
200
|
*/
|
201
|
protected abstract boolean isIgnore(STATE state);
|
202
|
|
203
|
protected <T extends CdmBase> T getInstance(Class<? extends T> clazz){
|
204
|
T result = null;
|
205
|
try {
|
206
|
Constructor<? extends T> constructor = clazz.getDeclaredConstructor();
|
207
|
constructor.setAccessible(true);
|
208
|
result = constructor.newInstance();
|
209
|
} catch (InstantiationException e) {
|
210
|
logger.error("Class " + clazz.getSimpleName()+" could not be instantiated. Class = " );
|
211
|
e.printStackTrace();
|
212
|
} catch (IllegalAccessException e) {
|
213
|
logger.error("Constructor of class "+clazz.getSimpleName()+" could not be accessed." );
|
214
|
e.printStackTrace();
|
215
|
} catch (SecurityException e) {
|
216
|
e.printStackTrace();
|
217
|
} catch (NoSuchMethodException e) {
|
218
|
logger.error("SecurityException for Constructor of class "+clazz.getSimpleName()+"." );
|
219
|
e.printStackTrace();
|
220
|
} catch (IllegalArgumentException e) {
|
221
|
logger.error("Empty Constructor does not exist for class "+clazz.getSimpleName()+"." );
|
222
|
e.printStackTrace();
|
223
|
} catch (InvocationTargetException e) {
|
224
|
logger.error("Empty Constructor could not be invoked for class "+clazz.getSimpleName()+"." );
|
225
|
e.printStackTrace();
|
226
|
}
|
227
|
return result;
|
228
|
}
|
229
|
|
230
|
|
231
|
protected String getSuccessString(boolean success){
|
232
|
if (success){
|
233
|
return "with success";
|
234
|
}else{
|
235
|
return "with errors";
|
236
|
}
|
237
|
}
|
238
|
|
239
|
@Override
|
240
|
public void updateProgress(STATE state, String message) {
|
241
|
updateProgress(state, message, 1);
|
242
|
}
|
243
|
|
244
|
@Override
|
245
|
public void updateProgress(STATE state, String message, int worked) {
|
246
|
if (state.getConfig().getProgressMonitor() == null){
|
247
|
IProgressMonitor progressMonitor = state.getConfig().getProgressMonitor();
|
248
|
if(progressMonitor != null){
|
249
|
progressMonitor.worked(worked);
|
250
|
progressMonitor.subTask(message);
|
251
|
}
|
252
|
}
|
253
|
}
|
254
|
|
255
|
@Override
|
256
|
public void warnProgress(STATE state, String message, Throwable e) {
|
257
|
if(state.getConfig().getProgressMonitor() != null){
|
258
|
IProgressMonitor monitor = state.getConfig().getProgressMonitor();
|
259
|
if (e == null) {
|
260
|
monitor.warning(message);
|
261
|
}else{
|
262
|
monitor.warning(message, e);
|
263
|
}
|
264
|
}
|
265
|
}
|
266
|
|
267
|
protected void fireProgressEvent(String message, String location) {
|
268
|
IoProgressEvent event = new IoProgressEvent();
|
269
|
event.setThrowingClass(this.getClass());
|
270
|
event.setMessage(message);
|
271
|
event.setLocation(location);
|
272
|
// int linenumber = new Exception().getStackTrace()[0].getLineNumber();
|
273
|
fire(event);
|
274
|
}
|
275
|
|
276
|
|
277
|
protected void fireWarningEvent(String message, String dataLocation, Integer severity) {
|
278
|
fireWarningEvent(message, dataLocation, severity, 1);
|
279
|
}
|
280
|
|
281
|
protected void fireWarningEvent(String message, String dataLocation, Integer severity, int stackDepth) {
|
282
|
stackDepth++;
|
283
|
StackTraceElement[] stackTrace = new Exception().getStackTrace();
|
284
|
int lineNumber = stackTrace[stackDepth].getLineNumber();
|
285
|
String methodName = stackTrace[stackDepth].getMethodName();
|
286
|
String className = stackTrace[stackDepth].getClassName();
|
287
|
Class<?> declaringClass;
|
288
|
try {
|
289
|
declaringClass = Class.forName(className);
|
290
|
} catch (ClassNotFoundException e) {
|
291
|
declaringClass = this.getClass();
|
292
|
}
|
293
|
|
294
|
IoProblemEvent event = IoProblemEvent.NewInstance(declaringClass, message, dataLocation,
|
295
|
lineNumber, severity, methodName);
|
296
|
|
297
|
//for performance improvement one may read:
|
298
|
//http://stackoverflow.com/questions/421280/in-java-how-do-i-find-the-caller-of-a-method-using-stacktrace-or-reflection
|
299
|
// Object o = new SecurityManager().getSecurityContext();
|
300
|
|
301
|
|
302
|
fire(event);
|
303
|
}
|
304
|
|
305
|
protected static boolean isBlank(String str){
|
306
|
return StringUtils.isBlank(str);
|
307
|
}
|
308
|
|
309
|
protected static boolean isNotBlank(String str){
|
310
|
return StringUtils.isNotBlank(str);
|
311
|
}
|
312
|
|
313
|
}
|