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 */
020package org.sonar.api.batch;
021
022import org.apache.commons.lang.builder.ToStringBuilder;
023import org.sonar.api.database.BaseIdentifiable;
024import org.sonar.api.database.model.Snapshot;
025
026import javax.persistence.*;
027import java.util.Date;
028
029import static com.google.common.base.Preconditions.checkNotNull;
030import static org.sonar.api.utils.DateUtils.longToDate;
031
032/**
033 * @since 1.10
034 */
035@Entity
036@Table(name = "events")
037public class Event extends BaseIdentifiable {
038  public static final String CATEGORY_VERSION = "Version";
039  public static final String CATEGORY_ALERT = "Alert";
040  public static final String CATEGORY_PROFILE = "Profile";
041
042  @Column(name = "name", updatable = true, nullable = true, length = 400)
043  private String name;
044
045  @Column(name = "description", updatable = true, nullable = true, length = 4000)
046  private String description;
047
048  @Column(name = "category", updatable = true, nullable = true, length = 50)
049  private String category;
050
051  @Column(name = "event_date", updatable = true, nullable = false)
052  private Long date;
053
054  @Column(name = "created_at", updatable = true, nullable = false)
055  private Long createdAt;
056
057  @Column(name = "event_data", updatable = true, nullable = true)
058  private String data;
059
060  @ManyToOne(fetch = FetchType.LAZY)
061  @JoinColumn(name = "snapshot_id", updatable = true, nullable = true)
062  private Snapshot snapshot;
063
064  @Column(name = "resource_id", updatable = true, nullable = true)
065  private Integer resourceId;
066
067  public Event() {
068  }
069
070  public Event(String name, String description, String category) {
071    this.name = name;
072    this.description = description;
073    this.category = category;
074  }
075
076  public String getName() {
077    return name;
078  }
079
080  public void setName(String name) {
081    this.name = name;
082  }
083
084  public String getDescription() {
085    return description;
086  }
087
088  public void setDescription(String description) {
089    this.description = description;
090  }
091
092  public String getCategory() {
093    return category;
094  }
095
096  public void setCategory(String category) {
097    this.category = category;
098  }
099
100  public boolean isVersionCategory() {
101    return CATEGORY_VERSION.equalsIgnoreCase(category);
102  }
103
104  public boolean isProfileCategory() {
105    return CATEGORY_PROFILE.equalsIgnoreCase(category);
106  }
107
108  public Date getDate() {
109    return longToDate(date);
110  }
111
112  public void setDate(Date date) {
113    this.date = date.getTime();
114  }
115
116  public Snapshot getSnapshot() {
117    return snapshot;
118  }
119
120  public Date getCreatedAt() {
121    return new Date(createdAt);
122  }
123
124  public void setCreatedAt(Date createdAt) {
125    this.createdAt = createdAt.getTime();
126  }
127
128  public final void setSnapshot(Snapshot snapshot) {
129    this.snapshot = checkNotNull(snapshot, "it is not possible to set a null snapshot linked to an event");
130    this.date = snapshot.getCreatedAtMs();
131    this.resourceId = snapshot.getResourceId();
132  }
133
134  public Integer getResourceId() {
135    return resourceId;
136  }
137
138  public Event setResourceId(Integer resourceId) {
139    this.resourceId = resourceId;
140    return this;
141  }
142
143  public String getData() {
144    return data;
145  }
146
147  public void setData(String data) {
148    this.data = data;
149  }
150
151  @Override
152  public String toString() {
153    return new ToStringBuilder(this)
154      .append("name", name)
155      .append("categ", category)
156      .append("date", date)
157      .append("snapshot", snapshot)
158      .append("resource", resourceId)
159      .toString();
160  }
161}