Commit 9638c11e by Kazuyuki Hida

JSONから値を取り出すユーティリティを集約した

parent ff4f0eee
......@@ -6,11 +6,15 @@ import jp.agentec.abook.abv.bl.common.exception.AcmsException;
import jp.agentec.abook.abv.bl.common.exception.JSONValidationException;
import jp.agentec.abook.abv.bl.common.log.Logger;
import jp.agentec.adf.core.CloneableObject;
import jp.agentec.adf.util.DateTimeFormat;
import jp.agentec.adf.util.DateTimeUtil;
import jp.agentec.adf.util.StringUtil;
import org.json.adf.JSONException;
import org.json.adf.JSONObject;
import java.util.Date;
public abstract class AcmsJSONParser extends CloneableObject {
/**
* {@link AcmsJSONParser} クラスのインスタンスを初期化します。
......@@ -129,4 +133,27 @@ public abstract class AcmsJSONParser extends CloneableObject {
}
}
protected int getIntOrDef(JSONObject json, String key, int def) {
if (json.has(key)) {
return json.getInt(key);
} else {
return def;
}
}
protected String getStringOrNull(JSONObject json, String key) {
if (json.has(key)) {
return json.getString(key);
} else {
return null;
}
}
protected Date getDateOrNull(JSONObject json, String key) {
if (json.has(key)) {
return DateTimeUtil.toDate(json.getString(key), "UTC", DateTimeFormat.yyyyMMdd_hyphen);
} else {
return null;
}
}
}
......@@ -5,14 +5,13 @@ import org.json.adf.JSONObject;
import java.util.Date;
import jp.agentec.abook.abv.bl.common.exception.AcmsException;
import jp.agentec.adf.util.DateTimeFormat;
import jp.agentec.adf.util.DateTimeUtil;
/**
* checkapi/lockReport/のレスポンスを変換するクラス
*/
public class LockReportJSON extends AcmsCommonJSON {
public static final int BAD_STATE = 999;
private Date presentTimeUTC;
private int reportStatus;
private String reportLockUserId;
......@@ -25,62 +24,45 @@ public class LockReportJSON extends AcmsCommonJSON {
@Override
protected void parse(JSONObject json) {
presentTimeUTC = dateOrNull(json, "presentTimeUTC");
reportStatus = intOrDef(json, "reportStatus", 999);
reportLockUserId = stringOrNull(json, "reportLockUserId");
reportLockUserName = stringOrNull(json, "reportLockUserName");
reportLockTime = dateOrNull(json, "reportLockTime");
presentTimeUTC = getDateOrNull(json, "presentTimeUTC");
reportStatus = getIntOrDef(json, "reportStatus", BAD_STATE);
reportLockUserId = getStringOrNull(json, "reportLockUserId");
reportLockUserName = getStringOrNull(json, "reportLockUserName");
reportLockTime = getDateOrNull(json, "reportLockTime");
}
@SuppressWarnings("unused")
int getHttpStatus() {
return httpStatus;
}
@SuppressWarnings("unused")
Date getPresentTime() {
return presentTime;
}
@SuppressWarnings("unused")
public Date getPresentTimeUTC() {
return presentTimeUTC;
}
@SuppressWarnings("unused")
public int getReportStatus() {
return reportStatus;
}
@SuppressWarnings("unused")
public String getReportLockUserId() {
return reportLockUserId;
}
@SuppressWarnings("unused")
public String getReportLockUserName() {
return reportLockUserName;
}
@SuppressWarnings("unused")
public Date getReportLockTime() {
return reportLockTime;
}
private int intOrDef(JSONObject json, String key, int def) {
if (json.has(key)) {
return json.getInt(key);
} else {
return def;
}
}
private String stringOrNull(JSONObject json, String key) {
if (json.has(key)) {
return json.getString(key);
} else {
return null;
}
}
private Date dateOrNull(JSONObject json, String key) {
if (json.has(key)) {
return DateTimeUtil.toDate(json.getString(key), "UTC", DateTimeFormat.yyyyMMdd_hyphen);
} else {
return null;
}
}
}
......@@ -5,10 +5,10 @@ import org.json.adf.JSONObject;
import java.util.Date;
import jp.agentec.abook.abv.bl.common.exception.AcmsException;
import jp.agentec.adf.util.DateTimeFormat;
import jp.agentec.adf.util.DateTimeUtil;
public class UnlockReportJSON extends AcmsCommonJSON {
public static final int BAD_STATE = 999;
private Date presentTimeUTC;
private int reportStatus;
......@@ -18,37 +18,25 @@ public class UnlockReportJSON extends AcmsCommonJSON {
@Override
protected void parse(JSONObject json) {
presentTimeUTC = dateOrNull(json, "presentTimeUTC");
reportStatus = intOrDef(json, "", 999);
presentTimeUTC = getDateOrNull(json, "presentTimeUTC");
reportStatus = getIntOrDef(json, "", BAD_STATE);
}
@SuppressWarnings("unused")
public int getHttpStatus() {
return httpStatus;
}
@SuppressWarnings("unused")
public Date getPresentTime() {
return presentTime;
}
@SuppressWarnings("unused")
public Date getPresentTimeUTC() {
return presentTimeUTC;
}
@SuppressWarnings("unused")
public int getReportStatus() {return reportStatus; }
private int intOrDef(JSONObject json, String key, int def) {
if (json.has(key)) {
return json.getInt(key);
} else {
return def;
}
}
private Date dateOrNull(JSONObject json, String key) {
if (json.has(key)) {
return DateTimeUtil.toDate(json.getString(key), "UTC", DateTimeFormat.yyyyMMdd_hyphen);
} else {
return null;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment