Add DI based resource search through /oic/res
[platform/upstream/iotivity.git] / cloud / resourcedirectory / src / main / java / org / iotivity / cloud / rdserver / db / DBManager.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.rdserver.db;
23
24 import java.util.ArrayList;
25 import java.util.HashMap;
26
27 import org.iotivity.cloud.rdserver.Constants;
28 import org.iotivity.cloud.rdserver.resources.presence.resource.ResPresencePayload;
29
30 public class DBManager {
31
32     private static DBManager mDBManager = new DBManager();
33     private MongoDB          mMongoDB   = null;;
34
35     private DBManager() {
36         try {
37             mMongoDB = new MongoDB(Constants.RD_DB_NAME);
38             mMongoDB.createTable(Constants.RD_TABLE);
39             mMongoDB.createTable(Constants.PRESENCE_TABLE);
40         } catch (Exception e) {
41             // TODO Auto-generated catch block
42             e.printStackTrace();
43         }
44     }
45
46     public static DBManager getInstance() {
47         return mDBManager;
48     }
49
50     public ArrayList<ResPresencePayload> registerResource(
51             ArrayList<HashMap<Object, Object>> pubResourceList) {
52         return mMongoDB.createRDResource(pubResourceList, Constants.RD_TABLE);
53     }
54
55     public ArrayList<HashMap<Object, Object>> findResourceAboutDi(String di) {
56         return mMongoDB.readResourceAboutDid(di, Constants.RD_TABLE);
57     }
58     
59     public ArrayList<HashMap<Object, Object>> findResourceAboutDiAndFilter(String di,
60             String key, String value) {
61         return mMongoDB.readResourceAboutDidAndFilter(di, key, value,
62                 Constants.RD_TABLE);
63     }
64
65     public ArrayList<ResPresencePayload> deleteResourceAboutDi(String di) {
66         return mMongoDB.deleteResourceAboutDi(di, Constants.RD_TABLE);
67     }
68
69     public ArrayList<ResPresencePayload> deleteResourceAboutDiAandIns(String di,
70             String ins) {
71         return mMongoDB.deleteResourceAboutDiAndIns(di, ins,
72                 Constants.RD_TABLE);
73     }
74
75     public Object findInsAboutDi(String di, String href) {
76         return mMongoDB.readInsAboutDid(di, href, Constants.RD_TABLE);
77     }
78
79     public void updateDeviceState(HashMap<Object, Object> deviceState) {
80         mMongoDB.createDevicePresenceResource(deviceState,
81                 Constants.PRESENCE_TABLE);
82     }
83
84     public String findDeviceState(String deviceId) {
85         return mMongoDB.readDeviceState(deviceId, Constants.PRESENCE_TABLE);
86     }
87 }