Skip to content

Commit 2c98130

Browse files
committed
automatic copying of views for sync point channel
1 parent 6377a19 commit 2c98130

File tree

2 files changed

+96
-13
lines changed

2 files changed

+96
-13
lines changed

res/raw/coconut.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ app_db=couchabb
22
couchAppInstanceUrl=couchabb/_design/couchabb/index.html
33
local_couch_app_port=8888
44
networked_master_server=http://192.168.0.193:5984/
5-
master_server=http://192.168.16.100:5984/
5+
master_server=http://192.168.16.110:5984/
66
iris_master_server=http://vetula.iriscouch.com:5984
77
syncpoint_app_id=site1
8-
syncpoint_default_channel=couchabb
8+
syncpoint_default_channel=Master

src/com/couchbase/callback/AndroidCouchbaseCallback.java

Lines changed: 94 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,31 @@
2323
import java.net.MalformedURLException;
2424
import java.net.URL;
2525
import java.util.HashMap;
26+
import java.util.List;
2627
import java.util.Map;
2728
import java.util.Properties;
28-
import java.util.Random;
2929

3030
import org.apache.cordova.DroidGap;
3131
import org.ektorp.CouchDbConnector;
32+
import org.ektorp.CouchDbInstance;
3233
import org.ektorp.DbAccessException;
34+
import org.ektorp.ViewQuery;
35+
import org.ektorp.ViewResult;
36+
import org.ektorp.http.HttpClient;
37+
import org.ektorp.impl.StdCouchDbInstance;
38+
import org.ektorp.support.DesignDocument;
39+
import org.ektorp.support.SimpleViewGenerator;
3340

3441
import android.accounts.Account;
3542
import android.accounts.AccountManager;
36-
import android.app.Activity;
3743
import android.app.ProgressDialog;
3844
import android.content.Context;
3945
import android.content.Intent;
40-
import android.content.SharedPreferences;
4146
import android.content.res.AssetManager;
4247
import android.content.res.Resources;
4348
import android.os.Bundle;
4449
import android.os.Handler;
50+
import android.os.Looper;
4551
import android.util.Log;
4652
import android.view.Gravity;
4753
import android.view.KeyEvent;
@@ -58,13 +64,15 @@
5864
import com.couchbase.syncpoint.SyncpointClient;
5965
import com.couchbase.syncpoint.impl.SyncpointClientImpl;
6066
import com.couchbase.syncpoint.model.SyncpointChannel;
61-
import com.couchbase.touchdb.TDBody;
67+
import com.couchbase.syncpoint.model.SyncpointInstallation;
68+
import com.couchbase.syncpoint.model.SyncpointSession;
6269
import com.couchbase.touchdb.TDDatabase;
6370
import com.couchbase.touchdb.TDServer;
6471
import com.couchbase.touchdb.TDView;
72+
import com.couchbase.touchdb.TDViewMapBlock;
73+
import com.couchbase.touchdb.ektorp.TouchDBHttpClient;
6574
import com.couchbase.touchdb.javascript.TDJavaScriptViewCompiler;
6675
import com.couchbase.touchdb.listener.TDListener;
67-
import com.couchbase.touchdb.replicator.TDReplicator;
6876

6977
public class AndroidCouchbaseCallback extends DroidGap
7078
{
@@ -152,7 +160,6 @@ public void onCreate(Bundle savedInstanceState)
152160
System.err.println("Failed to open microlog property file");
153161
}
154162

155-
156163
try {
157164
server = new TDServer(filesDir);
158165
listener = new TDListener(server, 8888);
@@ -237,6 +244,26 @@ public void onCreate(Bundle savedInstanceState)
237244
// create the syncpoint client
238245
try {
239246
syncpoint = new SyncpointClientImpl(getApplicationContext(), masterServerUrl, Constants.syncpointAppId);
247+
SyncpointChannel channel = syncpoint.getMyChannel(syncpointDefaultChannelName);
248+
SyncpointInstallation inst = channel.getInstallation(getApplicationContext());
249+
if(inst != null) {
250+
CouchDbConnector localDatabase = inst.getLocalDatabase(getApplicationContext());
251+
String localDatabaseName = localDatabase.getDatabaseName();
252+
Log.v(TAG, "localDatabaseName: " + localDatabaseName);
253+
TDDatabase origDb = server.getDatabaseNamed(appDb);
254+
origDb.open();
255+
TDDatabase newDb = server.getDatabaseNamed(localDatabaseName);
256+
newDb.open();
257+
List<TDView> views = origDb.getAllViews();
258+
for (TDView tdView : views) {
259+
//TDView origView = origDb.getExistingViewNamed("aview");
260+
TDViewMapBlock mapBlock = tdView.getMapBlock();
261+
TDView newView = newDb.getViewNamed("aview");
262+
newView.setMapReduceBlocks(mapBlock, null, "1");
263+
}
264+
origDb.close();
265+
newDb.close();
266+
}
240267
} catch (DbAccessException e1) {
241268
Log.e( TAG, "Error: " , e1);
242269
e1.printStackTrace();
@@ -259,10 +286,6 @@ public void onCreate(Bundle savedInstanceState)
259286
// replPull.start();
260287
// TDReplicator replPush = db.getReplicator(remote, true, true);
261288
// replPush.start();
262-
263-
// TODO: Check if the pairing has been approved.
264-
SyncpointChannel channel = syncpoint.getMyChannel(Constants.syncpointDefaultChannelName);
265-
CouchDbConnector database = channel.ensureLocalDatabase(getApplicationContext());
266289
} catch (Exception e) {
267290
// TODO Auto-generated catch block
268291
e.printStackTrace();
@@ -370,6 +393,14 @@ private void register() {
370393
if (selectedAccount != null) {
371394
try {
372395
syncpoint.pairSession("console", selectedAccount.name);
396+
HttpClient httpClient = new TouchDBHttpClient(server);
397+
CouchDbInstance localServer = new StdCouchDbInstance(httpClient);
398+
//CouchDbConnector userDb = localServer.createConnector("_users", false);
399+
CouchDbConnector localControlDatabase = localServer.createConnector(SyncpointClientImpl.LOCAL_CONTROL_DATABASE_NAME, false);
400+
//PairingUser pairingUser = session.getPairingUser();
401+
//PairingUser result = userDb.get(PairingUser.class, pairingUser.getId());
402+
//waitForPairingToComplete(localServer, localControlDatabase);
403+
pairingDidComplete(localServer);
373404
} catch (DbAccessException e) {
374405
Log.e( TAG, "Error: " , e);
375406
Toast.makeText(this, "Error: Unable to connect to Syncpoint Server: " + e.getMessage(), Toast.LENGTH_LONG).show();
@@ -378,7 +409,6 @@ private void register() {
378409
}
379410
}
380411
}
381-
382412
private void unregister() {
383413
if( registered ) {
384414
Log.d( TAG, "unregister()" );
@@ -387,6 +417,59 @@ private void unregister() {
387417
}
388418
}
389419

420+
void waitForPairingToComplete(final CouchDbInstance localServer, final CouchDbConnector localControlDatabase) {
421+
Log.v(TAG, "Waiting for pairing to complete...");
422+
Looper l = Looper.getMainLooper();
423+
Handler h = new Handler(l);
424+
h.postDelayed(new Runnable() {
425+
426+
@Override
427+
public void run() {
428+
String message = "Checking to see if account has been authorized.";
429+
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
430+
Log.v(TAG, message);
431+
SyncpointSession session = SyncpointSession.sessionInDatabase(getApplicationContext(), localServer, localControlDatabase);
432+
if (session.isPaired()) {
433+
pairingDidComplete(localServer);
434+
} else {
435+
message = "Authorization is in progress. ";
436+
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
437+
Log.v(TAG, message);
438+
waitForPairingToComplete(localServer, localControlDatabase);
439+
}
440+
/*PairingUser user = remote.get(PairingUser.class, userDoc.getId());
441+
if("paired".equals(user.getPairingState())) {
442+
pairingDidComplete(localServer);
443+
} else {
444+
message = "Pairing state is stuck at " + user.getPairingState();
445+
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
446+
Log.v(TAG, message);
447+
waitForPairingToComplete(localServer, session, remote, user);
448+
}*/
449+
}
450+
}, 3000);
451+
}
452+
453+
void pairingDidComplete(final CouchDbInstance localServer) {
454+
//CouchDbConnector localControlDatabase = localServer.createConnector(SyncpointClientImpl.LOCAL_CONTROL_DATABASE_NAME, false);
455+
//SyncpointSession session = SyncpointSession.sessionInDatabase(getApplicationContext(), localServer, localControlDatabase);
456+
//if(session != null) {
457+
//if(session.isPaired()) {
458+
459+
String message = "Authorization Completed";
460+
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
461+
Log.v(TAG, message);
462+
SyncpointChannel channel = syncpoint.getMyChannel(Constants.syncpointDefaultChannelName);
463+
CouchDbConnector database = channel.ensureLocalDatabase(getApplicationContext());
464+
// create a sample document to verify that replication in the channel is working
465+
Map<String,Object> testObject = new HashMap<String,Object>();
466+
testObject.put("key", "value");
467+
// store the document
468+
database.create(testObject);
469+
//}
470+
//}
471+
}
472+
390473
// kudos: http://stackoverflow.com/questions/6058843/android-how-to-select-texts-from-webview
391474
private void emulateShiftHeld(WebView view)
392475
{

0 commit comments

Comments
 (0)