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>
import java.util.ArrayList;
import java.util.HashMap;
-import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
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) {
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) {
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() {
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();
}
}
+ 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;
}
String propValue;
String resName = null;
String resURI = null;
+ String resType = null;
while (itr.hasNext()) {
property = itr.next();
if (null == property) {
resName = propValue;
} else if (propName.equals(Constants.RESOURCE_URI)) {
resURI = propValue;
+ } else if (propName.equals(Constants.RESOURCE_TYPE)) {
+ resType = propValue;
}
}
}
}
+ if (resTypeChanged) {
+ if (!changeResourceType(resource, resType)) {
+ return false;
+ }
+ }
+
return true;
}
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;
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;
update = true;
nameChange = true;
}
+
if (resourceManagerRef.isPropValueChanged(
resourceInSelection, properties,
Constants.RESOURCE_URI)) {
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
}
}
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()
resourceManagerRef
.getCurrentResourceInSelection(),
properties, uriChange,
- nameChange);
+ nameChange, typeChange);
if (interfaceChange)
result = Activator
.getDefault()
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);