Example usage for android.database.sqlite SQLiteDatabase releaseReference

List of usage examples for android.database.sqlite SQLiteDatabase releaseReference

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteDatabase releaseReference.

Prototype

public void releaseReference() 

Source Link

Document

Releases a reference to the object, closing the object if the last reference was released.

Usage

From source file:org.opendatakit.survey.android.tasks.InstanceUploaderTask.java

@Override
protected InstanceUploadOutcome doInBackground(String... toUpload) {
    mOutcome = new InstanceUploadOutcome();
    mOutcome.mResults = new HashMap<String, String>();
    mOutcome.mAuthRequestingServer = null;

    String auth = PropertiesSingleton.getProperty(appName, PreferencesActivity.KEY_AUTH);
    setAuth(auth);/*  ww w  .j a v a 2 s. c o m*/

    String urlString = null;

    /**
     * retrieve the URL string for the table, if defined... otherwise, use the
     * app property values to construct it.
     */
    {
        SQLiteDatabase db = null;

        Cursor c = null;
        try {
            db = DatabaseFactory.get().getDatabase(appContext, appName);
            c = db.query(DatabaseConstants.KEY_VALUE_STORE_ACTIVE_TABLE_NAME, null,
                    KeyValueStoreColumns.TABLE_ID + "=? AND " + KeyValueStoreColumns.PARTITION + "=? AND "
                            + KeyValueStoreColumns.ASPECT + "=? AND " + KeyValueStoreColumns.KEY + "=?",
                    new String[] { uploadTableId, KeyValueStoreConstants.PARTITION_TABLE,
                            KeyValueStoreConstants.ASPECT_DEFAULT, KeyValueStoreConstants.XML_SUBMISSION_URL },
                    null, null, null);
            if (c.getCount() == 1) {
                c.moveToFirst();
                int idxValue = c.getColumnIndex(KeyValueStoreColumns.VALUE);
                urlString = c.getString(idxValue);
            } else if (c.getCount() != 0) {
                throw new IllegalStateException(
                        "two or more entries for " + KeyValueStoreConstants.XML_SUBMISSION_URL);
            }
        } finally {
            c.close();
            db.releaseReference();
        }

        if (urlString == null) {
            urlString = PropertiesSingleton.getProperty(appName, PreferencesActivity.KEY_SERVER_URL);
            String submissionUrl = PropertiesSingleton.getProperty(appName,
                    PreferencesActivity.KEY_SUBMISSION_URL);
            urlString = urlString + submissionUrl;
        }
    }

    // FormInfo fi = new FormInfo(appContext, appName,
    // ODKFileUtils.getuploadingForm.formDefFile);
    // get shared HttpContext so that authentication and cookies are
    // retained.
    HttpContext localContext = ClientConnectionManagerFactory.get(appName).getHttpContext();
    HttpClient httpclient = ClientConnectionManagerFactory.get(appName)
            .createHttpClient(WebUtils.CONNECTION_TIMEOUT);

    Map<URI, URI> uriRemap = new HashMap<URI, URI>();

    for (int i = 0; i < toUpload.length; ++i) {
        if (isCancelled()) {
            return mOutcome;
        }
        publishProgress(i + 1, toUpload.length);

        Uri toUpdate = Uri.withAppendedPath(InstanceProviderAPI.CONTENT_URI,
                appName + "/" + uploadTableId + "/" + StringEscapeUtils.escapeHtml4(toUpload[i]));
        Cursor c = null;
        try {
            c = appContext.getContentResolver().query(toUpdate, null, null, null, null);
            if (c.getCount() == 1 && c.moveToFirst()) {

                String id = ODKDatabaseUtils.get().getIndexAsString(c, c.getColumnIndex(InstanceColumns._ID));
                String dataTableInstanceId = ODKDatabaseUtils.get().getIndexAsString(c,
                        c.getColumnIndex(InstanceColumns.DATA_INSTANCE_ID));
                String lastOutcome = ODKDatabaseUtils.get().getIndexAsString(c,
                        c.getColumnIndex(InstanceColumns.XML_PUBLISH_STATUS));
                String submissionInstanceId = ODKDataUtils.genUUID();
                // submissions always get a new legacy instance id UNLESS the last
                // submission failed,
                // in which case we retry the submission using the legacy instance id
                // associated with
                // that failure. This supports resumption of sends of forms with many
                // attachments.
                if (lastOutcome != null && lastOutcome.equals(InstanceColumns.STATUS_SUBMISSION_FAILED)) {
                    String lastId = ODKDatabaseUtils.get().getIndexAsString(c,
                            c.getColumnIndex(InstanceColumns.SUBMISSION_INSTANCE_ID));
                    if (lastId != null) {
                        submissionInstanceId = lastId;
                    }
                }
                c.close();

                FileSet instanceFiles;
                try {
                    instanceFiles = constructSubmissionFiles(dataTableInstanceId, submissionInstanceId);
                    // NOTE: /submission must not be translated! It is
                    // the well-known path on the server.

                    if (!uploadOneSubmission(urlString, toUpdate, id, submissionInstanceId, instanceFiles,
                            httpclient, localContext, uriRemap)) {
                        return mOutcome; // get credentials...
                    }
                } catch (JsonParseException e) {
                    WebLogger.getLogger(appName).printStackTrace(e);
                    mOutcome.mResults.put(id, fail + "unable to obtain manifest: " + dataTableInstanceId
                            + " :: details: " + e.toString());
                } catch (JsonMappingException e) {
                    WebLogger.getLogger(appName).printStackTrace(e);
                    mOutcome.mResults.put(id, fail + "unable to obtain manifest: " + dataTableInstanceId
                            + " :: details: " + e.toString());
                } catch (IOException e) {
                    WebLogger.getLogger(appName).printStackTrace(e);
                    mOutcome.mResults.put(id, fail + "unable to obtain manifest: " + dataTableInstanceId
                            + " :: details: " + e.toString());
                }
            } else {
                mOutcome.mResults.put("unknown",
                        fail + "unable to retrieve instance information via: " + toUpdate.toString());
            }
        } finally {
            if (c != null && !c.isClosed()) {
                c.close();
            }
        }
    }

    return mOutcome;
}