Project

General

Profile

Download (6.21 KB) Statistics
| Branch: | Tag: | Revision:
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

    
15
import org.apache.log4j.Logger;
16
import org.hibernate.SessionFactory;
17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.transaction.PlatformTransactionManager;
19
import org.springframework.transaction.TransactionDefinition;
20
import org.springframework.transaction.TransactionStatus;
21
import org.springframework.transaction.support.DefaultTransactionDefinition;
22

    
23
import eu.etaxonomy.cdm.api.application.CdmApplicationDefaultConfiguration;
24
import eu.etaxonomy.cdm.common.IProgressMonitor;
25
import eu.etaxonomy.cdm.model.common.CdmBase;
26

    
27
/**
28
 * @author a.mueller
29
 * @created 01.07.2008
30
 * @version 1.0
31
 */
32
public abstract class CdmIoBase<STATE extends IoStateBase> extends CdmApplicationDefaultConfiguration implements ICdmIO<STATE> {
33
	private static final Logger logger = Logger.getLogger(CdmIoBase.class);
34

    
35
	protected String ioName = null;
36

    
37
	protected abstract boolean doInvoke(STATE state);
38
	
39
	public int countSteps(){
40
		return 1;
41
	}
42

    
43

    
44
	/* (non-Javadoc)
45
	 * @see eu.etaxonomy.cdm.io.common.ICdmExport#invoke(eu.etaxonomy.cdm.io.common.ExportStateBase)
46
	 */
47
	public boolean invoke(STATE state) {
48
		if (isIgnore( state)){
49
			logger.warn("No invoke for " + ioName + " (ignored)");
50
			return true;
51
		}else{
52
			updateProgress(state, "Invoking " + ioName);
53
			return doInvoke(state);
54
		}
55
	}
56
	
57
	@Autowired
58
	SessionFactory sessionFactory;
59
	
60
	public void flush() {		
61
		sessionFactory.getCurrentSession().flush();
62
	}
63
	
64
	public TransactionStatus startTransaction() {
65
		return startTransaction(false);
66
	}
67
	
68
	public TransactionStatus startTransaction(Boolean readOnly) {
69
		
70
		DefaultTransactionDefinition defaultTxDef = new DefaultTransactionDefinition();
71
		defaultTxDef.setReadOnly(readOnly);
72
		TransactionDefinition txDef = defaultTxDef;
73

    
74
		// Log some transaction-related debug information.
75
		if (logger.isDebugEnabled()) { 
76
			logger.debug("Transaction name = " + txDef.getName());
77
			logger.debug("Transaction facets:");
78
			logger.debug("Propagation behavior = " + txDef.getPropagationBehavior());
79
			logger.debug("Isolation level = " + txDef.getIsolationLevel());
80
			logger.debug("Timeout = " + txDef.getTimeout());
81
			logger.debug("Read Only = " + txDef.isReadOnly());
82
			// org.springframework.orm.hibernate3.HibernateTransactionManager
83
			// provides more transaction/session-related debug information.
84
		}
85
		
86
		TransactionStatus txStatus = super.getTransactionManager().getTransaction(txDef);
87
		return txStatus;
88
	}
89

    
90
	public void commitTransaction(TransactionStatus txStatus){
91
		PlatformTransactionManager txManager = super.getTransactionManager();
92
		txManager.commit(txStatus);
93
		return;
94
	}
95
	
96
	/**
97
	 * 
98
	 */
99
	public CdmIoBase() {
100
		super();
101
		this.ioName = this.getClass().getSimpleName();
102
	}
103

    
104
	/* (non-Javadoc)
105
	 * @see eu.etaxonomy.cdm.io.common.ICdmIO#check(eu.etaxonomy.cdm.io.common.IIoConfigurator)
106
	 */
107
	public boolean check(STATE state) {
108
		if (isIgnore(state)){
109
			logger.warn("No check for " + ioName + " (ignored)");
110
			return true;
111
		}else{
112
			return doCheck(state);
113
		}
114
	}
115
	
116
	protected abstract boolean doCheck(STATE state);
117

    
118
	
119
	/* (non-Javadoc)
120
	 * @see eu.etaxonomy.cdm.io.common.ICdmIO#invoke(eu.etaxonomy.cdm.io.common.IIoConfigurator, java.util.Map)
121
	 */
122
//	public boolean invoke(T config,
123
//			Map<String, MapWrapper<? extends CdmBase>> stores) {
124
//		if (isIgnore(config)){
125
//			logger.warn("No invoke for " + ioName + " (ignored)");
126
//			return true;
127
//		}else{
128
//			return doInvoke(config, stores);
129
//		}
130
//	}
131
	
132
//	protected abstract boolean doInvoke(T config,
133
//			Map<String, MapWrapper<? extends CdmBase>> stores);
134

    
135
	
136
	/**
137
	 * Returns true if this (IO-)class should be ignored during the import/export process.
138
	 * This information is usually stored in the configuration
139
	 * @param config
140
	 * @return
141
	 */
142
	protected abstract boolean isIgnore(STATE state);
143

    
144
	protected <T extends CdmBase> T getInstance(Class<? extends T> clazz){
145
		T result = null;
146
		try {
147
			Constructor<? extends T> constructor = clazz.getDeclaredConstructor();
148
			constructor.setAccessible(true);
149
			result = constructor.newInstance();
150
		} catch (InstantiationException e) {
151
			logger.error("Class " + clazz.getSimpleName()+" could not be instantiated. Class = " );
152
			e.printStackTrace();
153
		} catch (IllegalAccessException e) {
154
			logger.error("Constructor of class "+clazz.getSimpleName()+" could not be accessed." );
155
			e.printStackTrace();
156
		} catch (SecurityException e) {
157
			e.printStackTrace();
158
		} catch (NoSuchMethodException e) {
159
			logger.error("SecurityException for Constructor of class "+clazz.getSimpleName()+"." );
160
			e.printStackTrace();
161
		} catch (IllegalArgumentException e) {
162
			logger.error("Empty Constructor does not exist for class "+clazz.getSimpleName()+"." );
163
			e.printStackTrace();
164
		} catch (InvocationTargetException e) {
165
			logger.error("Empty Constructor could not be invoked for class "+clazz.getSimpleName()+"." );
166
			e.printStackTrace();
167
		}
168
		return result;
169
	}
170
	
171
	
172
	protected String getSuccessString(boolean success){
173
		if (success){
174
			return "with success";
175
		}else{
176
			return "with errors";
177
		}
178
	}
179

    
180
	@Override
181
	public void updateProgress(STATE state, String message) {
182
		updateProgress(state, message, 1);
183
	};
184
	
185
	@Override
186
	public void updateProgress(STATE state, String message, int worked) {
187
		IProgressMonitor progressMonitor = state.getConfig().getProgressMonitor();
188
		if(progressMonitor != null){
189
			progressMonitor.worked(worked);
190
			progressMonitor.subTask(message);
191
		}
192
	}
193
	
194
	@Override
195
	public void warnProgress(STATE state, String message, Throwable e) {
196
		if(state.getConfig().getProgressMonitor() != null){
197
			IProgressMonitor monitor = state.getConfig().getProgressMonitor();
198
			if (e == null) {
199
				monitor.warning(message);
200
			}else{
201
				monitor.warning(message, e);
202
			}
203
		}
204
	};
205
}
(10-10/45)