View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.logic.functor;
17  
18  import java.util.Properties;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.seasar.framework.container.S2Container;
23  import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
24  import org.seasar.tuigwaa.cms.ContentsService;
25  import org.seasar.tuigwaa.cms.core.CmsConstants;
26  import org.seasar.tuigwaa.cms.core.Page;
27  import org.seasar.tuigwaa.cms.core.Resource;
28  import org.seasar.tuigwaa.model.common.EntityUtils;
29  import org.seasar.tuigwaa.net.EmailService;
30  import org.seasar.tuigwaa.system.TgwSecurityException;
31  import org.seasar.tuigwaa.system.TgwServiceException;
32  import org.seasar.tuigwaa.util.MimeMappings;
33  import org.seasar.tuigwaa.util.TgwContext;
34  import org.seasar.tuigwaa.util.TgwResource;
35  
36  import com.isenshi.util.functor.UnaryProcedure;
37  
38  public class EmailSendProcedure implements UnaryProcedure {
39  
40  	private Log log = LogFactory.getLog(getClass());
41  
42  	private String emailField;
43  
44  	private String pageName;
45  
46  	private String type;
47  	
48  	private static final String MAIL_SUBJECT_PREFIX = TgwResource.getProperty("mail.subject.prefix");	
49  	
50  	public EmailSendProcedure() {
51  	}
52  
53  	public EmailSendProcedure(String emailField, String pageName, String type) {
54  		this.emailField = emailField;
55  		this.pageName = pageName;
56  		this.type = type;
57  	}
58  
59  	public String getEmailField() {
60  		return emailField;
61  	}
62  
63  	public void setEmailField(String emailField) {
64  		this.emailField = emailField;
65  	}
66  
67  	public String getPageName() {
68  		return pageName;
69  	}
70  
71  	public void setPageName(String pageName) {
72  		this.pageName = pageName;
73  	}
74  
75  	public String getType() {
76  		return type;
77  	}
78  
79  	public void setType(String type) {
80  		this.type = type;
81  	}
82  
83  	public void run(Object obj) {
84  						
85  		String emailAddress = (String) EntityUtils.getProperty(obj, emailField);
86  		if (emailAddress == null || emailAddress.length() == 0) {
87  			log.warn(" The email address is null. email procedure is aborted.");
88  			return;
89  		}
90  		
91  		try {
92  			S2Container container = SingletonS2ContainerFactory.getContainer();
93  			EmailService emailService = (EmailService) container.getComponent(EmailService.class);			
94  			
95  			Page page = null;
96  			try {
97  				page = getMailContent();
98  			} catch (TgwSecurityException e) {
99  				log.warn("security exception.email procedure is aborted." + e.toString());
100 				return;
101 			}
102 			
103 			Resource resource = page.getResource();
104 			String subject = MAIL_SUBJECT_PREFIX + " " + resource.getPageName();
105 			String content = page.getContent().toString();
106 			
107 			Properties mailProperties = new Properties();			
108 			mailProperties.setProperty(EmailService.MAIL_SUBJECT,subject);
109 			if(MimeMappings.CONTENTTYPE_HTML.equals(resource.getContentType()) || "html".equals(type)){
110 				// WYSIWYG Page or sending style selected as html
111 				mailProperties.setProperty(EmailService.MAIL_CONTENTTYPE,MimeMappings.CONTENTTYPE_HTML);
112 			}else{ 
113 				// usual Wiki Page and sending style selected as text/plain
114 				mailProperties.setProperty(EmailService.MAIL_CONTENTTYPE,MimeMappings.CONTENTTYPE_TXT);
115 			}						
116 			emailService.sendEmail(emailAddress,content,mailProperties,null);
117 			
118 		} catch(TgwServiceException tse){
119 			tse.printStackTrace();
120 		}
121 	}
122 	
123 	private Page getMailContent() throws TgwSecurityException{
124 		Page page = null;
125 		String siteName = TgwContext.getSiteName();
126 		S2Container container = SingletonS2ContainerFactory.getContainer();
127 		ContentsService contentsService = (ContentsService) container.getComponent(ContentsService.class);
128 		
129 		if ("html".equals(type)) {
130 			page = contentsService.getPage(siteName, pageName,
131 					CmsConstants.OUTPUTTYPE_HTML);
132 		} else {
133 			page = contentsService.getPage(siteName, pageName,
134 					CmsConstants.OUTPUTTYPE_TEXT);
135 		}
136 		return page;
137 	}
138 		
139 }