social-networking-library-ws/src/main/java/org/gcube/portal/social/networking/ws/methods/v2/HashTags.java

300 lines
9.7 KiB
Java

package org.gcube.portal.social.networking.ws.methods.v2;
import java.util.List;
import java.util.Map;
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 org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.portal.databook.server.DatabookStore;
import org.gcube.portal.databook.shared.Post;
import org.gcube.portal.social.networking.liferay.ws.GroupManagerWSBuilder;
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 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;
/**
* REST interface for the social networking library (hash tags).
*/
@Path("2/hashtags")
@RequestHeaders ({
@RequestHeader( name = "Authorization", description = "Bearer token, see https://dev.d4science.org/how-to-access-resources"),
@RequestHeader( name = "Content-Type", description = "application/json")
})
public class HashTags {
// Logger
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(HashTags.class);
@GET
@Path("get-hashtags-and-occurrences/")
@Produces({MediaType.APPLICATION_JSON})
@StatusCodes ({
@ResponseCode ( code = 200, condition = "Hashtags and occurrences retrieved, reported in the 'result' field of the returned object"),
@ResponseCode ( code = 500, condition = ErrorMessages.ERROR_IN_API_RESULT)
})
/**
* @return hashtags in the context bound to the auth token
*/
public Response getHashTagsAndOccurrences(){
Caller caller = AuthorizationProvider.instance.get();
String username = caller.getClient().getId();
String context = ScopeProvider.instance.get();
ResponseBean responseBean = new ResponseBean();
Status status = Status.OK;
logger.info("User " + username + " has requested hashtags of context " + context);
try{
DatabookStore datastore = CassandraConnection.getInstance().getDatabookStore();
// TODO handle the case of VO and ROOT
boolean isVRE = GroupManagerWSBuilder.getInstance().getGroupManager().isVRE(GroupManagerWSBuilder.getInstance().getGroupManager().getGroupIdFromInfrastructureScope(context));
if(isVRE){
Map<String, Integer> map = datastore.getVREHashtagsWithOccurrence(context);
responseBean.setResult(map);
responseBean.setSuccess(true);
}else{
responseBean.setMessage("Please provide a VRE token. VO and ROOT VO cases are not yet managed.");
responseBean.setResult(false);
}
}catch(Exception e){
logger.error("Failed to retrieve hashtags", e);
status = Status.INTERNAL_SERVER_ERROR;
}
return Response.status(status).entity(responseBean).build();
}
//lib api
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("save-hashtag-lib")
public Response saveHashTagsLib(
@QueryParam("feedid")
String feedid,
@QueryParam("vreid")
String vreid,
@NotNull(message="hashtag to save is missing")
@Valid
List<String> hashtags
) throws ValidationException {
ResponseBean responseBean = new ResponseBean();
Status status = Status.OK;
try{
logger.info("Saving hashtags " );
boolean result = CassandraConnection.getInstance().getDatabookStore().saveHashTags(feedid,vreid,hashtags);
responseBean.setResult(result);
responseBean.setMessage("");
responseBean.setSuccess(result);
}catch(Exception e){
logger.error("Unable to save hashtags.", 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-hashtag-lib")
public Response deleteHashTagsLib(
@QueryParam("feedid")
String feedid,
@QueryParam("vreid")
String vreid,
@NotNull(message="hashtag to delete is missing")
@Valid
List<String> hashtags
) throws ValidationException{
ResponseBean responseBean = new ResponseBean();
Status status = Status.OK;
try{
logger.info("deleting hashtags " );
boolean result = CassandraConnection.getInstance().getDatabookStore().deleteHashTags(feedid,vreid,hashtags);
responseBean.setResult(result);
responseBean.setMessage("");
responseBean.setSuccess(result);
}catch(Exception e){
logger.error("Unable to delete hashtags.", 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-hashtag-comment-lib")
public Response saveHashTagsCommentLib(
@QueryParam("commentid")
String commentid,
@QueryParam("vreid")
String vreid,
@NotNull(message="hashtag to save is missing")
@Valid
List<String> hashtags
) throws ValidationException{
ResponseBean responseBean = new ResponseBean();
Status status = Status.OK;
try{
logger.info("Saving hashtags comments " );
boolean result = CassandraConnection.getInstance().getDatabookStore().saveHashTagsComment(commentid,vreid,hashtags);
responseBean.setResult(result);
responseBean.setMessage("");
responseBean.setSuccess(result);
}catch(Exception e){
logger.error("Unable to save hashtags 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("delete-hashtag-comment-lib")
public Response deleteHashTagsCommentLib(
@QueryParam("commentid")
String commentid,
@QueryParam("vreid")
String vreid,
@NotNull(message="hashtag to delete is missing")
@Valid
List<String> hashtags
) throws ValidationException{
ResponseBean responseBean = new ResponseBean();
Status status = Status.OK;
try{
logger.info("deleting hashtags " );
boolean result = CassandraConnection.getInstance().getDatabookStore().deleteHashTagsComment(commentid,vreid,hashtags);
responseBean.setResult(result);
responseBean.setMessage("");
responseBean.setSuccess(result);
}catch(Exception e){
logger.error("Unable to delete hashtags.", 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-vre-hashtags-occurrences-lib")
public Response getVREHashtagsWithOccurrenceLib(
@QueryParam("vreid")
String vreid
) throws ValidationException {
ResponseBean responseBean = new ResponseBean();
Status status = Status.OK;
Map<String, Integer> hashtags = null;
try{
logger.info("getting vre hashtags occurrences " + vreid);
hashtags = CassandraConnection.getInstance().getDatabookStore().getVREHashtagsWithOccurrence(vreid);
responseBean.setResult(hashtags);
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();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("get-vre-hashtags-occurrences-time-lib")
public Response getVREHashtagsWithOccurrenceFilteredByTimeLib(
@QueryParam("vreid")
String vreid,
@QueryParam("time")
@Min(value = 0, message="time cannot be negative")
long timestamp
) throws ValidationException {
ResponseBean responseBean = new ResponseBean();
Status status = Status.OK;
Map<String, Integer> hashtags = null;
try{
logger.info("getting vre hashtags occurrences " + vreid);
hashtags = CassandraConnection.getInstance().getDatabookStore().getVREHashtagsWithOccurrenceFilteredByTime(vreid, timestamp);
responseBean.setResult(hashtags);
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();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("get-vre-post-by-hashtag-lib")
public Response getVREFeedsByHashtagLib(
@QueryParam("vreid")
String vreid,
@QueryParam("hashtag")
String hashtag
) throws ValidationException {
hashtag = "#" + hashtag;
ResponseBean responseBean = new ResponseBean();
Status status = Status.OK;
List<Post> posts = null;
try{
logger.info("getting vre hashtags occurrences " + vreid);
posts = CassandraConnection.getInstance().getDatabookStore().getVREPostsByHashtag(vreid, hashtag);
responseBean.setResult(posts);
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();
}
}