001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/presentation/tool/src/java/org/theospi/portfolio/presentation/control/NotifyViewersController.java $
003: * $Id:NotifyViewersController.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 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.theospi.portfolio.presentation.control;
021:
022: import org.sakaiproject.authz.api.Member;
023: import org.sakaiproject.component.api.ServerConfigurationService;
024: import org.sakaiproject.email.cover.EmailService;
025: import org.sakaiproject.metaobj.shared.model.Agent;
026: import org.sakaiproject.metaobj.shared.model.Id;
027: import org.sakaiproject.metaobj.utils.Config;
028: import org.sakaiproject.metaobj.utils.mvc.intf.CustomCommandController;
029: import org.sakaiproject.metaobj.utils.mvc.intf.ListScrollIndexer;
030: import org.sakaiproject.tool.cover.ToolManager;
031: import org.sakaiproject.tool.cover.SessionManager;
032: import org.sakaiproject.tool.api.Tool;
033: import org.sakaiproject.user.api.User;
034: import org.sakaiproject.user.cover.UserDirectoryService;
035: import org.springframework.mail.MailSender;
036: import org.springframework.validation.Errors;
037: import org.springframework.web.servlet.ModelAndView;
038: import org.theospi.portfolio.presentation.model.NotificationForm;
039: import org.theospi.portfolio.presentation.model.Presentation;
040: import org.theospi.portfolio.presentation.model.VelocityMailMessage;
041: import org.theospi.portfolio.presentation.tool.DecoratedViewer;
042:
043: import java.util.*;
044: import java.util.regex.Pattern;
045:
046: public class NotifyViewersController extends
047: AbstractPresentationController implements
048: CustomCommandController {
049: public static final String PARAM_CANCEL = "_cancel";
050: private Config ospConfig;
051: private VelocityMailMessage mailMessage;
052: private MailSender mailer;
053: private ListScrollIndexer listScrollIndexer;
054: private ServerConfigurationService serverConfigurationService;
055: private static final Pattern emailPattern = Pattern
056: .compile(".*@.*");
057:
058: public Object formBackingObject(Map request, Map session,
059: Map application) {
060: NotificationForm form = new NotificationForm();
061: Id id = getIdManager().getId(
062: (String) request.get("presentationId"));
063: form.setPresentationId(id);
064: Collection viewers = getPresentationManager().getPresentation(
065: id).getViewers();
066: List viewerList = new ArrayList();
067: for (Iterator i = viewers.iterator(); i.hasNext();) {
068: Agent agent = (Agent) i.next();
069: DecoratedViewer viewer = new DecoratedViewer(agent);
070: viewerList.add(viewer);
071: }
072: request.put("viewers", viewerList);
073: request.put("presentation", getPresentationManager()
074: .getPresentation(id));
075: return form;
076: }
077:
078: public ModelAndView handleRequest(Object requestModel, Map request,
079: Map session, Map application, Errors errors) {
080: NotificationForm form = (NotificationForm) requestModel;
081: Agent agent = getAuthManager().getAgent();
082: Presentation presentation = getPresentationManager()
083: .getPresentation(form.getPresentationId());
084: if (request.containsKey(PARAM_CANCEL)) {
085: return setupPresentationList(new Hashtable(), request,
086: presentation);
087: }
088:
089: Map model = new HashMap();
090: model.put("owner", agent.getDisplayName());
091: model.put("message", form.getMessage());
092: model.put("presentation", presentation);
093: model.put("osp_agent", agent);
094: User user = UserDirectoryService.getCurrentUser();
095:
096: String url = getServerConfigurationService().getServerUrl()
097: + "/osp-presentation-tool/viewPresentation.osp?id="
098: + presentation.getId().getValue();
099: url += "&"
100: + Tool.PLACEMENT_ID
101: + "="
102: + SessionManager.getCurrentToolSession()
103: .getPlacementId();
104:
105: String message = form.getMessage()
106: + "\n"
107: + "****************************************************************"
108: + "\n" + user.getDisplayName()
109: + " has shared a Presentation" + "\n"
110: + "This Presentation's name is: "
111: + presentation.getName() + "\n"
112: + "Click here to view the presentation: " + url;
113:
114: try {
115:
116: String from = getServerConfigurationService().getString(
117: "setup.request",
118: "postmaster@"
119: .concat(getServerConfigurationService()
120: .getServerName()));
121:
122: //getMailMessage().setTo(form.getRecipients());
123: //getMailMessage().setModel(model);
124: //getMailMessage().setFrom(user.getEmail());
125: //getMailer().send(getMailMessage());
126: for (int i = 0; i < form.getRecipients().length; i++) {
127: String toUser = form.getRecipients()[i];
128: if (toUser.startsWith("ROLE")) {
129: String role = toUser.substring(5, toUser.length());
130: Set members = getWorksiteManager().getSite(
131: presentation.getSiteId()).getMembers();
132: for (Iterator j = members.iterator(); j.hasNext();) {
133: Member member = (Member) j.next();
134: if (member.getRole().getId().equals(role)) {
135: String email = UserDirectoryService
136: .getUser(member.getUserId())
137: .getEmail();
138: if (validateEmail(email)) {
139: EmailService.send(from, email,
140: getMailMessage().getSubject(),
141: message, null, null, null);
142:
143: }
144: }
145: }
146:
147: } else {
148: EmailService.send(from, toUser, getMailMessage()
149: .getSubject(), message, null, null, null);
150: }
151: }
152:
153: } catch (Exception e) {
154: logger.error("error sending email notification", e);
155: }
156:
157: return setupPresentationList(model, request, presentation);
158: }
159:
160: protected ModelAndView setupPresentationList(Map model,
161: Map request, Presentation presentation) {
162: model.put("isMaintainer", isMaintainer());
163:
164: List presentations = new ArrayList(getPresentationManager()
165: .findPresentationsByViewer(getAuthManager().getAgent(),
166: ToolManager.getCurrentPlacement().getId()));
167:
168: model.put("presentations", getListScrollIndexer().indexList(
169: request, model, presentations));
170: model.put("osp_agent", getAuthManager().getAgent());
171:
172: return new ModelAndView("success", model);
173: }
174:
175: protected boolean validateEmail(String displayName) {
176:
177: if (!emailPattern.matcher(displayName).matches()) {
178:
179: return false;
180: }
181:
182: /*try {
183: InternetAddress.parse(displayName, true);
184: }
185: catch (AddressException e) {
186:
187: return false;
188: } */
189:
190: return true;
191: }
192:
193: protected int getPresentationIndex(List presentations,
194: Presentation presentation) {
195: if (presentation.getId() == null) {
196: return 0;
197: }
198:
199: for (int i = 0; i < presentations.size(); i++) {
200: Presentation current = (Presentation) presentations.get(i);
201: if (current.getId().equals(presentation.getId())) {
202: return i;
203: }
204: }
205: return 0;
206: }
207:
208: public ModelAndView processCancel(Map request, Map session,
209: Map application, Object command, Errors errors)
210: throws Exception {
211: NotificationForm form = (NotificationForm) command;
212: Presentation presentation = getPresentationManager()
213: .getPresentation(form.getPresentationId());
214:
215: return setupPresentationList(new Hashtable(), request,
216: presentation);
217: }
218:
219: public Config getOspConfig() {
220: return ospConfig;
221: }
222:
223: public void setOspConfig(Config ospConfig) {
224: this .ospConfig = ospConfig;
225: }
226:
227: public VelocityMailMessage getMailMessage() {
228: return mailMessage;
229: }
230:
231: public void setMailMessage(VelocityMailMessage mailMessage) {
232: this .mailMessage = mailMessage;
233: }
234:
235: public MailSender getMailer() {
236: return mailer;
237: }
238:
239: public void setMailer(MailSender mailer) {
240: this .mailer = mailer;
241: }
242:
243: public Map referenceData(Map request, Object command, Errors errors) {
244: return null; //To change body of implemented methods use File | Settings | File Templates.
245: }
246:
247: public ListScrollIndexer getListScrollIndexer() {
248: return listScrollIndexer;
249: }
250:
251: public void setListScrollIndexer(ListScrollIndexer listScrollIndexer) {
252: this .listScrollIndexer = listScrollIndexer;
253: }
254:
255: public ServerConfigurationService getServerConfigurationService() {
256: return serverConfigurationService;
257: }
258:
259: public void setServerConfigurationService(
260: ServerConfigurationService serverConfigurationService) {
261: this.serverConfigurationService = serverConfigurationService;
262: }
263:
264: }
|