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