refactoring taxon controllers, reducing code duplication
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / metadata / CdmPreference.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 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 javax.validation.constraints.Size;
19
20 import org.hibernate.validator.constraints.Length;
21
22
23 /**
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
27 * certain class.
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.
32 *
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.
40 *
41 * Size of single fields may be enlarged in future versions. "Value" may
42 * become a CLOB.
43 *
44 * @author a.mueller
45 * @created 03.07.2013
46 */
47 @Entity
48 public final class CdmPreference implements Serializable {
49 private static final long serialVersionUID = 4307599154287181582L;
50
51
52 public static final CdmPreference NewInstance(PreferenceSubject subject, PreferencePredicate predicate, String value){
53 return new CdmPreference(subject, predicate, value);
54 }
55
56 public static PrefKey NewKey(PreferenceSubject subject, PreferencePredicate predicate){
57 return new PrefKey(subject, predicate);
58 }
59
60 @Embeddable
61 public static class PrefKey implements Serializable{
62 private static final long serialVersionUID = 9019957853773606194L;
63
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;
66
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;
69
70 //for hibernate use only
71 private PrefKey(){}
72
73
74 private PrefKey(PreferenceSubject subject, PreferencePredicate predicate){
75 this(subject.getKey(), predicate.getKey());
76 }
77
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");
83
84 this.subject = subject;
85 this.predicate = predicate;
86 }
87
88 @Override
89 public int hashCode() {
90 final int prime = 31;
91 int result = 1;
92 result = prime * result + ((predicate == null) ? 0 : predicate.hashCode());
93 result = prime * result + ((subject == null) ? 0 : subject.hashCode());
94 return result;
95 }
96
97 @Override
98 public boolean equals(Object obj) {
99 if (this == obj){
100 return true;
101 } else if (obj == null){
102 return false;
103 }else if (getClass() != obj.getClass()){
104 return false;
105 }else{
106 PrefKey other = (PrefKey) obj;
107 return ( predicate.equals(other.predicate) && subject.equals(other.subject));
108 }
109 }
110
111 }
112
113 @EmbeddedId
114 private PrefKey key;
115
116 @Length(max=1023)
117 private String value;
118
119 //****************** CONSTRUCTOR **********************/
120
121 //for hibernate use only
122 @SuppressWarnings("unused")
123 private CdmPreference(){};
124
125
126 /**
127 * Constructor.
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.
131 */
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));
137 }
138 this.value = value;
139 }
140
141
142 /**
143 * Constructor.
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.
147 */
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));
153 }
154 this.value = value;
155
156 }
157
158 //************************ GETTER / SETTER ***************************/
159
160 public String getSubject() {
161 return key.subject;
162 }
163
164 public String getPredicate() {
165 return key.predicate;
166 }
167
168 public String getValue() {
169 return value;
170 }
171
172 public PrefKey getKey() {
173 return key;
174 }
175 //
176 // we try to avoid setting of values
177 // public void setValue(String value) {
178 // this.value = value;
179 // }
180
181
182 }