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