Maulik M. Dodia Maulik M. Dodia - 2 months ago 13
Android Question

How to know that specific document is present in Cloudant Database using Android App

I have successfully created document in Cloudant database through my Android App. But I can not find a specific document present in Cloudant database. What I have tried is like below....

TabTwo_Fragment.java

package com.example.android02.insightapp11;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudant.sync.datastore.BasicDocumentRevision;
import com.cloudant.sync.datastore.Datastore;
import com.cloudant.sync.datastore.DatastoreManager;
import com.cloudant.sync.datastore.DatastoreNotCreatedException;
import com.cloudant.sync.datastore.DocumentBodyFactory;
import com.cloudant.sync.datastore.DocumentException;
import com.cloudant.sync.datastore.DocumentNotFoundException;
import com.cloudant.sync.datastore.DocumentRevision;
import com.cloudant.sync.datastore.MutableDocumentRevision;
import com.cloudant.sync.replication.Replicator;
import com.cloudant.sync.replication.ReplicatorBuilder;

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

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

/**
* A simple {@link Fragment} subclass.
*/
public class TabTwo_Fragment extends Fragment implements View.OnClickListener, SharedPreferences.OnSharedPreferenceChangeListener {

private static final String DATASTORE_MANGER_DIR = "data";
private static final String TASKS_DATASTORE_NAME = "tasks";

Datastore DataStore;
private Replicator PushReplicator, PullReplicator;
DocumentRevision revision;

static final String CLOUDANT_USER = "user_default";
static final String CLOUDANT_DB = "database_name";
static final String CLOUDANT_API_KEY = "api_key";
static final String CLOUDANT_API_SECRET = "api_key_pwd";

long TWITTER_ID = MainActivity.twitterID;
String TWITTER_NAME = MainActivity.userName;

Button btn_submit;
Spinner sp1, sp2, sp3, sp4, sp5, sp6, sp7, sp8;
TextView ans_total;

String user = "old";
HashMap<String, String> qa_hashmap;

public TabTwo_Fragment() {
// Required empty public constructor
}

public static TabTwo_Fragment newInstance() {
return new TabTwo_Fragment();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.tabtwo_fragment, container, false);
try {
init(rootView);
} catch (DocumentNotFoundException e) {
} catch (DocumentException e) {
} catch (URISyntaxException e) {
}
return rootView;
}

void init(View rootView) throws DocumentException, URISyntaxException {
Log.e("init: ", "in init");
find_view_by_id(rootView);
registerClickEvents();
defaultConfig();
checkForRegisteredUser();
}

void find_view_by_id(View rootView) {
Log.e("find_view_by_id: ", "in find_view_by_id");
btn_submit = (Button) rootView.findViewById(R.id.btn_submit);
sp1 = (Spinner) rootView.findViewById(R.id.sp1);
sp2 = (Spinner) rootView.findViewById(R.id.sp2);
sp3 = (Spinner) rootView.findViewById(R.id.sp3);
sp4 = (Spinner) rootView.findViewById(R.id.sp4);
sp5 = (Spinner) rootView.findViewById(R.id.sp5);
sp6 = (Spinner) rootView.findViewById(R.id.sp6);
sp7 = (Spinner) rootView.findViewById(R.id.sp7);
sp8 = (Spinner) rootView.findViewById(R.id.sp8);
ans_total = (TextView) rootView.findViewById(R.id.ans_total);
}

void registerClickEvents() {
Log.e("registerClickEvents: ", "in registerClickEvents");
btn_submit.setOnClickListener(this);
}

void defaultConfig() {
Log.e("defaultConfig: ", "in defaultConfig");
PreferenceManager.setDefaultValues(getContext(), R.xml.preferences, false);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
sharedPref.registerOnSharedPreferenceChangeListener(this);

File path = getContext().getDir(DATASTORE_MANGER_DIR, Context.MODE_PRIVATE);
DatastoreManager manager = new DatastoreManager(path.getAbsolutePath());

try {
DataStore = manager.openDatastore(TASKS_DATASTORE_NAME);
setUriPlusReplicators();
} catch (DatastoreNotCreatedException e) {
Toast.makeText(getContext(), "Unable to open Datastore!", Toast.LENGTH_SHORT).show();
} catch (URISyntaxException e) {
Toast.makeText(getContext(), "URI Exception!!", Toast.LENGTH_SHORT).show();
}
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
try {
setUriPlusReplicators();
} catch (URISyntaxException e) {
}
}

void setUriPlusReplicators() throws URISyntaxException {
Log.e("setUriPlusReplicators: ", "in setUriPlusReplicators");
URI uri = this.createServerUri();
Log.e("URI: ", "" + uri);
PushReplicator = ReplicatorBuilder.push().from(DataStore).to(uri).build();
PushReplicator.getEventBus().register(this);
PullReplicator = ReplicatorBuilder.pull().to(DataStore).from(uri).build();
PullReplicator.getEventBus().register(this);
}

private URI createServerUri() throws URISyntaxException {
Log.e("createServerUri: ", "in createServerUri");
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
String username = sharedPref.getString(CLOUDANT_USER, "");
String dbName = sharedPref.getString(CLOUDANT_DB, "");
String apiKey = sharedPref.getString(CLOUDANT_API_KEY, "");
String apiSecret = sharedPref.getString(CLOUDANT_API_SECRET, "");
String host = username + ".cloudant.com";
return new URI("https", apiKey + ":" + apiSecret, host, 443, "/" + dbName, null, null);
}

private URI createServerUri2(long twitterId) throws URISyntaxException {
Log.e("createServerUri2: ", "in createServerUri2");
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
String username = sharedPref.getString(CLOUDANT_USER, "");
String dbName = sharedPref.getString(CLOUDANT_DB, "");
String apiKey = sharedPref.getString(CLOUDANT_API_KEY, "");
String apiSecret = sharedPref.getString(CLOUDANT_API_SECRET, "");
String host = username + ".cloudant.com";
return new URI("https", apiKey + ":" + apiSecret, host, 443, "/" + dbName + "/_all_docs?", null, null);
}

void checkForRegisteredUser() throws DocumentNotFoundException, DocumentException, URISyntaxException {
BasicDocumentRevision retrieved = DataStore.getDocument(TWITTER_ID + "");
if (user.equals("new")) {
setQuizData(retrieved.getBody().toString());
} else {
newUser();
}
}

void setQuizData(String DataRetrieved) {
Log.e("setQuizData: ", "In setQuizData");
user = "old";
Log.e("DataRetrieved: ", "In " + DataRetrieved);
String[] ANSWERS = getResources().getStringArray(R.array.ans);
try {
JSONObject jsonObject = new JSONObject(DataRetrieved);
JSONObject jsonObject1 = jsonObject.getJSONObject("questions");
String qs1 = jsonObject1.getString("q1");
String qs2 = jsonObject1.getString("q2");
String qs3 = jsonObject1.getString("q3");
String qs4 = jsonObject1.getString("q4");
String qs5 = jsonObject1.getString("q5");
String qs6 = jsonObject1.getString("q6");
String qs7 = jsonObject1.getString("q7");
String qs8 = jsonObject1.getString("q8");

sp1.setSelection(Arrays.asList(ANSWERS).indexOf(qs1));
sp2.setSelection(Arrays.asList(ANSWERS).indexOf(qs2));
sp3.setSelection(Arrays.asList(ANSWERS).indexOf(qs3));
sp4.setSelection(Arrays.asList(ANSWERS).indexOf(qs4));
sp5.setSelection(Arrays.asList(ANSWERS).indexOf(qs5));
sp6.setSelection(Arrays.asList(ANSWERS).indexOf(qs6));
sp7.setSelection(Arrays.asList(ANSWERS).indexOf(qs7));
sp8.setSelection(Arrays.asList(ANSWERS).indexOf(qs8));
} catch (JSONException e) {
}
}

void newUser() {
Log.e("newUser: ", "In newUser");
user = "new";
}

void createDocument() throws DocumentException {
Log.e("createDocument: ", "In createDocument");
MutableDocumentRevision rev = new MutableDocumentRevision();
rev.docId = TWITTER_ID + "";
gatherQAData();

/*HashMap<String, Object> map_main = new HashMap<String, Object>();*/
HashMap<String, Object> map = new HashMap<String, Object>();
ArrayList<HashMap<String, Object>> arrayTeachers = new ArrayList<>();

map.put("twitter_id", TWITTER_ID + "");
map.put("twitter_name", TWITTER_NAME);
map.put("questions", qa_hashmap);
/*arrayTeachers.add(map);*/
/*map_main.put("user", arrayTeachers);*/

rev.body = DocumentBodyFactory.create(map);
revision = DataStore.createDocumentFromRevision(rev);
PushReplicator.start();
}

void updateDocument() {
Log.e("updateDocument: ", "In updateDocument");
}

void gatherQAData() {
Log.e("gatherQAData: ", "In gatherQAData");
qa_hashmap = new HashMap<String, String>();
String a1, a2, a3, a4, a5, a6, a7, a8;
a1 = sp1.getSelectedItem().toString();
a2 = sp2.getSelectedItem().toString();
a3 = sp3.getSelectedItem().toString();
a4 = sp4.getSelectedItem().toString();
a5 = sp5.getSelectedItem().toString();
a6 = sp6.getSelectedItem().toString();
a7 = sp7.getSelectedItem().toString();
a8 = sp8.getSelectedItem().toString();
qa_hashmap.put("q1", a1);
qa_hashmap.put("q2", a2);
qa_hashmap.put("q3", a3);
qa_hashmap.put("q4", a4);
qa_hashmap.put("q5", a5);
qa_hashmap.put("q6", a6);
qa_hashmap.put("q7", a7);
qa_hashmap.put("q8", a8);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_submit:
if (user.equals("new")) {
try {
createDocument();
} catch (DocumentException e) {
}
} else {
updateDocument();
}
break;
default:
}
}
}


Doc Stored in Cloudant DB

I can get the document which is stored in DataStore. But I am unable to get document stored in Cloudant database.

FYI: I am finding document in
checkForRegisteredUser
method.

Answer

Yes, I have done it. Below is my code for checkForRegisteredUser method of TabTwo_Fragment.java

void checkForRegisteredUser() {
        String url = "https://your_cloudant_user_name.cloudant.com/" + DB_NAME + "/" + DOC_ID;
        RequestQueue requestQueue = Volley.newRequestQueue(getContext());
        JsonObjectRequest jor = new JsonObjectRequest(Request.Method.GET, url, (String) null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                if (response.toString().length() > 0) {
                    setQuizData(response.toString());
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                newUser();
            }
        });
        requestQueue.add(jor);

Here is s Official Documentation LINK.

Here DOC_ID is the id of document that you want to check. Hope this will help to somebody.