@Override
public Response put(Project project) {
ResponseBuilder response;
EntityTag etag;
HProject hProject = projectDAO.getBySlug(getProjectSlug());
if (hProject == null) { // must be a create operation
response = request.evaluatePreconditions();
if (response != null) {
return response.build();
}
hProject = new HProject();
hProject.setSlug(projectSlug);
// pre-emptive entity permission check
identity.checkPermission(hProject, "insert");
response = Response.created(uri.getAbsolutePath());
}
// Project is obsolete
else if (Objects.equal(hProject.getStatus(), OBSOLETE)) {
return Response.status(Status.FORBIDDEN)
.entity("Project '" + projectSlug + "' is obsolete.")
.build();
}
// Project is ReadOnly
else if (Objects.equal(hProject.getStatus(), READONLY)) {
return Response.status(Status.FORBIDDEN)
.entity("Project '" + projectSlug + "' is read-only.")
.build();
} else {
// must be an update operation
// pre-emptive entity permission check
identity.checkPermission(hProject, "update");
etag = eTagUtils.generateTagForProject(projectSlug);
response = request.evaluatePreconditions(etag);
if (response != null) {
return response.build();
}
response = Response.ok();
}
// null project type accepted for compatibility with old clients
if (project.getDefaultType() != null) {
if (project.getDefaultType().isEmpty()) {
return Response.status(Status.BAD_REQUEST)
.entity("No valid default project type was specified.")
.build();
}
try {
ProjectType.getValueOf(project.getDefaultType());
} catch (Exception e) {
String validTypes =
StringUtils.join(ProjectType.values(), ", ");
return Response
.status(Status.BAD_REQUEST)
.entity("Project type \"" + project.getDefaultType()
+ "\" not valid for this server."
+ " Valid types: [" + validTypes + "]").build();
}
}
transfer(project, hProject);
if (identity != null && hProject.getMaintainers().isEmpty()) {
HAccount hAccount =
accountDAO.getByUsername(identity.getCredentials()
.getUsername());
if (hAccount != null && hAccount.getPerson() != null) {
hProject.getMaintainers().add(hAccount.getPerson());
}
}
projectDAO.makePersistent(hProject);
projectDAO.flush();