com.polyvi.xface.extension.XStorageExt.java Source code

Java tutorial

Introduction

Here is the source code for com.polyvi.xface.extension.XStorageExt.java

Source

/*
 This file was modified from or inspired by Apache Cordova.
    
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements. See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership. The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License. You may obtain a copy of the License at
    
http://www.apache.org/licenses/LICENSE-2.0
    
 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied. See the License for the
 specific language governing permissions and limitations
 under the License.
*/

package com.polyvi.xface.extension;

import java.io.File;
import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.polyvi.xface.extension.XExtensionResult.Status;
import com.polyvi.xface.plugin.api.XIWebContext;
import com.polyvi.xface.util.XFileUtils;
import com.polyvi.xface.util.XLog;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

public class XStorageExt extends XExtension {

    private static final String CLASS_NAME = XStorageExt.class.getSimpleName();

    private static final String COMMAND_OPEN_DATABASE = "openDatabase";

    private static final String COMMAND_EXECUTE_SQL = "executeSql";

    private static final String DDL_COMMAND_ALTER = "alter";

    private static final String DDL_COMMAND_CREATE = "create";

    private static final String DDL_COMMAND_DROP = "drop";

    private static final String DDL_COMMAND_TRUNCATE = "truncate";

    private SQLiteDatabase mMyDb = null;
    /**<Database object*/

    private static final String DB_DIR_NAME = "database";

    @Override
    public void sendAsyncResult(String result) {
    }

    @Override
    public boolean isAsync(String action) {
        return false;
    }

    @Override
    public XExtensionResult exec(String action, JSONArray args, XCallbackContext callbackCtx) throws JSONException {
        try {
            Status status = Status.OK;
            if (COMMAND_OPEN_DATABASE.equals(action)) {
                boolean isDatabaseopen = openDatabase(mWebContext, args.getString(0));
                if (!isDatabaseopen) {
                    status = Status.ERROR;
                }
            } else if (COMMAND_EXECUTE_SQL.equals(action)) {
                status = Status.NO_RESULT;
                String[] params = null;
                if (args.isNull(1)) {
                    params = new String[0];
                } else {
                    JSONArray paramJsonArray = args.getJSONArray(1);
                    int len = paramJsonArray.length();
                    params = new String[len];
                    for (int i = 0; i < len; i++) {
                        params[i] = paramJsonArray.getString(i);
                    }
                }
                executeSql(args.getString(0), params, args.getString(2), callbackCtx);
            }
            return new XExtensionResult(status);
        } catch (JSONException e) {
            return new XExtensionResult(Status.ERROR);
        }
    }

    /**
     * ?.
     *
     * @param db
     *            ???
     */
    private boolean openDatabase(XIWebContext webContext, String db) {
        if (null != mMyDb) {
            mMyDb.close();
        }

        File dbDir = new File(webContext.getApplication().getDataDir(), DB_DIR_NAME);
        if (!dbDir.exists()) {
            dbDir.mkdirs();
        }
        boolean isValidDbPath = true;
        try {
            // ???openDatabase???null??null???????
            String dbPath = new File(dbDir, db).getCanonicalPath();
            // ?????appData?database
            if (!XFileUtils.isFileAncestorOf(dbDir.getAbsolutePath(), dbPath)) {
                XLog.e(CLASS_NAME, dbPath);
                isValidDbPath = false;
            } else {
                mMyDb = SQLiteDatabase.openOrCreateDatabase(dbPath, null);
            }
        } catch (IOException e) {
            isValidDbPath = false;
            XLog.e(CLASS_NAME, e.toString());
        }
        return isValidDbPath;
    }

    /**
     * SQL?.
     *
     * @param query
     *            SQL?
     * @param params
     *            SQL??
     * @param tx_id
     *             id
     * @param callbackCtx
     *            
     */
    private void executeSql(String query, String[] params, String tx_id, XCallbackContext callbackCtx) {
        try {
            if (isDDL(query)) {
                mMyDb.execSQL(query);
                String jsScript = "xFace.require('xFace/extension/android/storage').completeQuery('" + tx_id
                        + "', '');";
                mWebContext.getApplication().loadJavascript(jsScript);
            } else {
                Cursor myCursor = mMyDb.rawQuery(query, params);
                processResults(myCursor, tx_id, callbackCtx);
                myCursor.close();
            }
        } catch (SQLiteException ex) {
            XLog.e(CLASS_NAME, "Storage.executeSql(): Error=" + ex.getMessage());
            String jsScript = "xFace.require('xFace/extension/android/storage').failQuery('" + ex.getMessage()
                    + "','" + tx_id + "');";
            callbackCtx.sendExtensionResult(jsScript);
        }
    }

    /**
     * SQL???
     *
     * @param query SQL?
     * @return true ?, false ???
     */
    private boolean isDDL(String query) {
        String cmd = query.toLowerCase();
        if (cmd.startsWith(DDL_COMMAND_ALTER) || cmd.startsWith(DDL_COMMAND_CREATE)
                || cmd.startsWith(DDL_COMMAND_DROP) || cmd.startsWith(DDL_COMMAND_TRUNCATE)) {
            return true;
        }
        return false;
    }

    /**
     * ?.
     *
     * @param cur
     *            
     * @param tx_id
     *             id
     * @param callbackCtx
     *            
     */
    private void processResults(Cursor cur, String tx_id, XCallbackContext callbackCtx) {

        String result = "[]";

        if (cur.moveToFirst()) {
            JSONArray fullresult = new JSONArray();
            String key = "";
            String value = "";
            int colCount = cur.getColumnCount();

            do {
                JSONObject row = new JSONObject();
                try {
                    for (int i = 0; i < colCount; ++i) {
                        key = cur.getColumnName(i);
                        value = cur.getString(i);
                        row.put(key, value);
                    }
                    fullresult.put(row);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            } while (cur.moveToNext());

            result = fullresult.toString();
        }

        String jsScript = "xFace.require('xFace/extension/android/storage').completeQuery('" + tx_id + "', "
                + result + ");";
        mWebContext.getApplication().loadJavascript(jsScript);
    }
}