Java tutorial
/* *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.services.api.notifications; import cz.muni.fi.editor.api.dto.NotificationDTO; import cz.muni.fi.editor.api.dto.NotificationRequest; import cz.muni.fi.editor.api.dto.NotificationResponse; import cz.muni.fi.editor.api.exceptions.FieldException; import cz.muni.fi.editor.api.NotificationService; import cz.muni.fi.editor.database.dao.NotificationDAO; import cz.muni.fi.editor.database.dao.OrganizationDAO; import cz.muni.fi.editor.database.domain.Notification; import cz.muni.fi.editor.database.domain.user.Organization; import cz.muni.fi.editor.database.domain.user.User; import cz.muni.fi.editor.services.commons.Mapper; import cz.muni.fi.editor.services.commons.NotificationListener; import cz.muni.fi.editor.services.commons.events.*; import cz.muni.fi.editor.services.commons.security.SecurityService; import lombok.extern.log4j.Log4j2; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.event.TransactionalEventListener; import java.util.List; import java.util.stream.Collectors; /** * @author Dominik Szalai - emptulik at gmail.com on 17.06.2016. */ @Component @Log4j2 public class NotificationServiceImpl implements NotificationService, NotificationListener { @Autowired private NotificationDAO notificationDAO; @Autowired private OrganizationDAO organizationDAO; @Autowired private SecurityService securityService; @Autowired private Mapper mapper; @Autowired private NotificationFactory notificationFactory; @Override @Transactional(readOnly = true) public NotificationResponse getNotifications(NotificationRequest request) throws IllegalArgumentException { User u = mapper.map(securityService.getPrincipal(), User.class); NotificationResponse result = new NotificationResponse(); result.setRequest(request); result.setResult( notificationDAO .getNotifications(request.getPage() * request.getPageSize(), request.getPageSize(), u, request.getIncludeSeen()) .stream().map(this::mapNotification).collect(Collectors.toList())); result.setTotalUnseen(notificationDAO.numberOfUnseen(u)); return result; } @Override @Transactional(readOnly = true) public NotificationResponse poll(NotificationDTO latest) throws IllegalArgumentException, FieldException { User user = mapper.map(securityService.getPrincipal(), User.class); NotificationResponse response = new NotificationResponse(); response.setTotalUnseen(notificationDAO.numberOfUnseen(user)); response.setResult( mapper.map(notificationDAO.getLatestNotifications(user, mapper.map(latest, Notification.class)), NotificationDTO.class)); return response; } @Override @Transactional public void markSeen(List<NotificationDTO> notifications) throws IllegalArgumentException, FieldException { notificationDAO.markSeen(mapper.map(notifications, Notification.class)); } @TransactionalEventListener @Async @Override @Transactional public void notify(ERequestCreated rec) { if (StringUtils.contains(rec.getRequest().getClazz(), "Organization")) { if (rec.getRequest().getAction().equals("create")) { notifyMultiple(rec, organizationDAO.getById(1L).getMembers()); } else if (rec.getRequest().getAction().equals("join")) { Organization o = organizationDAO.getById(rec.getRequest().getTargetId()); Notification n = notificationFactory.provideNotification(rec); n.setNotified(o.getOwner()); notificationDAO.create(n); } } } @TransactionalEventListener @Async @Transactional @Override public void eOrganizationRenamed(EOrganizationRenamed eOrganizationRenamed) { notifyMultiple(eOrganizationRenamed, organizationDAO.getById(eOrganizationRenamed.getOrganization().getId()).getMembers()); } @TransactionalEventListener @Async @Transactional @Override public void eOrganizationDisbanded(EOrganizationDisbanded eOrganizationDisbanded) { notifyMultiple(eOrganizationDisbanded, eOrganizationDisbanded.getPreviousMembers().stream() .map(this::idToUser).collect(Collectors.toList())); } @TransactionalEventListener @Async @Transactional @Override public void eOrganizationPromotion(EOrganizationPromoted eOrganizationPromoted) { Notification notification = notificationFactory.provideNotification(eOrganizationPromoted); notificationDAO.create(notification); } @Override @TransactionalEventListener @Async @Transactional public void eOrganizationKicked(EOrganizationKicked eOrganizationKicked) { notificationDAO.create(notificationFactory.provideNotification(eOrganizationKicked)); } @Override @TransactionalEventListener @Async @Transactional public void eOrganizationInvited(EOrganizationInvited eOrganizationInvited) { notificationDAO.create(notificationFactory.provideNotification(eOrganizationInvited)); } @Override @TransactionalEventListener @Async @Transactional public void eRequestApproved(ERequestApproved eRequestApproved) { notificationDAO.create(notificationFactory.provideNotification(eRequestApproved)); } private void notifyMultiple(EditorEvent event, List<User> notified) { notified.forEach(u -> { Notification n = notificationFactory.provideNotification(event); n.setNotified(u); notificationDAO.create(n); }); } private User idToUser(Long id) { User u = new User(); u.setId(id); return u; } private NotificationDTO mapNotification(Notification n) { NotificationDTO dto = mapper.map(n, NotificationDTO.class); dto.setRequestID(n.getRequest().getId()); return dto; } }