Project

General

Profile

Download (7.36 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.metadata;
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
import eu.etaxonomy.cdm.model.common.CdmBase;
24

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

    
35
	/* 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
	/**
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
	 * The fourth number should be incremented when minor schema changes take place that can
47
	 * be handled by SCHEMA_VALIDATION.UPDATE
48
	 * The last number represents the date of change.
49
	 */
50
//	private static final String dbSchemaVersion = "3.6.0.0.201527040000";
51
	private static final String dbSchemaVersion = "4.0.0.0.201604200000";
52
//	private static final String dbSchemaVersion = "3.5.0.0.201531030000";
53

    
54

    
55

    
56

    
57

    
58
	/**
59
	 * @return a list of default metadata objects
60
	 */
61
	public static final List<CdmMetaData> defaultMetaData(){
62
		List<CdmMetaData> result = new ArrayList<CdmMetaData>();
63
		// schema version
64
		result.add(new CdmMetaData(MetaDataPropertyName.DB_SCHEMA_VERSION, dbSchemaVersion));
65
		//term version
66
		result.add(new CdmMetaData(MetaDataPropertyName.TERMS_VERSION, termsVersion));
67
		// database create time
68
		result.add(new CdmMetaData(MetaDataPropertyName.DB_CREATE_DATE, new DateTime().toString()));
69
		return result;
70
	}
71

    
72
	/**
73
	 * The version number for the terms loaded by the termloader (csv-files)
74
	 * It is recommended to have the first two numbers equal to the CDM Library version number.
75
	 *
76
	 * But it is not obligatory as there may be cases when the library number changes but the
77
	 * schema version is not changing.
78
	 *
79
	 * The third should be incremented if the terms change in a way that is not compatible
80
	 * to the previous version (e.g. by changing the type of a term)
81
	 *
82
	 * The fourth number should be incremented when compatible term changes take place
83
	 * (e.g. when new terms were added)
84
	 *
85
	 * The last number represents the date of change.
86
	 */
87
//	private static final String termsVersion = "3.6.0.0.201527040000";
88
	private static final String termsVersion = "4.0.0.0.201604200000";
89

    
90

    
91
	public enum MetaDataPropertyName{
92
		DB_SCHEMA_VERSION,
93
		TERMS_VERSION,
94
 		DB_CREATE_DATE,
95
		DB_CREATE_NOTE;
96

    
97
		public String getSqlQuery(){
98
			return "SELECT value FROM CdmMetaData WHERE propertyname=" + this.ordinal();
99
		}
100
	}
101

    
102
	/* END OF CONFUSION */
103
	private MetaDataPropertyName propertyName;
104
	private String value;
105

    
106

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

    
119
//********************* Constructor *********************************************/
120

    
121
	/**
122
	 * Simple constructor to be used by Spring
123
	 */
124
	protected CdmMetaData(){
125
		super();
126
	}
127

    
128
	public CdmMetaData(MetaDataPropertyName propertyName, String value) {
129
		super();
130
		this.propertyName = propertyName;
131
		this.value = value;
132
	}
133

    
134
//****************** instance methods ****************************************/
135

    
136
	/**
137
	 * @return the propertyName
138
	 */
139
	public MetaDataPropertyName getPropertyName() {
140
		return propertyName;
141
	}
142

    
143
	/**
144
	 * @param propertyName the propertyName to set
145
	 */
146
	public void setPropertyName(MetaDataPropertyName propertyName) {
147
		this.propertyName = propertyName;
148
	}
149

    
150
	/**
151
	 * @return the value
152
	 */
153
	public String getValue() {
154
		return value;
155
	}
156

    
157
	/**
158
	 * @param value the value to set
159
	 */
160
	public void setValue(String value) {
161
		this.value = value;
162
	}
163

    
164
//******************** Version comparator **********************************/
165

    
166
	public static class VersionComparator implements Comparator<String>{
167
		Integer depth;
168
		IProgressMonitor monitor;
169

    
170
		public VersionComparator(Integer depth, IProgressMonitor monitor){
171
			this.depth = depth;
172
			this.monitor = monitor;
173
		}
174

    
175
		/* (non-Javadoc)
176
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
177
		 */
178
		@Override
179
        public int compare(String version1, String version2) {
180
			int result = 0;
181
			String[] version1Split = version1.split("\\.");
182
			String[] version2Split = version2.split("\\.");
183

    
184
			if(version1Split.length == 1 || version2Split.length == 1){
185
				throwException("Tried to compare version but given Strings don't seem to " +
186
						"contain version numbers. version1: " + version1 + ", version2:" + version2);
187
			}
188

    
189
			if(depth != null && (version1Split.length < depth || version2Split.length < depth )){
190
				throwException("Desired depth can not be achieved with the given strings. depth: " + depth  + ", version1: " + version1 + ", version2:" + version2);
191
			}
192

    
193
			int length = (depth == null ||version1Split.length < depth) ? version1Split.length : depth;
194
			for (int i = 0; i < length; i++){
195
				Long version1Part = Long.valueOf(version1Split[i]);
196
				Long version2Part = Long.valueOf(version2Split[i]);
197
				int partCompare = version1Part.compareTo(version2Part);
198
				if (partCompare != 0){
199
					return partCompare;
200
				}
201
			}
202
			return result;
203
		}
204

    
205
		private Throwable throwException(String message){
206
			RuntimeException exception =  new RuntimeException(message);
207
			if (monitor != null){
208
				monitor.warning(message, exception);
209
			}
210
			throw exception;
211
		}
212

    
213
	}
214

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

    
230
	public static boolean isDbSchemaVersionCompatible(String version){
231
		return compareVersion(dbSchemaVersion, version, 3, null) == 0;
232
	}
233

    
234
	public static String getDbSchemaVersion() {
235
		return dbSchemaVersion;
236
	}
237

    
238
	public static String getTermsVersion() {
239
		return termsVersion;
240
	}
241

    
242
	public static boolean isTermsVersionCompatible(String version){
243
		return compareVersion(termsVersion, version, 3, null) == 0;
244
	}
245

    
246
}
(1-1/4)