root/trunk/cdmlib/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/CdmMetaData.java

Revision 13185, 7.4 kB (checked in by a.mueller, 7 months ago)

move progress monitoring into subpackage and add CancelException?

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