Java tutorial
/* Copyright 2012 Simon Bear 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 au.csiro.notify.util; import java.net.URI; import java.util.Date; import java.util.GregorianCalendar; import java.util.List; import java.util.Set; import javax.annotation.Resource; import javax.xml.datatype.DatatypeFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.security.ldap.userdetails.LdapUserDetailsService; import org.springframework.stereotype.Component; import au.csiro.notify.dao.beans.Notification; import au.csiro.notify.dao.beans.TypeOfNotificationType; import au.csiro.notify.rest.dto.XMLNotificationType; import au.csiro.notify.rest.dto.XMLNotificationType.XMLRecipients; import au.csiro.notify.rest.dto.XMLNotificationURLType; import au.csiro.notify.rest.dto.XMLNotifications; import au.csiro.notify.rest.dto.XMLRecipientType; import au.csiro.notify.rest.dto.XMLTypeOfNotificationType; import au.csiro.notify.rest.dto.XMLTypeOfRecipientType; import com.google.common.collect.Sets; /** * Convert from Notification DTOs to Notification domain objects. * * Implemtation of the Assembler pattern from Patterns of Enterprise Application Architecture, Martin Fowler. * * Copyright 2012, Simon Bear All rights reserved. * * @author Simon on 21/04/2012 * @version $Revision$ $Date$ * @see http://martinfowler.com/eaaCatalog/dataTransferObject.html */ //@Converter @Component("notificationAssembler") public class NotificationAssembler { private static final Logger logger = LoggerFactory.getLogger(NotificationAssembler.class); @Resource(name = "datatypeFactory") private DatatypeFactory dtf; @Resource(name = "ldapEmailService") private LdapUserDetailsService ldapEmailService; @Resource(name = "ldapDNService") private LdapUserDetailsService ldapDNService; //@Converter public Notification createNotification(XMLNotificationType dto) { logger.trace("createNotification({})", dto); // Convert NotificationType from XML serializable to business object TypeOfNotificationType nt = null; if (dto.getNotificationType() != null) { switch (dto.getNotificationType()) { case ACTION_REQUIRED: nt = TypeOfNotificationType.ACTION_REQUIRED; break; case INFORMATION: default: nt = TypeOfNotificationType.INFORMATION; break; } } Date start = null; Date expiry = null; if (dto.getStartDate() != null) { start = dto.getStartDate().toGregorianCalendar().getTime(); } if (dto.getExpiryDate() != null) { expiry = dto.getExpiryDate().toGregorianCalendar().getTime(); } Set<String> recipients = Sets.newHashSet(); if (dto.getRecipients() != null) // dto.getRecipients().getRecipient is never null, so don't check { for (XMLRecipientType recipient : dto.getRecipients().getRecipient()) { String username; switch (recipient.getType()) { case EMAIL: username = ldapEmailService.loadUserByUsername(recipient.getValue()).getUsername(); break; case LDAP: username = ldapDNService.loadUserByUsername(recipient.getValue()).getUsername(); break; case USERNAME: default: username = recipient.getValue(); break; } recipients.add(username); } } Notification result = new Notification(dto.getSource(), nt, dto.getSubject(), dto.getMessage(), start, expiry, recipients); logger.trace("createNotification result: {}", result); return result; } //@Converter public XMLNotificationType createXMLNotificationType(Notification notification) { logger.trace("createXMLNotificationType({})", notification); XMLNotificationType xmlNotification = new XMLNotificationType(); xmlNotification.setSource(notification.getSource()); xmlNotification.setSubject(notification.getSubject()); xmlNotification.setMessage(notification.getMessage()); GregorianCalendar gc = new GregorianCalendar(); gc.setTime(notification.getStartDate()); xmlNotification.setStartDate(dtf.newXMLGregorianCalendar(gc)); gc.setTime(notification.getExpiryDate()); xmlNotification.setExpiryDate(dtf.newXMLGregorianCalendar(gc)); XMLTypeOfNotificationType type; switch (notification.getNotificationType()) { case ACTION_REQUIRED: type = XMLTypeOfNotificationType.ACTION_REQUIRED; break; case INFORMATION: default: type = XMLTypeOfNotificationType.INFORMATION; break; } xmlNotification.setNotificationType(type); xmlNotification.setRecipients(new XMLRecipients()); if (notification.getRecipients() != null) { for (String recipient : notification.getRecipients()) { XMLRecipientType xrt = new XMLRecipientType(); xrt.setType(XMLTypeOfRecipientType.USERNAME); xrt.setValue(recipient); xmlNotification.getRecipients().getRecipient().add(xrt); } } logger.trace("createXMLNotificationType result {}", xmlNotification); return xmlNotification; } //@Converter public XMLNotifications createXMLNotifications(List<Notification> notifications) { logger.trace("createXMLNotifications({})", notifications); XMLNotifications xmlNotifications = new XMLNotifications(); for (Notification notification : notifications) { XMLNotificationURLType xnut = new XMLNotificationURLType(); xnut.setId(notification.getId()); xnut.setUrl(URI.create("./" + notification.getId()).toString()); xmlNotifications.getNotification().add(xnut); } logger.trace("createXMLNotifications result {}", xmlNotifications); return xmlNotifications; } }