<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 (c) 2002-2013 Pentaho Corporation..  All rights reserved.
*/

package org.pentaho.agilebi.spoon.visualizations.analyzer;

import mondrian.rolap.agg.AggregationManager;
import org.pentaho.agilebi.modeler.util.ISpoonModelerSource;
import org.pentaho.metadata.model.concept.types.LocalizedString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.widgets.Composite;
import org.pentaho.agilebi.modeler.ModelerException;
import org.pentaho.agilebi.modeler.ModelerWorkspace;
import org.pentaho.agilebi.spoon.ModelerHelper;
import org.pentaho.agilebi.spoon.perspective.AgileBiModelerPerspective;
import org.pentaho.agilebi.spoon.publish.PublisherHelper;
import org.pentaho.agilebi.spoon.visualizations.AbstractVisualization;
import org.pentaho.agilebi.spoon.visualizations.IVisualization;
import org.pentaho.agilebi.spoon.visualizations.PropertyPanelController;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.EngineMetaInterface;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.gui.SpoonFactory;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.ui.spoon.FileListener;
import org.pentaho.di.ui.spoon.Spoon;
import org.pentaho.metadata.model.Domain;
import org.pentaho.metadata.model.IPhysicalModel;
import org.pentaho.metadata.model.IPhysicalTable;
import org.pentaho.platform.api.repository2.unified.RepositoryFile;
import org.pentaho.ui.xul.XulException;
import org.pentaho.ui.xul.binding.Binding;
import org.pentaho.ui.xul.binding.Binding.Type;
import org.pentaho.ui.xul.binding.BindingFactory;
import org.pentaho.ui.xul.binding.DefaultBindingFactory;
import org.pentaho.ui.xul.components.XulBrowser;
import org.pentaho.ui.xul.components.XulMessageBox;
import org.pentaho.ui.xul.containers.XulEditpanel;
import org.pentaho.ui.xul.impl.AbstractXulEventHandler;
import org.w3c.dom.Node;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

public class AnalyzerVisualizationController extends AbstractXulEventHandler implements FileListener, PropertyPanelController {

	public static final String XUL_FILE_ANALYZER_TOOLBAR_PROPERTIES = "plugins/spoon/agile-bi/ui/analyzer-toolbar.properties"; //$NON-NLS-1$

	private String xmiFileLocation = null;
	private String modelId = null;
	private AnalyzerVisualization visualization;
	private AnalyzerVisualizationMeta meta;
	private String visFileLocation = null;
	private XulBrowser browser;
	private Spoon spoon;
	private String location;
	private BindingFactory bf;
	private Binding modelNameBinding;
	private Binding factTableNameBinding;
	private String factTableName;
	private XulEditpanel propPanel;
	private ModelerWorkspace workspace;
	private boolean dirty = true;

	private static Logger logger = LoggerFactory.getLogger(AnalyzerVisualizationController.class);
	private String fileName;

	public AnalyzerVisualizationController(Composite parent, final AnalyzerVisualization visualization, String xmiFileLocation, String modelId, String aVisFileLocaiton, String fileName) throws SWTError {
		this.visualization = visualization;
		this.xmiFileLocation = xmiFileLocation;
		this.modelId = modelId;
		this.visFileLocation = aVisFileLocaiton;
		this.meta = new AnalyzerVisualizationMeta(this);
		this.spoon = ((Spoon) SpoonFactory.getInstance());
		this.location = visualization.generateNewUrl(xmiFileLocation, modelId);
		this.bf = new DefaultBindingFactory();
		this.fileName = fileName;
	}
	


	public void init() {
		this.browser = (XulBrowser) this.document.getElementById("web_visualization_browser");
		this.propPanel = (XulEditpanel) document.getElementById("propPanel");
		
		this.browser.setSrc(Const.NVL(this.location, "http://"));

		this.bf.setDocument(super.document);
		this.bf.setBindingType(Type.ONE_WAY);

		this.modelNameBinding = this.bf.createBinding(this, "modelId", "modelName", "value");
		this.factTableNameBinding = this.bf.createBinding(this, "factTableName", "factTableName", "value");
    this.bf.setBindingType(Type.BI_DIRECTIONAL);
    bf.createBinding(this.propPanel, "visible", this, "propVisible");
		fireBindings();
		setPropVisible(false);
	}
	
	public void openReport(String aReport) {
	  String theLocation = this.location.substring(0, this.location.indexOf("?"));
	  
	  try {
      aReport = URLEncoder.encode(aReport, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }

    // to make sure the report reflects any modifications to the underlying model
    // we must clear the cache & save the report. see AGILEBI-471
    // this is what the flush=true parameter is for.
    // also, the rnd param is to make sure that the browser does not display a cached version of the requested report
    long avoidBrowserCache = Calendar.getInstance().getTimeInMillis();
	  theLocation = theLocation + "?command=open&solution=&path=&action=" + aReport + "&edit=true&showFieldList=true&flush=true&rnd=" + avoidBrowserCache;
	  this.browser.setSrc(theLocation);
	}

	private String processFactTableName() {
		String theName = null;
		Domain theDomain = ModelerHelper.getInstance().loadDomain(this.xmiFileLocation);
		List<IPhysicalModel> theModels = theDomain.getPhysicalModels();
		if (theModels != null && theModels.size() > 0) {
			IPhysicalModel theModel = theModels.get(0);
			List theTables = theModel.getPhysicalTables();
			if (theTables != null && theTables.size() > 0) {
				IPhysicalTable theTable = (IPhysicalTable) theTables.get(0);
				theName = theTable.getName(LocalizedString.DEFAULT_LOCALE);
			}
		}
		return theName;
	}

	private void fireBindings() {
		try {
			this.modelNameBinding.fireSourceChanged();
			this.factTableNameBinding.fireSourceChanged();
		} catch (Exception e) {
			logger.info("Error firing bindings", e);
		}
	}

	public String getFactTableName() {
		if (this.factTableName == null) {<commit_msg>[PDI-11449] - Cannot open xanalyzer files - "loader constraint violation"<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 (c) 2002-2013 Pentaho Corporation..  All rights reserved.
*/

package org.pentaho.agilebi.spoon.visualizations.analyzer;

import mondrian.rolap.agg.AggregationManager;
import org.pentaho.agilebi.modeler.util.ISpoonModelerSource;
import org.pentaho.metadata.model.concept.types.LocalizedString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.widgets.Composite;
import org.pentaho.agilebi.modeler.ModelerException;
import org.pentaho.agilebi.modeler.ModelerWorkspace;
import org.pentaho.agilebi.spoon.ModelerHelper;
import org.pentaho.agilebi.spoon.perspective.AgileBiModelerPerspective;
import org.pentaho.agilebi.spoon.publish.PublisherHelper;
import org.pentaho.agilebi.spoon.visualizations.AbstractVisualization;
import org.pentaho.agilebi.spoon.visualizations.IVisualization;
import org.pentaho.agilebi.spoon.visualizations.PropertyPanelController;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.EngineMetaInterface;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.gui.SpoonFactory;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.ui.spoon.FileListener;
import org.pentaho.di.ui.spoon.Spoon;
import org.pentaho.metadata.model.Domain;
import org.pentaho.metadata.model.IPhysicalModel;
import org.pentaho.metadata.model.IPhysicalTable;
import org.pentaho.platform.api.repository2.unified.RepositoryFile;
import org.pentaho.ui.xul.XulException;
import org.pentaho.ui.xul.binding.Binding;
import org.pentaho.ui.xul.binding.Binding.Type;
import org.pentaho.ui.xul.binding.BindingFactory;
import org.pentaho.ui.xul.binding.DefaultBindingFactory;
import org.pentaho.ui.xul.components.XulBrowser;
import org.pentaho.ui.xul.components.XulMessageBox;
import org.pentaho.ui.xul.containers.XulEditpanel;
import org.pentaho.ui.xul.impl.AbstractXulEventHandler;
import org.w3c.dom.Node;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

public class AnalyzerVisualizationController extends AbstractXulEventHandler implements FileListener, PropertyPanelController {

	public static final String XUL_FILE_ANALYZER_TOOLBAR_PROPERTIES = "plugins/spoon/agile-bi/ui/analyzer-toolbar.properties"; //$NON-NLS-1$

	private String xmiFileLocation = null;
	private String modelId = null;
	private AnalyzerVisualization visualization;
	private AnalyzerVisualizationMeta meta;
	private String visFileLocation = null;
	private XulBrowser browser;
	private Spoon spoon;
	private String location;
	private BindingFactory bf;
	private Binding modelNameBinding;
	private Binding factTableNameBinding;
	private String factTableName;
	private XulEditpanel propPanel;
	private ModelerWorkspace workspace;
	private boolean dirty = true;

	private static Logger logger = LoggerFactory.getLogger(AnalyzerVisualizationController.class);
	private String fileName;

	public AnalyzerVisualizationController(Composite parent, final AnalyzerVisualization visualization, String xmiFileLocation, String modelId, String aVisFileLocaiton, String fileName) throws SWTError {
		this.visualization = visualization;
		this.xmiFileLocation = xmiFileLocation;
		this.modelId = modelId;
		this.visFileLocation = aVisFileLocaiton;
		this.meta = new AnalyzerVisualizationMeta(this);
		this.spoon = ((Spoon) SpoonFactory.getInstance());
		this.location = visualization.generateNewUrl(xmiFileLocation, modelId);
		this.bf = new DefaultBindingFactory();
		this.fileName = fileName;
	}
	


	public void init() {
		this.browser = (XulBrowser) this.document.getElementById("web_visualization_browser");
		this.propPanel = (XulEditpanel) document.getElementById("propPanel");
		
		this.browser.setSrc(Const.NVL(this.location, "http://"));

		this.bf.setDocument(super.document);
		this.bf.setBindingType(Type.ONE_WAY);

		this.modelNameBinding = this.bf.createBinding(this, "modelId", "modelName", "value");
		this.factTableNameBinding = this.bf.createBinding(this, "factTableName", "factTableName", "value");
    this.bf.setBindingType(Type.BI_DIRECTIONAL);
    bf.createBinding(this.propPanel, "visible", this, "propVisible");
		fireBindings();
		setPropVisible(false);
	}
	
	public void openReport(String aReport) {
	  String theLocation = visualization.generateOpenUrl( aReport );
	  this.browser.setSrc(theLocation);
	}

	private String processFactTableName() {
		String theName = null;
		Domain theDomain = ModelerHelper.getInstance().loadDomain(this.xmiFileLocation);
		List<IPhysicalModel> theModels = theDomain.getPhysicalModels();
		if (theModels != null && theModels.size() > 0) {
			IPhysicalModel theModel = theModels.get(0);
			List theTables = theModel.getPhysicalTables();
			if (theTables != null && theTables.size() > 0) {
				IPhysicalTable theTable = (IPhysicalTable) theTables.get(0);
				theName = theTable.getName(LocalizedString.DEFAULT_LOCALE);
			}
		}
		return theName;
	}

	private void fireBindings() {
		try {
			this.modelNameBinding.fireSourceChanged();
			this.factTableNameBinding.fireSourceChanged();
		} catch (Exception e) {
			logger.info("Error firing bindings", e);
		}
	}

	public String getFactTableName() {
		if (this.factTableName == null) {
{'fraction_non_alphanumeric': 0.10028742328905461, 'fraction_numerical': 0.006059193661151247, 'mean_word_length': 5.515182186234818, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '5760826', 'n_tokens_mistral': 4132, 'n_tokens_neox': 3880, 'n_words': 894}