/**
*
*/
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 domain.EventType;
/**
* This class represent a query for add event-type, like: Party, Meeting...
* @author Nufar Oren
*/
public class AddEventTypeQuery extends Query
{
/**
*
*/
private static final long serialVersionUID = -4910706814710152759L;
private String description;
private String color;
public AddEventTypeQuery() {} // required for Gson
/**
* Checks if the manager send this request,
* Checks if the given description and color are valid
* and then add the new event-type to the database
*/
@Override
protected Response internalExecute(HttpServletRequest request, Session databaseSession) {
Response response = null;
EventType eventTypeObject = 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");
}
eventTypeObject = new EventType();
if( (null == this.description) || (0 == this.description.length()) ) {
return new Response(ResponseStatus.FAIL, "Descrition is not valid");
}
if( (null == this.color) || (0 == this.color.length()) ) {
return new Response(ResponseStatus.FAIL, "Color is not valid");
}
eventTypeObject.setDescription(this.description);
eventTypeObject.setColor(this.color);
try {
Transaction transaction = databaseSession.beginTransaction();
try {
databaseSession.save(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;
}
}