[Resource-container] Backported init fix to 1.1-rel
[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.Constants;
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(Constants.DB_NAME);
43
44             mongoDB.createTable(Constants.DEVICE_TABLE);
45             mongoDB.createTable(Constants.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     /**
62      * API for storing session information of authorized user
63      *
64      * @param userId
65      *            user identifier
66      * @param sessionCode
67      *            session code
68      * @return Boolean - true if stored, otherwise false
69      */
70     public Boolean registerUserSessionCode(String userId, String sessionCode) {
71
72         UserSession userSession = new UserSession();
73
74         userSession.setUserId(userId);
75         userSession.setSessionCode(sessionCode);
76
77         mongoDB.createResource(userSession);
78         mongoDB.printResources();
79
80         return true;
81     }
82
83     /**
84      * API for storing device information of authorized user
85      *
86      * @param userId
87      *            user identifier
88      * @param deviceId
89      *            device identifier
90      * @return Boolean - true if stored, otherwise false
91      */
92     public Boolean registerUserDevice(String userId, String deviceId) {
93
94         UserDevice userDevice = new UserDevice();
95
96         userDevice.setUserId(userId);
97         userDevice.setDeviceId(deviceId);
98
99         mongoDB.createResource(userDevice);
100         mongoDB.printResources();
101
102         return true;
103     }
104
105     /**
106      * API for getting user identifier information corresponding with session
107      * code
108      *
109      * @param userId
110      *            identifier of authorized user
111      * @param sessionCode
112      *            session code
113      * @return Boolean - true if stored, otherwise false
114      */
115     public String getUserId(String sessionCode) {
116
117         String userId = null;
118
119         userId = mongoDB.getUserId(sessionCode);
120
121         return userId;
122     }
123
124     /**
125      * API for getting devices corresponding with user identifier
126      *
127      * @param userId
128      *            user identifier
129      * @return ArrayList<String> - list of devices
130      */
131     public ArrayList<String> getDevices(String userId) {
132
133         ArrayList<String> deviceList = mongoDB.getDevices(userId);
134
135         return deviceList;
136     }
137
138     private void registerAdminAccount() {
139
140         String adminId = "admin";
141         String adminSessionCode = "00000000";
142
143         UserSession userSession = new UserSession();
144
145         userSession.setUserId(adminId);
146         userSession.setSessionCode(adminSessionCode);
147
148         mongoDB.createResource(userSession);
149         mongoDB.printResources();
150     }
151 }