486 lines
15 KiB
Java
486 lines
15 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 org.gcube.portal.databook.shared.*;
|
|
import org.gcube.portal.social.networking.ws.outputs.ResponseBean;
|
|
import org.gcube.portal.social.networking.ws.utils.CassandraConnection;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import javax.validation.Valid;
|
|
import javax.validation.ValidationException;
|
|
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 (posts).
|
|
* @author Ahmed Ibrahim ISTI-CNR
|
|
*/
|
|
@Path("lib/posts")
|
|
@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 Posts {
|
|
|
|
// Logger
|
|
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Posts.class);
|
|
|
|
//library api
|
|
@POST
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
@Path("save-user-post-lib")
|
|
public Response saveUserPostLib(
|
|
@NotNull(message="post to add is missing")
|
|
@Valid
|
|
Post post
|
|
) throws ValidationException{
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
try{
|
|
boolean result = CassandraConnection.getInstance().getDatabookStore().saveUserPost(post);
|
|
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("save-user-post-attachments-lib")
|
|
public Response saveUserPostLib(
|
|
@NotNull(message="post to add is missing")
|
|
@Valid
|
|
PostWithAttachment postWithAttachment
|
|
) throws ValidationException{
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
try{
|
|
Post post = postWithAttachment.getPost();
|
|
logger.info("Post is " + post);
|
|
List<Attachment> attachments = postWithAttachment.getAttachments();
|
|
logger.info("Attachments are " + attachments);
|
|
boolean result = CassandraConnection.getInstance().getDatabookStore().saveUserPost(post,attachments);
|
|
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("save-app-post-attachments-lib")
|
|
public Response saveAppPostLib(
|
|
@NotNull(message="post to add is missing")
|
|
@Valid
|
|
PostWithAttachment postWithAttachment
|
|
) throws ValidationException{
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
try{
|
|
Post post = postWithAttachment.getPost();
|
|
List<Attachment> attachments = postWithAttachment.getAttachments();
|
|
boolean result = CassandraConnection.getInstance().getDatabookStore().saveAppPost(post,attachments);
|
|
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("save-app-post-lib")
|
|
public Response saveAppPostLib(
|
|
@NotNull(message="post to add is missing")
|
|
@Valid
|
|
Post post
|
|
) throws ValidationException{
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
try{
|
|
boolean result = CassandraConnection.getInstance().getDatabookStore().saveAppPost(post);
|
|
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("save-post-to-vretimeline-lib")
|
|
public Response savePostToVRETimelineLib(
|
|
@QueryParam("postid")
|
|
String postid,
|
|
@QueryParam("vreid")
|
|
String vreid
|
|
) throws ValidationException{
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
try{
|
|
boolean result = CassandraConnection.getInstance().getDatabookStore().savePostToVRETimeline(postid, vreid);
|
|
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-post-lib")
|
|
public Response readPostLib(
|
|
@QueryParam("postid")
|
|
String postid
|
|
) throws ValidationException{
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
try{
|
|
Post result = CassandraConnection.getInstance().getDatabookStore().readPost(postid);
|
|
responseBean.setResult(result);
|
|
responseBean.setMessage("");
|
|
responseBean.setSuccess(true);
|
|
}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("get-recent-posts-by-user-date-lib")
|
|
public Response getRecentPostsByUserAndDateLib(
|
|
@QueryParam("userid")
|
|
String userid,
|
|
@QueryParam("time")
|
|
long timeinmillis
|
|
) throws ValidationException{
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
try{
|
|
List<Post> result = CassandraConnection.getInstance().getDatabookStore().getRecentPostsByUserAndDate(userid, timeinmillis);
|
|
responseBean.setResult(result);
|
|
responseBean.setMessage("");
|
|
responseBean.setSuccess(true);
|
|
}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-post-lib")
|
|
public Response deletePostLib(
|
|
@QueryParam("postid")
|
|
String postid
|
|
) throws ValidationException{
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
try{
|
|
boolean result = CassandraConnection.getInstance().getDatabookStore().deletePost(postid);
|
|
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("get-all-posts-by-user-lib")
|
|
public Response getAllPostsByUserLib(
|
|
@QueryParam("userid")
|
|
String userid
|
|
) throws ValidationException{
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
try{
|
|
List<Post> result = CassandraConnection.getInstance().getDatabookStore().getAllPostsByUser(userid);
|
|
responseBean.setResult(result);
|
|
responseBean.setMessage("");
|
|
responseBean.setSuccess(true);
|
|
}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("get-all-posts-by-app-lib")
|
|
public Response getAllPostsByAppLib(
|
|
@QueryParam("appid")
|
|
String appid
|
|
) throws ValidationException{
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
try{
|
|
List<Post> result = CassandraConnection.getInstance().getDatabookStore().getAllPostsByApp(appid);
|
|
responseBean.setResult(result);
|
|
responseBean.setMessage("");
|
|
responseBean.setSuccess(true);
|
|
}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("get-recent-commented-posts-by-user-lib")
|
|
public Response getRecentCommentedPostsByUserAndDateLib(
|
|
@QueryParam("userid")
|
|
String userid,
|
|
@QueryParam("time")
|
|
long timeinmillis
|
|
) throws ValidationException{
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
try{
|
|
List<Post> result = CassandraConnection.getInstance().getDatabookStore().getRecentCommentedPostsByUserAndDate(userid, timeinmillis);
|
|
responseBean.setResult(result);
|
|
responseBean.setMessage("");
|
|
responseBean.setSuccess(true);
|
|
}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("get-portal-privacy-level-posts-lib")
|
|
public Response getAllPortalPrivacyLevelPostsLib() throws ValidationException{
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
try{
|
|
List<Post> result = CassandraConnection.getInstance().getDatabookStore().getAllPortalPrivacyLevelPosts();
|
|
responseBean.setResult(result);
|
|
responseBean.setMessage("");
|
|
responseBean.setSuccess(true);
|
|
}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("get-recent-posts-by-user-lib")
|
|
public Response getRecentPostsByUserLib(
|
|
@QueryParam("userid")
|
|
String userid,
|
|
@QueryParam("quantity")
|
|
int quantity
|
|
) throws ValidationException{
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
try{
|
|
List<Post> result = CassandraConnection.getInstance().getDatabookStore().getRecentPostsByUser(userid, quantity);
|
|
responseBean.setResult(result);
|
|
responseBean.setMessage("");
|
|
responseBean.setSuccess(true);
|
|
}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("get-all-posts-by-vre-lib")
|
|
public Response getAllPostsByVRELib(
|
|
@QueryParam("vreid")
|
|
String vreid
|
|
) throws ValidationException{
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
try{
|
|
List<Post> result = CassandraConnection.getInstance().getDatabookStore().getAllPostsByVRE(vreid);
|
|
responseBean.setResult(result);
|
|
responseBean.setMessage("");
|
|
responseBean.setSuccess(true);
|
|
}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("get-recent-posts-by-vre-lib")
|
|
public Response getRecentPostsByVRELib(
|
|
@QueryParam("vreid")
|
|
String vreid,
|
|
@QueryParam("quantity")
|
|
int quantity
|
|
) throws ValidationException{
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
try{
|
|
List<Post> result = CassandraConnection.getInstance().getDatabookStore().getRecentPostsByVRE(vreid, quantity);
|
|
responseBean.setResult(result);
|
|
responseBean.setMessage("");
|
|
responseBean.setSuccess(true);
|
|
}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("get-recent-posts-by-vre-range-lib")
|
|
public Response getRecentPostsByVREAndRangeLib(
|
|
@QueryParam("vreid")
|
|
String vreid,
|
|
@QueryParam("from")
|
|
int from,
|
|
@QueryParam("quantity")
|
|
int quantity
|
|
) throws ValidationException{
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
try{
|
|
RangePosts result = CassandraConnection.getInstance().getDatabookStore().getRecentPostsByVREAndRange(vreid, from, quantity);
|
|
responseBean.setResult(result);
|
|
responseBean.setMessage("");
|
|
responseBean.setSuccess(true);
|
|
}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("get-attachment-by-postid-lib")
|
|
public Response getAttachmentsByFeedIdLib(
|
|
@QueryParam("postid")
|
|
String postid
|
|
) throws ValidationException{
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
try{
|
|
List<Attachment> result = CassandraConnection.getInstance().getDatabookStore().getAttachmentsByFeedId(postid);
|
|
responseBean.setResult(result);
|
|
responseBean.setMessage("");
|
|
responseBean.setSuccess(true);
|
|
}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("get-all-vre-ids-lib")
|
|
public Response getAllVREIdsLib() throws ValidationException{
|
|
ResponseBean responseBean = new ResponseBean();
|
|
Status status = Status.OK;
|
|
try{
|
|
List<String> result = CassandraConnection.getInstance().getDatabookStore().getAllVREIds();
|
|
responseBean.setResult(result);
|
|
responseBean.setMessage("");
|
|
responseBean.setSuccess(true);
|
|
}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();
|
|
}
|
|
}
|