2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.common
;
12 import java
.lang
.reflect
.Constructor
;
13 import java
.lang
.reflect
.InvocationTargetException
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.hibernate
.SessionFactory
;
18 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
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
;
24 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationDefaultConfiguration
;
25 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
32 public abstract class CdmIoBase
<T
extends IIoConfigurator
> extends CdmApplicationDefaultConfiguration
implements ICdmIO
<T
> {
33 private static Logger logger
= Logger
.getLogger(CdmIoBase
.class);
35 protected String ioName
= null;
38 SessionFactory sessionFactory
;
41 sessionFactory
.getCurrentSession().flush();
44 public TransactionStatus
startTransaction() {
46 return startTransaction(false);
49 public TransactionStatus
startTransaction(Boolean readOnly
) {
51 DefaultTransactionDefinition defaultTxDef
= new DefaultTransactionDefinition();
52 defaultTxDef
.setReadOnly(readOnly
);
53 TransactionDefinition txDef
= defaultTxDef
;
55 // Log some transaction-related debug information.
56 if (logger
.isDebugEnabled()) {
57 logger
.debug("Transaction name = " + txDef
.getName());
58 logger
.debug("Transaction facets:");
59 logger
.debug("Propagation behavior = " + txDef
.getPropagationBehavior());
60 logger
.debug("Isolation level = " + txDef
.getIsolationLevel());
61 logger
.debug("Timeout = " + txDef
.getTimeout());
62 logger
.debug("Read Only = " + txDef
.isReadOnly());
63 // org.springframework.orm.hibernate3.HibernateTransactionManager
64 // provides more transaction/session-related debug information.
67 TransactionStatus txStatus
= super.getTransactionManager().getTransaction(txDef
);
71 public void commitTransaction(TransactionStatus txStatus
){
72 PlatformTransactionManager txManager
= super.getTransactionManager();
73 txManager
.commit(txStatus
);
82 this.ioName
= this.getClass().getSimpleName();
86 * @see eu.etaxonomy.cdm.io.common.ICdmIO#check(eu.etaxonomy.cdm.io.common.IIoConfigurator)
88 public boolean check(T config
) {
89 if (isIgnore(config
)){
90 logger
.warn("No check for " + ioName
+ " (ignored)");
93 return doCheck(config
);
97 protected abstract boolean doCheck(T config
);
101 * @see eu.etaxonomy.cdm.io.common.ICdmIO#invoke(eu.etaxonomy.cdm.io.common.IIoConfigurator, java.util.Map)
103 public boolean invoke(T config
,
104 Map
<String
, MapWrapper
<?
extends CdmBase
>> stores
) {
105 if (isIgnore(config
)){
106 logger
.warn("No invoke for " + ioName
+ " (ignored)");
109 return doInvoke(config
, stores
);
114 // * @see eu.etaxonomy.cdm.io.common.ICdmIO#invoke(eu.etaxonomy.cdm.io.common.IIoConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
116 // public boolean invoke(T config,
117 //// public boolean invoke(IIoConfigurator config,
118 // Map<String, MapWrapper<? extends CdmBase>> stores) {
119 // if (isIgnore(config)){
120 // logger.warn("No invoke for " + ioName + " (ignored)");
123 // return doInvoke(config, stores);
129 protected abstract boolean doInvoke(T config
,
130 Map
<String
, MapWrapper
<?
extends CdmBase
>> stores
);
134 * Returns true if this (IO-)class should be ignored during the import/export process.
135 * This information is usually stored in the configuration
139 protected abstract boolean isIgnore(T config
);
141 protected <T
extends CdmBase
> T
getInstance(Class
<?
extends T
> clazz
){
144 Constructor
<?
extends T
> constructor
= clazz
.getDeclaredConstructor();
145 constructor
.setAccessible(true);
146 result
= constructor
.newInstance();
147 } catch (InstantiationException e
) {
148 logger
.error("Class " + clazz
.getSimpleName()+" could not be instantiated. Class = " );
150 } catch (IllegalAccessException e
) {
151 logger
.error("Constructor of class "+clazz
.getSimpleName()+" could not be accessed." );
153 } catch (SecurityException e
) {
155 } catch (NoSuchMethodException e
) {
156 logger
.error("SecurityException for Constructor of class "+clazz
.getSimpleName()+"." );
158 } catch (IllegalArgumentException e
) {
159 logger
.error("Empty Constructor does not exist for class "+clazz
.getSimpleName()+"." );
161 } catch (InvocationTargetException e
) {
162 logger
.error("Empty Constructor could not be invoked for class "+clazz
.getSimpleName()+"." );