001: package org.contineo.web.document;
002:
003: import org.apache.commons.lang.builder.ReflectionToStringBuilder;
004: import org.apache.commons.logging.Log;
005: import org.apache.commons.logging.LogFactory;
006:
007: import org.contineo.core.FileBean;
008: import org.contineo.core.communication.Attachment;
009: import org.contineo.core.communication.EMail;
010: import org.contineo.core.communication.EMailSender;
011: import org.contineo.core.communication.Recipient;
012: import org.contineo.core.communication.dao.EMailDAO;
013: import org.contineo.core.document.Document;
014: import org.contineo.core.document.DownloadTicket;
015: import org.contineo.core.security.Menu;
016: import org.contineo.core.security.User;
017: import org.contineo.core.security.dao.MenuDAO;
018:
019: import org.contineo.util.Context;
020: import org.contineo.util.config.MimeTypeConfig;
021: import org.contineo.util.config.SettingsConfig;
022:
023: import org.contineo.web.SessionManagement;
024: import org.contineo.web.i18n.Messages;
025: import org.contineo.web.navigation.PageContentBean;
026:
027: import java.util.ArrayList;
028: import java.util.Collection;
029: import java.util.Date;
030:
031: /**
032: * This form is used ro send emails
033: *
034: * @author Michael Scholz, Marco Meschieri
035: * @version $Id: EMailForm.java,v 1.2 2006/09/03 16:24:37 marco Exp $
036: * @since 1.0
037: */
038: public class EMailForm {
039: protected static Log log = LogFactory.getLog(EMailForm.class);
040: private String author;
041: private String recipient;
042: private String subject;
043: private String text;
044: private Document selectedDocument;
045: private Collection<Menu> attachments = new ArrayList<Menu>();
046: private DownloadTicket downloadTicket;
047: private DocumentNavigation documentNavigation;
048:
049: public EMailForm() {
050: }
051:
052: public String getAuthor() {
053: return author;
054: }
055:
056: public void setAuthor(String author) {
057: this .author = author;
058: }
059:
060: public String getRecipient() {
061: return recipient;
062: }
063:
064: public void setRecipient(String recipient) {
065: this .recipient = recipient;
066: }
067:
068: public String getSubject() {
069: return subject;
070: }
071:
072: public void setSubject(String subject) {
073: this .subject = subject;
074: }
075:
076: public String getText() {
077: return text;
078: }
079:
080: public void setText(String text) {
081: this .text = text;
082: }
083:
084: public Collection<Menu> getAttachments() {
085: return attachments;
086: }
087:
088: public void setAttachments(Collection<Menu> attachments) {
089: this .attachments = attachments;
090: }
091:
092: public void reset() {
093: author = "";
094: recipient = "";
095: subject = "";
096: text = "";
097: attachments.clear();
098: downloadTicket = null;
099: selectedDocument = null;
100: }
101:
102: public Document getSelectedDocument() {
103: return selectedDocument;
104: }
105:
106: public void setSelectedDocument(Document selectedDocument) {
107: this .selectedDocument = selectedDocument;
108: }
109:
110: public DownloadTicket getDownloadTicket() {
111: return downloadTicket;
112: }
113:
114: public void setDownloadTicket(DownloadTicket downloadTicket) {
115: this .downloadTicket = downloadTicket;
116: }
117:
118: public String toString() {
119: return (new ReflectionToStringBuilder(this ) {
120: protected boolean accept(java.lang.reflect.Field f) {
121: return super .accept(f);
122: }
123: }).toString();
124: }
125:
126: public String send() {
127: EMail email;
128:
129: if (SessionManagement.isValid()) {
130: try {
131: User user = SessionManagement.getUser();
132: email = new EMail();
133:
134: EMailDAO emailDao = (EMailDAO) Context.getInstance()
135: .getBean(EMailDAO.class);
136: email.setAccountId(1);
137: email.setAuthor(user.getUserName());
138: email.setAuthorAddress(user.getEmail());
139:
140: Recipient recipient = new Recipient();
141: recipient.setAddress(getRecipient());
142: email.addRecipient(recipient);
143: email.setFolder("outbox");
144: email.setMessageText(getText());
145: email.setRead(1);
146: email.setSentDate(String.valueOf(new Date().getTime()));
147: email.setSubject(getSubject());
148: email.setUserName(user.getUserName());
149: emailDao.store(email);
150:
151: SettingsConfig conf = (SettingsConfig) Context
152: .getInstance().getBean(SettingsConfig.class);
153:
154: String udir = conf.getValue("userdir");
155: String maildir = udir + "/mails/"
156: + String.valueOf(email.getMessageId()) + "/";
157: FileBean.createDir(maildir);
158: FileBean.writeFile(email.getMessageText(), maildir
159: + "email.mail");
160:
161: int i = 2;
162:
163: for (Menu menu : attachments) {
164: createAttachment(email, menu.getMenuId(), i++);
165: }
166:
167: emailDao.store(email);
168:
169: try {
170: EMailSender sender = (EMailSender) Context
171: .getInstance().getBean(EMailSender.class);
172: sender.send(email);
173: Messages.addLocalizedInfo("msg.action.saveemail");
174: } catch (Exception ex) {
175: log.error(ex.getMessage(), ex);
176: Messages
177: .addLocalizedError("errors.action.saveemail");
178: }
179:
180: setAuthor(user.getEmail());
181: } catch (Exception e) {
182: log.error(e.getMessage(), e);
183: Messages.addLocalizedError("errors.action.saveemail");
184: }
185: } else {
186: return "login";
187: }
188:
189: documentNavigation.setSelectedPanel(new PageContentBean(
190: "documents"));
191: reset();
192:
193: return null;
194: }
195:
196: private void createAttachment(EMail email, int menuId, int partid) {
197: Attachment att = new Attachment();
198: MenuDAO menuDao = (MenuDAO) Context.getInstance().getBean(
199: MenuDAO.class);
200: Menu menu = menuDao.findByPrimaryKey(menuId);
201:
202: if (menuDao.isReadEnable(menu.getMenuId(), email.getUserName())) {
203: att.setIcon(menu.getMenuIcon());
204: att.setFilename(menu.getMenuRef());
205:
206: String extension = menu.getMenuRef().substring(
207: menu.getMenuRef().lastIndexOf(".") + 1);
208: MimeTypeConfig mtc = (MimeTypeConfig) Context.getInstance()
209: .getBean(MimeTypeConfig.class);
210: String mimetype = mtc.getMimeApp(extension);
211:
212: if ((mimetype == null) || mimetype.equals("")) {
213: mimetype = "application/octet-stream";
214: }
215:
216: att.setMimeType(mimetype);
217:
218: SettingsConfig conf = (SettingsConfig) Context
219: .getInstance().getBean(SettingsConfig.class);
220: String docdir = conf.getValue("docdir");
221: String doc = docdir + "/" + menu.getMenuPath() + "/"
222: + menu.getMenuId() + "/" + menu.getMenuRef();
223: String userdir = conf.getValue("userdir");
224: String mail = userdir + "/mails/"
225: + String.valueOf(email.getMessageId()) + "/";
226: FileBean.copyFile(doc, mail + menu.getMenuRef());
227:
228: if (att != null) {
229: email.addAttachment(partid, att);
230: }
231: }
232: }
233:
234: public void setDocumentNavigation(
235: DocumentNavigation documentNavigation) {
236: this.documentNavigation = documentNavigation;
237: }
238: }
|