001/* 002 * (C) Copyright 2006-2018 Nuxeo (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.lang3.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 @Override 081 @Id 082 @GeneratedValue(strategy = GenerationType.AUTO) 083 @Column(name = "LOG_EXTINFO_ID") 084 public Long getId() { 085 return id; 086 } 087 088 @Override 089 public void setId(Long id) { 090 this.id = id; 091 } 092 093 @Override 094 public String toString() { 095 return ToStringBuilder.reflectionToString(this); 096 } 097 098 @Override 099 @Transient 100 public Serializable getSerializableValue() { 101 throw new UnsupportedOperationException(); 102 } 103 104 @Override 105 public <T> T getValue(Class<T> clazz) { 106 return clazz.cast(this.getSerializableValue()); 107 } 108 109 @Entity 110 @DiscriminatorValue(value = "LONG") 111 public static class LongInfo extends ExtendedInfoImpl { 112 113 private static final long serialVersionUID = 1L; 114 115 // default constructor needed by hibernate 116 private LongInfo() { 117 } 118 119 public LongInfo(long value) { 120 this.longValue = value; 121 } 122 123 private long longValue; 124 125 @Override 126 @Transient 127 public Serializable getSerializableValue() { 128 return longValue; 129 } 130 131 @Column(name = "LOG_EXTINFO_LONG") 132 public Long getLongValue() { 133 return longValue; 134 } 135 136 public void setLongValue(Long value) { 137 this.longValue = value; 138 } 139 } 140 141 @Entity 142 @DiscriminatorValue(value = "DATE") 143 public static class DateInfo extends ExtendedInfoImpl { 144 145 private static final long serialVersionUID = 1L; 146 147 // default constructor needed by hibernate 148 private DateInfo() { 149 } 150 151 public DateInfo(Date value) { 152 dateValue = value; 153 } 154 155 private Date dateValue; 156 157 @Override 158 @Transient 159 public Serializable getSerializableValue() { 160 return dateValue; 161 } 162 163 @Column(name = "LOG_EXTINFO_DATE") 164 @Temporal(value = TemporalType.TIMESTAMP) 165 public Date getDateValue() { 166 return dateValue; 167 } 168 169 public void setDateValue(Date value) { 170 dateValue = value; 171 } 172 } 173 174 @Entity 175 @DiscriminatorValue(value = "STRING") 176 public static class StringInfo extends ExtendedInfoImpl { 177 178 private static final long serialVersionUID = 1L; 179 180 // default constructor needed by hibernate 181 private StringInfo() { 182 } 183 184 public StringInfo(String value) { 185 stringValue = value; 186 } 187 188 private String stringValue; 189 190 @Override 191 @Transient 192 public Serializable getSerializableValue() { 193 return stringValue; 194 } 195 196 @Column(name = "LOG_EXTINFO_STRING", length = 1024) 197 public String getStringValue() { 198 return stringValue; 199 } 200 201 public void setStringValue(String value) { 202 stringValue = value; 203 } 204 } 205 206 @Entity 207 @DiscriminatorValue(value = "DOUBLE") 208 public static class DoubleInfo extends ExtendedInfoImpl { 209 210 private static final long serialVersionUID = 1L; 211 212 // default constructor needed by hibernate 213 private DoubleInfo() { 214 } 215 216 public DoubleInfo(Double value) { 217 doubleValue = value; 218 } 219 220 private Double doubleValue; 221 222 @Override 223 @Transient 224 public Serializable getSerializableValue() { 225 return doubleValue; 226 } 227 228 @Column(name = "LOG_EXTINFO_DOUBLE") 229 public Double getDoubleValue() { 230 return doubleValue; 231 } 232 233 public void setDoubleValue(Double value) { 234 doubleValue = value; 235 } 236 } 237 238 @Entity 239 @DiscriminatorValue(value = "BOOLEAN") 240 public static class BooleanInfo extends ExtendedInfoImpl { 241 242 private static final long serialVersionUID = 1L; 243 244 // default constructor needed by hibernate 245 private BooleanInfo() { 246 } 247 248 public BooleanInfo(Boolean value) { 249 booleanValue = value; 250 } 251 252 private Boolean booleanValue; 253 254 @Override 255 @Transient 256 public Serializable getSerializableValue() { 257 return booleanValue; 258 } 259 260 @Column(name = "LOG_EXTINFO_BOOLEAN") 261 public Boolean getBooleanValue() { 262 return booleanValue; 263 } 264 265 public void setBooleanValue(Boolean value) { 266 booleanValue = value; 267 } 268 } 269 270 @Entity 271 @DiscriminatorValue(value = "BLOB") 272 public static class BlobInfo extends ExtendedInfoImpl { 273 274 private static final long serialVersionUID = 1L; 275 276 // default constructor needed by hibernate 277 private BlobInfo() { 278 } 279 280 public BlobInfo(Serializable value) { 281 blobValue = value; 282 } 283 284 private Serializable blobValue; 285 286 @Override 287 @Transient 288 public Serializable getSerializableValue() { 289 return blobValue; 290 } 291 292 @Column(name = "LOG_EXTINFO_BLOB") 293 @Lob 294 public Serializable getBlobValue() { 295 return blobValue; 296 } 297 298 public void setBlobValue(Serializable value) { 299 blobValue = value; 300 } 301 302 } 303 304}