[Huawei Developers] Sherlock F:
Introduction

Flight booking app allows user to search and book flight. In this article, we will integrate Huawei Cloud DB into demo flight booking application.

Huawei Cloud DB is a computing platform that provides functionality of traditional database with cloud computing. The cloud DB ensures data availability, reliability, consistency, security and enables seamless data synchronization between the device and cloud, and supports offline application operations, helps developers quickly develop device-cloud and multi-device synergy applications. Cloud DB builds the Mobile backend as a service capability for the AppGallery Connect platform. This provides developer to focus on application services, delivering the efficient product.

For prerequisite and set up, refer to part 1.

Usecase

1. Once user books the flight from the app, we will store flight booking information in cloud DB.

2. User can navigate to Itinerary Screen from navigation drawer to see all the flight booked by him in past.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201023112255.06257112802953580165845587573905:50511104060724:2800:883EC7720BB1FD896C9880298C5779ABE88BC3042DEA0C6A29F30521243A147A.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201023112255.06257112802953580165845587573905:50511104060724:2800:883EC7720BB1FD896C9880298C5779ABE88BC3042DEA0C6A29F30521243A147A.jpg[/IMG]

Permissions

By default, users who access Cloud DB from the AppGallery Connect console or cloud function have all permissions, as they are assigned with administrator role.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201023103740.24886363431043067281575893894737:50511104060724:2800:E054AD2D1E9A5843FBAE7C9CB70B892336A1BD309145908408A0D3D77A71C159.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201023103740.24886363431043067281575893894737:50511104060724:2800:E054AD2D1E9A5843FBAE7C9CB70B892336A1BD309145908408A0D3D77A71C159.png[/IMG]

Everyone : All users. This role has only query permission granted.

Authenticated user: Users who have passed the AppGallery Connect login.This role has the query permission by default and upsert and delete permission can be granted.

Data creator: Authenticated users who create data. Data creators can upsert or delete the data created only by themselves. The information about data creator is stored in data table system records. This role has all permissions by default and can customize the permissions.

Administrator: Developers who access Cloud DB from the AppGallery Connect console . This role has all permissions by default and can customize the permissions.

Accessing Cloud DB

Since Cloud DB is in beta stage, developers need to send a mail to activate this service in their app. Developer needs to create Huawei developer account. Once account created, developers need to create an app in App Gallery connect. After completing all the necessary steps, developer have to request the activation of the service by sending an e-mail to agconnect@huawei.com with the subject as "[Cloud DB]-[Company name]-[Developer account ID]-[App ID]". To access the app ID, see Querying App Information. Usually it takes 1 to 3 days for activation, once activated , developer can integrate cloud DB service in their app.

Getting started with Cloud DB

1. Log in to AppGallery Connect and select My Projects.

2. Select Application for which you want to create object types.

3. Choose Build > Cloud DB from left panel.

4. Click on Enable now.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201023103843.60715259947350346020481783808172:50511104060724:2800:936BA8D9F2688D78F70256A251E61F1BDFE2A6A96CA1B800111CDC29ACED1A5E.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201023103843.60715259947350346020481783808172:50511104060724:2800:936BA8D9F2688D78F70256A251E61F1BDFE2A6A96CA1B800111CDC29ACED1A5E.png[/IMG]

5. Click Add to create object type.

6. Set Object Type Name to EaseMyFlight, and click Next.

7. Click on Add fields , add the fields and click Next.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201023103923.31374416008230821101327592159494:50511104060724:2800:616505356EBB0704ED6B11C2BB4D2D1ED86D42169053FD39879663F32A94DD39.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201023103923.31374416008230821101327592159494:50511104060724:2800:616505356EBB0704ED6B11C2BB4D2D1ED86D42169053FD39879663F32A94DD39.png[/IMG]

8. Click Add Index, set Index Name and Indexed Field, and click Next.

9. Add permissions as follows and click Next. (For Everyone , upsert and delete access is not allowed).

10. Click Ok. Object types are created and displayed in the object type list.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201023103950.03753731838129454040503182928596:50511104060724:2800:891C4CE70AFC12AE481A02D1584F60E9ED88B7C0F1CEB23C49CABDD21FAEB004.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201023103950.03753731838129454040503182928596:50511104060724:2800:891C4CE70AFC12AE481A02D1584F60E9ED88B7C0F1CEB23C49CABDD21FAEB004.png[/IMG]

11. Click Export. Select JAVA as export file name. Enter the package name of JAVA file. package name can contain Letters A–Z or a–z, Digits 0–9, Special characters _ and .

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201023104008.93573960774723567586802463170601:50511104060724:2800:F4669E337408DAF226A2D2B967F033031151D414E2420ABE0CB05922D35E8E8B.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201023104008.93573960774723567586802463170601:50511104060724:2800:F4669E337408DAF226A2D2B967F033031151D414E2420ABE0CB05922D35E8E8B.png[/IMG]

12.Click Export. The file that contains all object types will be downloaded. Add the exported JAVA file in your project.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201023104105.99487422530084723605441900538369:50511104060724:2800:AFC8EE6CF2405305F807EFE8B8686017408C9BCF3446216D053EF649A082405A.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201023104105.99487422530084723605441900538369:50511104060724:2800:AFC8EE6CF2405305F807EFE8B8686017408C9BCF3446216D053EF649A082405A.png[/IMG]

Cloud DB Zone

1. Log in to AppGallery Connect and select My apps.

2. Select Application for which you want to create object types.

3. Choose Build > Cloud DB from left panel.

4. Click Cloud DB Zones tab.

5. Click Add to navigate to the Cloud DB zone creation page.

6. Enter easeMyFlightZone in the Cloud DB Zone Name text box.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201023104119.98112438751174455893080040887206:50511104060724:2800:92CD1D9763BEDD54BDB1EEF01A608DEE0B3D211A0795FE20EDA8489207CCAE9F.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20201023104119.98112438751174455893080040887206:50511104060724:2800:92CD1D9763BEDD54BDB1EEF01A608DEE0B3D211A0795FE20EDA8489207CCAE9F.png[/IMG]

Development

Prerequisites

Auth Service has to be integrated. Follow this tutorial to integrate Auth Service.

1. Add cloud DB SDK dependecy in app- level build.gradle.

// Add the Cloud DB SDK.
implementation 'com.huawei.agconnect:agconnect-database:1.2.1.301'

2. Add compatibility mode to JDK 1.8 in app- level build.gradle.

compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}

3. Initialize AGConnectCloudDB in Application class.

public class EaseMyFlightApplication extends Application {

@Override
public void onCreate() {
super.onCreate();

initAGConnectCloudDB(getApplicationContext());
}

public static void initAGConnectCloudDB(Context context) {
AGConnectCloudDB.initialize(context);
}
}

4. Get the instance of AGConnectCloudDB and create object types.

mCloudDB = AGConnectCloudDB.getInstance(); mCloudDB.createObjectType(ObjectTypeInfoHelper.getObjectTypeInfo());

5. Create the Cloud DB zone configuration file and open the Cloud DB zone.

mConfig = new CloudDBZoneConfig("easeMyFlightZone", CloudDBZoneConfig.CloudDBZoneSyncProperty.CLOUDDBZONE_CLOUD_CACHE,
CloudDBZoneConfig.CloudDBZoneAccessProperty.CLOUDDBZONE_PUBLIC);
mConfig.setPersistenceEnabled(true);
try {
mCloudDBZone = mCloudDB.openCloudDBZone(mConfig, true);
} catch (AGConnectCloudDBException e) {
Log.w(TAG, "openCloudDBZone: " + e.getMessage());
}

6. Writing Data

Data can be entered either manually from App Gallery Connect(administrator or data creater) or programmatically(authorized user) by using cloud DB SDK , if user has the permission. If same value in primary key is used, data will be modified.

public void insertDataToCloudDB(EaseMyFlight info) {
if (mCloudDBZone == null) {
Log.w(TAG, "CloudDBZone is null, try re-open it");
return;
}
CloudDBZoneTask<Integer> upsertTask = mCloudDBZone.executeUpsert(info);

upsertTask.addOnSuccessListener(new com.huawei.agconnect.cloud.database.OnSuccessListener<Integer>() {
@Override
public void onSuccess(Integer integer) {
Log.w(TAG, "upsert " + integer + " records");
}
}).addOnFailureListener(new com.huawei.agconnect.cloud.database.OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.e(TAG, "Insertion failed : "+ e.getMessage());
e.printStackTrace();
}
});

}

More information, you can visit https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201388320242800533&fid=0101187876626530001