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.server.notifications.reviews;
021    
022    import java.util.Map;
023    import java.util.Set;
024    
025    import org.apache.commons.lang.StringUtils;
026    import org.sonar.api.ServerComponent;
027    import org.sonar.api.notifications.Notification;
028    import org.sonar.api.notifications.NotificationManager;
029    
030    import com.google.common.collect.Sets;
031    
032    /**
033     * @since 2.10
034     */
035    public class ReviewsNotificationManager implements ServerComponent {
036    
037      private NotificationManager notificationManager;
038    
039      public ReviewsNotificationManager(NotificationManager notificationManager) {
040        this.notificationManager = notificationManager;
041      }
042    
043      /**
044       * @param reviewId reviewId id of review, which was modified
045       * @param author author of change (username)
046       * @param oldValues map of old values
047       * @param newValues map of new values
048       */
049      public void notifyChanged(Long reviewId, String author, Map<String, String> oldValues, Map<String, String> newValues) {
050        Notification notification = new Notification("review-changed")
051            .setFieldValue("reviewId", String.valueOf(reviewId))
052            .setFieldValue("project", newValues.get("project"))
053            .setFieldValue("resource", newValues.get("resource"))
054            .setFieldValue("title", newValues.get("title"))
055            .setFieldValue("author", author)
056            .setFieldValue("creator", newValues.get("creator"))
057            .setFieldValue("severity", newValues.get("severity"))
058            .setFieldValue("assignee", newValues.get("assignee"));
059        Set<String> fields = Sets.newHashSet();
060        fields.addAll(oldValues.keySet());
061        fields.addAll(newValues.keySet());
062        for (String field : fields) {
063          String oldValue = oldValues.get(field);
064          String newValue = newValues.get(field);
065          if ( !StringUtils.equals(oldValue, newValue)) {
066            notification.setFieldValue("new." + field, newValue);
067            notification.setFieldValue("old." + field, oldValue);
068          }
069        }
070        notificationManager.scheduleForSending(notification);
071      }
072    
073    }