implement CDMPreferences #3555
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / CdmPreferences.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.common;
11
12 import java.io.Serializable;
13
14 import javax.persistence.Embeddable;
15 import javax.persistence.EmbeddedId;
16 import javax.persistence.Entity;
17
18 import org.hibernate.validator.constraints.Length;
19
20 /**
21 * This class may hold all prefrences data for a CDM database.
22 * E.g. one may store what the default nomenclatural code is,
23 * or which default formatter (cache strategy) to use for a
24 * certain class.
25 * The structure represents a triple where the first item
26 * (subject) defines for which object the given information is valid.
27 * The second item (predicate) describes the type of information
28 * and the third item (value) represents the actual value.
29 *
30 * E.g. for defining a database wide default nomenclatural code
31 * you may define a triple ("database", "eu.etaxonomy.cdm.model.name.NomenclaturalCode", "ICZN").
32 * The set of allowed values and semantics for each combination
33 * is up to implementing classes.
34 * The only restrictions we have is the length of the fields and
35 * the fact that the first two items (subject, predicate) do
36 * create a unique key.
37 *
38 * Size of single fields may be enlarged in future versions. "Value" may
39 * become a CLOB.
40 *
41 * @author a.mueller
42 * @created 03.07.2013
43 */
44 @Entity
45 public class CdmPreferences implements Serializable {
46 private static final long serialVersionUID = 4307599154287181582L;
47
48
49 @Embeddable
50 public static class PrefKey implements Serializable{
51 private static final long serialVersionUID = 9019957853773606194L;
52
53 @Length(max=255)
54 private String subject;
55
56 @Length(max=255)
57 private String predicate;
58
59 public PrefKey(){}
60
61 public PrefKey(String subject, String predicate){
62 if (subject == null) throw new IllegalArgumentException("Subject must not be null for preference");
63 if (predicate == null) throw new IllegalArgumentException("Predicate must not be null for preference");
64 if (subject.length() > 255) throw new IllegalArgumentException("Subject must not be longer then 255 for preference");
65 if (predicate.length() > 255) throw new IllegalArgumentException("Predicate must not be longer then 255 for preference");
66
67 this.subject = subject;
68 this.predicate = predicate;
69 }
70
71 @Override
72 public int hashCode() {
73 final int prime = 31;
74 int result = 1;
75 result = prime * result + ((predicate == null) ? 0 : predicate.hashCode());
76 result = prime * result + ((subject == null) ? 0 : subject.hashCode());
77 return result;
78 }
79
80 @Override
81 public boolean equals(Object obj) {
82 if (this == obj){
83 return true;
84 } else if (obj == null){
85 return false;
86 }else if (getClass() != obj.getClass()){
87 return false;
88 }else{
89 PrefKey other = (PrefKey) obj;
90 return ( predicate.equals(other.predicate) && subject.equals(other.subject));
91 }
92 }
93
94 }
95
96 @EmbeddedId
97 private PrefKey key;
98
99 @Length(max=1023)
100 private String value;
101
102 /**
103 * Constructor.
104 * @param subject must not be null and must not be longer then 255 characters.
105 * @param predicate must not be null and must not be longer then 255 characters.
106 * @param value must not be longer then 1023 characters.
107 */
108 public CdmPreferences(String subject, String predicate, String value){
109 this.key = new PrefKey(subject, predicate);
110 //TODO are null values allowed? assert predicate != null : "value must not be null for preference";
111 if (value != null && value.length() > 1023) {throw new IllegalArgumentException(
112 String.format("value must not be longer then 1023 characters for preference. Value = %s", value));
113 }
114 this.value = value;
115
116 }
117
118 public String getSubject() {
119 return key.subject;
120 }
121
122 public String getPredicate() {
123 return key.predicate;
124 }
125
126 public String getValue() {
127 return value;
128 }
129
130 //TODO do we need a setter?
131
132
133 }