Package protocol

Source Code of protocol.AddEventQuery

package protocol;
import general.DateOperator;
import general.XssHandler;

import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import domain.Event;
import domain.EventId;
import domain.EventType;
import domain.PermissionType;
import domain.RepetitionType;
import domain.User;


/**
* This class represent a query for add events
* @author Nufar Oren
*
*/
public class AddEventQuery extends Query
{
  /**
   *
   */
  private static final long serialVersionUID = -6529794533000247528L;
  private String description;
  private String from; // start date
  private String to; // end date
  private Long eventType;
  private Long permissionType;
  private Long repetitionType;
 
 
  public AddEventQuery() {} // required for Gson

  /**
   * Checks if a user is logged-in,
   * Checks if the given dates are valid,
   * Checks if the event's type, description, repetition and permission are valid
   * and then add the new event to the database
   */
  @Override
  protected Response internalExecute(HttpServletRequest request, Session databaseSession) {
    Response response = null;
    Event event = null;
    HttpSession httpSession = request.getSession();
   
    User userObject = (User) httpSession.getAttribute("currentUser");
    if( null == userObject ) {
      return new Response(ResponseStatus.FAIL, "Problem identifying user");
    }

    Date fromDate = DateOperator.stringToDate(from);
    Date toDate = DateOperator.stringToDate(to);
   
    if( (null == fromDate) || (null == toDate) ) {
      return new Response(ResponseStatus.FAIL, "Dates parsing problem" );
    }
    if( !DateOperator.date1IsBeforeDate2(fromDate, toDate) ) {
      return new Response(ResponseStatus.FAIL, "Start date need to be before end date");
    }
   
    else {
      try {
        EventType eventTypeObject = (EventType) databaseSession.createCriteria(EventType.class).add(Restrictions.eq("id", this.eventType)).uniqueResult();
        if(null == eventTypeObject) {
          return new Response(ResponseStatus.FAIL, "Not valid event type");
        }
       
        EventId eventIdObject = new EventId()
        eventIdObject.setFrom(fromDate);
        eventIdObject.setTo(toDate);
        eventIdObject.setOwner(userObject);
        eventIdObject.setType(eventTypeObject);
       
        PermissionType permisssionTypeObject = (PermissionType) databaseSession.createCriteria(PermissionType.class).add(Restrictions.eq("id", this.permissionType)).uniqueResult();
        if(null == permisssionTypeObject) {
          return new Response(ResponseStatus.FAIL, "Not valid permisssion type");
        }
       
        RepetitionType repetitionTypeObject = (RepetitionType) databaseSession.createCriteria(RepetitionType.class).add(Restrictions.eq("id", this.repetitionType)).uniqueResult();
        if(null == repetitionTypeObject) {
          return new Response(ResponseStatus.FAIL, "Not valid repetition type");
        }
        if( !DateOperator.isRepetitionPossible(fromDate, toDate, repetitionTypeObject) ) {
          return new Response(ResponseStatus.FAIL, "Repetition is not possible. Make sure there is a sense in the repetition type you choose");
        }
       
        this.description = XssHandler.escape(this.description);
       
        event = new Event();
        event.setDescription(this.description);
        event.setPermission(permisssionTypeObject);
        event.setRepetition(repetitionTypeObject);
        event.setId(eventIdObject);
       
        Transaction transaction = databaseSession.beginTransaction();
        try {
          databaseSession.save(event);
          transaction.commit();
          // All was ok
          response = new Response(ResponseStatus.OK);
        } catch(Exception transEx) {
          transaction.rollback();
          response = new Response(ResponseStatus.FAIL, transEx.toString());
        }
      }
      catch (Exception 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.AddEventQuery

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.