Project

General

Profile

Download (7.44 KB) Statistics
| Branch: | Tag: | Revision:
1
// $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
package eu.etaxonomy.cdm.model.common;
12

    
13
import java.util.ArrayList;
14
import java.util.Comparator;
15
import java.util.List;
16

    
17
import javax.persistence.Entity;
18

    
19
import org.apache.log4j.Logger;
20
import org.joda.time.DateTime;
21

    
22
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
23

    
24
/**
25
 * @author a.mueller
26
 * @created 07.09.2009
27
 */
28
@Entity
29
public class CdmMetaData extends CdmBase{
30
	private static final long serialVersionUID = -3033376680593279078L;
31
	@SuppressWarnings("unused")
32
	private static final Logger logger = Logger.getLogger(CdmMetaData.class);
33

    
34
	/* It is a little bit confusing that this specific information is located in
35
	 * a generic class for metadata. Think about moving the schema version 
36
	 *  
37
	 */
38
	/**
39
	 * The database schema version number.
40
	 * It is recommended to have the first two numbers equal to the CDM Library version number.
41
	 * But it is not obligatory as there may be cases when the library number changes but the
42
	 * schema version is not changing.
43
	 * The third should be incremented if the schema changes in a way that SCHEMA_VALIDATION.UPDATE
44
	 * will probably not work or will not be enough to transform old data into new data.
45
	 * The fourth number should be incremented when minor schema changes take place that can
46
	 * be handled by SCHEMA_VALIDATION.UPDATE
47
	 * The last number represents the date of change.
48
	 */
49
//	private static final String dbSchemaVersion = "3.0.1.0.201104190000";	
50
	private static final String dbSchemaVersion = "3.3.0.0.201308010000";	
51
	
52
	
53

    
54
	/**
55
	 * @return a list of default metadata objects 
56
	 */
57
	public static final List<CdmMetaData> defaultMetaData(){
58
		List<CdmMetaData> result = new ArrayList<CdmMetaData>();
59
		// schema version
60
		result.add(new CdmMetaData(MetaDataPropertyName.DB_SCHEMA_VERSION, dbSchemaVersion));
61
		//term version
62
		result.add(new CdmMetaData(MetaDataPropertyName.TERMS_VERSION, termsVersion));
63
		// database create time
64
		result.add(new CdmMetaData(MetaDataPropertyName.DB_CREATE_DATE, new DateTime().toString()));
65
		return result;	
66
	}
67
	
68
	/**
69
	 * The version number for the terms loaded by the termloader (csv-files)
70
	 * It is recommended to have the first two numbers equal to the CDM Library version number.
71
	 * 
72
	 * But it is not obligatory as there may be cases when the library number changes but the
73
	 * schema version is not changing.
74
	 * 
75
	 * The third should be incremented if the terms change in a way that is not compatible
76
	 * to the previous version (e.g. by changing the type of a term)
77
	 * 
78
	 * The fourth number should be incremented when compatible term changes take place
79
	 * (e.g. when new terms were added)
80
	 * 
81
	 * The last number represents the date of change.
82
	 */
83
	private static final String termsVersion = "3.0.1.5.201109280000";
84
	
85
	
86
	public enum MetaDataPropertyName{
87
		DB_SCHEMA_VERSION,
88
		TERMS_VERSION,
89
 		DB_CREATE_DATE,
90
		DB_CREATE_NOTE;
91
		
92
		public String getSqlQuery(){
93
			return "SELECT value FROM CdmMetaData WHERE propertyname=" + this.ordinal();
94
		}
95
	}
96
	
97
	/* END OF CONFUSION */
98
	private MetaDataPropertyName propertyName;
99
	private String value;
100

    
101
	
102
	/**
103
	 * Method to retrieve a CDM Libraries meta data
104
	 * @return
105
	 */
106
	public static final List<CdmMetaData> propertyList(){
107
		List<CdmMetaData> result = new ArrayList<CdmMetaData>();
108
		result.add(new CdmMetaData(MetaDataPropertyName.DB_SCHEMA_VERSION, dbSchemaVersion));
109
		result.add(new CdmMetaData(MetaDataPropertyName.TERMS_VERSION, termsVersion));
110
		result.add(new CdmMetaData(MetaDataPropertyName.DB_CREATE_DATE, new DateTime().toString()));
111
		return result;
112
	}
113

    
114
//********************* Constructor *********************************************/	
115

    
116
	/**
117
	 * Simple constructor to be used by Spring
118
	 */
119
	protected CdmMetaData(){
120
		super();
121
	}
122

    
123
	public CdmMetaData(MetaDataPropertyName propertyName, String value) {
124
		super();
125
		this.propertyName = propertyName;
126
		this.value = value;
127
	}
128

    
129
//****************** instance methods ****************************************/	
130
	
131
	/**
132
	 * @return the propertyName
133
	 */
134
	public MetaDataPropertyName getPropertyName() {
135
		return propertyName;
136
	}
137

    
138
	/**
139
	 * @param propertyName the propertyName to set
140
	 */
141
	public void setPropertyName(MetaDataPropertyName propertyName) {
142
		this.propertyName = propertyName;
143
	}
144

    
145
	/**
146
	 * @return the value
147
	 */
148
	public String getValue() {
149
		return value;
150
	}
151

    
152
	/**
153
	 * @param value the value to set
154
	 */
155
	public void setValue(String value) {
156
		this.value = value;
157
	}
158

    
159
//******************** Version comparator **********************************/
160
	
161
	public static class VersionComparator implements Comparator<String>{
162
		Integer depth;
163
		IProgressMonitor monitor;
164
		
165
		public VersionComparator(Integer depth, IProgressMonitor monitor){
166
			this.depth = depth;
167
			this.monitor = monitor;
168
		}
169
		
170
		/* (non-Javadoc)
171
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
172
		 */
173
		public int compare(String version1, String version2) {
174
			int result = 0;
175
			String[] version1Split = version1.split("\\.");
176
			String[] version2Split = version2.split("\\.");
177
			
178
			if(version1Split.length == 1 || version2Split.length == 1){
179
				throwException("Tried to compare version but given Strings don't seem to " +
180
						"contain version numbers. version1: " + version1 + ", version2:" + version2); 
181
			}
182
			
183
			if(depth != null && (version1Split.length < depth || version2Split.length < depth )){
184
				throwException("Desired depth can not be achieved with the given strings. depth: " + depth  + ", version1: " + version1 + ", version2:" + version2); 
185
			}			
186
			
187
			int length = (depth == null ||version1Split.length < depth) ? version1Split.length : depth;
188
			for (int i = 0; i < length; i++){
189
				Long version1Part = Long.valueOf(version1Split[i]);
190
				Long version2Part = Long.valueOf(version2Split[i]);
191
				int partCompare = version1Part.compareTo(version2Part);
192
				if (partCompare != 0){
193
					return partCompare;
194
				}
195
			}
196
			return result;
197
		}
198
		
199
		private Throwable throwException(String message){
200
			RuntimeException exception =  new RuntimeException(message);
201
			if (monitor != null){
202
				monitor.warning(message, exception);
203
			}
204
			throw exception;
205
		}
206
		
207
	}
208

    
209
	/**
210
	 * Compares two version string. If version1 is higher than version2 a positive result is returned.
211
	 * If both are equal 0 is returned, otherwise -1 is returned.
212
	 * @see Comparator#compare(Object, Object)
213
	 * @param version1
214
	 * @param version2
215
	 * @param depth
216
	 * @param monitor
217
	 * @return
218
	 */
219
	public static int compareVersion(String version1, String version2, Integer depth, IProgressMonitor monitor){
220
		VersionComparator versionComparator = new VersionComparator(depth, monitor);
221
		return versionComparator.compare(version1, version2);
222
	}
223
	
224
	public static boolean isDbSchemaVersionCompatible(String version){
225
		return compareVersion(dbSchemaVersion, version, 3, null) == 0;
226
	}
227

    
228
	public static String getDbSchemaVersion() {
229
		return dbSchemaVersion;
230
	}
231
	
232
	public static String getTermsVersion() {
233
		return termsVersion;
234
	}
235

    
236
	public static boolean isTermsVersionCompatible(String version){
237
		return compareVersion(termsVersion, version, 3, null) == 0;
238
	}
239
	
240
}
(6-6/70)