Skip to content

Commit

Permalink
delete notifications API
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Aug 25, 2017
1 parent 9b3fb87 commit 7471e9d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package de.metas.ui.web.notification;

import java.util.List;

import org.adempiere.exceptions.AdempiereException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.base.Splitter;

import de.metas.ui.web.config.WebConfig;
import de.metas.ui.web.notification.json.JSONNotificationsList;
import de.metas.ui.web.session.UserSession;
Expand All @@ -25,11 +31,11 @@
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/
Expand Down Expand Up @@ -95,4 +101,32 @@ public void markAllAsRead()
userNotificationsService.markAllNotificationsAsRead(adUserId);
}

@DeleteMapping("/{notificationId}")
public void deleteById(@PathVariable("notificationId") final String notificationId)
{
userSession.assertLoggedIn();

final int adUserId = userSession.getAD_User_ID();
userNotificationsService.deleteNotification(adUserId, notificationId);
}

@DeleteMapping
public void deleteByIds(@RequestParam(name = "ids") final String notificationIdsListStr)
{
userSession.assertLoggedIn();

final int adUserId = userSession.getAD_User_ID();

final List<String> notificationIds = Splitter.on(",")
.trimResults()
.omitEmptyStrings()
.splitToList(notificationIdsListStr);
if (notificationIds.isEmpty())
{
throw new AdempiereException("No IDs provided");
}

notificationIds.forEach(notificationId -> userNotificationsService.deleteNotification(adUserId, notificationId));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,24 @@ public void setLanguage(final String adLanguage)
this.adLanguage = adLanguage;
}

public void delete(final String notificationId)
{
notificationsRepo.delete(notificationId);

final UserNotification notification = id2notification.remove(notificationId);
if(notification != null)
{
notifications.remove(notification);
}

// Update unread count
if (notification != null && !notification.isRead())
{
unreadCount.decrementAndGet();
}

//
// Notify on websocket
fireEventOnWebsocket(JSONNotificationEvent.eventDeleted(notificationId, unreadCount.get()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,10 @@ public int getNotificationsUnreadCount(final int adUserId)
{
return getNotificationsQueue(adUserId).getUnreadCount();
}

public void deleteNotification(final int adUserId, final String notificationId)
{
getNotificationsQueue(adUserId).delete(notificationId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.MoreObjects;

import lombok.ToString;

/*
* #%L
Expand All @@ -21,11 +22,11 @@
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/
Expand All @@ -38,7 +39,7 @@
*
* <pre>
* {
* eventType: "New" | "Read"
* eventType: "New" | "Read" | "Delete"
*
* notificationId: <the notification id>
*
Expand All @@ -56,7 +57,8 @@
*
*/
@SuppressWarnings("serial")
@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
@ToString
public final class JSONNotificationEvent implements Serializable
{
public static final JSONNotificationEvent eventNew(final JSONNotification notification, final int unreadCount)
Expand All @@ -71,9 +73,15 @@ public static final JSONNotificationEvent eventRead(final JSONNotification notif
return new JSONNotificationEvent(EventType.Read, notificationId, notification, unreadCount);
}

public static final JSONNotificationEvent eventDeleted(final String notificationId, final int unreadCount)
{
final JSONNotification notification = null;
return new JSONNotificationEvent(EventType.Delete, notificationId, notification, unreadCount);
}

public static enum EventType
{
New, Read
New, Read, Delete
};

@JsonProperty("eventType")
Expand All @@ -92,41 +100,9 @@ public static enum EventType

private JSONNotificationEvent(final EventType eventType, final String notificationId, final JSONNotification notification, final Integer unreadCount)
{
super();
this.eventType = eventType;
this.notificationId = notificationId;
this.notification = notification;
this.unreadCount = unreadCount;
}

@Override
public String toString()
{
return MoreObjects.toStringHelper(this)
.omitNullValues()
.add("eventType", eventType)
.add("notificationId", notificationId)
.add("notification", notification)
.toString();
}

public EventType getEventType()
{
return eventType;
}

public String getNotificationId()
{
return notificationId;
}

public JSONNotification getNotification()
{
return notification;
}

public Integer getUnreadCount()
{
return unreadCount;
}
}

0 comments on commit 7471e9d

Please sign in to comment.