add files
[freespace.git] / bak / DirectMessage.java
blobcd06a9b5104f61ab7975091e84b0b96e314018d8
1 /*
2 Copyright (c) 2007-2009, Yusuke Yamamoto
3 All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the Yusuke Yamamoto nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY
17 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 package weibo4android;
29 import java.util.ArrayList;
30 import java.util.Date;
31 import java.util.List;
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Element;
35 import org.w3c.dom.NodeList;
37 import weibo4android.http.Response;
38 import weibo4android.org.json.JSONArray;
39 import weibo4android.org.json.JSONException;
40 import weibo4android.org.json.JSONObject;
42 /**
43 * A data class representing sent/received direct message.
44 * @author Yusuke Yamamoto - yusuke at mac.com
46 public class DirectMessage extends WeiboResponse implements java.io.Serializable {
47 private Long id;
48 private String text;
49 private int sender_id;
50 private int recipient_id;
51 private Date created_at;
52 private String sender_screen_name;
53 private String recipient_screen_name;
54 private static final long serialVersionUID = -3253021825891789737L;
56 /*package*/DirectMessage(Response res, Weibo weibo) throws WeiboException {
57 super(res);
58 init(res, res.asDocument().getDocumentElement(), weibo);
60 /*package*/DirectMessage(Response res, Element elem, Weibo weibo) throws WeiboException {
61 super(res);
62 init(res, elem, weibo);
64 /*modify by sycheng add json call*/
65 /*package*/DirectMessage(JSONObject json) throws WeiboException {
66 try {
67 id = json.getLong("id");
68 text = json.getString("text");
69 sender_id = json.getInt("sender_id");
70 recipient_id = json.getInt("recipient_id");
71 created_at = parseDate(json.getString("created_at"), "EEE MMM dd HH:mm:ss z yyyy");
72 sender_screen_name = json.getString("sender_screen_name");
73 recipient_screen_name = json.getString("recipient_screen_name");
75 if(!json.isNull("sender"))
76 sender = new User(json.getJSONObject("sender"));
77 } catch (JSONException jsone) {
78 throw new WeiboException(jsone.getMessage() + ":" + json.toString(), jsone);
83 private void init(Response res, Element elem, Weibo weibo) throws WeiboException{
84 ensureRootNodeNameIs("direct_message", elem);
85 sender = new User(res, (Element) elem.getElementsByTagName("sender").item(0),
86 weibo);
87 recipient = new User(res, (Element) elem.getElementsByTagName("recipient").item(0),
88 weibo);
89 id = getChildLong("id", elem);
90 text = getChildText("text", elem);
91 sender_id = getChildInt("sender_id", elem);
92 recipient_id = getChildInt("recipient_id", elem);
93 created_at = getChildDate("created_at", elem);
94 sender_screen_name = getChildText("sender_screen_name", elem);
95 recipient_screen_name = getChildText("recipient_screen_name", elem);
98 public long getId() {
99 return id;
102 public String getText() {
103 return text;
106 public int getSenderId() {
107 return sender_id;
110 public int getRecipientId() {
111 return recipient_id;
115 * @return created_at
116 * @since Weibo4J 1.1.0
118 public Date getCreatedAt() {
119 return created_at;
122 public String getSenderScreenName() {
123 return sender_screen_name;
126 public String getRecipientScreenName() {
127 return recipient_screen_name;
130 private User sender;
132 public User getSender() {
133 return sender;
136 private User recipient;
138 public User getRecipient() {
139 return recipient;
142 /*package*/
143 static List<DirectMessage> constructDirectMessages(Response res,
144 Weibo weibo) throws WeiboException {
145 Document doc = res.asDocument();
146 if (isRootNodeNilClasses(doc)) {
147 return new ArrayList<DirectMessage>(0);
148 } else {
149 try {
150 ensureRootNodeNameIs("direct-messages", doc);
151 NodeList list = doc.getDocumentElement().getElementsByTagName(
152 "direct_message");
153 int size = list.getLength();
154 List<DirectMessage> messages = new ArrayList<DirectMessage>(size);
155 for (int i = 0; i < size; i++) {
156 Element status = (Element) list.item(i);
157 messages.add(new DirectMessage(res, status, weibo));
159 return messages;
160 } catch (WeiboException te) {
161 if (isRootNodeNilClasses(doc)) {
162 return new ArrayList<DirectMessage>(0);
163 } else {
164 throw te;
170 /*package*/
171 static List<DirectMessage> constructDirectMessages(Response res
172 ) throws WeiboException {
173 JSONArray list= res.asJSONArray();
175 try {
176 int size = list.length();
177 List<DirectMessage> messages = new ArrayList<DirectMessage>(size);
178 for (int i = 0; i < size; i++) {
180 messages.add(new DirectMessage(list.getJSONObject(i)));
182 return messages;
183 } catch (JSONException jsone) {
184 throw new WeiboException(jsone);
188 @Override
189 public int hashCode() {
190 return id.hashCode();
193 @Override
194 public boolean equals(Object obj) {
195 if (null == obj) {
196 return false;
198 if (this == obj) {
199 return true;
201 return obj instanceof DirectMessage && ((DirectMessage) obj).id == this.id;
204 @Override
205 public String toString() {
206 return "DirectMessage{" +
207 "id=" + id +
208 ", text='" + text + '\'' +
209 ", sender_id=" + sender_id +
210 ", recipient_id=" + recipient_id +
211 ", created_at=" + created_at +
212 ", sender_screen_name='" + sender_screen_name + '\'' +
213 ", recipient_screen_name='" + recipient_screen_name + '\'' +
214 ", sender=" + sender +
215 ", recipient=" + recipient +
216 '}';