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. */ package cz.muni.fi.editor.services.api.notifications; import cz.muni.fi.editor.database.domain.Notification; import cz.muni.fi.editor.database.domain.requests.Request; import cz.muni.fi.editor.services.commons.events.*; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import java.text.MessageFormat; import java.time.LocalDateTime; /** * Created by Dominik Szalai - emptulik at gmail.com on 12.11.2016. */ @Component public class NotificationFactoryImpl implements NotificationFactory { private final String REQUEST_PATTERN = "to {0} {1}"; @Override public Notification provideNotification(EditorEvent editorEvent) { Notification n = new Notification(); n.setNotificationDate(LocalDateTime.now()); switch (editorEvent.getType()) { case 1: { EOrganizationDisbanded disbanded = (EOrganizationDisbanded) editorEvent; n.setMessage(MessageFormat.format(editorEvent.getNotificationMessage(), disbanded.getName())); } break; case 2: { EOrganizationInvited invited = (EOrganizationInvited) editorEvent; n.setMessage(MessageFormat.format(editorEvent.getNotificationMessage(), invited.getOrganization().getOrganizationName())); n.setNotified(invited.getUser()); } break; case 3: { } break; case 4: { EOrganizationKicked kicked = (EOrganizationKicked) editorEvent; n.setMessage(MessageFormat.format(editorEvent.getNotificationMessage(), kicked.getOrganization().getOrganizationName())); n.setNotified(kicked.getUser()); } break; case 5: { EOrganizationPromoted promoted = (EOrganizationPromoted) editorEvent; n.setMessage(MessageFormat.format(editorEvent.getNotificationMessage(), promoted.getOrganization().getOrganizationName())); n.setNotified(promoted.getOrganization().getOwner()); } break; case 6: { EOrganizationRenamed renamed = (EOrganizationRenamed) editorEvent; n.setMessage(MessageFormat.format(editorEvent.getNotificationMessage(), renamed.getOriginalName(), renamed.getOrganization().getOrganizationName())); } break; case 7: { ERequestApproved approved = (ERequestApproved) editorEvent; n.setMessage(MessageFormat.format(editorEvent.getNotificationMessage(), requestToParameter(approved.getRequest()))); n.setRequest(approved.getRequest()); n.setNotified(approved.getRequest().getRequestor()); } break; case 8: { ERequestCreated created = (ERequestCreated) editorEvent; n.setRequest(created.getRequest()); n.setMessage(MessageFormat.format(editorEvent.getNotificationMessage(), requestToParameter(created.getRequest()))); } break; case 9: { ERequestDenied denied = (ERequestDenied) editorEvent; n.setRequest(denied.getRequest()); n.setMessage(MessageFormat.format(editorEvent.getNotificationMessage(), requestToParameter(denied.getRequest()))); } break; default: throw new IllegalArgumentException(MessageFormat .format("Invalid event type. Type cannot be greater than {0}", EditorEvent.types.length - 1)); } return n; } private String requestToParameter(Request request) { if (StringUtils.contains(request.getClazz(), "Organization")) { return MessageFormat.format(REQUEST_PATTERN, request.getAction(), "organization"); } else { //todo in the future return StringUtils.EMPTY; } } }