Project

General

Profile

Download (7.92 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.database.update;
10

    
11
import java.sql.SQLException;
12
import java.util.List;
13

    
14
import org.apache.log4j.Logger;
15

    
16
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
17
import eu.etaxonomy.cdm.database.ICdmDataSource;
18
import eu.etaxonomy.cdm.model.metadata.CdmMetaData;
19

    
20

    
21
/**
22
 * Common updater base class for updating schema or terms.
23
 *
24
 * @see CdmUpdater
25
 * @see ISchemaUpdater
26
 * @see ITermUpdater
27
 *
28
 * @author a.mueller
29
 * @date 16.11.2010
30
 *
31
 */
32
public abstract class UpdaterBase<T extends ISchemaUpdaterStep, U extends IUpdater<U>> implements IUpdater<U> {
33
	private static final Logger logger = Logger.getLogger(TermUpdaterBase.class);
34

    
35
	protected List<T> list;
36
	protected String startVersion;
37
	protected String targetVersion;
38

    
39

    
40
	protected abstract boolean updateVersion(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException;
41

    
42
	protected abstract String getCurrentVersion(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException;
43

    
44
	@Override
45
	public int countSteps(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType){
46
		int result = 0;
47
		//TODO test if previous updater is needed
48
		if (isToBeInvoked(datasource, monitor, caseType)){
49
			for (T step: list){
50
				result++; //+= list.size();
51
				result += step.getInnerSteps().size();
52
			}
53
			if (getPreviousUpdater() != null){
54
				result += getPreviousUpdater().countSteps(datasource, monitor, caseType);
55
			}
56
		}
57
		return result;
58
	}
59

    
60

    
61
	private boolean isToBeInvoked(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) {
62
		boolean result = true;
63
		String datasourceVersion;
64
		try {
65
			datasourceVersion = getCurrentVersion(datasource, monitor, caseType);
66
		} catch (SQLException e1) {
67
			monitor.warning("SQLException", e1);
68
			return false;
69
		}
70

    
71
		boolean isAfterMyStartVersion = isAfterMyStartVersion(datasourceVersion, monitor);
72
		boolean isBeforeMyStartVersion = isBeforeMyStartVersion(datasourceVersion, monitor);
73
//		boolean isBeforeMyTargetVersion = isBeforeMyTargetVersion(targetVersion, monitor);
74
		boolean isBeforeMyTargetVersion = isBeforeMyTargetVersion(targetVersion, monitor);
75
		boolean isDatasourceBeforeMyTargetVersion = isBeforeMyTargetVersion(datasourceVersion, monitor);
76

    
77
		result &= isDatasourceBeforeMyTargetVersion;
78
		result &= !(isAfterMyStartVersion && isBeforeMyTargetVersion);
79
		result &= ! (isBeforeMyStartVersion && getPreviousUpdater() == null);
80
		result &= !isBeforeMyTargetVersion;
81
		return result;
82
	}
83

    
84

    
85
	@Override
86
	public boolean invoke(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws Exception{
87
		String currentLibrarySchemaVersion = CdmMetaData.getDbSchemaVersion();
88
		return invoke(currentLibrarySchemaVersion, datasource, monitor, caseType);
89
	}
90

    
91
	@Override
92
	public boolean invoke(String targetVersion, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws Exception{
93
		boolean result = true;
94
		String datasourceVersion;
95

    
96
		try {
97
			datasourceVersion = getCurrentVersion(datasource, monitor, caseType);
98
		} catch (SQLException e1) {
99
			monitor.warning("SQLException", e1);
100
			return false;
101
		}
102

    
103

    
104
		boolean isAfterMyStartVersion = isAfterMyStartVersion(datasourceVersion, monitor);
105
		boolean isBeforeMyStartVersion = isBeforeMyStartVersion(datasourceVersion, monitor);
106
//		boolean isAfterMyTargetVersion = isAfterMyTargetVersion(targetVersion, monitor);
107
		boolean isBeforeMyTargetVersion = isBeforeMyTargetVersion(targetVersion, monitor);
108
		boolean isDatasourceBeforeMyTargetVersion = isBeforeMyTargetVersion(datasourceVersion, monitor);
109

    
110

    
111

    
112
		if (! isDatasourceBeforeMyTargetVersion){
113
			String warning = "Target version ("+targetVersion+") is not before updater target version ("+this.targetVersion+"). Nothing to update.";
114
			monitor.warning(warning);
115
			return true;
116
		}
117

    
118
		if (isAfterMyStartVersion && isBeforeMyTargetVersion){
119
			String warning = "Database version is higher than updater start version but lower than updater target version";
120
			RuntimeException exeption = new RuntimeException(warning);
121
			monitor.warning(warning, exeption);
122
			throw exeption;
123
		}
124

    
125
		if (isBeforeMyStartVersion){
126
			if (getPreviousUpdater() == null){
127
				String warning = "Database version is before updater version but no previous version updater exists";
128
				RuntimeException exeption = new RuntimeException(warning);
129
				monitor.warning(warning, exeption);
130
				throw exeption;
131
			}
132
			result &= getPreviousUpdater().invoke(startVersion, datasource, monitor, caseType);
133
		}
134

    
135

    
136

    
137
		if (isBeforeMyTargetVersion){
138
			String warning = "Target version ("+targetVersion+") is lower than updater target version ("+this.targetVersion+")";
139
			RuntimeException exeption = new RuntimeException(warning);
140
			monitor.warning(warning, exeption);
141
			throw exeption;
142
		}
143

    
144
		if (result == false){
145
			return result;
146
		}
147
//		datasource.startTransaction();  transaction already started by CdmUpdater
148
		try {
149
			for (T step : list){
150
				result &= handleSingleStep(datasource, monitor, result, step, false, caseType);
151
				if (result == false){
152
					break;
153
				}
154
			}
155
			if (result == true){
156
				result &= updateVersion(datasource, monitor, caseType);
157
			}else{
158
				datasource.rollback();
159
			}
160

    
161
		} catch (Exception e) {
162
			datasource.rollback();
163
			logger.error("Error occurred while trying to run updater: " + this.getClass().getName());
164
			result = false;
165
		}
166
		return result;
167

    
168
	}
169

    
170
//	protected abstract boolean handleSingleStep(ICdmDataSource datasource,	IProgressMonitor monitor, boolean result, ISchemaUpdaterStep step, boolean isInnerStep) throws Exception;
171

    
172
	protected boolean handleSingleStep(ICdmDataSource datasource, IProgressMonitor monitor, boolean result, ISchemaUpdaterStep step, boolean isInnerStep, CaseType caseType)
173
			throws Exception {
174
		try {
175
			monitor.subTask(step.getStepName());
176
			Integer invokeResult = step.invoke(datasource, monitor, caseType);
177
			result &= (invokeResult != null);
178
			for (ISchemaUpdaterStep innerStep : step.getInnerSteps()){
179
				result &= handleSingleStep(datasource, monitor, result, innerStep, true, caseType);
180
				if (!result){
181
				    break;
182
				}
183
			}
184
//			if (! isInnerStep){
185
				monitor.worked(1);
186
//			}
187
		} catch (Exception e) {
188
			monitor.warning("Monitor: Exception occurred while handling single schema updating step", e);
189
			datasource.rollback();
190
			result = false;
191
		}
192
		return result;
193
	}
194

    
195
	protected boolean isAfterMyStartVersion(String dataSourceSchemaVersion, IProgressMonitor monitor) {
196
		int depth = 4;
197
		int compareResult = CdmMetaData.compareVersion(dataSourceSchemaVersion, startVersion, depth, monitor);
198
		return compareResult > 0;
199
	}
200

    
201
	protected boolean isBeforeMyStartVersion(String dataSourceSchemaVersion, IProgressMonitor monitor) {
202
		int depth = 4;
203
		int compareResult = CdmMetaData.compareVersion(dataSourceSchemaVersion, startVersion, depth, monitor);
204
		return compareResult < 0;
205
	}
206

    
207
	protected boolean isAfterMyTargetVersion(String dataSourceSchemaVersion, IProgressMonitor monitor) {
208
		int depth = 4;
209
		int compareResult = CdmMetaData.compareVersion(dataSourceSchemaVersion, targetVersion, depth, monitor);
210
		return compareResult > 0;
211
	}
212

    
213
	protected boolean isBeforeMyTargetVersion(String dataSourceSchemaVersion, IProgressMonitor monitor) {
214
		int depth = 4;
215
		int compareResult = CdmMetaData.compareVersion(dataSourceSchemaVersion, targetVersion, depth, monitor);
216
		return compareResult < 0;
217
	}
218

    
219
	@Override
220
	public abstract U getNextUpdater();
221

    
222
	@Override
223
	public abstract U getPreviousUpdater();
224

    
225

    
226
	/**
227
	 * @return
228
	 */
229
	public String getTargetVersion() {
230
		return this.targetVersion;
231
	}
232
}
(35-35/36)