4b63121efbefe89ba2e8fbdd472b3618a8e8bd8b
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / database / update / CdmUpdater.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.cdm.database.update;
11
12 import org.apache.log4j.Logger;
13
14 import eu.etaxonomy.cdm.common.DefaultProgressMonitor;
15 import eu.etaxonomy.cdm.common.IProgressMonitor;
16 import eu.etaxonomy.cdm.database.CdmDataSource;
17 import eu.etaxonomy.cdm.database.ICdmDataSource;
18 import eu.etaxonomy.cdm.database.update.v25_30.SchemaUpdater_25_30;
19 import eu.etaxonomy.cdm.database.update.v30_31.TermUpdater_30_31;
20
21 /**
22 * @author a.mueller
23 * @date 10.09.2010
24 *
25 */
26 public class CdmUpdater {
27 private static final Logger logger = Logger.getLogger(CdmUpdater.class);
28
29 public static CdmUpdater NewInstance(){
30 return new CdmUpdater();
31 }
32
33 /**
34 * @param datasource
35 * @param monitor may be <code>null</code>
36 * @return
37 */
38 public boolean updateToCurrentVersion(ICdmDataSource datasource, IProgressMonitor monitor){
39 boolean result = true;
40 if (monitor == null){
41 monitor = DefaultProgressMonitor.NewInstance();
42 }
43
44 ISchemaUpdater currentSchemaUpdater = getCurrentSchemaUpdater();
45 // TODO do we really always update the terms??
46 ITermUpdater currentTermUpdater = getCurrentTermUpdater();
47
48 int steps = currentSchemaUpdater.countSteps(datasource, monitor);
49 steps += currentTermUpdater.countSteps(datasource, monitor);
50
51 String taskName = "Update to schema version " + currentSchemaUpdater.getTargetVersion() + " and to term version " + currentTermUpdater.getTargetVersion(); //+ currentSchemaUpdater.getVersion();
52 monitor.beginTask(taskName, steps);
53
54 try {
55 result &= currentSchemaUpdater.invoke(datasource, monitor);
56 // the above apparently did not work while testing. Did not want to set the version in CdmMetaData yet
57 // result &= currentSchemaUpdater.invoke(currentSchemaUpdater.getTargetVersion(), datasource, monitor);
58 result &= currentTermUpdater.invoke(datasource, monitor);
59 } catch (Exception e) {
60 result = false;
61 monitor.warning("Stopped schema updater");
62 } finally {
63 String message = "Update finished " + (result ? "successfully" : "with ERRORS");
64 monitor.subTask(message);
65 monitor.done();
66 logger.info(message);
67 }
68
69 return result;
70 }
71
72 private ITermUpdater getCurrentTermUpdater() {
73 return TermUpdater_30_31.NewInstance();
74 }
75
76 /**
77 * Returns the current CDM updater
78 * @return
79 */
80 private ISchemaUpdater getCurrentSchemaUpdater() {
81 return SchemaUpdater_25_30.NewInstance();
82 }
83
84 /**
85 * @param args
86 */
87 public static void main(String[] args) {
88 logger.warn("main method not yet fully implemented (only works with mysql!!!)");
89 if(args.length < 2){
90 logger.error("Arguments missing: server database [username [password]]");
91 }
92 //TODO better implementation
93 CdmUpdater myUpdater = new CdmUpdater();
94 String server = args[0];
95 String database = args[1];
96 String username = args.length > 2 ? args[2] : null;
97 String password = args.length > 3 ? args[3] : null;
98
99 ICdmDataSource dataSource = CdmDataSource.NewMySqlInstance(server, database, username, password);
100 boolean success = myUpdater.updateToCurrentVersion(dataSource, null);
101 System.out.println("DONE " + (success ? "successfully" : "with ERRORS"));
102 }
103
104 }