(no commit message)
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / application / CdmApplicationController.java
1 package eu.etaxonomy.cdm.api.application;
2
3 import java.io.FileNotFoundException;
4 import java.util.UUID;
5
6 import org.apache.log4j.Logger;
7 import org.hibernate.SessionFactory;
8 import org.hsqldb.Server;
9 import org.springframework.beans.factory.BeanCreationException;
10 import org.springframework.context.support.AbstractApplicationContext;
11 import org.springframework.context.support.AbstractXmlApplicationContext;
12 import org.springframework.context.support.FileSystemXmlApplicationContext;
13
14 import eu.etaxonomy.cdm.api.application.eclipse.EclipseRcpSaveFileSystemXmlApplicationContext;
15 import eu.etaxonomy.cdm.api.service.IAgentService;
16 import eu.etaxonomy.cdm.api.service.IDatabaseService;
17 import eu.etaxonomy.cdm.api.service.INameService;
18 import eu.etaxonomy.cdm.api.service.IReferenceService;
19 import eu.etaxonomy.cdm.api.service.ITaxonService;
20 import eu.etaxonomy.cdm.api.service.ITermService;
21 import eu.etaxonomy.cdm.database.CdmDataSource;
22 import eu.etaxonomy.cdm.database.DataSourceNotFoundException;
23 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
24 import eu.etaxonomy.cdm.model.common.NoDefinedTermClassException;
25 import eu.etaxonomy.cdm.model.common.init.TermLoader;
26
27
28 /**
29 * @author a.mueller
30 *
31 */
32 public class CdmApplicationController {
33 private static final Logger logger = Logger.getLogger(CdmApplicationController.class);
34
35 public AbstractApplicationContext applicationContext;
36 private INameService nameService;
37 private ITaxonService taxonService;
38 private IReferenceService referenceService;
39 private IAgentService agentService;
40 private IDatabaseService databaseService;
41 private ITermService termService;
42
43 private Server hsqldbServer;
44
45 public enum HBM2DDL{
46 VALIDATE,
47 UPDATE,
48 CREATE;
49
50 public String getHibernateString(){
51 switch (this){
52 case VALIDATE:
53 return "validate";
54 case UPDATE:
55 return "update";
56 case CREATE:
57 return "create";
58 default:
59 throw new IllegalArgumentException( "Unknown enumeration type" );
60 }
61 }
62 }
63
64
65 /**
66 * Constructor, opens an spring 2.5 ApplicationContext by using the default data source
67 * @param dataSource
68 */
69 public CdmApplicationController() {
70 logger.info("Start CdmApplicationController with default data source");
71 CdmDataSource dataSource = CdmDataSource.NewDefaultInstance();
72 HBM2DDL hbm2dll = HBM2DDL.VALIDATE;
73 setNewDataSource(dataSource, hbm2dll);
74 }
75
76 /**
77 * Constructor, opens an spring 2.5 ApplicationContext by using the according data source
78 * @param dataSource
79 */
80 public CdmApplicationController(CdmDataSource dataSource)
81 throws DataSourceNotFoundException{
82 logger.info("Start CdmApplicationController with datasource: " + dataSource);
83 HBM2DDL hbm2dll = HBM2DDL.VALIDATE;
84 if (setNewDataSource(dataSource, hbm2dll) == false){
85 throw new DataSourceNotFoundException("Wrong datasource: " + dataSource );
86 }
87 }
88
89
90 /**
91 * Constructor, opens an spring 2.5 ApplicationContext by using the according data source
92 * @param dataSource
93 */
94 public CdmApplicationController(CdmDataSource dataSource, HBM2DDL hbm2dll)
95 throws DataSourceNotFoundException{
96 logger.info("Start CdmApplicationController with datasource: " + dataSource);
97 if (setNewDataSource(dataSource, hbm2dll) == false){
98 throw new DataSourceNotFoundException("Wrong datasource: " + dataSource );
99 }
100 }
101
102
103 /**
104 * Sets the application context to a new spring ApplicationContext by using the according data source and initializes the Controller.
105 * @param dataSource
106 */
107 private boolean setNewDataSource(CdmDataSource dataSource, HBM2DDL hbm2dll) {
108 if (hbm2dll == null){
109 hbm2dll = hbm2dll.VALIDATE;
110 }
111 dataSource.updateSessionFactory(hbm2dll.getHibernateString());
112 logger.info("Connecting to '" + dataSource.getName() + "'");
113 FileSystemXmlApplicationContext appContext;
114 try {
115 //logger.debug("Start spring-2.5 ApplicationContex with 'hibernate.hbm2ddl.auto'='default'");
116 appContext = new EclipseRcpSaveFileSystemXmlApplicationContext(CdmApplicationUtils.getApplicationContextString());
117 } catch (BeanCreationException e) {
118 // create new schema
119 if (hbm2dll == HBM2DDL.VALIDATE) {
120 logger.error("ApplicationContext could not be created. " +
121 " Maybe your database schema is not up-to-date, " +
122 " but there might be other BeanCreation problems too." +
123 " Try to run CdmApplicationController with hbm2dll.CREATE or hbm2dll.UPDATE option. ");
124 } else {
125 logger.error("BeanCreationException (CdmApplicationController startet with " + hbm2dll.getHibernateString() + " option.");
126 }
127 e.printStackTrace();
128 return false;
129 // logger.warn("Database schema not up-to-date. Schema must be updated. All DefindeTerms are deleted and created new!");
130 // logger.debug("Start spring-2.5 ApplicationContex with hibernate.hbm2ddl.auto 'CREATE' property");
131 // dataSource.updateSessionFactory("create");
132 // appContext = new EclipseRcpSaveFileSystemXmlApplicationContext(CdmApplicationUtils.getApplicationContextString());
133 }
134 setApplicationContext(appContext);
135 // load defined terms if necessary
136 //TODO not necessary any more
137 if (testDefinedTermsAreMissing()){
138 TermLoader termLoader = (TermLoader) appContext.getBean("termLoader");
139 try {
140 termLoader.loadAllDefaultTerms();
141 } catch (FileNotFoundException fileNotFoundException) {
142 logger.error("One or more DefinedTerm initialisation files could not be found");
143 fileNotFoundException.printStackTrace();
144 return false;
145 } catch (NoDefinedTermClassException noDefinedTermClassException) {
146 logger.error("NoDefinedTermClassException");
147 noDefinedTermClassException.printStackTrace();
148 return false;
149 }
150 }
151 return true;
152 }
153
154
155 /**
156 * Tests if some DefinedTermsAreMissing.
157 * @return true, if at least one is missing, else false
158 */
159 public boolean testDefinedTermsAreMissing(){
160 UUID englishUuid = UUID.fromString("e9f8cdb7-6819-44e8-95d3-e2d0690c3523");
161 DefinedTermBase english = this.getTermService().getTermByUri(englishUuid.toString());
162 if ( english == null || ! english.getUuid().equals(englishUuid)){
163 return true;
164 }else{
165 return false;
166 }
167 }
168
169
170 /**
171 * Changes the ApplicationContext to the new dataSource
172 * @param dataSource
173 */
174 public boolean changeDataSource(CdmDataSource dataSource) {
175 logger.info("Change datasource to : " + dataSource);
176 return setNewDataSource(dataSource, HBM2DDL.VALIDATE);
177 }
178
179 /**
180 * Changes the ApplicationContext to the new dataSource
181 * @param dataSource
182 */
183 public boolean changeDataSource(CdmDataSource dataSource, HBM2DDL hbm2dll) {
184 logger.info("Change datasource to : " + dataSource);
185 return setNewDataSource(dataSource, hbm2dll);
186 }
187
188 /**
189 * Sets a new application Context.
190 * @param ac
191 */
192 public void setApplicationContext(AbstractXmlApplicationContext ac){
193 closeApplicationContext(); //closes old application context if necessary
194 applicationContext = ac;
195 applicationContext.registerShutdownHook();
196 init();
197 }
198
199 /* (non-Javadoc)
200 * @see java.lang.Object#finalize()
201 */
202 public void finalize(){
203 close();
204 }
205
206 /**
207 * closes the application
208 */
209 public void close(){
210 closeApplicationContext();
211 }
212
213 /**
214 * closes the application context
215 */
216 private void closeApplicationContext(){
217 if (applicationContext != null){
218 logger.info("Close ApplicationContext");
219 applicationContext.close();
220 }
221 }
222
223 private void init(){
224 logger.debug("Init " + this.getClass().getName() + " ... ");
225 if (logger.isDebugEnabled()){for (String beanName : applicationContext.getBeanDefinitionNames()){ logger.debug(beanName);}}
226 nameService = (INameService)applicationContext.getBean("nameServiceImpl");
227 taxonService = (ITaxonService)applicationContext.getBean("taxonServiceImpl");
228 referenceService = (IReferenceService)applicationContext.getBean("referenceServiceImpl");
229 agentService = (IAgentService)applicationContext.getBean("agentServiceImpl");
230 termService = (ITermService)applicationContext.getBean("termServiceImpl");
231 databaseService = (IDatabaseService)applicationContext.getBean("databaseServiceHibernateImpl");
232 databaseService.setApplicationController(this);
233 }
234
235
236
237 /* ****** Services *********/
238 public final INameService getNameService(){
239 return this.nameService;
240 }
241
242 public final ITaxonService getTaxonService(){
243 return this.taxonService;
244 }
245
246 public final IReferenceService getReferenceService(){
247 return this.referenceService;
248 }
249
250 public final IAgentService getAgentService(){
251 return this.agentService;
252 }
253
254 public final IDatabaseService getDatabaseService(){
255 return this.databaseService;
256 }
257
258 public final ITermService getTermService(){
259 return this.termService;
260 }
261
262 /* **** flush ***********/
263 public void flush() {
264 SessionFactory sf = (SessionFactory)applicationContext.getBean("sessionFactory");
265 sf.getCurrentSession().flush();
266 }
267
268
269
270 }