001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube 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     * SonarQube 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 License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.api.batch;
021    
022    import org.apache.commons.lang.builder.ToStringBuilder;
023    import org.sonar.api.database.BaseIdentifiable;
024    import org.sonar.api.database.model.Snapshot;
025    
026    import java.util.Date;
027    
028    import javax.persistence.*;
029    
030    /**
031     * @since 1.10
032     */
033    @Entity
034    @Table(name = "events")
035    public class Event extends BaseIdentifiable {
036      public static final String CATEGORY_VERSION = "Version";
037      public static final String CATEGORY_ALERT = "Alert";
038      public static final String CATEGORY_PROFILE = "Profile";
039    
040      @Column(name = "name", updatable = true, nullable = true, length = 400)
041      private String name;
042    
043      @Column(name = "description", updatable = true, nullable = true, length = 4000)
044      private String description;
045    
046      @Column(name = "category", updatable = true, nullable = true, length = 50)
047      private String category;
048    
049      @Column(name = "event_date", updatable = true, nullable = false)
050      private Date date;
051    
052      @Column(name = "created_at", updatable = true, nullable = true)
053      private Date createdAt;
054    
055      @Column(name = "event_data", updatable = true, nullable = true)
056      private String data;
057    
058      @ManyToOne(fetch = FetchType.LAZY)
059      @JoinColumn(name = "snapshot_id", updatable = true, nullable = true)
060      private Snapshot snapshot;
061    
062      @Column(name = "resource_id", updatable = true, nullable = true)
063      private Integer resourceId;
064    
065      public Event() {
066      }
067    
068      public Event(String name, String description, String category) {
069        this.name = name;
070        this.description = description;
071        this.category = category;
072      }
073    
074      /**
075       * @deprecated in 2.5
076       */
077      @Deprecated
078      public Event(String name, String description, String category, Date date, Integer resourceId) {
079        this.name = name;
080        this.description = description;
081        this.category = category;
082        this.date = date;
083        this.resourceId = resourceId;
084      }
085    
086      /**
087       * @deprecated in 2.5
088       */
089      @Deprecated
090      public Event(String name, String description, String category, Snapshot snapshot) {
091        this.name = name;
092        this.description = description;
093        this.category = category;
094        setSnapshot(snapshot);
095      }
096    
097      public String getName() {
098        return name;
099      }
100    
101      public void setName(String name) {
102        this.name = name;
103      }
104    
105      public String getDescription() {
106        return description;
107      }
108    
109      public void setDescription(String description) {
110        this.description = description;
111      }
112    
113      public String getCategory() {
114        return category;
115      }
116    
117      public void setCategory(String category) {
118        this.category = category;
119      }
120    
121      public boolean isVersionCategory() {
122        return CATEGORY_VERSION.equalsIgnoreCase(category);
123      }
124    
125      public boolean isProfileCategory() {
126        return CATEGORY_PROFILE.equalsIgnoreCase(category);
127      }
128    
129      public Date getDate() {
130        return date;
131      }
132    
133      public void setDate(Date date) {
134        this.date = date;
135      }
136    
137      public Snapshot getSnapshot() {
138        return snapshot;
139      }
140    
141      public Date getCreatedAt() {
142        return createdAt;
143      }
144    
145      public void setCreatedAt(Date createdAt) {
146        this.createdAt = createdAt;
147      }
148    
149      public final void setSnapshot(Snapshot snapshot) {
150        this.snapshot = snapshot;
151        if (snapshot != null) {
152          this.date = snapshot.getCreatedAt();
153          this.resourceId = snapshot.getResourceId();
154        }
155      }
156    
157      public Integer getResourceId() {
158        return resourceId;
159      }
160    
161      public Event setResourceId(Integer resourceId) {
162        this.resourceId = resourceId;
163        return this;
164      }
165    
166      public String getData() {
167        return data;
168      }
169    
170      public void setData(String data) {
171        this.data = data;
172      }
173    
174      @Override
175      public String toString() {
176        return new ToStringBuilder(this)
177            .append("name", name)
178            .append("categ", category)
179            .append("date", date)
180            .append("snapshot", snapshot)
181            .append("resource", resourceId)
182            .toString();
183      }
184    }