[Simulator] Minor UI changes fixing the IOT-1087.
[platform/upstream/iotivity.git] / cloud / account / src / main / java / org / iotivity / cloud / accountserver / db / AccountDBManager.java
1 /*
2  *******************************************************************
3  *
4  * Copyright 2016 Samsung Electronics All Rights Reserved.
5  *
6  *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  *
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22 package org.iotivity.cloud.accountserver.db;
23
24 import java.util.ArrayList;
25
26 import org.iotivity.cloud.accountserver.Const;
27
28 /**
29  *
30  * This class provides a set of APIs managing database.
31  *
32  */
33 public class AccountDBManager {
34
35     private static AccountDBManager accoutDBManager = new AccountDBManager();
36     private MongoDB                 mongoDB;
37
38     private AccountDBManager() {
39
40         try {
41
42             mongoDB = new MongoDB(Const.DB_NAME);
43
44             mongoDB.createTable(Const.DEVICE_TABLE);
45             mongoDB.createTable(Const.SESSION_TABLE);
46
47             registerAdminAccount();
48
49         } catch (Exception e) {
50
51             e.printStackTrace();
52
53         }
54     }
55
56     public static AccountDBManager getInstance() {
57
58         return accoutDBManager;
59     }
60
61     private void registerAdminAccount() {
62
63         String adminId = "admin";
64         String adminSessionCode = "00000000";
65
66         UserSession userSession = new UserSession();
67
68         userSession.setUserId(adminId);
69         userSession.setSessionCode(adminSessionCode);
70
71         mongoDB.createResource(userSession);
72         mongoDB.printResources();
73     }
74
75     /**
76      * API for storing session information of authorized user to mongoDB
77      * 
78      * @param userId
79      *            identifier of authorized user
80      * @param sessionCode
81      *            session code
82      * @return Boolean - true if stored, false if not
83      */
84     public Boolean registerUserSessionCode(String userId, String sessionCode) {
85
86         UserSession userSession = new UserSession();
87
88         userSession.setUserId(userId);
89         userSession.setSessionCode(sessionCode);
90
91         mongoDB.createResource(userSession);
92         mongoDB.printResources();
93
94         return true;
95     }
96
97     public Boolean registerUserDevice(String userId, String deviceId) {
98
99         UserDevice userDevice = new UserDevice();
100
101         userDevice.setUserId(userId);
102         userDevice.setDeviceId(deviceId);
103
104         mongoDB.createResource(userDevice);
105         mongoDB.printResources();
106
107         return true;
108     }
109
110     public String getUserId(String sessionCode) {
111
112         String userId = null;
113
114         userId = mongoDB.getUserId(sessionCode);
115
116         return userId;
117     }
118
119     /**
120      * API for getting devices according to authorized user
121      * 
122      * @param userId
123      *            identifier of authorized user
124      * @return ArrayList<String> - list of devices
125      */
126     public ArrayList<String> getDevices(String userId) {
127
128         ArrayList<String> deviceList = new ArrayList<String>();
129
130         deviceList = mongoDB.getDevices(userId);
131
132         return deviceList;
133     }
134 }