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.plugin.misc;
17  
18  import java.util.Properties;
19  
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  
23  import org.seasar.tuigwaa.cms.ContentsService;
24  import org.seasar.tuigwaa.cms.core.CmsConstants;
25  import org.seasar.tuigwaa.cms.core.CmsRequest;
26  import org.seasar.tuigwaa.cms.core.CmsResponse;
27  import org.seasar.tuigwaa.cms.core.Page;
28  import org.seasar.tuigwaa.cms.core.wiki.WikiContext;
29  import org.seasar.tuigwaa.database.DataTable;
30  import org.seasar.tuigwaa.database.function.DaoMethod;
31  import org.seasar.tuigwaa.database.function.criteria.EqCriteriaFunction;
32  import org.seasar.tuigwaa.model.common.EntityDAO;
33  import org.seasar.tuigwaa.model.common.EntityUtils;
34  import org.seasar.tuigwaa.model.core.TgwAttribute;
35  import org.seasar.tuigwaa.model.core.TgwEntity;
36  import org.seasar.tuigwaa.net.EmailService;
37  import org.seasar.tuigwaa.plugin.AbstractTgwPlugin;
38  import org.seasar.tuigwaa.plugin.PluginException;
39  import org.seasar.tuigwaa.plugin.PluginRequest;
40  import org.seasar.tuigwaa.plugin.TgwPluginUtils;
41  import org.seasar.tuigwaa.system.TgwServiceException;
42  import org.seasar.tuigwaa.util.TgwContext;
43  import org.seasar.tuigwaa.util.TgwResource;
44  
45  import com.isenshi.util.HtmlBuffer;
46  
47  public class EmailPlugin extends AbstractTgwPlugin {
48  
49  	private ContentsService contents = (ContentsService) getService(ContentsService.class);
50  	
51  	private EmailService mailService = (EmailService) getService(EmailService.class);
52  
53  	private static final String PARAM_TO = getParameterName("email.to");
54  
55  	private static final String PARAM_TITLE = getParameterName("email.title");
56  
57  	private static final String PARAM_TYPE = getParameterName("email.type");
58  
59  	private static final String PARAM_CONTENT = getParameterName("email.content");
60  
61  	private static final String PARAM_FORWARD = getParameterName("email.pageName");
62  
63  	private static final String PARAM_ENTITY = getParameterName("email.entity");
64  
65  	public String doHTMLView(CmsRequest request, CmsResponse response,
66  			PluginRequest prequest) throws PluginException {
67  
68  		WikiContext ctx = getConfiguration().getWikiContext();
69  		String action = ctx.getPluginProxyURL(prequest.getName(), request)
70  				.toString();
71  
72  		String content = null;
73  		String emails = null;
74  		String entityName = null;
75  		String pageName = null;
76  
77  		String[] args = prequest.getArgs();
78  		String forwardPage = args[0];
79  
80  		if (args != null && args.length > 1) {
81  			String pagePath = args[1];
82  			String[] pagePaths = pagePath.split("/");
83  			pageName = TgwResource.getProperty("mail.subject.prefix")
84  					+ pagePaths[pagePaths.length - 1];
85  			Page page = getPageData(request, pagePath);
86  			content = page.getContent().toString();
87  		}
88  		if (args != null && args.length > 2) {
89  			entityName = args[2];
90  
91  			emails = getEmails(request.getSiteName(), entityName,
92  					(args.length > 3) ? args[3] : null);
93  		}
94  
95  		HtmlBuffer buf = new HtmlBuffer();
96  		bindForm(buf, action, forwardPage, entityName, emails, pageName,
97  				content);
98  		return buf.toString();
99  	}
100 
101 	public String doAction(HttpServletRequest request,
102 			HttpServletResponse response) {
103 
104 		String pageName = request.getParameter(PARAM_FORWARD);
105 
106 		if (TgwContext.getPrincipal() == null) {
107 			return pageName;
108 		}
109 		
110 		String to = request.getParameter(PARAM_TO);
111 		String title = request.getParameter(PARAM_TITLE);
112 		String content = request.getParameter(PARAM_CONTENT);
113 		String entityName = request.getParameter(PARAM_ENTITY);
114 		String type = request.getParameter(PARAM_TYPE);
115 
116 		String siteName = TgwContext.getSiteName();
117 
118 		TgwEntity entity = getEntity(siteName, entityName);
119 		TgwAttribute emailAttr = EntityUtils.getEmailAttribute(entity);
120 
121 		DaoMethod method = getEntityDAO(entity).getMethod(EntityDAO.INJECT_CRITERIA);
122 
123 		Properties mailProperties = new Properties();
124 		mailProperties.setProperty(EmailService.MAIL_CONTENTTYPE,type);
125 		mailProperties.setProperty(EmailService.MAIL_SUBJECT,title);		
126 		
127 		String[] emails = to.split(",");
128 		for (int i = 0; i < emails.length; i++) {
129 			DataTable dataTable = (DataTable) method
130 					.evaluate(new EqCriteriaFunction(emailAttr.getName(),
131 							emails[i]));
132 			if (dataTable.hasNext()) {
133 				dataTable.next();
134 				Object obj = dataTable.getRowObject();
135 				TgwPluginUtils.bindEntityObject(entity, obj);
136 				Page page = null;
137 				
138 				if(CmsConstants.CONTENTTYPE_HTML.equals(type)){
139 					page = contents.convert(siteName, "",CmsConstants.CONTENTTYPE_XWIKI, content,
140 							CmsConstants.OUTPUTTYPE_HTML);
141 				}else{
142 					page = contents.convert(siteName, "",CmsConstants.CONTENTTYPE_XWIKI, content,
143 							CmsConstants.OUTPUTTYPE_TEXT);
144 				}
145 				
146 				try{
147 					mailService.sendEmail(emails[i],page.getContent().toString(),mailProperties,null);
148 				}catch(TgwServiceException tse){
149 					tse.printStackTrace();
150 				}				
151 			}
152 		}
153 		return pageName;
154 	}
155 
156 	private String getEmails(String domainName, String entityName,
157 			String filterName) {
158 		TgwEntity entity = getEntity(domainName, entityName);
159 		TgwAttribute emailAttribute = EntityUtils.getEmailAttribute(entity);
160 		if (emailAttribute == null) {
161 			return null;
162 		}
163 		StringBuffer buf = new StringBuffer();
164 
165 		DataTable dataTable = null;
166 		if (filterName != null && !"".equals(filterName)) {
167 			dataTable = (DataTable) getEntityDAO(entity).getMethod(filterName)
168 					.evaluate();
169 		} else {
170 			dataTable = getEntityDAO(entity).list();
171 		}
172 
173 		boolean first = true;
174 		while (dataTable.hasNext()) {
175 			dataTable.next();
176 			Object obj = dataTable.getRowObject();
177 			if (first) {
178 				first = false;
179 			} else {
180 				buf.append(",");
181 			}
182 			buf.append(EntityUtils.getProperty(obj, emailAttribute));
183 		}
184 		return buf.toString();
185 	}
186 
187 	private void bindForm(HtmlBuffer buf, String action, String forwardPage,
188 			String entityName, String emails, String title, String content) {
189 		buf.appendStartTag("form");
190 		buf.appendAttribute("action", action);
191 		buf.appendAttribute("method", "post");
192 
193 		buf.appendHidden(PARAM_FORWARD, forwardPage);
194 		buf.appendHidden(PARAM_ENTITY, entityName);
195 
196 		buf.appendFieldset(getMessage("email.form"));
197 
198 		String to = (emails == null) ? "" : emails;
199 		buf.appendStartTag("label");
200 		buf.appendBody(getMessage("email.to") + ":");
201 		buf.endTag();
202 		//buf.appendTextField(PARAM_TO, to, 70);
203 		buf.appendTextArea(PARAM_TO, to, 3, 70);
204 		buf.appendBr();
205 		buf.appendStartTag("label");
206 		buf.appendBody("&nbsp;");
207 		buf.endTag();
208 		buf.appendBody(getMessage("email.msg"));
209 		buf.appendBr();
210 
211 		buf.appendStartTag("label");
212 		buf.appendBody(getMessage("email.subject") + ":");
213 		buf.endTag();
214 		buf.appendTextField(PARAM_TITLE, title, 70);
215 		buf.appendBr();
216 
217 		
218 
219 		buf.appendBody(getMessage("email.content") + ":");
220 		buf.appendBody(getMessage("email.wiki"));
221 		
222 		buf.appendSelect(PARAM_TYPE, new String[] {
223 				CmsConstants.CONTENTTYPE_HTML, CmsConstants.CONTENTTYPE_TEXT });
224 		buf.appendBr();
225 		
226 		buf.setNewline(false);
227 		buf.setTab(false);
228 		buf.appendStartTag("textarea");
229 		buf.appendAttribute("name", PARAM_CONTENT);
230 		buf.appendAttribute("cols", "80");
231 		buf.appendAttribute("rows", "20");
232 		if (content != null) {
233 			buf.appendBody(content);
234 		} else {
235 			buf.appendBody("");
236 		}
237 		buf.endTag();
238 
239 		buf.appendBr();
240 		buf.appendSubmitButton(getMessage("button.submit"));
241 		buf.endTag();
242 		buf.endTag();
243 	}
244 }