001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/trunk/samigo-app/src/java/org/sakaiproject/tool/assessment/ui/bean/evaluation/TotalScoresBean.java $
003: * $Id: EmailBean.java 18134 2006-11-14 18:59:25Z jholtzman@berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 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.tool.assessment.ui.bean.util;
021:
022: import java.io.Serializable;
023: import java.util.ArrayList;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: import javax.faces.application.FacesMessage;
029: import javax.faces.context.ExternalContext;
030: import javax.faces.context.FacesContext;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.sakaiproject.component.cover.ServerConfigurationService;
035: import org.sakaiproject.content.api.ContentResource;
036: import org.sakaiproject.content.api.FilePickerHelper;
037: import org.sakaiproject.content.cover.ContentHostingService;
038: import org.sakaiproject.entity.api.Reference;
039: import org.sakaiproject.entity.impl.ReferenceComponent;
040: import org.sakaiproject.exception.IdUnusedException;
041: import org.sakaiproject.exception.PermissionException;
042: import org.sakaiproject.exception.TypeException;
043: import org.sakaiproject.tool.api.ToolSession;
044: import org.sakaiproject.tool.assessment.data.dao.assessment.AttachmentData;
045: import org.sakaiproject.tool.assessment.data.ifc.assessment.AttachmentIfc;
046: import org.sakaiproject.tool.assessment.services.assessment.AssessmentService;
047: import org.sakaiproject.tool.assessment.ui.listener.util.ContextUtil;
048: import org.sakaiproject.tool.assessment.util.SamigoEmailService;
049: import org.sakaiproject.tool.cover.SessionManager;
050:
051: public class EmailBean implements Serializable {
052: private static Log log = LogFactory.getLog(EmailBean.class);
053:
054: private String fromName;
055:
056: private String fromEmailAddress;
057:
058: private String toName;
059:
060: private String toEmailAddress;
061:
062: private String toFirstName;
063:
064: private String assessmentName;
065:
066: private String subject;
067:
068: private String ccMe = "no";
069:
070: private String message;
071:
072: private List attachmentList;
073:
074: private boolean hasAttachment = false;
075:
076: private String outcome;
077:
078: private HashMap resourceHash = new HashMap();
079:
080: /**
081: * Creates a new TotalScoresBean object.
082: */
083: public EmailBean() {
084: log.debug("Creating a new EmailBean");
085: }
086:
087: public String getFromName() {
088: return fromName;
089: }
090:
091: public void setFromName(String fromName) {
092: this .fromName = fromName;
093: }
094:
095: public String getFromEmailAddress() {
096: return fromEmailAddress;
097: }
098:
099: public void setFromEmailAddress(String fromEmailAddress) {
100: this .fromEmailAddress = fromEmailAddress;
101: }
102:
103: public String getToName() {
104: return toName;
105: }
106:
107: public void setToName(String toName) {
108: this .toName = toName;
109: }
110:
111: public String getToEmailAddress() {
112: return toEmailAddress;
113: }
114:
115: public void setToEmailAddress(String toEmailAddress) {
116: this .toEmailAddress = toEmailAddress;
117: }
118:
119: public String getToFirstName() {
120: return toFirstName;
121: }
122:
123: public void setToFirstName(String toFirstName) {
124: this .toFirstName = toFirstName;
125: }
126:
127: public String getAssessmentName() {
128: return assessmentName;
129: }
130:
131: public void setAssessmentName(String assessmentName) {
132: this .assessmentName = assessmentName;
133: }
134:
135: public String getSubject() {
136: return subject;
137: }
138:
139: public void setSubject(String subject) {
140: this .subject = subject;
141: }
142:
143: public String getCcMe() {
144: return ccMe;
145: }
146:
147: public void setCcMe(String ccMe) {
148: this .ccMe = ccMe;
149: }
150:
151: public String getMessage() {
152: return message;
153: }
154:
155: public void setMessage(String message) {
156: this .message = message;
157: }
158:
159: public void setMessageTemplate() {
160: StringBuffer sb = new StringBuffer(
161: ContextUtil
162: .getLocalizedString(
163: "org.sakaiproject.tool.assessment.bundle.EvaluationMessages",
164: "dear"));
165: sb.append(" ");
166: sb.append(toFirstName);
167: sb
168: .append(ContextUtil
169: .getLocalizedString(
170: "org.sakaiproject.tool.assessment.bundle.EvaluationMessages",
171: "comma"));
172: sb.append("<br/>");
173: sb
174: .append(ContextUtil
175: .getLocalizedString(
176: "org.sakaiproject.tool.assessment.bundle.EvaluationMessages",
177: "regarding"));
178: sb.append(" ");
179: sb.append(assessmentName);
180: this .message = sb.toString();
181: }
182:
183: public List getAttachmentList() {
184: return attachmentList;
185: }
186:
187: public void setAttachmentList(List attachmentList) {
188: this .attachmentList = attachmentList;
189: }
190:
191: public boolean getHasAttachment() {
192: return this .hasAttachment;
193: }
194:
195: public void setHasAttachment(boolean hasAttachment) {
196: this .hasAttachment = hasAttachment;
197: }
198:
199: public String getOutcome() {
200: return outcome;
201: }
202:
203: public void setOutcome(String outcome) {
204: this .outcome = outcome;
205: }
206:
207: public void prepareAttachment() {
208: ToolSession session = SessionManager.getCurrentToolSession();
209: ArrayList newAttachmentList = new ArrayList();
210: if (session
211: .getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS) != null) {
212: List refs = (List) session
213: .getAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
214: Reference ref;
215: if (refs.size() == 0) {
216: hasAttachment = false;
217: } else {
218: HashMap map = getResourceIdHash(attachmentList);
219: for (int i = 0; i < refs.size(); i++) {
220: ref = (Reference) refs.get(i);
221: String resourceId = ref.getId();
222: if (map.get(resourceId) == null) {
223: AssessmentService assessmentService = new AssessmentService();
224: AttachmentData attach = assessmentService
225: .createEmailAttachment(
226: resourceId,
227: ref
228: .getProperties()
229: .getProperty(
230: ref
231: .getProperties()
232: .getNamePropDisplayName()),
233: ServerConfigurationService
234: .getServerUrl());
235:
236: newAttachmentList.add(attach);
237:
238: } else {
239: newAttachmentList.add((AttachmentData) map
240: .get(resourceId));
241: map.remove(resourceId);
242: }
243:
244: hasAttachment = true;
245: }
246: }
247: session
248: .removeAttribute(FilePickerHelper.FILE_PICKER_ATTACHMENTS);
249: session
250: .removeAttribute(FilePickerHelper.FILE_PICKER_CANCEL);
251: } else {
252: hasAttachment = false;
253: }
254: attachmentList = newAttachmentList;
255: }
256:
257: private HashMap getResourceIdHash(List attachmentList) {
258: HashMap map = new HashMap();
259: if (attachmentList != null) {
260: Iterator iter = attachmentList.iterator();
261: while (iter.hasNext()) {
262: AttachmentData attach = (AttachmentData) iter.next();
263: map.put(attach.getResourceId(), attach);
264: }
265: }
266: return map;
267: }
268:
269: public String addAttachmentsRedirect() {
270: try {
271: List filePickerList = new ArrayList();
272: if (attachmentList != null) {
273: filePickerList = prepareReferenceList(attachmentList);
274: }
275: log.debug("**filePicker list=" + filePickerList.size());
276: ToolSession currentToolSession = SessionManager
277: .getCurrentToolSession();
278: currentToolSession.setAttribute(
279: FilePickerHelper.FILE_PICKER_ATTACHMENTS,
280: filePickerList);
281: ExternalContext context = FacesContext.getCurrentInstance()
282: .getExternalContext();
283: context.redirect("sakai.filepicker.helper/tool");
284: } catch (Exception e) {
285: log.error("fail to redirect to attachment page: "
286: + e.getMessage());
287: }
288: return "email";
289: }
290:
291: private List prepareReferenceList(List attachmentList) {
292: List list = new ArrayList();
293: for (int i = 0; i < attachmentList.size(); i++) {
294: ContentResource cr = null;
295: AttachmentIfc attach = (AttachmentIfc) attachmentList
296: .get(i);
297: try {
298: cr = ContentHostingService.getResource(attach
299: .getResourceId());
300: } catch (PermissionException e) {
301: log
302: .warn("PermissionException from ContentHostingService:"
303: + e.getMessage());
304: } catch (IdUnusedException e) {
305: log
306: .warn("IdUnusedException from ContentHostingService:"
307: + e.getMessage());
308: } catch (TypeException e) {
309: log.warn("TypeException from ContentHostingService:"
310: + e.getMessage());
311: }
312: if (cr != null) {
313: if (this .resourceHash == null) {
314: this .resourceHash = new HashMap();
315: }
316: this .resourceHash.put(attach.getResourceId(), cr);
317: ReferenceComponent ref = new ReferenceComponent(cr
318: .getReference());
319: if (ref != null) {
320: list.add(ref);
321: }
322: }
323: }
324: return list;
325: }
326:
327: public HashMap getResourceHash() {
328: return resourceHash;
329: }
330:
331: public void setResourceHash(HashMap resourceHash) {
332: this .resourceHash = resourceHash;
333: }
334:
335: public String send() {
336: log.debug("send()");
337: log.debug("fromName: " + fromName);
338: log.debug("fromEmailAddress: " + fromEmailAddress);
339: log.debug("toName: " + toName);
340: log.debug("toEmailAddress: " + toEmailAddress);
341: log.debug("subject: " + subject);
342: log.debug("ccMe: " + ccMe);
343: log.debug("message: " + message);
344:
345: SamigoEmailService samigoEmailService = new SamigoEmailService(
346: fromName, fromEmailAddress, toName, toEmailAddress,
347: ccMe, subject, message);
348: String result = samigoEmailService.send();
349:
350: if (result.equals("send")) {
351: String msgSent = ContextUtil
352: .getLocalizedString(
353: "org.sakaiproject.tool.assessment.bundle.EvaluationMessages",
354: "sent_email_confirmation");
355: FacesContext.getCurrentInstance().addMessage(null,
356: new FacesMessage(msgSent));
357:
358: return "confirmEmailSent";
359: } else {
360: String msgError = ContextUtil
361: .getLocalizedString(
362: "org.sakaiproject.tool.assessment.bundle.EvaluationMessages",
363: "sent_email_error");
364: FacesContext.getCurrentInstance().addMessage(null,
365: new FacesMessage(msgError));
366:
367: return "emailError";
368: }
369: }
370:
371: public void cancel() {
372: log.debug("cancel");
373: setAttachmentList(null);
374: setHasAttachment(false);
375: }
376:
377: }
|