package kr.kdev.dg1s.biowiki.models;
import android.text.TextUtils;
import org.json.JSONObject;
import kr.kdev.dg1s.biowiki.util.DateTimeUtils;
import kr.kdev.dg1s.biowiki.util.HtmlUtils;
import kr.kdev.dg1s.biowiki.util.JSONUtil;
import kr.kdev.dg1s.biowiki.util.StringUtils;
/**
* Created by nbradbury on 7/8/13.
* TODO: unify this with Comment.java
*/
public class ReaderComment {
public long commentId;
public long blogId;
public long postId;
public long parentId;
public long timestamp;
// not stored in db - denotes the indentation level when displaying this comment
public transient int level = 0;
private String authorName;
private String authorAvatar;
private String authorUrl;
private String status;
private String text;
private String published;
public static ReaderComment fromJson(JSONObject json, long blogId) {
if (json == null)
throw new IllegalArgumentException("null json comment");
ReaderComment comment = new ReaderComment();
comment.blogId = blogId;
comment.commentId = json.optLong("ID");
comment.status = JSONUtil.getString(json, "status");
// note that content may contain html, adapter needs to handle it
comment.text = HtmlUtils.stripScript(JSONUtil.getString(json, "content"));
comment.published = JSONUtil.getString(json, "date");
comment.timestamp = DateTimeUtils.iso8601ToTimestamp(comment.published);
JSONObject jsonPost = json.optJSONObject("post");
if (jsonPost != null)
comment.postId = jsonPost.optLong("ID");
JSONObject jsonAuthor = json.optJSONObject("author");
if (jsonAuthor != null) {
// author names may contain html entities (esp. pingbacks)
comment.authorName = JSONUtil.getStringDecoded(jsonAuthor, "name");
comment.authorAvatar = JSONUtil.getString(jsonAuthor, "avatar_URL");
comment.authorUrl = JSONUtil.getString(jsonAuthor, "URL");
}
JSONObject jsonParent = json.optJSONObject("parent");
if (jsonParent != null)
comment.parentId = jsonParent.optLong("ID");
return comment;
}
public String getAuthorName() {
return StringUtils.notNullStr(authorName);
}
public void setAuthorName(String authorName) {
this.authorName = StringUtils.notNullStr(authorName);
}
public String getAuthorAvatar() {
return StringUtils.notNullStr(authorAvatar);
}
public void setAuthorAvatar(String authorAvatar) {
this.authorAvatar = StringUtils.notNullStr(authorAvatar);
}
public String getAuthorUrl() {
return StringUtils.notNullStr(authorUrl);
}
public void setAuthorUrl(String authorUrl) {
this.authorUrl = StringUtils.notNullStr(authorUrl);
}
public String getText() {
return StringUtils.notNullStr(text);
}
public void setText(String text) {
this.text = StringUtils.notNullStr(text);
}
public String getStatus() {
return StringUtils.notNullStr(status);
}
public void setStatus(String status) {
this.status = StringUtils.notNullStr(status);
}
public String getPublished() {
return StringUtils.notNullStr(published);
}
public void setPublished(String published) {
this.published = StringUtils.notNullStr(published);
}
public boolean hasAuthorUrl() {
return !TextUtils.isEmpty(authorUrl);
}
}