package net.dev123.mblog.twitter;
import java.text.ParseException;
import java.util.ArrayList;
import net.dev123.commons.ServiceProvider;
import net.dev123.commons.util.ParseUtil;
import net.dev123.exception.ExceptionCode;
import net.dev123.exception.LibException;
import net.dev123.mblog.entity.Comment;
import net.dev123.mblog.entity.Status;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class TwitterCommentAdaptor {
public static Comment createCommentFromStatus(Status status) throws LibException {
if (null == status) {
throw new NullPointerException("status is null");
}
Comment comment = new Comment();
comment.setId(status.getId());
comment.setCreatedAt(status.getCreatedAt());
comment.setFavorited(status.isFavorited());
comment.setInReplyToStatus(status.getRetweetedStatus());
comment.setServiceProvider(status.getServiceProvider());
comment.setSource(status.getSource());
comment.setText(status.getText());
comment.setTruncated(status.isTruncated());
comment.setUser(status.getUser());
comment.setServiceProvider(ServiceProvider.Twitter);
return comment;
}
public static Comment createComment(String jsonString) throws LibException {
try {
JSONObject json = new JSONObject(jsonString);
return createComment(json);
} catch (JSONException e) {
throw new LibException(ExceptionCode.JSON_PARSE_ERROR, e);
}
}
public static ArrayList<Comment> createCommentList(String jsonString) throws LibException {
try {
if ("[]".equals(jsonString) || "{}".equals(jsonString)) {
return new ArrayList<Comment>(0);
}
JSONArray jsonList = new JSONArray(jsonString);
int size = jsonList.length();
ArrayList<Comment> comments = new ArrayList<Comment>(size);
if (size > 0) {
for (int i = 0; i < size; i++) {
comments.add(createComment(jsonList.getJSONObject(i)));
}
}
return comments;
} catch (JSONException e) {
throw new LibException(ExceptionCode.JSON_PARSE_ERROR, e);
}
}
static Comment createComment(JSONObject json) throws LibException {
try {
Comment comment = new Comment();
comment.setCreatedAt(ParseUtil.getDate("created_at", json));
comment.setFavorited(ParseUtil.getBoolean("favorited", json));
comment.setId(ParseUtil.getRawString("id", json));
comment.setTruncated(ParseUtil.getBoolean("truncated", json));
comment.setSource(ParseUtil.getRawString("source", json));
comment.setText(ParseUtil.getRawString("text", json));
if (!json.isNull("user")) {
comment.setUser(TwitterUserAdaptor.createUser(json.getJSONObject("user")));
}
if (!json.isNull("status")) {
comment.setInReplyToStatus(TwitterStatusAdaptor.createStatus(json.getJSONObject("status")));
}
if (!json.isNull("reply_comment")) {
comment.setInReplyToComment(createComment(json.getJSONObject("reply_comment")));
}
comment.setServiceProvider(ServiceProvider.Twitter);
return comment;
} catch (JSONException e) {
throw new LibException(ExceptionCode.JSON_PARSE_ERROR, e);
} catch (ParseException e) {
throw new LibException(ExceptionCode.DATE_PARSE_ERROR, e);
}
}
}