Skip to content

Instantly share code, notes, and snippets.

@likejazz
Last active December 11, 2015 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save likejazz/4617929 to your computer and use it in GitHub Desktop.
Save likejazz/4617929 to your computer and use it in GitHub Desktop.
refactoring. before and after.
public static HashMap<String, String> extractFABThumbnailJSON(String json)
throws Exception {
String YES = "y";
String NO = "n";
HashMap<String, String> map = new HashMap<String, String>();
boolean isVideo = Boolean.FALSE;
boolean isImage = Boolean.FALSE;
boolean hasImageThumbnail = Boolean.FALSE;
boolean hasVideoThumbnail = Boolean.FALSE;
String url = StringUtils.EMPTY;
/* --------- DEFAULT --------- */
map.put("i_thumb_yn", NO);
map.put("image_thumb_url", StringUtils.EMPTY);
map.put("v_thumb_yn", NO);
map.put("video_thumb_url", StringUtils.EMPTY);
/* --------------------------- */
try {
JsonDoc rootJson = new JsonUtil().createJsonDoc(json);
if (rootJson.hasPath("images") == Boolean.FALSE)
return map; // json error.
for (JsonDoc imageJson : rootJson.getJsonDocList("images")) {
isVideo = Boolean.parseBoolean(imageJson.getData("video"));
isImage = !isVideo;
if ((isVideo || isImage) == Boolean.FALSE)
continue; // json error.
if (isVideo && hasVideoThumbnail)
continue; // already created thumbnail.
else if (isImage && hasImageThumbnail)
continue; // already created thumbnail.
if (!StringUtils.equals(imageJson.getData("status"), "S00") || imageJson.hasPath("thumbs") == Boolean.FALSE)
continue; // status error
for (JsonDoc thumbJson : imageJson.getJsonDocList("thumbs")) {
url = thumbJson.getData("url");
if (url.length() < 30)
continue; // url error.
if (isVideo) {
map.put("v_thumb_yn", YES);
map.put("video_thumb_url", getHashKey(url));
hasVideoThumbnail = Boolean.TRUE;
} else if (isImage) {
map.put("i_thumb_yn", YES);
map.put("image_thumb_url", getHashKey(url));
hasImageThumbnail = Boolean.TRUE;
}
break;
}
if (hasImageThumbnail && hasVideoThumbnail)
break; // already created thumbnails for all.
}
} catch (Exception e) {
logger.error(e.toString());
logger.error(json);
return map;
}
return map;
}
public int reviseDocument(Document document, String json_str) throws Exception {
JSONObject json = null;
String status = "";
String url = "";
boolean isVideo = false;
String imgHashKey = "";
String vodHashKey = "";
boolean imgExist = false;
boolean vodExist = false;
document.update("i_thumb_yn", "n");
document.update("image_thumb_url", "");
document.update("v_thumb_yn", "n");
document.update("video_thumb_url", "");
if(json_str == null || json_str.length()<10)
return -1;
json = new JSONObject(json_str);
if (json.has("images") == true){
JSONArray jsonArray = json.getJSONArray("images");
for(int i=0; i<jsonArray.length(); i++){
JSONObject imageObject = jsonArray.getJSONObject(i);
status = imageObject.optString("status");
isVideo = imageObject.optBoolean("video");
if(status.equals("S00") && imageObject.has("thumbs") == true){
JSONArray thumbArray = imageObject.getJSONArray("thumbs");
for(int s=0; s<thumbArray.length(); s++){
JSONObject thumbObject = thumbArray.getJSONObject(s);
url = thumbObject.optString("url");
if(isVideo == false && imgExist == false){
if(url.length() > 30){
imgHashKey = getHashKey(url);
imgExist = true;
}
if(imgExist == true)
break;
}else if(isVideo == true && vodExist == false){
if(url.length() > 30){
vodHashKey = getHashKey(url);
vodExist = true;
}
if(vodExist == true)
break;
}
}
}
if(imgExist == true && vodExist == true)
break;
}
if(imgExist == true){
document.update("i_thumb_yn", "y");
document.update("image_thumb_url", imgHashKey);
}
if(vodExist == true){
document.update("v_thumb_yn", "y");
document.update("video_thumb_url", vodHashKey);
}
}
return DocCorrectionFilter.OK_INDEX;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment