📄 notificationservice.java
字号:
/*--------------------------------------------------------------------------*
| Copyright (C) 2006 Christopher Kohlhaas |
| |
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by the |
| Free Software Foundation. A copy of the license has been included with |
| these distribution in the COPYING file, if not go to www.fsf.org |
| |
| As a special exception, you are granted the permissions to link this |
| program with every library, which license fulfills the Open Source |
| Definition as published by the Open Source Initiative (OSI). |
*--------------------------------------------------------------------------*/
package org.rapla.plugin.notification;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.rapla.components.util.Command;
import org.rapla.components.util.CommandQueue;
import org.rapla.entities.User;
import org.rapla.entities.configuration.Preferences;
import org.rapla.entities.configuration.RaplaMap;
import org.rapla.entities.domain.Allocatable;
import org.rapla.entities.domain.Appointment;
import org.rapla.entities.domain.Repeating;
import org.rapla.entities.domain.Reservation;
import org.rapla.entities.dynamictype.Attribute;
import org.rapla.entities.dynamictype.Classification;
import org.rapla.facade.AllocationChangeEvent;
import org.rapla.facade.AllocationChangeListener;
import org.rapla.facade.ClientFacade;
import org.rapla.facade.RaplaComponent;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
import org.rapla.plugin.mail.MailPlugin;
import org.rapla.plugin.mail.MailToUserInterface;
/** Sends Notification Mails on allocation change.*/
public class NotificationService extends RaplaComponent
implements
AllocationChangeListener
{
public final static String ALLOCATIONLISTENERS_CONFIG = "org.rapla.plugin.notification.allocationlisteners";
public final static String NOTIFY_IF_OWNER_CONFIG = "org.rapla.plugin.notification.notify_if_owner";
ClientFacade clientFacade;
MailToUserInterface mailToUserInterface;
protected CommandQueue mailQueue;
public NotificationService(RaplaContext sm) throws RaplaException
{
super( sm);
mailToUserInterface = (MailToUserInterface) sm.lookup( MailToUserInterface.ROLE + "/" + MailPlugin.MAIL_ON_LOCALHOST );
clientFacade = (ClientFacade) sm.lookup(ClientFacade.ROLE);
setChildBundleName( NotificationPlugin.RESOURCE_FILE );
mailQueue = org.rapla.components.util.CommandQueue.createCommandQueue();
clientFacade.addAllocationChangedListener(this);
getLogger().info("NotificationServer Plugin started");
}
public void changed(AllocationChangeEvent[] changeEvents) {
try {
getLogger().debug("TRIGGERD");
User[] users = clientFacade.getUsers();
List mailList = new ArrayList();
for ( int i=0;i< users.length;i++) {
User user = users[i];
Preferences preferences = clientFacade.getPreferences(user);
Map allocatableMap ;
if (preferences != null && preferences.getEntry(ALLOCATIONLISTENERS_CONFIG)!= null ) {
allocatableMap = ((RaplaMap)(preferences.getEntry(ALLOCATIONLISTENERS_CONFIG)));
}else {
allocatableMap = new HashMap();
}
if ( allocatableMap.size()> 0)
{
boolean notifyIfOwner = preferences.getEntryAsBoolean(NOTIFY_IF_OWNER_CONFIG, false);
AllocationMail mail = getAllocationMail( new HashSet(allocatableMap.values()), changeEvents, preferences.getOwner(),notifyIfOwner);
if (mail != null) {
mailList.add(mail);
}
}
}
MailCommand mailCommand = new MailCommand(mailList);
mailQueue.enqueue(mailCommand);
} catch (RaplaException ex) {
getLogger().error("Can't trigger notification service." + ex.getMessage(),ex);
}
}
public boolean isInvokedOnAWTEventQueue() {
return false;
}
AllocationMail getAllocationMail(Collection allocatables, AllocationChangeEvent[] changeEvents, User owner,boolean notifyIfOwner) throws RaplaException {
HashMap reservationMap = null;
HashSet changedAllocatables = null;
for ( int i = 0; i< changeEvents.length; i++) {
if (reservationMap == null)
reservationMap = new HashMap(4);
AllocationChangeEvent event = changeEvents[i];
Reservation reservation = event.getNewReservation();
if (!allocatables.contains(event.getAllocatable()))
continue;
if (!notifyIfOwner && owner.equals(reservation.getOwner()))
continue;
List eventList = (List)reservationMap.get(reservation);
if (eventList == null) {
eventList = new ArrayList(3);
reservationMap.put(reservation,eventList);
changedAllocatables = new HashSet();
}
changedAllocatables.add(event.getAllocatable());
eventList.add(event);
}
if ( reservationMap == null) {
return null;
}
Set keySet = reservationMap.keySet();
// Check if we have any notifications.
if (keySet.size() == 0)
return null;
AllocationMail mail = new AllocationMail();
StringBuffer buf = new StringBuffer();
buf.append(getString("mail_body") + "\n");
Iterator it = keySet.iterator();
while (it.hasNext()) {
Reservation reservation = (Reservation)it.next();
List eventList = (List) reservationMap.get(reservation);
printEvents(buf,reservation,eventList);
buf.append("\n\n");
}
it = changedAllocatables.iterator();
StringBuffer allocatableNames = null;
while (it.hasNext()) {
if (allocatableNames == null) {
allocatableNames = new StringBuffer();
} else {
allocatableNames.append(", ");
}
allocatableNames.append(((Allocatable)it.next()).getName(getLocale()));
}
mail.subject = getI18n().format("mail_subject",allocatableNames.toString());
mail.body = buf.toString();
mail.recipient = owner.getUsername();
return mail;
}
private void printEvents(StringBuffer buf,Reservation reservation,List eventList) {
buf.append("\n");
buf.append("-----------");
buf.append(getString("changes"));
buf.append("-----------");
buf.append("\n");
buf.append("\n");
buf.append(getString("reservation"));
buf.append(": ");
buf.append(reservation.getName(getLocale()));
buf.append("\n");
buf.append("\n");
Iterator it = eventList.iterator();
boolean removed = true;
while (it.hasNext()) {
AllocationChangeEvent event = (AllocationChangeEvent)it.next();
if (!event.getType().equals( AllocationChangeEvent.REMOVE ))
removed = false;
buf.append(getI18n().format("appointment." + event.getType()
,event.getAllocatable().getName(getLocale()))
);
if (!event.getType().equals(AllocationChangeEvent.ADD )) {
printAppointment (buf, event.getOldAppointment() );
}
if (event.getType().equals( AllocationChangeEvent.CHANGE )) {
buf.append(getString("moved_to"));
}
if (!event.getType().equals( AllocationChangeEvent.REMOVE )) {
printAppointment (buf, event.getNewAppointment() );
}
if ( event.getUser() != null) {
buf.append("\n");
buf.append( getI18n().format("modified_by", event.getUser().getUsername() ) );
}
buf.append("\n");
buf.append("\n");
}
if (removed)
return;
buf.append("-----------");
buf.append(getString("complete_reservation"));
buf.append("-----------");
buf.append("\n");
buf.append("\n");
buf.append(getString("reservation.owner"));
buf.append(": ");
buf.append(reservation.getOwner().getUsername());
buf.append(" <");
buf.append(reservation.getOwner().getName());
buf.append(">");
buf.append("\n");
buf.append(getString("reservation_type"));
buf.append(": ");
Classification classification = reservation.getClassification();
buf.append( classification.getType().getName(getLocale()) );
Attribute[] attributes = classification.getAttributes();
for (int i=0; i< attributes.length; i++) {
Object value = classification.getValue(attributes[i]);
if (value == null)
continue;
buf.append("\n");
buf.append(attributes[i].getName(getLocale()));
buf.append(": ");
buf.append(classification.getValueAsString(attributes[i], getLocale()));
}
Allocatable[] resources = reservation.getResources();
if (resources.length>0) {
buf.append("\n");
buf.append( getString("resources"));
buf.append( ": ");
printAllocatables(buf,reservation,resources);
}
Allocatable[] persons = reservation.getPersons();
if (persons.length>0) {
buf.append("\n");
buf.append( getString("persons"));
buf.append( ": ");
printAllocatables(buf,reservation,persons);
}
Appointment[] appointments = reservation.getAppointments();
if (appointments.length>0) {
buf.append("\n");
buf.append("\n");
buf.append( getString("appointments"));
buf.append( ": ");
buf.append("\n");
}
for (int i = 0;i<appointments.length;i++) {
printAppointment(buf, appointments[i]);
}
}
private void printAppointment(StringBuffer buf, Appointment app) {
buf.append("\n");
buf.append(getAppointmentFormater().getSummary(app));
buf.append("\n");
Repeating repeating = app.getRepeating();
if ( repeating != null ) {
buf.append(getAppointmentFormater().getSummary(app.getRepeating()));
buf.append("\n");
if ( repeating.hasExceptions() ) {
String exceptionString =
getString("appointment.exceptions") + ": " + repeating.getExceptions().length ;
buf.append(exceptionString);
buf.append("\n");
}
}
}
private String printAllocatables(StringBuffer buf
,Reservation reservation
,Allocatable[] allocatables) {
for (int i = 0;i<allocatables.length;i++) {
Allocatable allocatable = allocatables[i];
buf.append(allocatable.getName( getLocale()));
printRestriction(buf,reservation, allocatable);
if (i<allocatables.length-1) {
buf.append (",");
}
}
return buf.toString();
}
private void printRestriction(StringBuffer buf
,Reservation reservation
, Allocatable allocatable) {
Appointment[] restriction = reservation.getRestriction(allocatable);
if ( restriction.length == 0 )
return;
buf.append(" (");
for (int i = 0; i < restriction.length ; i++) {
if (i >0)
buf.append(", ");
buf.append( getAppointmentFormater().getShortSummary( restriction[i]) );
}
buf.append(")");
}
class AllocationMail {
String recipient;
String subject;
String body;
public String toString() {
return "TO Username: " + recipient + "\n"
+ "Subject: " + subject + "\n"
+ body;
}
}
final class MailCommand implements Command {
List mailList;
public MailCommand(List mailList) {
this.mailList = mailList;
}
public void execute() {
Iterator it = mailList.iterator();
while (it.hasNext()) {
AllocationMail mail = (AllocationMail) it.next();
if (getLogger().isDebugEnabled())
getLogger().debug("Sending mail " + mail.toString());
else if (getLogger().isInfoEnabled())
getLogger().info("AllocationChange. Sending mail to " + mail.recipient);
try {
mailToUserInterface.sendMail(mail.recipient, mail.subject, mail.body );
getLogger().info("AllocationChange. Mail sent.");
} catch (RaplaException ex) {
getLogger().error("Could not send mail to " + mail.recipient + " Cause: " + ex.getMessage(), ex);
}
}
}
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -