3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.model
.metadata
;
12 import java
.io
.Serializable
;
14 import javax
.persistence
.Column
;
15 import javax
.persistence
.Embeddable
;
16 import javax
.persistence
.EmbeddedId
;
17 import javax
.persistence
.Entity
;
18 import javax
.validation
.constraints
.Size
;
20 import org
.hibernate
.validator
.constraints
.Length
;
24 * This class may hold all prefrences data for a CDM database.
25 * E.g. one may store what the default nomenclatural code is,
26 * or which default formatter (cache strategy) to use for a
28 * The structure represents a triple where the first item
29 * (subject) defines for which object the given information is valid.
30 * The second item (predicate) describes the type of information
31 * and the third item (value) represents the actual value.
33 * E.g. for defining a database wide default nomenclatural code
34 * you may define a triple ("database", "eu.etaxonomy.cdm.model.name.NomenclaturalCode", "ICZN").
35 * The set of allowed values and semantics for each combination
36 * is up to implementing classes.
37 * The only restrictions we have is the length of the fields and
38 * the fact that the first two items (subject, predicate) do
39 * create a unique key.
41 * Size of single fields may be enlarged in future versions. "Value" may
48 public final class CdmPreference
implements Serializable
{
49 private static final long serialVersionUID
= 4307599154287181582L;
52 public static final CdmPreference
NewInstance(PreferenceSubject subject
, PreferencePredicate predicate
, String value
){
53 return new CdmPreference(subject
, predicate
, value
);
56 public static PrefKey
NewKey(PreferenceSubject subject
, PreferencePredicate predicate
){
57 return new PrefKey(subject
, predicate
);
61 public static class PrefKey
implements Serializable
{
62 private static final long serialVersionUID
= 9019957853773606194L;
64 @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
65 private String subject
;
67 @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
68 private String predicate
;
70 //for hibernate use only
74 private PrefKey(PreferenceSubject subject
, PreferencePredicate predicate
){
75 this(subject
.getKey(), predicate
.getKey());
78 private PrefKey(String subject
, String predicate
){
79 if (subject
== null) throw new IllegalArgumentException("Subject must not be null for preference");
80 if (predicate
== null) throw new IllegalArgumentException("Predicate must not be null for preference");
81 if (subject
.length() > 255) throw new IllegalArgumentException("Subject must not be longer then 255 for preference");
82 if (predicate
.length() > 255) throw new IllegalArgumentException("Predicate must not be longer then 255 for preference");
84 this.subject
= subject
;
85 this.predicate
= predicate
;
89 public int hashCode() {
92 result
= prime
* result
+ ((predicate
== null) ?
0 : predicate
.hashCode());
93 result
= prime
* result
+ ((subject
== null) ?
0 : subject
.hashCode());
98 public boolean equals(Object obj
) {
101 } else if (obj
== null){
103 }else if (getClass() != obj
.getClass()){
106 PrefKey other
= (PrefKey
) obj
;
107 return ( predicate
.equals(other
.predicate
) && subject
.equals(other
.subject
));
117 private String value
;
119 //****************** CONSTRUCTOR **********************/
121 //for hibernate use only
122 @SuppressWarnings("unused")
123 private CdmPreference(){};
128 * @param subject must not be null and must not be longer then 255 characters.
129 * @param predicate must not be null and must not be longer then 255 characters.
130 * @param value must not be longer then 1023 characters.
132 public CdmPreference(PreferenceSubject subject
, PreferencePredicate predicate
, String value
){
133 this.key
= new PrefKey(subject
, predicate
);
134 //TODO are null values allowed? assert predicate != null : "value must not be null for preference";
135 if (value
!= null && value
.length() > 1023) {throw new IllegalArgumentException(
136 String
.format("value must not be longer then 1023 characters for preference. Value = %s", value
));
144 * @param subject must not be null and must not be longer then 255 characters.
145 * @param predicate must not be null and must not be longer then 255 characters.
146 * @param value must not be longer then 1023 characters.
148 public CdmPreference(String subject
, String predicate
, String value
){
149 this.key
= new PrefKey(subject
, predicate
);
150 //TODO are null values allowed? assert predicate != null : "value must not be null for preference";
151 if (value
!= null && value
.length() > 1023) {throw new IllegalArgumentException(
152 String
.format("value must not be longer then 1023 characters for preference. Value = %s", value
));
158 //************************ GETTER / SETTER ***************************/
160 public String
getSubject() {
164 public String
getPredicate() {
165 return key
.predicate
;
168 public String
getValue() {
172 public PrefKey
getKey() {
176 // we try to avoid setting of values
177 // public void setValue(String value) {
178 // this.value = value;