package protocol;
import javax.servlet.http.HttpServletRequest;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import domain.User;
/**
* This class represent a query for check if a specific user is exist
* @author Yury Michurin
*
*/
public class UserExistsQuery extends Query {
private static final long serialVersionUID = 3683075132628180631L;
private String username;
// required for GSON
public UserExistsQuery() {}
/**
* Checks if the given username is already exist in the database
*/
@Override
protected Response internalExecute(HttpServletRequest request, Session databaseSession) {
Response response = null;
User user = null;
try {
user = (User) databaseSession.createCriteria(User.class).add(Restrictions.eq("username", this.username)).uniqueResult();
if (null != user) { // user exists
response = new UserExistsResponse(ResponseStatus.OK, "", true);
} else { // user does not exists
response = new UserExistsResponse(ResponseStatus.OK, "", false);
}
} catch (HibernateException ex) {
// if there was an error, it'll be set here.
response = new UserExistsResponse(ResponseStatus.FAIL, ex.toString(), true);
}
// return the response
return response;
}
}