Project

General

Profile

Download (6.93 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.cdm.model.metadata;
11

    
12
import java.util.ArrayList;
13
import java.util.Comparator;
14
import java.util.List;
15
import java.util.UUID;
16

    
17
import javax.persistence.Column;
18
import javax.persistence.Entity;
19
import javax.validation.constraints.NotNull;
20
import javax.xml.bind.annotation.XmlAttribute;
21

    
22
import org.apache.log4j.Logger;
23
import org.hibernate.annotations.Type;
24
import org.joda.time.DateTime;
25

    
26
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
29
import eu.etaxonomy.cdm.model.common.TermType;
30
import eu.etaxonomy.cdm.model.common.TermVocabulary;
31

    
32
/**
33
 * @author a.mueller
34
 * @since 07.09.2009
35
 */
36
@Entity
37
public class CdmMetaData extends CdmBase{
38

    
39
    private static final long serialVersionUID = -3033376680593279078L;
40
	@SuppressWarnings("unused")
41
	private static final Logger logger = Logger.getLogger(CdmMetaData.class);
42

    
43
	private static final String UNNAMED = "- UNNAMED -";
44

    
45

    
46
	/* It is a little bit confusing that this specific information is located in
47
	 * a generic class for metadata. Think about moving the schema version
48
	 *
49
	 */
50
	/**
51
	 * The database schema version number.
52
	 * It is recommended to have the first two numbers equal to the CDM Library version number.
53
	 * But it is not obligatory as there may be cases when the library number changes but the
54
	 * schema version is not changing.
55
	 * The third should be incremented if the schema changes in a way that SCHEMA_VALIDATION.UPDATE
56
	 * will probably not work or will not be enough to transform old data into new data.
57
	 * The fourth number should be incremented when minor schema changes take place that can
58
	 * be handled by SCHEMA_VALIDATION.UPDATE
59
	 * The last number represents the date of change.
60
	 */
61
	private static final String dbSchemaVersion = "5.5.0.0.20190221";
62
//  private static final String dbSchemaVersion = "4.7.0.0.201710040000";
63
//  private static final String dbSchemaVersion = "5.0.0.0.20180514";
64

    
65

    
66
	/* END OF CONFUSION */
67

    
68
	   /**
69
     * The {@link TermType type} of this term. Needs to be the same type in a {@link DefinedTermBase defined term}
70
     * and in it's {@link TermVocabulary vocabulary}.
71
     */
72
    @XmlAttribute(name ="PropertyName")
73
    @Column(name="propertyName", length=20)
74
    @NotNull
75
    @Type(type = "eu.etaxonomy.cdm.hibernate.EnumUserType",
76
        parameters = {@org.hibernate.annotations.Parameter(name  = "enumClass", value = "eu.etaxonomy.cdm.model.metadata.CdmMetaDataPropertyName")}
77
    )
78
	private CdmMetaDataPropertyName propertyName;
79
	private String value;
80

    
81

    
82
//********************* Constructor *********************************************/
83

    
84
	/**
85
	 * Simple constructor to be used by Spring
86
	 */
87
	protected CdmMetaData(){
88
		super();
89
	}
90

    
91
	public CdmMetaData(CdmMetaDataPropertyName propertyName, String value) {
92
		super();
93
		this.propertyName = propertyName;
94
		this.value = value;
95
	}
96

    
97
// ******************** STATIC **********************************/
98

    
99

    
100
    /**
101
     * @return a list of default metadata objects
102
     */
103
    public static final List<CdmMetaData> defaultMetaData(){
104
        List<CdmMetaData> result = new ArrayList<>();
105
        // schema version
106
        result.add(new CdmMetaData(CdmMetaDataPropertyName.DB_SCHEMA_VERSION, dbSchemaVersion));
107
        // database create time
108
        result.add(new CdmMetaData(CdmMetaDataPropertyName.DB_CREATE_DATE, new DateTime().toString()));
109
        result.add(new CdmMetaData(CdmMetaDataPropertyName.INSTANCE_ID, UUID.randomUUID().toString()));
110
        result.add(new CdmMetaData(CdmMetaDataPropertyName.INSTANCE_NAME, UNNAMED));
111
        return result;
112
    }
113

    
114

    
115

    
116
//****************** instance methods ****************************/
117

    
118
	/**
119
	 * @return the propertyName
120
	 */
121
	public CdmMetaDataPropertyName getPropertyName() {
122
		return propertyName;
123
	}
124

    
125
	/**
126
	 * @param propertyName the propertyName to set
127
	 */
128
	public void setPropertyName(CdmMetaDataPropertyName propertyName) {
129
		this.propertyName = propertyName;
130
	}
131

    
132
	/**
133
	 * @return the value
134
	 */
135
	public String getValue() {
136
		return value;
137
	}
138

    
139
	/**
140
	 * @param value the value to set
141
	 */
142
	public void setValue(String value) {
143
		this.value = value;
144
	}
145

    
146
//******************** Version comparator **********************************/
147

    
148
	public static class VersionComparator implements Comparator<String>{
149
		Integer depth;
150
		IProgressMonitor monitor;
151

    
152
		public VersionComparator(Integer depth, IProgressMonitor monitor){
153
			this.depth = depth;
154
			this.monitor = monitor;
155
		}
156

    
157
		@Override
158
        public int compare(String version1, String version2) {
159
			int result = 0;
160

    
161
			if (version1.equals(version2)){
162
			    return 0;
163
			}
164

    
165
			String[] version1Split = version1.split("\\.");
166
			String[] version2Split = version2.split("\\.");
167

    
168
			if(version1Split.length == 1 || version2Split.length == 1){
169
				throwException("Tried to compare version but given Strings don't seem to " +
170
						"contain version numbers. version1: " + version1 + ", version2:" + version2);
171
			}
172

    
173
			if(depth != null && (version1Split.length < depth || version2Split.length < depth )){
174
				throwException("Desired depth can not be achieved with the given strings. depth: " + depth  + ", version1: " + version1 + ", version2:" + version2);
175
			}
176
			//use the shorter version to avoid arrayOutOfBoundsException, if version2Split.length < version1Split.length but > depth
177
			int length = (version1Split.length < version2Split.length) ? version1Split.length: version2Split.length;
178
			if (depth != null){
179
			    length = length<depth?length:depth;
180
			}
181
			for (int i = 0; i < length; i++){
182
				Long version1Part = Long.valueOf(version1Split[i]);
183
				Long version2Part = Long.valueOf(version2Split[i]);
184
				int partCompare = version1Part.compareTo(version2Part);
185
				if (partCompare != 0){
186
					return partCompare;
187
				}
188
			}
189
			return result;
190
		}
191

    
192
		private Throwable throwException(String message){
193
			RuntimeException exception =  new RuntimeException(message);
194
			if (monitor != null){
195
				monitor.warning(message, exception);
196
			}
197
			throw exception;
198
		}
199

    
200
	}
201

    
202
	/**
203
	 * Compares two version string. If version1 is higher than version2 a positive result is returned.
204
	 * If both are equal 0 is returned, otherwise -1 is returned.
205
	 * @see Comparator#compare(Object, Object)
206
	 * @param version1
207
	 * @param version2
208
	 * @param depth
209
	 * @param monitor
210
	 * @return
211
	 */
212
	public static int compareVersion(String version1, String version2, Integer depth, IProgressMonitor monitor){
213
		VersionComparator versionComparator = new VersionComparator(depth, monitor);
214
		return versionComparator.compare(version1, version2);
215
	}
216

    
217
	public static boolean isDbSchemaVersionCompatible(String version){
218
		return compareVersion(dbSchemaVersion, version, 3, null) == 0;
219
	}
220

    
221
	public static String getDbSchemaVersion() {
222
		return dbSchemaVersion;
223
	}
224

    
225
}
(1-1/13)