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
.List
;
17 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
19 import javax
.persistence
.Entity
;
21 import org
.apache
.log4j
.Logger
;
22 import org
.joda
.time
.DateTime
;
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 shoud 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
= "2.1.2.2.200909301715";
54 * @return a list of default metadata objects
56 public static final List
<CdmMetaData
> defaultMetaData(){
57 List
<CdmMetaData
> result
= new ArrayList
<CdmMetaData
>();
59 result
.add(new CdmMetaData(MetaDataPropertyName
.DB_SCHEMA_VERSION
, dbSchemaVersion
));
60 // database create time
61 result
.add(new CdmMetaData(MetaDataPropertyName
.DB_CREATE_DATE
, new DateTime().toString()));
65 * The version number for the terms loaded by the termloader (csv-files)
66 * It is recommended to have the first two numbers equal to the CDM Library version number.
67 * But it is not obligatory as there may be cases when the library number changes but the
68 * schema version is not changing.
69 * The third should be incremented if the terms change in a way that is not compatible
70 * to the previous version (e.g. by changing the type of a term)
71 * The fourth number shoud be incremented when compatible term changes take place
72 * (e.g. when new terms were added)
73 * The last number represents the date of change.
75 private static final String termsVersion
= "2.1.2.3.201003091500";
78 public enum MetaDataPropertyName
{
85 /* END OF CONFUSION */
86 private MetaDataPropertyName propertyName
;
91 * Method to retrieve a CDM Libraries meta data
94 public static final List
<CdmMetaData
> propertyList(){
95 List
<CdmMetaData
> result
= new ArrayList
<CdmMetaData
>();
96 result
.add(new CdmMetaData(MetaDataPropertyName
.DB_SCHEMA_VERSION
, dbSchemaVersion
));
97 result
.add(new CdmMetaData(MetaDataPropertyName
.TERMS_VERSION
, termsVersion
));
98 result
.add(new CdmMetaData(MetaDataPropertyName
.DB_CREATE_DATE
, new DateTime().toString()));
102 //********************* Constructor *********************************************/
105 * Simple constructor to be used by Spring
107 protected CdmMetaData(){
111 public CdmMetaData(MetaDataPropertyName propertyName
, String value
) {
113 this.propertyName
= propertyName
;
117 //****************** instance methods ****************************************/
120 * @return the propertyName
122 public MetaDataPropertyName
getPropertyName() {
127 * @param propertyName the propertyName to set
129 public void setPropertyName(MetaDataPropertyName propertyName
) {
130 this.propertyName
= propertyName
;
136 public String
getValue() {
141 * @param value the value to set
143 public void setValue(String value
) {
147 //************************ STATIC SCHEMA VERSION METHODS ************************/
149 public static String
getCurrentSchemaVersion() {
150 return dbSchemaVersion
;
154 * Gets the first i parts of the current CdmLibrary schema version.
155 * @param allCommonData
156 * @return current schema version.
158 public static String
getCurrentSchemaVersion(int i
) {
159 // Get current schema version
160 String schemaVersion
= CdmMetaData
.getCurrentSchemaVersion();
161 return getVersion(schemaVersion
, i
);
165 * Gets the first i parts of the passed database schema version.
166 * @param allCommonData
167 * @return database schema version.
169 public static String
getDatabaseSchemaVersion(Map
<MetaDataPropertyName
, CdmMetaData
> cdmMetaDataFromDatabase
, int i
) {
170 // Get database schema version
171 String schemaVersion
= cdmMetaDataFromDatabase
.get(MetaDataPropertyName
.DB_SCHEMA_VERSION
).getValue();
172 return getVersion(schemaVersion
, i
);
175 //************************ STATIC TERMS VERSION METHODS ************************/
176 public static String
getCurrentTermsVersion() {
177 return dbSchemaVersion
;
181 * Gets the first i parts of the current CdmLibrary terms version.
182 * @param allCommonData
183 * @return current schema version.
185 public static String
getCurrentTermsVersion(int i
) {
186 // Get current schema version
187 String schemaVersion
= CdmMetaData
.getCurrentTermsVersion();
188 return getVersion(schemaVersion
, i
);
192 * Gets the first i parts of the passed database schema version.
193 * @param allCommonData
194 * @return database schema version.
196 public static String
getDatabaseTermsVersion(Map
<MetaDataPropertyName
, CdmMetaData
> cdmMetaDataFromDatabase
, int i
) {
197 // Get database schema version
198 String termsVersion
= cdmMetaDataFromDatabase
.get(MetaDataPropertyName
.TERMS_VERSION
).getValue();
199 return getVersion(termsVersion
, i
);
203 //************************ helping methods ************************/
206 * @param versionProperty
207 * @return Version number as string.
209 private static String
getVersion(String versionProperty
, int i
) {
210 return versionProperty
.substring(0, nthIndexOf(versionProperty
, ".", i
));
214 * Calculates the n-th occurrence of a string.
215 * @param versionProperty
216 * @return Index of N-th occurence of a string.
218 private static int nthIndexOf(String versionProperty
, String pattern
, int n
) {
219 int currentIndex
= -1;
220 for (int i
=0; i
<n
; i
++) {
221 currentIndex
= versionProperty
.indexOf(pattern
, currentIndex
+ 1);