<commit_before>/*
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
 * Foundation.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 * or from the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * Copyright 2008 - 2009 Pentaho Corporation.  All rights reserved.
 *
 *
 * Created December 08, 2011
 * @author <NAME>
 */

package org.pentaho.platform.dataaccess.datasource.ui.importing;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.logging.Logger;

import org.pentaho.gwt.widgets.client.utils.i18n.ResourceBundle;
import org.pentaho.gwt.widgets.client.utils.string.StringUtils;
import org.pentaho.platform.dataaccess.datasource.ui.admindialog.DatasourceAdminDialogController;
import org.pentaho.platform.dataaccess.datasource.wizard.DatasourceMessages;
import org.pentaho.platform.dataaccess.datasource.wizard.controllers.ConnectionController;
import org.pentaho.platform.dataaccess.datasource.wizard.controllers.MessageHandler;
import org.pentaho.ui.database.event.IConnectionAutoBeanFactory;
import org.pentaho.ui.database.event.IDatabaseConnectionList;
import org.pentaho.ui.xul.XulComponent;
import org.pentaho.ui.xul.XulException;
import org.pentaho.ui.xul.binding.Binding;
import org.pentaho.ui.xul.binding.BindingConvertor;
import org.pentaho.ui.xul.binding.BindingFactory;
import org.pentaho.ui.xul.components.XulButton;
import org.pentaho.ui.xul.components.XulConfirmBox;
import org.pentaho.ui.xul.components.XulLabel;
import org.pentaho.ui.xul.components.XulMenuList;
import org.pentaho.ui.xul.components.XulMessageBox;
import org.pentaho.ui.xul.components.XulRadio;
import org.pentaho.ui.xul.components.XulTextbox;
import org.pentaho.ui.xul.containers.XulDeck;
import org.pentaho.ui.xul.containers.XulDialog;
import org.pentaho.ui.xul.containers.XulTree;
import org.pentaho.ui.xul.containers.XulVbox;
import org.pentaho.ui.xul.stereotype.Bindable;
import org.pentaho.ui.xul.util.AbstractXulDialogController;
import org.pentaho.ui.xul.util.XulDialogCallback;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.RootPanel;
//import org.pentaho.platform.dataaccess.datasource.wizard.service.IXulAsyncConnectionService;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.web.bindery.autobean.shared.AutoBean;
import com.google.web.bindery.autobean.shared.AutoBeanCodex;

@SuppressWarnings("all")
public class AnalysisImportDialogController extends AbstractXulDialogController<AnalysisImportDialogModel> implements
    IImportPerspective {

  private static final String MONDRIAN_POSTANALYSIS_URL = "plugin/data-access/api/mondrian/postAnalysis";

  private static Logger logger = Logger.getLogger(AnalysisImportDialogController.class.getName());

  private BindingFactory bf;

  private XulMenuList connectionList;

  private XulTree analysisParametersTree;

  private XulDialog importDialog;

  private XulDialog analysisParametersDialog;

  private ResourceBundle resBundle;

  private AnalysisImportDialogModel importDialogModel;

//  private IXulAsyncConnectionService connectionService;

  private XulTextbox paramNameTextBox;

  private XulTextbox paramValueTextBox;

  private XulDeck analysisPreferencesDeck;

  private XulRadio availableRadio;

  private XulRadio manualRadio;

  private XulButton acceptButton;

  private XulButton parametersAcceptButton;

  private FileUpload analysisUpload;

  private XulLabel schemaNameLabel;

  private XulLabel analysisFileLabel;

  private String importURL;

  private boolean overwrite = false;

  private static final Integer PARAMETER_MODE = 1;

  private static final Integer DATASOURCE_MODE = 0;

  protected static final String SUCCESS = "3";//to do - chnage to Integer

  protected static final int PUBLISH_SCHEMA_EXISTS_ERROR = 8;

  private static SubmitCompleteHandler submitHandler = null;

  private DatasourceMessages messages = null;

  private XulVbox hiddenArea;

  private FormPanel formPanel;

  private FlowPanel mainFormPanel;

  private FileUpload analysisFileUpload;

  protected IConnectionAutoBeanFactory connectionAutoBeanFactory;

  public void init() {
    try {
      connectionAutoBeanFactory = GWT.create(IConnectionAutoBeanFactory.class);
      resBundle = (ResourceBundle) super.getXulDomContainer().getResourceBundles().get(0);
//      connectionService = new ConnectionServiceGwtImpl();
      importDialogModel = new AnalysisImportDialogModel();
      connectionList = (XulMenuList) document.getElementById("connectionList");
      analysisParametersTree = (XulTree) document.getElementById("analysisParametersTree");
      importDialog = (XulDialog) document.getElementById("importDialog");
      analysisParametersDialog = (XulDialog) document.getElementById("analysisParametersDialog");
      paramNameTextBox = (XulTextbox) document.getElementById("paramNameTextBox");
      paramNameTextBox.addPropertyChangeListener(new ParametersChangeListener());
      paramValueTextBox = (XulTextbox) document.getElementById("paramValueTextBox");
      paramValueTextBox.addPropertyChangeListener(new ParametersChangeListener());
      analysisPreferencesDeck = (XulDeck) document.getElementById("analysisPreferencesDeck");
      availableRadio = (XulRadio) document.getElementById("availableRadio");
      manualRadio = (XulRadio) document.getElementById("manualRadio");
      hiddenArea = (XulVbox) document.getElementById("analysisImportCard");
      schemaNameLabel = (XulLabel) document.getElementById("schemaNameLabel");
      analysisFileLabel = (XulLabel) document.getElementById("analysisFileLabel");
      acceptButton = (XulButton) document.getElementById("importDialog_accept");
      acceptButton.setDisabled(true);

      parametersAcceptButton = (XulButton) document.getElementById("analysisParametersDialog_accept");
      parametersAcceptButton.setDisabled(true);

      bf.setBindingType(Binding.Type.ONE_WAY);
      bf.createBinding(connectionList, "selectedItem", importDialogModel, "connection");
      bf.createBinding(manualRadio, "checked", this, "preference", new PreferencesBindingConvertor());

      Binding connectionListBinding = bf.createBinding(importDialogModel, "connectionList", connectionList, "elements");
      Binding analysisParametersBinding = bf.createBinding(importDialogModel, "analysisParameters",
          analysisParametersTree, "elements");
      connectionListBinding.fireSourceChanged();
      analysisParametersBinding.fireSourceChanged();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void createWorkingForm() {
    if (formPanel == null) {
      formPanel = new FormPanel();
      formPanel.setMethod(FormPanel.METHOD_POST);
      formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
      formPanel.setAction(MONDRIAN_POSTANALYSIS_URL);
      formPanel.getElement().getStyle().setProperty("position", "absolute");
      formPanel.getElement().getStyle().setProperty("visibility", "hidden");
      formPanel.getElement().getStyle().setProperty("overflow", "hidden");
      formPanel.getElement().getStyle().setProperty("clip", "rect(0px,0px,0px,0px)");
      mainFormPanel = new FlowPanel();
      formPanel.add(mainFormPanel);
      analysisFileUpload = new FileUpload();
      analysisFileUpload.setName("uploadAnalysis");
      analysisFileUpload.getElement().setId("analysisFileUpload");
      analysisFileUpload.addChangeHandler(new ChangeHandler() {
        @Override
        public void onChange(ChangeEvent event) {
          schemaNameLabel.setValue(((FileUpload) event.getSource()).getFilename());
          importDialogModel.setUploadedFile(((FileUpload) event.getSource()).getFilename());<commit_msg>Fix for BISERVER-8303 Can't import an Analysis data source - connection names are corrupt<commit_after>/*
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
 * Foundation.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 * or from the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * Copyright 2008 - 2009 Pentaho Corporation.  All rights reserved.
 *
 *
 * Created December 08, 2011
 * @author <NAME>
 */

package org.pentaho.platform.dataaccess.datasource.ui.importing;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.pentaho.database.model.IDatabaseConnection;
import org.pentaho.gwt.widgets.client.utils.i18n.ResourceBundle;
import org.pentaho.gwt.widgets.client.utils.string.StringUtils;
import org.pentaho.platform.dataaccess.datasource.wizard.DatasourceMessages;
import org.pentaho.platform.dataaccess.datasource.wizard.controllers.MessageHandler;
import org.pentaho.ui.database.event.IConnectionAutoBeanFactory;
import org.pentaho.ui.database.event.IDatabaseConnectionList;
import org.pentaho.ui.xul.XulComponent;
import org.pentaho.ui.xul.XulException;
import org.pentaho.ui.xul.binding.Binding;
import org.pentaho.ui.xul.binding.BindingConvertor;
import org.pentaho.ui.xul.binding.BindingFactory;
import org.pentaho.ui.xul.components.XulButton;
import org.pentaho.ui.xul.components.XulConfirmBox;
import org.pentaho.ui.xul.components.XulLabel;
import org.pentaho.ui.xul.components.XulMenuList;
import org.pentaho.ui.xul.components.XulMessageBox;
import org.pentaho.ui.xul.components.XulRadio;
import org.pentaho.ui.xul.components.XulTextbox;
import org.pentaho.ui.xul.containers.XulDeck;
import org.pentaho.ui.xul.containers.XulDialog;
import org.pentaho.ui.xul.containers.XulTree;
import org.pentaho.ui.xul.containers.XulVbox;
import org.pentaho.ui.xul.stereotype.Bindable;
import org.pentaho.ui.xul.util.AbstractXulDialogController;
import org.pentaho.ui.xul.util.XulDialogCallback;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.web.bindery.autobean.shared.AutoBean;
import com.google.web.bindery.autobean.shared.AutoBeanCodex;
//import org.pentaho.platform.dataaccess.datasource.wizard.service.IXulAsyncConnectionService;

@SuppressWarnings("all")
public class AnalysisImportDialogController extends AbstractXulDialogController<AnalysisImportDialogModel> implements
    IImportPerspective {

  private static final String MONDRIAN_POSTANALYSIS_URL = "plugin/data-access/api/mondrian/postAnalysis";

  private static Logger logger = Logger.getLogger(AnalysisImportDialogController.class.getName());

  private BindingFactory bf;

  private XulMenuList connectionList;

  private XulTree analysisParametersTree;

  private XulDialog importDialog;

  private XulDialog analysisParametersDialog;

  private ResourceBundle resBundle;

  private AnalysisImportDialogModel importDialogModel;

//  private IXulAsyncConnectionService connectionService;

  private XulTextbox paramNameTextBox;

  private XulTextbox paramValueTextBox;

  private XulDeck analysisPreferencesDeck;

  private XulRadio availableRadio;

  private XulRadio manualRadio;

  private XulButton acceptButton;

  private XulButton parametersAcceptButton;

  private FileUpload analysisUpload;

  private XulLabel schemaNameLabel;

  private XulLabel analysisFileLabel;

  private String importURL;

  private boolean overwrite = false;

  private static final Integer PARAMETER_MODE = 1;

  private static final Integer DATASOURCE_MODE = 0;

  protected static final String SUCCESS = "3";//to do - chnage to Integer

  protected static final int PUBLISH_SCHEMA_EXISTS_ERROR = 8;

  private static SubmitCompleteHandler submitHandler = null;

  private DatasourceMessages messages = null;

  private XulVbox hiddenArea;

  private FormPanel formPanel;

  private FlowPanel mainFormPanel;

  private FileUpload analysisFileUpload;

  protected IConnectionAutoBeanFactory connectionAutoBeanFactory;

  public void init() {
    try {
      connectionAutoBeanFactory = GWT.create(IConnectionAutoBeanFactory.class);
      resBundle = (ResourceBundle) super.getXulDomContainer().getResourceBundles().get(0);
//      connectionService = new ConnectionServiceGwtImpl();
      importDialogModel = new AnalysisImportDialogModel();
      connectionList = (XulMenuList) document.getElementById("connectionList");
      analysisParametersTree = (XulTree) document.getElementById("analysisParametersTree");
      importDialog = (XulDialog) document.getElementById("importDialog");
      analysisParametersDialog = (XulDialog) document.getElementById("analysisParametersDialog");
      paramNameTextBox = (XulTextbox) document.getElementById("paramNameTextBox");
      paramNameTextBox.addPropertyChangeListener(new ParametersChangeListener());
      paramValueTextBox = (XulTextbox) document.getElementById("paramValueTextBox");
      paramValueTextBox.addPropertyChangeListener(new ParametersChangeListener());
      analysisPreferencesDeck = (XulDeck) document.getElementById("analysisPreferencesDeck");
      availableRadio = (XulRadio) document.getElementById("availableRadio");
      manualRadio = (XulRadio) document.getElementById("manualRadio");
      hiddenArea = (XulVbox) document.getElementById("analysisImportCard");
      schemaNameLabel = (XulLabel) document.getElementById("schemaNameLabel");
      analysisFileLabel = (XulLabel) document.getElementById("analysisFileLabel");
      acceptButton = (XulButton) document.getElementById("importDialog_accept");
      acceptButton.setDisabled(true);

      parametersAcceptButton = (XulButton) document.getElementById("analysisParametersDialog_accept");
      parametersAcceptButton.setDisabled(true);

      bf.setBindingType(Binding.Type.ONE_WAY);
      bf.createBinding(connectionList, "selectedIndex", importDialogModel, "connection", new BindingConvertor<Integer, IDatabaseConnection>() {
          @Override
          public Integer targetToSource(IDatabaseConnection connection) {
            return -1;
          }

          @Override
          public IDatabaseConnection sourceToTarget(Integer value) {
            if (value >= 0) {
              return importDialogModel.getConnectionList().get(value);
            }
            return null;
          }
      });

      bf.createBinding(manualRadio, "checked", this, "preference", new PreferencesBindingConvertor());
      bf.createBinding(this, "connectionNames", connectionList, "elements");
      
      Binding domainBinding = bf.createBinding(importDialogModel, "connectionList", this, "relationalConnections");
      Binding analysisParametersBinding = bf.createBinding(importDialogModel, "analysisParameters", analysisParametersTree, "elements");
      domainBinding.fireSourceChanged();
      analysisParametersBinding.fireSourceChanged();
      

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  
  @Bindable
  public void setRelationalConnections(List<IDatabaseConnection> connections) {
    List<String> names = new ArrayList<String>();
    for (IDatabaseConnection conn : connections) {
      names.add(conn.getName());
    }

    firePropertyChange("connectionNames", null, names);
  }

  private void createWorkingForm() {
    if (formPanel == null) {
      formPanel = new FormPanel();
      formPanel.setMethod(FormPanel.METHOD_POST);
      formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
      formPanel.setAction(MONDRIAN_POSTANALYSIS_URL);
      formPanel.getElement().getStyle().setProperty("position", "absolute");
      formPanel.getElement().getStyle().setProperty("visibility", "hidden");
      formPanel.getElement().getStyle().setProperty("overflow", "hidden");
      formPanel.getElement().getStyle().setProperty("clip", "rect(0px,0px,0px,0px)");
      mainFormPanel = new FlowPanel();
      formPanel.add(mainFormPanel);
      analysisFileUpload = new FileUpload();
      analysisFileUpload.setName("uploadAnalysis");
      analysisFileUpload.getElement().setId("analysisFileUpload");
      analysisFileUpload.addChangeHandler(new ChangeHandler() {
        @Override
        public void onChange(ChangeEvent event) {
          schemaNameLabel.setValue(((FileUpload) event.getSource()).getFilename());
          importDialogModel.setUploadedFile(((FileUpload) event.getSource()).getFilename());
{'fraction_non_alphanumeric': 0.09718710493046777, 'fraction_numerical': 0.004530130636325327, 'mean_word_length': 5.159961064243998, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4337775', 'n_tokens_mistral': 5395, 'n_tokens_neox': 5222, 'n_words': 1052}