001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013: package org.pentaho.designstudio.editors.actionsequence.pages;
014:
015: import java.io.IOException;
016:
017: import org.apache.commons.httpclient.HttpClient;
018: import org.apache.commons.httpclient.HttpException;
019: import org.apache.commons.httpclient.HttpStatus;
020: import org.apache.commons.httpclient.NameValuePair;
021: import org.apache.commons.httpclient.methods.PostMethod;
022: import org.dom4j.Document;
023: import org.dom4j.Element;
024: import org.dom4j.dom.DOMDocumentFactory;
025: import org.eclipse.core.resources.IFile;
026: import org.eclipse.core.runtime.Platform;
027: import org.eclipse.jface.dialogs.MessageDialog;
028: import org.eclipse.swt.SWT;
029: import org.eclipse.swt.browser.Browser;
030: import org.eclipse.swt.events.SelectionEvent;
031: import org.eclipse.swt.events.SelectionListener;
032: import org.eclipse.swt.layout.FillLayout;
033: import org.eclipse.swt.layout.GridData;
034: import org.eclipse.swt.layout.GridLayout;
035: import org.eclipse.swt.widgets.Button;
036: import org.eclipse.swt.widgets.Composite;
037: import org.eclipse.swt.widgets.Label;
038: import org.eclipse.swt.widgets.Text;
039: import org.eclipse.ui.IEditorInput;
040: import org.eclipse.ui.forms.IManagedForm;
041: import org.eclipse.ui.forms.editor.FormEditor;
042: import org.eclipse.ui.forms.editor.FormPage;
043: import org.eclipse.ui.part.FileEditorInput;
044: import org.osgi.service.prefs.BackingStoreException;
045: import org.osgi.service.prefs.Preferences;
046: import org.pentaho.designstudio.controls.WidgetFactory;
047: import org.pentaho.designstudio.editors.actionsequence.ActionSequenceDocumentProvider;
048: import org.pentaho.designstudio.messages.Messages;
049: import org.pentaho.designstudio.util.SolutionHelper;
050:
051: /**
052: * Page used to run an action sequence and show the results in a browser window.
053: *
054: * @author Doug Moran
055: *
056: */
057: public class RunPage extends FormPage {
058: protected ActionSequenceDocumentProvider actSeqDocProvider;
059: Browser browser = null;
060: SolutionHelper solutionHelper = null;
061: Text serverTextBox = null;
062: Text urlTextBox = null;
063:
064: /**
065: * Creates a run page.
066: * @param editor the form editor of which this page is a part.
067: * @param actSeqDocProvider the provider of the XML document.
068: */
069: public RunPage(FormEditor editor,
070: ActionSequenceDocumentProvider actSeqDocProvider) {
071: super (
072: editor,
073: "RunPage", Messages.getString("RunPage.UI_TEST_TAB_LABEL")); //$NON-NLS-1$ //$NON-NLS-2$
074: this .actSeqDocProvider = actSeqDocProvider;
075: }
076:
077: private static void printPreferences(Preferences prefNode) {
078: try {
079: String[] keys = prefNode.keys();
080: for (int i = 0; i < keys.length; i++) {
081: if (keys[i].indexOf("pentaho") >= 0) {
082: int x = 1;
083: }
084: }
085: String[] childNames = prefNode.childrenNames();
086: for (int i = 0; i < childNames.length; i++) {
087: Preferences child = prefNode.node(childNames[i]);
088: printPreferences(child);
089: }
090: } catch (BackingStoreException e) {
091: // TODO Auto-generated catch block
092: e.printStackTrace();
093: }
094: }
095:
096: /* (non-Javadoc)
097: * @see org.eclipse.ui.forms.editor.FormPage#createFormContent(org.eclipse.ui.forms.IManagedForm)
098: */
099: protected void createFormContent(IManagedForm managedForm) {
100: managedForm.getForm().getBody().setLayout(new FillLayout());
101:
102: Composite formBody = WidgetFactory.createComposite(managedForm
103: .getForm().getBody());
104: int formCols = 6;
105: formBody.setLayout(new GridLayout(formCols, false));
106:
107: Label l = WidgetFactory.createLabel(formBody, Messages
108: .getString("RunPage.UI_SERVER_URL_LABEL")); //$NON-NLS-1$
109: l.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
110: false));
111:
112: serverTextBox = WidgetFactory.createText(formBody,
113: getServerUrl());
114: serverTextBox.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER,
115: false, false));
116:
117: Button b = WidgetFactory.createButton(formBody, Messages
118: .getString("RunPage.UI_TEST_SERVER_LABEL"), SWT.PUSH); //$NON-NLS-1$
119: b.setToolTipText(Messages
120: .getString("RunPage.UI_TEST_SERVER_BTN_TOOLTIP")); //$NON-NLS-1$
121: b.setLayoutData(new GridData());
122: b.addSelectionListener(new SelectionListener() {
123:
124: public void widgetSelected(SelectionEvent e) {
125: doTest();
126: }
127:
128: public void widgetDefaultSelected(SelectionEvent e) {
129: doTest();
130: }
131:
132: });
133:
134: b = WidgetFactory.createButton(formBody, Messages
135: .getString("RunPage.GENERATE_URL_BTN"), SWT.PUSH); //$NON-NLS-1$
136: b.setToolTipText(Messages
137: .getString("RunPage.UI_GENERATE_URL_BTN_TOOLTIP")); //$NON-NLS-1$
138: b.setLayoutData(new GridData());
139: b.addSelectionListener(new SelectionListener() {
140:
141: public void widgetSelected(SelectionEvent e) {
142: doGenerateURL();
143: }
144:
145: public void widgetDefaultSelected(SelectionEvent e) {
146: doGenerateURL();
147: }
148:
149: });
150:
151: b = WidgetFactory.createButton(formBody, Messages
152: .getString("RunPage.UI_BROWSER_URL_BTN"), SWT.PUSH); //$NON-NLS-1$
153: b.setToolTipText(Messages
154: .getString("RunPage.UI_BROWSER_URL_BTN_TOOLTIP")); //$NON-NLS-1$
155: b.setLayoutData(new GridData());
156: b.addSelectionListener(new SelectionListener() {
157:
158: public void widgetSelected(SelectionEvent e) {
159: doGetBrowserURL();
160: }
161:
162: public void widgetDefaultSelected(SelectionEvent e) {
163: doGetBrowserURL();
164: }
165:
166: });
167:
168: WidgetFactory
169: .createLabel(formBody, "").setLayoutData(new GridData()); //$NON-NLS-1$
170:
171: l = WidgetFactory.createLabel(formBody, Messages
172: .getString("RunPage.UI_GENERATED_URL_LABEL")); //$NON-NLS-1$
173: l.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
174: false, 1, 1));
175:
176: urlTextBox = WidgetFactory.createText(formBody, ""); //$NON-NLS-1$
177: urlTextBox.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
178: true, false, formCols - 2, 1));
179:
180: b = WidgetFactory.createButton(formBody, Messages
181: .getString("RunPage.UI_RUN_BTN_LABEL"), SWT.PUSH); //$NON-NLS-1$
182: b.setToolTipText(Messages
183: .getString("RunPage.UI_RUN_URL_BTN_TOOLTIP")); //$NON-NLS-1$
184: b.setLayoutData(new GridData());
185: b.addSelectionListener(new SelectionListener() {
186:
187: public void widgetSelected(SelectionEvent e) {
188: doRun();
189: }
190:
191: public void widgetDefaultSelected(SelectionEvent e) {
192: doRun();
193: }
194:
195: });
196:
197: Composite composite = WidgetFactory.createComposite(formBody,
198: SWT.BORDER);
199: composite.setLayout(new FillLayout());
200: GridData gridData = new GridData(GridData.FILL_BOTH);
201: gridData.horizontalSpan = 6;
202: composite.setLayoutData(gridData);
203: browser = new Browser(composite, SWT.BORDER);
204: }
205:
206: /* (non-Javadoc)
207: * @see org.pentaho.designstudio.editors.actionsequence.pages.ActSeqBasePage#refreshPage(org.dom4j.Document)
208: */
209: protected void refresh() {
210: IEditorInput edInput = getEditorInput();
211:
212: if (getEditor().isDirty()) {
213: if (MessageDialog
214: .openQuestion(
215: getSite().getShell(),
216: Messages
217: .getString("RunPage.UI_SAVE_REQD_DLG_TITLE"), Messages.getString("RunPage.UI_SAVE_REQD_MESSAGE"))) { //$NON-NLS-1$ //$NON-NLS-2$
218: getEditor().doSave(null);
219: }
220: }
221:
222: if (edInput instanceof FileEditorInput) {
223: IFile file = ((FileEditorInput) edInput).getFile();
224: solutionHelper = new SolutionHelper(file, getSite()
225: .getShell());
226: // iconXA.setBasePath( file.getLocation().removeLastSegments(1) );
227: }
228:
229: getManagedForm().reflow(true);
230: }
231:
232: void doTest() {
233: callUrl(serverTextBox.getText().trim());
234: }
235:
236: void doRun() {
237: String theURL = urlTextBox.getText();
238: if (theURL.trim().length() == 0) {
239: doGenerateURL();
240: theURL = urlTextBox.getText();
241: }
242:
243: callUrl(theURL);
244: }
245:
246: void doPreview() {
247: String theUrl = serverTextBox.getText();
248: theUrl += "/ViewAction"; //$NON-NLS-1$
249: // theUrl += "&solution=" + "RuntimeSolution"; //$NON-NLS-1$
250: // theUrl += "&path=" + getActionPath(); //$NON-NLS-1$
251: // theUrl += "&action=" + getAction(); //$NON-NLS-1$
252:
253: // theURL += solutionHelper.getAction()
254:
255: HttpClient client = new HttpClient();
256: PostMethod method = new PostMethod(theUrl);
257: NameValuePair[] data = {
258: new NameValuePair("userid", "joe"), //$NON-NLS-1$ //$NON-NLS-2$
259: new NameValuePair("password", "password"), //$NON-NLS-1$ //$NON-NLS-2$
260: new NameValuePair("solution", "RuntimeSolution"), //$NON-NLS-1$ //$NON-NLS-2$
261: new NameValuePair("action", solutionHelper.getAction()), //$NON-NLS-1$
262: new NameValuePair(
263: "path", actSeqDocProvider.getDocument().asXML()), //$NON-NLS-1$
264: };
265: method.setRequestBody(data);
266: try {
267: // Execute the method.
268: int statusCode = client.executeMethod(method);
269:
270: if (statusCode != HttpStatus.SC_OK) {
271: System.err
272: .println(Messages
273: .getString("RunPage.METHOD_FAILED") + method.getStatusLine()); //$NON-NLS-1$
274: }
275:
276: // Read the response body.
277: byte[] responseBody = method.getResponseBody();
278:
279: // Deal with the response.
280: // Use caution: ensure correct character encoding and is not binary data
281: // System.out.println(new String(responseBody));
282: browser.setText(new String(responseBody));
283:
284: } catch (HttpException e) {
285: System.err
286: .println(Messages
287: .getString("RunPage.PROTOCOL_VIOLATION") + e.getMessage()); //$NON-NLS-1$
288: e.printStackTrace();
289: } catch (IOException e) {
290: System.err
291: .println(Messages
292: .getString("RunPage.TRANSPORT_ERROR") + e.getMessage()); //$NON-NLS-1$
293: e.printStackTrace();
294: } finally {
295: // Release the connection.
296: method.releaseConnection();
297: }
298: }
299:
300: private static String getServerUrl() {
301: String serverUrl = "";
302: // String defaultURL = DesignStudioCorePlugin.getDefault().getPluginPreferences().getString(DesignStudioCorePreferenceConstants.PENTAHO_SERVER_URL);
303: // printPreferences(Platform.getPreferencesService().getRootNode());
304: Preferences pref = Platform
305: .getPreferencesService()
306: .getRootNode()
307: .node("/instance/org.pentaho.designstudio.editors.core");
308: if (pref == null) {
309: pref = Platform.getPreferencesService().getRootNode().node(
310: "/default/org.pentaho.designstudio.editors.core");
311: }
312: if (pref != null) {
313: serverUrl = pref.get("pentaho.server.url", "");
314: }
315: return serverUrl;
316: }
317:
318: public static String doPreview(Element mql) {
319: String theUrl = getServerUrl();
320: theUrl += "/ViewAction"; //$NON-NLS-1$
321: String theResponse = null;
322:
323: HttpClient client = new HttpClient();
324: PostMethod method = new PostMethod(theUrl);
325: NameValuePair[] data = {
326: new NameValuePair("userid", "joe"), //$NON-NLS-1$ //$NON-NLS-2$
327: new NameValuePair("password", "password"), //$NON-NLS-1$ //$NON-NLS-2$
328: new NameValuePair("solution", "RuntimeSolution"), //$NON-NLS-1$ //$NON-NLS-2$
329: new NameValuePair("action", "MQLPreview.xaction"), //$NON-NLS-1$ //$NON-NLS-2$
330: new NameValuePair(
331: "path", createMQLReportActionSequence(mql).asXML()), //$NON-NLS-1$
332: };
333: method.setRequestBody(data);
334: try {
335: // Execute the method.
336: int statusCode = client.executeMethod(method);
337:
338: if (statusCode != HttpStatus.SC_OK) {
339: System.err
340: .println(Messages
341: .getString("RunPage.METHOD_FAILED") + method.getStatusLine()); //$NON-NLS-1$
342: }
343:
344: // Read the response body.
345: byte[] responseBody = method.getResponseBody();
346:
347: theResponse = new String(responseBody);
348:
349: } catch (HttpException e) {
350: System.err
351: .println(Messages
352: .getString("RunPage.PROTOCOL_VIOLATION") + e.getMessage()); //$NON-NLS-1$
353: e.printStackTrace();
354: } catch (IOException e) {
355: System.err
356: .println(Messages
357: .getString("RunPage.TRANSPORT_ERROR") + e.getMessage()); //$NON-NLS-1$
358: e.printStackTrace();
359: } finally {
360: // Release the connection.
361: method.releaseConnection();
362: }
363:
364: return (theResponse);
365: }
366:
367: void doGenerateURL() {
368: if (solutionHelper != null) {
369: solutionHelper.setBaseUrl(serverTextBox.getText());
370: String theUrl = solutionHelper.getActionUrl();
371: urlTextBox.setText(theUrl);
372: }
373: }
374:
375: void doGetBrowserURL() {
376: if (browser != null) {
377: urlTextBox.setText(browser.getUrl());
378: }
379: }
380:
381: void callUrl(String url) {
382: if ((url != null) && (browser != null)) {
383: if (url.equals(browser.getUrl())) {
384: browser.refresh();
385: } else {
386: browser.setUrl(url);
387: }
388: }
389: }
390:
391: public static Document createMQLReportActionSequence(Element mqlNode) {
392: try {
393: Document document = DOMDocumentFactory.getInstance()
394: .createDocument();
395: Element actionSeqElement = document
396: .addElement("action-sequence"); //$NON-NLS-1$
397: Element actionSeqNameElement = actionSeqElement
398: .addElement("name"); //$NON-NLS-1$
399: actionSeqNameElement.setText("MQLQueryPreview.xaction"); //$NON-NLS-1$
400: Element actionSeqVersionElement = actionSeqElement
401: .addElement("version"); //$NON-NLS-1$
402: actionSeqVersionElement.setText("1"); //$NON-NLS-1$
403: Element actionSeqTitleElement = actionSeqElement
404: .addElement("title"); //$NON-NLS-1$
405: actionSeqTitleElement.setText("MQL Query Preview"); //$NON-NLS-1$
406: Element documentationElement = actionSeqElement
407: .addElement("documentation"); //$NON-NLS-1$
408: Element authorElement = documentationElement
409: .addElement("author"); //$NON-NLS-1$
410: authorElement.setText("Web Query & Reporting"); //$NON-NLS-1$
411: Element descElement = documentationElement
412: .addElement("description"); //$NON-NLS-1$
413: descElement.setText("MQL Query Preview"); //$NON-NLS-1$
414: Element iconElement = documentationElement
415: .addElement("icon"); //$NON-NLS-1$
416: iconElement.setText("PentahoReporting.png"); //$NON-NLS-1$
417: Element helpElement = documentationElement
418: .addElement("help"); //$NON-NLS-1$
419: helpElement
420: .setText("Auto-generated action-sequence for WQR."); //$NON-NLS-1$
421: Element resultTypeElement = documentationElement
422: .addElement("result-type"); //$NON-NLS-1$
423: resultTypeElement.setText("report"); //$NON-NLS-1$
424: // inputs
425: // outputs
426: Element outputsElement = actionSeqElement
427: .addElement("outputs"); //$NON-NLS-1$
428: Element reportTypeElement = outputsElement
429: .addElement("data"); //$NON-NLS-1$
430: reportTypeElement.addAttribute("type", "result-set"); //$NON-NLS-1$ //$NON-NLS-2$
431:
432: Element actionsElement = actionSeqElement
433: .addElement("actions"); //$NON-NLS-1$
434:
435: Element actionDefinitionElement = actionsElement
436: .addElement("action-definition"); //$NON-NLS-1$
437: Element actionOutputsElement = actionDefinitionElement
438: .addElement("action-outputs"); //$NON-NLS-1$
439: Element ruleResultElement = actionOutputsElement
440: .addElement("data"); //$NON-NLS-1$
441: ruleResultElement.addAttribute("type", "result-set"); //$NON-NLS-1$ //$NON-NLS-2$
442: Element componentNameElement = actionDefinitionElement
443: .addElement("component-name"); //$NON-NLS-1$
444: componentNameElement
445: .setText("org.pentaho.plugin.mql.MQLRelationalDataComponent"); //$NON-NLS-1$
446: Element actionTypeElement = actionDefinitionElement
447: .addElement("action-type"); //$NON-NLS-1$
448: actionTypeElement.setText("rule"); //$NON-NLS-1$
449: Element componentDefinitionElement = actionDefinitionElement
450: .addElement("component-definition"); //$NON-NLS-1$
451: componentDefinitionElement.add(mqlNode);
452: // GEM PMD-175 removed display-names from component
453: //componentDefinitionElement.addElement( "display-names" ).setText( "false" ); //$NON-NLS-1$ //$NON-NLS-2$
454: return (document);
455: } catch (Exception e) {
456: e.printStackTrace();
457: }
458: return (null);
459: }
460:
461: public void setActive(boolean active) {
462: if (active) {
463: refresh();
464: }
465: super.setActive(active);
466: }
467: }
|