Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Issues #10, #11, #12, #13 bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcasters committed Dec 19, 2018
1 parent 43234dd commit d61c809
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 35 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,5 @@


<groupId>kettle-environment</groupId>
<version>1.2.0-SNAPSHOT</version>
<version>1.3.0-SNAPSHOT</version>
</project>
5 changes: 2 additions & 3 deletions src/main/java/org/kettle/env/environment/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,10 @@ public void modifyVariableSpace( VariableSpace space ) {
modifyVariableSpace( space, false );
}

private void modifyVariableSpace( VariableSpace space, boolean modifySystem ) {
public void modifyVariableSpace( VariableSpace space, boolean modifySystem ) {

if (space==null) {
space = new Variables();
space.initializeVariablesFrom(null);
space = Variables.getADefaultVariableSpace();
}

// Set the name of the active environment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ public static final void restoreSessionInSpoon( EnvironmentSession session ) {
//
for ( int i = session.getSessionFiles().size() - 1; i >= 0; i-- ) {
EnvironmentLastUsedFile lastUsedFile = session.getSessionFiles().get( i );
spoon.lastFileSelect( lastUsedFile.createLastUsedFile() );
if (lastUsedFile!=null) {
spoon.lastFileSelect( lastUsedFile.createLastUsedFile() );
}
}

// TODO: Select the last active file
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/org/kettle/env/spoon/EnvironmentHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
import org.kettle.env.util.Defaults;
import org.kettle.env.util.EnvironmentUtil;
import org.pentaho.di.core.gui.SpoonFactory;
import org.pentaho.di.core.logging.LogChannel;
import org.pentaho.di.ui.core.dialog.ErrorDialog;
import org.pentaho.di.ui.spoon.ISpoonMenuController;
import org.pentaho.di.ui.spoon.Spoon;
import org.pentaho.metastore.api.exceptions.MetaStoreException;
import org.pentaho.ui.xul.dom.Document;
import org.pentaho.ui.xul.impl.AbstractXulEventHandler;

Expand Down Expand Up @@ -80,7 +82,11 @@ public void switchEnvironment() {
// Save the last used environment
//
EnvironmentConfigSingleton.getConfig().setLastUsedEnvironment( selectedEnvironment );
EnvironmentConfigSingleton.saveConfig();
try {
EnvironmentConfigSingleton.saveConfig();
} catch( MetaStoreException e ) {
LogChannel.GENERAL.logError( "Error saving the environment system config:", e );
}

Environment environment = EnvironmentSingleton.getEnvironmentFactory().loadElement( selectedEnvironment );
spoon.getLog().logBasic( "Setting environment : '" + environment.getName() + "'" );
Expand Down
82 changes: 74 additions & 8 deletions src/main/java/org/kettle/env/util/EnvironmentUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@
import org.pentaho.di.core.Const;
import org.pentaho.di.core.KettleClientEnvironment;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.RowMetaAndData;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.logging.KettleLogStore;
import org.pentaho.di.core.logging.LogChannel;
import org.pentaho.di.core.logging.LogChannelInterface;
import org.pentaho.di.core.plugins.PluginInterface;
import org.pentaho.di.core.plugins.PluginRegistry;
import org.pentaho.di.core.row.value.ValueMetaString;
import org.pentaho.di.core.util.EnvUtil;
import org.pentaho.di.core.variables.VariableSpace;
import org.pentaho.di.core.variables.Variables;
import org.pentaho.di.core.vfs.KettleVFS;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.metastore.MetaStoreConst;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.ui.spoon.Spoon;
import org.pentaho.di.ui.spoon.SpoonPerspective;
import org.pentaho.di.ui.spoon.SpoonPerspectiveManager;
Expand All @@ -33,6 +38,7 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class EnvironmentUtil {

Expand All @@ -51,8 +57,12 @@ public class EnvironmentUtil {
* @throws MetaStoreException
*/
public static void enableEnvironment( Environment environment, DelegatingMetaStore delegatingMetaStore ) throws KettleException, MetaStoreException {

environment.modifySystem();

// Variable system variables but also apply them to variables
// We'll use those to change the loaded variables in Spoon
//
VariableSpace variables = new Variables();
environment.modifyVariableSpace( variables, true );

// Create Kettle home folder in case it doesn't exist
//
Expand Down Expand Up @@ -83,13 +93,25 @@ public static void enableEnvironment( Environment environment, DelegatingMetaSto
}
}

loadSpoonGitRepository( environment );

// See if we need to restore the default Spoon session for this environment
// The name of the session is the name of the environment
// This will cause the least amount of issues.
//
if (Spoon.getInstance()!=null) {
Spoon spoon = Spoon.getInstance();
if ( spoon != null ) {

loadSpoonGitRepository( environment );

// Clear last used, fill it with something useful.
//
spoon.variables = new RowMetaAndData();
for ( String variable : variables.listVariables() ) {
String value = variables.getVariable( variable );
if ( !variable.startsWith( Const.INTERNAL_VARIABLE_PREFIX ) ) {
spoon.variables.addValue( new ValueMetaString( variable ), value );
}
}

if ( environment.isAutoRestoringSpoonSession() ) {
MetaStoreFactory<EnvironmentSession> sessionFactory = new MetaStoreFactory<>( EnvironmentSession.class, delegatingMetaStore, PentahoDefaults.NAMESPACE );
EnvironmentSession environmentSession = sessionFactory.loadElement( environment.getName() );
Expand All @@ -99,6 +121,51 @@ public static void enableEnvironment( Environment environment, DelegatingMetaSto
EnvironmentSessionUtil.restoreSessionInSpoon( environmentSession );
}
}


}
}

/**
* Method copied from Spoon.java because it's private.
*
* @param spoon
* @param vars
*/
private static void fillVariables( Spoon spoon, RowMetaAndData vars ) {
TransMeta[] transMetas = spoon.getLoadedTransformations();
JobMeta[] jobMetas = spoon.getLoadedJobs();
if ( ( transMetas == null || transMetas.length == 0 ) && ( jobMetas == null || jobMetas.length == 0 ) ) {
return;
}

Properties sp = new Properties();
sp.putAll( System.getProperties() );

VariableSpace space = Variables.getADefaultVariableSpace();
String[] keys = space.listVariables();
for ( String key : keys ) {
sp.put( key, space.getVariable( key ) );
}

for ( TransMeta transMeta : transMetas ) {
List<String> list = transMeta.getUsedVariables();
for ( String varName : list ) {
String varValue = sp.getProperty( varName, "" );
if ( vars.getRowMeta().indexOfValue( varName ) < 0 && !varName.startsWith( Const.INTERNAL_VARIABLE_PREFIX ) ) {
vars.addValue( new ValueMetaString( varName ), varValue );
}
}
}

for ( JobMeta jobMeta : jobMetas ) {
List<String> list = jobMeta.getUsedVariables();
for ( String varName : list ) {
String varValue = sp.getProperty( varName, "" );
if ( vars.getRowMeta().indexOfValue( varName ) < 0 && !varName.startsWith( Const.INTERNAL_VARIABLE_PREFIX ) ) {
vars.addValue( new ValueMetaString( varName ), varValue );
}
}
}
}

Expand All @@ -124,8 +191,7 @@ public static void loadSpoonGitRepository( Environment environment ) throws Kett
return;
}

VariableSpace space = new Variables();
space.initializeVariablesFrom( null );
VariableSpace space = Variables.getADefaultVariableSpace();

String realRepoName = space.environmentSubstitute( repoName );
try {
Expand All @@ -141,7 +207,7 @@ public static void loadSpoonGitRepository( Environment environment ) throws Kett
}
}
} catch ( Exception e ) {
throw new KettleException( "Unable to load git repository", e );
LogChannel.UI.logError( "Unable to open GitSpoon project '"+realRepoName+"'", e);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@
import org.pentaho.di.core.gui.BasePainter;
import org.pentaho.di.core.gui.GCInterface;
import org.pentaho.di.core.gui.Point;
import org.pentaho.di.core.gui.PrimitiveGCInterface;
import org.pentaho.di.core.gui.PrimitiveGCInterface.EColor;
import org.pentaho.di.core.logging.LogChannelInterface;
import org.pentaho.di.trans.TransPainter;
import org.pentaho.di.ui.spoon.SWTGC;

@ExtensionPoint(
id = "DrawEnvironmentTransExtensionPoint",
Expand All @@ -51,39 +53,43 @@ public void callExtensionPoint( LogChannelInterface log, Object object ) throws
TransPainter painter = (TransPainter) object;

String activeEnvironment = System.getProperty( Defaults.VARIABLE_ACTIVE_ENVIRONMENT );
if (StringUtils.isNotEmpty( activeEnvironment )) {
drawActiveEnvironment( painter, activeEnvironment);
if ( StringUtils.isNotEmpty( activeEnvironment ) ) {
drawActiveEnvironment( painter, activeEnvironment );
}
}

public static void drawActiveEnvironment( BasePainter<?,?> painter, String activeEnvironment ) {
public static void drawActiveEnvironment( BasePainter<?, ?> painter, String activeEnvironment ) {

GCInterface gc = painter.getGc();
EColor color = EColor.DARKGRAY;
GCInterface defaultGc = painter.getGc();
if ( defaultGc instanceof SWTGC ) {
SWTGC gc = (SWTGC) defaultGc;

Point textSize = gc.textExtent( activeEnvironment );
gc.setFont( "Courier", 8, true, false);
gc.setForeground( 180, 180, 180 );

Point areaSize = painter.getArea();
Point textSize = gc.textExtent( activeEnvironment );

int x = areaSize.x - Math.round((float)textSize.x*painter.getMagnification()) - 20;
int y = 10;
Point areaSize = painter.getArea();

// Take zoom into account
//
x = Math.round( (float)x/painter.getMagnification() );
y = Math.round( (float)y/painter.getMagnification() );
int x = areaSize.x - Math.round( (float) textSize.x * painter.getMagnification() ) - 20;
int y = 10;

// Take zoom into account
//
x = Math.round( (float) x / painter.getMagnification() );
y = Math.round( (float) y / painter.getMagnification() );

// System.out.println( "Active environment : "+activeEnvironment +", drawing on ("+x+","+y+") : "+textSize.x+"x"+textSize.y+", magnification="+painter.getMagnification());

gc.setForeground( color );
gc.drawText( activeEnvironment, x, y, true );
// System.out.println( "Active environment : "+activeEnvironment +", drawing on ("+x+","+y+") : "+textSize.x+"x"+textSize.y+", magnification="+painter.getMagnification());

// Let the world know where the name of the environment is.
// Maybe later we can click on it and edit the properties
//
painter.getAreaOwners().add(new AreaOwner( AreaType.CUSTOM, x, y, textSize.x, textSize.y, painter.getOffset(),
Defaults.AREA_DRAWN_ENVIRONMENT_NAME, activeEnvironment));
gc.drawText( activeEnvironment, x, y, true );

// Let the world know where the name of the environment is.
// Maybe later we can click on it and edit the properties
//
painter.getAreaOwners().add( new AreaOwner( AreaType.CUSTOM, x, y, textSize.x, textSize.y, painter.getOffset(),
Defaults.AREA_DRAWN_ENVIRONMENT_NAME, activeEnvironment ) );
}
}

}

0 comments on commit d61c809

Please sign in to comment.