package com.partynetwork.iparty.app.bean;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import com.partynetwork.iparty.app.AppException;
import com.partynetwork.iparty.app.util.StringUtils;
/**
* 应用程序更新实体类
*
* @author liux (http://my.oschina.net/liux)
* @version 1.0
* @created 2012-3-21
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class BUpdate implements Serializable {
// 最新版本
private String newVersion;
// 最低支持版本
private String lastVersion;
// 版本显示
private String versionName;
// 发布日期
private String releaseDate;
// 升级日志
private String changelog;
// 下载地址
private String downloadUrl;
// 升级类型:0:BUG修复;1:功能改进;2:重大更新
private int versionType;
// 安装包文件名
private String packageName;
public String getNewVersion() {
return newVersion;
}
public void setNewVersion(String newVersion) {
this.newVersion = newVersion;
}
public String getLastVersion() {
return lastVersion;
}
public void setLastVersion(String lastVersion) {
this.lastVersion = lastVersion;
}
public String getVersionName() {
return versionName;
}
public void setVersionName(String versionName) {
this.versionName = versionName;
}
public String getReleaseDate() {
return releaseDate;
}
public void setReleaseDate(String releaseDate) {
this.releaseDate = releaseDate;
}
public String getChangelog() {
return changelog;
}
public void setChangelog(String changelog) {
this.changelog = changelog;
}
public String getDownloadUrl() {
return downloadUrl;
}
public void setDownloadUrl(String downloadUrl) {
this.downloadUrl = downloadUrl;
}
public int getVersionType() {
return versionType;
}
public void setVersionType(int versionType) {
this.versionType = versionType;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public static BUpdate parse(InputStream inputStream) throws AppException {
BUpdate update = null;
ObjectMapper om = new ObjectMapper();
// om.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,
// false);
try {
JsonNode treeNode = om.readTree(inputStream);
int result = StringUtils.toInt(treeNode.path("result")
.getValueAsText(), 0);
if (result == 0) {
// 失败
String why = treeNode.path("description").getValueAsText();
throw AppException.fail(why);
} else if (result == 1) {
// 成功
JsonNode dataNode = treeNode.path("details");
update = om.readValue(dataNode.toString(), BUpdate.class);
} else {
throw AppException.fail("接口异常");
}
} catch (JsonParseException e) {
throw AppException.json(e);
} catch (JsonMappingException e) {
throw AppException.json(e);
} catch (IOException e) {
throw AppException.io(e);
}
return update;
}
}