Added UI Support to change resource type.
authorG S Senthil Kumar <senthil.gs@samsung.com>
Tue, 1 Mar 2016 15:15:53 +0000 (20:45 +0530)
committerMadan Lanka <lanka.madan@samsung.com>
Thu, 3 Mar 2016 08:22:11 +0000 (08:22 +0000)
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 <senthil.gs@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/5283
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/Data.java
service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/manager/ResourceManager.java
service/simulator/java/eclipse-plugin/ServiceProviderPlugin/src/oic/simulator/serviceprovider/view/MetaPropertiesView.java

index 2cd2eee..b8a4674 100644 (file)
@@ -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<String, SingleResource> singleResourceMap;
 
-    // Holds resource type as key and a resource URI set as values.
-    // Helps in performing operations based on resource type.
-    private Map<String, Set<String>>    resTypeToResUriMap;
-
     public Data() {
         singleResourceMap = new HashMap<String, SingleResource>();
-        resTypeToResUriMap = new HashMap<String, Set<String>>();
     }
 
     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<String> newTypeSet;
-        newTypeSet = resTypeToResUriMap.get(resType);
-        if (null == newTypeSet) {
-            newTypeSet = new HashSet<String>();
-            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<String> newTypeSet;
-        newTypeSet = resTypeToResUriMap.get(resType);
-        if (null != newTypeSet) {
-            newTypeSet.remove(uri);
-        }
-        if (null == newTypeSet || newTypeSet.isEmpty()) {
-            resTypeToResUriMap.remove(resType);
-        }
     }
 
     public List<SingleResource> getSingleResources() {
index ac01be2..2d26b9b 100644 (file)
@@ -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<MetaProperty> 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;
index 56712dc..82ff675 100644 (file)
@@ -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);