package in.partake.model.dto; import in.partake.base.DateTime; import java.util.Arrays; import org.apache.commons.lang.ObjectUtils; import org.codehaus.jackson.node.JsonNodeFactory; import org.codehaus.jackson.node.ObjectNode; public class UserThumbnail extends PartakeModel<UserThumbnail> { private String id; private String userId; private String type; private byte[] data; private DateTime createdAt; public UserThumbnail(String id, String userId, String type, byte[] data, DateTime createdAt) { this.id = id; this.userId = userId; this.type = type; this.data = data; this.createdAt = createdAt; } public UserThumbnail(UserThumbnail src) { this(src.id, src.userId, src.type, src.data, src.createdAt); } public UserThumbnail(ObjectNode obj) { this.id = obj.get("id").asText(); this.userId = obj.path("userId").asText(); this.type = obj.get("type").asText(); if (obj.has("createdAt")) this.createdAt = new DateTime(obj.get("createdAt").asLong()); // We don't create data from JSONObject. } @Override public Object getPrimaryKey() { return id; } // ---------------------------------------------------------------------- // equals / hashCode @Override public boolean equals(Object obj) { if (!(obj instanceof UserThumbnail)) { return false; } UserThumbnail lhs = this; UserThumbnail rhs = (UserThumbnail) obj; if (!ObjectUtils.equals(lhs.id, rhs.id)) { return false; } if (!ObjectUtils.equals(lhs.userId, rhs.userId)) { return false; } if (!ObjectUtils.equals(lhs.type, rhs.type)) { return false; } if (!Arrays.equals(lhs.data, rhs.data)) { return false; } if (!ObjectUtils.equals(lhs.createdAt, rhs.createdAt)) { return false; } return true; } @Override public int hashCode() { int code = 0; code = code * 37 + ObjectUtils.hashCode(id); return code; } @Override public ObjectNode toJSON() { ObjectNode obj = new ObjectNode(JsonNodeFactory.instance); obj.put("id", id); obj.put("userId", userId); obj.put("type", type); if (createdAt != null) obj.put("createdAt", createdAt.getTime()); return obj; } // ---------------------------------------------------------------------- // accessors public String getId() { return id; } public String getUserId() { return userId; } public String getType() { return type; } public byte[] getData() { return data; } public DateTime getCreatedAt() { return createdAt; } public void setId(String id) { checkFrozen(); this.id = id; } public void setUserId(String userId) { checkFrozen(); this.userId = userId; } public void setType(String type) { checkFrozen(); this.type = type; } public void setData(byte[] data) { checkFrozen(); this.data = data; } public void setCreatedAt(DateTime createdAt) { checkFrozen(); this.createdAt = createdAt; } }