merge-update from trunk
[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 java.sql.ResultSet;
13
14 import org.apache.log4j.Logger;
15
16 import eu.etaxonomy.cdm.common.monitor.DefaultProgressMonitor;
17 import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
18 import eu.etaxonomy.cdm.database.CdmDataSource;
19 import eu.etaxonomy.cdm.database.ICdmDataSource;
20 import eu.etaxonomy.cdm.database.update.v33_34.SchemaUpdater_331_34;
21 import eu.etaxonomy.cdm.database.update.v33_34.TermUpdater_33_34;
22 import eu.etaxonomy.cdm.model.metadata.CdmMetaData;
23
24 /**
25 * This class launches CDM model updates.
26 * Currently it splits the update in model updates and defined term related updates by
27 * using a {@link ISchemaUpdater schema updater} and a {@link ITermUpdater}. However, this
28 * architecture often results in problems and therefore will be replaced by only 1 schema updater.
29 * Term updates will be handled differently or also by using the schema updater.
30 * <BR>
31 * For each new schema version number there usually exists 1 {@link ISchemaUpdater} which is
32 * represents a list of schema update steps. {@link ISchemaUpdater schema updaters} are linked
33 * to previous updaters which are called, if relevant, previous to the latest updater.
34 * So it is possible to upgrade multiple schema version steps in one call.
35 * <BR><BR>
36 * As said before each {@link ISchemaUpdater schema updater} creates a list of
37 * {@link ISchemaUpdaterStep update steps}.
38 * <BR><BR>
39 * {@link ISchemaUpdater} support progression monitoring with each update step being one task.
40 * <BR><BR>
41 * ATTENTION: Some steps in the schema update are not transactional by nature. E.g. adding or removing a column
42 * to a table in a SQL database can not be handled in a transaction. Therefore failures in
43 * certain steps may not lead to a complete rollback of all steps covered by a {@link ISchemaUpdater}.
44 * This may lead to a situation where the database becomes inconsistent.
45 * <BR><BR>
46 * <u>HOW TO ADD A NEW UPDATER?</u><BR>
47 * Adding a new updater currently still needs adjustment at multiple places.
48 * <BR>
49 * <BR>1.) Increment {@link CdmMetaData} schema version number and term version number.
50 * <BR>2.) Create a new class instance of {@link SchemaUpdaterBase} (e.g. by copying an old one).
51 * <BR>3.) Update startSchemaVersion and endSchemaVersion in this new class, where startSchemaVersion
52 * is the old schema version and endSchemaVersion is the new schema version.
53 * <BR>4.) Implement {@link ISchemaUpdater#getPreviousUpdater()} and {@link ISchemaUpdater#getNextUpdater()}
54 * in a way that the former returns an instance of the previous schema updater and the later returns null (for now).
55 * <BR>5.) Go to the previous schema updater class and adjust {@link ISchemaUpdater#getNextUpdater()}
56 * in a way that it returns an instance of the newly created updater.
57 * <BR>6.) Repeat steps 2.-5. for {@link ITermUpdater}
58 * <BR>7.) Adjust {@link #getCurrentSchemaUpdater()} and {@link #getCurrentTermUpdater()} to return
59 * instances of the newly created updaters.
60 *
61 * @see ISchemaUpdater
62 * @see ITermUpdater
63 * @see ISchemaUpdaterStep
64 * @see ITermUpdaterStep
65 *
66 * @author a.mueller
67 * @date 10.09.2010
68 *
69 */
70 public class CdmUpdater {
71 private static final Logger logger = Logger.getLogger(CdmUpdater.class);
72
73 public static CdmUpdater NewInstance(){
74 return new CdmUpdater();
75 }
76
77 /**
78 * @param datasource
79 * @param monitor may be <code>null</code>
80 * @return
81 */
82 public boolean updateToCurrentVersion(ICdmDataSource datasource, IProgressMonitor monitor){
83 boolean result = true;
84 if (monitor == null){
85 monitor = DefaultProgressMonitor.NewInstance();
86 }
87 CaseType caseType = CaseType.caseTypeOfDatasource(datasource);
88
89 ISchemaUpdater currentSchemaUpdater = getCurrentSchemaUpdater();
90 // TODO do we really always update the terms??
91 ITermUpdater currentTermUpdater = getCurrentTermUpdater();
92
93 int steps = currentSchemaUpdater.countSteps(datasource, monitor, caseType);
94 steps += currentTermUpdater.countSteps(datasource, monitor, caseType);
95 steps++; //for hibernate_sequences update
96
97 String taskName = "Update to schema version " + currentSchemaUpdater.getTargetVersion() + " and to term version " + currentTermUpdater.getTargetVersion(); //+ currentSchemaUpdater.getVersion();
98 monitor.beginTask(taskName, steps);
99
100 try {
101 datasource.startTransaction();
102 result &= currentSchemaUpdater.invoke(datasource, monitor, caseType);
103 if (result == true){
104 result &= currentTermUpdater.invoke(datasource, monitor, caseType);
105 updateHibernateSequence(datasource, monitor, caseType);
106 }
107 if (result == false){
108 datasource.rollback();
109 }else{
110 datasource.commitTransaction();
111 }
112
113 } catch (Exception e) {
114 result = false;
115 monitor.warning("Stopped schema updater");
116 } finally {
117 String message = "Update finished " + (result ? "successfully" : "with ERRORS");
118 monitor.subTask(message);
119 if (!result){
120 monitor.warning(message);
121 monitor.setCanceled(true);
122 }else{
123 monitor.done();
124 }
125 logger.info(message);
126 }
127
128 return result;
129 }
130
131
132
133 /**
134 * Updating terms often inserts new terms, vocabularies and representations.
135 * Therefore the counter in hibernate_sequences must be increased.
136 * We do this once at the end of term updating.
137 * @param caseType
138 * @return true if update was successful, false otherwise
139 */
140 private boolean updateHibernateSequence(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) {
141 boolean result = true;
142 monitor.subTask("Update hibernate sequences");
143 try {
144 String sql = "SELECT * FROM hibernate_sequences ";
145 ResultSet rs = datasource.executeQuery(sql);
146 while (rs.next()){
147 String table = rs.getString("sequence_name");
148 Integer val = rs.getInt("next_val");
149 result &= updateSingleValue(datasource,monitor, table, val, caseType);
150 }
151 } catch (Exception e) {
152 String message = "Exception occurred when trying to update hibernate_sequences table: " + e.getMessage();
153 monitor.warning(message, e);
154 logger.error(message);
155 result = false;
156 }finally{
157 monitor.worked(1);
158 }
159 return result;
160 }
161
162 /**
163 *
164 * @param datasource
165 * @param monitor
166 * @param table
167 * @param oldVal
168 * @param caseType
169 * @return
170 */
171 private boolean updateSingleValue(ICdmDataSource datasource, IProgressMonitor monitor, String table, Integer oldVal, CaseType caseType){
172 if (table.equals("default")){ //found in flora central africa test database
173 return true;
174 }
175 try {
176 Integer newVal;
177 try {
178 String sql = " SELECT max(id) FROM %s ";
179 newVal = (Integer)datasource.getSingleValue(String.format(sql, caseType.transformTo(table)));
180 } catch (Exception e) {
181 String message = "Could not retrieve max value for table '%s'. Will not update hibernate_sequence for this table. " +
182 "Usually this will not cause problems, however, if new data has been added to " +
183 "this table by the update script one may encounter 'unique identifier' " +
184 "exceptions when trying to add further data.";
185 monitor.warning(String.format(message,table), e);
186 //TODO
187 return true;
188 }
189
190 if (newVal != null){
191 //This is how {@link PooledOptimizer#generate(org.hibernate.id.enhanced.AccessCallback)} works
192 //it substracts the increment size from the value in hibernate_sequences to get the initial value.
193 //Haven't checked why.
194 //For the correct increment size see eu.etaxonomy.cdm.model.common.package-info.java
195 int incrementSize = 10;
196 newVal = newVal + incrementSize;
197 if (newVal != null && newVal >= oldVal){
198 String sql = " UPDATE hibernate_sequences " +
199 " SET next_val = %d " +
200 " WHERE sequence_name = '%s' ";
201 datasource.executeUpdate(String.format(sql, newVal + 1 , table) );
202 }
203 }
204 return true;
205 } catch (Exception e) {
206 String message = "Exception occurred when trying to read or update hibernate_sequences table for value " + table + ": " + e.getMessage();
207 monitor.warning(message, e);
208 logger.error(message);
209 return false;
210 }
211
212 }
213
214
215 private ITermUpdater getCurrentTermUpdater() {
216 return TermUpdater_33_34.NewInstance();
217 }
218
219 /**
220 * Returns the current CDM updater
221 * @return
222 */
223 private ISchemaUpdater getCurrentSchemaUpdater() {
224 return SchemaUpdater_331_34.NewInstance();
225 }
226
227 /**
228 * @param args
229 */
230 public static void main(String[] args) {
231 // logger.warn("main method not yet fully implemented (only works with mysql!!!)");
232 // if(args.length < 2){
233 // logger.error("Arguments missing: server database [username [password]]");
234 // }
235 //TODO better implementation
236 CdmUpdater myUpdater = new CdmUpdater();
237 String server = args[0];
238 String database = args[1];
239 String username = args.length > 2 ? args[2] : null;
240 String password = args.length > 3 ? args[3] : null;
241 int port = 3306;
242 if( args.length > 4){
243 try {
244 port = Integer.parseInt(args[4]);
245 } catch (Exception e) {
246 // ignore
247 }
248 }
249
250 ICdmDataSource dataSource = CdmDataSource.NewMySqlInstance(server, database, port, username, password, null);
251 boolean success = myUpdater.updateToCurrentVersion(dataSource, null);
252 System.out.println("DONE " + (success ? "successfully" : "with ERRORS"));
253 }
254
255 }