/**
*
*/
package protocol;
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.EventType;
/**
* @author Nufar Oren
*
*/
public class EditEventTypeQuery extends Query
{
/**
*
*/
private static final long serialVersionUID = 1898632870192382727L;
private Long id;
private String description;
private String color;
public EditEventTypeQuery() {} // required for Gson
/**
* Checks if the manager send this request,
* Checks if the given id, description and color are valid
* and then update the event-type in the database
*/
@Override
protected Response internalExecute(HttpServletRequest request, Session databaseSession) {
Response response = null;
EventType oldEventType = 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 {
oldEventType = (EventType) databaseSession.createCriteria(EventType.class)
.add(Restrictions.eq("id", this.id))
.uniqueResult();
if (null == oldEventType) {
throw new IllegalArgumentException("The user you are editing was not found.");
}
Transaction transaction = databaseSession.beginTransaction();
oldEventType.setColor(this.color);
oldEventType.setDescription(this.description);
databaseSession.update(oldEventType);
transaction.commit();
response = new Response(ResponseStatus.OK);
}
catch (Exception ex) {
// if there was an error, it'll be set here.
response = new Response(ResponseStatus.FAIL, ex.toString());
}
return response;
}
}