Project

General

Profile

Download (7.2 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
import java.util.UUID;
16

    
17
import javax.persistence.Column;
18
import javax.persistence.Entity;
19
import javax.validation.constraints.NotNull;
20
import javax.xml.bind.annotation.XmlAttribute;
21

    
22
import org.apache.log4j.Logger;
23
import org.hibernate.annotations.Type;
24
import org.joda.time.DateTime;
25

    
26
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
29
import eu.etaxonomy.cdm.model.term.TermType;
30
import eu.etaxonomy.cdm.model.term.TermVocabulary;
31

    
32
/**
33
 * @author a.mueller
34
 * @since 07.09.2009
35
 */
36
@Entity
37
public class CdmMetaData extends CdmBase{
38

    
39
    private static final long serialVersionUID = -3033376680593279078L;
40
	@SuppressWarnings("unused")
41
	private static final Logger logger = Logger.getLogger(CdmMetaData.class);
42

    
43
	private static final String UNNAMED = "- UNNAMED -";
44

    
45
	/**
46
	 * The database schema version number.
47
	 * It is recommended to have the first two numbers equal to the CDM Library version number.
48
	 * But it is not obligatory as there may be cases when the library number changes but the
49
	 * schema version is not changing.
50
	 * The third should be incremented if the schema changes in a way that SCHEMA_VALIDATION.UPDATE
51
	 * will probably not work or will not be enough to transform old data into new data.
52
	 * The fourth number should be incremented when minor schema changes take place that can
53
	 * be handled by SCHEMA_VALIDATION.UPDATE
54
	 * The last number represents the date of change.
55
	 */
56
	private static final String dbSchemaVersion = CdmVersion.V_05_25_01.versionString;
57

    
58
	public enum CdmVersion {
59
	    V_05_12_00("5.12.0.0.20191202"),
60
        V_05_15_00("5.15.0.0.20200510"),
61
	    V_05_15_01("5.15.1.0.20200610"),
62
	    V_05_15_02("5.15.2.0.20200611"),
63
	    V_05_18_00("5.18.0.0.20200902"),
64
        V_05_18_01("5.18.1.0.20200914"),
65
        V_05_18_02("5.18.2.0.20200921"),
66
        V_05_18_03("5.18.3.0.20200924"),
67
        V_05_18_04("5.18.4.0.20201020"),
68
        V_05_18_05("5.18.5.0.20201103"),
69
        V_05_18_06("5.18.6.0.20201124"),
70
        V_05_22_00("5.22.0.0.20210315"),
71
        V_05_23_00("5.23.0.0.20210422"),
72
        V_05_25_00("5.25.0.0.20210609"),
73
        V_05_25_01("5.25.1.0.20210702")
74
        ;
75
        private String versionString;
76
	    private CdmVersion(String versionString){
77
	        this.versionString = versionString;
78
	    }
79
	    public String versionString(){
80
	        return versionString;
81
	    }
82
	}
83

    
84
	/**
85
     * The {@link TermType type} of this term. Needs to be the same type in a {@link DefinedTermBase defined term}
86
     * and in it's {@link TermVocabulary vocabulary}.
87
     */
88
    @XmlAttribute(name ="PropertyName")
89
    @Column(name="propertyName", length=20)
90
    @NotNull
91
    @Type(type = "eu.etaxonomy.cdm.hibernate.EnumUserType",
92
        parameters = {@org.hibernate.annotations.Parameter(name = "enumClass", value = "eu.etaxonomy.cdm.model.metadata.CdmMetaDataPropertyName")}
93
    )
94
	private CdmMetaDataPropertyName propertyName;
95
	private String value;
96

    
97

    
98
//********************* Constructor *********************************************/
99

    
100
	/**
101
	 * Simple constructor to be used by Spring
102
	 */
103
	protected CdmMetaData(){
104
		super();
105
	}
106

    
107
	public CdmMetaData(CdmMetaDataPropertyName propertyName, String value) {
108
		super();
109
		this.propertyName = propertyName;
110
		this.value = value;
111
	}
112

    
113
// ******************** STATIC **********************************/
114

    
115
    /**
116
     * @return a list of default metadata objects
117
     */
118
    public static final List<CdmMetaData> defaultMetaData(){
119
        List<CdmMetaData> result = new ArrayList<>();
120
        // schema version
121
        result.add(new CdmMetaData(CdmMetaDataPropertyName.DB_SCHEMA_VERSION, dbSchemaVersion));
122
        // database create time
123
        result.add(new CdmMetaData(CdmMetaDataPropertyName.DB_CREATE_DATE, new DateTime().toString()));
124
        result.add(new CdmMetaData(CdmMetaDataPropertyName.INSTANCE_ID, UUID.randomUUID().toString()));
125
        result.add(new CdmMetaData(CdmMetaDataPropertyName.INSTANCE_NAME, UNNAMED));
126
        return result;
127
    }
128

    
129
//****************** instance methods ****************************/
130

    
131
	public CdmMetaDataPropertyName getPropertyName() {
132
		return propertyName;
133
	}
134
	public void setPropertyName(CdmMetaDataPropertyName propertyName) {
135
		this.propertyName = propertyName;
136
	}
137

    
138
	public String getValue() {
139
		return value;
140
	}
141
	public void setValue(String value) {
142
		this.value = value;
143
	}
144

    
145
//******************** Version comparator **********************************/
146

    
147
	public static class VersionComparator implements Comparator<String>{
148
		Integer depth;
149
		IProgressMonitor monitor;
150

    
151
		public VersionComparator(Integer depth, IProgressMonitor monitor){
152
			this.depth = depth;
153
			this.monitor = monitor;
154
		}
155

    
156
		@Override
157
        public int compare(String version1, String version2) {
158
			int result = 0;
159

    
160
			if (version1.equals(version2)){
161
			    return 0;
162
			}
163

    
164
			String[] version1Split = version1.split("\\.");
165
			String[] version2Split = version2.split("\\.");
166

    
167
			if(version1Split.length == 1 || version2Split.length == 1){
168
				throwException("Tried to compare version but given Strings don't seem to " +
169
						"contain version numbers. version1: " + version1 + ", version2:" + version2);
170
			}
171

    
172
			if(depth != null && (version1Split.length < depth || version2Split.length < depth )){
173
				throwException("Desired depth can not be achieved with the given strings. depth: " + depth  + ", version1: " + version1 + ", version2:" + version2);
174
			}
175
			//use the shorter version to avoid arrayOutOfBoundsException, if version2Split.length < version1Split.length but > depth
176
			int length = (version1Split.length < version2Split.length) ? version1Split.length: version2Split.length;
177
			if (depth != null){
178
			    length = length<depth?length:depth;
179
			}
180
			for (int i = 0; i < length; i++){
181
				Long version1Part = Long.valueOf(version1Split[i]);
182
				Long version2Part = Long.valueOf(version2Split[i]);
183
				int partCompare = version1Part.compareTo(version2Part);
184
				if (partCompare != 0){
185
					return partCompare;
186
				}
187
			}
188
			return result;
189
		}
190

    
191
		private Throwable throwException(String message){
192
			RuntimeException exception =  new RuntimeException(message);
193
			if (monitor != null){
194
				monitor.warning(message, exception);
195
			}
196
			throw exception;
197
		}
198
	}
199

    
200
	/**
201
	 * Compares two version string. If version1 is higher than version2 a positive result is returned.
202
	 * If both are equal 0 is returned, otherwise -1 is returned.
203
	 * @see Comparator#compare(Object, Object)
204
	 * @return
205
	 */
206
	public static int compareVersion(String version1, String version2, Integer depth, IProgressMonitor monitor){
207
		VersionComparator versionComparator = new VersionComparator(depth, monitor);
208
		return versionComparator.compare(version1, version2);
209
	}
210

    
211
	public static boolean isDbSchemaVersionCompatible(String version){
212
		return compareVersion(dbSchemaVersion, version, 3, null) == 0;
213
	}
214

    
215
	public static String getDbSchemaVersion() {
216
		return dbSchemaVersion;
217
	}
218
}
(1-1/18)