Project

General

Profile

Download (7.35 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 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

    
10
package eu.etaxonomy.cdm.model.metadata;
11

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

    
16
import javax.persistence.Entity;
17

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

    
21
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
22
import eu.etaxonomy.cdm.model.common.CdmBase;
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.6.0.0.201527040000";
50
//	private static final String dbSchemaVersion = "4.0.0.0.201604200000";
51
	private static final String dbSchemaVersion = "4.1.0.0.201607300000";
52

    
53

    
54

    
55

    
56

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

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

    
89

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

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

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

    
105

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

    
118
//********************* Constructor *********************************************/
119

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

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

    
133
//****************** instance methods ****************************************/
134

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

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

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

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

    
163
//******************** Version comparator **********************************/
164

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

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

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

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

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

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

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

    
212
	}
213

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

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

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

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

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

    
245
}
(1-1/6)