social-networking-library/src/main/java/org/gcube/portal/databook/shared/Post.java

394 lines
12 KiB
Java

package org.gcube.portal.databook.shared;
import com.datastax.oss.driver.api.core.cql.Row;
import com.datastax.oss.driver.api.core.type.DataTypes;
import org.gcube.portal.databook.shared.ex.PostTypeNotFoundException;
import org.gcube.portal.databook.shared.ex.PrivacyLevelTypeNotFoundException;
import org.gcube.portal.databook.server.Schema.*;
import java.io.Serializable;
import java.util.Date;
import java.util.Objects;
import static org.gcube.portal.databook.server.Schema.*;
/**
*
* @author Massimiliano Assante, ISTI-CNR
*
*/
@SuppressWarnings("serial")
public class Post implements Serializable, Comparable<Post> {
private String key;
private PostType type;
private String entityId;
private Date time;
private String vreid;
private String uri;
private String uriThumbnail;
private String description;
private PrivacyLevel privacy;
private String fullName;
private String email;
private String thumbnailURL;
private String commentsNo;
private String likesNo;
private String linkTitle;
private String linkDescription;
private String linkHost;
boolean applicationPost;
/**
* this boolean indicates that the attachments to the post are > 1
*/
boolean multiFileUpload;
/**
* default constructor
*/
public Post() {
super();
}
/**
* To use ONLY for USER Posts
*
*
* @param key a UUID
* @param type an instance of <class>PostType</class>
* @param entityId the user or the app unique indentifier
* @param time when
* @param vreid a unique vre id
* @param uri optional uri
* @param uriThumbnail the thumbnail for the link posted
* @param description optional description
* @param privacy the privacy level of <class>PrivacyLevel</class>
* @param fullName
* @param email
* @param thumbnailURL this is the user thumbnail url
* @param linkTitle optional to be used when posting links
* @param linkDescription optional to be used when posting links
* @param linkHost option to be used when posting linkgs
*/
public Post(String key, PostType type, String entityId, Date time,
String vreid, String uri, String uriThumbnail, String description, PrivacyLevel privacy,
String fullName, String email, String thumbnailURL, String linkTitle, String linkDescription, String linkHost) {
this.key = key;
this.type = type;
this.entityId = entityId;
this.time = time;
this.vreid = vreid;
this.uri = uri;
this.uriThumbnail = uriThumbnail;
this.description = description;
this.privacy = privacy;
this.fullName = fullName;
this.email = email;
this.thumbnailURL = thumbnailURL;
this.commentsNo = "0";
this.likesNo = "0";
this.linkDescription = linkDescription;
this.linkTitle = linkTitle;
this.linkHost = linkHost;
this.applicationPost = false;
}
/**
* To use for USER and ApplicationProfile Posts
*
* @param key a UUID
* @param type an instance of <class>PostType</class>
* @param entityId the user or the app unique indentifier
* @param time when
* @param vreid a unique vre id
* @param uri optional uri
* @param uriThumbnail the thumbnail for the link posted
* @param description optional description
* @param privacy the privacy level of <class>PrivacyLevel</class>
* @param fullName
* @param email
* @param thumbnailURL this is the user thumbnail url
* @param linkTitle optional to be used when posting links
* @param linkDescription optional to be used when posting links
* @param applicationPost tell if this is an application Post or a user Post
*/
public Post(String key, PostType type, String entityId, Date time,
String vreid, String uri, String uriThumbnail, String description, PrivacyLevel privacy,
String fullName, String email, String thumbnailURL, String linkTitle, String linkDescription, String linkHost, boolean applicationPost) {
this(key, type, entityId, time, vreid, uri, uriThumbnail, description, privacy, fullName, email, thumbnailURL, linkTitle, linkDescription, linkHost);
this.applicationPost= applicationPost;
}
/**
* for serialization purposes
* @param key a UUID
* @param type an instance of <class>PostType</class>
* @param entityId the user or the app unique indentifier
* @param time when
* @param vreid a unique vre id
* @param uri optional uri
* @param uriThumbnail the thumbnail for the link posted
* @param description optional description
* @param privacy the privacy level of <class>PrivacyLevel</class>
* @param fullName
* @param email
* @param thumbnailURL this is the user thumbnail url
* @param linkTitle optional to be used when posting links
* @param linkDescription optional to be used when posting links
*/
public Post(String key, PostType type, String entityId, Date time,
String vreid, String uri, String uriThumbnail, String description, PrivacyLevel privacy,
String fullName, String email, String thumbnailURL, String commentsNo,
String likesNo, String linkTitle, String linkDescription, String linkHost, boolean applicationPost, boolean multiFileUpload) {
super();
this.key = key;
this.type = type;
this.entityId = entityId;
this.time = time;
this.vreid = vreid;
this.uri = uri;
this.uriThumbnail = uriThumbnail;
this.description = description;
this.privacy = privacy;
this.fullName = fullName;
this.email = email;
this.thumbnailURL = thumbnailURL;
this.commentsNo = commentsNo;
this.likesNo = likesNo;
this.linkDescription = linkDescription;
this.linkTitle = linkTitle;
this.linkHost = linkHost;
this.applicationPost = applicationPost;
this.multiFileUpload = multiFileUpload;
}
public Post(Row record) throws PostTypeNotFoundException, PrivacyLevelTypeNotFoundException {
super();
this.key = Objects.requireNonNull(record.getUuid(POST_ID)).toString();
this.type = this.getPostType(Objects.requireNonNull(record.getString(TYPE)));
this.entityId = record.getString(ENTITY_ID);
this.time = Date.from(Objects.requireNonNull(record.getInstant(TIMESTAMP)));
this.vreid = record.getString(VRE_ID);
this.uri = record.getString(URI);
this.uriThumbnail = record.getString(URI_THUMBNAIL);
this.description = record.getString(DESCRIPTION);
this.privacy = this.getPrivacyLevel(Objects.requireNonNull(record.getString(PRIVACY)));
this.fullName = record.getString(FULL_NAME);
this.email = record.getString(EMAIL);
this.thumbnailURL = record.getString(THUMBNAIL_URL);
this.commentsNo = String.valueOf(record.getLong(COMMENTS_NO));
this.likesNo = String.valueOf(record.getLong(LIKES_NO));
this.linkDescription = record.getString(LINK_DESCRIPTION);
this.linkTitle = record.getString(LINK_TITLE);
this.linkHost = record.getString(LINK_HOST);
this.applicationPost = record.getBoolean(IS_APPLICATION_POST);
this.multiFileUpload = record.getBoolean(MULTI_FILE_UPLOAD);
}
/**
*
* @return post id
*/
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public PostType getType() {
return type;
}
public void setType(PostType type) {
this.type = type;
}
/**
*
* @return the User or the App id
*/
public String getEntityId() {
return entityId;
}
/**
* set the User or the App id
* @param entityId the UserId or the AppId id
*/
public void setEntityId(String entityId) {
this.entityId = entityId;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public String getVreid() {
return vreid;
}
public void setVreid(String vreid) {
this.vreid = vreid;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public PrivacyLevel getPrivacy() {
return privacy;
}
public void setPrivacy(PrivacyLevel privacy) {
this.privacy = privacy;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getThumbnailURL() {
return thumbnailURL;
}
public void setThumbnailURL(String thumbnailURL) {
this.thumbnailURL = thumbnailURL;
}
public String getCommentsNo() {
return commentsNo;
}
public void setCommentsNo(String commentsNo) {
this.commentsNo = commentsNo;
}
public String getLikesNo() {
return likesNo;
}
public void setLikesNo(String likesNo) {
this.likesNo = likesNo;
}
public String getUriThumbnail() {
return uriThumbnail;
}
public void setUriThumbnail(String uriThumbnail) {
this.uriThumbnail = uriThumbnail;
}
public String getLinkTitle() {
return linkTitle;
}
public void setLinkTitle(String linkTitle) {
this.linkTitle = linkTitle;
}
public String getLinkDescription() {
return linkDescription;
}
public void setLinkDescription(String linkDescription) {
this.linkDescription = linkDescription;
}
public int compareTo(Post toCompare) {
if (this.time.after(toCompare.getTime()))
return 1;
if (this.time.before(toCompare.getTime()))
return -1;
return 0;
}
public String getLinkHost() {
return linkHost;
}
public void setLinkHost(String linkHost) {
this.linkHost = linkHost;
}
public boolean isApplicationPost() {
return applicationPost;
}
public void setApplicationPost(boolean applicationPost) {
this.applicationPost = applicationPost;
}
public boolean isMultiFileUpload() {
return multiFileUpload;
}
public void setMultiFileUpload(boolean multiFileUpload) {
this.multiFileUpload = multiFileUpload;
}
@Override
public String toString() {
return "Post [key=" + key + ", type=" + type + ", entityId=" + entityId
+ ", time=" + time + ", vreid=" + vreid + ", uri=" + uri
+ ", uriThumbnail=" + uriThumbnail + ", description="
+ description + ", privacy=" + privacy + ", fullName="
+ fullName + ", email=" + email + ", thumbnailURL="
+ thumbnailURL + ", commentsNo=" + commentsNo + ", likesNo="
+ likesNo + ", linkTitle=" + linkTitle + ", linkDescription="
+ linkDescription + ", linkHost=" + linkHost
+ ", applicationPost=" + applicationPost
+ ", multiFileUpload=" + multiFileUpload + "]";
}
public static PostType getPostType(String type) throws PostTypeNotFoundException {
if (type.compareTo("TWEET") == 0) {
return PostType.TWEET;
}
else if (type.compareTo("JOIN") == 0) {
return PostType.JOIN;
}
else if (type.compareTo("PUBLISH") == 0) {
return PostType.PUBLISH;
}
else if (type.compareTo("SHARE") == 0) {
return PostType.SHARE;
}
else if (type.compareTo("ACCOUNTING") == 0) {
return PostType.ACCOUNTING;
}
else if (type.compareTo("DISABLED") == 0) {
return PostType.DISABLED;
}
else
throw new PostTypeNotFoundException("The Post Type was not recognized should be one of " + PostType.values() + " asked for: " + type);
}
public static PrivacyLevel getPrivacyLevel(String privacyLevel) throws PrivacyLevelTypeNotFoundException {
if (privacyLevel.compareTo("CONNECTION") == 0)
return PrivacyLevel.CONNECTION;
else if (privacyLevel.compareTo("PRIVATE") == 0)
return PrivacyLevel.PRIVATE;
else if (privacyLevel.compareTo("PUBLIC") == 0)
return PrivacyLevel.PUBLIC;
else if (privacyLevel.compareTo("VRES") == 0)
return PrivacyLevel.VRES;
else if (privacyLevel.compareTo("SINGLE_VRE") == 0)
return PrivacyLevel.SINGLE_VRE;
else if (privacyLevel.compareTo("PORTAL") == 0)
return PrivacyLevel.PORTAL;
else
throw new PrivacyLevelTypeNotFoundException("The Privacy Level was not recognized should be one of " + PrivacyLevel.values() + " asked for: " + privacyLevel);
}
}