001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.jpa.entity;
021    
022    import org.apache.commons.io.IOUtils;
023    import org.apache.commons.lang.builder.ReflectionToStringBuilder;
024    import org.apache.commons.lang.builder.ToStringStyle;
025    import org.sonar.api.notifications.Notification;
026    import org.sonar.api.utils.SonarException;
027    
028    import java.io.*;
029    import java.util.Date;
030    
031    import javax.persistence.*;
032    
033    @Entity
034    @Table(name = "notifications")
035    public class NotificationQueueElement {
036    
037      @Id
038      @Column(name = "id")
039      @GeneratedValue
040      private Integer id;
041    
042      @Column(name = "created_at")
043      private Date createdAt;
044    
045      @Column(name = "data", updatable = true, nullable = true, length = 167772150)
046      private byte[] data;
047    
048      public Integer getId() {
049        return id;
050      }
051    
052      public void setId(Integer id) {
053        this.id = id;
054      }
055    
056      public Date getCreatedAt() {
057        return createdAt;
058      }
059    
060      public void setCreatedAt(Date createdAt) {
061        this.createdAt = createdAt;
062      }
063    
064      public void setNotification(Notification notification) {
065        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
066        try {
067          ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
068          objectOutputStream.writeObject(notification);
069          objectOutputStream.close();
070          this.data = byteArrayOutputStream.toByteArray();
071    
072        } catch (IOException e) {
073          throw new SonarException(e);
074    
075        } finally {
076          IOUtils.closeQuietly(byteArrayOutputStream);
077        }
078      }
079    
080      public Notification getNotification() {
081        if (this.data == null) {
082          return null;
083        }
084        ByteArrayInputStream byteArrayInputStream = null;
085        try {
086          byteArrayInputStream = new ByteArrayInputStream(this.data);
087          ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
088          Object result = objectInputStream.readObject();
089          objectInputStream.close();
090          return (Notification) result;
091    
092        } catch (IOException e) {
093          throw new SonarException(e);
094    
095        } catch (ClassNotFoundException e) {
096          throw new SonarException(e);
097          
098        } finally {
099          IOUtils.closeQuietly(byteArrayInputStream);
100        }
101      }
102    
103      @Override
104      public String toString() {
105        return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString();
106      }
107    }