2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.model
.metadata
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Comparator
;
14 import java
.util
.List
;
15 import java
.util
.UUID
;
17 import javax
.persistence
.Column
;
18 import javax
.persistence
.Entity
;
19 import javax
.validation
.constraints
.NotNull
;
20 import javax
.xml
.bind
.annotation
.XmlAttribute
;
22 import org
.apache
.log4j
.Logger
;
23 import org
.hibernate
.annotations
.Type
;
24 import org
.joda
.time
.DateTime
;
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
;
37 public class CdmMetaData
extends CdmBase
{
39 private static final long serialVersionUID
= -3033376680593279078L;
40 @SuppressWarnings("unused")
41 private static final Logger logger
= Logger
.getLogger(CdmMetaData
.class);
43 private static final String UNNAMED
= "- UNNAMED -";
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.
56 private static final String dbSchemaVersion
= CdmVersion
.V_05_18_06
.versionString
;
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")
72 private String versionString
;
73 private CdmVersion(String versionString
){
74 this.versionString
= versionString
;
76 public String
versionString(){
82 * The {@link TermType type} of this term. Needs to be the same type in a {@link DefinedTermBase defined term}
83 * and in it's {@link TermVocabulary vocabulary}.
85 @XmlAttribute(name
="PropertyName")
86 @Column(name
="propertyName", length
=20)
88 @Type(type
= "eu.etaxonomy.cdm.hibernate.EnumUserType",
89 parameters
= {@org.hibernate
.annotations
.Parameter(name
= "enumClass", value
= "eu.etaxonomy.cdm.model.metadata.CdmMetaDataPropertyName")}
91 private CdmMetaDataPropertyName propertyName
;
95 //********************* Constructor *********************************************/
98 * Simple constructor to be used by Spring
100 protected CdmMetaData(){
104 public CdmMetaData(CdmMetaDataPropertyName propertyName
, String value
) {
106 this.propertyName
= propertyName
;
110 // ******************** STATIC **********************************/
113 * @return a list of default metadata objects
115 public static final List
<CdmMetaData
> defaultMetaData(){
116 List
<CdmMetaData
> result
= new ArrayList
<>();
118 result
.add(new CdmMetaData(CdmMetaDataPropertyName
.DB_SCHEMA_VERSION
, dbSchemaVersion
));
119 // database create time
120 result
.add(new CdmMetaData(CdmMetaDataPropertyName
.DB_CREATE_DATE
, new DateTime().toString()));
121 result
.add(new CdmMetaData(CdmMetaDataPropertyName
.INSTANCE_ID
, UUID
.randomUUID().toString()));
122 result
.add(new CdmMetaData(CdmMetaDataPropertyName
.INSTANCE_NAME
, UNNAMED
));
126 //****************** instance methods ****************************/
128 public CdmMetaDataPropertyName
getPropertyName() {
131 public void setPropertyName(CdmMetaDataPropertyName propertyName
) {
132 this.propertyName
= propertyName
;
135 public String
getValue() {
138 public void setValue(String value
) {
142 //******************** Version comparator **********************************/
144 public static class VersionComparator
implements Comparator
<String
>{
146 IProgressMonitor monitor
;
148 public VersionComparator(Integer depth
, IProgressMonitor monitor
){
150 this.monitor
= monitor
;
154 public int compare(String version1
, String version2
) {
157 if (version1
.equals(version2
)){
161 String
[] version1Split
= version1
.split("\\.");
162 String
[] version2Split
= version2
.split("\\.");
164 if(version1Split
.length
== 1 || version2Split
.length
== 1){
165 throwException("Tried to compare version but given Strings don't seem to " +
166 "contain version numbers. version1: " + version1
+ ", version2:" + version2
);
169 if(depth
!= null && (version1Split
.length
< depth
|| version2Split
.length
< depth
)){
170 throwException("Desired depth can not be achieved with the given strings. depth: " + depth
+ ", version1: " + version1
+ ", version2:" + version2
);
172 //use the shorter version to avoid arrayOutOfBoundsException, if version2Split.length < version1Split.length but > depth
173 int length
= (version1Split
.length
< version2Split
.length
) ? version1Split
.length
: version2Split
.length
;
175 length
= length
<depth?length
:depth
;
177 for (int i
= 0; i
< length
; i
++){
178 Long version1Part
= Long
.valueOf(version1Split
[i
]);
179 Long version2Part
= Long
.valueOf(version2Split
[i
]);
180 int partCompare
= version1Part
.compareTo(version2Part
);
181 if (partCompare
!= 0){
188 private Throwable
throwException(String message
){
189 RuntimeException exception
= new RuntimeException(message
);
190 if (monitor
!= null){
191 monitor
.warning(message
, exception
);
198 * Compares two version string. If version1 is higher than version2 a positive result is returned.
199 * If both are equal 0 is returned, otherwise -1 is returned.
200 * @see Comparator#compare(Object, Object)
203 public static int compareVersion(String version1
, String version2
, Integer depth
, IProgressMonitor monitor
){
204 VersionComparator versionComparator
= new VersionComparator(depth
, monitor
);
205 return versionComparator
.compare(version1
, version2
);
208 public static boolean isDbSchemaVersionCompatible(String version
){
209 return compareVersion(dbSchemaVersion
, version
, 3, null) == 0;
212 public static String
getDbSchemaVersion() {
213 return dbSchemaVersion
;