Changed DefinedTermBase.media form OneToMany to ManyToMany #560
[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.Comparator;
15 import java.util.List;
16
17 import javax.persistence.Entity;
18
19 import org.apache.log4j.Logger;
20 import org.joda.time.DateTime;
21
22 import eu.etaxonomy.cdm.common.IProgressMonitor;
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 = "3.0.0.1.201101050000";
51 private static final String dbSchemaVersion = "3.0.0.0.201011090000";
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 //term version
61 result.add(new CdmMetaData(MetaDataPropertyName.TERMS_VERSION, termsVersion));
62 // database create time
63 result.add(new CdmMetaData(MetaDataPropertyName.DB_CREATE_DATE, new DateTime().toString()));
64 return result;
65 }
66 /**
67 * The version number for the terms loaded by the termloader (csv-files)
68 * It is recommended to have the first two numbers equal to the CDM Library version number.
69 *
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 *
73 * The third should be incremented if the terms change in a way that is not compatible
74 * to the previous version (e.g. by changing the type of a term)
75 *
76 * The fourth number should be incremented when compatible term changes take place
77 * (e.g. when new terms were added)
78 *
79 * The last number represents the date of change.
80 */
81 private static final String termsVersion = "3.0.1.0.201012150000";
82
83
84 public enum MetaDataPropertyName{
85 DB_SCHEMA_VERSION,
86 TERMS_VERSION,
87 DB_CREATE_DATE,
88 DB_CREATE_NOTE;
89
90 public String getSqlQuery(){
91 return "SELECT value FROM CdmMetaData WHERE propertyname=" + this.ordinal();
92 }
93 }
94
95 /* END OF CONFUSION */
96 private MetaDataPropertyName propertyName;
97 private String value;
98
99
100 /**
101 * Method to retrieve a CDM Libraries meta data
102 * @return
103 */
104 public static final List<CdmMetaData> propertyList(){
105 List<CdmMetaData> result = new ArrayList<CdmMetaData>();
106 result.add(new CdmMetaData(MetaDataPropertyName.DB_SCHEMA_VERSION, dbSchemaVersion));
107 result.add(new CdmMetaData(MetaDataPropertyName.TERMS_VERSION, termsVersion));
108 result.add(new CdmMetaData(MetaDataPropertyName.DB_CREATE_DATE, new DateTime().toString()));
109 return result;
110 }
111
112 //********************* Constructor *********************************************/
113
114 /**
115 * Simple constructor to be used by Spring
116 */
117 protected CdmMetaData(){
118 super();
119 }
120
121 public CdmMetaData(MetaDataPropertyName propertyName, String value) {
122 super();
123 this.propertyName = propertyName;
124 this.value = value;
125 }
126
127 //****************** instance methods ****************************************/
128
129 /**
130 * @return the propertyName
131 */
132 public MetaDataPropertyName getPropertyName() {
133 return propertyName;
134 }
135
136 /**
137 * @param propertyName the propertyName to set
138 */
139 public void setPropertyName(MetaDataPropertyName propertyName) {
140 this.propertyName = propertyName;
141 }
142
143 /**
144 * @return the value
145 */
146 public String getValue() {
147 return value;
148 }
149
150 /**
151 * @param value the value to set
152 */
153 public void setValue(String value) {
154 this.value = value;
155 }
156
157 //******************** Version comparator **********************************/
158
159 public static class VersionComparator implements Comparator<String>{
160 Integer depth;
161 IProgressMonitor monitor;
162
163 public VersionComparator(Integer depth, IProgressMonitor monitor){
164 this.depth = depth;
165 this.monitor = monitor;
166 }
167
168 /* (non-Javadoc)
169 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
170 */
171 public int compare(String version1, String version2) {
172 int result = 0;
173 String[] version1Split = version1.split("\\.");
174 String[] version2Split = version2.split("\\.");
175
176 if(version1Split.length == 1 || version2Split.length == 1){
177 throwException("Tried to compare version but given Strings don't seem to " +
178 "contain version numbers. version1: " + version1 + ", version2:" + version2);
179 }
180
181 if(depth != null && (version1Split.length < depth || version2Split.length < depth )){
182 throwException("Desired depth can not be achieved with the given strings. depth: " + depth + ", version1: " + version1 + ", version2:" + version2);
183 }
184
185 int length = (depth == null ||version1Split.length < depth) ? version1Split.length : depth;
186 for (int i = 0; i < length; i++){
187 Long version1Part = Long.valueOf(version1Split[i]);
188 Long version2Part = Long.valueOf(version2Split[i]);
189 int partCompare = version1Part.compareTo(version2Part);
190 if (partCompare != 0){
191 return partCompare;
192 }
193 }
194 return result;
195 }
196
197 private Throwable throwException(String message){
198 RuntimeException exception = new RuntimeException(message);
199 if (monitor != null){
200 monitor.warning(message, exception);
201 }
202 throw exception;
203 }
204
205 }
206
207 /**
208 * Compares two version string. If version1 is higher than version2 a positive result is returned.
209 * If both are equal 0 is returned, otherwise -1 is returned.
210 * @see Comparator#compare(Object, Object)
211 * @param version1
212 * @param version2
213 * @param depth
214 * @param monitor
215 * @return
216 */
217 public static int compareVersion(String version1, String version2, Integer depth, IProgressMonitor monitor){
218 VersionComparator versionComparator = new VersionComparator(depth, monitor);
219 return versionComparator.compare(version1, version2);
220 }
221
222 public static boolean isDbSchemaVersionCompatible(String version){
223 return compareVersion(dbSchemaVersion, version, 3, null) == 0;
224 }
225
226 public static String getDbSchemaVersion() {
227 return dbSchemaVersion;
228 }
229
230 public static String getTermsVersion() {
231 return termsVersion;
232 }
233
234 public static boolean isTermsVersionCompatible(String version){
235 return compareVersion(termsVersion, version, 3, null) == 0;
236 }
237
238 }