Search in sources :

Example 1 with ExpansionIndexItem

use of scal.io.liger.model.ExpansionIndexItem in project storymaker by StoryMaker.

the class ServerManager method index.

// NEW/TEMP
// DOWNLOAD AVAILABE INDEX FOR CURRENT USER AND SAVE TO TARGET FILE
// RETURN TRUE IF SUCCESSFUL
public boolean index() {
    try {
        connect();
    } catch (IOException ioe) {
        Timber.e("UNABLE TO CONNECT TO SERVER, CAN'T GET INDEX");
        return false;
    }
    // TODO: IF ENDPOINT ONLY REQUIRES TOKEN, DO WE CARE ABOUT AUTH/USER?
    Auth auth = (new AuthTable()).getAuthDefault(mContext, Auth.SITE_STORYMAKER);
    if (auth != null) {
        String user = auth.getUserName();
        if (user != null && user.length() > 0) {
            // load baseline index
            HashMap<String, ExpansionIndexItem> contentItems = IndexManager.loadAvailableOrderIndex(mContext);
            // download users index
            // TODO: WHERE IS INTERFACE VERSION SPECIFIED?
            JSONArray jArray = smWrapper.index(1);
            if (jArray == null) {
                Timber.e("FAILED TO DOWNLOAD NEW ASSIGNMENTS");
                return false;
            }
            if (jArray.length() == 0) {
                Timber.d("NO ASSIGNMENTS FOUND, INDEX WILL NOT BE UPDATED");
                return true;
            }
            // convert items and add to index
            for (int i = 0; i < jArray.length(); i++) {
                try {
                    JSONObject jObject = jArray.getJSONObject(i);
                    String filePath = "Android/data/" + mContext.getPackageName() + "/files/";
                    ExpansionIndexItem contentItem = new // packageName
                    ExpansionIndexItem(// packageName
                    jObject.getString("organization"), // expansionId
                    jObject.getString("uuid"), Integer.parseInt(jObject.getString("sortOrder")), // patchOrder (using arbitrary negative value to avoid collisions with existing items
                    "" + (-1 - i), jObject.getString("contentType"), // expansionFileVersion
                    "" + jObject.getInt("version"), // expansionFilePath
                    filePath, // expansionFileUrl - obb filename
                    jObject.getString("obb_file").substring(0, jObject.getString("obb_file").lastIndexOf("/") + 1), // expansionThumbnail
                    jObject.getString("thumbnail_path"));
                    contentItem.setTitle(jObject.getString("title"));
                    contentItem.setDescription(jObject.getString("description"));
                    contentItem.setExpansionFileSize(jObject.getLong("size"));
                    contentItem.setExpansionFileChecksum(jObject.getString("obb_checksum"));
                    contentItem.setDateUpdated(jObject.getString("updated"));
                    contentItems.put(contentItem.getPatchOrder(), contentItem);
                    Timber.d("ADDED ITEM TO INDEX (" + contentItem.getTitle() + ")");
                } catch (JSONException je) {
                    Timber.e("FAILED TO EXTRACT VALUE FROM JSON OBJECT (ARRAY ELEMENT " + i + "): " + je.getMessage());
                }
            }
            IndexManager.saveAvailableIndex(mContext, contentItems);
            Timber.d("SAVED UPDATED INDEX");
            return true;
        } else {
            Timber.e("NO USER NAME");
        }
    } else {
        Timber.e("NOT LOGGED IN");
    }
    Timber.e("CAN'T DOWNLOAD NEW ASSIGNMENTS");
    return false;
}
Also used : JSONObject(org.json.JSONObject) Auth(org.storymaker.app.model.Auth) AuthTable(org.storymaker.app.model.AuthTable) JSONArray(org.json.JSONArray) ExpansionIndexItem(scal.io.liger.model.ExpansionIndexItem) JSONException(org.json.JSONException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 Auth (org.storymaker.app.model.Auth)1 AuthTable (org.storymaker.app.model.AuthTable)1 ExpansionIndexItem (scal.io.liger.model.ExpansionIndexItem)1