Refactoring for Imports
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / CdmIoBase.java
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.Map;
15
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;
23
24 import eu.etaxonomy.cdm.api.application.CdmApplicationDefaultConfiguration;
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<T extends IIoConfigurator> extends CdmApplicationDefaultConfiguration implements ICdmIO<T> {
33 private static Logger logger = Logger.getLogger(CdmIoBase.class);
34
35 protected String ioName = null;
36
37 @Autowired
38 SessionFactory sessionFactory;
39
40 public void flush() {
41 sessionFactory.getCurrentSession().flush();
42 }
43
44 public TransactionStatus startTransaction() {
45
46 return startTransaction(false);
47 }
48
49 public TransactionStatus startTransaction(Boolean readOnly) {
50
51 DefaultTransactionDefinition defaultTxDef = new DefaultTransactionDefinition();
52 defaultTxDef.setReadOnly(readOnly);
53 TransactionDefinition txDef = defaultTxDef;
54
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.
65 }
66
67 TransactionStatus txStatus = super.getTransactionManager().getTransaction(txDef);
68 return txStatus;
69 }
70
71 public void commitTransaction(TransactionStatus txStatus){
72 PlatformTransactionManager txManager = super.getTransactionManager();
73 txManager.commit(txStatus);
74 return;
75 }
76
77 /**
78 *
79 */
80 public CdmIoBase() {
81 super();
82 this.ioName = this.getClass().getSimpleName();
83 }
84
85 /* (non-Javadoc)
86 * @see eu.etaxonomy.cdm.io.common.ICdmIO#check(eu.etaxonomy.cdm.io.common.IIoConfigurator)
87 */
88 public boolean check(T config) {
89 if (isIgnore(config)){
90 logger.warn("No check for " + ioName + " (ignored)");
91 return true;
92 }else{
93 return doCheck(config);
94 }
95 }
96
97 protected abstract boolean doCheck(T config);
98
99
100 /* (non-Javadoc)
101 * @see eu.etaxonomy.cdm.io.common.ICdmIO#invoke(eu.etaxonomy.cdm.io.common.IIoConfigurator, java.util.Map)
102 */
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)");
107 return true;
108 }else{
109 return doInvoke(config, stores);
110 }
111 }
112
113 // /* (non-Javadoc)
114 // * @see eu.etaxonomy.cdm.io.common.ICdmIO#invoke(eu.etaxonomy.cdm.io.common.IIoConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
115 // */
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)");
121 // return true;
122 // }else{
123 // return doInvoke(config, stores);
124 // }
125 // }
126
127
128
129 protected abstract boolean doInvoke(T config,
130 Map<String, MapWrapper<? extends CdmBase>> stores);
131
132
133 /**
134 * Returns true if this (IO-)class should be ignored during the import/export process.
135 * This information is usually stored in the configuration
136 * @param config
137 * @return
138 */
139 protected abstract boolean isIgnore(T config);
140
141 protected <T extends CdmBase> T getInstance(Class<? extends T> clazz){
142 T result = null;
143 try {
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 = " );
149 e.printStackTrace();
150 } catch (IllegalAccessException e) {
151 logger.error("Constructor of class "+clazz.getSimpleName()+" could not be accessed." );
152 e.printStackTrace();
153 } catch (SecurityException e) {
154 e.printStackTrace();
155 } catch (NoSuchMethodException e) {
156 logger.error("SecurityException for Constructor of class "+clazz.getSimpleName()+"." );
157 e.printStackTrace();
158 } catch (IllegalArgumentException e) {
159 logger.error("Empty Constructor does not exist for class "+clazz.getSimpleName()+"." );
160 e.printStackTrace();
161 } catch (InvocationTargetException e) {
162 logger.error("Empty Constructor could not be invoked for class "+clazz.getSimpleName()+"." );
163 e.printStackTrace();
164 }
165 return result;
166 }
167
168
169 }