2 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package oic.simulator.serviceprovider.view.dialogs;
19 import org.eclipse.core.runtime.FileLocator;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.jface.dialogs.MessageDialog;
24 import org.eclipse.jface.operation.IRunnableWithProgress;
25 import org.eclipse.jface.resource.ImageDescriptor;
26 import org.eclipse.jface.wizard.IWizardPage;
27 import org.eclipse.jface.wizard.Wizard;
28 import org.eclipse.jface.wizard.WizardDialog;
29 import org.eclipse.swt.widgets.Display;
31 import java.io.FileInputStream;
32 import java.lang.reflect.InvocationTargetException;
34 import java.util.Date;
35 import java.util.HashMap;
36 import java.util.Iterator;
40 import org.oic.simulator.ILogger.Level;
41 import org.oic.simulator.SimulatorException;
42 import org.oic.simulator.SimulatorResourceAttribute;
44 import oic.simulator.serviceprovider.Activator;
45 import oic.simulator.serviceprovider.manager.UiListenerHandler;
46 import oic.simulator.serviceprovider.model.AttributeHelper;
47 import oic.simulator.serviceprovider.model.Resource;
48 import oic.simulator.serviceprovider.model.ResourceType;
49 import oic.simulator.serviceprovider.model.SingleResource;
50 import oic.simulator.serviceprovider.utils.Constants;
51 import oic.simulator.serviceprovider.utils.Utility;
52 import oic.simulator.serviceprovider.view.dialogs.MainPage.Option;
55 * This class creates a UI wizard for create resource operation.
57 public class CreateResourceWizard extends Wizard {
59 private MainPage mainPage;
60 private SimpleResourceBasicDetailsPage simpleResourceBasicDetailsPage;
61 private SimpleResourceAddAttributePage simpleResourceAddAttributePage;
62 private SimpleResourceOtherDetailsPage simpleResourceOtherDetailsPage;
63 private LoadRamlPage loadRamlPage;
64 private UpdatePropertiesPage updatePropPage;
66 private String status;
68 private WizardDialog wizDialog;
69 private boolean dlgForceClosed;
71 private Resource createdResource;
73 private IProgressMonitor progressMonitor;
75 public CreateResourceWizard() {
76 setWindowTitle("Create resources");
77 IPath path = new Path("/icons/oic_logo_64x64.png");
78 URL find = FileLocator.find(Activator.getDefault().getBundle(), path,
80 setDefaultPageImageDescriptor(ImageDescriptor.createFromURL(find));
82 setNeedsProgressMonitor(true);
86 public void addPages() {
87 mainPage = new MainPage();
88 simpleResourceBasicDetailsPage = new SimpleResourceBasicDetailsPage();
89 simpleResourceAddAttributePage = new SimpleResourceAddAttributePage();
90 simpleResourceOtherDetailsPage = new SimpleResourceOtherDetailsPage();
91 loadRamlPage = new LoadRamlPage();
92 updatePropPage = new UpdatePropertiesPage();
95 addPage(simpleResourceBasicDetailsPage);
96 addPage(simpleResourceAddAttributePage);
97 addPage(simpleResourceOtherDetailsPage);
98 addPage(loadRamlPage);
99 addPage(updatePropPage);
102 public void setWizardDialog(WizardDialog dlg) {
106 public WizardDialog getWizardDialog() {
110 public boolean isDlgForceClosed() {
111 return dlgForceClosed;
114 public void setDlgForceClosed(boolean dlgForceClosed) {
115 this.dlgForceClosed = dlgForceClosed;
118 public String getConfigFilePath() {
119 if (null == loadRamlPage) {
122 return loadRamlPage.getConfigFilePath();
125 public int getResourceCount() {
126 if (null == loadRamlPage) {
129 return loadRamlPage.getResourceCount();
132 public Resource getCreatedResource() {
133 return createdResource;
137 public boolean canFinish() {
138 IWizardPage curPage = this.getContainer().getCurrentPage();
139 if ((curPage == updatePropPage && ((mainPage.getOption() == Option.SIMPLE_FROM_RAML) || !Activator
140 .getDefault().getResourceManager().isAnyResourceExist()))
141 || curPage == simpleResourceAddAttributePage
142 || (curPage == loadRamlPage && loadRamlPage.isSelectionDone() && loadRamlPage
143 .isMultiResourceCreation())) {
150 public boolean performFinish() {
151 final IWizardPage curPage = this.getContainer().getCurrentPage();
152 // Handling Simple Resource Creation without RAML
153 if (curPage == simpleResourceAddAttributePage) {
155 getContainer().run(true, false, new IRunnableWithProgress() {
158 public void run(IProgressMonitor monitor)
159 throws InvocationTargetException,
160 InterruptedException {
163 "Single Resource Creation Without RAML", 2);
165 createSingleResourceWithoutRAML();
171 } catch (InvocationTargetException e) {
172 Activator.getDefault().getLogManager()
173 .log(Level.ERROR.ordinal(), new Date(), e.getMessage());
175 } catch (InterruptedException e) {
176 Activator.getDefault().getLogManager()
177 .log(Level.ERROR.ordinal(), new Date(), e.getMessage());
180 } else if (curPage == loadRamlPage) {
181 // Validate the file path.
183 new FileInputStream(loadRamlPage.getConfigFilePath());
184 } catch (Exception e) {
186 .openError(getShell(), "Invalid File",
187 "File doesn't exist. Either the file path or file name is invalid.");
191 // Handling multiple instance creation of simple resource with RAML
192 if ((loadRamlPage.getResourceCount() + Activator.getDefault()
193 .getResourceManager().getResourceCount()) > Constants.MAX_RESOURCE_COUNT) {
195 .openInformation(Display.getDefault().getActiveShell(),
196 "Resource limit exceeded",
197 "Exceeded the limit of resources that can exist in the server.");
201 final int[] resCreatedCount = new int[1];
203 getContainer().run(true, true, new IRunnableWithProgress() {
206 public void run(final IProgressMonitor monitor)
207 throws InvocationTargetException,
208 InterruptedException {
209 progressMonitor = monitor;
212 "Single Resource Creation(multi-instance) With RAML",
213 loadRamlPage.getResourceCount());
214 resCreatedCount[0] = createMultiInstanceSingleResourceWithoutRAML();
221 } catch (InvocationTargetException e) {
222 Activator.getDefault().getLogManager()
223 .log(Level.ERROR.ordinal(), new Date(), e.getMessage());
225 } catch (InterruptedException e) {
226 Activator.getDefault().getLogManager()
227 .log(Level.ERROR.ordinal(), new Date(), e.getMessage());
230 boolean canceled = false;
231 if (null != progressMonitor && progressMonitor.isCanceled()
232 && 0 == resCreatedCount[0]) {
235 progressMonitor = null;
239 if (resCreatedCount[0] > 0) {
240 UiListenerHandler.getInstance()
241 .resourceCreatedUINotification(ResourceType.SINGLE);
244 } else if (curPage == updatePropPage) {
245 // Handling the single instance
246 String resName = updatePropPage.getResName();
247 String resURI = updatePropPage.getResURI();
248 if (null == resName || resName.trim().length() < 1) {
249 MessageDialog.openError(Display.getDefault().getActiveShell(),
250 "Invalid Resource Name.", "Resource name is invalid");
253 if (!Utility.isUriValid(resURI)) {
254 MessageDialog.openError(Display.getDefault().getActiveShell(),
255 "Invalid Resource URI.", Constants.INVALID_URI_MESSAGE);
258 // Creation of simple resource with RAML
259 // Checking whether the URI is used by any other resource.
260 if (Activator.getDefault().getResourceManager()
261 .isResourceExist(updatePropPage.getResURI())) {
263 .openError(getShell(), "Resource URI in use",
264 "Entered resource URI is in use. Please try a different one.");
269 getContainer().run(true, false, new IRunnableWithProgress() {
272 public void run(IProgressMonitor monitor)
273 throws InvocationTargetException,
274 InterruptedException {
276 monitor.beginTask("Completing Resource Creation", 2);
278 completeResourceCreationWithRAML();
285 } catch (InvocationTargetException e) {
286 Activator.getDefault().getLogManager()
287 .log(Level.ERROR.ordinal(), new Date(), e.getMessage());
289 } catch (InterruptedException e) {
290 Activator.getDefault().getLogManager()
291 .log(Level.ERROR.ordinal(), new Date(), e.getMessage());
298 public MainPage getMainPage() {
302 public SimpleResourceBasicDetailsPage getSimpleResourceBasicDetailsPage() {
303 return simpleResourceBasicDetailsPage;
306 public SimpleResourceAddAttributePage getSimpleResourceAddAttributePage() {
307 return simpleResourceAddAttributePage;
310 public SimpleResourceOtherDetailsPage getSimpleResourceOtherDetailsPage() {
311 return simpleResourceOtherDetailsPage;
314 public LoadRamlPage getLoadRamlPage() {
318 public UpdatePropertiesPage getUpdatePropPage() {
319 return updatePropPage;
322 public void setStatus(String status) {
323 this.status = status;
326 public String getStatus() {
330 private void createSingleResourceWithoutRAML() {
331 SingleResource resource = new SingleResource();
332 // Basic resource details
333 resource.setResourceURI(simpleResourceBasicDetailsPage.getResURI());
334 resource.setResourceName(simpleResourceBasicDetailsPage.getResName());
335 resource.setResourceType(simpleResourceBasicDetailsPage.getResType());
336 resource.setObservable(simpleResourceBasicDetailsPage.isObservable());
337 resource.setDiscoverable(simpleResourceBasicDetailsPage
339 resource.setResourceInterfaces(simpleResourceBasicDetailsPage
340 .getInterfaceTypes());
342 // Resource Attributes
343 Map<String, SimulatorResourceAttribute> attributes = new HashMap<String, SimulatorResourceAttribute>();
344 Set<AttributeHelper> attributeSet = simpleResourceAddAttributePage
346 if (null != attributeSet && !attributeSet.isEmpty()) {
347 Iterator<AttributeHelper> itr = attributeSet.iterator();
348 AttributeHelper attHelper;
349 SimulatorResourceAttribute attribute;
350 while (itr.hasNext()) {
351 attHelper = itr.next();
352 if (null != attHelper) {
353 attribute = attHelper.convertToSimulatorResourceAttribute();
354 attributes.put(attribute.name(), attribute);
360 resource.setGetAllowed(simpleResourceOtherDetailsPage.isGetChecked());
361 resource.setPutAllowed(simpleResourceOtherDetailsPage.isPutChecked());
362 resource.setPostAllowed(simpleResourceOtherDetailsPage.isPostChecked());
364 // Call method of ResourceManager and update the response in the status
367 boolean result = Activator.getDefault().getResourceManager()
368 .createSingleResource(resource, attributes);
370 status = "Resource created.";
371 createdResource = resource;
373 status = "Failed to create resource.";
374 createdResource = null;
376 } catch (Exception e) {
377 status = "Failed to create resource.\n"
378 + Utility.getSimulatorErrorString(e, null);
379 createdResource = null;
383 private void completeResourceCreationWithRAML() {
385 boolean result = false;
386 Resource res = loadRamlPage.getResource();
387 if (res instanceof SingleResource) {
390 .getResourceManager()
391 .completeSingleResourceCreationByRAML(res,
392 updatePropPage.getResURI(),
393 updatePropPage.getResName(), false);
397 status = "Resource created.";
398 createdResource = res;
400 status = "Failed to create resource.";
401 createdResource = null;
403 } catch (Exception e) {
404 status = "Failed to create resource.\n"
405 + Utility.getSimulatorErrorString(e, null);
406 createdResource = null;
410 private int createMultiInstanceSingleResourceWithoutRAML() {
411 int toCreateCount = loadRamlPage.getResourceCount();
412 int resCreatedCount = 0;
413 Set<SingleResource> resources;
415 resources = Activator
417 .getResourceManager()
418 .createSingleResourceMultiInstances(
419 loadRamlPage.getConfigFilePath(), toCreateCount,
421 if (null != progressMonitor && progressMonitor.isCanceled()) {
423 Activator.getDefault().getResourceManager()
424 .removeSingleResources(resources);
425 } catch (SimulatorException e) {
429 if (null != resources)
430 resCreatedCount = resources.size();
431 if (resCreatedCount > 0) {
432 status = "[" + resCreatedCount + " out of " + toCreateCount
434 status += ((resCreatedCount == 1) ? "resource" : "resources")
435 + " created successfully.";
437 status = "Failed to create resources.";
439 } catch (SimulatorException e) {
440 status = "Failed to create resource.\n"
441 + Utility.getSimulatorErrorString(e, null);
443 return resCreatedCount;