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