202 lines
6.8 KiB
Java
202 lines
6.8 KiB
Java
package org.gcube.portal.social.networking.ws.lib;
|
|
|
|
|
|
import com.webcohesion.enunciate.metadata.Ignore;
|
|
import com.webcohesion.enunciate.metadata.rs.RequestHeader;
|
|
import com.webcohesion.enunciate.metadata.rs.RequestHeaders;
|
|
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
|
|
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
|
|
import org.gcube.portal.databook.shared.Comment;
|
|
import org.gcube.portal.social.networking.ws.outputs.ResponseBean;
|
|
import org.gcube.portal.social.networking.ws.utils.CassandraConnection;
|
|
import org.gcube.portal.social.networking.ws.utils.ErrorMessages;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import javax.validation.Valid;
|
|
import javax.validation.ValidationException;
|
|
import javax.validation.constraints.Min;
|
|
import javax.validation.constraints.NotNull;
|
|
import javax.ws.rs.*;
|
|
import javax.ws.rs.core.MediaType;
|
|
import javax.ws.rs.core.Response;
|
|
import javax.ws.rs.core.Response.Status;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* REST interface for the social networking library (comments).
|
|
* @author Ahmed Ibrahim ISTI-CNR
|
|
*/
|
|
@Path("lib/comments")
|
|
@RequestHeaders ({
|
|
@RequestHeader( name = "Authorization", description = "Bearer token, see https://dev.d4science.org/how-to-access-resources"),
|
|
@RequestHeader( name = "Content-Type", description = "application/json")
|
|
})
|
|
@Ignore
|
|
public class Comments {
|
|
|
|
// Logger
|
|
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Comments.class);
|
|
|
|
//lib api
|
|
@POST
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
@Path("add-comment-lib")
|
|
public Response addCommentLib(@NotNull(message="Comment to write is missing")
|
|
@Valid
|
|
Comment comment)throws ValidationException {
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
try {
|
|
logger.info("Writing comment with commentid " + comment.getKey());
|
|
boolean result = CassandraConnection.getInstance().getDatabookStore().addComment(comment);
|
|
responseBean.setResult(result);
|
|
responseBean.setMessage("");
|
|
responseBean.setSuccess(result);
|
|
} catch (Exception e) {
|
|
logger.error("Unable to write comment.", e);
|
|
responseBean.setMessage(e.getMessage());
|
|
responseBean.setSuccess(false);
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
|
}
|
|
return Response.status(status).entity(responseBean).build();
|
|
}
|
|
|
|
@GET
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
@Path("read-comment-by-id-lib")
|
|
public Response readCommentByIdLib(@QueryParam("commentid")
|
|
String commentid)throws ValidationException{
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
try{
|
|
logger.info("Getting comment with commentid " + commentid);
|
|
Comment result = CassandraConnection.getInstance().getDatabookStore().readCommentById(commentid);
|
|
responseBean.setResult(result);
|
|
responseBean.setMessage("");
|
|
responseBean.setSuccess(true);
|
|
}catch(Exception e){
|
|
logger.error("Unable to find comment.", e);
|
|
responseBean.setMessage(e.getMessage());
|
|
responseBean.setSuccess(false);
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
|
}
|
|
return Response.status(status).entity(responseBean).build();
|
|
}
|
|
|
|
@GET
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
@Path("get-comments-by-post-id-lib")
|
|
@StatusCodes ({
|
|
@ResponseCode ( code = 200, condition = "The list of comments is put into the 'result' field"),
|
|
@ResponseCode ( code = 500, condition = ErrorMessages.ERROR_IN_API_RESULT)
|
|
})
|
|
public Response getAllCommentsByPostIdLib(
|
|
@NotNull
|
|
@QueryParam("postid")
|
|
String postid) {
|
|
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
List<Comment> comments = null;
|
|
|
|
try{
|
|
logger.info("Retrieving comments for postid " + postid);
|
|
comments = CassandraConnection.getInstance().getDatabookStore().getAllCommentByPost(postid);
|
|
responseBean.setResult(comments);
|
|
responseBean.setSuccess(true);
|
|
}catch(Exception e){
|
|
logger.error("Unable to retrieve such comments.", e);
|
|
responseBean.setMessage(e.getMessage());
|
|
responseBean.setSuccess(false);
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
|
}
|
|
|
|
return Response.status(status).entity(responseBean).build();
|
|
}
|
|
|
|
@GET
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
@Path("get-recent-comments-by-user-and-date-lib")
|
|
public Response getRecentCommentsByUserAndDateLib(
|
|
@QueryParam("time")
|
|
@Min(value = 0, message="time cannot be negative")
|
|
long timeInMillis,
|
|
@QueryParam("username")
|
|
String username
|
|
) throws ValidationException{
|
|
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
List<Comment> comments = null;
|
|
|
|
try{
|
|
logger.info("Retrieving comments for user id " + username);
|
|
comments = CassandraConnection.getInstance().getDatabookStore().getRecentCommentsByUserAndDate(username, timeInMillis);
|
|
responseBean.setResult(comments);
|
|
responseBean.setMessage("");
|
|
responseBean.setSuccess(true);
|
|
}catch(Exception e){
|
|
logger.error("Unable to retrieve such comments.", e);
|
|
responseBean.setMessage(e.getMessage());
|
|
responseBean.setSuccess(false);
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
|
}
|
|
return Response.status(status).entity(responseBean).build();
|
|
}
|
|
|
|
|
|
@POST
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
@Path("edit-comment-lib")
|
|
public Response editCommentLib(
|
|
@NotNull(message="Comment to edit is missing")
|
|
@Valid
|
|
Comment comment
|
|
) throws ValidationException{
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
try{
|
|
logger.info("Writing comment with commentid " + comment.getKey());
|
|
boolean result = CassandraConnection.getInstance().getDatabookStore().editComment(comment);
|
|
responseBean.setResult(result);
|
|
responseBean.setMessage("");
|
|
responseBean.setSuccess(result);
|
|
}catch(Exception e){
|
|
logger.error("Unable to write comment.", e);
|
|
responseBean.setMessage(e.getMessage());
|
|
responseBean.setSuccess(false);
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
|
}
|
|
return Response.status(status).entity(responseBean).build();
|
|
}
|
|
|
|
@POST
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
@Path("delete-comment-lib")
|
|
public Response deleteCommentLib(
|
|
@QueryParam("commentid")
|
|
String commentid,
|
|
@QueryParam("postid")
|
|
String postid
|
|
) throws ValidationException{
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
try{
|
|
logger.info("deleting comment with commentid " + commentid);
|
|
boolean result = CassandraConnection.getInstance().getDatabookStore().deleteComment(commentid, postid);
|
|
responseBean.setResult(result);
|
|
responseBean.setMessage("");
|
|
responseBean.setSuccess(result);
|
|
}catch(Exception e){
|
|
logger.error("Unable to delete comment.", e);
|
|
responseBean.setMessage(e.getMessage());
|
|
responseBean.setSuccess(false);
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
|
}
|
|
return Response.status(status).entity(responseBean).build();
|
|
}
|
|
}
|