Android Open Source - Scanner-For-Zotero World Cat Handler






From Project

Back to project page Scanner-For-Zotero.

License

The source code is released under:

GNU General Public License

If you think the Android project Scanner-For-Zotero listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/** 
 * Copyright 2011 John M. Schanck/*from  w  w  w .jav a  2s.c  o  m*/
 * 
 * ScannerForZotero is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ScannerForZotero is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ScannerForZotero.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.ale.scanner.zotero.web.worldcat;

import org.ale.scanner.zotero.PendingListAdapter;
import org.ale.scanner.zotero.web.APIHandler;
import org.ale.scanner.zotero.web.APIRequest;
import org.apache.http.StatusLine;
import org.json.JSONObject;

public class WorldCatHandler extends APIHandler{

    private static WorldCatHandler mInstance = null;

    public static WorldCatHandler getInstance(){
        if(mInstance == null){
            mInstance = new WorldCatHandler();
            APIHandler.HANDLERS.add(mInstance);
        }
        return mInstance;
    }

    // APIHandler.MAIN is guaranteed to be non-null
    // when these methods are called
    protected void onStart(APIRequest req) {
        
    }

    protected void onProgress(APIRequest req, int percent) {
        
    }

    protected void onStatusLine(APIRequest req, StatusLine status) {
        String id = req.getExtra().getString(WorldCatAPIClient.EXTRA_ISBN);
        int statusCode = status.getStatusCode();
        if(statusCode >= 400) {
            int errReason;
            switch(statusCode){
            case 400:
                errReason = PendingListAdapter.STATUS_BAD_REQUEST;
                break;
            case 500:
                errReason = PendingListAdapter.STATUS_SERVER_ERROR;
                break;
            default:
                errReason = PendingListAdapter.STATUS_FAILED;
                break;
            }
            APIHandler.MAIN.bibFetchFailure(id, errReason);
        }
    }

    protected void onException(APIRequest req, Exception exc) {
        String id = req.getExtra().getString(WorldCatAPIClient.EXTRA_ISBN);
        exc.printStackTrace();
        //TODO: Be more helpful here, might not be a network issue
        APIHandler.MAIN.bibFetchFailure(id, PendingListAdapter.STATUS_NO_NETWORK);
    }

    protected void onSuccess(APIRequest req, final String resp){
        final String id = req.getExtra().getString(WorldCatAPIClient.EXTRA_ISBN);
        new Thread(new Runnable() {
            public void run(){
                JSONObject jsonresp = WorldCatAPIClient.strToJSON(resp);
                int status = WorldCatAPIClient.getStatus(jsonresp);
                boolean failed = (status != WorldCatAPIClient.STATUS_OK);

                JSONObject tmpTrans = null;
                if(!failed){
                    tmpTrans = WorldCatAPIClient.translateJsonResponse(id, jsonresp);
                    failed |= (tmpTrans == null);
                }

                Runnable toPost;
                // Since that might have taken some time, check that the 
                // activity is still around and post the BibInfo.
                if(failed){
                    final int reason;
                    switch(status){
                    case WorldCatAPIClient.STATUS_OVER_LIMIT:
                        reason = PendingListAdapter.STATUS_QUOTA_EXCEEDED;
                        break;
                    case WorldCatAPIClient.STATUS_INVALID:
                        reason = PendingListAdapter.STATUS_BAD_REQUEST;
                        break;
                    case WorldCatAPIClient.STATUS_NOT_FOUND:
                        reason = PendingListAdapter.STATUS_NOT_FOUND;
                        break;
                    default:
                        reason = PendingListAdapter.STATUS_FAILED;
                        break;
                    }
                    toPost = new Runnable(){
                                 public void run(){
                                     APIHandler.MAIN.bibFetchFailure(id, reason);
                                 }
                             };
                }else{
                    final JSONObject translated = tmpTrans;
                    toPost = new Runnable(){
                                 public void run(){
                                     APIHandler.MAIN.bibFetchSuccess(id, translated);
                                 }
                             };
                }
                checkActivityAndRun(toPost);
            }
        }).start();
    }
}




Java Source Code List

org.ale.scanner.zotero.BibDetailJSONAdapter.java
org.ale.scanner.zotero.BibItemListAdapter.java
org.ale.scanner.zotero.Dialogs.java
org.ale.scanner.zotero.EditItemActivity.java
org.ale.scanner.zotero.LoginActivity.java
org.ale.scanner.zotero.MainActivity.java
org.ale.scanner.zotero.ManageAccountsActivity.java
org.ale.scanner.zotero.PString.java
org.ale.scanner.zotero.PendingListAdapter.java
org.ale.scanner.zotero.SafeViewFlipper.java
org.ale.scanner.zotero.Util.java
org.ale.scanner.zotero.data.Access.java
org.ale.scanner.zotero.data.Account.java
org.ale.scanner.zotero.data.BibItemDBHandler.java
org.ale.scanner.zotero.data.BibItem.java
org.ale.scanner.zotero.data.Collection.java
org.ale.scanner.zotero.data.CreatorType.java
org.ale.scanner.zotero.data.Database.java
org.ale.scanner.zotero.data.Group.java
org.ale.scanner.zotero.data.ItemField.java
org.ale.scanner.zotero.data.ItemType.java
org.ale.scanner.zotero.web.APIHandler.java
org.ale.scanner.zotero.web.APIRequest.java
org.ale.scanner.zotero.web.HttpsClient.java
org.ale.scanner.zotero.web.RequestQueue.java
org.ale.scanner.zotero.web.googlebooks.GoogleBooksAPIClient.java
org.ale.scanner.zotero.web.googlebooks.GoogleBooksHandler.java
org.ale.scanner.zotero.web.worldcat.WorldCatAPIClient.java
org.ale.scanner.zotero.web.worldcat.WorldCatHandler.java
org.ale.scanner.zotero.web.zotero.ZoteroAPIClient.java
org.ale.scanner.zotero.web.zotero.ZoteroHandler.java