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   import org.torweg.pulse.accesscontrol.User;
38   import org.torweg.pulse.bundle.JDOMable;
39   import org.torweg.pulse.service.request.Command;
40   import org.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   public abstract class AbstractValue<T> extends AbstractBasicEntity implements
57           JDOMable {
58   
59       /**
60        * The serialVersionUID.
61        */
62       private static final long serialVersionUID = 5506451585041906560L;
63   
64       /**
65        * the logger.
66        */
67       private static final 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       private User user;
77   
78       /**
79        * returns the attribute the value is associated with.
80        * 
81        * @return the attribute
82        */
83       @Transient
84       public abstract AbstractAttribute<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       public abstract void setAttribute(final AbstractAttribute<T> a);
94   
95       /**
96        * returns the user the value is associated with.
97        * 
98        * @return the user
99        */
100      public final User getUser() {
101          return this.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              return null;
118          }
119      }
120  
121      /**
122       * sets the user the value is associated with.
123       * 
124       * @param u
125       *            the user to set
126       */
127      public final void setUser(final User 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      public abstract 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      public abstract void setValue(T v);
150  
151      /**
152       * returns an Element representing the value.
153       * 
154       * @return an Element representing the value
155       */
156      @Transient
157      public abstract 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      public abstract void updateFromCommand(Command c);
167  
168      /**
169       * attributes must return a suitable String representation.
170       * 
171       * @return a String representation
172       */
173      @Override
174      public abstract String toString();
175  
176  }
177