root/head/ambra/webapp/src/main/java/org/topazproject/ambra/article/action/EmailArticleAction.java @ 6940

Revision 6940, 8.3 KB (checked in by dragisak, 21 months ago)

Get rid of static calls to AmbraUser?.geCurrentUser(). Remove dependency on servlet session from POJO's.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id HeadURL Revision
Line 
1/* $HeadURL::                                                                            $
2 * $Id$
3 *
4 * Copyright (c) 2006-2007 by Topaz, Inc.
5 * http://topazproject.org
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19package org.topazproject.ambra.article.action;
20
21import org.apache.commons.lang.StringUtils;
22import org.apache.commons.logging.Log;
23import org.apache.commons.logging.LogFactory;
24import org.apache.commons.validator.EmailValidator;
25import org.springframework.transaction.annotation.Transactional;
26import org.springframework.beans.factory.annotation.Required;
27import org.topazproject.ambra.article.service.ArticleOtmService;
28import org.topazproject.ambra.article.service.NoSuchArticleIdException;
29import org.topazproject.ambra.email.impl.FreemarkerTemplateMailer;
30import org.topazproject.ambra.models.ObjectInfo;
31import org.topazproject.ambra.service.AmbraMailer;
32import org.topazproject.ambra.user.AmbraUser;
33import org.topazproject.ambra.user.action.UserActionSupport;
34
35
36import java.util.ArrayList;
37import java.util.HashMap;
38import java.util.Iterator;
39import java.util.Map;
40import java.util.StringTokenizer;
41import java.net.URI;
42
43/**
44 * Email the article to another user.
45 */
46public class EmailArticleAction extends UserActionSupport {
47  private String articleURI;
48  private String emailTo;
49  private String emailFrom;
50  private String senderName;
51  private String note;
52  private String title;
53  private String description;
54  private String journalName;
55  private AmbraMailer ambraMailer;
56  private ArticleOtmService articleOtmService;
57  private static final Log log = LogFactory.getLog(EmailArticleAction.class);
58  private static final int MAX_TO_EMAIL = 5;
59
60  /**
61   * Render the page with the values passed in
62   * @return webwork status
63   * @throws Exception Exception
64   */
65  @Transactional(readOnly = true)
66  public String executeRender() throws Exception {
67    final AmbraUser ambraUser = getCurrentUser();
68    if (null != ambraUser) {
69      senderName = ambraUser.getDisplayName();
70      emailFrom = ambraUser.getEmail();
71    }
72    setArticleTitleAndDesc(articleURI);
73    return SUCCESS;
74  }
75
76  /**
77   * Send the email
78   * @return webwork status
79   * @throws Exception Exception
80   */
81  @Transactional(readOnly = true)
82  public String executeSend() throws Exception {
83    if (!validates()) return INPUT;
84    setArticleTitleAndDesc(articleURI);
85    final Map<String, String> mapFields = new HashMap<String, String>();
86    mapFields.put("articleURI", articleURI);
87    mapFields.put("senderName", senderName);
88    mapFields.put(FreemarkerTemplateMailer.USER_NAME_KEY, senderName);
89    mapFields.put("note", note);
90    mapFields.put("title", title);
91    mapFields.put("description", description);
92    mapFields.put("journalName", journalName);
93    mapFields.put("subject", "An Article from " + journalName);
94    ambraMailer.sendEmailThisArticleEmail(emailTo, emailFrom, mapFields);
95
96    return SUCCESS;
97  }
98
99  private void setArticleTitleAndDesc(final String articleURI) throws NoSuchArticleIdException {
100    final ObjectInfo articleInfo = articleOtmService.getArticle(URI.create(articleURI));
101    title = articleInfo.getDublinCore().getTitle();
102    description = articleInfo.getDublinCore().getDescription();
103  }
104
105  private boolean validates() {
106    boolean isValid = true;
107    if (StringUtils.isBlank(articleURI)) {
108      addFieldError("articleURI", "Article URI cannot be empty");
109      isValid = false;
110    }
111    if (StringUtils.isBlank(emailFrom)) {
112      addFieldError("emailFrom", "Your e-mail address cannot be empty");
113      isValid = false;
114    }
115    if (!EmailValidator.getInstance().isValid(emailFrom)) {
116      addFieldError("emailFrom", "Invalid e-mail address");
117      isValid = false;
118    }
119    if (StringUtils.isBlank(emailTo)) {
120      addFieldError("emailTo", "To e-mail address cannot be empty");
121      isValid = false;
122    }
123    isValid = checkEmails(emailTo) && isValid;
124    if (StringUtils.isBlank(senderName)) {
125      addFieldError("senderName", "Your name cannot be empty");
126      isValid = false;
127    }
128
129    return isValid;
130  }
131
132  private boolean checkEmails (String emailList) {
133    final StringTokenizer emailTokens = new StringTokenizer(emailList, " \t\n\r\f,");
134    if (emailTokens.countTokens() > MAX_TO_EMAIL) {
135      addFieldError ("emailTo", "Maximum of " + MAX_TO_EMAIL + " email addresses");
136      return false;
137    }
138    EmailValidator validator = EmailValidator.getInstance();
139    ArrayList <String> invalidEmails = new ArrayList<String> ();
140
141    while (emailTokens.hasMoreTokens()) {
142      String email = emailTokens.nextToken();
143      if (!validator.isValid(email)) {
144        invalidEmails.add(email);
145      }
146    }
147    final int numInvalid = invalidEmails.size();
148    if (numInvalid != 0) {
149      StringBuilder errorMsg = new StringBuilder ("Invalid e-mail address");
150      if (numInvalid > 1) {
151        errorMsg.append ("es: ");
152      } else {
153        errorMsg.append(": ");
154      }
155      Iterator<String> iter = invalidEmails.iterator();
156      while (iter.hasNext()){
157        errorMsg.append (iter.next());
158        if (iter.hasNext()) {
159          errorMsg.append(", ");
160        }
161      }
162      addFieldError ("emailTo", errorMsg.toString());
163    }
164    return (numInvalid == 0);
165  }
166
167  /**
168   * Getter for articleURI.
169   * @return Value of articleURI.
170   */
171  public String getArticleURI() {
172    return articleURI;
173  }
174
175  /**
176   * Setter for articleURI.
177   * @param articleURI Value to set for articleURI.
178   */
179  public void setArticleURI(final String articleURI) {
180    this.articleURI = articleURI;
181  }
182
183  /**
184   * Getter for emailFrom.
185   * @return Value of emailFrom.
186   */
187  public String getEmailFrom() {
188    return emailFrom;
189  }
190
191  /**
192   * Setter for emailFrom.
193   * @param emailFrom Value to set for emailFrom.
194   */
195  public void setEmailFrom(final String emailFrom) {
196    this.emailFrom = emailFrom;
197  }
198
199  /**
200   * Getter for emailTo.
201   * @return Value of emailTo.
202   */
203  public String getEmailTo() {
204    return emailTo;
205  }
206
207  /**
208   * Setter for emailTo.
209   * @param emailTo Value to set for emailTo.
210   */
211  public void setEmailTo(final String emailTo) {
212    this.emailTo = emailTo;
213  }
214
215  /**
216   * Getter for note.
217   * @return Value of note.
218   */
219  public String getNote() {
220    return note;
221  }
222
223  /**
224   * Setter for note.
225   * @param note Value to set for note.
226   */
227  public void setNote(final String note) {
228    this.note = note;
229  }
230
231  /**
232   * Getter for senderName.
233   * @return Value of senderName.
234   */
235  public String getSenderName() {
236    return senderName;
237  }
238
239  /**
240   * Setter for senderName.
241   * @param senderName Value to set for senderName.
242   */
243  public void setSenderName(final String senderName) {
244    this.senderName = senderName;
245  }
246
247  /**
248   * Setter for ambraMailer.
249   * @param ambraMailer Value to set for ambraMailer.
250   */
251  @Required
252  public void setAmbraMailer(final AmbraMailer ambraMailer) {
253    this.ambraMailer = ambraMailer;
254  }
255
256  /**
257   * Setter for fetchArticleService.
258   * @param articleOtmService Value to set for ArticleOtmService.
259   */
260  @Required
261  public void setArticleOtmService(ArticleOtmService articleOtmService) {
262    this.articleOtmService = articleOtmService;
263  }
264
265  /**
266   * Getter for description.
267   * @return Value of description.
268   */
269  public String getDescription() {
270    return description;
271  }
272
273  /**
274   * Getter for title.
275   * @return Value of title.
276   */
277  public String getTitle() {
278    return title;
279  }
280
281  /**
282   * @param title The title to set.
283   */
284  public void setTitle(String title) {
285    this.title = title;
286  }
287
288  /**
289   * @return Returns the journalName.
290   */
291  public String getJournalName() {
292    return journalName;
293  }
294
295  /**
296   * @param journalName The journalName to set.
297   */
298  public void setJournalName(String journalName) {
299    this.journalName = journalName;
300  }
301
302  /**
303   * @return Returns the mAX_TO_EMAIL.
304   */
305  public static int getMaxEmails() {
306    return MAX_TO_EMAIL;
307  }
308}
Note: See TracBrowser for help on using the browser.