001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     matic
016 */
017package org.nuxeo.ecm.platform.audit.impl;
018
019import java.io.Serializable;
020import java.util.Date;
021
022import javax.persistence.Column;
023import javax.persistence.DiscriminatorColumn;
024import javax.persistence.DiscriminatorValue;
025import javax.persistence.Entity;
026import javax.persistence.GeneratedValue;
027import javax.persistence.GenerationType;
028import javax.persistence.Id;
029import javax.persistence.Inheritance;
030import javax.persistence.InheritanceType;
031import javax.persistence.Lob;
032import javax.persistence.Table;
033import javax.persistence.Temporal;
034import javax.persistence.TemporalType;
035import javax.persistence.Transient;
036
037import org.apache.commons.lang.builder.ToStringBuilder;
038import org.nuxeo.ecm.platform.audit.api.ExtendedInfo;
039
040/**
041 * Extended audit info entities, used to persist contributed extended information.
042 *
043 * @author Stephane Lacoin (Nuxeo EP software engineer)
044 */
045@Entity
046@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
047@Table(name = "NXP_LOGS_EXTINFO")
048@DiscriminatorColumn(name = "DISCRIMINATOR")
049public class ExtendedInfoImpl implements ExtendedInfo {
050
051    private static final long serialVersionUID = 1L;
052
053    private ExtendedInfoImpl() {
054    }
055
056    public static ExtendedInfoImpl createExtendedInfo(Serializable value) {
057        Class<?> clazz = value.getClass();
058        if (Long.class.isAssignableFrom(clazz)) {
059            return new LongInfo((Long) value);
060        }
061        if (Double.class.isAssignableFrom(clazz)) {
062            return new DoubleInfo((Double) value);
063        }
064        if (Date.class.isAssignableFrom(clazz)) {
065            return new DateInfo((Date) value);
066        }
067        if (String.class.isAssignableFrom(clazz)) {
068            return new StringInfo((String) value);
069        }
070        if (Boolean.class.isAssignableFrom(clazz)) {
071            return new BooleanInfo((Boolean) value);
072        }
073        return new BlobInfo(value);
074    }
075
076    private Long id;
077
078    @Id
079    @GeneratedValue(strategy = GenerationType.AUTO)
080    @Column(name = "LOG_EXTINFO_ID")
081    public Long getId() {
082        return id;
083    }
084
085    public void setId(Long id) {
086        this.id = id;
087    }
088
089    @Override
090    public String toString() {
091        return ToStringBuilder.reflectionToString(this);
092    }
093
094    @Transient
095    public Serializable getSerializableValue() {
096        throw new UnsupportedOperationException();
097    }
098
099    public <T> T getValue(Class<T> clazz) {
100        return clazz.cast(this.getSerializableValue());
101    }
102
103    @Entity
104    @DiscriminatorValue(value = "LONG")
105    public static class LongInfo extends ExtendedInfoImpl {
106
107        private static final long serialVersionUID = 1L;
108
109        private LongInfo() {
110        }
111
112        private LongInfo(long value) {
113            this.longValue = value;
114        }
115
116        private long longValue;
117
118        @Override
119        @Transient
120        public Serializable getSerializableValue() {
121            return longValue;
122        }
123
124        @Column(name = "LOG_EXTINFO_LONG")
125        public Long getLongValue() {
126            return longValue;
127        }
128
129        public void setLongValue(Long value) {
130            this.longValue = value;
131        }
132    }
133
134    @Entity
135    @DiscriminatorValue(value = "DATE")
136    public static class DateInfo extends ExtendedInfoImpl {
137
138        private static final long serialVersionUID = 1L;
139
140        private DateInfo() {
141        }
142
143        private DateInfo(Date value) {
144            dateValue = value;
145        }
146
147        private Date dateValue;
148
149        @Override
150        @Transient
151        public Serializable getSerializableValue() {
152            return dateValue;
153        }
154
155        @Column(name = "LOG_EXTINFO_DATE")
156        @Temporal(value = TemporalType.TIMESTAMP)
157        public Date getDateValue() {
158            return dateValue;
159        }
160
161        public void setDateValue(Date value) {
162            dateValue = value;
163        }
164    }
165
166    @Entity
167    @DiscriminatorValue(value = "STRING")
168    public static class StringInfo extends ExtendedInfoImpl {
169
170        private static final long serialVersionUID = 1L;
171
172        private StringInfo() {
173        }
174
175        private StringInfo(String value) {
176            stringValue = value;
177        }
178
179        private String stringValue;
180
181        @Override
182        @Transient
183        public Serializable getSerializableValue() {
184            return stringValue;
185        }
186
187        @Column(name = "LOG_EXTINFO_STRING")
188        public String getStringValue() {
189            return stringValue;
190        }
191
192        public void setStringValue(String value) {
193            stringValue = value;
194        }
195    }
196
197    @Entity
198    @DiscriminatorValue(value = "DOUBLE")
199    public static class DoubleInfo extends ExtendedInfoImpl {
200
201        private static final long serialVersionUID = 1L;
202
203        private DoubleInfo() {
204        }
205
206        private DoubleInfo(Double value) {
207            doubleValue = value;
208        }
209
210        private Double doubleValue;
211
212        @Override
213        @Transient
214        public Serializable getSerializableValue() {
215            return doubleValue;
216        }
217
218        @Column(name = "LOG_EXTINFO_DOUBLE")
219        public Double getDoubleValue() {
220            return doubleValue;
221        }
222
223        public void setDoubleValue(Double value) {
224            doubleValue = value;
225        }
226    }
227
228    @Entity
229    @DiscriminatorValue(value = "BOOLEAN")
230    public static class BooleanInfo extends ExtendedInfoImpl {
231
232        private static final long serialVersionUID = 1L;
233
234        private BooleanInfo() {
235        }
236
237        private BooleanInfo(Boolean value) {
238            booleanValue = value;
239        }
240
241        private Boolean booleanValue;
242
243        @Override
244        @Transient
245        public Serializable getSerializableValue() {
246            return booleanValue;
247        }
248
249        @Column(name = "LOG_EXTINFO_BOOLEAN")
250        public Boolean getBooleanValue() {
251            return booleanValue;
252        }
253
254        public void setBooleanValue(Boolean value) {
255            booleanValue = value;
256        }
257    }
258
259    @Entity
260    @DiscriminatorValue(value = "BLOB")
261    public static class BlobInfo extends ExtendedInfoImpl {
262
263        private static final long serialVersionUID = 1L;
264
265        private BlobInfo() {
266        }
267
268        private BlobInfo(Serializable value) {
269            blobValue = value;
270        }
271
272        private Serializable blobValue;
273
274        @Override
275        @Transient
276        public Serializable getSerializableValue() {
277            return blobValue;
278        }
279
280        @Column(name = "LOG_EXTINFO_BLOB")
281        @Lob
282        public Serializable getBlobValue() {
283            return blobValue;
284        }
285
286        public void setBlobValue(Serializable value) {
287            blobValue = value;
288        }
289
290    }
291
292}