/**
*
*/
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.EventType;
/**
* This class represent a query for delete event-types
* @author Nufar Oren
*/
public class DeleteEventTypeQuery extends Query {
/**
*
*/
private static final long serialVersionUID = 4217626862859327334L;
private Long id;
public DeleteEventTypeQuery() {} // required for Gson
/**
* Checks if the manager send this request,
* Checks that the given event-type is valid
* and then delete the event-type 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");
}
EventType eventTypeObject = (EventType) databaseSession.createCriteria(EventType.class)
.add(Restrictions.eq("id", this.id))
.uniqueResult();
if(null == eventTypeObject) {
return new Response(ResponseStatus.FAIL, "Not valid event type");
}
try {
Transaction transaction = databaseSession.beginTransaction();
transaction.begin();
try {
databaseSession.delete(eventTypeObject);
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;
}
}