1 /*
2 * Copyright 2008 :torweg free software group
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */18 package org.torweg.pulse.accesscontrol.attributes;
19 20 import javax.persistence.Entity;
21 import javax.persistence.Inheritance;
22 import javax.persistence.InheritanceType;
23 import javax.persistence.ManyToOne;
24 import javax.persistence.Transient;
25 import javax.xml.bind.annotation.XmlAccessOrder;
26 import javax.xml.bind.annotation.XmlAccessType;
27 import javax.xml.bind.annotation.XmlAccessorOrder;
28 import javax.xml.bind.annotation.XmlAccessorType;
29 import javax.xml.bind.annotation.XmlElement;
30 import javax.xml.bind.annotation.XmlRootElement;
31 import javax.xml.bind.annotation.XmlTransient;
32 33 import org.hibernate.LazyInitializationException;
34 import org.jdom.Element;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 importorg.torweg.pulse.accesscontrol.User;
38 importorg.torweg.pulse.bundle.JDOMable;
39 importorg.torweg.pulse.service.request.Command;
40 importorg.torweg.pulse.util.entity.AbstractBasicEntity;
41 42 /**
43 * represents the tripel of an {@code AbstractAttribute}, a {@code User} and the
44 * value {@code <T>}.
45 *
46 * @param <T>
47 * the type of the value
48 * @author Thomas Weber, Daniel Dietz
49 * @version $Revision: 1481 $
50 */51 @Entity
52 @Inheritance(strategy = InheritanceType.JOINED)
53 @XmlRootElement(name = "abstract-value")
54 @XmlAccessorOrder(XmlAccessOrder.UNDEFINED)
55 @XmlAccessorType(XmlAccessType.FIELD)
56 publicabstractclass AbstractValue<T> extendsAbstractBasicEntityimplements57 JDOMable {
58 59 /**
60 * The serialVersionUID.
61 */62 privatestaticfinallong serialVersionUID = 5506451585041906560L;
63 64 /**
65 * the logger.
66 */67 privatestaticfinal Logger LOGGER = LoggerFactory
68 .getLogger(AbstractValue.class);
69 70 /**
71 * the user the value is associated with.
72 */73 @ManyToOne
74 @XmlTransient
75 // getter JAXB-annotated { -> this.getUserIdJAXB() }
76 privateUser user;
77 78 /**
79 * returns the attribute the value is associated with.
80 *
81 * @return the attribute
82 */83 @Transient
84 publicabstractAbstractAttribute<T> getAttribute();
85 86 /**
87 * sets the attribute the value is associated with.
88 *
89 * @param a
90 * the attribute to set
91 */92 @Transient
93 publicabstractvoid setAttribute(finalAbstractAttribute<T> a);
94 95 /**
96 * returns the user the value is associated with.
97 *
98 * @return the user
99 */100 publicfinalUser getUser() {
101 returnthis.user;
102 }
103 104 /**
105 * For JAXB only.
106 *
107 * @return this.getUser().getId()
108 */109 @XmlElement(name = "user-id")
110 @SuppressWarnings("unused")
111 @Deprecated
112 private Long getUserIdJAXB() {
113 try {
114 return getUser().getId();
115 } catch (LazyInitializationException e) {
116 LOGGER.debug("ignored: {}", e.getLocalizedMessage());
117 returnnull;
118 }
119 }
120 121 /**
122 * sets the user the value is associated with.
123 *
124 * @param u
125 * the user to set
126 */127 publicfinalvoid setUser(finalUser u) {
128 this.user = u;
129 if (u.getAttributeValue(this.getAttribute()) == null) {
130 u.setAttributeValue(this);
131 }
132 }
133 134 /**
135 * returns the value the value is associated with.
136 *
137 * @return the value
138 */139 @Transient
140 publicabstract T getValue();
141 142 /**
143 * sets the value the value is associated with.
144 *
145 * @param v
146 * the value to set
147 */148 @Transient
149 publicabstractvoid setValue(T v);
150 151 /**
152 * returns an Element representing the value.
153 *
154 * @return an Element representing the value
155 */156 @Transient
157 publicabstract Element deserializeToJDOM();
158 159 /**
160 * updates the values from a given {@code Command}.
161 *
162 * @param c
163 * the {@code Command}
164 */165 @Transient
166 publicabstractvoid updateFromCommand(Command c);
167 168 /**
169 * attributes must return a suitable String representation.
170 *
171 * @return a String representation
172 */173 @Override
174 publicabstract String toString();
175 176 }
177