Project

General

Profile

Download (7.34 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.api.application;
2

    
3
import java.io.FileNotFoundException;
4

    
5
import org.apache.log4j.Logger;
6
import org.hibernate.SessionFactory;
7
import org.hibernate.engine.loading.LoadContexts;
8
import org.hibernate.impl.SessionFactoryImpl;
9
import org.hsqldb.Server;
10
import org.springframework.beans.BeansException;
11
import org.springframework.beans.factory.BeanCreationException;
12
import org.springframework.context.ApplicationContext;
13
import org.springframework.context.support.AbstractApplicationContext;
14
import org.springframework.context.support.AbstractXmlApplicationContext;
15
import org.springframework.context.support.ClassPathXmlApplicationContext;
16
import org.springframework.context.support.FileSystemXmlApplicationContext;
17
import org.springframework.orm.hibernate3.HibernateTransactionManager;
18
import org.springframework.transaction.TransactionDefinition;
19
import org.springframework.transaction.TransactionStatus;
20
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
21

    
22
import eu.etaxonomy.cdm.api.service.IAgentService;
23
import eu.etaxonomy.cdm.api.service.IDatabaseService;
24
import eu.etaxonomy.cdm.api.service.INameService;
25
import eu.etaxonomy.cdm.api.service.IReferenceService;
26
import eu.etaxonomy.cdm.api.service.ITaxonService;
27
import eu.etaxonomy.cdm.api.service.ITermService;
28
import eu.etaxonomy.cdm.common.CdmUtils;
29
import eu.etaxonomy.cdm.database.CdmDataSource;
30
import eu.etaxonomy.cdm.database.DataSourceNotFoundException;
31
import eu.etaxonomy.cdm.database.init.TermLoader;
32
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
33
import eu.etaxonomy.cdm.model.common.IReferencedEntity;
34
import eu.etaxonomy.cdm.model.common.NoDefinedTermClassException;
35

    
36

    
37
/**
38
 * @author a.mueller
39
 *
40
 */
41
public class CdmApplicationController {
42
	private static final Logger logger = Logger.getLogger(CdmApplicationController.class);
43
	
44
	public AbstractApplicationContext applicationContext;
45
	private INameService nameService;
46
	private ITaxonService taxonService;
47
	private IReferenceService referenceService;
48
	private IAgentService agentService;
49
	private IDatabaseService databaseService;
50
	private ITermService termService;
51
	
52
	
53
	private Server hsqldbServer;
54
	
55
	
56
	/**
57
	 * Constructor, opens an spring 2.5 ApplicationContext by using the default data source
58
	 * @param dataSource
59
	 */
60
	public CdmApplicationController() {
61
		logger.info("Start CdmApplicationController with default data source");
62
		CdmDataSource dataSource = CdmDataSource.NewDefaultInstance();
63
		setNewDataSource(dataSource);
64
	}
65
	
66
	/**
67
	 * Constructor, opens an spring 2.5 ApplicationContext by using the according data source
68
	 * @param dataSource
69
	 */
70
	public CdmApplicationController(CdmDataSource dataSource) 
71
			throws DataSourceNotFoundException{
72
		logger.info("Start CdmApplicationController with datasource: " + dataSource);
73
		if (setNewDataSource(dataSource) == false){
74
			throw new DataSourceNotFoundException("Wrong datasource: " + dataSource );
75
		}
76
	}
77

    
78
	
79
	/**
80
	 * Sets the application context to a new spring ApplicationContext by using the according data source and initializes the Controller.
81
	 * @param dataSource
82
	 */
83
	private boolean setNewDataSource(CdmDataSource dataSource) {
84
		dataSource.updateSessionFactory(null);
85
		FileSystemXmlApplicationContext appContext;
86
		try {
87
			logger.debug("Start spring-2.5 ApplicationContex with 'hibernate.hbm2ddl.auto'='default'");
88
			appContext = new FileSystemXmlApplicationContext(CdmUtils.getApplicationContextString());
89
		} catch (BeanCreationException e) {
90
			// create new schema
91
			logger.warn("Database schema not up-to-date. Schema must be updated. All DefindeTerms are deleted and created new!");
92
			logger.debug("Start spring-2.5 ApplicationContex with hibernate.hbm2ddl.auto 'CREATE' property");
93
			dataSource.updateSessionFactory("create"); 
94
			appContext = new FileSystemXmlApplicationContext(CdmUtils.getApplicationContextString());
95
		}
96
		setApplicationContext(appContext);
97
		// load defined terms if necessary 
98
		if (testDefinedTermsAreMissing()){
99
			TermLoader termLoader = (TermLoader) appContext.getBean("termLoader");
100
			try {
101
				termLoader.loadAllDefaultTerms();
102
			} catch (FileNotFoundException fileNotFoundException) {
103
				logger.error("One or more DefinedTerm initialisation files could not be found");
104
				fileNotFoundException.printStackTrace();
105
				return false;
106
			} catch (NoDefinedTermClassException noDefinedTermClassException) {
107
				logger.error("NoDefinedTermClassException");
108
				noDefinedTermClassException.printStackTrace();
109
				return false;
110
			}
111
		}
112
		return true;
113
	}
114
	
115
	
116
	/**
117
	 * Tests if some DefinedTermsAreMissing.
118
	 * @return true, if at least one is missing, else false
119
	 */
120
	public boolean testDefinedTermsAreMissing(){
121
		String englishUuid = "e9f8cdb7-6819-44e8-95d3-e2d0690c3523";
122
		DefinedTermBase english = this.getTermService().getTermByUri(englishUuid);
123
		if ( english == null || ! english.getUuid().equalsIgnoreCase(englishUuid)){
124
			return true;
125
		}else{
126
			return false;
127
		}
128
	}
129
	
130

    
131
	/**
132
	 * Changes the ApplicationContext to the new dataSource
133
	 * @param dataSource
134
	 */
135
	public boolean changeDataSource(CdmDataSource dataSource) {
136
		logger.info("Change datasource to : " + dataSource);
137
		return setNewDataSource(dataSource);
138
	}
139
	
140
	/**
141
	 * Sets a new application Context.
142
	 * @param ac
143
	 */
144
	public void setApplicationContext(AbstractXmlApplicationContext ac){
145
		closeApplicationContext(); //closes old application context if necessary
146
		applicationContext = ac;
147
		applicationContext.registerShutdownHook();
148
		init();
149
	}
150
	
151
	/* (non-Javadoc)
152
	 * @see java.lang.Object#finalize()
153
	 */
154
	public void finalize(){
155
		close();
156
	}
157
	
158
	/**
159
	 * closes the application
160
	 */
161
	public void close(){
162
		closeApplicationContext();
163
	}
164
	
165
	/**
166
	 * closes the application context
167
	 */
168
	private void closeApplicationContext(){
169
		if (applicationContext != null){
170
			logger.info("Close ApplicationContext");
171
			applicationContext.close();
172
		}
173
	}
174
	
175
	private void init(){
176
		//TODO ? also possible via SPRING?
177
		nameService = (INameService)applicationContext.getBean("nameServiceImpl");
178
		taxonService = (ITaxonService)applicationContext.getBean("taxonServiceImpl");
179
		referenceService = (IReferenceService)applicationContext.getBean("referenceServiceImpl");
180
		agentService = (IAgentService)applicationContext.getBean("agentServiceImpl");
181
		termService = (ITermService)applicationContext.getBean("termServiceImpl");
182
		DefinedTermBase.initTermList(termService);
183
		databaseService = (IDatabaseService)applicationContext.getBean("databaseServiceHibernateImpl");
184
		databaseService.setApplicationController(this);
185
	}
186
	
187

    
188
	
189
	/* ******  Services *********/
190
	public final INameService getNameService(){
191
		return this.nameService;
192
	}
193

    
194
	public final ITaxonService getTaxonService(){
195
		return this.taxonService;
196
	}
197

    
198
	public final IReferenceService getReferenceService(){
199
		return this.referenceService;
200
	}
201
	
202
	public final IAgentService getAgentService(){
203
		return this.agentService;
204
	}
205
	
206
	public final IDatabaseService getDatabaseService(){
207
		return this.databaseService;
208
	}
209
	
210
	public final ITermService getTermService(){
211
		return this.termService;
212
	}
213
	
214
	/* **** flush ***********/
215
	public void flush() {
216
		SessionFactory sf = (SessionFactory)applicationContext.getBean("sessionFactory");
217
		sf.getCurrentSession().flush();
218
	}
219
		
220
		
221
	
222
}
    (1-1/1)