Project

General

Profile

Download (5.99 KB) Statistics
| Branch: | Tag: | Revision:
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
package eu.etaxonomy.cdm.model.metadata;
11

    
12
import java.io.Serializable;
13

    
14
import javax.persistence.Column;
15
import javax.persistence.Embeddable;
16
import javax.persistence.EmbeddedId;
17
import javax.persistence.Entity;
18
import org.hibernate.validator.constraints.Length;
19

    
20

    
21
/**
22
 * This class may hold all prefrences data for a CDM database.
23
 * E.g. one may store what the default nomenclatural code is,
24
 * or which default formatter (cache strategy) to use for a 
25
 * certain class.
26
 * The structure represents a triple where the first item 
27
 * (subject) defines for which object the given information is valid. 
28
 * The second item (predicate) describes the type of information
29
 * and the third item (value) represents the actual value.
30
 * 
31
 *  E.g. for defining a database wide default nomenclatural code
32
 *  you may define a triple ("database", "eu.etaxonomy.cdm.model.name.NomenclaturalCode", "ICZN").
33
 *  The set of allowed values and semantics for each combination 
34
 *  is up to implementing classes.
35
 *  The only restrictions we have is the length of the fields and
36
 *  the fact that the first two items (subject, predicate) do
37
 *  create a unique key.
38
 *  
39
 *  Size of single fields may be enlarged in future versions. "Value" may
40
 *  become a CLOB.
41
 * 
42
 * @author a.mueller
43
 * @created 03.07.2013
44
 */
45
@Entity
46
public final class CdmPreference implements Serializable {
47
	private static final long serialVersionUID = 4307599154287181582L;
48

    
49
	
50
	public static final CdmPreference NewInstance(PreferenceSubject subject, PreferencePredicate predicate, String value){
51
		return new CdmPreference(subject, predicate, value);
52
	}
53
	
54
	public static PrefKey NewKey(PreferenceSubject subject, PreferencePredicate predicate){
55
		return new PrefKey(subject, predicate);
56
	}
57
	
58
	@Embeddable
59
	public static class PrefKey implements Serializable{
60
		private static final long serialVersionUID = 9019957853773606194L;
61

    
62
		@Column(name="key_subject", length=100) //for now we keep the combined key short as indizes for such keys are very limited in size in some DBMS. Size may be increased later
63
		private String subject;
64
		
65
		@Column(name="key_predicate", length=100) //for now we keep the combined key short as indizes for such keys are very limited in size in some DBMS. Size may be increased later
66
		private String predicate;
67
		
68
		//for hibernate use only
69
		private PrefKey(){}
70
		
71

    
72
		private PrefKey(PreferenceSubject subject, PreferencePredicate predicate){
73
			this(subject.getKey(), predicate.getKey());
74
		}
75
		
76
		private PrefKey(String subject, String predicate){
77
			if (subject == null) throw new IllegalArgumentException("Subject must not be null for preference");
78
			if (predicate == null) throw new IllegalArgumentException("Predicate must not be null for preference");
79
			if (subject.length() > 255) throw new IllegalArgumentException("Subject must not be longer then 255 for preference");
80
			if (predicate.length() > 255) throw new IllegalArgumentException("Predicate must not be longer then 255 for preference");
81
			
82
			this.subject = subject;
83
			this.predicate = predicate;
84
		}
85

    
86
		@Override
87
		public int hashCode() {
88
			final int prime = 31;
89
			int result = 1;
90
			result = prime * result + ((predicate == null) ? 0 : predicate.hashCode());
91
			result = prime * result	+ ((subject == null) ? 0 : subject.hashCode());
92
			return result;
93
		}
94

    
95
		@Override
96
		public boolean equals(Object obj) {
97
			if (this == obj){
98
				return true;
99
			} else if (obj == null){
100
				return false;
101
			}else if (getClass() != obj.getClass()){
102
				return false;
103
			}else{
104
				PrefKey other = (PrefKey) obj;
105
				return ( predicate.equals(other.predicate) && subject.equals(other.subject));
106
			}
107
		} 
108
	
109
	}
110
	
111
	@EmbeddedId
112
	private PrefKey key;
113
	
114
	@Length(max=1023)
115
	private String value;
116
	
117
//****************** CONSTRUCTOR **********************/	
118
	
119
	//for hibernate use only
120
	@SuppressWarnings("unused")
121
	private CdmPreference(){};
122

    
123
	
124
	/**
125
	 * Constructor.
126
	 * @param subject must not be null and must not be longer then 255 characters.
127
	 * @param predicate must not be null and must not be longer then 255 characters.
128
	 * @param value must not be longer then 1023 characters.
129
	 */
130
	public CdmPreference(PreferenceSubject subject, PreferencePredicate predicate, String value){
131
		this.key = new PrefKey(subject, predicate);
132
		//TODO are null values allowed?		assert predicate != null : "value must not be null for preference";
133
		if (value != null && value.length() > 1023) {throw new IllegalArgumentException(
134
				String.format("value must not be longer then 1023 characters for preference. Value = %s", value));
135
		}
136
		this.value = value;
137
	}
138

    
139
	
140
	/**
141
	 * Constructor.
142
	 * @param subject must not be null and must not be longer then 255 characters.
143
	 * @param predicate must not be null and must not be longer then 255 characters.
144
	 * @param value must not be longer then 1023 characters.
145
	 */
146
	public CdmPreference(String subject, String predicate, String value){
147
		this.key = new PrefKey(subject, predicate);
148
		//TODO are null values allowed?		assert predicate != null : "value must not be null for preference";
149
		if (value != null && value.length() > 1023) {throw new IllegalArgumentException(
150
			String.format("value must not be longer then 1023 characters for preference. Value = %s", value));
151
		}
152
		this.value = value;
153

    
154
	}
155

    
156
//************************ GETTER / SETTER ***************************/	
157
	
158
	public String getSubject() {
159
		return key.subject;
160
	}
161
	
162
	public String getPredicate() {
163
		return key.predicate;
164
	}
165

    
166
	public String getValue() {
167
		return value;
168
	}
169

    
170
	public PrefKey getKey() {
171
		return key;
172
	}
173
//
174
//	we try to avoid setting of values
175
//	public void setValue(String value) {
176
//		this.value = value;
177
//	}
178
	
179
	
180
}
(2-2/4)