minor
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / CdmMetaData.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.cdm.model.common;
12
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.Map;
16
17 import eu.etaxonomy.cdm.model.common.CdmBase;
18
19 import javax.persistence.Entity;
20
21 import org.apache.log4j.Logger;
22 import org.joda.time.DateTime;
23
24 /**
25 * @author a.mueller
26 * @created 07.09.2009
27 * @version 1.0
28 */
29 @Entity
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);
34
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
37 *
38 */
39 /**
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.
49 */
50 private static final String dbSchemaVersion = "2.4.1.2.201004231015";
51
52
53 /**
54 * @return a list of default metadata objects
55 */
56 public static final List<CdmMetaData> defaultMetaData(){
57 List<CdmMetaData> result = new ArrayList<CdmMetaData>();
58 // schema version
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()));
62 return result;
63 }
64 /**
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.
74 */
75 private static final String termsVersion = "2.4.2.2.201006011715";
76
77
78 public enum MetaDataPropertyName{
79 DB_SCHEMA_VERSION,
80 TERMS_VERSION,
81 DB_CREATE_DATE,
82 DB_CREATE_NOTE
83 }
84
85 /* END OF CONFUSION */
86 private MetaDataPropertyName propertyName;
87 private String value;
88
89
90 /**
91 * Method to retrieve a CDM Libraries meta data
92 * @return
93 */
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()));
99 return result;
100 }
101
102 //********************* Constructor *********************************************/
103
104 /**
105 * Simple constructor to be used by Spring
106 */
107 protected CdmMetaData(){
108 super();
109 }
110
111 public CdmMetaData(MetaDataPropertyName propertyName, String value) {
112 super();
113 this.propertyName = propertyName;
114 this.value = value;
115 }
116
117 //****************** instance methods ****************************************/
118
119 /**
120 * @return the propertyName
121 */
122 public MetaDataPropertyName getPropertyName() {
123 return propertyName;
124 }
125
126 /**
127 * @param propertyName the propertyName to set
128 */
129 public void setPropertyName(MetaDataPropertyName propertyName) {
130 this.propertyName = propertyName;
131 }
132
133 /**
134 * @return the value
135 */
136 public String getValue() {
137 return value;
138 }
139
140 /**
141 * @param value the value to set
142 */
143 public void setValue(String value) {
144 this.value = value;
145 }
146
147 //************************ STATIC SCHEMA VERSION METHODS ************************/
148
149 public static String getCurrentSchemaVersion() {
150 return dbSchemaVersion;
151 }
152
153 /**
154 * Gets the first i parts of the current CdmLibrary schema version.
155 * @param allCommonData
156 * @return current schema version.
157 */
158 public static String getCurrentSchemaVersion(int i) {
159 // Get current schema version
160 String schemaVersion = CdmMetaData.getCurrentSchemaVersion();
161 return getVersion(schemaVersion, i);
162 }
163
164 /**
165 * Gets the first i parts of the passed database schema version.
166 * @param allCommonData
167 * @return database schema version.
168 */
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);
173 }
174
175 //************************ STATIC TERMS VERSION METHODS ************************/
176 public static String getCurrentTermsVersion() {
177 return dbSchemaVersion;
178 }
179
180 /**
181 * Gets the first i parts of the current CdmLibrary terms version.
182 * @param allCommonData
183 * @return current schema version.
184 */
185 public static String getCurrentTermsVersion(int i) {
186 // Get current schema version
187 String schemaVersion = CdmMetaData.getCurrentTermsVersion();
188 return getVersion(schemaVersion, i);
189 }
190
191 /**
192 * Gets the first i parts of the passed database schema version.
193 * @param allCommonData
194 * @return database schema version.
195 */
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);
200 }
201
202
203 //************************ helping methods ************************/
204
205 /**
206 * @param versionProperty
207 * @return Version number as string.
208 */
209 private static String getVersion(String versionProperty, int i) {
210 return versionProperty.substring(0, nthIndexOf(versionProperty, ".", i));
211 }
212
213 /**
214 * Calculates the n-th occurrence of a string.
215 * @param versionProperty
216 * @return Index of N-th occurence of a string.
217 */
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);
222 }
223 return currentIndex;
224 }
225
226
227
228 }