How to get data from your Notes database to Android mobiles and use them offline? That was a task which we need to solve in recent days. Users should be able to edit and save their contacts, activities on Android mobiles and then send them back to Notes database. But not only texts, numbers, dates but also images, audio and files. And that all in offline mode as well. On top of that, the application should access mobile’s sensors like location, speed, camera etc. The last point means that we have to create native Android application, so we’ve chosen Android Studio to do the job but how to store data on mobile.
Where to store data on mobile
You can pick up from several options, how to store your data on mobiles. You can use text files or more obvious choice is SQLite database, in-memory mobile database for storing structured data. But we need to synchronize that data with server, which means write your own code which will handle all synchronization challenges, something like Notes replication and that is not quite easy. That is why we’ve selected Couchbase database which promises to solve that issue.
Couchbase mobile and Sync Gateway
Couchbase database is like Lotus Notes, no structured database, which stores its data in JSON format. Apart from standard server, it has also mobile version, which can be installed on mobile together with your application. Mobile application stores data in Couchbase Mobile and then replicate them with server part using Sync Gateway. Main replication features:
Push and pull replication
Replication can start on command or it can be continuous. It means that replication is active whenever user is connected to the internet. For instance, user saves his contact and that change is replicated to the server within seconds.
Replication includes not only primitive data but also images, files.
Using Couchbase infrastructure we get data from mobile devices to the Couchbase server, but we still need to move them to Notes databases.
Java synchronization agent between Couchbase and Notes
For the synchronization purpose we’ve created Java agent in Notes database, which compares modification dates on both sides then compares values of each record and creates, updates or deletes records on specific side. It was not as tricky as one can image. Especially replication conflicts can be avoided by setting up proper permissions, assign which device has higher priority, shortly by set up specific application logic. For Java agent development we’ve use Couchbase Java SDK. Here is a snapshot of Java code: