Implement acl/group resource in cloud account server.
[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 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.Map;
28 import java.util.Map.Entry;
29 import java.util.Set;
30
31 import org.bson.Document;
32 import org.iotivity.cloud.accountserver.Constants;
33 import org.iotivity.cloud.base.exception.ServerException.InternalServerErrorException;
34
35 /**
36  *
37  * This class provides a set of APIs managing database.
38  *
39  */
40 public class AccountDBManager {
41
42     private static AccountDBManager            accoutDBManager = new AccountDBManager();
43
44     private MongoDB                            mongoDB;
45
46     private HashMap<String, ArrayList<String>> keyField        = new HashMap<String, ArrayList<String>>();
47
48     private AccountDBManager() {
49
50         createDatabase();
51         createTables();
52         createIndexes();
53     }
54
55     private void createDatabase() {
56
57         try {
58
59             mongoDB = new MongoDB(Constants.DB_NAME);
60         } catch (Exception e) {
61             e.printStackTrace();
62             throw new InternalServerErrorException(
63                     "Database or Table create failed!");
64         }
65     }
66
67     private void createTables() {
68
69         mongoDB.createTable(Constants.USER_TABLE);
70         mongoDB.createTable(Constants.TOKEN_TABLE);
71         mongoDB.createTable(Constants.GROUP_TABLE);
72         mongoDB.createTable(Constants.DEVICE_TABLE);
73         mongoDB.createTable(Constants.ACL_TABLE);
74         mongoDB.createTable(Constants.ACLTEMPLATE_TABLE);
75     }
76
77     private void createIndexes() {
78
79         ArrayList<String> keys = new ArrayList<>();
80         keys.add(Constants.KEYFIELD_UUID);
81
82         mongoDB.createIndex(Constants.USER_TABLE, keys);
83         keyField.put(Constants.USER_TABLE, keys);
84
85         keys = new ArrayList<>();
86         keys.add(Constants.KEYFIELD_UUID);
87         keys.add(Constants.KEYFIELD_DID);
88
89         mongoDB.createIndex(Constants.TOKEN_TABLE, keys);
90         keyField.put(Constants.TOKEN_TABLE, keys);
91
92         keys = new ArrayList<>();
93         keys.add(Constants.KEYFIELD_GID);
94
95         mongoDB.createIndex(Constants.GROUP_TABLE, keys);
96         keyField.put(Constants.GROUP_TABLE, keys);
97
98         keys = new ArrayList<>();
99         keys.add(Constants.KEYFIELD_UUID);
100         keys.add(Constants.KEYFIELD_DID);
101
102         mongoDB.createIndex(Constants.DEVICE_TABLE, keys);
103         keyField.put(Constants.DEVICE_TABLE, keys);
104
105         keys = new ArrayList<>();
106         keys.add(Constants.KEYFIELD_ACLID);
107
108         mongoDB.createIndex(Constants.ACL_TABLE, keys);
109         keyField.put(Constants.ACL_TABLE, keys);
110
111         keys = new ArrayList<>();
112         keys.add(Constants.KEYFIELD_GTYPE);
113
114         mongoDB.createIndex(Constants.ACLTEMPLATE_TABLE, keys);
115         keyField.put(Constants.ACLTEMPLATE_TABLE, keys);
116
117     }
118
119     public static AccountDBManager getInstance() {
120
121         return accoutDBManager;
122     }
123
124     /**
125      * API for inserting a record into DB table. the record will not be inserted
126      * if duplicated one.
127      * 
128      * @param tableName
129      *            table name to be inserted
130      * @param record
131      *            record to be inserted
132      */
133     public void insertRecord(String tableName, HashMap<String, Object> insert) {
134
135         if (!_insertRecord(tableName, insert))
136             throw new InternalServerErrorException(
137                     "Database record insert failed");
138     }
139
140     /**
141      * API for inserting a record into DB table. the record will be replaced if
142      * duplicated one.
143      * 
144      * @param tableName
145      *            table name to be inserted
146      * @param replace
147      *            record to be inserted
148      */
149     public void insertAndReplaceRecord(String tableName,
150             HashMap<String, Object> replace) {
151
152         if (!_insertAndReplaceRecord(tableName, replace))
153             throw new InternalServerErrorException(
154                     "Database record insert failed");
155     }
156
157     /**
158      * API for selecting records from DB table.
159      * 
160      * @param tableName
161      *            table name to be inserted
162      * @param condition
163      *            condition record to be selected
164      * @return selected records
165      */
166     public ArrayList<HashMap<String, Object>> selectRecord(String tableName,
167             HashMap<String, Object> condition) {
168
169         return _selectRecord(tableName, condition);
170     }
171
172     /**
173      * API for deleting records from DB table.
174      * 
175      * @param tableName
176      *            table name to be inserted
177      * @param condition
178      *            condition record to be deleted
179      */
180     public void deleteRecord(String tableName,
181             HashMap<String, Object> condition) {
182
183         if (!_deleteRecord(tableName, condition))
184             throw new InternalServerErrorException(
185                     "Database record delete failed");
186     }
187
188     /**
189      * API for updating a record into DB table.
190      * 
191      * @param tableName
192      *            table name to be inserted
193      * @param replace
194      *            record to be updated
195      */
196     public void updateRecord(String tableName,
197             HashMap<String, Object> replace) {
198
199         if (!_updateRecord(tableName, replace))
200             throw new InternalServerErrorException(
201                     "Database record update failed");
202
203     }
204
205     private Boolean _insertRecord(String tableName,
206             HashMap<String, Object> record) {
207
208         Document doc = createDocument(record);
209
210         return mongoDB.insertRecord(tableName, doc);
211     }
212
213     private Boolean _insertAndReplaceRecord(String tableName,
214             HashMap<String, Object> record) {
215
216         Document doc = createDocument(record);
217         Document filter = getKeyFilter(tableName, record);
218
219         return mongoDB.insertAndReplaceRecord(tableName, filter, doc);
220     }
221
222     private Boolean _deleteRecord(String tableName,
223             HashMap<String, Object> condition) {
224
225         Document doc = createDocument(condition);
226
227         return mongoDB.deleteRecord(tableName, doc);
228     }
229
230     private Boolean _updateRecord(String tableName,
231             HashMap<String, Object> record) {
232
233         Document replace = createDocument(record);
234         Document filter = getKeyFilter(tableName, record);
235
236         return mongoDB.updateRecord(tableName, filter, replace);
237     }
238
239     private ArrayList<HashMap<String, Object>> _selectRecord(String tableName,
240             HashMap<String, Object> record) {
241
242         Document doc = createDocument(record);
243
244         return mongoDB.selectRecord(tableName, doc);
245     }
246
247     private Document getKeyFilter(String tableName,
248             HashMap<String, Object> record) {
249
250         Document filterDoc = new Document();
251
252         ArrayList<String> keys = keyField.get(tableName);
253
254         for (String key : keys) {
255
256             String value = (String) record.get(key);
257             filterDoc.append(key, value);
258         }
259
260         return filterDoc;
261     }
262
263     private Document createDocument(HashMap<String, Object> record) {
264
265         Document doc = new Document();
266         Set<Entry<String, Object>> resEntrySet = record.entrySet();
267         Iterator<Entry<String, Object>> entryIter = resEntrySet.iterator();
268
269         while (entryIter.hasNext()) {
270             Map.Entry<String, Object> entry = (Map.Entry<String, Object>) entryIter
271                     .next();
272             doc.append(entry.getKey().toString(), entry.getValue());
273         }
274
275         return doc;
276     }
277
278 }