Package protocol

Source Code of protocol.DeleteUserQuery

/**
*
*/
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.User;

/**
*  This class represent a query for delete users
* @author Nufar Oren
*/
public class DeleteUserQuery extends Query {
 
  /**
   *
   */
  private static final long serialVersionUID = 5685540309043006427L;
  private Long id;
 

  public DeleteUserQuery() {} // required for Gson
 
  /**
   * Checks if the manager send this request,
   * Checks that the given user is valid
   * and then delete the user from the database
   */
  @Override
  protected Response internalExecute(HttpServletRequest request, Session databaseSession) {
   
    Response response = 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");
    }
   
   
    User userObject = (User) databaseSession.createCriteria(User.class)
                .add(Restrictions.eq("id", this.id))
                .uniqueResult();
    if(null == userObject) {
      return new Response(ResponseStatus.FAIL, "Not valid user");
    }
   
    try {
      Transaction transaction = databaseSession.beginTransaction();
      transaction.begin();
     
      try {
        databaseSession.delete(userObject);
        transaction.commit();
        // All was ok
        response = new Response(ResponseStatus.OK);
      } 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.DeleteUserQuery

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.