2 *******************************************************************
4 * Copyright 2016 Samsung Electronics All Rights Reserved.
6 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 package org.iotivity.cloud.accountserver.db;
24 import java.util.ArrayList;
26 import org.iotivity.cloud.accountserver.Const;
30 * This class provides a set of APIs managing database.
33 public class AccountDBManager {
35 private static AccountDBManager accoutDBManager = new AccountDBManager();
36 private MongoDB mongoDB;
38 private AccountDBManager() {
42 mongoDB = new MongoDB(Const.DB_NAME);
44 mongoDB.createTable(Const.DEVICE_TABLE);
45 mongoDB.createTable(Const.SESSION_TABLE);
47 registerAdminAccount();
49 } catch (Exception e) {
56 public static AccountDBManager getInstance() {
58 return accoutDBManager;
61 private void registerAdminAccount() {
63 String adminId = "admin";
64 String adminSessionCode = "00000000";
66 UserSession userSession = new UserSession();
68 userSession.setUserId(adminId);
69 userSession.setSessionCode(adminSessionCode);
71 mongoDB.createResource(userSession);
72 mongoDB.printResources();
76 * API for storing session information of authorized user to mongoDB
79 * identifier of authorized user
82 * @return Boolean - true if stored, false if not
84 public Boolean registerUserSessionCode(String userId, String sessionCode) {
86 UserSession userSession = new UserSession();
88 userSession.setUserId(userId);
89 userSession.setSessionCode(sessionCode);
91 mongoDB.createResource(userSession);
92 mongoDB.printResources();
97 public Boolean registerUserDevice(String userId, String deviceId) {
99 UserDevice userDevice = new UserDevice();
101 userDevice.setUserId(userId);
102 userDevice.setDeviceId(deviceId);
104 mongoDB.createResource(userDevice);
105 mongoDB.printResources();
110 public String getUserId(String sessionCode) {
112 String userId = null;
114 userId = mongoDB.getUserId(sessionCode);
120 * API for getting devices according to authorized user
123 * identifier of authorized user
124 * @return ArrayList<String> - list of devices
126 public ArrayList<String> getDevices(String userId) {
128 ArrayList<String> deviceList = new ArrayList<String>();
130 deviceList = mongoDB.getDevices(userId);