Project

General

Profile

Download (7.55 KB) Statistics
| Branch: | Tag: | Revision:
1 50d46092 Andreas Müller
// $Id$
2
/**
3
* Copyright (C) 2007 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
11 cfcdda3c Andreas Müller
package eu.etaxonomy.cdm.model.metadata;
12 50d46092 Andreas Müller
13
import java.util.ArrayList;
14 90b75b52 Andreas Müller
import java.util.Comparator;
15 50d46092 Andreas Müller
import java.util.List;
16 9cc4226b Katja Luther
17 4f608eed a.babadshanjan
import javax.persistence.Entity;
18
19 50d46092 Andreas Müller
import org.apache.log4j.Logger;
20 2c00c4a5 n.hoffmann
import org.joda.time.DateTime;
21 50d46092 Andreas Müller
22 79c0eaa0 Andreas Müller
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
23 cfcdda3c Andreas Müller
import eu.etaxonomy.cdm.model.common.CdmBase;
24 90b75b52 Andreas Müller
25 50d46092 Andreas Müller
/**
26
 * @author a.mueller
27
 * @created 07.09.2009
28
 */
29 4f608eed a.babadshanjan
@Entity
30 f42fc263 a.babadshanjan
public class CdmMetaData extends CdmBase{
31 052be4c2 Andreas Müller
	private static final long serialVersionUID = -3033376680593279078L;
32 50d46092 Andreas Müller
	@SuppressWarnings("unused")
33
	private static final Logger logger = Logger.getLogger(CdmMetaData.class);
34
35 2c00c4a5 n.hoffmann
	/* It is a little bit confusing that this specific information is located in
36
	 * a generic class for metadata. Think about moving the schema version 
37
	 *  
38
	 */
39 50d46092 Andreas Müller
	/**
40
	 * The database schema version number.
41
	 * It is recommended to have the first two numbers equal to the CDM Library version number.
42
	 * But it is not obligatory as there may be cases when the library number changes but the
43
	 * schema version is not changing.
44
	 * The third should be incremented if the schema changes in a way that SCHEMA_VALIDATION.UPDATE
45
	 * will probably not work or will not be enough to transform old data into new data.
46 4c897ead Andreas Kohlbecker
	 * The fourth number should be incremented when minor schema changes take place that can
47 50d46092 Andreas Müller
	 * be handled by SCHEMA_VALIDATION.UPDATE
48
	 * The last number represents the date of change.
49
	 */
50 01b26e58 Andreas Müller
//	private static final String dbSchemaVersion = "3.3.1.0.201401140000";
51
	private static final String dbSchemaVersion = "3.4.0.0.201407010000";
52
	
53 59c2c696 Andreas Müller
	
54
	
55 9cc4226b Katja Luther
56 2c00c4a5 n.hoffmann
	/**
57
	 * @return a list of default metadata objects 
58
	 */
59
	public static final List<CdmMetaData> defaultMetaData(){
60 50d46092 Andreas Müller
		List<CdmMetaData> result = new ArrayList<CdmMetaData>();
61 2c00c4a5 n.hoffmann
		// schema version
62 50d46092 Andreas Müller
		result.add(new CdmMetaData(MetaDataPropertyName.DB_SCHEMA_VERSION, dbSchemaVersion));
63 2ad7be5d Andreas Müller
		//term version
64
		result.add(new CdmMetaData(MetaDataPropertyName.TERMS_VERSION, termsVersion));
65 2c00c4a5 n.hoffmann
		// database create time
66
		result.add(new CdmMetaData(MetaDataPropertyName.DB_CREATE_DATE, new DateTime().toString()));
67 9cc4226b Katja Luther
		return result;	
68
	}
69 6d4b8d16 Andreas Müller
	
70 9cc4226b Katja Luther
	/**
71
	 * The version number for the terms loaded by the termloader (csv-files)
72
	 * It is recommended to have the first two numbers equal to the CDM Library version number.
73 22679e42 n.hoffmann
	 * 
74 9cc4226b Katja Luther
	 * But it is not obligatory as there may be cases when the library number changes but the
75
	 * schema version is not changing.
76 22679e42 n.hoffmann
	 * 
77 9cc4226b Katja Luther
	 * The third should be incremented if the terms change in a way that is not compatible
78
	 * to the previous version (e.g. by changing the type of a term)
79 22679e42 n.hoffmann
	 * 
80
	 * The fourth number should be incremented when compatible term changes take place
81 9cc4226b Katja Luther
	 * (e.g. when new terms were added)
82 22679e42 n.hoffmann
	 * 
83 9cc4226b Katja Luther
	 * The last number represents the date of change.
84
	 */
85 01b26e58 Andreas Müller
//	private static final String termsVersion = "3.3.0.0.201309010000";
86
	private static final String termsVersion = "3.4.0.0.201407010000";
87
88 9cc4226b Katja Luther
	
89
	public enum MetaDataPropertyName{
90
		DB_SCHEMA_VERSION,
91
		TERMS_VERSION,
92
 		DB_CREATE_DATE,
93 22679e42 n.hoffmann
		DB_CREATE_NOTE;
94
		
95
		public String getSqlQuery(){
96
			return "SELECT value FROM CdmMetaData WHERE propertyname=" + this.ordinal();
97
		}
98 50d46092 Andreas Müller
	}
99
	
100 2c00c4a5 n.hoffmann
	/* END OF CONFUSION */
101
	private MetaDataPropertyName propertyName;
102
	private String value;
103 9cc4226b Katja Luther
104 294bd6f5 em.lee
	
105 9cc4226b Katja Luther
	/**
106
	 * Method to retrieve a CDM Libraries meta data
107
	 * @return
108
	 */
109
	public static final List<CdmMetaData> propertyList(){
110
		List<CdmMetaData> result = new ArrayList<CdmMetaData>();
111
		result.add(new CdmMetaData(MetaDataPropertyName.DB_SCHEMA_VERSION, dbSchemaVersion));
112
		result.add(new CdmMetaData(MetaDataPropertyName.TERMS_VERSION, termsVersion));
113
		result.add(new CdmMetaData(MetaDataPropertyName.DB_CREATE_DATE, new DateTime().toString()));
114
		return result;
115
	}
116
117
//********************* Constructor *********************************************/	
118
119 2c00c4a5 n.hoffmann
	/**
120
	 * Simple constructor to be used by Spring
121
	 */
122
	protected CdmMetaData(){
123
		super();
124
	}
125 5185fb4b em.lee
126 50d46092 Andreas Müller
	public CdmMetaData(MetaDataPropertyName propertyName, String value) {
127
		super();
128
		this.propertyName = propertyName;
129
		this.value = value;
130
	}
131
132 9cc4226b Katja Luther
//****************** instance methods ****************************************/	
133
	
134 50d46092 Andreas Müller
	/**
135
	 * @return the propertyName
136
	 */
137
	public MetaDataPropertyName getPropertyName() {
138
		return propertyName;
139
	}
140
141
	/**
142
	 * @param propertyName the propertyName to set
143
	 */
144
	public void setPropertyName(MetaDataPropertyName propertyName) {
145
		this.propertyName = propertyName;
146
	}
147
148
	/**
149
	 * @return the value
150
	 */
151
	public String getValue() {
152
		return value;
153
	}
154
155
	/**
156
	 * @param value the value to set
157
	 */
158
	public void setValue(String value) {
159
		this.value = value;
160
	}
161 90b75b52 Andreas Müller
162
//******************** Version comparator **********************************/
163
	
164
	public static class VersionComparator implements Comparator<String>{
165
		Integer depth;
166
		IProgressMonitor monitor;
167
		
168
		public VersionComparator(Integer depth, IProgressMonitor monitor){
169
			this.depth = depth;
170
			this.monitor = monitor;
171
		}
172
		
173
		/* (non-Javadoc)
174
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
175
		 */
176
		public int compare(String version1, String version2) {
177
			int result = 0;
178
			String[] version1Split = version1.split("\\.");
179
			String[] version2Split = version2.split("\\.");
180 22679e42 n.hoffmann
			
181
			if(version1Split.length == 1 || version2Split.length == 1){
182
				throwException("Tried to compare version but given Strings don't seem to " +
183
						"contain version numbers. version1: " + version1 + ", version2:" + version2); 
184 90b75b52 Andreas Müller
			}
185 22679e42 n.hoffmann
			
186
			if(depth != null && (version1Split.length < depth || version2Split.length < depth )){
187
				throwException("Desired depth can not be achieved with the given strings. depth: " + depth  + ", version1: " + version1 + ", version2:" + version2); 
188
			}			
189
			
190 90b75b52 Andreas Müller
			int length = (depth == null ||version1Split.length < depth) ? version1Split.length : depth;
191
			for (int i = 0; i < length; i++){
192
				Long version1Part = Long.valueOf(version1Split[i]);
193
				Long version2Part = Long.valueOf(version2Split[i]);
194
				int partCompare = version1Part.compareTo(version2Part);
195
				if (partCompare != 0){
196
					return partCompare;
197
				}
198
			}
199
			return result;
200
		}
201
		
202 22679e42 n.hoffmann
		private Throwable throwException(String message){
203
			RuntimeException exception =  new RuntimeException(message);
204
			if (monitor != null){
205
				monitor.warning(message, exception);
206
			}
207
			throw exception;
208
		}
209
		
210 90b75b52 Andreas Müller
	}
211
212
	/**
213
	 * Compares two version string. If version1 is higher than version2 a positive result is returned.
214
	 * If both are equal 0 is returned, otherwise -1 is returned.
215
	 * @see Comparator#compare(Object, Object)
216
	 * @param version1
217
	 * @param version2
218
	 * @param depth
219
	 * @param monitor
220
	 * @return
221
	 */
222
	public static int compareVersion(String version1, String version2, Integer depth, IProgressMonitor monitor){
223
		VersionComparator versionComparator = new VersionComparator(depth, monitor);
224
		return versionComparator.compare(version1, version2);
225
	}
226 9cc4226b Katja Luther
	
227 22679e42 n.hoffmann
	public static boolean isDbSchemaVersionCompatible(String version){
228
		return compareVersion(dbSchemaVersion, version, 3, null) == 0;
229 9cc4226b Katja Luther
	}
230
231 22679e42 n.hoffmann
	public static String getDbSchemaVersion() {
232 9cc4226b Katja Luther
		return dbSchemaVersion;
233
	}
234
	
235 22679e42 n.hoffmann
	public static String getTermsVersion() {
236
		return termsVersion;
237 9cc4226b Katja Luther
	}
238
239 22679e42 n.hoffmann
	public static boolean isTermsVersionCompatible(String version){
240
		return compareVersion(termsVersion, version, 3, null) == 0;
241 9cc4226b Katja Luther
	}
242 50d46092 Andreas Müller
	
243
}