Code clean-up, Bug fixes and enhancements in eclipse plug-ins.
[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 java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Date;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.Vector;
28
29 import oic.simulator.serviceprovider.Activator;
30 import oic.simulator.serviceprovider.model.AttributeElement;
31 import oic.simulator.serviceprovider.model.CollectionResource;
32 import oic.simulator.serviceprovider.model.Device;
33 import oic.simulator.serviceprovider.model.MetaProperty;
34 import oic.simulator.serviceprovider.model.Resource;
35 import oic.simulator.serviceprovider.model.ResourceType;
36 import oic.simulator.serviceprovider.model.SingleResource;
37 import oic.simulator.serviceprovider.utils.Constants;
38 import oic.simulator.serviceprovider.utils.Utility;
39
40 import org.eclipse.swt.widgets.Display;
41 import org.oic.simulator.AttributeProperty;
42 import org.oic.simulator.AttributeProperty.Type;
43 import org.oic.simulator.AttributeValue;
44 import org.oic.simulator.AttributeValue.TypeInfo;
45 import org.oic.simulator.AttributeValue.ValueType;
46 import org.oic.simulator.DeviceInfo;
47 import org.oic.simulator.DeviceListener;
48 import org.oic.simulator.ILogger.Level;
49 import org.oic.simulator.PlatformInfo;
50 import org.oic.simulator.SimulatorException;
51 import org.oic.simulator.SimulatorManager;
52 import org.oic.simulator.SimulatorResourceAttribute;
53 import org.oic.simulator.SimulatorResourceModel;
54 import org.oic.simulator.server.Observer;
55 import org.oic.simulator.server.SimulatorCollectionResource;
56 import org.oic.simulator.server.SimulatorResource;
57 import org.oic.simulator.server.SimulatorResource.AutoUpdateListener;
58 import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
59 import org.oic.simulator.server.SimulatorResource.ObserverListener;
60 import org.oic.simulator.server.SimulatorResource.ResourceModelChangeListener;
61 import org.oic.simulator.server.SimulatorSingleResource;
62
63 /**
64  * This class acts as an interface between the simulator java SDK and the
65  * various UI modules. It maintains all the details of resources and provides
66  * other UI modules with the information required. It also handles model change,
67  * automation, and observer related events from native layer and propagates
68  * those events to the registered UI listeners.
69  */
70 public class ResourceManager {
71
72     private Data                           data;
73
74     private Resource                       currentResourceInSelection;
75
76     private Device                         currentDeviceInSelection;
77
78     private ResourceModelChangeListener    resourceModelChangeListener;
79
80     private AutoUpdateListener             automationListener;
81
82     private ObserverListener               observer;
83
84     private DeviceListener                 deviceListener;
85
86     private NotificationSynchronizerThread synchronizerThread;
87
88     private Thread                         threadHandle;
89
90     private DeviceInfo                     deviceInfo;
91     private PlatformInfo                   platformInfo;
92
93     private String                         deviceName;
94
95     public ResourceManager() {
96         data = new Data();
97
98         // Set the default device and platform information
99         deviceName = "IoTivity Simulator";
100         try {
101             SimulatorManager.setDeviceInfo(deviceName);
102         } catch (SimulatorException e) {
103             Activator
104                     .getDefault()
105                     .getLogManager()
106                     .log(Level.ERROR.ordinal(),
107                             new Date(),
108                             "Error while registering the device info.\n"
109                                     + Utility.getSimulatorErrorString(e, null));
110         }
111
112         platformInfo = new PlatformInfo();
113         platformInfo.setPlatformID("Samsung Platform Identifier");
114         platformInfo.setManufacturerName("Samsung");
115         platformInfo.setManufacturerUrl("www.samsung.com");
116         platformInfo.setModelNumber("Samsung Model Num01");
117         platformInfo.setDateOfManufacture("2015-09-10T11:10:30Z");
118         platformInfo.setPlatformVersion("PlatformVersion01");
119         platformInfo.setOperationSystemVersion("OSVersion01");
120         platformInfo.setHardwareVersion("HardwareVersion01");
121         platformInfo.setFirmwareVersion("FirwareVersion01");
122         platformInfo.setSupportUrl("http://www.samsung.com/support");
123         platformInfo.setSystemTime("2015-09-10T11:10:30Z");
124         try {
125             SimulatorManager.setPlatformInfo(platformInfo);
126         } catch (SimulatorException e) {
127             Activator
128                     .getDefault()
129                     .getLogManager()
130                     .log(Level.ERROR.ordinal(),
131                             new Date(),
132                             "Error while registering the platform info.\n"
133                                     + Utility.getSimulatorErrorString(e, null));
134         }
135
136         deviceListener = new DeviceListener() {
137
138             @Override
139             public void onDeviceFound(final String host,
140                     final DeviceInfo deviceInfo) {
141                 if (null != ResourceManager.this.deviceInfo
142                         || null == deviceInfo || null == host) {
143                     return;
144                 }
145                 synchronizerThread.addToQueue(new Runnable() {
146                     @Override
147                     public void run() {
148                         String rcvdDeviceName = deviceInfo.getName();
149                         if (null == rcvdDeviceName) {
150                             return;
151                         }
152                         if (deviceName.equalsIgnoreCase(rcvdDeviceName)) {
153                             ResourceManager.this.deviceInfo = deviceInfo;
154
155                             // Notify the UI Listeners
156                             UiListenerHandler.getInstance()
157                                     .deviceInfoReceivedNotification();
158                         }
159                     }
160                 });
161             }
162         };
163
164         // Get the device information to show other details of the device in UI.
165         try {
166             SimulatorManager.findDevices("", deviceListener);
167         } catch (SimulatorException e) {
168             Activator
169                     .getDefault()
170                     .getLogManager()
171                     .log(Level.ERROR.ordinal(),
172                             new Date(),
173                             "Failed to get the local device information.\n"
174                                     + Utility.getSimulatorErrorString(e, null));
175         }
176
177         resourceModelChangeListener = new ResourceModelChangeListener() {
178
179             @Override
180             public void onResourceModelChanged(final String resourceURI,
181                     final SimulatorResourceModel resourceModelN) {
182                 synchronizerThread.addToQueue(new Runnable() {
183
184                     @Override
185                     public void run() {
186                         if (null == resourceURI || null == resourceModelN) {
187                             return;
188                         }
189
190                         Display.getDefault().asyncExec(new Runnable() {
191                             @Override
192                             public void run() {
193                                 Resource resource = data
194                                         .getResourceByURI(resourceURI);
195                                 if (null != resource) {
196                                     try {
197                                         resource.setResourceRepresentation(resourceModelN);
198                                     } catch (NumberFormatException e) {
199                                         Activator
200                                                 .getDefault()
201                                                 .getLogManager()
202                                                 .log(Level.ERROR.ordinal(),
203                                                         new Date(),
204                                                         "Error while trying to update the attributes.\n"
205                                                                 + Utility
206                                                                         .getSimulatorErrorString(
207                                                                                 e,
208                                                                                 null));
209                                     }
210                                 }
211                             }
212                         });
213                     }
214                 });
215             }
216         };
217
218         automationListener = new AutoUpdateListener() {
219
220             @Override
221             public void onUpdateComplete(final String resourceURI,
222                     final int automationId) {
223                 synchronizerThread.addToQueue(new Runnable() {
224
225                     @Override
226                     public void run() {
227                         SingleResource resource = data
228                                 .getSingleResourceByURI(resourceURI);
229                         if (null == resource) {
230                             return;
231                         }
232                         // Checking whether this notification is for an
233                         // attribute or a resource
234                         if (resource.isResourceAutomationInProgress()) {
235                             changeResourceLevelAutomationStatus(resource, false);
236                             // Notify the UI listeners
237                             UiListenerHandler.getInstance()
238                                     .automationCompleteUINotification(resource,
239                                             null);
240                         } else if (resource.isAttributeAutomationInProgress()) {
241                             // Find the attribute with the given automation id
242                             final AttributeElement attribute = getAttributeWithGivenAutomationId(
243                                     resource, automationId);
244                             if (null != attribute) {
245                                 attribute.setAutoUpdateState(false);
246                                 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
247                             } else {
248                                 // Setting the attribute automation status to
249                                 // false.
250                                 resource.setAttributeAutomationInProgress(false);
251                             }
252                         }
253                     }
254                 });
255             }
256         };
257
258         observer = new ObserverListener() {
259
260             public void onObserverChanged(final String resourceURI,
261                     final int status, final Observer observer) {
262                 new Thread() {
263                     @Override
264                     public void run() {
265                         if (null == resourceURI || null == observer) {
266                             return;
267                         }
268                         Resource resource = data.getResourceByURI(resourceURI);
269                         if (null == resource) {
270                             return;
271                         }
272                         // Update the observers information
273                         if (status == 0) {
274                             resource.addObserverInfo(observer);
275                         } else {
276                             resource.removeObserverInfo(observer);
277                         }
278                         // Notify the UI listeners
279                         UiListenerHandler.getInstance()
280                                 .observerListChangedUINotification(resource);
281                     }
282                 }.start();
283             }
284
285             @Override
286             public void onObserverAdded(String resourceURI, Observer observer) {
287                 onObserverChanged(resourceURI, 0, observer);
288             }
289
290             @Override
291             public void onObserverRemoved(String resourceURI, Observer observer) {
292                 onObserverChanged(resourceURI, 1, observer);
293             }
294         };
295
296         synchronizerThread = new NotificationSynchronizerThread();
297         threadHandle = new Thread(synchronizerThread);
298         threadHandle.setName("Simulator service provider event queue");
299         threadHandle.start();
300     }
301
302     private static class NotificationSynchronizerThread implements Runnable {
303
304         LinkedList<Runnable> notificationQueue = new LinkedList<Runnable>();
305
306         @Override
307         public void run() {
308             while (!Thread.interrupted()) {
309                 synchronized (this) {
310                     try {
311                         while (notificationQueue.isEmpty()) {
312                             this.wait();
313                             break;
314                         }
315                     } catch (InterruptedException e) {
316                         return;
317                     }
318                 }
319
320                 Runnable thread;
321                 synchronized (this) {
322                     thread = notificationQueue.pop();
323                 }
324                 try {
325                     thread.run();
326                 } catch (Exception e) {
327                     if (e instanceof InterruptedException) {
328                         return;
329                     }
330                     e.printStackTrace();
331                 }
332             }
333         }
334
335         public void addToQueue(Runnable event) {
336             synchronized (this) {
337                 notificationQueue.add(event);
338                 this.notify();
339             }
340         }
341     }
342
343     public void setDeviceInfo(List<MetaProperty> metaProperties) {
344         if (null == metaProperties || metaProperties.size() < 1) {
345             return;
346         }
347         Iterator<MetaProperty> itr = metaProperties.iterator();
348         MetaProperty prop;
349         String propName;
350         String propValue;
351         boolean found = false;
352         while (itr.hasNext()) {
353             prop = itr.next();
354             propName = prop.getPropName();
355             propValue = prop.getPropValue();
356             if (propName.equals(Constants.DEVICE_NAME)) {
357                 this.deviceName = propValue;
358                 found = true;
359                 break;
360             }
361         }
362
363         if (!found) {
364             return;
365         }
366
367         try {
368             SimulatorManager.setDeviceInfo(deviceName);
369         } catch (SimulatorException e) {
370             Activator
371                     .getDefault()
372                     .getLogManager()
373                     .log(Level.ERROR.ordinal(),
374                             new Date(),
375                             "Error while registering the device info.\n"
376                                     + Utility.getSimulatorErrorString(e, null));
377         }
378     }
379
380     public boolean isDeviceInfoValid(List<MetaProperty> metaProperties) {
381         if (null == metaProperties || metaProperties.size() < 1) {
382             return false;
383         }
384
385         Iterator<MetaProperty> itr = metaProperties.iterator();
386         MetaProperty prop;
387         String propName;
388         String propValue;
389         while (itr.hasNext()) {
390             prop = itr.next();
391             propName = prop.getPropName();
392             propValue = prop.getPropValue();
393             if (propName.equals(Constants.DEVICE_NAME)) {
394                 if (null == propValue || propValue.length() < 1) {
395                     return false;
396                 }
397                 break;
398             }
399         }
400         return true;
401     }
402
403     public List<MetaProperty> getDeviceInfo() {
404         List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
405         metaProperties.add(new MetaProperty(Constants.DEVICE_NAME, deviceName));
406         if (null != deviceInfo) {
407             metaProperties.add(new MetaProperty(Constants.DEVICE_ID, deviceInfo
408                     .getID()));
409             metaProperties.add(new MetaProperty(Constants.DEVICE_SPEC_VERSION,
410                     deviceInfo.getSpecVersion()));
411             metaProperties.add(new MetaProperty(Constants.DEVICE_DMV,
412                     deviceInfo.getDataModelVersion()));
413         }
414         return metaProperties;
415     }
416
417     public List<MetaProperty> getPlatformInfo() {
418         List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
419         metaProperties.add(new MetaProperty(Constants.PLATFORM_ID, platformInfo
420                 .getPlatformID()));
421         metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_NAME,
422                 platformInfo.getManufacturerName()));
423         metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_URL,
424                 platformInfo.getManufacturerUrl()));
425         metaProperties.add(new MetaProperty(Constants.PLATFORM_MODEL_NO,
426                 platformInfo.getModelNumber()));
427         metaProperties.add(new MetaProperty(Constants.PLATFORM_DATE_OF_MANUFAC,
428                 platformInfo.getDateOfManufacture()));
429         metaProperties.add(new MetaProperty(Constants.PLATFORM_VERSION,
430                 platformInfo.getPlatformVersion()));
431         metaProperties.add(new MetaProperty(Constants.PLATFORM_OS_VERSION,
432                 platformInfo.getOperationSystemVersion()));
433         metaProperties.add(new MetaProperty(
434                 Constants.PLATFORM_HARDWARE_VERSION, platformInfo
435                         .getHardwareVersion()));
436         metaProperties.add(new MetaProperty(
437                 Constants.PLATFORM_FIRMWARE_VERSION, platformInfo
438                         .getFirmwareVersion()));
439         metaProperties.add(new MetaProperty(Constants.PLATFORM_SUPPORT_URL,
440                 platformInfo.getSupportUrl()));
441         metaProperties.add(new MetaProperty(Constants.PLATFORM_SYSTEM_TIME,
442                 platformInfo.getSystemTime()));
443         return metaProperties;
444     }
445
446     public void setPlatformInfo(List<MetaProperty> metaProperties) {
447         if (null == metaProperties || metaProperties.size() < 1) {
448             return;
449         }
450         Iterator<MetaProperty> itr = metaProperties.iterator();
451         MetaProperty prop;
452         String propName;
453         String propValue;
454         while (itr.hasNext()) {
455             prop = itr.next();
456             propName = prop.getPropName();
457             propValue = prop.getPropValue();
458             if (propName.equals(Constants.PLATFORM_ID)) {
459                 platformInfo.setPlatformID(propValue);
460             } else if (propName.equals(Constants.PLATFORM_MANUFAC_NAME)) {
461                 platformInfo.setManufacturerName(propValue);
462             } else if (propName.equals(Constants.PLATFORM_MANUFAC_URL)) {
463                 platformInfo.setManufacturerUrl(propValue);
464             } else if (propName.equals(Constants.PLATFORM_MODEL_NO)) {
465                 platformInfo.setModelNumber(propValue);
466             } else if (propName.equals(Constants.PLATFORM_DATE_OF_MANUFAC)) {
467                 platformInfo.setDateOfManufacture(propValue);
468             } else if (propName.equals(Constants.PLATFORM_VERSION)) {
469                 platformInfo.setPlatformVersion(propValue);
470             } else if (propName.equals(Constants.PLATFORM_OS_VERSION)) {
471                 platformInfo.setOperationSystemVersion(propValue);
472             } else if (propName.equals(Constants.PLATFORM_HARDWARE_VERSION)) {
473                 platformInfo.setHardwareVersion(propValue);
474             } else if (propName.equals(Constants.PLATFORM_FIRMWARE_VERSION)) {
475                 platformInfo.setFirmwareVersion(propValue);
476             } else if (propName.equals(Constants.PLATFORM_SUPPORT_URL)) {
477                 platformInfo.setSupportUrl(propValue);
478             } else if (propName.equals(Constants.PLATFORM_SYSTEM_TIME)) {
479                 platformInfo.setSystemTime(propValue);
480             }
481         }
482         try {
483             SimulatorManager.setPlatformInfo(platformInfo);
484         } catch (SimulatorException e) {
485             Activator
486                     .getDefault()
487                     .getLogManager()
488                     .log(Level.ERROR.ordinal(),
489                             new Date(),
490                             "Error while registering the platform info.\n"
491                                     + Utility.getSimulatorErrorString(e, null));
492         }
493     }
494
495     public boolean isPlatformInfoValid(List<MetaProperty> metaProperties) {
496         if (null == metaProperties || metaProperties.size() < 1) {
497             return false;
498         }
499         Iterator<MetaProperty> itr = metaProperties.iterator();
500         MetaProperty prop;
501         String propValue;
502         while (itr.hasNext()) {
503             prop = itr.next();
504             propValue = prop.getPropValue();
505             if (null == propValue || propValue.length() < 1) {
506                 return false;
507             }
508         }
509         return true;
510     }
511
512     public synchronized Resource getCurrentResourceInSelection() {
513         return currentResourceInSelection;
514     }
515
516     public synchronized void setCurrentResourceInSelection(Resource resource) {
517         this.currentResourceInSelection = resource;
518     }
519
520     public synchronized Device getCurrentDeviceInSelection() {
521         return currentDeviceInSelection;
522     }
523
524     public synchronized void setCurrentDeviceInSelection(Device dev) {
525         this.currentDeviceInSelection = dev;
526     }
527
528     public boolean isResourceExist(String resourceURI) {
529         return data.isResourceExist(resourceURI);
530     }
531
532     public boolean isAnyResourceExist() {
533         return data.isAnyResourceExist();
534     }
535
536     public boolean createSingleResource(SingleResource resource,
537             Map<String, SimulatorResourceAttribute> attributes)
538             throws SimulatorException {
539         if (null == resource) {
540             return false;
541         }
542         String resType = (String) resource.getResourceTypes().toArray()[0];
543         try {
544             // 1. Create the resource.
545             SimulatorResource jSimulatorResource = SimulatorManager
546                     .createResource(SimulatorResource.Type.SINGLE,
547                             resource.getResourceName(),
548                             resource.getResourceURI(), resType);
549             if (null == jSimulatorResource
550                     || !(jSimulatorResource instanceof SimulatorSingleResource)) {
551                 return false;
552             }
553             SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) jSimulatorResource;
554             resource.setSimulatorResource(jSimulatorSingleResource);
555
556             // 2. Cancel observable property if requested by user.
557             if (!resource.isObservable()) {
558                 jSimulatorSingleResource.setObservable(false);
559             }
560
561             // 3. Set the model change listener.
562             jSimulatorSingleResource
563                     .setResourceModelChangeListener(resourceModelChangeListener);
564
565             // 4. Set the observer listener if the resource is observable.
566             if (resource.isObservable()) {
567                 jSimulatorSingleResource.setObserverListener(observer);
568             }
569
570             // 5. Add attributes.
571             if (null != attributes && !attributes.isEmpty()) {
572                 SimulatorResourceAttribute value;
573                 for (Map.Entry<String, SimulatorResourceAttribute> entry : attributes
574                         .entrySet()) {
575                     value = entry.getValue();
576                     if (null != value)
577                         jSimulatorSingleResource.addAttribute(value);
578                 }
579
580                 // 6. Get the resource model java object reference.
581                 resource.setResourceModel(jSimulatorSingleResource
582                         .getResourceModel());
583
584                 resource.setResourceRepresentation(resource.getResourceModel());
585             }
586
587             // 6. Register the resource with the platform.
588             jSimulatorSingleResource.start();
589             resource.setStarted(true);
590         } catch (SimulatorException e) {
591             Activator
592                     .getDefault()
593                     .getLogManager()
594                     .log(Level.ERROR.ordinal(), new Date(),
595                             Utility.getSimulatorErrorString(e, null));
596             throw e;
597         }
598
599         // 8. Add to local cache.
600         data.addResource(resource);
601
602         // 9. Update UI listeners
603         UiListenerHandler.getInstance().resourceCreatedUINotification(
604                 ResourceType.SINGLE);
605
606         return true;
607     }
608
609     public boolean createCollectionResource(CollectionResource resource)
610             throws SimulatorException {
611         if (null == resource) {
612             return false;
613         }
614         String resType = (String) resource.getResourceTypes().toArray()[0];
615         try {
616             // 1. Create the resource.
617             SimulatorResource jSimulatorResource = SimulatorManager
618                     .createResource(SimulatorResource.Type.COLLECTION,
619                             resource.getResourceName(),
620                             resource.getResourceURI(), resType);
621             if (null == jSimulatorResource
622                     || !(jSimulatorResource instanceof SimulatorCollectionResource)) {
623                 return false;
624             }
625             SimulatorCollectionResource jSimulatorCollectionResource = (SimulatorCollectionResource) jSimulatorResource;
626             resource.setSimulatorResource(jSimulatorCollectionResource);
627
628             // 2. Cancel observable property if requested by user.
629             if (!resource.isObservable()) {
630                 jSimulatorCollectionResource.setObservable(false);
631             }
632
633             // 3. Set the observer listener if the resource is observable.
634             if (resource.isObservable()) {
635                 jSimulatorCollectionResource.setObserverListener(observer);
636             }
637
638             // 4. Set the model change listener.
639             jSimulatorCollectionResource
640                     .setResourceModelChangeListener(resourceModelChangeListener);
641
642             // 5. Fetch the model and attributes.
643             resource.setResourceRepresentation(jSimulatorCollectionResource
644                     .getResourceModel());
645
646             // 6. Register the resource with the platform.
647             jSimulatorCollectionResource.start();
648             resource.setStarted(true);
649         } catch (SimulatorException e) {
650             Activator
651                     .getDefault()
652                     .getLogManager()
653                     .log(Level.ERROR.ordinal(), new Date(),
654                             Utility.getSimulatorErrorString(e, null));
655             throw e;
656         }
657
658         // 6. Add to local cache.
659         data.addResource(resource);
660
661         // 7. Update UI listeners
662         UiListenerHandler.getInstance().resourceCreatedUINotification(
663                 ResourceType.COLLECTION);
664
665         return true;
666     }
667
668     public Resource createResourceByRAML(String configFilePath)
669             throws SimulatorException {
670         Resource resource = null;
671         try {
672             // 1. Create the resource
673             SimulatorResource jSimulatorResource = SimulatorManager
674                     .createResource(configFilePath);
675             if (null == jSimulatorResource) {
676                 return null;
677             }
678             if (jSimulatorResource instanceof SimulatorSingleResource) {
679                 resource = new SingleResource();
680             } else {
681                 resource = new CollectionResource();
682             }
683             resource.setSimulatorResource(jSimulatorResource);
684
685             // 2. Fetch and locally store the resource name and uri.
686             String uri = jSimulatorResource.getURI();
687             if (null == uri || uri.trim().isEmpty()) {
688                 return null;
689             }
690             resource.setResourceURI(uri.trim());
691
692             String name = jSimulatorResource.getName();
693             if (null == name || name.trim().isEmpty()) {
694                 return null;
695             }
696             resource.setResourceName(name.trim());
697         } catch (SimulatorException e) {
698             Activator
699                     .getDefault()
700                     .getLogManager()
701                     .log(Level.ERROR.ordinal(), new Date(),
702                             Utility.getSimulatorErrorString(e, null));
703             throw e;
704         }
705         return resource;
706     }
707
708     /**
709      * This method can set/change the resource uri and name of an already
710      * created resource which is not yet registered with the platform. This
711      * method registers the model change and observer listeners, registers the
712      * resource, fetches the resource attributes, updates the local cache and
713      * notifies the UI listeners.
714      */
715     public boolean completeSingleResourceCreationByRAML(Resource resource,
716             String uri, String name, boolean multiInstance)
717             throws SimulatorException {
718         if (null == resource || !(resource instanceof SingleResource)) {
719             return false;
720         }
721         try {
722             SingleResource singleRes = (SingleResource) resource;
723
724             SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) resource
725                     .getSimulatorResource();
726             if (null == jSimulatorSingleResource) {
727                 return false;
728             }
729
730             // 1. Update resource URI and Name if they are changed.
731             String newUri = uri.trim();
732             String newName = name.trim();
733
734             if (multiInstance) {
735                 singleRes.setResourceURI(newUri);
736                 singleRes.setResourceName(newName);
737             } else {
738                 if (!singleRes.getResourceURI().equals(newUri)) {
739                     jSimulatorSingleResource.setURI(newUri);
740                     singleRes.setResourceURI(newUri);
741                 }
742                 if (!singleRes.getResourceName().equals(newName)) {
743                     jSimulatorSingleResource.setName(newName);
744                     singleRes.setResourceName(newName);
745                 }
746             }
747
748             // 2. Set the model change listener.
749             jSimulatorSingleResource
750                     .setResourceModelChangeListener(resourceModelChangeListener);
751
752             // 3. Set the observer listener if the resource is observable.
753             if (jSimulatorSingleResource.isObservable()) {
754                 jSimulatorSingleResource.setObserverListener(observer);
755                 singleRes.setObservable(true);
756             }
757
758             // 4. Fetch the resource model.
759             SimulatorResourceModel jResModel = jSimulatorSingleResource
760                     .getResourceModel();
761             if (null == jResModel) {
762                 return false;
763             }
764             singleRes.setResourceModel(jResModel);
765
766             // 5. Fetch the basic details of the resource.
767             singleRes.addResourceType(jSimulatorSingleResource
768                     .getResourceType());
769             singleRes
770                     .setResourceInterfaces(Utility
771                             .convertVectorToSet(jSimulatorSingleResource
772                                     .getInterface()));
773
774             // 6. Fetch the resource attributes.
775             singleRes.setResourceRepresentation(jResModel);
776
777             // 7. Register the resource with the platform.
778             jSimulatorSingleResource.start();
779             singleRes.setStarted(true);
780
781             // 8. Add to local cache.
782             data.addResource(singleRes);
783
784             // 9. Update UI listeners for single instance creation
785             if (!multiInstance)
786                 UiListenerHandler.getInstance().resourceCreatedUINotification(
787                         ResourceType.SINGLE);
788         } catch (Exception e) {
789             Activator
790                     .getDefault()
791                     .getLogManager()
792                     .log(Level.ERROR.ordinal(), new Date(),
793                             Utility.getSimulatorErrorString(e, null));
794             throw e;
795         }
796         return true;
797     }
798
799     /**
800      * This method can set/change the resource uri and name of an already
801      * created resource which is not yet registered with the platform. This
802      * method registers the model change and observer listeners, registers the
803      * resource, fetches the resource attributes, updates the local cache and
804      * notifies the UI listeners.
805      */
806     public boolean completeCollectionResourceCreationByRAML(Resource resource,
807             String uri, String name) throws SimulatorException {
808         if (null == resource || !(resource instanceof CollectionResource)) {
809             return false;
810         }
811
812         CollectionResource collectionRes = (CollectionResource) resource;
813
814         SimulatorCollectionResource jSimulatorCollectionResource = null;
815
816         try {
817             jSimulatorCollectionResource = (SimulatorCollectionResource) resource
818                     .getSimulatorResource();
819             if (null == jSimulatorCollectionResource) {
820                 return false;
821             }
822
823             // 1. Update resource URI and Name if they are changed.
824             String newUri = uri.trim();
825             String newName = name.trim();
826
827             if (!collectionRes.getResourceURI().equals(newUri)) {
828                 jSimulatorCollectionResource.setURI(newUri);
829                 collectionRes.setResourceURI(newUri);
830             }
831             if (!collectionRes.getResourceName().equals(newName)) {
832                 jSimulatorCollectionResource.setName(newName);
833                 collectionRes.setResourceName(newName);
834             }
835
836             // 2. Set the model change listener.
837             jSimulatorCollectionResource
838                     .setResourceModelChangeListener(resourceModelChangeListener);
839
840             // 3. Fetch the resource model.
841             SimulatorResourceModel jResModel = jSimulatorCollectionResource
842                     .getResourceModel();
843             if (null == jResModel) {
844                 return false;
845             }
846             collectionRes.setResourceModel(jResModel);
847
848             // 4. Fetch the basic details of the resource.
849             collectionRes.addResourceType(jSimulatorCollectionResource
850                     .getResourceType());
851             collectionRes.setResourceInterfaces(Utility
852                     .convertVectorToSet(jSimulatorCollectionResource
853                             .getInterface()));
854
855             // 5. Set the observer listener if the resource is observable.
856             if (jSimulatorCollectionResource.isObservable()) {
857                 jSimulatorCollectionResource.setObserverListener(observer);
858                 collectionRes.setObservable(true);
859             }
860
861             // 6. Fetch the resource attributes.
862             collectionRes.setResourceRepresentation(jResModel);
863
864             // 7. Register the resource with the platform.
865             jSimulatorCollectionResource.start();
866             collectionRes.setStarted(true);
867
868             // 8. Add to local cache.
869             data.addResource(collectionRes);
870
871             // 9. Update UI listeners for single instance creation
872             UiListenerHandler.getInstance().resourceCreatedUINotification(
873                     ResourceType.COLLECTION);
874         } catch (Exception e) {
875             Activator
876                     .getDefault()
877                     .getLogManager()
878                     .log(Level.ERROR.ordinal(), new Date(),
879                             Utility.getSimulatorErrorString(e, null));
880             throw e;
881         }
882         return true;
883     }
884
885     public int createSingleResourceMultiInstances(String configFile, int count)
886             throws SimulatorException {
887         int createCount = 0;
888         try {
889             Vector<SimulatorResource> jSimulatorResources = SimulatorManager
890                     .createResource(configFile, count);
891             if (null == jSimulatorResources || jSimulatorResources.size() < 1) {
892                 return 0;
893             }
894             SimulatorSingleResource jResource;
895             SingleResource resource;
896             boolean result;
897             for (SimulatorResource jSimulatorResource : jSimulatorResources) {
898                 jResource = (SimulatorSingleResource) jSimulatorResource;
899                 resource = new SingleResource();
900                 resource.setSimulatorResource(jResource);
901                 try {
902                     result = completeSingleResourceCreationByRAML(resource,
903                             jResource.getURI(), jResource.getName(), true);
904                     if (result) {
905                         createCount++;
906                     }
907                 } catch (SimulatorException eInner) {
908                     Activator
909                             .getDefault()
910                             .getLogManager()
911                             .log(Level.ERROR.ordinal(),
912                                     new Date(),
913                                     Utility.getSimulatorErrorString(eInner,
914                                             null));
915                 }
916             }
917             if (createCount > 0) {
918                 UiListenerHandler.getInstance().resourceCreatedUINotification(
919                         ResourceType.SINGLE);
920             }
921         } catch (SimulatorException eOuter) {
922             Activator
923                     .getDefault()
924                     .getLogManager()
925                     .log(Level.ERROR.ordinal(), new Date(),
926                             Utility.getSimulatorErrorString(eOuter, null));
927             throw eOuter;
928         }
929         return createCount;
930     }
931
932     public Device createDevice(String deviceName, Set<Resource> childs) {
933         // 1. Create device
934         Device dev = new Device();
935         dev.setDeviceName(deviceName);
936         data.addDevice(dev);
937
938         // 2. Add children to device
939         if (null != childs && !childs.isEmpty())
940             addResourceToDevice(dev, childs);
941
942         // 3. Update ui listeners
943         UiListenerHandler.getInstance().resourceListUpdateUINotification(
944                 ResourceType.DEVICE);
945         return dev;
946     }
947
948     public List<Resource> getResourceList() {
949         List<Resource> resourceList = data.getResources();
950         if (null == resourceList) {
951             return null;
952         }
953         // Sort the list
954         Collections.sort(resourceList, Utility.resourceComparator);
955
956         return resourceList;
957     }
958
959     public List<SingleResource> getSingleResourceList() {
960         List<SingleResource> resourceList = data.getSingleResources();
961         if (null == resourceList) {
962             return null;
963         }
964         // Sort the list
965         Collections.sort(resourceList, Utility.singleResourceComparator);
966
967         return resourceList;
968     }
969
970     public List<CollectionResource> getCollectionResourceList() {
971         List<CollectionResource> resourceList = data.getCollectionResources();
972         if (null == resourceList) {
973             return null;
974         }
975         // Sort the list
976         Collections.sort(resourceList, Utility.collectionResourceComparator);
977
978         return resourceList;
979     }
980
981     public List<Device> getDeviceList() {
982         List<Device> deviceList = data.getDevices();
983         if (null == deviceList) {
984             return null;
985         }
986         // Sort the list
987         Collections.sort(deviceList, Utility.deviceComparator);
988         return deviceList;
989     }
990
991     // Returns the number of resources which are added properly to the
992     // collection.
993     public int addResourceToCollection(CollectionResource collectionParent,
994             Set<Resource> childs) {
995         if (null == collectionParent || null == childs || childs.isEmpty()) {
996             return -1;
997         }
998         Iterator<Resource> itr = childs.iterator();
999         Resource res;
1000         int count = childs.size();
1001         while (itr.hasNext()) {
1002             res = itr.next();
1003             try {
1004                 addResourceToCollection(collectionParent, res);
1005             } catch (SimulatorException e) {
1006                 count--;
1007             }
1008         }
1009         return count;
1010     }
1011
1012     public void addResourceToCollection(CollectionResource collectionParent,
1013             Resource child) throws SimulatorException {
1014         if (null == collectionParent || null == child) {
1015             return;
1016         }
1017         try {
1018             // 1. Add child to collection
1019             collectionParent.addChildResource(child);
1020
1021             // 2. Add a reference to the collection in the child
1022             if (child instanceof SingleResource) {
1023                 ((SingleResource) child)
1024                         .addCollectionMembership(collectionParent);
1025             } else {
1026                 ((CollectionResource) child).addMembership(collectionParent);
1027             }
1028         } catch (SimulatorException e) {
1029             Activator
1030                     .getDefault()
1031                     .getLogManager()
1032                     .log(Level.ERROR.ordinal(), new Date(),
1033                             Utility.getSimulatorErrorString(e, null));
1034             throw e;
1035         }
1036     }
1037
1038     public int addResourceToCollection(Set<CollectionResource> collections,
1039             Resource child) {
1040         if (null == collections || collections.isEmpty() || null == child) {
1041             return -1;
1042         }
1043         Iterator<CollectionResource> itr = collections.iterator();
1044         CollectionResource res;
1045         int count = collections.size();
1046         while (itr.hasNext()) {
1047             res = itr.next();
1048             try {
1049                 addResourceToCollection(res, child);
1050             } catch (SimulatorException e) {
1051                 count--;
1052             }
1053         }
1054         return count;
1055     }
1056
1057     public void addResourceToDevice(Device dev, Set<Resource> childs) {
1058         // 1. Add children to the device.
1059         dev.addChildResource(childs);
1060
1061         // 2. Add a reference to the device in all children.
1062         Iterator<Resource> itr = childs.iterator();
1063         Resource res;
1064         while (itr.hasNext()) {
1065             res = itr.next();
1066             if (res instanceof SingleResource) {
1067                 ((SingleResource) res).addDeviceMembership(dev);
1068             } else {
1069                 ((CollectionResource) res).addDeviceMembership(dev);
1070             }
1071         }
1072     }
1073
1074     public void addResourceToDevice(Device dev, Resource child) {
1075         // 1. Add child to the device.
1076         dev.addChildResource(child);
1077
1078         // 2. Add a reference to the device in the child.
1079         if (child instanceof SingleResource) {
1080             ((SingleResource) child).addDeviceMembership(dev);
1081         } else {
1082             ((CollectionResource) child).addDeviceMembership(dev);
1083         }
1084     }
1085
1086     public void addResourceToDevice(Set<Device> devices, Resource child) {
1087         // 1. Add device reference in child.
1088         if (child instanceof SingleResource)
1089             ((SingleResource) child).addDeviceMembership(devices);
1090         else
1091             ((CollectionResource) child).addDeviceMembership(devices);
1092
1093         // 2. Add a reference to the child in all devices.
1094         Iterator<Device> itr = devices.iterator();
1095         Device dev;
1096         while (itr.hasNext()) {
1097             dev = itr.next();
1098             dev.addChildResource(child);
1099         }
1100     }
1101
1102     public int removeResourceFromCollection(
1103             Set<CollectionResource> collections, Resource resource) {
1104         // 1. Remove the reference of resource from all the collections.
1105         Iterator<CollectionResource> itr = collections.iterator();
1106         CollectionResource colRes;
1107         int count = collections.size();
1108         while (itr.hasNext()) {
1109             colRes = itr.next();
1110             try {
1111                 removeResourceFromCollection(colRes, resource);
1112             } catch (SimulatorException e) {
1113                 count--;
1114             }
1115         }
1116         return count;
1117
1118     }
1119
1120     public void removeResourceFromDevice(Set<Device> devices, Resource resource) {
1121         // 1. Remove the reference of resource from all the devices.
1122         Iterator<Device> itr = devices.iterator();
1123         Device dev;
1124         while (itr.hasNext()) {
1125             dev = itr.next();
1126             dev.removeChildResource(resource);
1127         }
1128
1129         // 2. Remove the reference of devices from the resource.
1130         resource.removeDeviceMembership(devices);
1131     }
1132
1133     // Returns the count of resources removed from the collection
1134     public int removeResourcesFromCollection(CollectionResource colRes,
1135             Set<Resource> resources) {
1136         Iterator<Resource> itr = resources.iterator();
1137         Resource res;
1138         int count = resources.size();
1139         while (itr.hasNext()) {
1140             res = itr.next();
1141             try {
1142                 removeResourceFromCollection(colRes, res);
1143             } catch (SimulatorException e) {
1144                 count--;
1145             }
1146         }
1147         return count;
1148     }
1149
1150     public void removeResourcesFromDevice(Device dev, Set<Resource> resources) {
1151         Iterator<Resource> itr = resources.iterator();
1152         Resource res;
1153         while (itr.hasNext()) {
1154             res = itr.next();
1155             res.removeDeviceMembership(dev);
1156         }
1157         dev.removeChildResource(resources);
1158     }
1159
1160     public void removeResourceFromCollection(CollectionResource parent,
1161             Resource child) throws SimulatorException {
1162         try {
1163             // 1. Remove the child from the parent
1164             parent.removeChildResource(child);
1165
1166             // 2. Remove the reference to parent from child
1167             if (child instanceof SingleResource) {
1168                 ((SingleResource) child).removeCollectionMembership(parent);
1169             } else {
1170                 ((CollectionResource) child).removeMembership(parent);
1171             }
1172         } catch (SimulatorException e) {
1173             Activator
1174                     .getDefault()
1175                     .getLogManager()
1176                     .log(Level.ERROR.ordinal(), new Date(),
1177                             Utility.getSimulatorErrorString(e, null));
1178             throw e;
1179         }
1180     }
1181
1182     public void removeResourceFromDevice(Device parent, Resource child) {
1183         // 1. Remove the reference to parent from child
1184         child.removeDeviceMembership(parent);
1185
1186         // 2. Remove the child from the parent
1187         parent.removeChildResource(child);
1188     }
1189
1190     public void removeSingleResources(Set<SingleResource> resources)
1191             throws SimulatorException {
1192         if (null == resources) {
1193             return;
1194         }
1195         Iterator<SingleResource> itr = resources.iterator();
1196         while (itr.hasNext()) {
1197             removeResource(itr.next());
1198         }
1199     }
1200
1201     public void removeCollectionResources(Set<CollectionResource> resources)
1202             throws SimulatorException {
1203         if (null == resources) {
1204             return;
1205         }
1206         Iterator<CollectionResource> itr = resources.iterator();
1207         while (itr.hasNext()) {
1208             removeResource(itr.next());
1209         }
1210     }
1211
1212     public void removeResource(Resource res) throws SimulatorException {
1213         // 1. Unregister the resource from the platform.
1214         SimulatorResource simRes = res.getSimulatorResource();
1215         try {
1216             simRes.stop();
1217         } catch (SimulatorException e) {
1218             Activator
1219                     .getDefault()
1220                     .getLogManager()
1221                     .log(Level.ERROR.ordinal(), new Date(),
1222                             Utility.getSimulatorErrorString(e, null));
1223             throw e;
1224         }
1225
1226         Set<CollectionResource> collectionMembership;
1227         Set<Device> deviceMembership;
1228
1229         if (res instanceof SingleResource) {
1230             collectionMembership = ((SingleResource) res)
1231                     .getCollectionMembership();
1232             deviceMembership = ((SingleResource) res).getDeviceMembership();
1233         } else {
1234             collectionMembership = ((CollectionResource) res).getMembership();
1235             deviceMembership = ((CollectionResource) res).getDeviceMembership();
1236         }
1237
1238         // 2. Delete from the collections to which this resource is a member.
1239         if (null != collectionMembership && !collectionMembership.isEmpty()) {
1240             removeResourceFromCollection(collectionMembership, res);
1241         }
1242
1243         // 3. Delete from the devices to which this resource is a member.
1244         if (null != deviceMembership && !deviceMembership.isEmpty()) {
1245             removeResourceFromDevice(deviceMembership, res);
1246         }
1247
1248         // 4. Delete this resource
1249         data.deleteResource(res);
1250     }
1251
1252     public void removeDevice(Device dev) {
1253         Set<Resource> childs = dev.getChildResources();
1254         if (null != childs && !childs.isEmpty()) {
1255             // 1. Remove the reference from all the children.
1256             Iterator<Resource> itr = childs.iterator();
1257             Resource res;
1258             while (itr.hasNext()) {
1259                 res = itr.next();
1260                 res.removeDeviceMembership(dev);
1261             }
1262         }
1263         // 2. Delete the device.
1264         data.deleteDevice(dev);
1265     }
1266
1267     public boolean isUriUnique(List<MetaProperty> properties) {
1268         if (null == properties) {
1269             return false;
1270         }
1271         MetaProperty prop;
1272         Iterator<MetaProperty> itr = properties.iterator();
1273         while (itr.hasNext()) {
1274             prop = itr.next();
1275             if (prop.getPropName().equals(Constants.RESOURCE_URI)) {
1276                 String uri = prop.getPropValue();
1277                 return !data.isResourceExist(uri);
1278             }
1279         }
1280         return false;
1281     }
1282
1283     public List<CollectionResource> getCollectionsForAddingToSingleResource(
1284             SingleResource resource) {
1285         List<CollectionResource> collectionResources = data
1286                 .getCollectionResources();
1287         if (null == collectionResources || collectionResources.isEmpty()) {
1288             return null;
1289         }
1290
1291         Set<CollectionResource> collectionMembership;
1292         collectionMembership = resource.getCollectionMembership();
1293         if (null == collectionMembership || collectionMembership.isEmpty()) {
1294             return collectionResources;
1295         }
1296
1297         if (collectionMembership.size() == collectionResources.size()) {
1298             return null;
1299         }
1300
1301         collectionResources.removeAll(collectionMembership);
1302
1303         // Sort the list
1304         Collections.sort(collectionResources,
1305                 Utility.collectionResourceComparator);
1306
1307         return collectionResources;
1308     }
1309
1310     public List<SingleResource> getSingleTypeResourcesForAddingToCollectionResource(
1311             CollectionResource colRes) {
1312         List<SingleResource> singleResources = data.getSingleResources();
1313         if (null == singleResources || singleResources.isEmpty()) {
1314             return null;
1315         }
1316
1317         Set<SingleResource> childs;
1318         childs = colRes.getSingleTypeChildResources();
1319         if (null == childs || childs.isEmpty()) {
1320             return singleResources;
1321         }
1322
1323         if (childs.size() == singleResources.size()) {
1324             return null;
1325         }
1326
1327         singleResources.removeAll(childs);
1328
1329         // Sort the list
1330         Collections.sort(singleResources, Utility.singleResourceComparator);
1331
1332         return singleResources;
1333     }
1334
1335     public List<SingleResource> getSingleTypeResourcesForAddingToDevice(
1336             Device dev) {
1337         List<SingleResource> singleResources = data.getSingleResources();
1338         if (null == singleResources || singleResources.isEmpty()) {
1339             return null;
1340         }
1341
1342         Set<SingleResource> childs;
1343         childs = dev.getSingleTypeChildResources();
1344         if (null == childs || childs.isEmpty()) {
1345             return singleResources;
1346         }
1347
1348         if (childs.size() == singleResources.size()) {
1349             return null;
1350         }
1351
1352         singleResources.removeAll(childs);
1353
1354         // Sort the list
1355         Collections.sort(singleResources, Utility.singleResourceComparator);
1356
1357         return singleResources;
1358     }
1359
1360     public List<CollectionResource> getCollectionTypeResourcesForAddingToCollectionResource(
1361             CollectionResource colRes) {
1362         List<CollectionResource> collectionResources = data
1363                 .getCollectionResources();
1364         if (null == collectionResources || collectionResources.isEmpty()) {
1365             return null;
1366         }
1367
1368         // Remove the colRes from the list
1369         collectionResources.remove(colRes);
1370
1371         Set<CollectionResource> childs;
1372         childs = colRes.getCollectionTypeChildResources();
1373         if (null == childs || childs.isEmpty()) {
1374             return collectionResources;
1375         }
1376
1377         if (childs.size() == collectionResources.size()) {
1378             return null;
1379         }
1380
1381         collectionResources.removeAll(childs);
1382
1383         // Sort the list
1384         Collections.sort(collectionResources,
1385                 Utility.collectionResourceComparator);
1386
1387         return collectionResources;
1388     }
1389
1390     public List<CollectionResource> getCollectionTypeResourcesForAddingToDevice(
1391             Device dev) {
1392         List<CollectionResource> collectionResources = data
1393                 .getCollectionResources();
1394         if (null == collectionResources || collectionResources.isEmpty()) {
1395             return null;
1396         }
1397
1398         Set<CollectionResource> childs;
1399         childs = dev.getCollectionTypeChildResources();
1400         if (null == childs || childs.isEmpty()) {
1401             return collectionResources;
1402         }
1403
1404         if (childs.size() == collectionResources.size()) {
1405             return null;
1406         }
1407
1408         collectionResources.removeAll(childs);
1409
1410         // Sort the list
1411         Collections.sort(collectionResources,
1412                 Utility.collectionResourceComparator);
1413
1414         return collectionResources;
1415     }
1416
1417     public List<Device> getDevicesForAddingToResource(Resource resource) {
1418         List<Device> devices = data.getDevices();
1419         if (null == devices || devices.isEmpty()) {
1420             return null;
1421         }
1422
1423         Set<Device> deviceMembership;
1424         if (resource instanceof SingleResource) {
1425             deviceMembership = ((SingleResource) resource)
1426                     .getDeviceMembership();
1427         } else {
1428             deviceMembership = ((CollectionResource) resource)
1429                     .getDeviceMembership();
1430         }
1431         if (null == deviceMembership || deviceMembership.isEmpty()) {
1432             return devices;
1433         }
1434
1435         if (devices.size() == deviceMembership.size()) {
1436             return null;
1437         }
1438
1439         devices.removeAll(deviceMembership);
1440
1441         // Sort the list
1442         Collections.sort(devices, Utility.deviceComparator);
1443
1444         return devices;
1445     }
1446
1447     public List<CollectionResource> getResourceReferences(
1448             SingleResource resource) {
1449         List<CollectionResource> resources = Utility
1450                 .getCollectionResourceListFromSet(resource
1451                         .getCollectionMembership());
1452         if (null == resources || resources.isEmpty()) {
1453             return null;
1454         }
1455
1456         Collections.sort(resources, Utility.collectionResourceComparator);
1457
1458         return resources;
1459     }
1460
1461     public List<Device> getDeviceReferences(Resource resource) {
1462         Set<Device> deviceMembership;
1463         if (resource instanceof SingleResource) {
1464             deviceMembership = ((SingleResource) resource)
1465                     .getDeviceMembership();
1466         } else {
1467             deviceMembership = ((CollectionResource) resource)
1468                     .getDeviceMembership();
1469         }
1470
1471         List<Device> devices = Utility.getDeviceListFromSet(deviceMembership);
1472         if (null == devices || devices.isEmpty()) {
1473             return null;
1474         }
1475
1476         Collections.sort(devices, Utility.deviceComparator);
1477
1478         return devices;
1479     }
1480
1481     public List<SingleResource> getSingleTypeChilds(CollectionResource colRes) {
1482         Set<SingleResource> childs = colRes.getSingleTypeChildResources();
1483         return Utility.getSingleResourceListFromSet(childs);
1484     }
1485
1486     public List<SingleResource> getSingleTypeChilds(Device dev) {
1487         Set<SingleResource> childs = dev.getSingleTypeChildResources();
1488         return Utility.getSingleResourceListFromSet(childs);
1489     }
1490
1491     public List<CollectionResource> getCollectionTypeChilds(
1492             CollectionResource colRes) {
1493         Set<CollectionResource> childs = colRes
1494                 .getCollectionTypeChildResources();
1495         return Utility.getCollectionResourceListFromSet(childs);
1496     }
1497
1498     public List<CollectionResource> getCollectionTypeChilds(Device dev) {
1499         Set<CollectionResource> childs = dev.getCollectionTypeChildResources();
1500         return Utility.getCollectionResourceListFromSet(childs);
1501     }
1502
1503     public void resourceSelectionChanged(final Resource selectedResource) {
1504         new Thread() {
1505             @Override
1506             public void run() {
1507
1508                 setCurrentDeviceInSelection(null);
1509
1510                 if (null != selectedResource) {
1511                     setCurrentResourceInSelection(selectedResource);
1512                 } else {
1513                     setCurrentResourceInSelection(null);
1514                 }
1515                 // Notify all observers for resource selection change event
1516                 UiListenerHandler.getInstance()
1517                         .resourceSelectionChangedUINotification(
1518                                 selectedResource);
1519             }
1520         }.start();
1521     }
1522
1523     public void deviceSelectionChanged(final Device selectedDevice) {
1524         new Thread() {
1525             @Override
1526             public void run() {
1527
1528                 setCurrentResourceInSelection(null);
1529
1530                 if (null != selectedDevice) {
1531                     setCurrentDeviceInSelection(selectedDevice);
1532                 } else {
1533                     setCurrentDeviceInSelection(null);
1534                 }
1535                 // Notify all observers for resource selection change event
1536                 UiListenerHandler.getInstance()
1537                         .deviceSelectionChangedUINotification(selectedDevice);
1538             }
1539         }.start();
1540     }
1541
1542     public List<MetaProperty> getMetaProperties(Resource resource) {
1543         if (null != resource) {
1544             String propName;
1545             String propValue;
1546
1547             List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
1548
1549             for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
1550                 propName = Constants.META_PROPERTIES[index];
1551                 if (propName.equals(Constants.RESOURCE_NAME)) {
1552                     propValue = resource.getResourceName();
1553                 } else if (propName.equals(Constants.RESOURCE_URI)) {
1554                     propValue = resource.getResourceURI();
1555                 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
1556                     Set<String> resTypes = resource.getResourceTypes();
1557                     if (null != resTypes && !resTypes.isEmpty()) {
1558                         propValue = "";
1559                         Iterator<String> itr = resTypes.iterator();
1560                         while (itr.hasNext()) {
1561                             propValue += itr.next();
1562                             if (itr.hasNext()) {
1563                                 propValue += ", ";
1564                             }
1565                         }
1566                     } else {
1567                         propValue = null;
1568                     }
1569                 } else {
1570                     propValue = null;
1571                 }
1572                 if (null != propValue) {
1573                     metaPropertyList.add(new MetaProperty(propName, propValue));
1574                 }
1575             }
1576             return metaPropertyList;
1577         }
1578         return null;
1579     }
1580
1581     public List<MetaProperty> getMetaProperties(Device dev) {
1582         if (null != dev) {
1583             List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
1584             metaPropertyList.add(new MetaProperty(Constants.DEVICE_NAME, dev
1585                     .getDeviceName()));
1586             return metaPropertyList;
1587         }
1588         return null;
1589     }
1590
1591     public boolean startResource(Resource resource) throws SimulatorException {
1592         if (null == resource) {
1593             return false;
1594         }
1595         SimulatorResource server = resource.getSimulatorResource();
1596         if (null == server) {
1597             return false;
1598         }
1599         try {
1600             server.start();
1601             resource.setStarted(true);
1602         } catch (SimulatorException e) {
1603             Activator
1604                     .getDefault()
1605                     .getLogManager()
1606                     .log(Level.ERROR.ordinal(),
1607                             new Date(),
1608                             "There is an error while starting the resource.\n"
1609                                     + Utility.getSimulatorErrorString(e, null));
1610             throw e;
1611         }
1612         return true;
1613     }
1614
1615     public boolean stopResource(Resource resource) throws SimulatorException {
1616         if (null == resource) {
1617             return false;
1618         }
1619         SimulatorResource server = resource.getSimulatorResource();
1620         if (null == server) {
1621             return false;
1622         }
1623         try {
1624             server.stop();
1625             resource.setStarted(false);
1626         } catch (SimulatorException e) {
1627             Activator
1628                     .getDefault()
1629                     .getLogManager()
1630                     .log(Level.ERROR.ordinal(),
1631                             new Date(),
1632                             "There is an error while stopping the resource.\n"
1633                                     + Utility.getSimulatorErrorString(e, null));
1634             throw e;
1635         }
1636         return true;
1637     }
1638
1639     public boolean changeResourceName(Resource resource, String newName)
1640             throws SimulatorException {
1641         if (null == resource || null == newName) {
1642             return false;
1643         }
1644
1645         if (!stopResource(resource)) {
1646             return false;
1647         }
1648
1649         SimulatorResource server = resource.getSimulatorResource();
1650         try {
1651             server.setName(newName);
1652             resource.setResourceName(newName);
1653         } catch (SimulatorException e) {
1654             Activator
1655                     .getDefault()
1656                     .getLogManager()
1657                     .log(Level.ERROR.ordinal(),
1658                             new Date(),
1659                             "There is an error while changing the resource name.\n"
1660                                     + Utility.getSimulatorErrorString(e, null));
1661             throw e;
1662         }
1663
1664         if (!startResource(resource)) {
1665             return false;
1666         }
1667
1668         return true;
1669     }
1670
1671     public boolean changeDeviceName(Device dev, String newName) {
1672         if (null == dev || null == newName) {
1673             return false;
1674         }
1675         data.changeDeviceName(dev, dev.getDeviceName(), newName);
1676         return true;
1677     }
1678
1679     public boolean changeResourceURI(Resource resource, String newURI)
1680             throws SimulatorException {
1681         if (null == resource || null == newURI) {
1682             return false;
1683         }
1684
1685         if (!stopResource(resource)) {
1686             return false;
1687         }
1688
1689         String curURI = resource.getResourceURI();
1690         setResourceURI(resource, newURI);
1691
1692         try {
1693             if (!startResource(resource)) {
1694                 return false;
1695             }
1696         } catch (SimulatorException e) {
1697             setResourceURI(resource, curURI);
1698         }
1699
1700         return true;
1701     }
1702
1703     public void setResourceURI(Resource resource, String newURI)
1704             throws SimulatorException {
1705         String curURI = resource.getResourceURI();
1706         SimulatorResource server = resource.getSimulatorResource();
1707         try {
1708             server.setURI(newURI);
1709             data.changeResourceURI(resource, curURI, newURI);
1710         } catch (SimulatorException e) {
1711             Activator
1712                     .getDefault()
1713                     .getLogManager()
1714                     .log(Level.ERROR.ordinal(),
1715                             new Date(),
1716                             "There is an error while changing the resource URI.\n"
1717                                     + Utility.getSimulatorErrorString(e, null));
1718             throw e;
1719         }
1720     }
1721
1722     public boolean updateResourceProperties(Resource resource,
1723             List<MetaProperty> properties, boolean uriChanged,
1724             boolean nameChanged) throws SimulatorException {
1725         if (null == resource || null == properties) {
1726             return false;
1727         }
1728
1729         // Updating the properties
1730         Iterator<MetaProperty> itr = properties.iterator();
1731         MetaProperty property;
1732         String propName;
1733         String propValue;
1734         String resName = null;
1735         String resURI = null;
1736         while (itr.hasNext()) {
1737             property = itr.next();
1738             if (null == property) {
1739                 continue;
1740             }
1741             propName = property.getPropName();
1742             propValue = property.getPropValue();
1743             if (propName.equals(Constants.RESOURCE_NAME)) {
1744                 resName = propValue;
1745             } else if (propName.equals(Constants.RESOURCE_URI)) {
1746                 resURI = propValue;
1747             }
1748         }
1749
1750         if (nameChanged) {
1751             if (!changeResourceName(resource, resName)) {
1752                 return false;
1753             }
1754
1755             // Notify UI Listeners
1756             UiListenerHandler.getInstance().propertiesChangedUINotification(
1757                     Resource.class);
1758         }
1759
1760         if (uriChanged) {
1761             if (!changeResourceURI(resource, resURI)) {
1762                 return false;
1763             }
1764         }
1765
1766         return true;
1767     }
1768
1769     public boolean updateDeviceProperties(Device dev,
1770             List<MetaProperty> properties) {
1771         if (null == dev || null == properties) {
1772             return false;
1773         }
1774
1775         // Updating the properties
1776         Iterator<MetaProperty> itr = properties.iterator();
1777         MetaProperty property;
1778         String propName;
1779         String propValue;
1780         String devName = null;
1781         while (itr.hasNext()) {
1782             property = itr.next();
1783             if (null == property) {
1784                 continue;
1785             }
1786             propName = property.getPropName();
1787             propValue = property.getPropValue();
1788             if (propName.equals(Constants.DEVICE_NAME)) {
1789                 devName = propValue;
1790             }
1791         }
1792
1793         if (!changeDeviceName(dev, devName)) {
1794             return false;
1795         }
1796
1797         // Notify UI Listeners
1798         UiListenerHandler.getInstance().propertiesChangedUINotification(
1799                 Device.class);
1800
1801         return true;
1802     }
1803
1804     public void attributeValueUpdated(SingleResource resource,
1805             String attributeName, AttributeValue value) {
1806         if (null != resource && null != attributeName && null != value) {
1807             SimulatorSingleResource simRes = (SimulatorSingleResource) resource
1808                     .getSimulatorResource();
1809             if (null != simRes) {
1810                 try {
1811                     simRes.updateAttribute(attributeName, value);
1812                 } catch (SimulatorException e) {
1813                     Activator
1814                             .getDefault()
1815                             .getLogManager()
1816                             .log(Level.ERROR.ordinal(), new Date(),
1817                                     Utility.getSimulatorErrorString(e, null));
1818                 }
1819             }
1820         }
1821     }
1822
1823     // TODO: This method should get the status from the native layer.
1824     public boolean isResourceStarted(Resource resource) {
1825         if (null == resource) {
1826             return false;
1827         }
1828         return resource.isStarted();
1829     }
1830
1831     public boolean isPropertyValueInvalid(Resource resource,
1832             List<MetaProperty> properties, String propName) {
1833         if (null == resource || null == properties || null == propName) {
1834             return false;
1835         }
1836         boolean invalid = false;
1837         MetaProperty prop;
1838         Iterator<MetaProperty> itr = properties.iterator();
1839         while (itr.hasNext()) {
1840             prop = itr.next();
1841             if (prop.getPropName().equals(propName)) {
1842                 String value = prop.getPropValue();
1843                 if (propName.equals(Constants.RESOURCE_URI)) {
1844                     if (!Utility.isUriValid(value)) {
1845                         invalid = true;
1846                     }
1847                 } else {
1848                     if (null == value || value.trim().isEmpty()) {
1849                         invalid = true;
1850                     }
1851                 }
1852             }
1853         }
1854         return invalid;
1855     }
1856
1857     public boolean isPropertyValueInvalid(Device dev,
1858             List<MetaProperty> properties, String propName) {
1859         if (null == dev || null == properties || null == propName) {
1860             return false;
1861         }
1862         boolean invalid = false;
1863         MetaProperty prop;
1864         Iterator<MetaProperty> itr = properties.iterator();
1865         while (itr.hasNext()) {
1866             prop = itr.next();
1867             if (prop.getPropName().equals(propName)) {
1868                 String value = prop.getPropValue();
1869                 if (null == value || value.trim().isEmpty()) {
1870                     invalid = true;
1871                 }
1872             }
1873         }
1874         return invalid;
1875     }
1876
1877     public boolean isPropValueChanged(Resource resource,
1878             List<MetaProperty> properties, String propName) {
1879         if (null == resource || null == properties || null == propName) {
1880             return false;
1881         }
1882         boolean changed = false;
1883         MetaProperty prop;
1884         String oldValue;
1885         Iterator<MetaProperty> itr = properties.iterator();
1886         while (itr.hasNext()) {
1887             prop = itr.next();
1888             if (prop.getPropName().equals(propName)) {
1889                 oldValue = getPropertyValueFromResource(resource, propName);
1890                 if (null != oldValue && !prop.getPropValue().equals(oldValue)) {
1891                     changed = true;
1892                 }
1893                 break;
1894             }
1895         }
1896         return changed;
1897     }
1898
1899     public boolean isPropValueChanged(Device dev,
1900             List<MetaProperty> properties, String propName) {
1901         if (null == dev || null == properties || null == propName) {
1902             return false;
1903         }
1904         boolean changed = false;
1905         MetaProperty prop;
1906         String oldValue;
1907         Iterator<MetaProperty> itr = properties.iterator();
1908         while (itr.hasNext()) {
1909             prop = itr.next();
1910             if (prop.getPropName().equals(propName)) {
1911                 oldValue = dev.getDeviceName();
1912                 if (null != oldValue && !prop.getPropValue().equals(oldValue)) {
1913                     changed = true;
1914                 }
1915                 break;
1916             }
1917         }
1918         return changed;
1919     }
1920
1921     private String getPropertyValueFromResource(Resource resource,
1922             String propName) {
1923         if (null == resource || null == propName) {
1924             return null;
1925         }
1926         if (propName.equals(Constants.RESOURCE_URI)) {
1927             return resource.getResourceURI();
1928         } else if (propName.equals(Constants.RESOURCE_NAME)) {
1929             return resource.getResourceName();
1930         } else if (propName.equals(Constants.RESOURCE_TYPE)) {
1931             return resource.getResourceTypes().toString();
1932         } else {
1933             return null;
1934         }
1935     }
1936
1937     public boolean isURIChanged(Resource resource, List<MetaProperty> properties) {
1938         if (null == resource || null == properties) {
1939             return false;
1940         }
1941         boolean changed = false;
1942         MetaProperty prop;
1943         Iterator<MetaProperty> itr = properties.iterator();
1944         while (itr.hasNext()) {
1945             prop = itr.next();
1946             if (prop.getPropName().equals(Constants.RESOURCE_URI)) {
1947                 if (!prop.getPropValue().equals(resource.getResourceURI())) {
1948                     changed = true;
1949                 }
1950                 break;
1951             }
1952         }
1953         return changed;
1954     }
1955
1956     public boolean startResource(SingleResource resource) {
1957         if (null == resource || resource.isStarted()) {
1958             return false;
1959         }
1960         boolean result;
1961         SimulatorResource server = resource.getSimulatorResource();
1962         if (null == server) {
1963             result = false;
1964         } else {
1965             try {
1966                 server.start();
1967                 resource.setStarted(true);
1968                 result = true;
1969             } catch (SimulatorException e) {
1970                 Activator
1971                         .getDefault()
1972                         .getLogManager()
1973                         .log(Level.ERROR.ordinal(), new Date(),
1974                                 Utility.getSimulatorErrorString(e, null));
1975                 result = false;
1976             }
1977         }
1978         return result;
1979     }
1980
1981     public boolean stopResource(SingleResource resource) {
1982         if (null == resource || !resource.isStarted()) {
1983             return false;
1984         }
1985         boolean result;
1986         SimulatorResource server = resource.getSimulatorResource();
1987         if (null == server) {
1988             result = false;
1989         } else {
1990             try {
1991                 server.stop();
1992                 resource.setStarted(false);
1993                 result = true;
1994             } catch (SimulatorException e) {
1995                 Activator
1996                         .getDefault()
1997                         .getLogManager()
1998                         .log(Level.ERROR.ordinal(), new Date(),
1999                                 Utility.getSimulatorErrorString(e, null));
2000                 result = false;
2001             }
2002         }
2003         return result;
2004     }
2005
2006     public boolean isAttHasRangeOrAllowedValues(SimulatorResourceAttribute att) {
2007         if (null == att) {
2008             return false;
2009         }
2010         AttributeProperty prop = att.property();
2011         if (null == prop) {
2012             return false;
2013         }
2014         Type attProp = prop.type();
2015         if (attProp == Type.UNKNOWN) {
2016             return false;
2017         }
2018         return true;
2019     }
2020
2021     public int startAutomation(SingleResource resource,
2022             AttributeElement attribute, AutoUpdateType autoType,
2023             int autoUpdateInterval) {
2024         int autoId = -1;
2025         if (null != resource && null != attribute) {
2026             SimulatorSingleResource server = (SimulatorSingleResource) resource
2027                     .getSimulatorResource();
2028             if (null != server) {
2029                 String attrName = attribute.getSimulatorResourceAttribute()
2030                         .name();
2031                 try {
2032                     autoId = server.startAttributeUpdation(attrName, autoType,
2033                             autoUpdateInterval, automationListener);
2034                 } catch (SimulatorException e) {
2035                     Activator
2036                             .getDefault()
2037                             .getLogManager()
2038                             .log(Level.ERROR.ordinal(),
2039                                     new Date(),
2040                                     "[" + e.getClass().getSimpleName() + "]"
2041                                             + e.code().toString() + "-"
2042                                             + e.message());
2043                     return -1;
2044                 }
2045                 if (-1 != autoId) {
2046                     attribute.setAutoUpdateId(autoId);
2047                     attribute.setAutoUpdateType(autoType);
2048                     attribute.setAutoUpdateInterval(autoUpdateInterval);
2049                     attribute.setAutoUpdateState(true);
2050                     resource.setAttributeAutomationInProgress(true);
2051                 }
2052             }
2053         }
2054         return autoId;
2055     }
2056
2057     public void stopAutomation(SingleResource resource, AttributeElement att,
2058             int autoId) {
2059         if (null != resource) {
2060             SimulatorSingleResource server = (SimulatorSingleResource) resource
2061                     .getSimulatorResource();
2062             if (null != server) {
2063                 try {
2064                     server.stopUpdation(autoId);
2065                 } catch (SimulatorException e) {
2066                     Activator
2067                             .getDefault()
2068                             .getLogManager()
2069                             .log(Level.ERROR.ordinal(),
2070                                     new Date(),
2071                                     "[" + e.getClass().getSimpleName() + "]"
2072                                             + e.code().toString() + "-"
2073                                             + e.message());
2074                     return;
2075                 }
2076                 // Change the automation status
2077                 att.setAutoUpdateState(false);
2078                 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
2079             }
2080         }
2081     }
2082
2083     public boolean startResourceAutomationUIRequest(AutoUpdateType autoType,
2084             int autoUpdateInterval, final SingleResource resource) {
2085         if (null == resource) {
2086             return false;
2087         }
2088         boolean status = false;
2089         changeResourceLevelAutomationStatus(resource, true);
2090         // Invoke the native automation method
2091         SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
2092                 .getSimulatorResource();
2093         if (null != resourceServer) {
2094             int autoId = -1;
2095             try {
2096                 autoId = resourceServer.startResourceUpdation(autoType,
2097                         autoUpdateInterval, automationListener);
2098             } catch (SimulatorException e) {
2099                 Activator
2100                         .getDefault()
2101                         .getLogManager()
2102                         .log(Level.ERROR.ordinal(), new Date(),
2103                                 Utility.getSimulatorErrorString(e, null));
2104                 autoId = -1;
2105             }
2106             if (-1 == autoId) {
2107                 // Automation request failed and hence status is being
2108                 // rolled back
2109                 changeResourceLevelAutomationStatus(resource, false);
2110             } else {
2111                 // Automation request accepted.
2112                 resource.setAutomationId(autoId);
2113
2114                 // Notify the UI listeners in a different thread.
2115                 Thread notifyThread = new Thread() {
2116                     public void run() {
2117                         UiListenerHandler.getInstance()
2118                                 .resourceAutomationStartedUINotification(
2119                                         resource);
2120                     };
2121                 };
2122                 notifyThread.setPriority(Thread.MAX_PRIORITY);
2123                 notifyThread.start();
2124
2125                 status = true;
2126             }
2127         }
2128         return status;
2129     }
2130
2131     public boolean stopResourceAutomationUIRequest(final SingleResource resource) {
2132         if (null == resource) {
2133             return false;
2134         }
2135         final int autoId = resource.getAutomationId();
2136         if (-1 == autoId) {
2137             return false;
2138         }
2139         SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
2140                 .getSimulatorResource();
2141         if (null == resourceServer) {
2142             return false;
2143         }
2144         // Call native method
2145         try {
2146             resourceServer.stopUpdation(autoId);
2147         } catch (SimulatorException e) {
2148             Activator
2149                     .getDefault()
2150                     .getLogManager()
2151                     .log(Level.ERROR.ordinal(), new Date(),
2152                             Utility.getSimulatorErrorString(e, null));
2153             return false;
2154         }
2155
2156         // Notify the UI Listeners. Invoke the automation complete callback.
2157         Thread stopThread = new Thread() {
2158             public void run() {
2159                 automationListener.onUpdateComplete(resource.getResourceURI(),
2160                         autoId);
2161             }
2162         };
2163         stopThread.start();
2164         return true;
2165     }
2166
2167     private boolean isAnyAttributeInAutomation(SingleResource resource) {
2168         if (null == resource || null == resource.getResourceRepresentation()) {
2169             return false;
2170         }
2171
2172         Map<String, AttributeElement> attributes = resource
2173                 .getResourceRepresentation().getAttributes();
2174         if (null == attributes || 0 == attributes.size())
2175             return false;
2176
2177         for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
2178             if (entry.getValue().isAutoUpdateInProgress())
2179                 return true;
2180         }
2181
2182         return false;
2183     }
2184
2185     // Changes the automation state of the resource and its attributes
2186     private void changeResourceLevelAutomationStatus(SingleResource resource,
2187             boolean status) {
2188
2189         if (null == resource || null == resource.getResourceRepresentation()) {
2190             return;
2191         }
2192
2193         Map<String, AttributeElement> attributes = resource
2194                 .getResourceRepresentation().getAttributes();
2195         if (null == attributes || 0 == attributes.size())
2196             return;
2197
2198         for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
2199             entry.getValue().setAutoUpdateState(status);
2200         }
2201
2202         resource.setResourceAutomationInProgress(status);
2203     }
2204
2205     private AttributeElement getAttributeWithGivenAutomationId(
2206             SingleResource resource, int automationId) {
2207         if (null == resource || null == resource.getResourceRepresentation()) {
2208             return null;
2209         }
2210
2211         Map<String, AttributeElement> attributes = resource
2212                 .getResourceRepresentation().getAttributes();
2213         if (null == attributes || 0 == attributes.size())
2214             return null;
2215
2216         for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
2217             if (automationId == entry.getValue().getAutoUpdateId())
2218                 return entry.getValue();
2219         }
2220
2221         return null;
2222     }
2223
2224     public boolean isResourceAutomationStarted(SingleResource resource) {
2225         boolean status = false;
2226         if (null != resource) {
2227             status = resource.isResourceAutomationInProgress();
2228         }
2229         return status;
2230     }
2231
2232     public boolean isAttributeAutomationStarted(SingleResource resource) {
2233         if (null == resource) {
2234             return false;
2235         }
2236         return resource.isAttributeAutomationInProgress();
2237     }
2238
2239     public void notifyObserverRequest(Resource resource, int observerId) {
2240         if (null == resource) {
2241             return;
2242         }
2243         SimulatorResource simulatorResource = resource.getSimulatorResource();
2244         if (null == simulatorResource) {
2245             return;
2246         }
2247         try {
2248             simulatorResource.notifyObserver(observerId);
2249         } catch (SimulatorException e) {
2250             Activator
2251                     .getDefault()
2252                     .getLogManager()
2253                     .log(Level.ERROR.ordinal(), new Date(),
2254                             Utility.getSimulatorErrorString(e, null));
2255         }
2256     }
2257
2258     public void shutdown() {
2259         threadHandle.interrupt();
2260     }
2261
2262     public List<String> getAllValuesOfAttribute(SimulatorResourceAttribute att) {
2263         if (null == att) {
2264             return null;
2265         }
2266
2267         AttributeValue val = att.value();
2268         if (null == val) {
2269             return null;
2270         }
2271
2272         TypeInfo type = val.typeInfo();
2273
2274         AttributeProperty prop = att.property();
2275         if (null == prop) {
2276             return null;
2277         }
2278
2279         List<String> values = new ArrayList<String>();
2280
2281         Type valuesType = prop.type();
2282
2283         if (valuesType == Type.UNKNOWN) {
2284             // Adding the default value
2285             values.add(Utility.getAttributeValueAsString(val));
2286             return values;
2287         }
2288
2289         if (type.mType != ValueType.RESOURCEMODEL) {
2290             if (type.mType == ValueType.ARRAY) {
2291                 if (type.mDepth == 1) {
2292                     AttributeProperty childProp = prop.getChildProperty();
2293                     if (null != childProp) {
2294                         valuesType = childProp.type();
2295                         if (valuesType == Type.RANGE) {
2296                             List<String> list = getRangeForPrimitiveNonArrayAttributes(
2297                                     childProp, type.mBaseType);
2298                             if (null != list) {
2299                                 values.addAll(list);
2300                             }
2301                         } else if (valuesType == Type.VALUESET) {
2302                             List<String> list = getAllowedValuesForPrimitiveNonArrayAttributes(
2303                                     childProp.valueSet(), type.mBaseType);
2304                             if (null != list) {
2305                                 values.addAll(list);
2306                             }
2307                         }
2308                     }
2309                 }
2310             } else {
2311                 if (valuesType == Type.RANGE) {
2312                     List<String> list = getRangeForPrimitiveNonArrayAttributes(
2313                             prop, type.mType);
2314                     if (null != list) {
2315                         values.addAll(list);
2316                     }
2317                 } else if (valuesType == Type.VALUESET) {
2318                     List<String> list = getAllowedValuesForPrimitiveNonArrayAttributes(
2319                             prop.valueSet(), type.mType);
2320                     if (null != list) {
2321                         values.addAll(list);
2322                     }
2323                 }
2324             }
2325         }
2326
2327         return values;
2328     }
2329
2330     public List<String> getRangeForPrimitiveNonArrayAttributes(
2331             AttributeProperty prop, ValueType type) {
2332         if (null == prop) {
2333             return null;
2334         }
2335
2336         if (type == ValueType.ARRAY || type == ValueType.RESOURCEMODEL) {
2337             return null;
2338         }
2339
2340         List<String> values = new ArrayList<String>();
2341         switch (type) {
2342             case INTEGER:
2343                 int min = (int) prop.min();
2344                 int max = (int) prop.max();
2345                 for (int iVal = min; iVal <= max; iVal++) {
2346                     values.add(String.valueOf(iVal));
2347                 }
2348                 break;
2349             case DOUBLE:
2350                 double minD = (double) prop.min();
2351                 double maxD = (double) prop.max();
2352                 for (double iVal = minD; iVal <= maxD; iVal = iVal + 1.0) {
2353                     values.add(String.valueOf(iVal));
2354                 }
2355                 break;
2356             default:
2357         }
2358         return values;
2359     }
2360
2361     public List<String> getAllowedValuesForPrimitiveNonArrayAttributes(
2362             AttributeValue[] attValues, ValueType type) {
2363         if (null == attValues || attValues.length < 1) {
2364             return null;
2365         }
2366
2367         if (type == ValueType.ARRAY || type == ValueType.RESOURCEMODEL) {
2368             return null;
2369         }
2370
2371         Object obj;
2372         List<String> values = new ArrayList<String>();
2373         for (AttributeValue val : attValues) {
2374             if (null == val) {
2375                 continue;
2376             }
2377             obj = val.get();
2378             if (null == obj) {
2379                 continue;
2380             }
2381             values.add(String.valueOf(obj));
2382         }
2383         return values;
2384     }
2385 }