001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/event/tags/sakai_2-4-1/event-util/util/src/java/org/sakaiproject/util/SiteEmailNotification.java $
003: * $Id: SiteEmailNotification.java 13804 2006-08-17 02:47:37Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.util;
021:
022: import java.util.List;
023: import java.util.Vector;
024:
025: import org.sakaiproject.alias.api.Alias;
026: import org.sakaiproject.alias.cover.AliasService;
027: import org.sakaiproject.authz.cover.SecurityService;
028: import org.sakaiproject.component.cover.ServerConfigurationService;
029: import org.sakaiproject.entity.api.Reference;
030: import org.sakaiproject.entity.cover.EntityManager;
031: import org.sakaiproject.event.api.Event;
032: import org.sakaiproject.event.api.NotificationAction;
033: import org.sakaiproject.event.cover.NotificationService;
034: import org.sakaiproject.mailarchive.cover.MailArchiveService;
035: import org.sakaiproject.site.api.Site;
036: import org.sakaiproject.site.cover.SiteService;
037:
038: /**
039: * <p>
040: * SiteEmailNotification is an EmailNotification that selects the site's participants (based on site access) as the recipients of the notification.
041: * </p>
042: * <p>
043: * getRecipients() is satified here (although it can be customized by the extensions to this class).
044: * </p>
045: * <p>
046: * The following should be specified to extend the class:
047: * <ul>
048: * <li>getResourceAbility() - to require an additional permission to qualify as a recipient (other than site membership)</li>
049: * <li>addSpecialRecipients() - to add other recipients</li>
050: * </ul>
051: * </p>
052: */
053: public class SiteEmailNotification extends EmailNotification {
054: /**
055: * Construct.
056: */
057: public SiteEmailNotification() {
058: }
059:
060: /**
061: * Construct.
062: *
063: * @param siteId
064: * The id of the site whose users will get a mailing.
065: */
066: public SiteEmailNotification(String siteId) {
067: super (siteId);
068: }
069:
070: /**
071: * @inheritDoc
072: */
073: public NotificationAction getClone() {
074: SiteEmailNotification clone = new SiteEmailNotification();
075: clone.set(this );
076:
077: return clone;
078: }
079:
080: /**
081: * @inheritDoc
082: */
083: protected List getRecipients(Event event) {
084: // get the resource reference
085: Reference ref = EntityManager.newReference(event.getResource());
086:
087: // use either the configured site, or if not configured, the site (context) of the resource
088: String siteId = (getSite() != null) ? getSite() : ref
089: .getContext();
090:
091: // if the site is published, use the list of users who can SITE_VISIT the site,
092: // else use the list of users who can SITE_VISIT_UNP the site.
093: try {
094: Site site = SiteService.getSite(siteId);
095: String ability = SiteService.SITE_VISIT;
096: if (!site.isPublished()) {
097: ability = SiteService.SITE_VISIT_UNPUBLISHED;
098: }
099:
100: // get the list of users who can do the right kind of visit
101: List users = SecurityService.unlockUsers(ability, ref
102: .getReference());
103:
104: // get the list of users who have the appropriate access to the resource
105: if (getResourceAbility() != null) {
106: List users2 = SecurityService.unlockUsers(
107: getResourceAbility(), ref.getReference());
108:
109: // find intersection of users and user2
110: users.retainAll(users2);
111: }
112:
113: // add any other users
114: addSpecialRecipients(users, ref);
115:
116: return users;
117: } catch (Exception any) {
118: return new Vector();
119: }
120: }
121:
122: /**
123: * Add to the user list any other users who should be notified about this ref's change.
124: *
125: * @param users
126: * The user list, already populated based on site visit and resource ability.
127: * @param ref
128: * The entity reference.
129: */
130: protected void addSpecialRecipients(List users, Reference ref) {
131: }
132:
133: /**
134: * Get the additional security function string needed for the resource that is the target of the notification <br />
135: * users who get notified need to have this ability with this resource, too.
136: *
137: * @return The additional ability string needed for a user to receive notification.
138: */
139: protected String getResourceAbility() {
140: return null;
141: }
142:
143: /**
144: * Format a to address, sensitive to the notification service's replyable configuration.
145: *
146: * @param event
147: * @return
148: */
149: protected String getTo(Event event) {
150: if (NotificationService.isNotificationToReplyable()) {
151: // to site title <email>
152: return "To: " + getToSite(event);
153: } else {
154: // to the site, but with no reply
155: return "To: " + getToSiteNoReply(event);
156: }
157: }
158:
159: /**
160: * Format a to address, to the related site, but with no reply.
161: *
162: * @param event
163: * The event that matched criteria to cause the notification.
164: * @return a to address, to the related site, but with no reply.
165: */
166: protected String getToSiteNoReply(Event event) {
167: Reference ref = EntityManager.newReference(event.getResource());
168:
169: // use either the configured site, or if not configured, the site (context) of the resource
170: String siteId = (getSite() != null) ? getSite() : ref
171: .getContext();
172:
173: // get a site title
174: String title = siteId;
175: try {
176: Site site = SiteService.getSite(siteId);
177: title = site.getTitle();
178: } catch (Exception ignore) {
179: }
180:
181: return "\"" + title + "\"<no-reply@"
182: + ServerConfigurationService.getServerName() + ">";
183: }
184:
185: /**
186: * Format the to site email address.
187: *
188: * @param event
189: * The event that matched criteria to cause the notification.
190: * @return the email address attribution for the site.
191: */
192: protected String getToSite(Event event) {
193: Reference ref = EntityManager.newReference(event.getResource());
194:
195: // use either the configured site, or if not configured, the site (context) of the resource
196: String siteId = (getSite() != null) ? getSite() : ref
197: .getContext();
198:
199: // get a site title
200: String title = siteId;
201: String email = null;
202: try {
203: Site site = SiteService.getSite(siteId);
204: title = site.getTitle();
205:
206: // check that the channel exists
207: String channel = "/mailarchive/channel/" + siteId + "/main";
208: MailArchiveService.getChannel(channel);
209:
210: // find the alias for this site's mail channel
211: List all = AliasService.getAliases(channel);
212: if (!all.isEmpty())
213: email = ((Alias) all.get(0)).getId();
214: } catch (Exception ignore) {
215: }
216:
217: // if for any reason we did not find an email, setup for the no-reply for email
218: if (email == null)
219: email = "no-reply";
220:
221: String rv = "\"" + title + "\" <" + email + "@"
222: + ServerConfigurationService.getServerName() + ">";
223:
224: return rv;
225: }
226: }
|