From d5843f59a477a9fbd6ef82f863ad4c808e92ad77 Mon Sep 17 00:00:00 2001 From: G S Senthil Kumar Date: Tue, 1 Mar 2016 20:45:53 +0530 Subject: [PATCH] Added UI Support to change resource type. Allowing the resource type to be changed after resource creation. Removed unused data maintained by the ServiceProvider plug-in. Change-Id: I2e7b617888bd2fe866a84fee920d2e327a9f9597 Signed-off-by: G S Senthil Kumar Reviewed-on: https://gerrit.iotivity.org/gerrit/5283 Tested-by: jenkins-iotivity Reviewed-by: Madan Lanka --- .../simulator/serviceprovider/manager/Data.java | 51 ------------------- .../serviceprovider/manager/ResourceManager.java | 58 +++++++++++++++++++++- .../serviceprovider/view/MetaPropertiesView.java | 52 ++++++++++++------- 3 files changed, 91 insertions(+), 70 deletions(-) diff --git a/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/Data.java b/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/Data.java index 2cd2eee..b8a4674 100644 --- a/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/Data.java +++ b/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/Data.java @@ -18,7 +18,6 @@ package oic.simulator.serviceprovider.manager; import java.util.ArrayList; import java.util.HashMap; -import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -30,13 +29,8 @@ import oic.simulator.serviceprovider.model.SingleResource; public class Data { private Map singleResourceMap; - // Holds resource type as key and a resource URI set as values. - // Helps in performing operations based on resource type. - private Map> resTypeToResUriMap; - public Data() { singleResourceMap = new HashMap(); - resTypeToResUriMap = new HashMap>(); } public synchronized void addResource(Resource resource) { @@ -47,28 +41,6 @@ public class Data { singleResourceMap.put(resource.getResourceURI(), (SingleResource) resource); } - addToTypeAndUriMap(resource); - } - - public synchronized void addToTypeAndUriMap(Resource resource) { - if (null == resource) { - return; - } - String resType = resource.getResourceType(); - if (null == resType || resType.isEmpty()) { - return; - } - String uri = resource.getResourceURI(); - if (null == uri || uri.isEmpty()) { - return; - } - Set newTypeSet; - newTypeSet = resTypeToResUriMap.get(resType); - if (null == newTypeSet) { - newTypeSet = new HashSet(); - resTypeToResUriMap.put(resType, newTypeSet); - } - newTypeSet.add(uri); } public synchronized void deleteResource(Resource resource) { @@ -78,29 +50,6 @@ public class Data { if (resource instanceof SingleResource) { singleResourceMap.remove(resource.getResourceURI()); } - removeFromTypeAndUriMap(resource); - } - - public synchronized void removeFromTypeAndUriMap(Resource resource) { - if (null == resource) { - return; - } - String resType = resource.getResourceType(); - if (null == resType || !resType.isEmpty()) { - return; - } - String uri = resource.getResourceURI(); - if (null == uri || uri.isEmpty()) { - return; - } - Set newTypeSet; - newTypeSet = resTypeToResUriMap.get(resType); - if (null != newTypeSet) { - newTypeSet.remove(uri); - } - if (null == newTypeSet || newTypeSet.isEmpty()) { - resTypeToResUriMap.remove(resType); - } } public List getSingleResources() { diff --git a/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/ResourceManager.java b/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/ResourceManager.java index ac01be2..2d26b9b 100644 --- a/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/ResourceManager.java +++ b/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/ResourceManager.java @@ -1029,6 +1029,30 @@ public class ResourceManager { return true; } + public boolean changeResourceType(Resource resource, String newResourceType) + throws SimulatorException { + if (null == resource || null == newResourceType) { + return false; + } + + if (!stopResource(resource)) { + return false; + } + + String curResourceType = resource.getResourceType(); + setResourceType(resource, newResourceType); + + try { + if (!startResource(resource)) { + return false; + } + } catch (SimulatorException e) { + setResourceType(resource, curResourceType); + } + + return true; + } + public void setResourceURI(Resource resource, String newURI) throws SimulatorException { String curURI = resource.getResourceURI(); @@ -1048,9 +1072,28 @@ public class ResourceManager { } } + public void setResourceType(Resource resource, String newResourceType) + throws SimulatorException { + SimulatorResource server = resource.getSimulatorResource(); + try { + server.setResourceType(newResourceType); + resource.setResourceType(newResourceType); + } catch (SimulatorException e) { + Activator + .getDefault() + .getLogManager() + .log(Level.ERROR.ordinal(), + new Date(), + "There is an error while changing the resource Type.\n" + + Utility.getSimulatorErrorString(e, null)); + throw e; + } + } + public boolean updateResourceProperties(Resource resource, List properties, boolean uriChanged, - boolean nameChanged) throws SimulatorException { + boolean nameChanged, boolean resTypeChanged) + throws SimulatorException { if (null == resource || null == properties) { return false; } @@ -1062,6 +1105,7 @@ public class ResourceManager { String propValue; String resName = null; String resURI = null; + String resType = null; while (itr.hasNext()) { property = itr.next(); if (null == property) { @@ -1073,6 +1117,8 @@ public class ResourceManager { resName = propValue; } else if (propName.equals(Constants.RESOURCE_URI)) { resURI = propValue; + } else if (propName.equals(Constants.RESOURCE_TYPE)) { + resType = propValue; } } @@ -1092,6 +1138,12 @@ public class ResourceManager { } } + if (resTypeChanged) { + if (!changeResourceType(resource, resType)) { + return false; + } + } + return true; } @@ -1197,6 +1249,10 @@ public class ResourceManager { if (!Utility.isUriValid(value)) { invalid = true; } + } else if (propName.equals(Constants.RESOURCE_TYPE)) { + if (!Utility.isResourceTypeValid(value)) { + invalid = true; + } } else { if (null == value || value.trim().isEmpty()) { invalid = true; diff --git a/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/view/MetaPropertiesView.java b/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/view/MetaPropertiesView.java index 56712dc..82ff675 100644 --- a/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/view/MetaPropertiesView.java +++ b/service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/view/MetaPropertiesView.java @@ -174,8 +174,19 @@ public class MetaPropertiesView extends ViewPart { return; } + result = resourceManagerRef.isPropertyValueInvalid( + resourceInSelection, properties, + Constants.RESOURCE_TYPE); + if (result) { + MessageDialog.openError(parent.getShell(), + "Invalid Resource Type.", + Constants.INVALID_RESOURCE_TYPE_MESSAGE); + return; + } + boolean update = false; boolean uriChange = false; + boolean typeChange = false; boolean nameChange = false; boolean interfaceChange = false; @@ -185,6 +196,7 @@ public class MetaPropertiesView extends ViewPart { update = true; nameChange = true; } + if (resourceManagerRef.isPropValueChanged( resourceInSelection, properties, Constants.RESOURCE_URI)) { @@ -196,20 +208,16 @@ public class MetaPropertiesView extends ViewPart { return; } - if (resourceManagerRef - .isResourceStarted(resourceInSelection)) { - update = MessageDialog.openQuestion( - parent.getShell(), "Save Details", - "Resource will be restarted to take the changes." - + " Do you want to continue?"); - if (!update) { - return; - } - } - update = true; uriChange = true; } + + if (resourceManagerRef.isPropValueChanged( + resourceInSelection, properties, + Constants.RESOURCE_TYPE)) { + update = true; + typeChange = true; + } // Checking whether any changes made in resource // interfaces by // comparing the current interface set and updated @@ -232,8 +240,21 @@ public class MetaPropertiesView extends ViewPart { } } if (update) { + if (uriChange || typeChange || interfaceChange) { + if (resourceManagerRef + .isResourceStarted(resourceInSelection)) { + update = MessageDialog.openQuestion( + parent.getShell(), + "Save Details", + "Resource will be restarted to take the changes." + + " Do you want to continue?"); + if (!update) { + return; + } + } + } try { - if (uriChange || nameChange) + if (uriChange || nameChange || typeChange) result = Activator .getDefault() .getResourceManager() @@ -241,7 +262,7 @@ public class MetaPropertiesView extends ViewPart { resourceManagerRef .getCurrentResourceInSelection(), properties, uriChange, - nameChange); + nameChange, typeChange); if (interfaceChange) result = Activator .getDefault() @@ -442,12 +463,7 @@ public class MetaPropertiesView extends ViewPart { if (!getEnableEdit()) { return null; } - // Disabling edit for resource type String propName = ((MetaProperty) element).getPropName(); - if (null != propName && propName.equals(Constants.RESOURCE_TYPE)) { - return null; - } - CellEditor editor = new TextCellEditor(viewer.getTable()); if (null != propName && propName.equals(Constants.INTERFACE_TYPES)) { editor.setStyle(SWT.READ_ONLY); -- 2.7.4