0001: /**********************************************************************************
0002: * $URL: https://source.sakaiproject.org/svn/mailtool/tags/sakai_2-4-1/mailtool/src/java/org/sakaiproject/tool/mailtool/Mailtool.java $
0003: * $Id: Mailtool.java 29125 2007-04-18 23:00:44Z ajpoland@iupui.edu $
0004: ***********************************************************************************
0005: *
0006: * Copyright (c) 2006, 2007 The Sakai Foundation.
0007: *
0008: * Licensed under the Educational Community License, Version 1.0 (the "License");
0009: * you may not use this file except in compliance with the License.
0010: * You may obtain a copy of the License at
0011: *
0012: * http://www.opensource.org/licenses/ecl1.php
0013: *
0014: * Unless required by applicable law or agreed to in writing, software
0015: * distributed under the License is distributed on an "AS IS" BASIS,
0016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0017: * See the License for the specific language governing permissions and
0018: * limitations under the License.
0019: *
0020: **********************************************************************************/package org.sakaiproject.tool.mailtool;
0021:
0022: // import java.lang.Thread;
0023: import java.io.File;
0024: import java.util.ArrayList;
0025: import java.util.Collection;
0026: import java.util.Collections;
0027: import java.util.TreeSet;
0028: import java.util.Iterator;
0029: import java.util.List;
0030: import java.util.Set;
0031: import java.util.Vector;
0032: import java.util.regex.Pattern;
0033: import java.util.regex.Matcher;
0034: import org.apache.commons.fileupload.FileItem;
0035: import org.apache.commons.logging.Log;
0036: import org.apache.commons.logging.LogFactory;
0037: import org.apache.commons.io.FilenameUtils;
0038: import org.sakaiproject.tool.cover.ToolManager; // import org.sakaiproject.email.cover.EmailService;
0039: import org.sakaiproject.event.cover.NotificationService;
0040: import org.sakaiproject.authz.api.AuthzGroup;
0041: import org.sakaiproject.authz.cover.AuthzGroupService;
0042: import org.sakaiproject.authz.api.Role;
0043: import org.sakaiproject.component.cover.ServerConfigurationService;
0044: import org.sakaiproject.site.api.Group;
0045: import org.sakaiproject.site.api.Site;
0046: import org.sakaiproject.site.api.ToolConfiguration;
0047: import org.sakaiproject.time.cover.TimeService;
0048: import org.sakaiproject.user.api.User;
0049: import org.sakaiproject.user.cover.UserDirectoryService;
0050: import org.sakaiproject.mailarchive.api.MailArchiveChannel;
0051: import org.sakaiproject.mailarchive.api.MailArchiveMessageEdit;
0052: import org.sakaiproject.mailarchive.api.MailArchiveMessageHeaderEdit;
0053: import org.sakaiproject.mailarchive.cover.MailArchiveService;
0054: import org.sakaiproject.site.cover.SiteService; // import org.sakaiproject.site.api.SitePage;
0055: // import org.sakaiproject.tool.api.ToolSession;
0056: // import org.sakaiproject.tool.cover.SessionManager;
0057: import javax.faces.context.FacesContext;
0058: import javax.faces.application.FacesMessage;
0059: import javax.faces.event.PhaseId;
0060: import javax.faces.event.ValueChangeEvent; // import javax.faces.event.ActionEvent;
0061: import javax.faces.event.AbortProcessingException;
0062: import javax.faces.validator.ValidatorException;
0063: import javax.faces.component.UIComponent;
0064: import java.util.Properties;
0065: import javax.mail.BodyPart;
0066: import javax.mail.Message;
0067: import javax.mail.Multipart;
0068: import javax.mail.Session;
0069: import javax.mail.Transport;
0070: import javax.mail.internet.MimeBodyPart;
0071: import javax.mail.internet.MimeMessage;
0072: import javax.mail.internet.MimeMultipart;
0073: import javax.mail.internet.InternetAddress;
0074: import javax.activation.DataHandler;
0075: import javax.activation.DataSource;
0076: import javax.activation.FileDataSource;
0077:
0078: /**
0079: * Mailtool bean for compose (expanded by kimsooil@bu.edu)
0080: *
0081: * @author sgithen
0082: *
0083: */
0084: public class Mailtool {
0085: private final Log log = LogFactory.getLog(this .getClass());
0086:
0087: protected FacesContext facesContext = FacesContext
0088: .getCurrentInstance();
0089:
0090: protected boolean DEBUG_NO_EMAIL = true;
0091:
0092: protected static final int NUMBER_ROLES = 15;
0093:
0094: private int MaxNumAttachment = readMaxNumAttachment();
0095:
0096: /** Config Parameters * */
0097: protected String m_realm = "";
0098:
0099: protected List /* EmailRole */m_emailroles = new ArrayList();
0100:
0101: protected String m_recipview = "";
0102:
0103: protected String uploaddirectoryDefault = "/tmp/";
0104:
0105: protected String recipviewDefault = "tree";
0106:
0107: protected String groupAwareRoleDefault = "";
0108:
0109: protected String groupAwareRoleFound = "";
0110:
0111: /** For Main.jsp * */
0112: protected String m_subject = "";
0113:
0114: protected String m_subjectprefix = "";
0115:
0116: protected String m_otheremails = "";
0117:
0118: protected String m_replytootheremail = "";
0119:
0120: protected String m_body = "";
0121:
0122: protected String m_editortype = "";
0123:
0124: protected String m_replyto = "";
0125:
0126: protected String m_sitetype = "";
0127:
0128: protected String m_mode = "";
0129:
0130: protected String m_siteid = "";
0131:
0132: protected String m_realmid = "";
0133:
0134: protected String sitename = "";
0135:
0136: protected boolean is_fckeditor = false;
0137:
0138: protected boolean is_htmlarea = false;
0139:
0140: protected RecipientSelector m_recipientSelector = null;
0141:
0142: protected RecipientSelector m_recipientSelector1 = null;
0143:
0144: protected RecipientSelector m_recipientSelector2 = null;
0145:
0146: protected RecipientSelector m_recipientSelector3 = null;
0147:
0148: protected boolean m_selectByRole = false;
0149:
0150: protected boolean m_selectByUser = false;
0151:
0152: protected boolean m_selectByTree = false;
0153:
0154: protected boolean m_selectSideBySide = false;
0155:
0156: protected boolean m_selectByFoothill = false;
0157:
0158: protected boolean m_archiveMessage = false;
0159:
0160: protected boolean m_sendmecopy = false;
0161:
0162: protected boolean m_replytosender = false;
0163:
0164: protected boolean m_donotreply = false;
0165:
0166: protected boolean m_replytoother = false;
0167:
0168: protected boolean m_allusers = false;
0169:
0170: protected boolean EmailArchiveInSite = false;
0171:
0172: protected int num_groups = 0;
0173:
0174: protected int num_sections = 0;
0175:
0176: protected int num_groupawarerole = 0;
0177:
0178: protected String m_textformat = "";
0179:
0180: private String m_recipJSPfrag = "";
0181:
0182: protected String m_results = "";
0183:
0184: /** Set Sakai Services */
0185: // protected EmailService m_emailService = null;
0186: private UserDirectoryService m_userDirectoryService;
0187:
0188: private AuthzGroupService m_realmService;
0189:
0190: private AuthzGroup arole;
0191:
0192: private SiteService siteService;
0193:
0194: protected ToolConfiguration m_toolConfig = null;
0195:
0196: protected Site currentSite = null;
0197:
0198: private List attachedFiles = new ArrayList();
0199:
0200: private List renamedRoles = new ArrayList();
0201:
0202: private int num_roles_renamed = 0;
0203:
0204: private int num_role_id = 0;
0205:
0206: private String roleid = "";
0207:
0208: private String singular = "";
0209:
0210: private String plural = "";
0211:
0212: private boolean already_configured = false;
0213:
0214: private String filename = "";
0215:
0216: private int num_files = 0;
0217:
0218: private int num_id = 0;
0219:
0220: private boolean attachClicked = false;
0221:
0222: private boolean groupviewClicked = false;
0223:
0224: private boolean sectionviewClicked = false;
0225:
0226: private boolean groupAwareRoleviewClicked = false;
0227:
0228: private boolean showRenamingRolesClicked = false;
0229:
0230: private boolean allGroupSelected = false;
0231:
0232: private boolean allSectionSelected = false;
0233:
0234: private boolean allGroupAwareRoleSelected = false;
0235:
0236: private boolean GroupAwareRoleExist = false;
0237:
0238: private List selected = null;
0239:
0240: private List selectedGroupAwareRoleUsers = null;
0241:
0242: private List selectedGroupUsers = null;
0243:
0244: private List selectedSectionUsers = null;
0245:
0246: /**
0247: * Mailtool bean for compose page
0248: */
0249: public Mailtool() {
0250: num_groups = 0;
0251: num_sections = 0;
0252: num_groupawarerole = 0;
0253:
0254: m_sitetype = getSiteType();
0255: m_siteid = getSiteID();
0256: m_realmid = getSiteRealmID();
0257: groupAwareRoleDefault = getGroupAwareRoleDefault();
0258: groupAwareRoleFound = getGroupAwareRole();
0259: sitename = getSiteTitle();
0260:
0261: setSelectorType();
0262: getRecipientSelectors();
0263:
0264: checkifGroupAwareRoleExist(); /* this initialization solves SAK-6810 */
0265:
0266: setMessageSubject(getSubjectPrefix().equals("") ? getSubjectPrefixFromConfig()
0267: : getSubjectPrefix());
0268: setSubjectPrefix(getSubjectPrefixFromConfig());
0269: setEmailArchiveInSite(isEmailArchiveAddedToSite());
0270:
0271: String reply = getConfigParam("replyto").trim().toLowerCase();
0272: if (reply.equals("") || reply.equals("yes")) {
0273: setReplyToSelected("yes");
0274: } else if (reply.equals("no")) {
0275: setReplyToSelected("no");
0276: } else { // reply to other email
0277: setReplyToSelected("otheremail");
0278: setReplyToOtherEmail(getConfigParam("replyto").trim()
0279: .toLowerCase());
0280: }
0281:
0282: setSendMeCopy(getConfigParam("sendmecopy").trim().toLowerCase()
0283: .equals("yes"));
0284:
0285: setArchiveMessage(getConfigParam("emailarchive").trim()
0286: .toLowerCase().equals("yes"));
0287:
0288: String textformat = getConfigParam("messageformat").trim()
0289: .toLowerCase();
0290: if (textformat.equals("") || textformat.equals("htmltext")) {
0291: setTextFormat("htmltext");
0292: } else {
0293: setTextFormat("plaintext");
0294: }
0295:
0296: log.debug("Constructor");
0297: }
0298:
0299: /** begin: Done Setting Sakai Services * */
0300:
0301: // public void setEmailService(EmailService service) { this.m_emailService =
0302: // service; }
0303: public void setUserDirectoryService(UserDirectoryService service) {
0304: this .m_userDirectoryService = service;
0305: }
0306:
0307: public void setAuthzGroupService(AuthzGroupService service) {
0308: this .m_realmService = service;
0309: }
0310:
0311: // public void setLogger(Logger logger) { this.logger = logger; } // by SK
0312: // 6/30/2006
0313:
0314: /** end: Done Setting Sakai Services * */
0315:
0316: public boolean isGroupviewClicked() {
0317: return groupviewClicked;
0318: }
0319:
0320: public void setGroupviewClicked(boolean groupviewClicked) {
0321: this .groupviewClicked = groupviewClicked;
0322: }
0323:
0324: public boolean isSectionviewClicked() {
0325: return sectionviewClicked;
0326: }
0327:
0328: public void setSectionviewClicked(boolean sectionviewClicked) {
0329: this .sectionviewClicked = sectionviewClicked;
0330: }
0331:
0332: public void toggle_groupviewClicked() {
0333: groupviewClicked = groupviewClicked ? false : true;
0334: sectionviewClicked = false; // exclusive rendering
0335: groupAwareRoleviewClicked = false;
0336: }
0337:
0338: public void toggle_sectionviewClicked() {
0339: sectionviewClicked = sectionviewClicked ? false : true;
0340: groupviewClicked = false; // exclusive rendering
0341: groupAwareRoleviewClicked = false;
0342: }
0343:
0344: public void toggle_groupAwareRoleviewClicked() {
0345: groupAwareRoleviewClicked = groupAwareRoleviewClicked ? false
0346: : true;
0347: sectionviewClicked = false; // exclusive rendering
0348: groupviewClicked = false; // exclusive rendering
0349: }
0350:
0351: public boolean isAllGroupSelected() {
0352: return allGroupSelected;
0353: }
0354:
0355: public void setAllGroupSelected(boolean allGroupSelected) {
0356: this .allGroupSelected = allGroupSelected;
0357: }
0358:
0359: public boolean isAllSectionSelected() {
0360: return allSectionSelected;
0361: }
0362:
0363: public void setAllSectionSelected(boolean allSectionSelected) {
0364: this .allSectionSelected = allSectionSelected;
0365: }
0366:
0367: public int getNum_sections() {
0368: return num_sections;
0369: }
0370:
0371: public void setNum_sections(int num_sections) {
0372: this .num_sections = num_sections;
0373: }
0374:
0375: public int getNum_groups() {
0376: return num_groups;
0377: }
0378:
0379: public void setNum_groups(int num_groups) {
0380: this .num_groups = num_groups;
0381: }
0382:
0383: public void setattachClicked(boolean a) {
0384: this .attachClicked = a;
0385: }
0386:
0387: /**
0388: * @return
0389: * return if attach clicked
0390: */
0391: public boolean getattachClicked() {
0392: return attachClicked;
0393: }
0394:
0395: /**
0396: * read a configuration from registeration file
0397: *
0398: * @param parameter
0399: * @return
0400: * return configuration string which matches parameter
0401: */
0402: protected String getConfigParam(String parameter) {
0403: String p = ToolManager.getCurrentPlacement()
0404: .getPlacementConfig().getProperty(parameter);
0405: if (p == null)
0406: return "";
0407: return p;
0408: }
0409:
0410: protected void setConfigParam(String parameter, String newvalue) {
0411: ToolManager.getCurrentPlacement().getPlacementConfig()
0412: .setProperty(parameter, newvalue);
0413: // ToolManager.getCurrentPlacement().save(); // will be saved in
0414: // processUpdateOptions
0415: }
0416:
0417: protected String getSiteID() {
0418: return (ToolManager.getCurrentPlacement().getContext());
0419: }
0420:
0421: private String getSiteRealmID() {
0422: return ("/site/" + ToolManager.getCurrentPlacement()
0423: .getContext());
0424: }
0425:
0426: protected String getSiteType() {
0427: // String sid=getSiteID();
0428: String type = "";
0429: try {
0430: type = SiteService.getSite(m_siteid).getType();
0431: } catch (Exception e) {
0432: log.debug("Exception: Mailtool.getSiteType(), "
0433: + e.getMessage());
0434: }
0435: return type;
0436: }
0437:
0438: protected String getSiteTitle() {
0439: // String sid=getSiteID();
0440: String title = "";
0441: try {
0442: title = SiteService.getSite(m_siteid).getTitle();
0443: } catch (Exception e) {
0444: log.debug("Exception: Mailtool.getSiteTitle(), "
0445: + e.getMessage());
0446: }
0447: return title;
0448:
0449: }
0450:
0451: public String processGoToOptions() {
0452: return "configure";
0453: }
0454:
0455: public String getfilename() {
0456: return filename;
0457: }
0458:
0459: public void setfilename(String filename) {
0460: this .filename = filename;
0461: }
0462:
0463: public String getRoleID() {
0464: return roleid;
0465: }
0466:
0467: public void setRoleID(String r) {
0468: this .roleid = r;
0469: }
0470:
0471: public int getnum_files() {
0472: return this .num_files;
0473: }
0474:
0475: public void setnum_files(int num_files) {
0476: this .num_files = num_files;
0477: }
0478:
0479: /**
0480: * @return
0481: * return the type of editor
0482: */
0483: public String getEditorType() {
0484: return m_editortype;
0485: }
0486:
0487: /**
0488: * read maximum number number of attachment from mailtool.max.num.attachment in sakai.properties
0489: * @return
0490: * return mailtool.max.num.attachment if set
0491: * Or return 10000 if not set.
0492: */
0493: public int readMaxNumAttachment() {
0494: try {
0495: int maxnumattachment = Integer
0496: .parseInt(ServerConfigurationService
0497: .getString("mailtool.max.num.attachment"));
0498: return maxnumattachment;
0499: } catch (NumberFormatException e) {
0500: log
0501: .debug("Exception: Mailtool Max Num. of attachment is set to 10000, "
0502: + e.getMessage());
0503: return 10000; // Actually this means "unlimited if not set or
0504: // invalid"
0505: }
0506: }
0507:
0508: public int getMaxNumAttachment() {
0509: return MaxNumAttachment;
0510: }
0511:
0512: public void setMaxNumAttachment(int m) {
0513: this .MaxNumAttachment = m;
0514: }
0515:
0516: public String getUploadDirectory() {
0517: String ud = ServerConfigurationService
0518: .getString("mailtool.upload.directory");
0519: if (ud != "" && ud != null) {
0520: File dir = new File(ud);
0521: if (dir.isDirectory())
0522: return ud;
0523: }
0524: return uploaddirectoryDefault;
0525: }
0526:
0527: /**
0528: * check if mailtool.show.renaming.role=yes|true in sakai.properties
0529: * @return
0530: * return true if mailtool.show.renaming.role=yes|true
0531: */
0532: public boolean isShowRenamingRoles() {
0533: String rename = ServerConfigurationService
0534: .getString("mailtool.show.renaming.roles");
0535: if (rename != "" && rename != null) {
0536: return (rename.trim().toLowerCase().equals("yes")
0537: || rename.trim().toLowerCase().equals("true") ? true
0538: : false);
0539: }
0540: return false;
0541: }
0542:
0543: public void setEditorType(String editor) {
0544: m_editortype = editor;
0545: }
0546:
0547: public String getOtherEmails() {
0548: return m_otheremails;
0549: }
0550:
0551: public void setOtherEmails(String otheremails) {
0552: m_otheremails = otheremails;
0553: }
0554:
0555: public String getReplyToOtherEmail() {
0556: return m_replytootheremail;
0557: }
0558:
0559: public void setReplyToOtherEmail(String email) {
0560: m_replytootheremail = email;
0561: }
0562:
0563: public String getMessageSubject() {
0564: return m_subject;
0565: }
0566:
0567: public void setMessageSubject(String subject) {
0568: m_subject = subject;
0569: }
0570:
0571: public String getSubjectPrefix() {
0572: return m_subjectprefix;
0573: }
0574:
0575: public void setSubjectPrefix(String prefix) {
0576: m_subjectprefix = prefix;
0577: }
0578:
0579: public String getMessageBody() {
0580: return m_body;
0581: }
0582:
0583: public void setMessageBody(String body) {
0584: m_body = body;
0585: }
0586:
0587: public String getResults() {
0588: return m_results;
0589: }
0590:
0591: /**
0592: * set setectortype when needed.
0593: */
0594: protected void setSelectorType() {
0595: String type = getRecipview();
0596: if (type.equals("") || type == null)
0597: type = recipviewDefault;
0598:
0599: m_selectByRole = false;
0600: m_selectByUser = false;
0601: m_selectByTree = false;
0602: m_selectSideBySide = false;
0603: m_selectByFoothill = false;
0604:
0605: if (type.equals("role")) {
0606: m_selectByRole = true;
0607: m_recipJSPfrag = "selectByRole.jsp";
0608: } else if (type.equals("user")) {
0609: m_selectByUser = true;
0610: m_recipJSPfrag = "selectByUser.jsp";
0611: } else if (type.equals("sidebyside")) {
0612: m_selectSideBySide = true;
0613: m_recipJSPfrag = "selectSideBySide.jsp";
0614: } else if (type.equals("foothill")) {
0615: m_selectByFoothill = true;
0616: m_recipJSPfrag = "selectByFoothill.jsp";
0617: } else if (type.equals("tree")) {
0618: m_selectByTree = true;
0619: m_recipJSPfrag = "selectByTree.jsp";
0620: }
0621: }
0622:
0623: public boolean isSelectByRole() {
0624: setSelectorType();
0625: return m_selectByRole;
0626: }
0627:
0628: public boolean isSelectByUser() {
0629: setSelectorType();
0630: return m_selectByUser;
0631: }
0632:
0633: public boolean isSelectByTree() {
0634: setSelectorType();
0635: return m_selectByTree;
0636: }
0637:
0638: public boolean isSelectSideBySide() {
0639: setSelectorType();
0640: return m_selectSideBySide;
0641: }
0642:
0643: public boolean isSelectByFoothill() {
0644: setSelectorType();
0645: return m_selectByFoothill;
0646: }
0647:
0648: /**
0649: * initialize compose page. it will remove all previous editing
0650: * @return
0651: * "cancel" for navigating to compse page
0652: */
0653: public String processCancelEmail() {
0654:
0655: this .m_recipientSelector = null;
0656: this .m_subject = getSubjectPrefix().equals("") ? getSubjectPrefixFromConfig()
0657: : getSubjectPrefix();
0658: m_otheremails = "";
0659: this .m_body = "";
0660: num_files = 0;
0661: attachedFiles.clear();
0662: // m_buildNewView = true;
0663: m_recipientSelector = null;
0664: m_recipientSelector1 = null;
0665: m_recipientSelector2 = null;
0666: m_recipientSelector3 = null;
0667: setAllUsersSelected(false);
0668: setAllGroupSelected(false);
0669: setAllSectionSelected(false);
0670:
0671: return "cancel";
0672: }
0673:
0674: /**
0675: * Refactored mail-sending function (not using Sakai EmailService)
0676: * @return
0677: * return "results" for navigating to results page
0678: */
0679: public String processSendEmail() {
0680: /* EmailUser */selected = m_recipientSelector
0681: .getSelectedUsers();
0682: if (m_selectByTree) {
0683: selectedGroupAwareRoleUsers = m_recipientSelector1
0684: .getSelectedUsers();
0685: selectedGroupUsers = m_recipientSelector2
0686: .getSelectedUsers();
0687: selectedSectionUsers = m_recipientSelector3
0688: .getSelectedUsers();
0689:
0690: selected.addAll(selectedGroupAwareRoleUsers);
0691: selected.addAll(selectedGroupUsers);
0692: selected.addAll(selectedSectionUsers);
0693: }
0694: // Put everyone in a set so the same person doesn't get multiple emails.
0695: Set emailusers = new TreeSet();
0696: if (isAllUsersSelected()) { // the button for this is inactivated ...
0697: // leave for future
0698: for (Iterator i = getEmailGroups().iterator(); i.hasNext();) {
0699: EmailGroup group = (EmailGroup) i.next();
0700: emailusers.addAll(group.getEmailusers());
0701: }
0702: }
0703: if (isAllGroupSelected()) {
0704: for (Iterator i = getEmailGroupsByType("section")
0705: .iterator(); i.hasNext();) {
0706: EmailGroup group = (EmailGroup) i.next();
0707: if (group.getEmailrole().roletype.equals("section")) {
0708: selected.addAll(group.getEmailusers());
0709: }
0710: }
0711: }
0712: if (isAllSectionSelected()) {
0713: for (Iterator i = getEmailGroupsByType("group").iterator(); i
0714: .hasNext();) {
0715: EmailGroup group = (EmailGroup) i.next();
0716: if (group.getEmailrole().roletype.equals("group")) {
0717: selected.addAll(group.getEmailusers());
0718: }
0719: }
0720: }
0721: if (isAllGroupAwareRoleSelected()) {
0722: for (Iterator i = getEmailGroupsByType("role_groupaware")
0723: .iterator(); i.hasNext();) {
0724: EmailGroup group = (EmailGroup) i.next();
0725: if (group.getEmailrole().roletype
0726: .equals("role_groupaware")) {
0727: selected.addAll(group.getEmailusers());
0728: }
0729: }
0730: }
0731: emailusers = new TreeSet(selected); // convert List to Set (remove
0732: // duplicates)
0733:
0734: m_subjectprefix = getSubjectPrefixFromConfig();
0735:
0736: EmailUser curUser = getCurrentUser();
0737:
0738: String fromEmail = "";
0739: String fromDisplay = "";
0740: if (curUser != null) {
0741: fromEmail = curUser.getEmail();
0742: fromDisplay = curUser.getDisplayname();
0743: }
0744: String fromString = fromDisplay + " <" + fromEmail + ">";
0745:
0746: m_results = "Message sent to: <br>";
0747:
0748: String subject = m_subject;
0749:
0750: // Should we append this to the archive?
0751: String emailarchive = "/mailarchive/channel/" + m_siteid
0752: + "/main";
0753: if (m_archiveMessage && isEmailArchiveInSite()) {
0754: String attachment_info = "<br/>";
0755: Attachment a = null;
0756: Iterator iter = attachedFiles.iterator();
0757: int i = 0;
0758: while (iter.hasNext()) {
0759: a = (Attachment) iter.next();
0760: attachment_info += "<br/>";
0761: attachment_info += "Attachment #" + (i + 1) + ": "
0762: + a.getFilename() + "(" + a.getSize()
0763: + " Bytes)";
0764: i++;
0765: }
0766: this .appendToArchive(emailarchive, fromString, subject,
0767: m_body + attachment_info);
0768: }
0769: List headers = new ArrayList();
0770: if (getTextFormat().equals("htmltext"))
0771: headers.add("content-type: text/html");
0772: else
0773: headers.add("content-type: text/plain");
0774:
0775: String smtp_server = ServerConfigurationService
0776: .getString("smtp@org.sakaiproject.email.api.EmailService");
0777: // String smtp_port = ServerConfigurationService.getString("smtp.port");
0778: try {
0779: Properties props = new Properties();
0780: props.put("mail.smtp.host", smtp_server);
0781: // props.put("mail.smtp.port", smtp_port);
0782: Session s = Session.getInstance(props, null);
0783:
0784: MimeMessage message = new MimeMessage(s);
0785:
0786: InternetAddress from = new InternetAddress(fromString);
0787: message.setFrom(from);
0788: String reply = getReplyToSelected().trim().toLowerCase();
0789: if (reply.equals("yes")) {
0790: // "reply to sender" is default. So do nothing
0791: } else if (reply.equals("no")) {
0792: String noreply = getSiteTitle() + " <noreply@"
0793: + smtp_server + ">";
0794: InternetAddress noreplyemail = new InternetAddress(
0795: noreply);
0796: message.setFrom(noreplyemail);
0797: } else if (reply.equals("otheremail")
0798: && getReplyToOtherEmail().equals("") != true) {
0799: // need input(email) validation
0800: InternetAddress replytoList[] = { new InternetAddress(
0801: getConfigParam("replyto").trim()) };
0802: message.setReplyTo(replytoList);
0803: }
0804: message.setSubject(subject);
0805: String text = m_body;
0806: String attachmentdirectory = getUploadDirectory();
0807:
0808: // Create the message part
0809: BodyPart messageBodyPart = new MimeBodyPart();
0810:
0811: // Fill the message
0812: String messagetype = "";
0813:
0814: if (getTextFormat().equals("htmltext")) {
0815: messagetype = "text/html";
0816: } else {
0817: messagetype = "text/plain";
0818: }
0819: messageBodyPart.setContent(text, messagetype);
0820: Multipart multipart = new MimeMultipart();
0821: multipart.addBodyPart(messageBodyPart);
0822:
0823: // Part two is attachment
0824: Attachment a = null;
0825: Iterator iter = attachedFiles.iterator();
0826: while (iter.hasNext()) {
0827: a = (Attachment) iter.next();
0828: messageBodyPart = new MimeBodyPart();
0829: DataSource source = new FileDataSource(
0830: attachmentdirectory
0831: + this .getCurrentUser().getUserid()
0832: + "-" + a.getFilename());
0833: messageBodyPart.setDataHandler(new DataHandler(source));
0834: messageBodyPart.setFileName(a.getFilename());
0835: multipart.addBodyPart(messageBodyPart);
0836: }
0837: message.setContent(multipart);
0838:
0839: // Send the emails
0840: String recipientsString = "";
0841: for (Iterator i = emailusers.iterator(); i.hasNext(); recipientsString += ",") {
0842: EmailUser euser = (EmailUser) i.next();
0843: String toEmail = euser.getEmail(); // u.getEmail();
0844: String toDisplay = euser.getDisplayname(); // u.getDisplayName();
0845: // if AllUsers are selected, do not add current user's email to
0846: // recipients
0847: if (isAllUsersSelected()
0848: && getCurrentUser().getEmail().equals(toEmail)) {
0849: // don't add sender to recipients
0850: } else {
0851: recipientsString += toEmail;
0852: m_results += toDisplay
0853: + (i.hasNext() ? "<br/>" : "");
0854: }
0855: // InternetAddress to[] = {new InternetAddress(toEmail) };
0856: // Transport.send(message,to);
0857: }
0858: if (m_otheremails.trim().equals("") != true) {
0859: //
0860: // multiple email validation is needed here
0861: //
0862: String refinedOtherEmailAddresses = m_otheremails
0863: .trim().replace(';', ',');
0864: recipientsString += refinedOtherEmailAddresses;
0865: m_results += "<br/>" + refinedOtherEmailAddresses;
0866: // InternetAddress to[] = {new
0867: // InternetAddress(refinedOtherEmailAddresses) };
0868: // Transport.send(message, to);
0869: }
0870: if (m_sendmecopy) {
0871: message.addRecipients(Message.RecipientType.CC,
0872: fromEmail);
0873: // trying to solve SAK-7410
0874: // recipientsString+=fromEmail;
0875: // InternetAddress to[] = {new InternetAddress(fromEmail) };
0876: // Transport.send(message, to);
0877: }
0878: // message.addRecipients(Message.RecipientType.TO,
0879: // recipientsString);
0880: message.addRecipients(Message.RecipientType.BCC,
0881: recipientsString);
0882:
0883: Transport.send(message);
0884: } catch (Exception e) {
0885: log
0886: .debug("Mailtool Exception while trying to send the email: "
0887: + e.getMessage());
0888: }
0889:
0890: // Clear the Subject and Body of the Message
0891: m_subject = getSubjectPrefix().equals("") ? getSubjectPrefixFromConfig()
0892: : getSubjectPrefix();
0893: m_otheremails = "";
0894: m_body = "";
0895: num_files = 0;
0896: attachedFiles.clear();
0897: m_recipientSelector = null;
0898: m_recipientSelector1 = null;
0899: m_recipientSelector2 = null;
0900: m_recipientSelector3 = null;
0901: setAllUsersSelected(false);
0902: setAllGroupSelected(false);
0903: setAllSectionSelected(false);
0904:
0905: // Display Users with Bad Emails if the option is turned on.
0906: boolean showBadEmails = getDisplayInvalidEmailAddr();
0907: if (showBadEmails == true) {
0908: m_results += "<br/><br/>";
0909:
0910: List /* String */badnames = new ArrayList();
0911:
0912: for (Iterator i = selected.iterator(); i.hasNext();) {
0913: EmailUser user = (EmailUser) i.next();
0914: /* This check should maybe be some sort of regular expression */
0915: if (user.getEmail().equals("")) {
0916: badnames.add(user.getDisplayname());
0917: }
0918: }
0919: if (badnames.size() > 0) {
0920: m_results += "The following users do not have valid email addresses:<br/>";
0921: for (Iterator i = badnames.iterator(); i.hasNext();) {
0922: String name = (String) i.next();
0923: if (i.hasNext() == true)
0924: m_results += name + "/ ";
0925: else
0926: m_results += name;
0927: }
0928: }
0929: }
0930: return "results";
0931: }
0932:
0933: public RecipientSelector getRecipientSelector() {
0934: getRecipientSelectors();
0935:
0936: return m_recipientSelector;
0937: }
0938:
0939: public RecipientSelector getRecipientSelector_GroupAwareRole() {
0940: getRecipientSelectors();
0941:
0942: return m_recipientSelector1;
0943: }
0944:
0945: public RecipientSelector getRecipientSelector_Group() {
0946: getRecipientSelectors();
0947:
0948: return m_recipientSelector2;
0949: }
0950:
0951: public RecipientSelector getRecipientSelector_Section() {
0952: getRecipientSelectors();
0953:
0954: return m_recipientSelector3;
0955: }
0956:
0957: /**
0958: * By the selected type of recipient view, initialize/populate recipient selector(s)
0959: */
0960: public void getRecipientSelectors() {
0961: if (m_recipientSelector == null) {
0962: if (m_selectByUser == true) {
0963: m_recipientSelector = new UserSelector();
0964: } else if (m_selectByTree == true) {
0965: m_recipientSelector = new TreeSelector();
0966: m_recipientSelector1 = new TreeSelector();
0967: m_recipientSelector2 = new TreeSelector(); // groups
0968: m_recipientSelector3 = new TreeSelector(); // sections
0969: } else if (m_selectSideBySide == true) {
0970: m_recipientSelector = new SideBySideSelector();
0971: } else if (m_selectByFoothill == true) {
0972: m_recipientSelector = new FoothillSelector();
0973: }
0974:
0975: if (m_selectByTree == true) {
0976: List emailGroups1 = getEmailGroupsByType("role");
0977: List emailGroups1_1 = getEmailGroupsByType("role_groupaware");
0978: List emailGroups2 = getEmailGroupsByType("group");
0979: List emailGroups3 = getEmailGroupsByType("section");
0980: m_recipientSelector.populate(emailGroups1);
0981: m_recipientSelector1.populate(emailGroups1_1);
0982: m_recipientSelector2.populate(emailGroups2);
0983: m_recipientSelector3.populate(emailGroups3);
0984: } else {
0985: List emailGroups = getEmailGroups();
0986: m_recipientSelector.populate(emailGroups);
0987: }
0988: }
0989: }
0990:
0991: // Get Information from the Tool Config
0992: public String getSubjectPrefixFromConfig() {
0993: String prefix = this .getConfigParam("subjectprefix");
0994: if (prefix == null || prefix == "") {
0995: String titleDefault = getSiteTitle() + ": ";
0996: return titleDefault;
0997: } else
0998: return prefix;
0999: }
1000:
1001: /**
1002: * // Get Information from the Tool Config
1003: * @return
1004: * return the string of "recipview=" in sakai.mailtool.xml
1005: */
1006: public String getRecipview() {
1007: String recipview = this .getConfigParam("recipview");
1008: if (recipview == null || recipview.trim().equals(""))
1009: return recipviewDefault;
1010: else
1011: return recipview;
1012: }
1013:
1014: /**
1015: * // OOTB(Out of the box) Sakai defaults
1016: * @return
1017: * return default group-aware role by type
1018: * if type=course, return Student.
1019: * if type=project, return access.
1020: */
1021: public String getGroupAwareRoleDefault() {
1022: if (getSiteType().equals("course"))
1023: return "Student";
1024: if (getSiteType().equals("project"))
1025: return "access";
1026: return "";
1027: }
1028:
1029: /**
1030: * Get group-aware role which is set in sakai.properties
1031: * e.g. "mailtool.group.aware.role=Student,access"
1032: *
1033: * @return
1034: * return the String of group-aware role name
1035: */
1036: public String getGroupAwareRole() {
1037: String gar = ServerConfigurationService
1038: .getString("mailtool.group.aware.role");
1039: String[] gartokens = gar.split(",");
1040: try {
1041: arole = m_realmService.getAuthzGroup(m_realmid);
1042: } catch (Exception e) {
1043: log.debug("Exception: Mailtool.getEmailRoles(), "
1044: + e.getMessage());
1045: }
1046: for (Iterator i = arole.getRoles().iterator(); i.hasNext();) {
1047: Role r = (Role) i.next();
1048: String rolename = r.getId();
1049: for (int t = 0; t < gartokens.length; t++) {
1050: if (gartokens[t].trim().equals(rolename.trim()))
1051: return rolename;
1052: }
1053: }
1054: return groupAwareRoleDefault;
1055: }
1056:
1057: /**
1058: * check if role is listed as group-aware role in mailtool.group.aware.role (in sakai.properties)
1059: * @param role
1060: * @return
1061: * return true if role is listed in mailtool.group.aware.role
1062: */
1063: public boolean isGroupAwareRoleInSettings(String role) {
1064: String gar = ServerConfigurationService
1065: .getString("mailtool.group.aware.role");
1066: String[] gartokens = gar.split(",");
1067:
1068: for (int i = 0; i < gartokens.length; i++) {
1069: if (gartokens[i].trim().equals(role.trim()))
1070: return true;
1071: }
1072: return false;
1073: }
1074:
1075: /***************************************************************************
1076: * ** it's user-permission-based checking
1077: *
1078: * public boolean isAllowedToConfigure() {
1079: *
1080: * String siteid="/site/"+getSiteID(); //return
1081: * m_realmService.unlock(this.getCurrentUser().getUserid(), "mail.new",
1082: * siteid); return
1083: * m_realmService.isAllowed(this.getCurrentUser().getUserid(),
1084: * "mailtool.admin", siteid);
1085: * }
1086: **************************************************************************/
1087: // role-based permission checking ... modified thanks to Seth at Columbia
1088: // Jan 3 2007
1089: //
1090: public boolean isAllowedToSend() {
1091: String mySendRole = m_realmService.getUserRole(this
1092: .getCurrentUser().getUserid(), getSiteRealmID());
1093: return hasPermissionForRole(mySendRole, "mailtool.send");
1094: }
1095:
1096: // role-based permission
1097: public boolean isAllowedToConfigure() {
1098: String myConfigRole = m_realmService.getUserRole(this
1099: .getCurrentUser().getUserid(), getSiteRealmID());
1100: return hasPermissionForRole(myConfigRole, "mailtool.admin");
1101: }
1102:
1103: public boolean isAllowedToArchiveMessage() {
1104: String myConfigRole = m_realmService.getUserRole(this
1105: .getCurrentUser().getUserid(), getSiteRealmID());
1106: return hasPermissionForRole(myConfigRole, "mail.new");
1107: }
1108:
1109: /**
1110: * explicitly add the permissions for this role in !site.helper with the
1111: * following
1112: * @param role
1113: * @param permission
1114: * @return
1115: * return true if role has permission
1116: */
1117: private boolean hasPermissionForRole(String role, String permission) {
1118: Collection realmList = new ArrayList();
1119: realmList.add(getSiteRealmID());
1120: AuthzGroup authzGroup = null;
1121: try {
1122: authzGroup = m_realmService.getAuthzGroup("!site.helper");
1123: } catch (Exception e) {
1124: log.info("No site helper template found");
1125: }
1126: if (authzGroup != null) {
1127: realmList.add(authzGroup.getId());
1128: }
1129: Set allowedFunctions = m_realmService.getAllowedFunctions(role,
1130: realmList);
1131: return allowedFunctions.contains(permission);
1132: }
1133:
1134: /**
1135: * check if fckeditor is set for text editing
1136: * @return
1137: * return if fckeditor is set for the text editing
1138: */
1139: public boolean isFCKeditor() {
1140: String editortype = this .getConfigParam("wysiwygeditor");
1141: if (editortype.equals("") || editortype == null) {
1142: editortype = ServerConfigurationService
1143: .getString("wysiwyg.editor");
1144: if (editortype == null)
1145: return false;
1146:
1147: if (editortype.equals(""))
1148: return false;
1149:
1150: if (editortype.equalsIgnoreCase("fckeditor"))
1151: return true;
1152:
1153: return false;
1154: } else if (editortype.equalsIgnoreCase("fckeditor"))
1155: return true;
1156:
1157: return false;
1158: }
1159:
1160: /**
1161: * check if htmlarea is set for text editing
1162: * @return
1163: * return true if htmlarea is set for text editing
1164: */
1165: public boolean isHTMLArea() {
1166: String editortype = this .getConfigParam("wysiwygeditor");
1167: if (editortype.equals("") || editortype == null) {
1168: editortype = ServerConfigurationService
1169: .getString("wysiwyg.editor");
1170: if (editortype == null)
1171: return false;
1172:
1173: if (editortype.equals(""))
1174: return false;
1175:
1176: if (editortype.equalsIgnoreCase("htmlarea"))
1177: return true;
1178:
1179: return false;
1180: } else if (editortype.equalsIgnoreCase("htmlarea"))
1181: return true;
1182:
1183: return false;
1184: }
1185:
1186: /**
1187: * check if DisplayInvalidEmailAddr set in sakai.mailtool.xml
1188: * @return
1189: * return true if "displayinvalidemailaddrs=yes" in Tool Config(registration) file
1190: */
1191: public boolean getDisplayInvalidEmailAddr() {
1192: String invalid = this
1193: .getConfigParam("displayinvalidemailaddrs");
1194: return (invalid == null ? false : (invalid.trim().toLowerCase()
1195: .equals("yes") ? true : false));
1196: }
1197:
1198: /**
1199: * Read the tool config and build the email roles that are specified
1200: * @return
1201: * return EmailRoles (called from getEmailGroups())
1202: */
1203: public List /* EmailRole */getEmailRoles() {
1204: List /* EmailRole */theroles = new ArrayList();
1205: List allgroups = new ArrayList();
1206: List allsections = new ArrayList();
1207: for (int i = 1; i < (NUMBER_ROLES + 1); i++) {
1208: String rolerealm = this .getConfigParam("role" + i
1209: + "realmid");
1210: String rolename = this .getConfigParam("role" + i + "id");
1211: String rolesingular = this .getConfigParam("role" + i
1212: + "singular");
1213: String roleplural = this .getConfigParam("role" + i
1214: + "plural");
1215: if ((rolerealm != null && rolerealm != "")
1216: && (rolename != null && rolename != "")
1217: && (rolesingular != null && rolesingular != "")
1218: && (roleplural != null && roleplural != "")) {
1219: EmailRole emailrole = null;
1220:
1221: // if (isGroupAwareRoleInSettings(rolename)){
1222: if (getGroupAwareRole().equals(rolename)) {
1223: emailrole = new EmailRole(rolerealm, rolename,
1224: rolesingular, roleplural, "role_groupaware");
1225: num_groupawarerole++;
1226: } else {
1227: emailrole = new EmailRole(rolerealm, rolename,
1228: rolesingular, roleplural, "role");
1229: }
1230: theroles.add(emailrole);
1231: already_configured = true;
1232: }
1233: } // for
1234: if (already_configured == false) {
1235: try {
1236: arole = m_realmService.getAuthzGroup(m_realmid);
1237: } catch (Exception e) {
1238: log.debug("Exception: Mailtool.getEmailRoles(), "
1239: + e.getMessage());
1240: }
1241: for (Iterator i = arole.getRoles().iterator(); i.hasNext();) {
1242: Role r = (Role) i.next();
1243: String rolename = r.getId();
1244: String singular = "";
1245: String plural = "";
1246: if (rolename.equals("maintain")) {
1247: singular = rolename;
1248: plural = rolename + "ers";
1249: } else if (rolename.equals("access")) {
1250: singular = rolename;
1251: plural = rolename + " users";
1252: } else {
1253: singular = rolename;
1254: plural = rolename + "s";
1255: }
1256: EmailRole emailrole = null;
1257: if (getGroupAwareRole().equals(rolename)) {
1258: emailrole = new EmailRole("/site/" + m_siteid,
1259: rolename, singular, plural,
1260: "role_groupaware");
1261: num_groupawarerole++;
1262: } else
1263: emailrole = new EmailRole("/site/" + m_siteid,
1264: rolename, singular, plural, "role");
1265: theroles.add(emailrole);
1266: }
1267: }
1268: // adding groups as roles
1269: try {
1270: currentSite = siteService.getSite(m_siteid);
1271: } catch (Exception e) {
1272: log.debug("Exception: Mailtool.getEmailRoles(): , "
1273: + e.getMessage());
1274: }
1275: Collection groups = currentSite.getGroups();
1276: for (Iterator groupIterator = groups.iterator(); groupIterator
1277: .hasNext();) {
1278: Group currentGroup = (Group) groupIterator.next();
1279: String groupname = currentGroup.getTitle();
1280: String groupid = currentGroup.getProviderGroupId();
1281: EmailRole emailrole2 = null;
1282: if (currentGroup.getProperties().getProperty(
1283: "sections_category") != null) {
1284: emailrole2 = new EmailRole(groupid, groupname,
1285: groupname, groupname, "section");
1286: allsections.add(emailrole2);
1287: num_sections++;
1288: } else {
1289: emailrole2 = new EmailRole(groupid, groupname,
1290: groupname, groupname, "group");
1291: allgroups.add(emailrole2);
1292: num_groups++;
1293: }
1294: }
1295: theroles.addAll(allgroups); // for sorted list in side-by-side view &
1296: // scrolling list view
1297: theroles.addAll(allsections); // for sorted list ...
1298:
1299: return theroles;
1300: }
1301:
1302: /**
1303: * Check if there is a group-aware role in the site
1304: */
1305: public void checkifGroupAwareRoleExist() {
1306: String realmid = getSiteRealmID();
1307: try {
1308: arole = m_realmService.getAuthzGroup(realmid);
1309: } catch (Exception e) {
1310: log.debug("Exception: Mailtool.initializeCurrentRoles(), "
1311: + e.getMessage());
1312: }
1313: for (Iterator i = arole.getRoles().iterator(); i.hasNext();) {
1314: Role r = (Role) i.next();
1315: String rolename = r.getId();
1316: if (isGroupAwareRoleInSettings(rolename)) {
1317: setGroupAwareRoleExist(true);
1318: break;
1319: } else if (getGroupAwareRole().equals(rolename)) {
1320: setGroupAwareRoleExist(true);
1321: break;
1322: }
1323: }
1324: }
1325:
1326: /**
1327: * Using SiteService & SITE.getTools(), check if Email Archive is among the tools in the site
1328: * @return
1329: * return true if Email Archive is added to the site
1330: */
1331: public boolean isEmailArchiveAddedToSite() {
1332: boolean hasEmailArchive = false;
1333: String toolid = "sakai.mailbox";
1334: try {
1335: Site site = SiteService.getSite(m_siteid);
1336: Collection toolsInSite = site.getTools(toolid);
1337: if (!toolsInSite.isEmpty()) {
1338: hasEmailArchive = true;
1339: }
1340: } catch (Exception e) {
1341: log
1342: .debug("Exception: Mailtool.isEmailArchiveAddedToSite(), "
1343: + e.getMessage());
1344: }
1345: return hasEmailArchive;
1346: }
1347:
1348: public boolean isEmailArchived() {
1349: String emailarchive = this .getConfigParam("emailarchive");
1350: if (emailarchive == null)
1351: return false;
1352: if (emailarchive.equals(""))
1353: return false;
1354:
1355: return true;
1356: }
1357:
1358: public String getTextFormat() {
1359: return m_textformat;
1360: }
1361:
1362: public void setTextFormat(String format) {
1363: m_textformat = format;
1364: }
1365:
1366: public void setDoNotReply(boolean value) {
1367: m_donotreply = value;
1368: }
1369:
1370: public boolean isArchiveMessage() {
1371: return m_archiveMessage;
1372: }
1373:
1374: public void setArchiveMessage(boolean value) {
1375: m_archiveMessage = value;
1376: }
1377:
1378: public boolean isSendMeCopy() {
1379: return m_sendmecopy;
1380: }
1381:
1382: public void setSendMeCopy(boolean value) {
1383: m_sendmecopy = value;
1384: }
1385:
1386: public boolean isAllUsersSelected() {
1387: return m_allusers;
1388: }
1389:
1390: public void setAllUsersSelected(boolean value) {
1391: m_allusers = value;
1392: }
1393:
1394: /**
1395: * Build all groups that will be used for this
1396: *
1397: * @return
1398: * return EmailGroups in the site
1399: */
1400: public List /* EmailGroup */getEmailGroups() {
1401: List /* EmailGroup */thegroups = new ArrayList();
1402:
1403: List emailroles = this .getEmailRoles();
1404:
1405: for (Iterator i = emailroles.iterator(); i.hasNext();) {
1406: EmailRole emailrole = (EmailRole) i.next();
1407:
1408: if (emailrole.roletype.equals("role")) {
1409: String realmid = emailrole.getRealmid();
1410:
1411: AuthzGroup therealm = null;
1412: try {
1413: therealm = m_realmService.getAuthzGroup(realmid);
1414: } catch (Exception e) {
1415: log
1416: .debug("Exception: Mailtool.getEmailGroups() #1, "
1417: + e.getMessage());
1418: }
1419: Set users = therealm.getUsersHasRole(emailrole
1420: .getRoleid());
1421: List /* EmailUser */mailusers = new ArrayList();
1422: for (Iterator j = users.iterator(); j.hasNext();) {
1423: String userid = (String) j.next();
1424: try {
1425: User theuser = m_userDirectoryService
1426: .getUser(userid);
1427: String firstname_for_display = "";
1428: String lastname_for_display = "";
1429: if (theuser.getFirstName().trim().equals("")) {
1430: if (theuser.getEmail().trim().equals("")
1431: && theuser.getLastName().trim()
1432: .equals(""))
1433: firstname_for_display = theuser
1434: .getDisplayId(); // fix
1435: // for
1436: // SAK-7539
1437: else
1438: firstname_for_display = theuser
1439: .getEmail(); // fix
1440: // for
1441: // SAK-7356
1442: } else {
1443: firstname_for_display = theuser
1444: .getFirstName();
1445: }
1446: lastname_for_display = theuser.getLastName();
1447: EmailUser emailuser = new EmailUser(theuser
1448: .getId(), firstname_for_display,
1449: lastname_for_display, theuser
1450: .getEmail());
1451: mailusers.add(emailuser);
1452: } catch (Exception e) {
1453: log
1454: .debug("Exception: Mailtool.getEmailGroups() #2, "
1455: + e.getMessage());
1456: }
1457: }
1458: Collections.sort(mailusers);
1459: EmailGroup thegroup = new EmailGroup(emailrole,
1460: mailusers);
1461: thegroups.add(thegroup);
1462: } else if (emailrole.roletype.equals("group")) {
1463: String sid = getSiteID();
1464: Site currentSite = null;
1465: try {
1466: currentSite = siteService.getSite(sid);
1467: } catch (Exception e) {
1468: log
1469: .debug("Exception: Mailtool.getEmailGroups() #3, "
1470: + e.getMessage());
1471: }
1472: Collection groups = currentSite.getGroups();
1473: Group agroup = null;
1474: for (Iterator groupIterator = groups.iterator(); groupIterator
1475: .hasNext();) {
1476: agroup = (Group) groupIterator.next();
1477: String groupname = agroup.getTitle();
1478: if (emailrole.getRoleid().equals(groupname))
1479: break;
1480: }
1481: Set users2 = agroup.getUsers();
1482: List mailusers2 = new ArrayList();
1483: for (Iterator k = users2.iterator(); k.hasNext();) {
1484: String userid2 = (String) k.next();
1485: try {
1486: User theuser2 = m_userDirectoryService
1487: .getUser(userid2);
1488: String firstname_for_display = "";
1489: String lastname_for_display = "";
1490: if (theuser2.getFirstName().trim().equals("")) {
1491: if (theuser2.getEmail().trim().equals("")
1492: && theuser2.getLastName().trim()
1493: .equals(""))
1494: firstname_for_display = theuser2
1495: .getDisplayId(); // fix
1496: // for
1497: // SAK-7539
1498: else
1499: firstname_for_display = theuser2
1500: .getEmail(); // fix
1501: // for
1502: // SAK-7356
1503: } else {
1504: firstname_for_display = theuser2
1505: .getFirstName();
1506: }
1507:
1508: lastname_for_display = theuser2.getLastName();
1509: EmailUser emailuser2 = new EmailUser(theuser2
1510: .getId(), firstname_for_display,
1511: lastname_for_display, theuser2
1512: .getEmail());
1513: mailusers2.add(emailuser2);
1514: } catch (Exception e) {
1515: log
1516: .debug("Exception: Mailtool.getEmailGroups() #3-1, "
1517: + e.getMessage());
1518: }
1519: }
1520: Collections.sort(mailusers2);
1521: EmailGroup thegroup2 = new EmailGroup(emailrole,
1522: mailusers2);
1523: thegroups.add(thegroup2);
1524: } // else
1525: else if (emailrole.roletype.equals("section")) {
1526: String sid = getSiteID();
1527: Site currentSite = null;
1528: try {
1529: currentSite = siteService.getSite(sid);
1530: } catch (Exception e) {
1531: log
1532: .debug("Exception: Mailtool.getEmailGroups() #4, "
1533: + e.getMessage());
1534: }
1535: Collection groups = currentSite.getGroups();
1536: Group agroup = null;
1537: for (Iterator groupIterator = groups.iterator(); groupIterator
1538: .hasNext();) {
1539: agroup = (Group) groupIterator.next();
1540: String groupname = agroup.getTitle();
1541: if (emailrole.getRoleid().equals(groupname))
1542: break;
1543: }
1544: Set users2 = agroup.getUsers();
1545: List mailusers2 = new ArrayList();
1546: for (Iterator k = users2.iterator(); k.hasNext();) {
1547: String userid2 = (String) k.next();
1548: try {
1549: User theuser2 = m_userDirectoryService
1550: .getUser(userid2);
1551: String firstname_for_display = "";
1552: String lastname_for_display = "";
1553: if (theuser2.getFirstName().trim().equals("")) {
1554: if (theuser2.getEmail().trim().equals("")
1555: && theuser2.getLastName().trim()
1556: .equals(""))
1557: firstname_for_display = theuser2
1558: .getDisplayId(); // fix
1559: // for
1560: // SAK-7539
1561: else
1562: firstname_for_display = theuser2
1563: .getEmail(); // fix
1564: // for
1565: // SAK-7356
1566: } else {
1567: firstname_for_display = theuser2
1568: .getFirstName();
1569: }
1570:
1571: lastname_for_display = theuser2.getLastName();
1572:
1573: EmailUser emailuser2 = new EmailUser(theuser2
1574: .getId(), firstname_for_display,
1575: lastname_for_display, theuser2
1576: .getEmail());
1577:
1578: mailusers2.add(emailuser2);
1579: } catch (Exception e) {
1580: log
1581: .debug("Exception: Mailtool.getEmailGroups() #4-1, "
1582: + e.getMessage());
1583: }
1584: }
1585: Collections.sort(mailusers2);
1586: EmailGroup thegroup2 = new EmailGroup(emailrole,
1587: mailusers2);
1588: thegroups.add(thegroup2);
1589: } // else
1590: }
1591: return thegroups;
1592: }
1593:
1594: /**
1595: * Build all groups that will be used for this
1596: * @param roletypefilter
1597: * @return
1598: * return EmailGroup by Type
1599: */
1600: public List /* EmailGroup */getEmailGroupsByType(
1601: String roletypefilter) {
1602: List /* EmailGroup */thegroups = new ArrayList();
1603:
1604: List emailroles = this .getEmailRoles();
1605:
1606: for (Iterator i = emailroles.iterator(); i.hasNext();) {
1607: EmailRole emailrole = (EmailRole) i.next();
1608: if (emailrole.roletype.equals("role")
1609: && roletypefilter.equals("role")) {
1610: String realmid = emailrole.getRealmid();
1611: AuthzGroup therealm = null;
1612: try {
1613: // therealm = m_realmService.getRealm(realmid);
1614: therealm = m_realmService.getAuthzGroup(realmid);
1615: } catch (Exception e) {
1616: log
1617: .debug("Exception: Mailtool.getEmailGroups() #1, "
1618: + e.getMessage());
1619: }
1620: Set users = therealm.getUsersHasRole(emailrole
1621: .getRoleid());
1622: List /* EmailUser */mailusers = new ArrayList();
1623: for (Iterator j = users.iterator(); j.hasNext();) {
1624: String userid = (String) j.next();
1625: try {
1626: User theuser = m_userDirectoryService
1627: .getUser(userid);
1628: // trying to fix SAK-7356 (Guests are not included in
1629: // recipient lists)
1630: // also SAK-7539
1631: String firstname_for_display = "";
1632: String lastname_for_display = "";
1633: if (theuser.getFirstName().trim().equals("")) {
1634: if (theuser.getEmail().trim().equals("")
1635: && theuser.getLastName().trim()
1636: .equals(""))
1637: firstname_for_display = theuser
1638: .getDisplayId(); // fix
1639: // for
1640: // SAK-7539
1641: else
1642: firstname_for_display = theuser
1643: .getEmail(); // fix
1644: // for
1645: // SAK-7356
1646: } else {
1647: firstname_for_display = theuser
1648: .getFirstName();
1649: }
1650:
1651: lastname_for_display = theuser.getLastName();
1652:
1653: EmailUser emailuser = new EmailUser(theuser
1654: .getId(), firstname_for_display,
1655: lastname_for_display, theuser
1656: .getEmail());
1657: mailusers.add(emailuser);
1658: } catch (Exception e) {
1659: log
1660: .debug("Exception: Mailtool.getEmailGroups() #2, "
1661: + e.getMessage());
1662: }
1663: }
1664: Collections.sort(mailusers);
1665: EmailGroup thegroup = new EmailGroup(emailrole,
1666: mailusers);
1667: thegroups.add(thegroup);
1668: }
1669:
1670: else if (emailrole.roletype.equals("group")
1671: && roletypefilter.equals("group")) {
1672: String sid = getSiteID();
1673: Site currentSite = null;
1674: try {
1675: currentSite = siteService.getSite(sid);
1676: } catch (Exception e) {
1677: log
1678: .debug("Exception: Mailtool.getEmailGroups() #3, "
1679: + e.getMessage());
1680: }
1681: Collection groups = currentSite.getGroups();
1682: Group agroup = null;
1683: for (Iterator groupIterator = groups.iterator(); groupIterator
1684: .hasNext();) {
1685: agroup = (Group) groupIterator.next();
1686: String groupname = agroup.getTitle();
1687: if (emailrole.getRoleid().equals(groupname))
1688: break;
1689: }
1690: Set users2 = agroup
1691: .getUsersHasRole(groupAwareRoleFound);
1692: List mailusers2 = new ArrayList();
1693: for (Iterator k = users2.iterator(); k.hasNext();) {
1694: String userid2 = (String) k.next();
1695: try {
1696: User theuser2 = m_userDirectoryService
1697: .getUser(userid2);
1698: String firstname_for_display = "";
1699: String lastname_for_display = "";
1700: if (theuser2.getFirstName().trim().equals("")) {
1701: if (theuser2.getEmail().trim().equals("")
1702: && theuser2.getLastName().trim()
1703: .equals(""))
1704: firstname_for_display = theuser2
1705: .getDisplayId(); // fix
1706: // for
1707: // SAK-7539
1708: else
1709: firstname_for_display = theuser2
1710: .getEmail(); // fix
1711: // for
1712: // SAK-7356
1713: } else {
1714: firstname_for_display = theuser2
1715: .getFirstName();
1716: }
1717:
1718: lastname_for_display = theuser2.getLastName();
1719:
1720: EmailUser emailuser2 = new EmailUser(theuser2
1721: .getId(), firstname_for_display,
1722: lastname_for_display, theuser2
1723: .getEmail());
1724:
1725: mailusers2.add(emailuser2);
1726: } catch (Exception e) {
1727: log
1728: .debug("Exception: Mailtool.getEmailGroups() #3-1, "
1729: + e.getMessage());
1730: }
1731: }
1732: Collections.sort(mailusers2);
1733: EmailGroup thegroup2 = new EmailGroup(emailrole,
1734: mailusers2);
1735: thegroups.add(thegroup2);
1736: } // else
1737: else if (emailrole.roletype.equals("section")
1738: && roletypefilter.equals("section")) {
1739: String sid = getSiteID();
1740: Site currentSite = null;
1741: try {
1742: currentSite = siteService.getSite(sid);
1743: } catch (Exception e) {
1744: log
1745: .debug("Exception: Mailtool.getEmailGroups() #4, "
1746: + e.getMessage());
1747: }
1748: Collection groups = currentSite.getGroups();
1749: Group agroup = null;
1750: for (Iterator groupIterator = groups.iterator(); groupIterator
1751: .hasNext();) {
1752: agroup = (Group) groupIterator.next();
1753: String groupname = agroup.getTitle();
1754: if (emailrole.getRoleid().equals(groupname))
1755: break;
1756: }
1757: Set users2 = agroup
1758: .getUsersHasRole(groupAwareRoleFound);
1759:
1760: List mailusers2 = new ArrayList();
1761: for (Iterator k = users2.iterator(); k.hasNext();) {
1762: String userid2 = (String) k.next();
1763: try {
1764: User theuser2 = m_userDirectoryService
1765: .getUser(userid2);
1766: String firstname_for_display = "";
1767: String lastname_for_display = "";
1768: if (theuser2.getFirstName().trim().equals("")) {
1769: if (theuser2.getEmail().trim().equals("")
1770: && theuser2.getLastName().trim()
1771: .equals(""))
1772: firstname_for_display = theuser2
1773: .getDisplayId(); // fix
1774: // for
1775: // SAK-7539
1776: else
1777: firstname_for_display = theuser2
1778: .getEmail(); // fix
1779: // for
1780: // SAK-7356
1781: } else {
1782: firstname_for_display = theuser2
1783: .getFirstName();
1784: }
1785: lastname_for_display = theuser2.getLastName();
1786: EmailUser emailuser2 = new EmailUser(theuser2
1787: .getId(), firstname_for_display,
1788: lastname_for_display, theuser2
1789: .getEmail());
1790: mailusers2.add(emailuser2);
1791: } catch (Exception e) {
1792: log
1793: .debug("Exception: Mailtool.getEmailGroups() #4-1, "
1794: + e.getMessage());
1795: }
1796: }
1797: Collections.sort(mailusers2);
1798: EmailGroup thegroup2 = new EmailGroup(emailrole,
1799: mailusers2);
1800: thegroups.add(thegroup2);
1801: } // else
1802: else if (emailrole.roletype.equals("role_groupaware")
1803: && roletypefilter.equals("role_groupaware")) {
1804: String realmid = emailrole.getRealmid();
1805: AuthzGroup therealm = null;
1806: try {
1807: therealm = m_realmService.getAuthzGroup(realmid);
1808: } catch (Exception e) {
1809: log
1810: .debug("Exception: Mailtool.getEmailGroups() #5, "
1811: + e.getMessage());
1812: }
1813: Set users = therealm.getUsersHasRole(emailrole
1814: .getRoleid());
1815: List /* EmailUser */mailusers = new ArrayList();
1816: for (Iterator j = users.iterator(); j.hasNext();) {
1817: String userid = (String) j.next();
1818: try {
1819: User theuser = m_userDirectoryService
1820: .getUser(userid);
1821: // trying to fix SAK-7356 (Guests are not included in
1822: // recipient lists)
1823: // also SAK-7539
1824: String firstname_for_display = "";
1825: String lastname_for_display = "";
1826: if (theuser.getFirstName().trim().equals("")) {
1827: if (theuser.getEmail().trim().equals("")
1828: && theuser.getLastName().trim()
1829: .equals(""))
1830: firstname_for_display = theuser
1831: .getDisplayId(); // fix
1832: // for
1833: // SAK-7539
1834: else
1835: firstname_for_display = theuser
1836: .getEmail(); // fix
1837: // for
1838: // SAK-7356
1839: } else {
1840: firstname_for_display = theuser
1841: .getFirstName();
1842: }
1843: lastname_for_display = theuser.getLastName();
1844: EmailUser emailuser = new EmailUser(theuser
1845: .getId(), firstname_for_display,
1846: lastname_for_display, theuser
1847: .getEmail());
1848: mailusers.add(emailuser);
1849: } catch (Exception e) {
1850: log
1851: .debug("Exception: Mailtool.getEmailGroups() #5-1, "
1852: + e.getMessage());
1853: }
1854: }
1855: Collections.sort(mailusers);
1856: EmailGroup thegroup = new EmailGroup(emailrole,
1857: mailusers);
1858: thegroups.add(thegroup);
1859: } // else
1860: }
1861: return thegroups;
1862: }
1863:
1864: /**
1865: * @return
1866: * return the current user
1867: */
1868: public EmailUser getCurrentUser() {
1869: EmailUser euser = null;
1870: User curU = null;
1871: try {
1872: curU = m_userDirectoryService.getCurrentUser();
1873: euser = new EmailUser(curU.getId(), curU.getDisplayName(),
1874: curU.getEmail());
1875: } catch (Exception e) {
1876: log.debug("Exception: Mailtool.getCurrentUser(), "
1877: + e.getMessage());
1878: }
1879: return euser;
1880: }
1881:
1882: /**
1883: * Append email to Email Archive
1884: *
1885: * @param channelRef
1886: * @param sender
1887: * @param subject
1888: * @param body
1889: * @return
1890: * true if success
1891: */
1892: protected boolean appendToArchive(String channelRef, String sender,
1893: String subject, String body) {
1894: MailArchiveChannel channel = null;
1895: try {
1896: channel = MailArchiveService
1897: .getMailArchiveChannel(channelRef);
1898: } catch (Exception e) {
1899: log.debug("Exception: Mailtool.appendToArchive() #1, "
1900: + e.getMessage());
1901: return false;
1902: }
1903: if (channel == null) {
1904: log.debug("Mailtool: The channel: " + channelRef
1905: + " is null.");
1906:
1907: return false;
1908: }
1909: List mailHeaders = new Vector();
1910: if (isFCKeditor() || isHTMLArea()) {
1911: mailHeaders
1912: .add(MailArchiveService.HEADER_OUTER_CONTENT_TYPE
1913: + ": text/html; charset=ISO-8859-1");
1914: mailHeaders
1915: .add(MailArchiveService.HEADER_INNER_CONTENT_TYPE
1916: + ": text/html; charset=ISO-8859-1");
1917: } else {
1918: mailHeaders
1919: .add(MailArchiveService.HEADER_OUTER_CONTENT_TYPE
1920: + ": text/plain; charset=ISO-8859-1");
1921: mailHeaders
1922: .add(MailArchiveService.HEADER_INNER_CONTENT_TYPE
1923: + ": text/plain; charset=ISO-8859-1");
1924: }
1925: mailHeaders.add("Mime-Version: 1.0");
1926: mailHeaders.add("From: " + sender);
1927: mailHeaders.add("Reply-To: " + sender);
1928: try {
1929: // This way actually sends the email too
1930: // channel.addMailArchiveMessage(subject, sender,
1931: // TimeService.newTime(), mailHeaders, null, body);
1932: MailArchiveMessageEdit edit = (MailArchiveMessageEdit) channel
1933: .addMessage();
1934: MailArchiveMessageHeaderEdit header = edit
1935: .getMailArchiveHeaderEdit();
1936: edit.setBody(body);
1937: header.replaceAttachments(null);
1938: header.setSubject(subject);
1939: header.setFromAddress(sender);
1940: header.setDateSent(TimeService.newTime());
1941: header.setMailHeaders(mailHeaders);
1942: channel.commitMessage(edit, NotificationService.NOTI_NONE);
1943: } catch (Exception e) {
1944: log.debug("Exception: Mailtool.appendToArchive() #2, "
1945: + e.getMessage());
1946:
1947: return false;
1948: }
1949: return true;
1950: }
1951:
1952: /**
1953: * File upload via valuechangeEvent listener
1954: * @param event
1955: * @throws AbortProcessingException
1956: */
1957: public void processFileUpload(ValueChangeEvent event)
1958: throws AbortProcessingException {
1959: Attachment att = new Attachment();
1960: int maxnumattachment = getMaxNumAttachment();
1961: if (num_files < maxnumattachment) {
1962: try {
1963: FileItem item = (FileItem) event.getNewValue();
1964: long fileSize = item.getSize();
1965:
1966: filename = item.getName();
1967: if (filename != null) {
1968: filename = FilenameUtils.getName(filename);
1969: att.setFilename(filename);
1970: }
1971: String ud = getUploadDirectory();
1972: File dir = new File(ud);
1973:
1974: if (isNotAlreadyUploaded(filename, attachedFiles) == false
1975: && dir.isDirectory()) {
1976: File fNew = new File(getUploadDirectory(), this
1977: .getCurrentUser().getUserid()
1978: + "-" + filename);
1979: // in IE, fi.getName() returns full path, while in FF,
1980: // returns only name.
1981: att.setSize((String) Long.toString(fileSize));
1982: att.setId(num_id);
1983: num_files++;
1984: num_id++;
1985: item.write(fNew);
1986: attachedFiles.add(att);
1987: }
1988: } catch (Exception e) {
1989: log.debug("Exception: Mailtool.processFileUpload(), "
1990: + e.getMessage()); // handle exception
1991: }
1992: } // end if
1993: }
1994:
1995: /**
1996: * remove the attachment file specified by id
1997: */
1998: public void processRemoveFile() {
1999: String id = getFacesParamValue(facesContext, "id");
2000: Attachment a = null;
2001: Attachment aForRemoval = null;
2002: Iterator iter = attachedFiles.iterator();
2003: while (iter.hasNext()) {
2004: a = (Attachment) iter.next();
2005: if (id.equals(a.getFilename())) {
2006: aForRemoval = a;
2007: }
2008: }
2009: attachedFiles.remove(aForRemoval);
2010: num_files--;
2011: }
2012:
2013: public boolean isNotAlreadyUploaded(String s, List attachedFiles) {
2014: Attachment a = null;
2015: Iterator iter = attachedFiles.iterator();
2016: while (iter.hasNext()) {
2017: a = (Attachment) iter.next();
2018: if (s.equals(a.getFilename())) {
2019: return true;
2020: }
2021: }
2022: return false;
2023: }
2024:
2025: public void toggle_attachClicked() {
2026: attachClicked = attachClicked ? false : true;
2027: }
2028:
2029: /**
2030: * @return
2031: * return attachedFiles List
2032: */
2033: public List getAllAttachments() {
2034: return attachedFiles;
2035: }
2036:
2037: public static String getFacesParamValue(FacesContext fc, String name) {
2038: return (String) fc.getCurrentInstance().getExternalContext()
2039: .getRequestParameterMap().get(name);
2040: }
2041:
2042: /**
2043: * Validate email address(throws validatorexception if the entered string does not look like email address
2044: *
2045: * @param context
2046: * @param toValidate
2047: * @param value
2048: * @throws ValidatorException
2049: */
2050: public void validateEmail(FacesContext context,
2051: UIComponent toValidate, Object value)
2052: throws ValidatorException {
2053: String enteredEmail = (String) value;
2054: Pattern p = Pattern.compile("(.+@.+\\.[a-z]+)"); // Set the email
2055: // pattern string
2056:
2057: // Match the given string with the pattern
2058: Matcher m = p.matcher(enteredEmail);
2059:
2060: // Check whether match is found
2061: boolean matchFound = m.matches();
2062:
2063: if (!matchFound) {
2064: FacesMessage message = new FacesMessage();
2065: message.setDetail("Email not valid");
2066: message.setSummary("Email not valid");
2067: message.setSeverity(FacesMessage.SEVERITY_ERROR);
2068: throw new ValidatorException(message);
2069: }
2070: }
2071:
2072: public String getReplyToSelected() {
2073: return m_replyto;
2074: }
2075:
2076: public void setReplyToSelected(String r) {
2077: this .m_replyto = r;
2078: }
2079:
2080: public boolean isEmailArchiveInSite() {
2081: return EmailArchiveInSite;
2082: }
2083:
2084: public void setEmailArchiveInSite(boolean emailArchiveInSite) {
2085: EmailArchiveInSite = emailArchiveInSite;
2086: }
2087:
2088: public boolean isGroupAwareRoleviewClicked() {
2089: return groupAwareRoleviewClicked;
2090: }
2091:
2092: public void setGroupAwareRoleviewClicked(
2093: boolean groupAwareRoleviewClicked) {
2094: this .groupAwareRoleviewClicked = groupAwareRoleviewClicked;
2095: }
2096:
2097: public boolean isAllGroupAwareRoleSelected() {
2098: return allGroupAwareRoleSelected;
2099: }
2100:
2101: public void setAllGroupAwareRoleSelected(
2102: boolean allGroupAwareRoleSelected) {
2103: this .allGroupAwareRoleSelected = allGroupAwareRoleSelected;
2104: }
2105:
2106: public int getNum_groupawarerole() {
2107: return num_groupawarerole;
2108: }
2109:
2110: public void setNum_groupawarerole(int num_groupawarerole) {
2111: this .num_groupawarerole = num_groupawarerole;
2112: }
2113:
2114: public boolean isGroupAwareRoleExist() {
2115: return GroupAwareRoleExist;
2116: }
2117:
2118: public void setGroupAwareRoleExist(boolean groupAwareRoleExist) {
2119: GroupAwareRoleExist = groupAwareRoleExist;
2120: }
2121:
2122: public String getSitename() {
2123: return sitename;
2124: }
2125:
2126: public void setSitename(String sitename) {
2127: this.sitename = sitename;
2128: }
2129: }
|