Imported Upstream version 1.2.0
[platform/upstream/iotivity.git] / cloud / resourcedirectory / src / main / java / org / iotivity / cloud / rdserver / resources / directory / rd / InsManager.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.resources.directory.rd;
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.db.DBManager;
29
30 /**
31  *
32  * This class provides a set of APIs to handle ins(unique value of resource)
33  *
34  */
35 public class InsManager {
36
37     private HashMap<String, Integer> mNextIns      = new HashMap<>();
38     private int                      mInitialValue = 1;
39
40     /**
41      * API for getting ins from DB
42      * 
43      * @param di
44      *            device id
45      * @param href
46      *            resource uri
47      * @return ins
48      */
49     public int getIns(String di, String href) {
50
51         HashMap<String, Object> condition = new HashMap<>();
52         condition.put(Constants.DEVICE_ID, di);
53         condition.put(Constants.HREF, href);
54         ArrayList<HashMap<String, Object>> records = DBManager.getInstance()
55                 .selectRecord(Constants.RD_TABLE, condition);
56
57         if (records.isEmpty()) {
58             return -1;
59         } else {
60             return (int) (records.get(0).get(Constants.INS));
61         }
62     }
63
64     /**
65      * API for creating ins
66      * 
67      * @param di
68      *            device id
69      * @return created ins
70      */
71     public int createIns(String di) {
72         Object objectIns;
73
74         objectIns = mNextIns.get(di);
75         if (objectIns == null) {
76             mNextIns.put(di, mInitialValue + 1);
77             return mInitialValue;
78         } else {
79             int ins = (int) objectIns;
80             mNextIns.put(di, ins + 1);
81             return ins;
82         }
83     }
84 }