package com.partynetwork.iparty.app.bean;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.ObjectMapper;
import com.partynetwork.dataprovider.util.L;
import com.partynetwork.dataprovider.util.StringUtil;
import com.partynetwork.iparty.app.AppException;
import com.partynetwork.iparty.info.IpartyInfo;
import com.partynetwork.iparty.info.IshareInfo;
import com.partynetwork.iparty.info.MessageTypeInfo;
public class NoticeList {
private List<MessageTypeInfo> noticeList = new ArrayList<MessageTypeInfo>();
public List<MessageTypeInfo> getNoticeList() {
return noticeList;
}
public void setNoticeList(List<MessageTypeInfo> noticeList) {
this.noticeList = noticeList;
}
public static NoticeList parse(InputStream stream) throws AppException {
NoticeList noticeList = new NoticeList();
String str = StringUtil.convertStreamToString(stream);
ObjectMapper om = new ObjectMapper();
try {
JsonNode rootNode = om.readTree(str);
int result = rootNode.path("result").getIntValue();
if (result == 0) {
// 失败
String why = rootNode.path("description").getTextValue();
L.i("发送失败:" + why);
throw AppException.fail(why);
} else if (result == 1) {
// 成功
JsonNode dataNode = rootNode.path("details");
JsonFactory f = new JsonFactory();
JsonParser jp = f.createJsonParser(dataNode.toString());
jp.nextToken();
while (jp.nextToken() == JsonToken.START_OBJECT) {
MessageTypeInfo info = om.readValue(jp,
MessageTypeInfo.class);
noticeList.getNoticeList().add(info);
}
return noticeList;
} else {
throw AppException.fail("接口异常");
}
} catch (JsonProcessingException e) {
L.i("json转换失败");
throw AppException.json(e);
} catch (IOException e) {
throw AppException.io(e);
}
}
}