IBM/MFP-JSONStore-OfflineSync

Name: MFP-JSONStore-OfflineSync

Owner: International Business Machines

Description: null

Created: 2017-12-01 20:51:54.0

Updated: 2018-05-24 14:02:47.0

Pushed: 2018-05-24 14:02:46.0

Homepage: null

Size: 2880

Language: TypeScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

- Work in progress -

Secure Offline Synchronization using IBM Mobile Foundation

The increasing focus on Digital Transformation has led to more and more use cases where organizations want their enterprise apps to be usable even when the device is offline, and later synchronize the data with the enterprise when the device comes online again. In addition, organizations want to leverage the benefits of:

In this IBM code pattern, we will show you how to combine the following technologies to securely implement the mobile offline synchronization use case.

When you have completed this code pattern, you will understand:

Flow
Online scenario

Architecture diagram - online scenario

  1. When there is network connectivity, user installs and launches the mobile app, enters his/her credentials on the login screen and clicks Login.
  2. Mobile app sends the user credentials to MFP server for validation.
  3. MFP server invokes the security adapter logic to validate user credentials and returns an appropriate response to the mobile app. For the sake of this demo, we will use a simple security adapter that returns success when password equals username.
  4. If user authentication succeeds, mobile app initializes JSONStore collection with the current username and password.
  5. Mobile app initiates data synchronization with Cloudant database by making a call to MFP sync adapter.
  6. MFP sync adapter makes REST calls to Cloudant database and returns synchronization data to the mobile app. The data fetched from Cloudant database will have references to the images stored on Cloud Object Storage.
  7. In parallel to step 5 above, mobile app makes a call to MFP adapter to get the Authorization token for interacting with Cloud Object Storage service.
  8. MFP adapter makes a call to Cloud Object Storage service's token manager endpoint to get the Authorization token and returns it to the mobile app.
  9. Mobile app initializes image-caching plugin and asks it to use an HTTP header of Authorization=<value returned from MFP adapter> while fetching images.
  10. Once JSONStore synchronization is complete and Cloud Object Storage Authorization token is fetched, mobile app displays the synchronized data from JSONStore as a list of items on the Home page. The image caching plugin running on the mobile app downloads and caches images from Cloud Object Storage.
  11. User clicks on one of the list item to see more details. A detail page is shown consisting of image and geo-location marked inside Google Maps.
  12. Back in the home page, user clicks on + button to report a new civic problem. A new page is shown where user can enter a description for the new civic problem as well as capture image and geo-location of the problem spot. User clicks on Submit button.
  13. Mobile app stores the new data into JSONStore collection, which automatically initiates synchronization of the new data with Cloudant database by making a call to MFP sync adapter.
  14. MFP sync adapter POSTs the new data to Cloudant database.
  15. In parallel to step 14 above, mobile app creates a thumbnail image by resizing the captured image and uploads both the captured image and thumbnail to Cloud Object Storage.
  16. Other users who click on refresh button on the home page (and those who log in afresh) are shown the updated list of problem reports.
Offline scenario

Architecture diagram - offline scenario

  1. User launches the mobile app when the device is offline, enters his/her credentials on the login screen and clicks Login.
  2. Mobile app tries to initialize the JSONStore collection with the username and password entered by user. JSONStore init succeeds only if the correct password is entered. (Recollect that the JSONStore password was set when the device was last online and user authentication had succeeded after invocation of the MFP security adapter).
  3. If user authentication succeeds (through successful JSONStore init), mobile app reads data from the (previously synchronised) JSONStore collection, and shows the list of civic problems on the Home page.
  4. User can click on one of the problems to see more details. In case the problem detail was previously seen when the device was online, then the problem's image would have been cached by imgcache.js, and the Cordova plugin for Google Maps would make sure that the map view works even in offline mode.
  5. Back in the home page, user clicks on + button to report a new civic problem. A new page is shown where user can enter a description for the new civic problem as well as capture image and geo-location of the problem spot. User clicks on Submit button.
  6. Mobile app stores the new data in JSONStore collection, and the image and its thumbnail on local file storage. Back on the Home page, user can see the new problem listed.
  7. At at later time, when the device comes online, the mobile app automatically initiates the synchronization of JSONStore collection with Cloudant database by making a call to MFP sync adapter.
  8. MFP sync adapter POSTs new data to Cloudant database.
  9. In parallel to step 7 above, mobile app fetches Authorization token for interacting with Cloud Object Storage service by making a call to MFP adapter, and then uploads the new images to Cloud Object Storage.
  10. Other users who click on refresh button on the home page (and those who log in afresh) can see the newly reported civic problem and its details.
Included Components
Featured Technologies

Watch the Video

Steps

Prerequisite steps

This project builds on top of https://github.com/IBM/Ionic-MFP-App. Run following steps from that base project to provision the needed mobile backend services from IBM Cloud and populate them with sample data, as well as to setup Ionic and MFP CLI on your development machine.

Step 5. Download source repo and customize
5.1 Clone repo
t clone https://github.com/IBM/MFP-JSONStore-OfflineSync.git
 MFP-JSONStore-OfflineSync
5.2 Update App ID, Name and Description

Update IonicMobileApp/config.xml as below. Change id, name, description and author details appropriately.


<?xml version='1.0' encoding='utf-8'?>
<widget id="org.mycity.myward" version="2.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0" xmlns:mfp="http://www.ibm.com/mobilefirst/cordova-plugin-mfp">
    <name>MyWard</name>
    <description>Get your civic issues resolved by posting through this app.</description>
    <author email="shivahr@gmail.com" href="https://developer.ibm.com/code/author/shivahr/">Shiva Kumar H R</author>
...
5.3 Specify Cloudant credentials in MFP adapter

Open MobileFoundationAdapters/JSONStoreCloudantSync/src/main/adapter-resources/adapter.xml and update the following properties to point to the Cloudant database created in Step 2.


<mfp:adapter name="JSONStoreCloudantSync" ...>
  <property name="account" displayName="Cloudant account" defaultValue=""/>
  <property name="key" displayName="Cloudant key" defaultValue=""/>
  <property name="password" displayName="Cloudant password" defaultValue=""/>
  <property name="DBName" displayName="Cloudant DB name" defaultValue="myward"/>
  <property name="protocol" displayName="DB protocol" defaultValue="https" />
  <property name="port" displayName="Db port" defaultValue="443" />
  <property name="createDatabaseIfItDoesNotExist" displayName="Create database if it does not exist?" defaultValue="false" />
  ...
</mfp:adapter>
5.4 Specify Cloud Object Storage credentials in MFP Adapter

Open MobileFoundationAdapters/MyWardData/src/main/adapter-resources/adapter.xml and update the following properties to point to the Cloud Object Storage created in Step 3.


<mfp:adapter name="MyWardData" ...>
  ...
  <property name="endpointURL" displayName="Cloud Object Storage Endpoint Public URL" defaultValue="https://s3-api.us-geo.objectstorage.softlayer.net"/>
  <property name="bucketName" displayName="Cloud Object Storage Bucket Name" defaultValue=""/>
  <property name="serviceId" displayName="Cloud Object Storage Service ID" defaultValue=""  />
  <property name="apiKey" displayName="Cloud Object Storage API Key" defaultValue=""/>
</mfp:adapter>
Step 6. Deploy the MFP Adapters and Test them
6.1 Build and Deploy the MFP adapters

Build and deploy UserLogin Adapter as below.

 MobileFoundationAdapters/

 UserLogin
pdev adapter build
pdev adapter deploy

Note: In Step 4, if you specified No to Make this server the default?, then you need to specify the name of your server profile (Cloud-MFP in our case) at the end of mfpdev adapter deploy command as shown below.

pdev adapter deploy Cloud-MFP

Build and deploy MyWardData adapter as below.

 ../MyWardData/
pdev adapter build
pdev adapter deploy

Build and deploy JSONStoreCloudantSync adapter as below.

 ../JSONStoreCloudantSync/
pdev adapter build
pdev adapter deploy
6.2 Launch MFP dashboard and verify adapter configurations

Launch MFP Dashboard as below:

Verify MFP Adapter configuration as below:

6.3 Test the JSONStoreCloudantSync adapter

Create temporary credentials to test adapter REST API as below:

Test adapter REST API as below:

Delete the temporary credentials after testing adapter REST API as below:

Step 7. Run application on Android phone

Follow the instructions in Step 7 of base project to run the application on Android phone.

Step 8. Test the app functionality in offline mode
How the app works in offline mode

A note on how the offline mode is supported for each of the pages* in MyWard app: (*Ionic page is the equivalent of iOS View or Android Activity)

Note: In the current implementation, images and its thumbnails are stored as-is on the local file storage at cordova.file.dataDirectory which is private to the application. If you have a more stringent compliance requirement of having to encrypt the images stored locally on the phone, then follow the recommendations on this page.

8.1 Test app in online mode
8.2 Test app in offline mode

Troubleshooting

Please see instructions for debugging Android hybrid app using Chrome Developer Tools or troubleshooting guide for solutions to some commonly occuring problems.

References

License

Apache 2.0


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.