001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/syllabus/tags/sakai_2-4-1/syllabus-impl/src/java/org/sakaiproject/component/app/syllabus/SiteEmailNotificationSyllabus.java $
003: * $Id: SiteEmailNotificationSyllabus.java 15061 2006-09-20 16:57:12Z lance@indiana.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.component.app.syllabus;
021:
022: import java.util.ArrayList;
023: import java.util.List;
024: import java.util.Iterator;
025: import java.util.Set;
026: import java.util.ResourceBundle;
027: import java.util.Vector;
028:
029: import org.sakaiproject.api.app.syllabus.SyllabusData;
030: import org.sakaiproject.api.app.syllabus.SyllabusItem;
031: import org.sakaiproject.api.app.syllabus.SyllabusManager;
032: import org.sakaiproject.api.app.syllabus.SyllabusService;
033: import org.sakaiproject.user.cover.UserDirectoryService;
034: import org.sakaiproject.component.cover.ComponentManager;
035: import org.sakaiproject.component.cover.ServerConfigurationService;
036: import org.sakaiproject.entity.api.Reference;
037: import org.sakaiproject.entity.cover.EntityManager;
038: import org.sakaiproject.event.api.Event;
039: import org.sakaiproject.event.api.NotificationAction;
040: import org.sakaiproject.site.api.Site;
041: import org.sakaiproject.site.cover.SiteService;
042: import org.sakaiproject.util.SiteEmailNotification;
043:
044: public class SiteEmailNotificationSyllabus extends
045: SiteEmailNotification {
046: // //private static ResourceBundle rb = ResourceBundle.getBundle("siteemaanc");
047: private static ResourceBundle rb = ResourceBundle
048: .getBundle("siteemacon");
049:
050: private org.sakaiproject.component.api.ComponentManager cm;
051:
052: private static String SYLLABUS_MANAGER = "org.sakaiproject.api.app.syllabus.SyllabusManager";
053:
054: private SyllabusManager syllabusManager;
055:
056: public SiteEmailNotificationSyllabus() {
057: cm = ComponentManager.getInstance();
058: syllabusManager = (org.sakaiproject.api.app.syllabus.SyllabusManager) cm
059: .get(SYLLABUS_MANAGER);
060: }
061:
062: public SiteEmailNotificationSyllabus(String siteId) {
063: super (siteId);
064: }
065:
066: public NotificationAction getClone() {
067: SiteEmailNotificationSyllabus clone = new SiteEmailNotificationSyllabus();
068: clone.set(this );
069:
070: return clone;
071: }
072:
073: protected String getSubject(Event event) {
074: Reference ref = EntityManager.newReference(event.getResource());
075: String function = event.getEvent();
076: String siteId = (getSite() != null) ? getSite() : ref
077: .getContext();
078:
079: if (siteId == null) {
080: int lastIndex = ref.getReference().lastIndexOf("/");
081: String dataId = ref.getReference().substring(lastIndex + 1);
082: SyllabusData syllabusData = syllabusManager
083: .getSyllabusData(dataId);
084: SyllabusItem syllabusItem = syllabusData.getSyllabusItem();
085: siteId = syllabusItem.getContextId();
086: }
087:
088: String title = siteId;
089: try {
090: Site site = SiteService.getSite(siteId);
091: title = site.getTitle();
092: } catch (Exception ignore) {
093: }
094:
095: // String syllabusName = props.getPropertyFormatted(ResourceProperties.PROP_DISPLAY_NAME);
096: int lastIndex = ref.getReference().lastIndexOf("/");
097: String dataId = ref.getReference().substring(lastIndex + 1);
098: SyllabusData syllabusData = syllabusManager
099: .getSyllabusData(dataId);
100: String syllabusName = syllabusData.getTitle();
101: String returnedString = "";
102: if (SyllabusService.EVENT_SYLLABUS_POST_NEW.equals(function)) {
103: returnedString = "[ " + title + " - "
104: + "New Posted Syllabus Item" + " ] " + syllabusName;
105: ;
106: } else if (SyllabusService.EVENT_SYLLABUS_POST_CHANGE
107: .equals(function)) {
108: returnedString = "[ " + title + " - "
109: + "Existing Syllabus Item Changed" + " ] "
110: + syllabusName;
111: ;
112: } else if (SyllabusService.EVENT_SYLLABUS_DELETE_POST
113: .equals(function)) {
114: returnedString = "[ " + title + " - "
115: + "Posted Syllabus Item Has Been Deleted" + " ] "
116: + syllabusName;
117: ;
118: }
119:
120: return returnedString;
121: }
122:
123: /**
124: * @inheritDoc
125: */
126: protected String getMessage(Event event) {
127: StringBuffer buf = new StringBuffer();
128:
129: if (SyllabusService.EVENT_SYLLABUS_POST_NEW.equals(event
130: .getEvent())
131: || SyllabusService.EVENT_SYLLABUS_POST_CHANGE
132: .equals(event.getEvent())) {
133: String newline = "<br />\n";
134:
135: Reference ref = EntityManager.newReference(event
136: .getResource());
137: String siteId = (getSite() != null) ? getSite() : ref
138: .getContext();
139:
140: int lastIndex = ref.getReference().lastIndexOf("/");
141: String dataId = ref.getReference().substring(lastIndex + 1);
142: SyllabusData syllabusData = syllabusManager
143: .getSyllabusData(dataId);
144: SyllabusItem syllabusItem = syllabusData.getSyllabusItem();
145: if (siteId == null) {
146: siteId = syllabusItem.getContextId();
147: }
148: // get a site title
149: String title = siteId;
150: try {
151: Site site = SiteService.getSite(siteId);
152: title = site.getTitle();
153: } catch (Exception ignore) {
154: }
155:
156: buf.append(syllabusData.getAsset() + newline);
157: } else if (SyllabusService.EVENT_SYLLABUS_DELETE_POST
158: .equals(event.getEvent())) {
159: String newline = "<br />\n";
160:
161: Reference ref = EntityManager.newReference(event
162: .getResource());
163: String siteId = (getSite() != null) ? getSite() : ref
164: .getContext();
165:
166: int lastIndex = ref.getReference().lastIndexOf("/");
167: String dataId = ref.getReference().substring(lastIndex + 1);
168: SyllabusData syllabusData = syllabusManager
169: .getSyllabusData(dataId);
170: SyllabusItem syllabusItem = syllabusData.getSyllabusItem();
171: if (siteId == null) {
172: siteId = syllabusItem.getContextId();
173: }
174:
175: buf.append(" Syllabus Item - ");
176: buf.append(syllabusData.getTitle());
177: buf.append(" for Site - ");
178: buf.append(siteId);
179: buf.append(" has been deleted.");
180: buf.append(newline);
181: }
182:
183: return buf.toString();
184: }
185:
186: /**
187: * @inheritDoc
188: */
189: protected List getHeaders(Event event) {
190: List rv = new Vector();
191:
192: // Set the content type of the message body to HTML
193: rv.add("Content-Type: text/html");
194:
195: // set the subject
196: rv.add("Subject: " + getSubject(event));
197:
198: // from
199: rv.add(getFrom(event));
200:
201: // to
202: rv.add(getTo(event));
203:
204: return rv;
205: }
206:
207: /**
208: * @inheritDoc
209: */
210: protected boolean isBodyHTML(Event e) {
211: return true;
212: }
213:
214: /**
215: * @inheritDoc
216: */
217: protected String getTag(String newline, String title) {
218: // tag the message - HTML version
219: String rv = newline
220: + rb.getString("separator")
221: + newline
222: + rb.getString("this")
223: + " "
224: + ServerConfigurationService.getString("ui.service",
225: "Sakai") + " (<a href=\""
226: + ServerConfigurationService.getPortalUrl() + "\">"
227: + ServerConfigurationService.getPortalUrl() + "</a>) "
228: + rb.getString("forthe") + " " + title + " "
229: + rb.getString("site") + newline
230: + rb.getString("youcan") + newline;
231: return rv;
232: }
233:
234: protected void addSpecialRecipients(List users, Reference ref) {
235: //for SiteEmailNotification.getRecipients method doesn't get syllabus' recipients.
236: //List users = SecurityService.unlockUsers(ability, ref.getReference()); doesn't get users for the site because of permission messing
237: //need add syllabus permission later
238: try {
239: String siteId = (getSite() != null) ? getSite() : ref
240: .getContext();
241: Site site = SiteService.getSite(siteId);
242: Set activeUsersIdSet = site.getUsers();
243: List activeUsersIdList = new ArrayList(activeUsersIdSet
244: .size());
245: // List activeUserList = new Vector();
246: Iterator iter = activeUsersIdSet.iterator();
247: while (iter.hasNext()) {
248: activeUsersIdList.add((String) iter.next());
249: }
250:
251: List activeUserList = UserDirectoryService
252: .getUsers(activeUsersIdList);
253: activeUserList.removeAll(users);
254: for (int i = 0; i < activeUserList.size(); i++) {
255: users.add(activeUserList.get(i));
256: }
257: } catch (Exception e) {
258: System.out.println(e); // TODO need a logger
259: }
260: }
261:
262: }
|