Complete quota upgrades with coupons. Also fix equals() and hashCode() for Invitation...
[pithos] / src / gr / ebs / gss / server / CouponVerifier.java
1 /*
2  * Copyright 2010 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
6  * GSS 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  * GSS 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 GSS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 package gr.ebs.gss.server;
20
21 import gr.ebs.gss.server.ejb.TransactionHelper;
22
23 import java.io.IOException;
24 import java.util.concurrent.Callable;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 /**
30  * The servlet that handles user registration.
31  *
32  * @author past
33  */
34 public class CouponVerifier extends BaseServlet {
35         /**
36          * The request parameter name for the verification flag.
37          */
38         private static final String VERIFY_PARAM = "verify";
39
40         /**
41          * The request parameter name for the coupon code.
42          */
43         private static final String CODE_PARAM = "code";
44
45         /**
46          * The request parameter name for the firstname.
47          */
48         private static final String FIRSTNAME_PARAM = "firstname";
49
50         /**
51          * The request parameter name for the lastname.
52          */
53         private static final String LASTNAME_PARAM = "lastname";
54
55         /**
56          * The request parameter name for the username.
57          */
58         private static final String USERNAME_PARAM = "username";
59
60         /**
61          * The request parameter name for the e-mail.
62          */
63         private static final String EMAIL_PARAM = "email";
64
65         /**
66          * The serial version UID of the class.
67          */
68         private static final long serialVersionUID = 1L;
69
70         @Override
71         public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
72                 String firstname = request.getParameter(FIRSTNAME_PARAM);
73                 String lastname = request.getParameter(LASTNAME_PARAM);
74                 String email = request.getParameter(EMAIL_PARAM);
75                 final String username = request.getParameter(USERNAME_PARAM);
76                 final String code = request.getParameter(CODE_PARAM);
77                 String verify = request.getParameter(VERIFY_PARAM);
78                 response.setContentType("text/html");
79
80                 // Validate input parameters.
81                 if (username == null || username.isEmpty()) {
82                         handleException(response, "No username was specified");
83                         return;
84                 } else if (firstname == null || firstname.isEmpty()) {
85                         handleException(response, "No firstname was specified");
86                         return;
87                 } else if (lastname == null || lastname.isEmpty()) {
88                         handleException(response, "No lastname was specified");
89                         return;
90                 } else if (email == null || email.isEmpty()) {
91                         handleException(response, "No email was specified");
92                         return;
93                 } else if (code == null || code.isEmpty()) {
94                         handleException(response, "No code was specified");
95                         return;
96                 } else if (!"on".equalsIgnoreCase(verify)) {
97                         String error = encode("You must verify that the coupon belongs to you");
98                         String errorUrl = "couponSubmission.jsp?error=" + error;
99                         errorUrl += "&username=" + username;
100                         errorUrl += "&firstname=" + firstname;
101                         errorUrl += "&lastname=" + lastname;
102                         errorUrl += "&code=" + code;
103                         errorUrl += "&email=" + email;
104                         response.sendRedirect(errorUrl);
105                         return;
106                 }
107
108                 try {
109                         new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
110                                 @Override
111                                 public Void call() throws Exception {
112                                         getService().upgradeUserClass(username, code);
113                                         return null;
114                                 }
115
116                         });
117                         response.sendRedirect("couponSubmitted.jsp?newQuota=" + getService().getCouponUserClass().getQuotaAsString());
118                 } catch (Exception e) {
119                         handleException(response, e.getMessage());
120                 }
121         }
122
123         private void handleException(HttpServletResponse response, String error) throws IOException {
124                 String errorUrl = "coupon.jsp?error=" + error;
125                 response.sendRedirect(errorUrl);
126         }
127 }