-
[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.monitor.DefaultProgressMonitor;
15 import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
16 import eu.etaxonomy.cdm.database.CdmDataSource;
17 import eu.etaxonomy.cdm.database.ICdmDataSource;
18 import eu.etaxonomy.cdm.database.update.v30_31.TermUpdater_314_315;
19 import eu.etaxonomy.cdm.database.update.v31_33.SchemaUpdater_31_33;
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
59 result &= currentTermUpdater.invoke(datasource, monitor);
60 } catch (Exception e) {
61 result = false;
62 monitor.warning("Stopped schema updater");
63 } finally {
64 String message = "Update finished " + (result ? "successfully" : "with ERRORS");
65 monitor.subTask(message);
66 monitor.done();
67 logger.info(message);
68 }
69
70 return result;
71 }
72
73 private ITermUpdater getCurrentTermUpdater() {
74 return TermUpdater_314_315.NewInstance();
75 }
76
77 /**
78 * Returns the current CDM updater
79 * @return
80 */
81 private ISchemaUpdater getCurrentSchemaUpdater() {
82 return SchemaUpdater_31_33.NewInstance();
83 }
84
85 /**
86 * @param args
87 */
88 public static void main(String[] args) {
89 logger.warn("main method not yet fully implemented (only works with mysql!!!)");
90 if(args.length < 2){
91 logger.error("Arguments missing: server database [username [password]]");
92 }
93 //TODO better implementation
94 CdmUpdater myUpdater = new CdmUpdater();
95 String server = args[0];
96 String database = args[1];
97 String username = args.length > 2 ? args[2] : null;
98 String password = args.length > 3 ? args[3] : null;
99
100 ICdmDataSource dataSource = CdmDataSource.NewMySqlInstance(server, database, username, password);
101 boolean success = myUpdater.updateToCurrentVersion(dataSource, null);
102 System.out.println("DONE " + (success ? "successfully" : "with ERRORS"));
103 }
104
105 }