Added UI Support to change resource type.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / manager / ResourceManager.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package oic.simulator.serviceprovider.manager;
18
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.swt.widgets.Display;
21
22 import java.text.NumberFormat;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.Date;
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
32 import java.util.Vector;
33
34 import org.oic.simulator.ArrayProperty;
35 import org.oic.simulator.AttributeProperty;
36 import org.oic.simulator.AttributeProperty.Type;
37 import org.oic.simulator.AttributeValue;
38 import org.oic.simulator.AttributeValue.TypeInfo;
39 import org.oic.simulator.AttributeValue.ValueType;
40 import org.oic.simulator.BooleanProperty;
41 import org.oic.simulator.DeviceInfo;
42 import org.oic.simulator.DeviceListener;
43 import org.oic.simulator.DoubleProperty;
44 import org.oic.simulator.ILogger.Level;
45 import org.oic.simulator.IntegerProperty;
46 import org.oic.simulator.PlatformInfo;
47 import org.oic.simulator.SimulatorException;
48 import org.oic.simulator.SimulatorManager;
49 import org.oic.simulator.SimulatorResourceAttribute;
50 import org.oic.simulator.SimulatorResourceModel;
51 import org.oic.simulator.StringProperty;
52 import org.oic.simulator.server.Observer;
53 import org.oic.simulator.server.SimulatorResource;
54 import org.oic.simulator.server.SimulatorResource.AutoUpdateListener;
55 import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
56 import org.oic.simulator.server.SimulatorResource.ObserverListener;
57 import org.oic.simulator.server.SimulatorResource.ResourceModelChangeListener;
58 import org.oic.simulator.server.SimulatorSingleResource;
59
60 import oic.simulator.serviceprovider.Activator;
61 import oic.simulator.serviceprovider.model.AttributeElement;
62 import oic.simulator.serviceprovider.model.MetaProperty;
63 import oic.simulator.serviceprovider.model.Resource;
64 import oic.simulator.serviceprovider.model.ResourceType;
65 import oic.simulator.serviceprovider.model.SingleResource;
66 import oic.simulator.serviceprovider.utils.Constants;
67 import oic.simulator.serviceprovider.utils.Utility;
68
69 /**
70  * This class acts as an interface between the simulator java SDK and the
71  * various UI modules. It maintains all the details of resources and provides
72  * other UI modules with the information required. It also handles model change,
73  * automation, and observer related events from native layer and propagates
74  * those events to the registered UI listeners.
75  */
76 public class ResourceManager {
77
78     private Data                           data;
79
80     private Resource                       currentResourceInSelection;
81
82     private ResourceModelChangeListener    resourceModelChangeListener;
83
84     private AutoUpdateListener             automationListener;
85
86     private ObserverListener               observer;
87
88     private DeviceListener                 deviceListener;
89
90     private NotificationSynchronizerThread synchronizerThread;
91
92     private Thread                         threadHandle;
93
94     private DeviceInfo                     deviceInfo;
95     private PlatformInfo                   platformInfo;
96
97     private String                         deviceName;
98
99     public ResourceManager() {
100         data = new Data();
101
102         deviceListener = new DeviceListener() {
103
104             @Override
105             public void onDeviceFound(final String host,
106                     final DeviceInfo deviceInfo) {
107                 if (null != ResourceManager.this.deviceInfo
108                         || null == deviceInfo || null == host) {
109                     return;
110                 }
111                 synchronizerThread.addToQueue(new Runnable() {
112                     @Override
113                     public void run() {
114                         String rcvdDeviceName = deviceInfo.getName();
115                         if (null == rcvdDeviceName) {
116                             return;
117                         }
118                         if (deviceName.equalsIgnoreCase(rcvdDeviceName)) {
119                             ResourceManager.this.deviceInfo = deviceInfo;
120
121                             // Notify the UI Listeners
122                             UiListenerHandler.getInstance()
123                                     .deviceInfoReceivedNotification();
124                         }
125                     }
126                 });
127             }
128         };
129
130         resourceModelChangeListener = new ResourceModelChangeListener() {
131
132             @Override
133             public void onResourceModelChanged(final String resourceURI,
134                     final SimulatorResourceModel resourceModelN) {
135                 synchronizerThread.addToQueue(new Runnable() {
136
137                     @Override
138                     public void run() {
139                         if (null == resourceURI || null == resourceModelN) {
140                             return;
141                         }
142
143                         Display.getDefault().asyncExec(new Runnable() {
144                             @Override
145                             public void run() {
146                                 Resource resource = data
147                                         .getResourceByURI(resourceURI);
148                                 if (null != resource) {
149                                     try {
150                                         resource.updateResourceRepresentation(resourceModelN);
151                                     } catch (NumberFormatException e) {
152                                         Activator
153                                                 .getDefault()
154                                                 .getLogManager()
155                                                 .log(Level.ERROR.ordinal(),
156                                                         new Date(),
157                                                         "Error while trying to update the attributes.\n"
158                                                                 + Utility
159                                                                         .getSimulatorErrorString(
160                                                                                 e,
161                                                                                 null));
162                                     }
163                                 }
164                             }
165                         });
166                     }
167                 });
168             }
169         };
170
171         automationListener = new AutoUpdateListener() {
172
173             @Override
174             public void onUpdateComplete(final String resourceURI,
175                     final int automationId) {
176                 synchronizerThread.addToQueue(new Runnable() {
177
178                     @Override
179                     public void run() {
180                         SingleResource resource = data
181                                 .getSingleResourceByURI(resourceURI);
182                         if (null == resource) {
183                             return;
184                         }
185                         // Checking whether this notification is for an
186                         // attribute or a resource
187                         if (resource.isResourceAutomationInProgress()) {
188                             changeResourceLevelAutomationStatus(resource, false);
189                             // Notify the UI listeners
190                             UiListenerHandler.getInstance()
191                                     .automationCompleteUINotification(resource,
192                                             null);
193                         } else if (resource.isAttributeAutomationInProgress()) {
194                             // Find the attribute with the given automation id
195                             final AttributeElement attribute = getAttributeWithGivenAutomationId(
196                                     resource, automationId);
197                             if (null != attribute) {
198                                 attribute.setAutoUpdateState(false);
199                                 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
200                             } else {
201                                 // Setting the attribute automation status to
202                                 // false.
203                                 resource.setAttributeAutomationInProgress(false);
204                             }
205                         }
206                     }
207                 });
208             }
209         };
210
211         observer = new ObserverListener() {
212
213             public void onObserverChanged(final String resourceURI,
214                     final int status, final Observer observer) {
215                 new Thread() {
216                     @Override
217                     public void run() {
218                         if (null == resourceURI || null == observer) {
219                             return;
220                         }
221                         Resource resource = data.getResourceByURI(resourceURI);
222                         if (null == resource) {
223                             return;
224                         }
225                         // Update the observers information
226                         if (status == 0) {
227                             resource.addObserverInfo(observer);
228                         } else {
229                             resource.removeObserverInfo(observer);
230                         }
231                         // Notify the UI listeners
232                         UiListenerHandler.getInstance()
233                                 .observerListChangedUINotification(resource);
234                     }
235                 }.start();
236             }
237
238             @Override
239             public void onObserverAdded(String resourceURI, Observer observer) {
240                 onObserverChanged(resourceURI, 0, observer);
241             }
242
243             @Override
244             public void onObserverRemoved(String resourceURI, Observer observer) {
245                 onObserverChanged(resourceURI, 1, observer);
246             }
247         };
248
249         synchronizerThread = new NotificationSynchronizerThread();
250         threadHandle = new Thread(synchronizerThread);
251         threadHandle.setName("Simulator service provider event queue");
252         threadHandle.start();
253
254         // Set the default device name.
255         deviceName = "IoTivity Simulator";
256         try {
257             SimulatorManager.setDeviceInfo(deviceName);
258         } catch (SimulatorException e) {
259             Activator
260                     .getDefault()
261                     .getLogManager()
262                     .log(Level.ERROR.ordinal(),
263                             new Date(),
264                             "Error while registering the device info.\n"
265                                     + Utility.getSimulatorErrorString(e, null));
266         }
267
268         // Set the default platform information.
269         platformInfo = new PlatformInfo();
270         platformInfo.setPlatformID("Samsung Platform Identifier");
271         platformInfo.setManufacturerName("Samsung");
272         platformInfo.setManufacturerUrl("www.samsung.com");
273         platformInfo.setModelNumber("Samsung Model Num01");
274         platformInfo.setDateOfManufacture("2015-09-10T11:10:30Z");
275         platformInfo.setPlatformVersion("PlatformVersion01");
276         platformInfo.setOperationSystemVersion("OSVersion01");
277         platformInfo.setHardwareVersion("HardwareVersion01");
278         platformInfo.setFirmwareVersion("FirwareVersion01");
279         platformInfo.setSupportUrl("http://www.samsung.com/support");
280         platformInfo.setSystemTime("2015-09-10T11:10:30Z");
281         try {
282             SimulatorManager.setPlatformInfo(platformInfo);
283         } catch (SimulatorException e) {
284             Activator
285                     .getDefault()
286                     .getLogManager()
287                     .log(Level.ERROR.ordinal(),
288                             new Date(),
289                             "Error while registering the platform info.\n"
290                                     + Utility.getSimulatorErrorString(e, null));
291         }
292
293         // Get the device information to show other details of the device in UI.
294         try {
295             SimulatorManager.findDevices("", deviceListener);
296         } catch (SimulatorException e) {
297             Activator
298                     .getDefault()
299                     .getLogManager()
300                     .log(Level.ERROR.ordinal(),
301                             new Date(),
302                             "Failed to get the local device information.\n"
303                                     + Utility.getSimulatorErrorString(e, null));
304         }
305     }
306
307     private static class NotificationSynchronizerThread implements Runnable {
308
309         LinkedList<Runnable> notificationQueue = new LinkedList<Runnable>();
310
311         @Override
312         public void run() {
313             while (!Thread.interrupted()) {
314                 synchronized (this) {
315                     try {
316                         while (notificationQueue.isEmpty()) {
317                             this.wait();
318                             break;
319                         }
320                     } catch (InterruptedException e) {
321                         return;
322                     }
323                 }
324
325                 Runnable thread;
326                 synchronized (this) {
327                     thread = notificationQueue.pop();
328                 }
329                 try {
330                     thread.run();
331                 } catch (Exception e) {
332                     if (e instanceof InterruptedException) {
333                         return;
334                     }
335                     e.printStackTrace();
336                 }
337             }
338         }
339
340         public void addToQueue(Runnable event) {
341             synchronized (this) {
342                 notificationQueue.add(event);
343                 this.notify();
344             }
345         }
346     }
347
348     public void setDeviceInfo(List<MetaProperty> metaProperties) {
349         if (null == metaProperties || metaProperties.size() < 1) {
350             return;
351         }
352         Iterator<MetaProperty> itr = metaProperties.iterator();
353         MetaProperty prop;
354         String propName;
355         String propValue;
356         boolean found = false;
357         while (itr.hasNext()) {
358             prop = itr.next();
359             propName = prop.getPropName();
360             propValue = prop.getPropValue();
361             if (propName.equals(Constants.DEVICE_NAME)) {
362                 this.deviceName = propValue;
363                 found = true;
364                 break;
365             }
366         }
367
368         if (!found) {
369             return;
370         }
371
372         try {
373             SimulatorManager.setDeviceInfo(deviceName);
374         } catch (SimulatorException e) {
375             Activator
376                     .getDefault()
377                     .getLogManager()
378                     .log(Level.ERROR.ordinal(),
379                             new Date(),
380                             "Error while registering the device info.\n"
381                                     + Utility.getSimulatorErrorString(e, null));
382         }
383     }
384
385     public boolean isDeviceInfoValid(List<MetaProperty> metaProperties) {
386         if (null == metaProperties || metaProperties.size() < 1) {
387             return false;
388         }
389
390         Iterator<MetaProperty> itr = metaProperties.iterator();
391         MetaProperty prop;
392         String propName;
393         String propValue;
394         while (itr.hasNext()) {
395             prop = itr.next();
396             propName = prop.getPropName();
397             propValue = prop.getPropValue();
398             if (propName.equals(Constants.DEVICE_NAME)) {
399                 if (null == propValue || propValue.length() < 1) {
400                     return false;
401                 }
402                 break;
403             }
404         }
405         return true;
406     }
407
408     public List<MetaProperty> getDeviceInfo() {
409         List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
410         metaProperties.add(new MetaProperty(Constants.DEVICE_NAME, deviceName));
411         if (null != deviceInfo) {
412             metaProperties.add(new MetaProperty(Constants.DEVICE_ID, deviceInfo
413                     .getID()));
414             metaProperties.add(new MetaProperty(Constants.DEVICE_SPEC_VERSION,
415                     deviceInfo.getSpecVersion()));
416             metaProperties.add(new MetaProperty(Constants.DEVICE_DMV,
417                     deviceInfo.getDataModelVersion()));
418         }
419         return metaProperties;
420     }
421
422     public List<MetaProperty> getPlatformInfo() {
423         List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
424         metaProperties.add(new MetaProperty(Constants.PLATFORM_ID, platformInfo
425                 .getPlatformID()));
426         metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_NAME,
427                 platformInfo.getManufacturerName()));
428         metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_URL,
429                 platformInfo.getManufacturerUrl()));
430         metaProperties.add(new MetaProperty(Constants.PLATFORM_MODEL_NO,
431                 platformInfo.getModelNumber()));
432         metaProperties.add(new MetaProperty(Constants.PLATFORM_DATE_OF_MANUFAC,
433                 platformInfo.getDateOfManufacture()));
434         metaProperties.add(new MetaProperty(Constants.PLATFORM_VERSION,
435                 platformInfo.getPlatformVersion()));
436         metaProperties.add(new MetaProperty(Constants.PLATFORM_OS_VERSION,
437                 platformInfo.getOperationSystemVersion()));
438         metaProperties.add(new MetaProperty(
439                 Constants.PLATFORM_HARDWARE_VERSION, platformInfo
440                         .getHardwareVersion()));
441         metaProperties.add(new MetaProperty(
442                 Constants.PLATFORM_FIRMWARE_VERSION, platformInfo
443                         .getFirmwareVersion()));
444         metaProperties.add(new MetaProperty(Constants.PLATFORM_SUPPORT_URL,
445                 platformInfo.getSupportUrl()));
446         metaProperties.add(new MetaProperty(Constants.PLATFORM_SYSTEM_TIME,
447                 platformInfo.getSystemTime()));
448         return metaProperties;
449     }
450
451     public void setPlatformInfo(List<MetaProperty> metaProperties) {
452         if (null == metaProperties || metaProperties.size() < 1) {
453             return;
454         }
455         Iterator<MetaProperty> itr = metaProperties.iterator();
456         MetaProperty prop;
457         String propName;
458         String propValue;
459         while (itr.hasNext()) {
460             prop = itr.next();
461             propName = prop.getPropName();
462             propValue = prop.getPropValue();
463             if (propName.equals(Constants.PLATFORM_ID)) {
464                 platformInfo.setPlatformID(propValue);
465             } else if (propName.equals(Constants.PLATFORM_MANUFAC_NAME)) {
466                 platformInfo.setManufacturerName(propValue);
467             } else if (propName.equals(Constants.PLATFORM_MANUFAC_URL)) {
468                 platformInfo.setManufacturerUrl(propValue);
469             } else if (propName.equals(Constants.PLATFORM_MODEL_NO)) {
470                 platformInfo.setModelNumber(propValue);
471             } else if (propName.equals(Constants.PLATFORM_DATE_OF_MANUFAC)) {
472                 platformInfo.setDateOfManufacture(propValue);
473             } else if (propName.equals(Constants.PLATFORM_VERSION)) {
474                 platformInfo.setPlatformVersion(propValue);
475             } else if (propName.equals(Constants.PLATFORM_OS_VERSION)) {
476                 platformInfo.setOperationSystemVersion(propValue);
477             } else if (propName.equals(Constants.PLATFORM_HARDWARE_VERSION)) {
478                 platformInfo.setHardwareVersion(propValue);
479             } else if (propName.equals(Constants.PLATFORM_FIRMWARE_VERSION)) {
480                 platformInfo.setFirmwareVersion(propValue);
481             } else if (propName.equals(Constants.PLATFORM_SUPPORT_URL)) {
482                 platformInfo.setSupportUrl(propValue);
483             } else if (propName.equals(Constants.PLATFORM_SYSTEM_TIME)) {
484                 platformInfo.setSystemTime(propValue);
485             }
486         }
487         try {
488             SimulatorManager.setPlatformInfo(platformInfo);
489         } catch (SimulatorException e) {
490             Activator
491                     .getDefault()
492                     .getLogManager()
493                     .log(Level.ERROR.ordinal(),
494                             new Date(),
495                             "Error while registering the platform info.\n"
496                                     + Utility.getSimulatorErrorString(e, null));
497         }
498     }
499
500     public boolean isPlatformInfoValid(List<MetaProperty> metaProperties) {
501         if (null == metaProperties || metaProperties.size() < 1) {
502             return false;
503         }
504         Iterator<MetaProperty> itr = metaProperties.iterator();
505         MetaProperty prop;
506         String propValue;
507         while (itr.hasNext()) {
508             prop = itr.next();
509             propValue = prop.getPropValue();
510             if (null == propValue || propValue.length() < 1) {
511                 return false;
512             }
513         }
514         return true;
515     }
516
517     public synchronized Resource getCurrentResourceInSelection() {
518         return currentResourceInSelection;
519     }
520
521     public synchronized void setCurrentResourceInSelection(Resource resource) {
522         this.currentResourceInSelection = resource;
523     }
524
525     public boolean isResourceExist(String resourceURI) {
526         return data.isResourceExist(resourceURI);
527     }
528
529     public boolean isAnyResourceExist() {
530         return data.isAnyResourceExist();
531     }
532
533     public boolean createSingleResource(SingleResource resource,
534             Map<String, SimulatorResourceAttribute> attributes)
535             throws SimulatorException {
536         if (null == resource) {
537             return false;
538         }
539
540         try {
541             // Create the resource.
542             SimulatorResource jSimulatorResource = SimulatorManager
543                     .createResource(SimulatorResource.Type.SINGLE,
544                             resource.getResourceName(),
545                             resource.getResourceURI(),
546                             resource.getResourceType());
547             if (null == jSimulatorResource
548                     || !(jSimulatorResource instanceof SimulatorSingleResource)) {
549                 return false;
550             }
551             SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) jSimulatorResource;
552             resource.setSimulatorResource(jSimulatorSingleResource);
553
554             // Cancel discoverable property if requested by user.
555             if (!resource.isDiscoverable()) {
556                 jSimulatorSingleResource.setDiscoverable(false);
557             }
558
559             // Cancel observable property if requested by user.
560             if (!resource.isObservable()) {
561                 jSimulatorSingleResource.setObservable(false);
562             }
563
564             // Set the model change listener.
565             jSimulatorSingleResource
566                     .setResourceModelChangeListener(resourceModelChangeListener);
567
568             // Set the observer listener if the resource is observable.
569             if (resource.isObservable()) {
570                 jSimulatorSingleResource.setObserverListener(observer);
571             }
572
573             // Add attributes.
574             if (null != attributes && !attributes.isEmpty()) {
575                 SimulatorResourceAttribute value;
576                 for (Map.Entry<String, SimulatorResourceAttribute> entry : attributes
577                         .entrySet()) {
578                     value = entry.getValue();
579                     if (null != value)
580                         jSimulatorSingleResource.addAttribute(value);
581                 }
582
583                 // Get the resource model java object reference.
584                 resource.setResourceModel(jSimulatorSingleResource
585                         .getResourceModel());
586
587                 resource.createResourceRepresentation(jSimulatorSingleResource
588                         .getAttributes());
589             }
590
591             // Set the resource interfaces.
592             jSimulatorSingleResource
593                     .setInterface(Utility.convertSetToVectorString(resource
594                             .getResourceInterfaces()));
595
596             // Register the resource with the platform.
597             jSimulatorSingleResource.start();
598             resource.setStarted(true);
599         } catch (SimulatorException e) {
600             Activator
601                     .getDefault()
602                     .getLogManager()
603                     .log(Level.ERROR.ordinal(), new Date(),
604                             Utility.getSimulatorErrorString(e, null));
605             throw e;
606         }
607
608         // Add to local cache.
609         data.addResource(resource);
610
611         // Update UI listeners
612         UiListenerHandler.getInstance().resourceCreatedUINotification(
613                 ResourceType.SINGLE);
614
615         return true;
616     }
617
618     public Resource createResourceByRAML(String configFilePath)
619             throws SimulatorException {
620         Resource resource = null;
621         try {
622             // Create the resource
623             SimulatorResource jSimulatorResource = SimulatorManager
624                     .createResource(configFilePath);
625             if (null == jSimulatorResource) {
626                 return null;
627             }
628             if (jSimulatorResource instanceof SimulatorSingleResource) {
629                 resource = new SingleResource();
630             } else {
631                 return null;
632             }
633             resource.setSimulatorResource(jSimulatorResource);
634
635             // Fetch and locally store the resource name and uri.
636             String uri = jSimulatorResource.getURI();
637             if (null == uri || uri.trim().isEmpty()) {
638                 return null;
639             }
640             resource.setResourceURI(uri.trim());
641
642             String name = jSimulatorResource.getName();
643             if (null == name || name.trim().isEmpty()) {
644                 return null;
645             }
646             resource.setResourceName(name.trim());
647         } catch (SimulatorException e) {
648             Activator
649                     .getDefault()
650                     .getLogManager()
651                     .log(Level.ERROR.ordinal(), new Date(),
652                             Utility.getSimulatorErrorString(e, null));
653             throw e;
654         }
655         return resource;
656     }
657
658     /**
659      * This method can set/change the resource uri and name of an already
660      * created resource which is not yet registered with the platform. This
661      * method registers the model change and observer listeners, registers the
662      * resource, fetches the resource attributes, updates the local cache and
663      * notifies the UI listeners.
664      */
665     public boolean completeSingleResourceCreationByRAML(Resource resource,
666             String uri, String name, boolean multiInstance)
667             throws SimulatorException {
668         if (null == resource || !(resource instanceof SingleResource)) {
669             return false;
670         }
671         try {
672             SingleResource singleRes = (SingleResource) resource;
673
674             SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) resource
675                     .getSimulatorResource();
676             if (null == jSimulatorSingleResource) {
677                 return false;
678             }
679
680             // Update resource URI and Name if they are changed.
681             String newUri = uri.trim();
682             String newName = name.trim();
683
684             if (multiInstance) {
685                 singleRes.setResourceURI(newUri);
686                 singleRes.setResourceName(newName);
687             } else {
688                 if (!singleRes.getResourceURI().equals(newUri)) {
689                     jSimulatorSingleResource.setURI(newUri);
690                     singleRes.setResourceURI(newUri);
691                 }
692                 if (!singleRes.getResourceName().equals(newName)) {
693                     jSimulatorSingleResource.setName(newName);
694                     singleRes.setResourceName(newName);
695                 }
696             }
697
698             // Set the model change listener.
699             jSimulatorSingleResource
700                     .setResourceModelChangeListener(resourceModelChangeListener);
701
702             // Set the observer listener if the resource is observable.
703             if (jSimulatorSingleResource.isObservable()) {
704                 jSimulatorSingleResource.setObserverListener(observer);
705                 singleRes.setObservable(true);
706             }
707
708             // Fetch the resource model.
709             SimulatorResourceModel jResModel = jSimulatorSingleResource
710                     .getResourceModel();
711             if (null == jResModel) {
712                 return false;
713             }
714             singleRes.setResourceModel(jResModel);
715
716             // Fetch the basic details of the resource.
717             singleRes.setResourceType(jSimulatorSingleResource
718                     .getResourceType());
719             singleRes
720                     .setResourceInterfaces(Utility
721                             .convertVectorToSet(jSimulatorSingleResource
722                                     .getInterface()));
723
724             // Fetch the resource attributes.
725             singleRes.createResourceRepresentation(jSimulatorSingleResource
726                     .getAttributes());
727
728             // Register the resource with the platform.
729             jSimulatorSingleResource.start();
730             singleRes.setStarted(true);
731
732             // Add to local cache.
733             data.addResource(singleRes);
734
735             // Update UI listeners for single instance creation
736             if (!multiInstance)
737                 UiListenerHandler.getInstance().resourceCreatedUINotification(
738                         ResourceType.SINGLE);
739         } catch (Exception e) {
740             Activator
741                     .getDefault()
742                     .getLogManager()
743                     .log(Level.ERROR.ordinal(), new Date(),
744                             Utility.getSimulatorErrorString(e, null));
745             throw e;
746         }
747         return true;
748     }
749
750     public Set<SingleResource> createSingleResourceMultiInstances(
751             String configFile, int count, IProgressMonitor progressMonitor)
752             throws SimulatorException {
753         Set<SingleResource> resultSet;
754         try {
755             resultSet = new HashSet<SingleResource>();
756             Vector<SimulatorResource> jSimulatorResources = SimulatorManager
757                     .createResource(configFile, count);
758             if (null == jSimulatorResources || jSimulatorResources.size() < 1) {
759                 return null;
760             }
761             SimulatorSingleResource jResource;
762             SingleResource resource;
763             boolean result;
764             for (SimulatorResource jSimulatorResource : jSimulatorResources) {
765                 // If the resource creation progress is canceled, then stop the
766                 // creation and stop/delete
767                 // the resources created already.
768                 if (progressMonitor.isCanceled()) {
769                     removeSingleResources(resultSet);
770                     return null;
771                 }
772                 jResource = (SimulatorSingleResource) jSimulatorResource;
773                 resource = new SingleResource();
774                 resource.setSimulatorResource(jResource);
775                 try {
776                     result = completeSingleResourceCreationByRAML(resource,
777                             jResource.getURI(), jResource.getName(), true);
778                     if (result) {
779                         resultSet.add(resource);
780                     }
781                 } catch (SimulatorException eInner) {
782                     Activator
783                             .getDefault()
784                             .getLogManager()
785                             .log(Level.ERROR.ordinal(),
786                                     new Date(),
787                                     Utility.getSimulatorErrorString(eInner,
788                                             null));
789                 }
790                 progressMonitor.worked(1);
791             }
792         } catch (SimulatorException eOuter) {
793             Activator
794                     .getDefault()
795                     .getLogManager()
796                     .log(Level.ERROR.ordinal(), new Date(),
797                             Utility.getSimulatorErrorString(eOuter, null));
798             throw eOuter;
799         }
800         return resultSet;
801     }
802
803     public List<Resource> getResourceList() {
804         List<Resource> resourceList = data.getResources();
805         if (null == resourceList) {
806             return null;
807         }
808         // Sort the list
809         Collections.sort(resourceList, Utility.resourceComparator);
810
811         return resourceList;
812     }
813
814     public List<SingleResource> getSingleResourceList() {
815         List<SingleResource> resourceList = data.getSingleResources();
816         if (null == resourceList) {
817             return null;
818         }
819         // Sort the list
820         Collections.sort(resourceList, Utility.singleResourceComparator);
821
822         return resourceList;
823     }
824
825     public void removeSingleResources(Set<SingleResource> resources)
826             throws SimulatorException {
827         if (null == resources) {
828             return;
829         }
830         Iterator<SingleResource> itr = resources.iterator();
831         while (itr.hasNext()) {
832             removeResource(itr.next());
833         }
834     }
835
836     public void removeResource(Resource res) throws SimulatorException {
837         // Unregister the resource from the platform.
838         SimulatorResource simRes = res.getSimulatorResource();
839         try {
840             simRes.stop();
841         } catch (SimulatorException e) {
842             Activator
843                     .getDefault()
844                     .getLogManager()
845                     .log(Level.ERROR.ordinal(), new Date(),
846                             Utility.getSimulatorErrorString(e, null));
847             throw e;
848         }
849
850         // Delete this resource
851         data.deleteResource(res);
852     }
853
854     public boolean isUriUnique(List<MetaProperty> properties) {
855         if (null == properties) {
856             return false;
857         }
858         MetaProperty prop;
859         Iterator<MetaProperty> itr = properties.iterator();
860         while (itr.hasNext()) {
861             prop = itr.next();
862             if (prop.getPropName().equals(Constants.RESOURCE_URI)) {
863                 String uri = prop.getPropValue();
864                 return !data.isResourceExist(uri);
865             }
866         }
867         return false;
868     }
869
870     public void resourceSelectionChanged(final Resource selectedResource) {
871         new Thread() {
872             @Override
873             public void run() {
874                 if (null != selectedResource) {
875                     setCurrentResourceInSelection(selectedResource);
876                 } else {
877                     setCurrentResourceInSelection(null);
878                 }
879                 // Notify all observers for resource selection change event
880                 UiListenerHandler.getInstance()
881                         .resourceSelectionChangedUINotification(
882                                 selectedResource);
883             }
884         }.start();
885     }
886
887     public List<MetaProperty> getMetaProperties(Resource resource) {
888         if (null != resource) {
889             String propName;
890             String propValue;
891
892             List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
893
894             for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
895                 propName = Constants.META_PROPERTIES[index];
896                 if (propName.equals(Constants.RESOURCE_NAME)) {
897                     propValue = resource.getResourceName();
898                 } else if (propName.equals(Constants.RESOURCE_URI)) {
899                     propValue = resource.getResourceURI();
900                 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
901                     propValue = resource.getResourceType();
902                 } else if (propName.equals(Constants.INTERFACE_TYPES)) {
903                     Set<String> ifTypes = resource.getResourceInterfaces();
904                     if (null != ifTypes && !ifTypes.isEmpty()) {
905                         propValue = "";
906                         Iterator<String> itr = ifTypes.iterator();
907                         while (itr.hasNext()) {
908                             propValue += itr.next();
909                             if (itr.hasNext()) {
910                                 propValue += ", ";
911                             }
912                         }
913                     } else {
914                         propValue = null;
915                     }
916                 } else {
917                     propValue = null;
918                 }
919                 if (null != propValue) {
920                     metaPropertyList.add(new MetaProperty(propName, propValue));
921                 }
922             }
923             return metaPropertyList;
924         }
925         return null;
926     }
927
928     public boolean startResource(Resource resource) throws SimulatorException {
929         if (null == resource) {
930             return false;
931         }
932         SimulatorResource server = resource.getSimulatorResource();
933         if (null == server) {
934             return false;
935         }
936         try {
937             server.start();
938             resource.setStarted(true);
939         } catch (SimulatorException e) {
940             Activator
941                     .getDefault()
942                     .getLogManager()
943                     .log(Level.ERROR.ordinal(),
944                             new Date(),
945                             "There is an error while starting the resource.\n"
946                                     + Utility.getSimulatorErrorString(e, null));
947             throw e;
948         }
949         return true;
950     }
951
952     public boolean stopResource(Resource resource) throws SimulatorException {
953         if (null == resource) {
954             return false;
955         }
956         SimulatorResource server = resource.getSimulatorResource();
957         if (null == server) {
958             return false;
959         }
960         try {
961             server.stop();
962             resource.setStarted(false);
963         } catch (SimulatorException e) {
964             Activator
965                     .getDefault()
966                     .getLogManager()
967                     .log(Level.ERROR.ordinal(),
968                             new Date(),
969                             "There is an error while stopping the resource.\n"
970                                     + Utility.getSimulatorErrorString(e, null));
971             throw e;
972         }
973         return true;
974     }
975
976     public boolean changeResourceName(Resource resource, String newName)
977             throws SimulatorException {
978         if (null == resource || null == newName) {
979             return false;
980         }
981
982         if (!stopResource(resource)) {
983             return false;
984         }
985
986         SimulatorResource server = resource.getSimulatorResource();
987         try {
988             server.setName(newName);
989             resource.setResourceName(newName);
990         } catch (SimulatorException e) {
991             Activator
992                     .getDefault()
993                     .getLogManager()
994                     .log(Level.ERROR.ordinal(),
995                             new Date(),
996                             "There is an error while changing the resource name.\n"
997                                     + Utility.getSimulatorErrorString(e, null));
998             throw e;
999         }
1000
1001         if (!startResource(resource)) {
1002             return false;
1003         }
1004
1005         return true;
1006     }
1007
1008     public boolean changeResourceURI(Resource resource, String newURI)
1009             throws SimulatorException {
1010         if (null == resource || null == newURI) {
1011             return false;
1012         }
1013
1014         if (!stopResource(resource)) {
1015             return false;
1016         }
1017
1018         String curURI = resource.getResourceURI();
1019         setResourceURI(resource, newURI);
1020
1021         try {
1022             if (!startResource(resource)) {
1023                 return false;
1024             }
1025         } catch (SimulatorException e) {
1026             setResourceURI(resource, curURI);
1027         }
1028
1029         return true;
1030     }
1031
1032     public boolean changeResourceType(Resource resource, String newResourceType)
1033             throws SimulatorException {
1034         if (null == resource || null == newResourceType) {
1035             return false;
1036         }
1037
1038         if (!stopResource(resource)) {
1039             return false;
1040         }
1041
1042         String curResourceType = resource.getResourceType();
1043         setResourceType(resource, newResourceType);
1044
1045         try {
1046             if (!startResource(resource)) {
1047                 return false;
1048             }
1049         } catch (SimulatorException e) {
1050             setResourceType(resource, curResourceType);
1051         }
1052
1053         return true;
1054     }
1055
1056     public void setResourceURI(Resource resource, String newURI)
1057             throws SimulatorException {
1058         String curURI = resource.getResourceURI();
1059         SimulatorResource server = resource.getSimulatorResource();
1060         try {
1061             server.setURI(newURI);
1062             data.changeResourceURI(resource, curURI, newURI);
1063         } catch (SimulatorException e) {
1064             Activator
1065                     .getDefault()
1066                     .getLogManager()
1067                     .log(Level.ERROR.ordinal(),
1068                             new Date(),
1069                             "There is an error while changing the resource URI.\n"
1070                                     + Utility.getSimulatorErrorString(e, null));
1071             throw e;
1072         }
1073     }
1074
1075     public void setResourceType(Resource resource, String newResourceType)
1076             throws SimulatorException {
1077         SimulatorResource server = resource.getSimulatorResource();
1078         try {
1079             server.setResourceType(newResourceType);
1080             resource.setResourceType(newResourceType);
1081         } catch (SimulatorException e) {
1082             Activator
1083                     .getDefault()
1084                     .getLogManager()
1085                     .log(Level.ERROR.ordinal(),
1086                             new Date(),
1087                             "There is an error while changing the resource Type.\n"
1088                                     + Utility.getSimulatorErrorString(e, null));
1089             throw e;
1090         }
1091     }
1092
1093     public boolean updateResourceProperties(Resource resource,
1094             List<MetaProperty> properties, boolean uriChanged,
1095             boolean nameChanged, boolean resTypeChanged)
1096             throws SimulatorException {
1097         if (null == resource || null == properties) {
1098             return false;
1099         }
1100
1101         // Updating the properties
1102         Iterator<MetaProperty> itr = properties.iterator();
1103         MetaProperty property;
1104         String propName;
1105         String propValue;
1106         String resName = null;
1107         String resURI = null;
1108         String resType = null;
1109         while (itr.hasNext()) {
1110             property = itr.next();
1111             if (null == property) {
1112                 continue;
1113             }
1114             propName = property.getPropName();
1115             propValue = property.getPropValue();
1116             if (propName.equals(Constants.RESOURCE_NAME)) {
1117                 resName = propValue;
1118             } else if (propName.equals(Constants.RESOURCE_URI)) {
1119                 resURI = propValue;
1120             } else if (propName.equals(Constants.RESOURCE_TYPE)) {
1121                 resType = propValue;
1122             }
1123         }
1124
1125         if (nameChanged) {
1126             if (!changeResourceName(resource, resName)) {
1127                 return false;
1128             }
1129
1130             // Notify UI Listeners
1131             UiListenerHandler.getInstance().propertiesChangedUINotification(
1132                     Resource.class);
1133         }
1134
1135         if (uriChanged) {
1136             if (!changeResourceURI(resource, resURI)) {
1137                 return false;
1138             }
1139         }
1140
1141         if (resTypeChanged) {
1142             if (!changeResourceType(resource, resType)) {
1143                 return false;
1144             }
1145         }
1146
1147         return true;
1148     }
1149
1150     public boolean updateResourceInterfaces(Resource resource,
1151             Set<String> newIfSet) throws SimulatorException {
1152         if (null == resource || null == newIfSet || newIfSet.isEmpty()) {
1153             return false;
1154         }
1155         SimulatorResource jResource = resource.getSimulatorResource();
1156         if (null == jResource) {
1157             return false;
1158         }
1159         Set<String> curIfSet = resource.getResourceInterfaces();
1160         Iterator<String> itr = curIfSet.iterator();
1161         String interfaceType;
1162         boolean resourceRestartRequired = false;
1163         while (itr.hasNext()) {
1164             interfaceType = itr.next();
1165             if (!newIfSet.contains(interfaceType)) {
1166                 resourceRestartRequired = true;
1167                 break;
1168             }
1169         }
1170
1171         try {
1172             // As there is no support from native layer for interface removal,
1173             // supporting it from the simulator requires restarting the resource
1174             // if any existing interfaces are removed.
1175
1176             if (resourceRestartRequired) {
1177                 stopResource(resource);
1178                 jResource.setInterface(Utility
1179                         .convertSetToVectorString(newIfSet));
1180                 startResource(resource);
1181             } else {
1182                 // Existing interfaces are not removed.
1183                 itr = newIfSet.iterator();
1184                 while (itr.hasNext()) {
1185                     interfaceType = itr.next();
1186                     if (!curIfSet.contains(interfaceType)) {
1187                         jResource.addInterface(interfaceType);
1188                     }
1189                 }
1190             }
1191         } catch (SimulatorException e) {
1192             Activator
1193                     .getDefault()
1194                     .getLogManager()
1195                     .log(Level.ERROR.ordinal(),
1196                             new Date(),
1197                             "There is an error while changing the interface types."
1198                                     + Utility.getSimulatorErrorString(e, null));
1199             throw e;
1200         }
1201
1202         // Set the resource interfaces.
1203         resource.setResourceInterfaces(newIfSet);
1204
1205         return true;
1206     }
1207
1208     public boolean attributeValueUpdated(SingleResource resource,
1209             String attributeName, AttributeValue value) {
1210         if (null != resource && null != attributeName && null != value) {
1211             SimulatorSingleResource simRes = (SimulatorSingleResource) resource
1212                     .getSimulatorResource();
1213             if (null != simRes) {
1214                 try {
1215                     simRes.updateAttribute(attributeName, value);
1216                     return true;
1217                 } catch (SimulatorException e) {
1218                     Activator
1219                             .getDefault()
1220                             .getLogManager()
1221                             .log(Level.ERROR.ordinal(), new Date(),
1222                                     Utility.getSimulatorErrorString(e, null));
1223                 }
1224             }
1225         }
1226         return false;
1227     }
1228
1229     public boolean isResourceStarted(Resource resource) {
1230         if (null == resource) {
1231             return false;
1232         }
1233         return resource.isStarted();
1234     }
1235
1236     public boolean isPropertyValueInvalid(Resource resource,
1237             List<MetaProperty> properties, String propName) {
1238         if (null == resource || null == properties || null == propName) {
1239             return false;
1240         }
1241         boolean invalid = false;
1242         MetaProperty prop;
1243         Iterator<MetaProperty> itr = properties.iterator();
1244         while (itr.hasNext()) {
1245             prop = itr.next();
1246             if (prop.getPropName().equals(propName)) {
1247                 String value = prop.getPropValue();
1248                 if (propName.equals(Constants.RESOURCE_URI)) {
1249                     if (!Utility.isUriValid(value)) {
1250                         invalid = true;
1251                     }
1252                 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
1253                     if (!Utility.isResourceTypeValid(value)) {
1254                         invalid = true;
1255                     }
1256                 } else {
1257                     if (null == value || value.trim().isEmpty()) {
1258                         invalid = true;
1259                     }
1260                 }
1261             }
1262         }
1263         return invalid;
1264     }
1265
1266     public boolean isPropValueChanged(Resource resource,
1267             List<MetaProperty> properties, String propName) {
1268         if (null == resource || null == properties || null == propName) {
1269             return false;
1270         }
1271         boolean changed = false;
1272         MetaProperty prop;
1273         String oldValue;
1274         Iterator<MetaProperty> itr = properties.iterator();
1275         while (itr.hasNext()) {
1276             prop = itr.next();
1277             if (prop.getPropName().equals(propName)) {
1278                 oldValue = getPropertyValueFromResource(resource, propName);
1279                 if (null != oldValue && !prop.getPropValue().equals(oldValue)) {
1280                     changed = true;
1281                 }
1282                 break;
1283             }
1284         }
1285         return changed;
1286     }
1287
1288     private String getPropertyValueFromResource(Resource resource,
1289             String propName) {
1290         if (null == resource || null == propName) {
1291             return null;
1292         }
1293         if (propName.equals(Constants.RESOURCE_URI)) {
1294             return resource.getResourceURI();
1295         } else if (propName.equals(Constants.RESOURCE_NAME)) {
1296             return resource.getResourceName();
1297         } else if (propName.equals(Constants.RESOURCE_TYPE)) {
1298             return resource.getResourceType();
1299         } else {
1300             return null;
1301         }
1302     }
1303
1304     public boolean isAttHasRangeOrAllowedValues(SimulatorResourceAttribute att) {
1305         if (null == att) {
1306             return false;
1307         }
1308         AttributeProperty prop = att.property();
1309         if (null == prop) {
1310             return false;
1311         }
1312
1313         if (prop.getType() == Type.INTEGER) {
1314             IntegerProperty intProperty = prop.asInteger();
1315             if (null != intProperty) {
1316                 return (intProperty.hasRange() || intProperty.hasValues());
1317             } else {
1318                 return false;
1319             }
1320         } else if (prop.getType() == Type.DOUBLE) {
1321             DoubleProperty dblProperty = prop.asDouble();
1322             if (null != dblProperty) {
1323                 return (dblProperty.hasRange() || dblProperty.hasValues());
1324             } else {
1325                 return false;
1326             }
1327         } else if (prop.getType() == Type.STRING) {
1328             StringProperty stringProperty = prop.asString();
1329             if (null != stringProperty) {
1330                 return stringProperty.hasValues();
1331             } else {
1332                 return false;
1333             }
1334         }
1335
1336         return true;
1337     }
1338
1339     public int startAutomation(SingleResource resource,
1340             AttributeElement attribute, AutoUpdateType autoType,
1341             int autoUpdateInterval) {
1342         int autoId = -1;
1343         if (null != resource && null != attribute) {
1344             SimulatorSingleResource server = (SimulatorSingleResource) resource
1345                     .getSimulatorResource();
1346             if (null != server) {
1347                 String attrName = attribute.getSimulatorResourceAttribute()
1348                         .name();
1349                 try {
1350                     autoId = server.startAttributeUpdation(attrName, autoType,
1351                             autoUpdateInterval, automationListener);
1352                 } catch (SimulatorException e) {
1353                     Activator
1354                             .getDefault()
1355                             .getLogManager()
1356                             .log(Level.ERROR.ordinal(),
1357                                     new Date(),
1358                                     "[" + e.getClass().getSimpleName() + "]"
1359                                             + e.code().toString() + "-"
1360                                             + e.message());
1361                     return -1;
1362                 }
1363                 if (-1 != autoId) {
1364                     attribute.setAutoUpdateId(autoId);
1365                     attribute.setAutoUpdateType(autoType);
1366                     attribute.setAutoUpdateInterval(autoUpdateInterval);
1367                     attribute.setAutoUpdateState(true);
1368                     resource.setAttributeAutomationInProgress(true);
1369                 }
1370             }
1371         }
1372         return autoId;
1373     }
1374
1375     public void stopAutomation(SingleResource resource, AttributeElement att,
1376             int autoId) {
1377         if (null != resource) {
1378             SimulatorSingleResource server = (SimulatorSingleResource) resource
1379                     .getSimulatorResource();
1380             if (null != server) {
1381                 try {
1382                     server.stopUpdation(autoId);
1383                 } catch (SimulatorException e) {
1384                     Activator
1385                             .getDefault()
1386                             .getLogManager()
1387                             .log(Level.ERROR.ordinal(),
1388                                     new Date(),
1389                                     "[" + e.getClass().getSimpleName() + "]"
1390                                             + e.code().toString() + "-"
1391                                             + e.message());
1392                     return;
1393                 }
1394                 // Change the automation status
1395                 att.setAutoUpdateState(false);
1396                 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
1397             }
1398         }
1399     }
1400
1401     public boolean startResourceAutomationUIRequest(AutoUpdateType autoType,
1402             int autoUpdateInterval, final SingleResource resource) {
1403         if (null == resource) {
1404             return false;
1405         }
1406         boolean status = false;
1407         // Invoke the native automation method
1408         SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
1409                 .getSimulatorResource();
1410         if (null != resourceServer) {
1411             int autoId = -1;
1412             try {
1413                 autoId = resourceServer.startResourceUpdation(autoType,
1414                         autoUpdateInterval, automationListener);
1415             } catch (SimulatorException e) {
1416                 Activator
1417                         .getDefault()
1418                         .getLogManager()
1419                         .log(Level.ERROR.ordinal(), new Date(),
1420                                 Utility.getSimulatorErrorString(e, null));
1421                 autoId = -1;
1422             }
1423             if (-1 != autoId) {
1424                 // Automation request accepted.
1425                 changeResourceLevelAutomationStatus(resource, true);
1426
1427                 resource.setAutomationId(autoId);
1428
1429                 // Notify the UI listeners in a different thread.
1430                 Thread notifyThread = new Thread() {
1431                     public void run() {
1432                         UiListenerHandler.getInstance()
1433                                 .resourceAutomationStartedUINotification(
1434                                         resource);
1435                     };
1436                 };
1437                 notifyThread.setPriority(Thread.MAX_PRIORITY);
1438                 notifyThread.start();
1439
1440                 status = true;
1441             }
1442         }
1443         return status;
1444     }
1445
1446     public boolean stopResourceAutomationUIRequest(final SingleResource resource) {
1447         if (null == resource) {
1448             return false;
1449         }
1450         final int autoId = resource.getAutomationId();
1451         if (-1 == autoId) {
1452             return false;
1453         }
1454         SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
1455                 .getSimulatorResource();
1456         if (null == resourceServer) {
1457             return false;
1458         }
1459         // Call native method
1460         try {
1461             resourceServer.stopUpdation(autoId);
1462         } catch (SimulatorException e) {
1463             Activator
1464                     .getDefault()
1465                     .getLogManager()
1466                     .log(Level.ERROR.ordinal(), new Date(),
1467                             Utility.getSimulatorErrorString(e, null));
1468             return false;
1469         }
1470
1471         // Notify the UI Listeners. Invoke the automation complete callback.
1472         Thread stopThread = new Thread() {
1473             public void run() {
1474                 automationListener.onUpdateComplete(resource.getResourceURI(),
1475                         autoId);
1476             }
1477         };
1478         stopThread.start();
1479         return true;
1480     }
1481
1482     private boolean isAnyAttributeInAutomation(SingleResource resource) {
1483         if (null == resource || null == resource.getResourceRepresentation()) {
1484             return false;
1485         }
1486
1487         Map<String, AttributeElement> attributes = resource
1488                 .getResourceRepresentation().getAttributes();
1489         if (null == attributes || 0 == attributes.size())
1490             return false;
1491
1492         for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1493             if (entry.getValue().isAutoUpdateInProgress())
1494                 return true;
1495         }
1496
1497         return false;
1498     }
1499
1500     // Changes the automation state of the resource and its attributes
1501     private void changeResourceLevelAutomationStatus(SingleResource resource,
1502             boolean status) {
1503
1504         if (null == resource || null == resource.getResourceRepresentation()) {
1505             return;
1506         }
1507
1508         Map<String, AttributeElement> attributes = resource
1509                 .getResourceRepresentation().getAttributes();
1510         if (null == attributes || 0 == attributes.size())
1511             return;
1512
1513         for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1514             entry.getValue().setAutoUpdateState(status);
1515         }
1516
1517         resource.setResourceAutomationInProgress(status);
1518     }
1519
1520     private AttributeElement getAttributeWithGivenAutomationId(
1521             SingleResource resource, int automationId) {
1522         if (null == resource || null == resource.getResourceRepresentation()) {
1523             return null;
1524         }
1525
1526         Map<String, AttributeElement> attributes = resource
1527                 .getResourceRepresentation().getAttributes();
1528         if (null == attributes || 0 == attributes.size())
1529             return null;
1530
1531         for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1532             if (automationId == entry.getValue().getAutoUpdateId())
1533                 return entry.getValue();
1534         }
1535
1536         return null;
1537     }
1538
1539     public boolean isResourceAutomationStarted(SingleResource resource) {
1540         boolean status = false;
1541         if (null != resource) {
1542             status = resource.isResourceAutomationInProgress();
1543         }
1544         return status;
1545     }
1546
1547     public boolean isAttributeAutomationStarted(SingleResource resource) {
1548         if (null == resource) {
1549             return false;
1550         }
1551         return resource.isAttributeAutomationInProgress();
1552     }
1553
1554     public void notifyObserverRequest(Resource resource, int observerId) {
1555         if (null == resource) {
1556             return;
1557         }
1558         SimulatorResource simulatorResource = resource.getSimulatorResource();
1559         if (null == simulatorResource) {
1560             return;
1561         }
1562         try {
1563             simulatorResource.notifyObserver(observerId);
1564         } catch (SimulatorException e) {
1565             Activator
1566                     .getDefault()
1567                     .getLogManager()
1568                     .log(Level.ERROR.ordinal(), new Date(),
1569                             Utility.getSimulatorErrorString(e, null));
1570         }
1571     }
1572
1573     public List<String> getAllValuesOfAttribute(SimulatorResourceAttribute att) {
1574         if (null == att) {
1575             return null;
1576         }
1577
1578         AttributeValue val = att.value();
1579         if (null == val) {
1580             return null;
1581         }
1582
1583         TypeInfo type = val.typeInfo();
1584
1585         AttributeProperty prop = att.property();
1586         if (null == prop) {
1587             return null;
1588         }
1589
1590         List<String> values = new ArrayList<String>();
1591
1592         if (type.mType != ValueType.RESOURCEMODEL) {
1593             if (type.mType == ValueType.ARRAY) {
1594                 if (type.mDepth == 1) {
1595                     ArrayProperty arrayProperty = prop.asArray();
1596                     if (null != arrayProperty) {
1597                         AttributeProperty childProp = arrayProperty
1598                                 .getElementProperty();
1599                         switch (childProp.getType()) {
1600                             case INTEGER:
1601                                 IntegerProperty intProperty = childProp
1602                                         .asInteger();
1603                                 if (null != intProperty) {
1604                                     values.addAll(getAllValues(intProperty,
1605                                             Type.INTEGER));
1606                                 }
1607                                 break;
1608                             case DOUBLE:
1609                                 DoubleProperty dblProperty = childProp
1610                                         .asDouble();
1611                                 if (null != dblProperty) {
1612                                     values.addAll(getAllValues(dblProperty,
1613                                             Type.DOUBLE));
1614                                 }
1615                                 break;
1616                             case BOOLEAN:
1617                                 BooleanProperty boolProperty = childProp
1618                                         .asBoolean();
1619                                 if (null != boolProperty) {
1620                                     values.addAll(getAllValues(boolProperty,
1621                                             Type.BOOLEAN));
1622                                 }
1623                                 break;
1624                             case STRING:
1625                                 StringProperty stringProperty = childProp
1626                                         .asString();
1627                                 if (null != stringProperty) {
1628                                     values.addAll(getAllValues(stringProperty,
1629                                             Type.STRING));
1630                                 }
1631                                 break;
1632                             default:
1633                                 break;
1634                         }
1635                     }
1636                 }
1637             } else {
1638                 switch (prop.getType()) {
1639                     case INTEGER:
1640                         IntegerProperty intProperty = prop.asInteger();
1641                         if (null != intProperty) {
1642                             values.addAll(getAllValues(intProperty,
1643                                     Type.INTEGER));
1644                         }
1645                         break;
1646                     case DOUBLE:
1647                         DoubleProperty dblProperty = prop.asDouble();
1648                         if (null != dblProperty) {
1649                             values.addAll(getAllValues(dblProperty, Type.DOUBLE));
1650                         }
1651                         break;
1652                     case BOOLEAN:
1653                         BooleanProperty boolProperty = prop.asBoolean();
1654                         if (null != boolProperty) {
1655                             values.addAll(getAllValues(boolProperty,
1656                                     Type.BOOLEAN));
1657                         }
1658                         break;
1659                     case STRING:
1660                         StringProperty stringProperty = prop.asString();
1661                         if (null != stringProperty) {
1662                             values.addAll(getAllValues(stringProperty,
1663                                     Type.STRING));
1664                         }
1665                         break;
1666                     default:
1667                         break;
1668                 }
1669             }
1670         }
1671
1672         return values;
1673     }
1674
1675     public List<String> getAllValues(IntegerProperty intProperty,
1676             AttributeProperty.Type type) {
1677         List<String> values = new ArrayList<String>();
1678
1679         if (intProperty.hasRange()) {
1680             int min = (int) intProperty.min();
1681             int max = (int) intProperty.max();
1682             for (int iVal = min; iVal <= max; iVal++) {
1683                 values.add(String.valueOf(iVal));
1684             }
1685         } else if (intProperty.hasValues()) {
1686             for (Integer val : intProperty.getValues()) {
1687                 values.add(String.valueOf(val));
1688             }
1689         } else {
1690             // Adding the default value.
1691             values.add(String.valueOf(intProperty.getDefaultValue()));
1692         }
1693         return values;
1694     }
1695
1696     public List<String> getAllValues(DoubleProperty dblProperty,
1697             AttributeProperty.Type type) {
1698         NumberFormat formatter = NumberFormat.getInstance();
1699         List<String> values = new ArrayList<String>();
1700
1701         if (dblProperty.hasRange()) {
1702             double min = (double) dblProperty.min();
1703             double max = (double) dblProperty.max();
1704             for (double val = min; val <= max; val += 0.1) {
1705                 formatter.setMaximumFractionDigits(1);
1706                 formatter.setMinimumFractionDigits(1);
1707                 values.add(formatter.format(val));
1708             }
1709         } else if (dblProperty.hasValues()) {
1710             for (Double val : dblProperty.getValues()) {
1711                 values.add(String.valueOf(val));
1712             }
1713         } else {
1714             // Adding the default value.
1715             values.add(String.valueOf(dblProperty.getDefaultValue()));
1716         }
1717         return values;
1718     }
1719
1720     public List<String> getAllValues(BooleanProperty boolProperty,
1721             AttributeProperty.Type type) {
1722         List<String> values = new ArrayList<String>();
1723         values.add("true");
1724         values.add("false");
1725         return values;
1726     }
1727
1728     public List<String> getAllValues(StringProperty stringProperty,
1729             AttributeProperty.Type type) {
1730         List<String> values = new ArrayList<String>();
1731
1732         if (stringProperty.hasValues()) {
1733             for (String val : stringProperty.getValues()) {
1734                 values.add(String.valueOf(val));
1735             }
1736         } else {
1737             // Adding the default value.
1738             values.add(String.valueOf(stringProperty.getDefaultValue()));
1739         }
1740         return values;
1741     }
1742
1743     public int getResourceCount() {
1744         return data.getResourceCount();
1745     }
1746
1747     public void shutdown() {
1748         threadHandle.interrupt();
1749     }
1750 }