View Javadoc

1   /*
2    * Copyright (C) 2007 Alf Mikula
3    * 
4    * This file is part of PromoteGo.
5    *
6    * PromoteGo is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * PromoteGo is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with PromoteGo.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.promotego.controllers;
20  
21  import javax.servlet.http.HttpServletRequest;
22  
23  import org.promotego.beans.ScheduledGame;
24  import org.promotego.beans.User;
25  import org.promotego.beans.UserHolder;
26  import org.promotego.dao.interfaces.ScheduledGameDao;
27  import org.promotego.dao.interfaces.UserDao;
28  import org.promotego.editors.BeanIdEditor;
29  import org.promotego.formbackingbeans.MailMessage;
30  import org.promotego.formbackingbeans.MailMessageContext;
31  import org.promotego.formbackingbeans.ScheduledGameMessageContext;
32  import org.springframework.beans.factory.annotation.Required;
33  import org.springframework.mail.MailException;
34  import org.springframework.mail.MailSender;
35  import org.springframework.mail.SimpleMailMessage;
36  import org.springframework.web.bind.ServletRequestDataBinder;
37  import org.springframework.web.servlet.ModelAndView;
38  import org.springframework.web.servlet.mvc.SimpleFormController;
39  
40  public class SendMessageController extends SimpleFormController
41  {
42      private UserHolder m_userHolder;
43      private String m_baseUrl;
44      private MailSender m_mailSender;
45      private UserDao m_userDao;
46      private ScheduledGameDao m_scheduledGameDao;
47  
48      @Override
49      public ModelAndView onSubmit(Object command)
50      {
51          assert m_userHolder.getUser() != null : "User may not be null";
52          
53          MailMessage mailMessage = (MailMessage)command;
54          sendMessage(m_userHolder.getUser(), mailMessage);
55          return new ModelAndView(getSuccessView(), "message", "Your message has been sent to "
56                  + mailMessage.getRecipient().getUsername() + ".");
57      }
58  
59      /***
60       * Adds a <code>BeanEditor</code> for mapping <code>User</code> object to and from
61       * id's.
62       */
63      @Override
64      protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
65      {
66          binder.registerCustomEditor(User.class, new BeanIdEditor<User>(m_userDao));
67          
68          super.initBinder(request, binder);
69      }
70  
71      /***
72       * Send a message to another user.
73       * 
74       * @param scheduledGame The new scheduled game we're notifying about.
75       */
76      private void sendMessage(User sender, MailMessage theMessage) throws MailException
77      {
78          SimpleMailMessage msg = new SimpleMailMessage();
79          msg.setTo(theMessage.getRecipient().getEmailAddress());
80          
81          // TODO Make sender configurable
82          msg.setFrom("noreply@promotego.org");
83          msg.setSubject(theMessage.getMessageContext().getSubject());
84          
85          // TODO Use a velocity template and Mime multipart/alternative to send this message.
86          msg.setText(theMessage.getMessageContext().getSummary() + "\n\n" + theMessage.getMessageContext().getRespondMessage()
87                  + "\n\n--BEGIN MESSAGE--\n" + theMessage.getMessageText()
88                  + "\n--END MESSAGE--\n\n");
89  
90          m_mailSender.send(msg);
91      }
92      
93      @Override
94      public Object formBackingObject(HttpServletRequest request) throws Exception
95      {
96          MailMessage retval = (MailMessage)createCommand();
97          
98          String recipientIdString = request.getParameter("recipientId");
99          User recipient = null;
100         if (recipientIdString != null)
101         {
102             long recipientId = Long.parseLong(recipientIdString);
103             recipient = m_userDao.getById(recipientId);
104             retval.setRecipient(recipient);
105         }
106 
107         if (recipient == null)
108         {
109             throw new IllegalArgumentException("Recipient may not be null");
110         }
111         
112         // For now, only allow messages regarding upcoming scheduled games.
113         // Later, we can extend the concept of MailMessageContext to support
114         // other contexts in which users may want/need to send messages to
115         // one another.  --Alf
116         String scheduledGameIdString = request.getParameter("scheduledGameId");
117         ScheduledGame theScheduledGame = null;
118         if (scheduledGameIdString != null)
119         {
120             long scheduledGameId = Long.parseLong(scheduledGameIdString);
121             theScheduledGame = m_scheduledGameDao.getById(scheduledGameId);
122         }
123 
124         if (theScheduledGame == null)
125         {
126             throw new IllegalArgumentException("Scheduled game may not be null");
127         }
128         
129         if (theScheduledGame.getOfferer().getId() != m_userHolder.getUser().getId()
130                 && theScheduledGame.getAccepter().getId() != m_userHolder.getUser().getId())
131         {
132             throw new IllegalArgumentException("You may only send messages about your own scheduled games");
133         }
134         
135         MailMessageContext theContext = new ScheduledGameMessageContext(m_userHolder.getUser(), theScheduledGame, m_baseUrl);
136         retval.setMessageContext(theContext);
137 
138         return retval;
139     }
140 
141     @Required
142     public void setUserHolder(UserHolder userHolder)
143     {
144         m_userHolder = userHolder;
145     }
146 
147     @Required
148     public void setMailSender(MailSender mailSender)
149     {
150         m_mailSender = mailSender;
151     }
152 
153     @Required
154     public void setBaseUrl(String baseUrl)
155     {
156         m_baseUrl = baseUrl;
157     }
158 
159     @Required
160     public void setUserDao(UserDao userDao)
161     {
162         m_userDao = userDao;
163     }
164 
165     @Required
166     public void setScheduledGameDao(ScheduledGameDao scheduledGameDao)
167     {
168         m_scheduledGameDao = scheduledGameDao;
169     }
170 }