cz.muni.fi.editor.webapp.controllers.ajax.AjaxNotificationController.java Source code

Java tutorial

Introduction

Here is the source code for cz.muni.fi.editor.webapp.controllers.ajax.AjaxNotificationController.java

Source

/*
*Copyright  2016 Dominik Szalai (emptulik@gmail.com)
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/
/*
 * Copyright  2016 Dominik Szalai (emptulik@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.editor.webapp.controllers.ajax;

import cz.muni.fi.editor.api.NotificationService;
import cz.muni.fi.editor.api.dto.NotificationDTO;
import cz.muni.fi.editor.api.exceptions.FieldException;
import cz.muni.fi.editor.webapp.additions.MarkSeenNotificationRequest;
import cz.muni.fi.editor.webapp.additions.PollRequest;
import cz.muni.fi.editor.webapp.additions.WebNotificationResponse;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.stream.Collectors;

/**
 * Created by Dominik Szalai - emptulik at gmail.com on 8/30/16.
 */
@Log4j2
@Controller
public class AjaxNotificationController {
    @Autowired
    private NotificationService notificationService;

    @MessageMapping("/poll")
    @SendToUser("/topic/notifications")
    public WebNotificationResponse pollForNotifications(PollRequest pollRequest) throws Exception {
        if (pollRequest.getNotification() == null) {
            return new WebNotificationResponse(notificationService.poll(defaultNotification()));
        } else {
            NotificationDTO n = new NotificationDTO();
            n.setId(pollRequest.getNotification());
            return new WebNotificationResponse(notificationService.poll(n));
        }
    }

    @RequestMapping("/auth/ajax/notification/mark/")
    public ResponseEntity<String> mark(MarkSeenNotificationRequest markSeenNotificationRequest) {
        try {
            notificationService.markSeen(markSeenNotificationRequest.getValues().stream().map(this::notification)
                    .collect(Collectors.toList()));
            return new ResponseEntity<>("SUCCESS", HttpStatus.OK);
        } catch (FieldException e) {
            log.warn(e);
            return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT);
        }
    }

    private NotificationDTO notification(Long id) {
        NotificationDTO dto = new NotificationDTO();
        dto.setId(id);

        return dto;
    }

    private final NotificationDTO defaultNotification() {
        NotificationDTO n = new NotificationDTO();
        n.setId(0L);

        return n;
    }

    //    @MessageMapping("/hello")
    //    @SendTo("/topic/greetings")
    //    public WebNotificationResponse poll(PollRequest pollRequest)
    //    {
    //        log.info("There is a request for poll with id {}", pollRequest);
    //        NotificationDTO dto = new NotificationDTO();
    //        dto.setId(pollRequest.getNotification());
    //
    //        try
    //        {
    //            return new WebNotificationResponse(notificationService.poll(dto));
    //        }
    //        catch (FieldException ex)
    //        {
    //            log.fatal(ex);
    //            throw new IllegalArgumentException(ex);
    //        }
    //    }

    //    @Autowired
    //    private NotificationService notificationService;
    //
    //    @RequestMapping("/")
    //    public
    //    @ResponseBody
    //    WebNotificationResponse notifications()
    //    {
    //        return new WebNotificationResponse(notificationService.getLatestNotifications());
    //    }
    //
    //    @RequestMapping("/mark/")
    //    public ResponseEntity<String> mark(@RequestParam("value") List<Long> value)
    //    {
    //        try
    //        {
    //            notificationService.markSeen(value.stream().map(id ->
    //            {
    //                NotificationDTO dto = new NotificationDTO();
    //                dto.setId(id);
    //
    //                return dto;
    //            }).collect(Collectors.toList()));
    //            return new ResponseEntity<>("SUCCESS", HttpStatus.OK);
    //        }
    //        catch (FieldException e)
    //        {
    //            log.warn(e);
    //            return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT);
    //        }
    //    }
    //
    //    @RequestMapping("/poll/")
    //    public
    //    @ResponseBody
    //    ResponseEntity<WebNotificationResponse> poll(@RequestParam(required = false) Long last)
    //    {
    //        NotificationDTO latest = new NotificationDTO();
    //        if (last == null)
    //        {
    //            latest.setId(Long.valueOf(1L));
    //        }
    //        else
    //        {
    //            latest.setId(last);
    //        }
    //
    //        try
    //        {
    //            return new ResponseEntity<>(new WebNotificationResponse(notificationService.getNewNotifications(latest)), HttpStatus.OK);
    //        }
    //        catch (FieldException ex)
    //        {
    //            log.warn(ex);
    //            return new ResponseEntity<>(HttpStatus.CONFLICT);
    //        }
    //    }

}