Skip to content

Commit

Permalink
Merge pull request #11 from weslleyma/back-end-master
Browse files Browse the repository at this point in the history
Back end master
  • Loading branch information
weslleyma committed Dec 8, 2015
2 parents 6d3727a + abfc214 commit 38d3a4b
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
return;
}
}
if (!httpRequest.getMethod().equals("POST") && !httpRequest.getContextPath().contains("users")) {
if (!httpRequest.getMethod().equals("POST") && (!httpRequest.getContextPath().contains("users")
|| !httpRequest.getContextPath().contains("teachers"))) {
buildErrorResponse(response);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ public User findUserByToken(String token) {
@Override
@SuppressWarnings("unchecked")
public User preInsert(User user) {
String jpql = "from " + entityClass.getSimpleName() + " u where u.username = ?1";
if (user.getTeacher() != null) {
jpql += " or u.teacher = ?2 ";
}
String jpql = "from " + entityClass.getSimpleName() + " u where u.username = ?1 or u.teacher = ?2 ";

Query query = getEntityManager().createQuery(jpql);
query.setParameter(1, user.getUsername());
Expand All @@ -49,7 +46,11 @@ public User preInsert(User user) {
}
List<User> users = (List<User>) query.getResultList();
if (users.size() == 1) {
throw new EntityExistsException();
if (user.getTeacher().getId().equals(users.get(0).getTeacher().getId())) {
throw new EntityExistsException("Teacher already associated with other user.");
} else {
throw new EntityExistsException("Username already used.");
}
}
return super.preInsert(user);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void setWorkload(Integer workload) {
this.workload = workload;
}

@Column(name = "ABOUT")
@Column(name = "ABOUT", columnDefinition="TEXT")
public String getAbout() {
return about;
}
Expand All @@ -100,7 +100,7 @@ public void setAbout(String about) {
this.about = about;
}

@Column(name = "RG")
@Column(name = "RG", length = 15)
public String getRg() {
return rg;
}
Expand All @@ -109,7 +109,7 @@ public void setRg(String rg) {
this.rg = rg;
}

@Column(name = "CPF")
@Column(name = "CPF", length = 20)
public String getCpf() {
return cpf;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package br.ufg.inf.sdd_ufg.model.enums;

public enum HttpHeaders {
SESSION_TOKEN("Session-Token");
LOCATION("Location"), SESSION_TOKEN("Session-Token");

private final String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ protected Response getInsertErrorResponse() {
.build();
}

protected Response getInsertErrorResponse(String msg) {
ErrorResponse errorResponse = new ErrorResponse(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()
, msg);

return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(errorResponse)
.build();
}

protected Response getResourceNotFoundResponse() {
ErrorResponse errorResponse = new ErrorResponse(Response.Status.NOT_FOUND.getStatusCode()
, "Resource Not Found");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ public ClazzScheduleResource(final ClazzScheduleDao clazzScheduleDao) {
@Path("/{id}")
public Response retrieveClazzScheduleById(@PathParam("id") Long id,
@Context final HttpServletRequest request) {
if (getLoggedUser(request) == null) {
return getAuthenticationErrorResponse();
}

ClazzSchedule clazzSchedule = clazzScheduleDao.findById(id, 1);
if (clazzSchedule == null) {
return getResourceNotFoundResponse();
Expand All @@ -54,10 +50,6 @@ public Response retrieveClazzScheduleById(@PathParam("id") Long id,
@GET
public Response retrieveAllClazzSchedules(@QueryParam("page") Integer page,
@Context final HttpServletRequest request) {
if (getLoggedUser(request) == null) {
return getAuthenticationErrorResponse();
}

List<ClazzSchedule> clazzSchedules = clazzScheduleDao.findAll(0);
if (page == null) {
page = 1;
Expand All @@ -71,10 +63,6 @@ public Response retrieveAllClazzSchedules(@QueryParam("page") Integer page,
@POST
public Response insertClazzSchedule(
@Context final HttpServletRequest request, @Context UriInfo info) {
if (getLoggedUser(request) == null) {
return getAuthenticationErrorResponse();
}

ClazzSchedule clazzSchedule;
try {
clazzSchedule = retrieveClazzScheduleFromJson(request);
Expand All @@ -98,10 +86,6 @@ public Response insertClazzSchedule(
@Path("/{id}")
public Response updateClazzSchedule(@PathParam("id") Long id,
@Context final HttpServletRequest request, @Context UriInfo info) {
if (getLoggedUser(request) == null) {
return getAuthenticationErrorResponse();
}

ClazzSchedule clazzSchedule;
try {
clazzSchedule = retrieveClazzScheduleFromJson(request);
Expand Down Expand Up @@ -138,10 +122,6 @@ private ClazzSchedule retrieveClazzScheduleFromJson(
@Path("/{id}")
public Response deleteClazzSchedule(@PathParam("id") Long id,
@Context final HttpServletRequest request) {
if (getLoggedUser(request) == null) {
return getAuthenticationErrorResponse();
}

try {
clazzScheduleDao.delete(id);
} catch (IllegalArgumentException iae) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ public KnowledgeGroupResource(final KnowledgeGroupDao knowledgeGroupDao,
@Path("/{id}")
public Response retrieveKnowledgeGroupById(@PathParam("id") Long id,
@Context final HttpServletRequest request) {
if (getLoggedUser(request) == null) {
return getAuthenticationErrorResponse();
}

KnowledgeGroup knowledgeGroup = knowledgeGroupDao.findById(id, 1);
if (knowledgeGroup == null) {
return getResourceNotFoundResponse();
Expand All @@ -61,9 +57,6 @@ public Response retrieveKnowledgeGroupById(@PathParam("id") Long id,
public Response retrieveAllKnowledgeGroups(
@QueryParam("page") Integer page,
@Context final HttpServletRequest request) {
if (getLoggedUser(request) == null) {
return getAuthenticationErrorResponse();
}

List<KnowledgeGroup> knowledgeGroups = knowledgeGroupDao.findAll(0);
if (page == null) {
Expand All @@ -78,10 +71,6 @@ public Response retrieveAllKnowledgeGroups(
@POST
public Response insertKnowledgeGroup(
@Context final HttpServletRequest request, @Context UriInfo info) {
if (getLoggedUser(request) == null) {
return getAuthenticationErrorResponse();
}

KnowledgeGroup knowledgeGroup;
try {
knowledgeGroup = retrieveKnowledgeGroupFromJson(request);
Expand Down Expand Up @@ -121,10 +110,6 @@ private void updateTeachersKnowledge(KnowledgeGroup kg) {
@Path("/{id}")
public Response updateKnowledgeGroup(@PathParam("id") Long id,
@Context final HttpServletRequest request, @Context UriInfo info) {
if (getLoggedUser(request) == null) {
return getAuthenticationErrorResponse();
}

KnowledgeGroup knowledgeGroup;
try {
knowledgeGroup = retrieveKnowledgeGroupFromJson(request);
Expand Down Expand Up @@ -159,10 +144,6 @@ private KnowledgeGroup retrieveKnowledgeGroupFromJson(
@Path("/{id}")
public Response deleteKnowledgeGroup(@PathParam("id") Long id,
@Context final HttpServletRequest request) {
if (getLoggedUser(request) == null) {
return getAuthenticationErrorResponse();
}

try {
knowledgeGroupDao.delete(id);
} catch (IllegalArgumentException iae) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ public KnowledgeLevelResource(final KnowledgeLevelDao knowledgeLevelDao,
@Path("/{id}")
public Response retrieveKnowledgeLevelById(@PathParam("id") Long id,
@Context final HttpServletRequest request) {
if (getLoggedUser(request) == null) {
return getAuthenticationErrorResponse();
}

KnowledgeLevel knowledgeLevel = knowledgeLevelDao.findById(id, 1);
if (knowledgeLevel == null) {
return getResourceNotFoundResponse();
Expand All @@ -65,10 +61,6 @@ public Response retrieveKnowledgeLevelById(@PathParam("id") Long id,
public Response retrieveAllKnowledgeLevels(
@QueryParam("page") Integer page,
@Context final HttpServletRequest request) {
if (getLoggedUser(request) == null) {
return getAuthenticationErrorResponse();
}

List<KnowledgeLevel> knowledgeLevels = knowledgeLevelDao.findAll(0);
if (knowledgeLevels == null || knowledgeLevels.size() == 0) {
return Response.status(Response.Status.NOT_FOUND).build();
Expand All @@ -85,10 +77,6 @@ public Response retrieveAllKnowledgeLevels(
@POST
public Response insertKnowledgeLevel(
@Context final HttpServletRequest request, @Context UriInfo info) {
if (getLoggedUser(request) == null) {
return getAuthenticationErrorResponse();
}

KnowledgeLevel knowledgeLevel;
try {
knowledgeLevel = retrieveKnowledgeLevelFromJson(request);
Expand All @@ -112,10 +100,6 @@ public Response insertKnowledgeLevel(
@Path("/{id}")
public Response updateKnowledgeLevel(@PathParam("id") Long id,
@Context final HttpServletRequest request, @Context UriInfo info) {
if (getLoggedUser(request) == null) {
return getAuthenticationErrorResponse();
}

KnowledgeLevel existingKL;
try {

Expand Down Expand Up @@ -171,10 +155,6 @@ private KnowledgeLevel retrieveKnowledgeLevelFromJson(
@Path("/{id}")
public Response deleteKnowledgeLevel(@PathParam("id") Long id,
@Context final HttpServletRequest request) {
if (getLoggedUser(request) == null) {
return getAuthenticationErrorResponse();
}

try {
knowledgeLevelDao.delete(id);
} catch (IllegalArgumentException iae) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ public TeacherResource(final TeacherDao teacherDao,
@Path("/{id}")
public Response retrieveTeacherById(@PathParam("id") Long id,
@Context final HttpServletRequest request) {
if (getLoggedUser(request) == null) {
return getAuthenticationErrorResponse();
}

Teacher teacher = teacherDao.findById(id, 2);
if (teacher == null) {
return getResourceNotFoundResponse();
Expand All @@ -62,10 +58,6 @@ public Response retrieveTeacherById(@PathParam("id") Long id,
@GET
public Response retrieveAllTeachers(@QueryParam("page") Integer page,
@Context final HttpServletRequest request) {
if (getLoggedUser(request) == null) {
return getAuthenticationErrorResponse();
}

List<Teacher> teachers = teacherDao.findAll(0);
if (teachers == null || teachers.size() == 0) {
return getResourceNotFoundResponse();
Expand All @@ -82,10 +74,6 @@ public Response retrieveAllTeachers(@QueryParam("page") Integer page,
@POST
public Response insertTeacher(@Context final HttpServletRequest request,
@Context UriInfo info) {
if (getLoggedUser(request) == null) {
return getAuthenticationErrorResponse();
}

Teacher teacher;
try {
teacher = retrieveTeacherFromJson(request);
Expand All @@ -97,7 +85,11 @@ public Response insertTeacher(@Context final HttpServletRequest request,
} catch (Exception e) {
return getBadRequestResponse();
}
return Response.ok(teacher).build();
return Response
.status(Response.Status.CREATED)
.header(HttpHeaders.LOCATION.toString(),
"/teachers/" + teacher.getId())
.entity(teacher).build();
}

private void fillKnowledgeLevels(Teacher teacher) {
Expand All @@ -116,10 +108,6 @@ private void fillKnowledgeLevels(Teacher teacher) {
@Path("/{id}")
public Response updateTeacher(@PathParam("id") Long id,
@Context final HttpServletRequest request, @Context UriInfo info) {
if (getLoggedUser(request) == null) {
return getAuthenticationErrorResponse();
}

Teacher teacher;
try {
teacher = retrieveTeacherFromJson(request);
Expand Down Expand Up @@ -165,10 +153,6 @@ private Teacher retrieveTeacherFromJson(final HttpServletRequest request)
@Path("/{id}")
public Response deleteTeacher(@PathParam("id") Long id,
@Context final HttpServletRequest request) {
if (getLoggedUser(request) == null) {
return getAuthenticationErrorResponse();
}

try {
teacherDao.delete(id);
} catch (IllegalArgumentException iae) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,15 @@ public Response insertUser(@Context final HttpServletRequest request,
user = retrieveUserFromJson(request);
userDao.insert(user);
} catch (EntityExistsException eee) {
return getInsertErrorResponse();
return getInsertErrorResponse(eee.getMessage());
} catch (Exception e) {
return getBadRequestResponse();
}

URI location = info.getBaseUriBuilder().path("/users")
.path(user.getId().toString()).build();
return Response
.created(location)
.header(HttpHeaders.SESSION_TOKEN.toString(),
getLoggedUser(request).getSessionToken())
.status(Response.Status.CREATED)
.header(HttpHeaders.LOCATION.toString(),
"/users/" + user.getId())
.entity(user).build();
}

Expand All @@ -100,7 +98,7 @@ public Response updateUser(@PathParam("id") Long id,

userDao.update(existingUser);
} catch (EntityExistsException eee) {
return getInsertErrorResponse();
return getInsertErrorResponse(eee.getMessage());
} catch (IllegalArgumentException iae) {
return getResourceNotFoundResponse();
} catch (Exception e) {
Expand All @@ -121,6 +119,8 @@ private User retrieveUserFromJson(final HttpServletRequest request)
User user = new User();
if (content.get("username") != null) {
user.setUsername(content.get("username").toString());
} else if (request.getMethod().equals("POST")) {
throw new NullPointerException();
}
user.setPassword(content.get("password").toString());
user.setEmail(content.get("email").toString());
Expand All @@ -129,11 +129,13 @@ private User retrieveUserFromJson(final HttpServletRequest request)
} else {
user.setIsAdmin(false);
}
if (content.get("teacher_id") != null) {
Teacher teacher = teacherDao.findById(
new Long(content.get("teacher_id").toString()), 0);
user.setTeacher(teacher);

Teacher teacher = teacherDao.findById(
new Long(content.get("teacher_id").toString()), 0);
if (teacher == null) {
throw new NullPointerException();
}
user.setTeacher(teacher);

return user;
}
Expand Down

0 comments on commit 38d3a4b

Please sign in to comment.