Package protocol

Source Code of protocol.AddUserQuery

/**
*
*/
package protocol;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import domain.Role;
import domain.User;

/**
* This class represent a query for add users
* @author Nufar Oren
*/
public class AddUserQuery extends Query {
 
  /**
   *
   */
  private static final long serialVersionUID = -8536992559308011637L;

  private String username;
  private String password;
  private String fullname;
  private Long role;
 
 
  public AddUserQuery() {} // required for Gson
 
 
  /**
   * Checks if the manager send this request,
   * Checks if the given fullname, password, username and role are valid
   * and then add the new user to the database
   */
  @Override
  protected Response internalExecute(HttpServletRequest request, Session databaseSession) {
 
    Response response = null;
    User user = null;
    HttpSession httpSession = request.getSession();
   
    // if user is not a manager, he can't continue
    if( ! ((Boolean) httpSession.getAttribute("manager")) ) {
     
      return new Response(ResponseStatus.FAIL, "No authorization");
    }
   
   
    try {
      Role userRole = (Role) databaseSession.createCriteria(Role.class).add(Restrictions.eq("id", this.role)).uniqueResult();
     
      if (null == userRole) {
        return new Response(ResponseStatus.FAIL, "User role does not exist, database failure.");
      }
     
      user = new User();
     
      if( (null == this.fullname) || (0 == this.fullname.length()) ) {
        return new Response(ResponseStatus.FAIL, "Descrition is not valid");
      }
      if( (null == this.password) || (0 == this.password.length()) ) {
        return new Response(ResponseStatus.FAIL, "Color is not valid");
      }
      if( (null == this.username) || (0 == this.username.length()) ) {
        return new Response(ResponseStatus.FAIL, "Color is not valid");
      }
     
      user.setRole(userRole);
      user.setFullname(this.fullname);
      user.setPassword(this.password);
      user.setUsername(this.username);

   
      Transaction transaction = databaseSession.beginTransaction();
      try {
        databaseSession.save(user);
        transaction.commit();
       
        // All was ok
        response = new Response(ResponseStatus.OK);

        // "LogIn" the user to the session
        httpSession.setAttribute("currentUser", user);
      } catch(Exception transEx) {
        transaction.rollback();
        response = new Response(ResponseStatus.FAIL, transEx.toString());
      }
     
    } catch (HibernateException ex) {
      // if there was an error, it'll be set here.
      response = new Response(ResponseStatus.FAIL, ex.toString());
    }
   
    return response; 
  }
}
TOP

Related Classes of protocol.AddUserQuery

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.