3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.cdm
.model
.metadata
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Comparator
;
15 import java
.util
.List
;
17 import javax
.persistence
.Entity
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.joda
.time
.DateTime
;
22 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
23 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
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);
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
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.
50 // private static final String dbSchemaVersion = "3.0.1.0.201104190000";
51 // private static final String dbSchemaVersion = "3.3.0.0.201309240000";
52 private static final String dbSchemaVersion
= "3.3.1.0.201401140000";
57 * @return a list of default metadata objects
59 public static final List
<CdmMetaData
> defaultMetaData(){
60 List
<CdmMetaData
> result
= new ArrayList
<CdmMetaData
>();
62 result
.add(new CdmMetaData(MetaDataPropertyName
.DB_SCHEMA_VERSION
, dbSchemaVersion
));
64 result
.add(new CdmMetaData(MetaDataPropertyName
.TERMS_VERSION
, termsVersion
));
65 // database create time
66 result
.add(new CdmMetaData(MetaDataPropertyName
.DB_CREATE_DATE
, new DateTime().toString()));
71 * The version number for the terms loaded by the termloader (csv-files)
72 * It is recommended to have the first two numbers equal to the CDM Library version number.
74 * But it is not obligatory as there may be cases when the library number changes but the
75 * schema version is not changing.
77 * The third should be incremented if the terms change in a way that is not compatible
78 * to the previous version (e.g. by changing the type of a term)
80 * The fourth number should be incremented when compatible term changes take place
81 * (e.g. when new terms were added)
83 * The last number represents the date of change.
85 private static final String termsVersion
= "3.3.0.0.201309010000";
88 public enum MetaDataPropertyName
{
94 public String
getSqlQuery(){
95 return "SELECT value FROM CdmMetaData WHERE propertyname=" + this.ordinal();
99 /* END OF CONFUSION */
100 private MetaDataPropertyName propertyName
;
101 private String value
;
105 * Method to retrieve a CDM Libraries meta data
108 public static final List
<CdmMetaData
> propertyList(){
109 List
<CdmMetaData
> result
= new ArrayList
<CdmMetaData
>();
110 result
.add(new CdmMetaData(MetaDataPropertyName
.DB_SCHEMA_VERSION
, dbSchemaVersion
));
111 result
.add(new CdmMetaData(MetaDataPropertyName
.TERMS_VERSION
, termsVersion
));
112 result
.add(new CdmMetaData(MetaDataPropertyName
.DB_CREATE_DATE
, new DateTime().toString()));
116 //********************* Constructor *********************************************/
119 * Simple constructor to be used by Spring
121 protected CdmMetaData(){
125 public CdmMetaData(MetaDataPropertyName propertyName
, String value
) {
127 this.propertyName
= propertyName
;
131 //****************** instance methods ****************************************/
134 * @return the propertyName
136 public MetaDataPropertyName
getPropertyName() {
141 * @param propertyName the propertyName to set
143 public void setPropertyName(MetaDataPropertyName propertyName
) {
144 this.propertyName
= propertyName
;
150 public String
getValue() {
155 * @param value the value to set
157 public void setValue(String value
) {
161 //******************** Version comparator **********************************/
163 public static class VersionComparator
implements Comparator
<String
>{
165 IProgressMonitor monitor
;
167 public VersionComparator(Integer depth
, IProgressMonitor monitor
){
169 this.monitor
= monitor
;
173 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
175 public int compare(String version1
, String version2
) {
177 String
[] version1Split
= version1
.split("\\.");
178 String
[] version2Split
= version2
.split("\\.");
180 if(version1Split
.length
== 1 || version2Split
.length
== 1){
181 throwException("Tried to compare version but given Strings don't seem to " +
182 "contain version numbers. version1: " + version1
+ ", version2:" + version2
);
185 if(depth
!= null && (version1Split
.length
< depth
|| version2Split
.length
< depth
)){
186 throwException("Desired depth can not be achieved with the given strings. depth: " + depth
+ ", version1: " + version1
+ ", version2:" + version2
);
189 int length
= (depth
== null ||version1Split
.length
< depth
) ? version1Split
.length
: depth
;
190 for (int i
= 0; i
< length
; i
++){
191 Long version1Part
= Long
.valueOf(version1Split
[i
]);
192 Long version2Part
= Long
.valueOf(version2Split
[i
]);
193 int partCompare
= version1Part
.compareTo(version2Part
);
194 if (partCompare
!= 0){
201 private Throwable
throwException(String message
){
202 RuntimeException exception
= new RuntimeException(message
);
203 if (monitor
!= null){
204 monitor
.warning(message
, exception
);
212 * Compares two version string. If version1 is higher than version2 a positive result is returned.
213 * If both are equal 0 is returned, otherwise -1 is returned.
214 * @see Comparator#compare(Object, Object)
221 public static int compareVersion(String version1
, String version2
, Integer depth
, IProgressMonitor monitor
){
222 VersionComparator versionComparator
= new VersionComparator(depth
, monitor
);
223 return versionComparator
.compare(version1
, version2
);
226 public static boolean isDbSchemaVersionCompatible(String version
){
227 return compareVersion(dbSchemaVersion
, version
, 3, null) == 0;
230 public static String
getDbSchemaVersion() {
231 return dbSchemaVersion
;
234 public static String
getTermsVersion() {
238 public static boolean isTermsVersionCompatible(String version
){
239 return compareVersion(termsVersion
, version
, 3, null) == 0;