[IOT-1536] add logic checking the device presence when finding resource. 17/13817/4
authoreunok.shin <eunok.shin@samsung.com>
Fri, 28 Oct 2016 07:56:38 +0000 (16:56 +0900)
committerJee Hyeok Kim <jihyeok13.kim@samsung.com>
Mon, 14 Nov 2016 09:49:38 +0000 (09:49 +0000)
Change-Id: I6cbfb62855a80d03bf0aebf505505222a146ca38
Signed-off-by: eunok.shin <eunok.shin@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/13817
Reviewed-by: Yeonghun Nam <yeonghun.nam@samsung.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jee Hyeok Kim <jihyeok13.kim@samsung.com>
cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/db/DBManager.java
cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/directory/RDManager.java
cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/directory/res/DiscoveryResource.java

index 734c52f..d4be884 100644 (file)
@@ -112,7 +112,7 @@ public class DBManager {
      * duplicated one.
      * 
      * @param tableName
-     *            table name to be inserted
+     *            table name to be inserted or replaced
      * @param replace
      *            record to be inserted
      */
@@ -128,7 +128,7 @@ public class DBManager {
      * API for selecting records from DB table.
      * 
      * @param tableName
-     *            table name to be inserted
+     *            table name to be selected
      * @param condition
      *            condition record to be selected
      * @return selected records
@@ -139,11 +139,41 @@ public class DBManager {
         return _selectRecord(tableName, condition);
     }
 
+    /**
+     * API for selecting records to primary key from DB table
+     * 
+     * @param tableName
+     *            table name to be selected
+     * 
+     * @param condition
+     *            condition record to be selected
+     * @return selected record
+     */
+
+    public HashMap<String, Object> selectOneRecord(String tableName,
+            HashMap<String, Object> condition) {
+
+        ArrayList<HashMap<String, Object>> records = _selectRecord(tableName,
+                condition);
+
+        if (records.size() > 1) {
+            throw new InternalServerErrorException(
+                    "Database record select failed");
+        }
+
+        if (records.isEmpty()) {
+            return new HashMap<String, Object>();
+        } else {
+            return records.get(0);
+        }
+
+    }
+
     /**
      * API for deleting records from DB table.
      * 
      * @param tableName
-     *            table name to be inserted
+     *            table name to be deleted
      * @param condition
      *            condition record to be deleted
      */
@@ -159,7 +189,7 @@ public class DBManager {
      * API for updating a record into DB table.
      * 
      * @param tableName
-     *            table name to be inserted
+     *            table name to be updated
      * @param replace
      *            record to be updated
      */
index 881eead..92f30d2 100644 (file)
@@ -240,7 +240,9 @@ public class RDManager {
     private void readResource(List<String> diList,
             HashMap<String, Object> condition, ArrayList<Object> response) {
 
-        for (String di : diList) {
+        ArrayList<String> onDiList = getPresenceOnDevices(diList);
+
+        for (String di : onDiList) {
             condition.put(Constants.DEVICE_ID, di);
             ArrayList<HashMap<String, Object>> records = DBManager.getInstance()
                     .selectRecord(Constants.RD_TABLE, condition);
@@ -252,6 +254,24 @@ public class RDManager {
         }
     }
 
+    private ArrayList<String> getPresenceOnDevices(List<String> diList) {
+        ArrayList<String> onDiList = new ArrayList<>();
+        HashMap<String, Object> condition = new HashMap<>();
+
+        for (String di : diList) {
+            condition.put(Constants.DEVICE_ID, di);
+            HashMap<String, Object> record = DBManager.getInstance()
+                    .selectOneRecord(Constants.PRESENCE_TABLE, condition);
+
+            if (!record.isEmpty() && record.get(Constants.PRESENCE_STATE)
+                    .equals(Constants.PRESENCE_ON)) {
+                onDiList.add(di);
+            }
+
+        }
+        return onDiList;
+    }
+
     private HashMap<String, Object> makeDiscoverResponseSegment(
             ArrayList<HashMap<String, Object>> records) {
 
@@ -290,4 +310,4 @@ public class RDManager {
         return mResourcePresence;
     }
 
-}
+}
\ No newline at end of file
index 1f7199f..bb2174d 100644 (file)
@@ -76,6 +76,11 @@ public class DiscoveryResource extends Resource {
 
         HashMap<String, List<String>> queryMap = request.getUriQueryMap();
 
+        if (queryMap == null) {
+            return MessageBuilder.createResponse(request,
+                    ResponseStatus.NOT_FOUND);
+        }
+
         List<String> diList = queryMap.get(Constants.DEVICE_ID);
         List<String> rtList = queryMap.get(Constants.RESOURCE_TYPE);
         List<String> ifList = queryMap.get(Constants.INTERFACE);