From 9f5c6903aa2187ca8b7326416f764bed044b1c61 Mon Sep 17 00:00:00 2001 From: "eunok.shin" Date: Fri, 28 Oct 2016 16:56:38 +0900 Subject: [PATCH] [IOT-1536] add logic checking the device presence when finding resource. Change-Id: I6cbfb62855a80d03bf0aebf505505222a146ca38 Signed-off-by: eunok.shin Reviewed-on: https://gerrit.iotivity.org/gerrit/13817 Reviewed-by: Yeonghun Nam Tested-by: jenkins-iotivity Reviewed-by: Jee Hyeok Kim --- .../org/iotivity/cloud/rdserver/db/DBManager.java | 38 +++++++++++++++++++--- .../rdserver/resources/directory/RDManager.java | 24 ++++++++++++-- .../resources/directory/res/DiscoveryResource.java | 5 +++ 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/db/DBManager.java b/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/db/DBManager.java index 734c52f..d4be884 100644 --- a/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/db/DBManager.java +++ b/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/db/DBManager.java @@ -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 @@ -140,10 +140,40 @@ public class DBManager { } /** + * 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 selectOneRecord(String tableName, + HashMap condition) { + + ArrayList> records = _selectRecord(tableName, + condition); + + if (records.size() > 1) { + throw new InternalServerErrorException( + "Database record select failed"); + } + + if (records.isEmpty()) { + return new HashMap(); + } 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 */ diff --git a/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/directory/RDManager.java b/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/directory/RDManager.java index 881eead..92f30d2 100644 --- a/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/directory/RDManager.java +++ b/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/directory/RDManager.java @@ -240,7 +240,9 @@ public class RDManager { private void readResource(List diList, HashMap condition, ArrayList response) { - for (String di : diList) { + ArrayList onDiList = getPresenceOnDevices(diList); + + for (String di : onDiList) { condition.put(Constants.DEVICE_ID, di); ArrayList> records = DBManager.getInstance() .selectRecord(Constants.RD_TABLE, condition); @@ -252,6 +254,24 @@ public class RDManager { } } + private ArrayList getPresenceOnDevices(List diList) { + ArrayList onDiList = new ArrayList<>(); + HashMap condition = new HashMap<>(); + + for (String di : diList) { + condition.put(Constants.DEVICE_ID, di); + HashMap 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 makeDiscoverResponseSegment( ArrayList> records) { @@ -290,4 +310,4 @@ public class RDManager { return mResourcePresence; } -} +} \ No newline at end of file diff --git a/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/directory/res/DiscoveryResource.java b/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/directory/res/DiscoveryResource.java index 1f7199f..bb2174d 100644 --- a/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/directory/res/DiscoveryResource.java +++ b/cloud/resourcedirectory/src/main/java/org/iotivity/cloud/rdserver/resources/directory/res/DiscoveryResource.java @@ -76,6 +76,11 @@ public class DiscoveryResource extends Resource { HashMap> queryMap = request.getUriQueryMap(); + if (queryMap == null) { + return MessageBuilder.createResponse(request, + ResponseStatus.NOT_FOUND); + } + List diList = queryMap.get(Constants.DEVICE_ID); List rtList = queryMap.get(Constants.RESOURCE_TYPE); List ifList = queryMap.get(Constants.INTERFACE); -- 2.7.4