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.Constants;
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(Constants.DB_NAME);
44 mongoDB.createTable(Constants.DEVICE_TABLE);
45 mongoDB.createTable(Constants.SESSION_TABLE);
47 registerAdminAccount();
49 } catch (Exception e) {
56 public static AccountDBManager getInstance() {
58 return accoutDBManager;
62 * API for storing session information of authorized user
68 * @return Boolean - true if stored, otherwise false
70 public Boolean registerUserSessionCode(String userId, String sessionCode) {
72 UserSession userSession = new UserSession();
74 userSession.setUserId(userId);
75 userSession.setSessionCode(sessionCode);
77 mongoDB.createResource(userSession);
78 mongoDB.printResources();
84 * API for storing device information of authorized user
90 * @return Boolean - true if stored, otherwise false
92 public Boolean registerUserDevice(String userId, String deviceId) {
94 UserDevice userDevice = new UserDevice();
96 userDevice.setUserId(userId);
97 userDevice.setDeviceId(deviceId);
99 mongoDB.createResource(userDevice);
100 mongoDB.printResources();
106 * API for getting user identifier information corresponding with session
110 * identifier of authorized user
113 * @return Boolean - true if stored, otherwise false
115 public String getUserId(String sessionCode) {
117 String userId = null;
119 userId = mongoDB.getUserId(sessionCode);
125 * API for getting devices corresponding with user identifier
129 * @return ArrayList<String> - list of devices
131 public ArrayList<String> getDevices(String userId) {
133 ArrayList<String> deviceList = mongoDB.getDevices(userId);
138 private void registerAdminAccount() {
140 String adminId = "admin";
141 String adminSessionCode = "00000000";
143 UserSession userSession = new UserSession();
145 userSession.setUserId(adminId);
146 userSession.setSessionCode(adminSessionCode);
148 mongoDB.createResource(userSession);
149 mongoDB.printResources();