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
.common
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Comparator
;
15 import java
.util
.List
;
18 import javax
.persistence
.Entity
;
20 import org
.apache
.log4j
.Logger
;
21 import org
.joda
.time
.DateTime
;
23 import eu
.etaxonomy
.cdm
.common
.IProgressMonitor
;
31 public class CdmMetaData
extends CdmBase
{
32 private static final long serialVersionUID
= -3033376680593279078L;
33 @SuppressWarnings("unused")
34 private static final Logger logger
= Logger
.getLogger(CdmMetaData
.class);
36 /* It is a little bit confusing that this specific information is located in
37 * a generic class for metadata. Think about moving the schema version
41 * The database schema version number.
42 * It is recommended to have the first two numbers equal to the CDM Library version number.
43 * But it is not obligatory as there may be cases when the library number changes but the
44 * schema version is not changing.
45 * The third should be incremented if the schema changes in a way that SCHEMA_VALIDATION.UPDATE
46 * will probably not work or will not be enough to transform old data into new data.
47 * The fourth number shoud be incremented when minor schema changes take place that can
48 * be handled by SCHEMA_VALIDATION.UPDATE
49 * The last number represents the date of change.
51 private static final String dbSchemaVersion
= "3.0.0.0.201009201255";
55 * @return a list of default metadata objects
57 public static final List
<CdmMetaData
> defaultMetaData(){
58 List
<CdmMetaData
> result
= new ArrayList
<CdmMetaData
>();
60 result
.add(new CdmMetaData(MetaDataPropertyName
.DB_SCHEMA_VERSION
, dbSchemaVersion
));
62 result
.add(new CdmMetaData(MetaDataPropertyName
.TERMS_VERSION
, termsVersion
));
63 // database create time
64 result
.add(new CdmMetaData(MetaDataPropertyName
.DB_CREATE_DATE
, new DateTime().toString()));
68 * The version number for the terms loaded by the termloader (csv-files)
69 * It is recommended to have the first two numbers equal to the CDM Library version number.
70 * But it is not obligatory as there may be cases when the library number changes but the
71 * schema version is not changing.
72 * The third should be incremented if the terms change in a way that is not compatible
73 * to the previous version (e.g. by changing the type of a term)
74 * The fourth number shoud be incremented when compatible term changes take place
75 * (e.g. when new terms were added)
76 * The last number represents the date of change.
78 private static final String termsVersion
= "3.0.0.0.201009201255";
81 public enum MetaDataPropertyName
{
88 /* END OF CONFUSION */
89 private MetaDataPropertyName propertyName
;
94 * Method to retrieve a CDM Libraries meta data
97 public static final List
<CdmMetaData
> propertyList(){
98 List
<CdmMetaData
> result
= new ArrayList
<CdmMetaData
>();
99 result
.add(new CdmMetaData(MetaDataPropertyName
.DB_SCHEMA_VERSION
, dbSchemaVersion
));
100 result
.add(new CdmMetaData(MetaDataPropertyName
.TERMS_VERSION
, termsVersion
));
101 result
.add(new CdmMetaData(MetaDataPropertyName
.DB_CREATE_DATE
, new DateTime().toString()));
105 //********************* Constructor *********************************************/
108 * Simple constructor to be used by Spring
110 protected CdmMetaData(){
114 public CdmMetaData(MetaDataPropertyName propertyName
, String value
) {
116 this.propertyName
= propertyName
;
120 //****************** instance methods ****************************************/
123 * @return the propertyName
125 public MetaDataPropertyName
getPropertyName() {
130 * @param propertyName the propertyName to set
132 public void setPropertyName(MetaDataPropertyName propertyName
) {
133 this.propertyName
= propertyName
;
139 public String
getValue() {
144 * @param value the value to set
146 public void setValue(String value
) {
150 //******************** Version comparator **********************************/
152 public static class VersionComparator
implements Comparator
<String
>{
154 IProgressMonitor monitor
;
156 public VersionComparator(Integer depth
, IProgressMonitor monitor
){
158 this.monitor
= monitor
;
162 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
164 public int compare(String version1
, String version2
) {
166 String
[] version1Split
= version1
.split("\\.");
167 String
[] version2Split
= version2
.split("\\.");
168 if (version1Split
.length
!= version2Split
.length
){
169 String warning
= "Versionstrings are not compatible: " + version1
+ "<->" + version2
;
170 RuntimeException exception
= new RuntimeException(warning
);
171 if (monitor
!= null){
172 monitor
.warning(warning
, exception
);
176 int length
= (depth
== null ||version1Split
.length
< depth
) ? version1Split
.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){
191 * Compares two version string. If version1 is higher than version2 a positive result is returned.
192 * If both are equal 0 is returned, otherwise -1 is returned.
193 * @see Comparator#compare(Object, Object)
200 public static int compareVersion(String version1
, String version2
, Integer depth
, IProgressMonitor monitor
){
201 VersionComparator versionComparator
= new VersionComparator(depth
, monitor
);
202 return versionComparator
.compare(version1
, version2
);
205 //************************ STATIC SCHEMA VERSION METHODS ************************/
207 public static String
getCurrentSchemaVersion() {
208 return dbSchemaVersion
;
212 * Gets the first i parts of the current CdmLibrary schema version.
213 * @param allCommonData
214 * @return current schema version.
216 public static String
getCurrentSchemaVersion(int i
) {
217 // Get current schema version
218 String schemaVersion
= CdmMetaData
.getCurrentSchemaVersion();
219 return getVersion(schemaVersion
, i
);
223 * Gets the first i parts of the passed database schema version.
224 * @param allCommonData
225 * @return database schema version.
227 public static String
getDatabaseSchemaVersion(Map
<MetaDataPropertyName
, CdmMetaData
> cdmMetaDataFromDatabase
, int i
) {
228 // Get database schema version
229 String schemaVersion
= cdmMetaDataFromDatabase
.get(MetaDataPropertyName
.DB_SCHEMA_VERSION
).getValue();
230 return getVersion(schemaVersion
, i
);
234 public static boolean isVersionEqual(String databaseSchemaVersion
, int index
){
235 String currentSchemaVersionPrefix
= getCurrentSchemaVersion(index
);
236 String databaseSchemaVersionPrefix
= getVersion(databaseSchemaVersion
, index
);
237 if (currentSchemaVersionPrefix
.equals( databaseSchemaVersionPrefix
)) {
244 //************************ STATIC TERMS VERSION METHODS ************************/
245 public static String
getCurrentTermsVersion() {
246 return dbSchemaVersion
;
250 * Gets the first i parts of the current CdmLibrary terms version.
251 * @param allCommonData
252 * @return current schema version.
254 public static String
getCurrentTermsVersion(int i
) {
255 // Get current schema version
256 String schemaVersion
= CdmMetaData
.getCurrentTermsVersion();
257 return getVersion(schemaVersion
, i
);
261 * Gets the first i parts of the passed database schema version.
262 * @param allCommonData
263 * @return database schema version.
265 public static String
getDatabaseTermsVersion(Map
<MetaDataPropertyName
, CdmMetaData
> cdmMetaDataFromDatabase
, int i
) {
266 // Get database schema version
267 String termsVersion
= cdmMetaDataFromDatabase
.get(MetaDataPropertyName
.TERMS_VERSION
).getValue();
268 return getVersion(termsVersion
, i
);
274 //************************ helping methods ************************/
277 * @param versionProperty
278 * @return Version number as string.
280 private static String
getVersion(String versionProperty
, int i
) {
281 return versionProperty
.substring(0, nthIndexOf(versionProperty
, ".", i
));
285 * Calculates the n-th occurrence of a string.
286 * @param versionProperty
287 * @return Index of N-th occurence of a string.
289 private static int nthIndexOf(String versionProperty
, String pattern
, int n
) {
290 int currentIndex
= -1;
291 for (int i
=0; i
<n
; i
++) {
292 currentIndex
= versionProperty
.indexOf(pattern
, currentIndex
+ 1);