Skip to content

Instantly share code, notes, and snippets.

@IPRIT
Created April 17, 2015 17:26
Show Gist options
  • Select an option

  • Save IPRIT/75c37f2d5c402803a703 to your computer and use it in GitHub Desktop.

Select an option

Save IPRIT/75c37f2d5c402803a703 to your computer and use it in GitHub Desktop.
package ru.twosphere.android.misisbooks;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.Serializable;
import java.util.ArrayList;
/**
* Created by Александр on 31.01.2015.
*/
public class Edition implements Serializable {
private static final long serialVersionUID = -1096217523172222472L;
private final static String LOG_TAG = Edition.class.getSimpleName();
private final static String EDITION_NAME = "name";
private final static String EDITION_AUTHORS = "authors";
private final static String EDITION_ID = "id";
private final static String EDITION_DOWNLOAD_URL = "download_url";
private final static String EDITION_CATEGORY = "category";
private final static String EDITION_CATEGORY_ID = "id";
private final static String EDITION_CATEGORY_NAME = "name";
private Integer mEditionID;
private ArrayList<String> mAuthors = new ArrayList<>();
private String mName;
private String mDownloadUrl;
private String mCategoryName;
private Integer mCategoryID;
public Edition() {}
public Edition(JSONObject jsonObject) {
if (jsonObject != null) {
parseJSONObject(jsonObject);
}
}
public void parseJSONObject(JSONObject jsonObject) {
try {
mEditionID = jsonObject.has(EDITION_ID) ? jsonObject.getInt(EDITION_ID) : null;
JSONArray authors = jsonObject.has(EDITION_AUTHORS) ?
jsonObject.getJSONArray(EDITION_AUTHORS) : null;
if (authors != null) {
Object buf;
for (Integer i = 0; i < authors.length(); ++i) {
buf = authors.get(i);
if (buf instanceof String) {
mAuthors.add((String)buf);
}
}
}
mName = jsonObject.has(EDITION_NAME) ? jsonObject.getString(EDITION_NAME) : null;
mDownloadUrl = jsonObject.has(EDITION_DOWNLOAD_URL) ?
jsonObject.getString(EDITION_DOWNLOAD_URL) : null;
JSONObject categoryObject = jsonObject.has(EDITION_CATEGORY) ?
jsonObject.getJSONObject(EDITION_CATEGORY) : null;
if (categoryObject != null) {
mCategoryID = categoryObject.has(EDITION_CATEGORY_ID) ?
categoryObject.getInt(EDITION_CATEGORY_ID) : null;
mCategoryName = categoryObject.has(EDITION_CATEGORY_NAME) ?
categoryObject.getString(EDITION_CATEGORY_NAME) : null;
}
} catch (final JSONException e) {
e.printStackTrace();
}
}
public String getName() {
return mName;
}
public String getName(JSONObject jsonObject) {
if (jsonObject != null) {
parseJSONObject(jsonObject);
}
return mName != null ? mName : "";
}
public Object[] getAuthorsArray() {
if (mAuthors != null) {
return mAuthors.toArray();
}
return new Object[0];
}
public String getAuthorsJoin(String sep) {
if (mAuthors != null) {
String mRetVal = "";
Integer mNum = 0;
Object[] mArr = getAuthorsArray();
for (Object author: mArr) {
mRetVal += (String)author;
mNum++;
if (mNum != mArr.length) {
mRetVal += sep;
}
}
return mRetVal;
}
return "";
}
public ArrayList<String> getAuthorArrayList() {
return mAuthors != null ? mAuthors : new ArrayList<String>();
}
public Integer getEditionId() {
return mEditionID != null ? mEditionID : null;
}
public String getDownloadUrl() {
return mDownloadUrl != null ? mDownloadUrl : null;
}
public String getCategoryName() {
return mCategoryName != null ? mCategoryName : null;
}
public Integer getCategoryID() {
return mCategoryID != null ? mCategoryID : null;
}
public boolean isEmpty() {
return mEditionID == null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment