fix jpa lock query
[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.domain.UserClass;
22 import gr.ebs.gss.server.ejb.TransactionHelper;
23
24 import java.io.IOException;
25 import java.util.concurrent.Callable;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 /**
31  * The servlet that handles user registration.
32  *
33  * @author past
34  */
35 public class CouponVerifier extends BaseServlet {
36         /**
37          * The request parameter name for the verification flag.
38          */
39         private static final String VERIFY_PARAM = "verify";
40
41         /**
42          * The request parameter name for the coupon code.
43          */
44         private static final String CODE_PARAM = "code";
45
46         /**
47          * The request parameter name for the firstname.
48          */
49         private static final String FIRSTNAME_PARAM = "firstname";
50
51         /**
52          * The request parameter name for the lastname.
53          */
54         private static final String LASTNAME_PARAM = "lastname";
55
56         /**
57          * The request parameter name for the username.
58          */
59         private static final String USERNAME_PARAM = "username";
60
61         /**
62          * The request parameter name for the e-mail.
63          */
64         private static final String EMAIL_PARAM = "email";
65
66         /**
67          * The serial version UID of the class.
68          */
69         private static final long serialVersionUID = 1L;
70
71         @Override
72         public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
73                 String firstname = request.getParameter(FIRSTNAME_PARAM);
74                 String lastname = request.getParameter(LASTNAME_PARAM);
75                 String email = request.getParameter(EMAIL_PARAM);
76                 final String username = request.getParameter(USERNAME_PARAM);
77                 final String code = request.getParameter(CODE_PARAM);
78                 String verify = request.getParameter(VERIFY_PARAM);
79                 response.setContentType("text/html");
80
81                 // Validate input parameters.
82                 if (username == null || username.isEmpty()) {
83                         handleException(response, "No username was specified");
84                         return;
85                 } else if (firstname == null || firstname.isEmpty()) {
86                         handleException(response, "No firstname was specified");
87                         return;
88                 } else if (lastname == null || lastname.isEmpty()) {
89                         handleException(response, "No lastname was specified");
90                         return;
91                 } else if (email == null || email.isEmpty()) {
92                         handleException(response, "No email was specified");
93                         return;
94                 } else if (code == null || code.isEmpty()) {
95                         handleException(response, "No code was specified");
96                         return;
97                 } else if (!"on".equalsIgnoreCase(verify)) {
98                         String error = encode("You must verify that the coupon belongs to you");
99                         String errorUrl = "couponSubmission.jsp?error=" + error;
100                         errorUrl += "&username=" + username;
101                         errorUrl += "&firstname=" + firstname;
102                         errorUrl += "&lastname=" + lastname;
103                         errorUrl += "&code=" + code;
104                         errorUrl += "&email=" + email;
105                         response.sendRedirect(errorUrl);
106                         return;
107                 }
108
109                 try {
110                         UserClass newClass = new TransactionHelper<UserClass>().tryExecute(new Callable<UserClass>() {
111                                 @Override
112                                 public UserClass call() throws Exception {
113                                         return getService().upgradeUserClass(username, code);
114                                 }
115
116                         });
117                         response.sendRedirect("couponSubmitted.jsp?newQuota=" + newClass.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 }