/*
 * Perfclispe
 * 
 * 
 * Copyright (c) 2013 Jakub Knetl
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.perfclipse.ui.wizards;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
import org.perfcake.model.ObjectFactory;
import org.perfcake.model.Scenario;
import org.perfclipse.scenario.ScenarioException;
import org.perfclipse.scenario.ScenarioManager;
import org.perfclipse.ui.editors.ScenarioDesignEditorInput;
import org.perfclipse.ui.wizards.pages.GeneratorPage;
import org.perfclipse.ui.wizards.pages.ScenarioNewFilePage;
import org.perfclipse.ui.wizards.pages.SenderPage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ScenarioWizard extends Wizard implements INewWizard {

	private GeneratorPage generatorAndSenderPage;
	private WizardNewFileCreationPage fileCreationPage;
	IStructuredSelection selection;
	
	static final Logger log = LoggerFactory.getLogger(ScenarioWizard.class);
	private SenderPage senderPage;

	public ScenarioWizard() {
		super();
	}

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.selection = selection;
		

	}

	@Override
	public boolean performFinish() {
		ObjectFactory scenarioFactory = new ObjectFactory();
		Scenario scenario = scenarioFactory.createScenario();

		//Generator section
		Scenario.Generator generator = scenarioFactory.createScenarioGenerator();
		Scenario.Generator.Run run = scenarioFactory.createScenarioGeneratorRun();
		run.setType("time");
		run.setValue("5000");
		generator.setClazz(generatorAndSenderPage.getGeneratorName());
		generator.setRun(run);
		generator.setThreads("1");
		scenario.setGenerator(generator);

		//Sender section
		Scenario.Sender sender = scenarioFactory.createScenarioSender();
		sender.setClazz(senderPage.getSenderName());
		scenario.setSender(sender);
		
		
		try {
				IFile scenarioFile = fileCreationPage.createNewFile();;
				
				ScenarioManager manager = new ScenarioManager();
				PipedOutputStream out = new PipedOutputStream();
				PipedInputStream in = new PipedInputStream(out);
				manager.createXML(scenario, out);
				out.close();
				scenarioFile.setContents(in, false, true, null);
				
				//open scenario editor
				String scenarioEditorID = "org.perfclipse.ui.editors.scenario";
				IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				//TODO : editor cannot be initialized after Eclipse reload
				page.openEditor(new ScenarioDesignEditorInput(scenarioFile), scenarioEditorID);
				return true;
		} catch (CoreException e) {
			MessageDialog.openError(getShell(), "Core exception", "Cannot create or open file with scenario." );
			log.error("Cannot create or open file with scenario.", e);
		} catch (IOException e) {
			MessageDialog.openError(getShell(), "IO Exception", "Cannot create or read resource");
			log.error("Cannot create or read resource", e);
		} catch (ScenarioException e) {
			MessageDialog.openError(getShell(), "XML conversion error", "Cannot convert given data into XML.");
			log.error("Cannot convert given data into XML.", e);
		}
		
		return false;
	}
	
	@Override
	public void addPages(){
		fileCreationPage = new ScenarioNewFilePage("New Scenario file", selection);
		generatorAndSenderPage = new GeneratorPage();
		senderPage = new SenderPage();

		addPage(fileCreationPage);
		addPage(generatorAndSenderPage);
		addPage(senderPage);
	}

	@Override
	public boolean canFinish() {
		return super.canFinish();
	}
	
	

}

@@ -666,27 +666,36 @@
 java.io.
-IOException
+ByteArrayInputStream
 ;%0Aimport
@@ -703,23 +703,28 @@
 java.io.
-PipedIn
+ByteArrayOut
 putStrea
@@ -741,33 +741,27 @@
 java.io.
-PipedOutputStream
+IOException
 ;%0A%0Aimpor
@@ -2831,17 +2831,16 @@
 ry %7B%0A%09%09%09
-%09
 IFile sc
@@ -2894,125 +2894,167 @@
 %0A%09%09%09
-%09%0A%09%09%09%09ScenarioManager manager = new ScenarioManager();%0A%09%09%09%09PipedOutputStream out = new PipedOutputStream();%0A%09%09%09%09Piped
+ByteArrayOutputStream out = new ByteArrayOutputStream();%0A%0A%09%09%09ScenarioManager manager = new ScenarioManager();%0A%09%09%09manager.createXML(scenario, out);%0A%09%09%09ByteArray
 Inpu
@@ -3074,13 +3074,17 @@
 new 
-Piped
+ByteArray
 Inpu
@@ -3098,50 +3098,25 @@
 (out
-);%0A%09%09%09%09manager.createXML(scenario, out
+.toByteArray()
 );%0A
-%09
 %09%09%09o
@@ -3127,17 +3127,16 @@
 lose();%0A
-%09
 %09%09%09scena
@@ -3183,17 +3183,12 @@
 l);%0A
-%09%09%09%09
 %0A%09%09%09
-%09
 //op
@@ -3206,17 +3206,16 @@
  editor%0A
-%09
 %09%09%09Strin
@@ -3273,17 +3273,16 @@
 nario%22;%0A
-%09
 %09%09%09IWork
@@ -3371,17 +3371,16 @@
 e();%0A%09%09%09
-%09
 //TODO :
@@ -3430,17 +3430,16 @@
  reload%0A
-%09
 %09%09%09page.
@@ -3510,20 +3510,25 @@
 EditorID
+, true
 );%0A
-%09
 %09%09%09retur

Fix JAXB create scenario