View Javadoc
1   /*
2    * 
3    * $Revision: 1.18 $
4    * $Date: 2005/03/15 21:16:20 $
5    *
6    * ====================================================================
7    * struts-it
8    * Copyright (C) 2004-2005 - Manfred Wolff and the strutsit community
9    * 
10   * Licensed under the Apache License, Version 2.0 (the "License");
11   * you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   * 
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   * 
16   * Unless required by applicable law or agreed to in writing, software
17   * distributed under the License is distributed on an "AS IS" BASIS,
18   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   * See the License for the specific language governing permissions and
20   * limitations under the License.
21   *
22   * Projekt       : struts-it solutions for webbased projects
23   * Part          : demo application for the struts-it architecture
24   * Created       : 26.02.2005 mwolff
25   */
26  package org.struts.velocity.demo.service;
27  
28  import java.util.Date;
29  import java.util.List;
30  
31  import net.sf.hibernate.HibernateException;
32  import net.sf.hibernate.Query;
33  import net.sf.hibernate.Session;
34  
35  import org.apache.commons.chain.Context;
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  import org.struts.velocity.demo.login.AddAccountForm;
39  import org.struts.velocity.demo.persistence.Access;
40  import org.struts.velocity.demo.util.Constants;
41  import org.strutsit.architecture.hibernate.HibernateContext;
42  import org.strutsit.architecture.util.MD5Generator;
43  import org.strutsit.architecture.util.RandomChars;
44  import org.strutsit.examples.service.EmailService;
45  
46  /***
47   * @author <a href="mailto:wolff@struts-it.de">Manfred Wolff</a>
48   * @since JDK 1.4
49   * @version $Revision: 1.18 $
50   */
51  public class LoginService implements org.strutsit.architecture.service.Service {
52  
53      // the logger for this class
54      private static Log log = LogFactory.getLog(LoginService.class);
55  
56      /*** The Emailservice to use */
57      private EmailService eservice;
58  
59      /***
60       * This service provides the following action:
61       * <ul>
62       * <li>LOGIN_ACOUNT_EXISTS: Prooves if an account already exists.</li>
63       * <li>LOGIN_NEW_ACOUNT: Creates a new account.</li>
64       * </ul>
65       */
66      public void execute(Context context) throws Exception {
67  
68          if (context == null) {
69              throw new IllegalArgumentException("Context not exists.");
70          }
71          
72          String action = ((HibernateContext) context).getAction();
73  
74          if (Constants.LOGIN_ACOUNT_EXISTS.equals(action)) {
75              ((HibernateContext) context)
76                      .setResult(proofAccount((HibernateContext) context));
77          }
78          if (Constants.LOGIN_NEW_ACOUNT.equals(action)) {
79              saveAccount((HibernateContext) context);
80          }
81  
82          if (Constants.LOGIN_COMPARE_PASSWD.equals(action)) {
83              ((HibernateContext) context)
84                      .setResult(comparePasswd((HibernateContext) context));
85          }
86      }
87  
88      /***
89       * @param context
90       * @return
91       */
92      private boolean proofAccount(HibernateContext context) throws Exception {
93  
94          if (log.isTraceEnabled()) {
95              log.trace("proofAccount -> START");
96          }
97  
98          Session session = context.getHibSession();
99  
100         if (session == null) {
101             throw new IllegalArgumentException("Hibernate session not exists.");
102         }
103 
104         // getting infos
105         AddAccountForm form = (AddAccountForm) context.getForm();
106 
107         Query query = null;
108         try {
109             query = session
110                     .createQuery(""
111                             + "from access in class org.struts.velocity.demo.persistence.Access "
112                             + "where access.loginname = :loginName");
113         } catch (Exception e) {
114             log.error(e);
115         }
116 
117         query.setProperties(form);
118 
119         List retVal = query.list();
120 
121         if (log.isTraceEnabled()) {
122             log.trace("proofAccount -> END");
123         }
124 
125         return (retVal.size() != 0);
126     }
127 
128     /***
129      * @param context
130      * @throws HibernateException
131      * @TODO ErrorHandling
132      */
133     private void saveAccount(HibernateContext context) throws Exception {
134 
135         if (log.isTraceEnabled()) {
136             log.trace("saveAccount -> START");
137         }
138 
139         Session session = context.getHibSession();
140 
141         // be pessimistic
142         context.setResult(false);
143 
144         // Getting the form
145         AddAccountForm form = (AddAccountForm) context.getForm();
146 
147         // Create a random password
148         String passwd = RandomChars.getPasswd(8);
149 
150         Access access = new Access();
151         access.setCreated(new Date());
152         access.setName(form.getName());
153         access.setPreName(form.getPrename());
154         access.setEmail(form.getEmail());
155         access.setLoginname(form.getLoginName());
156         access.setPassword(MD5Generator.hash(passwd));
157         try {
158             session.saveOrUpdate(access);
159             session.flush();
160         } catch (HibernateException e) {
161             // @TODO ERRORHANDLING
162             throw new Exception(e);
163         }
164 
165         // sending the password to the Email address
166         EmailService service = getEservice();
167         service.send(form.getEmail(), form.getPrename() + " " + form.getName(),
168                 "Your new password", "Your password is: " + passwd + ".");
169 
170         context.setResult(true);
171 
172         if (log.isTraceEnabled()) {
173             log.trace("saveAccount -> END");
174         }
175     }
176 
177     private boolean comparePasswd(HibernateContext context) throws Exception {
178 
179         // look if there is a username / passwd combination in the database
180         Session session = context.getHibSession();
181 
182         if (session == null) {
183             throw new IllegalArgumentException("Hibernate session not exists.");
184         }
185 
186         // getting infos
187         AddAccountForm form = (AddAccountForm) context.getForm();
188 
189         if (form == null) {
190             throw new IllegalArgumentException("From in session not exists.");
191         }
192 
193         form.setPassword(MD5Generator.hash(form.getPassword()));
194 
195         Query query = null;
196         try {
197             query = session
198                     .createQuery("from access in class org.struts.velocity.demo.persistence.Access "
199                             + "where (access.loginname = :loginName) and (access.password = :password)");
200             query.setProperties(form);
201         } catch (Exception e) {
202             log.error(e);
203         }
204 
205         return query.list().size() > 0;
206     }
207 
208     /***
209      * @return Returns the eservice.
210      */
211     public EmailService getEservice() {
212         return eservice;
213     }
214 
215     /***
216      * This service will be set by hivemind.
217      * 
218      * @param eservice The eservice to set.
219      */
220     public void setEservice(EmailService eservice) {
221 
222         if (log.isTraceEnabled()) {
223             log.trace("setEservice -> START");
224         }
225 
226         this.eservice = eservice;
227 
228         if (log.isTraceEnabled()) {
229             log.trace("setEservice -> END");
230         }
231     }
232 }
233