001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info AT sonarsource DOT com
005 *
006 * This program 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 * This program 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.notifications;
021
022import com.google.common.collect.Maps;
023
024import javax.annotation.CheckForNull;
025import javax.annotation.Nullable;
026
027import java.io.Serializable;
028import java.util.Map;
029
030/**
031 * This class represents a notification that will be delivered to users. This is a general concept and it has no
032 * knowledge of the possible ways to be delivered (see {@link NotificationChannel}).
033 * <p>
034 * When creating a new notification, it is strongly advised to give a default message that can be  used by channels
035 * that don't want to specifically format messages for different notification types. You can use
036 * {@link Notification#setDefaultMessage(String)} for that purpose.
037 * 
038 *
039 * @since 2.10
040 */
041public class Notification implements Serializable {
042
043  private static final String DEFAULT_MESSAGE_KEY = "default_message";
044
045  private final String type;
046  private final Map<String, String> fields = Maps.newHashMap();
047
048  /**
049   * <p>
050   * Create a new {@link Notification} of the given type.
051   * 
052   * Example: type = "new-violations"
053   *
054   * @param type the type of notification
055   */
056  public Notification(String type) {
057    this.type = type;
058  }
059
060  /**
061   * Returns the type of the notification
062   *
063   * @return the type
064   */
065  public String getType() {
066    return type;
067  }
068
069  /**
070   * <p>
071   * When creating a new notification, it is strongly advised to give a default message that can be
072   * used by channels that don't want to specifically format messages for different notification types.
073   * 
074   * <p>
075   * This method is equivalent to setting a value for the field {@link #DEFAULT_MESSAGE_KEY} with
076   * {@link #setFieldValue(String, String)}.
077   * 
078   *
079   * @since 3.5
080   */
081  public Notification setDefaultMessage(String value) {
082    setFieldValue(DEFAULT_MESSAGE_KEY, value);
083    return this;
084  }
085
086  /**
087   * Returns the default message to display for this notification.
088   */
089  public String getDefaultMessage() {
090    String defaultMessage = getFieldValue(DEFAULT_MESSAGE_KEY);
091    if (defaultMessage == null) {
092      defaultMessage = this.toString();
093    }
094    return defaultMessage;
095  }
096
097  /**
098   * Adds a field (kind of property) to the notification
099   *
100   * @param field the name of the field (= the key)
101   * @param value the value of the field
102   * @return the notification itself
103   */
104  public Notification setFieldValue(String field, @Nullable String value) {
105    fields.put(field, value);
106    return this;
107  }
108
109  /**
110   * Returns the value of a field.
111   *
112   * @param field the field
113   * @return the value of the field
114   */
115  @CheckForNull
116  public String getFieldValue(String field) {
117    return fields.get(field);
118  }
119
120  @Override
121  public boolean equals(Object obj) {
122    if (!(obj instanceof Notification)) {
123      return false;
124    }
125    if (this == obj) {
126      return true;
127    }
128    Notification other = (Notification) obj;
129    return this.type.equals(other.type) && this.fields.equals(other.fields);
130  }
131
132  @Override
133  public int hashCode() {
134    return type.hashCode() * 31 + fields.hashCode();
135  }
136
137  @Override
138  public String toString() {
139    StringBuilder sb = new StringBuilder("Notification{");
140    sb.append("type='").append(type).append('\'');
141    sb.append(", fields=").append(fields);
142    sb.append('}');
143    return sb.toString();
144  }
145}