Search in sources :

Example 1 with UserDefinedJavaClassMeta

use of org.pentaho.di.trans.steps.userdefinedjavaclass.UserDefinedJavaClassMeta in project pentaho-kettle by pentaho.

the class UserDefinedJavaClassDialog method createPlugin.

protected boolean createPlugin() {
    // Create a step with the information in this dialog
    UserDefinedJavaClassMeta udjcMeta = new UserDefinedJavaClassMeta();
    getInfo(udjcMeta);
    try {
        String pluginName = "Processor";
        for (UserDefinedJavaClassDef def : udjcMeta.getDefinitions()) {
            if (def.isTransformClass()) {
                pluginName = def.getClassName();
            }
        }
        File pluginFile = new File(String.format("plugins/steps/%s/%s.step.xml", pluginName, pluginName));
        pluginFile.getParentFile().mkdirs();
        PrintWriter pw = new PrintWriter(new FileWriter(pluginFile));
        StringBuilder outXML = new StringBuilder("<step>\n");
        outXML.append(String.format("\t<name>%s</name>\n", stepname));
        outXML.append("\t<type>UserDefinedJavaClass</type>\n");
        outXML.append("\t<description/>\n\t");
        outXML.append(udjcMeta.getXML());
        outXML.append("</step>");
        pw.println(outXML.toString());
        pw.flush();
        pw.close();
        ShowMessageDialog msgDialog = new ShowMessageDialog(shell, SWT.ICON_INFORMATION | SWT.OK, BaseMessages.getString(PKG, "UserDefinedJavaClassDialog.Plugin.CreateSuccess"), BaseMessages.getString(PKG, "UserDefinedJavaClassDialog.Plugin.CreatedFile", pluginFile.getPath()), false);
        msgDialog.open();
    } catch (IOException e) {
        e.printStackTrace();
        new ErrorDialog(shell, BaseMessages.getString(PKG, "UserDefinedJavaClassDialog.Plugin.CreateErrorTitle"), BaseMessages.getString(PKG, "UserDefinedJavaClassDialog.Plugin.CreateErrorMessage", stepname), e);
    }
    return true;
}
Also used : FileWriter(java.io.FileWriter) ShowMessageDialog(org.pentaho.di.ui.core.dialog.ShowMessageDialog) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) UserDefinedJavaClassMeta(org.pentaho.di.trans.steps.userdefinedjavaclass.UserDefinedJavaClassMeta) UserDefinedJavaClassDef(org.pentaho.di.trans.steps.userdefinedjavaclass.UserDefinedJavaClassDef) IOException(java.io.IOException) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 2 with UserDefinedJavaClassMeta

use of org.pentaho.di.trans.steps.userdefinedjavaclass.UserDefinedJavaClassMeta in project pentaho-kettle by pentaho.

the class TransMetaTest method testGetPrevInfoFields.

@Test
public void testGetPrevInfoFields() throws KettleStepException {
    DataGridMeta dgm1 = new DataGridMeta();
    dgm1.setFieldName(new String[] { "id", "colA" });
    dgm1.allocate(2);
    dgm1.setFieldType(new String[] { ValueMetaFactory.getValueMetaName(ValueMetaInterface.TYPE_INTEGER), ValueMetaFactory.getValueMetaName(ValueMetaInterface.TYPE_STRING) });
    List<List<String>> dgm1Data = new ArrayList<>();
    dgm1Data.add(asList("1", "A"));
    dgm1Data.add(asList("2", "B"));
    dgm1.setDataLines(dgm1Data);
    DataGridMeta dgm2 = new DataGridMeta();
    dgm2.allocate(1);
    dgm2.setFieldName(new String[] { "moreData" });
    dgm2.setFieldType(new String[] { ValueMetaFactory.getValueMetaName(ValueMetaInterface.TYPE_STRING) });
    List<List<String>> dgm2Data = new ArrayList<>();
    dgm2Data.add(asList("Some Informational Data"));
    dgm2.setDataLines(dgm2Data);
    StepMeta dg1 = new StepMeta("input1", dgm1);
    StepMeta dg2 = new StepMeta("input2", dgm2);
    final String UDJC_METHOD = "public boolean processRow( StepMetaInterface smi, StepDataInterface sdi ) throws KettleException { return " + "false; }";
    UserDefinedJavaClassMeta udjcMeta = new UserDefinedJavaClassMeta();
    udjcMeta.getInfoStepDefinitions().add(new InfoStepDefinition(dg2.getName(), dg2.getName(), dg2, "info_data"));
    udjcMeta.replaceDefinitions(singletonList(new UserDefinedJavaClassDef(UserDefinedJavaClassDef.ClassType.TRANSFORM_CLASS, "MainClass", UDJC_METHOD)));
    StepMeta udjc = new StepMeta("PDI-14910", udjcMeta);
    TransHopMeta hop1 = new TransHopMeta(dg1, udjc, true);
    TransHopMeta hop2 = new TransHopMeta(dg2, udjc, true);
    transMeta.addStep(dg1);
    transMeta.addStep(dg2);
    transMeta.addStep(udjc);
    transMeta.addTransHop(hop1);
    transMeta.addTransHop(hop2);
    RowMetaInterface row = null;
    row = transMeta.getPrevInfoFields(udjc);
    assertNotNull(row);
    assertEquals(1, row.size());
    assertEquals("moreData", row.getValueMeta(0).getName());
    assertEquals(ValueMetaInterface.TYPE_STRING, row.getValueMeta(0).getType());
}
Also used : InfoStepDefinition(org.pentaho.di.trans.steps.userdefinedjavaclass.InfoStepDefinition) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) UserDefinedJavaClassMeta(org.pentaho.di.trans.steps.userdefinedjavaclass.UserDefinedJavaClassMeta) UserDefinedJavaClassDef(org.pentaho.di.trans.steps.userdefinedjavaclass.UserDefinedJavaClassDef) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Mockito.anyString(org.mockito.Mockito.anyString) DataGridMeta(org.pentaho.di.trans.steps.datagrid.DataGridMeta) StepMeta(org.pentaho.di.trans.step.StepMeta) Test(org.junit.Test)

Example 3 with UserDefinedJavaClassMeta

use of org.pentaho.di.trans.steps.userdefinedjavaclass.UserDefinedJavaClassMeta in project pentaho-kettle by pentaho.

the class UserDefinedJavaClassDialog method test.

private boolean test() {
    PluginRegistry registry = PluginRegistry.getInstance();
    String scriptStepName = wStepname.getText();
    if (!checkForTransformClass()) {
        return false;
    }
    // Create a step with the information in this dialog
    UserDefinedJavaClassMeta udjcMeta = new UserDefinedJavaClassMeta();
    getInfo(udjcMeta);
    try {
        // First, before we get into the trial run, just see if the classes
        // all compile.
        udjcMeta.cookClasses();
        if (udjcMeta.cookErrors.size() == 1) {
            Exception e = udjcMeta.cookErrors.get(0);
            new ErrorDialog(shell, "Error during class compilation", e.toString(), e);
            return false;
        } else if (udjcMeta.cookErrors.size() > 1) {
            Exception e = udjcMeta.cookErrors.get(0);
            new ErrorDialog(shell, "Errors during class compilation", String.format("Multiple errors during class compilation. First error:\n%s", e.toString()), e);
            return false;
        }
        // What fields are coming into the step?
        RowMetaInterface rowMeta = transMeta.getPrevStepFields(stepname).clone();
        if (rowMeta != null) {
            // time
            if (genMeta == null) {
                genMeta = new RowGeneratorMeta();
                genMeta.setRowLimit("10");
                genMeta.allocate(rowMeta.size());
                // CHECKSTYLE:Indentation:OFF
                for (int i = 0; i < rowMeta.size(); i++) {
                    ValueMetaInterface valueMeta = rowMeta.getValueMeta(i);
                    if (valueMeta.isStorageBinaryString()) {
                        valueMeta.setStorageType(ValueMetaInterface.STORAGE_TYPE_NORMAL);
                    }
                    genMeta.getFieldName()[i] = valueMeta.getName();
                    genMeta.getFieldType()[i] = valueMeta.getTypeDesc();
                    genMeta.getFieldLength()[i] = valueMeta.getLength();
                    genMeta.getFieldPrecision()[i] = valueMeta.getPrecision();
                    genMeta.getCurrency()[i] = valueMeta.getCurrencySymbol();
                    genMeta.getDecimal()[i] = valueMeta.getDecimalSymbol();
                    genMeta.getGroup()[i] = valueMeta.getGroupingSymbol();
                    String string = null;
                    switch(valueMeta.getType()) {
                        case ValueMetaInterface.TYPE_DATE:
                            genMeta.getFieldFormat()[i] = "yyyy/MM/dd HH:mm:ss";
                            valueMeta.setConversionMask(genMeta.getFieldFormat()[i]);
                            string = valueMeta.getString(new Date());
                            break;
                        case ValueMetaInterface.TYPE_STRING:
                            string = "test value test value";
                            break;
                        case ValueMetaInterface.TYPE_INTEGER:
                            genMeta.getFieldFormat()[i] = "#";
                            valueMeta.setConversionMask(genMeta.getFieldFormat()[i]);
                            string = valueMeta.getString(Long.valueOf(0L));
                            break;
                        case ValueMetaInterface.TYPE_NUMBER:
                            genMeta.getFieldFormat()[i] = "#.#";
                            valueMeta.setConversionMask(genMeta.getFieldFormat()[i]);
                            string = valueMeta.getString(Double.valueOf(0.0D));
                            break;
                        case ValueMetaInterface.TYPE_BIGNUMBER:
                            genMeta.getFieldFormat()[i] = "#.#";
                            valueMeta.setConversionMask(genMeta.getFieldFormat()[i]);
                            string = valueMeta.getString(BigDecimal.ZERO);
                            break;
                        case ValueMetaInterface.TYPE_BOOLEAN:
                            string = valueMeta.getString(Boolean.TRUE);
                            break;
                        case ValueMetaInterface.TYPE_BINARY:
                            string = valueMeta.getString(new byte[] { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74 });
                            break;
                        default:
                            break;
                    }
                    genMeta.getValue()[i] = string;
                }
            }
            StepMeta genStep = new StepMeta(registry.getPluginId(StepPluginType.class, genMeta), "## TEST DATA ##", genMeta);
            genStep.setLocation(50, 50);
            StepMeta scriptStep = new StepMeta(registry.getPluginId(StepPluginType.class, udjcMeta), Const.NVL(scriptStepName, "## SCRIPT ##"), udjcMeta);
            scriptStepName = scriptStep.getName();
            scriptStep.setLocation(150, 50);
            // Create a hop between both steps...
            // 
            TransHopMeta hop = new TransHopMeta(genStep, scriptStep);
            // Generate a new test transformation...
            // 
            TransMeta transMeta = new TransMeta();
            transMeta.setName(wStepname.getText() + " - PREVIEW");
            transMeta.addStep(genStep);
            transMeta.addStep(scriptStep);
            transMeta.addTransHop(hop);
            // 
            if (Spoon.getInstance().editStep(transMeta, genStep) != null) {
                // Now run this transformation and grab the results...
                // 
                TransPreviewProgressDialog progressDialog = new TransPreviewProgressDialog(shell, transMeta, new String[] { scriptStepName }, new int[] { Const.toInt(genMeta.getRowLimit(), 10) });
                progressDialog.open();
                Trans trans = progressDialog.getTrans();
                String loggingText = progressDialog.getLoggingText();
                if (!progressDialog.isCancelled()) {
                    if (trans.getResult() != null && trans.getResult().getNrErrors() > 0) {
                        EnterTextDialog etd = new EnterTextDialog(shell, BaseMessages.getString("System.Dialog.PreviewError.Title"), BaseMessages.getString("System.Dialog.PreviewError.Message"), loggingText, true);
                        etd.setReadOnly();
                        etd.open();
                    }
                }
                RowMetaInterface previewRowsMeta = progressDialog.getPreviewRowsMeta(wStepname.getText());
                List<Object[]> previewRows = progressDialog.getPreviewRows(wStepname.getText());
                if (previewRowsMeta != null && previewRows != null && previewRows.size() > 0) {
                    PreviewRowsDialog prd = new PreviewRowsDialog(shell, transMeta, SWT.NONE, wStepname.getText(), previewRowsMeta, previewRows, loggingText);
                    prd.open();
                }
            }
            return true;
        } else {
            throw new KettleException(BaseMessages.getString(PKG, "UserDefinedJavaClassDialog.Exception.CouldNotGetFields"));
        }
    } catch (Exception e) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "UserDefinedJavaClassDialog.TestFailed.DialogTitle"), BaseMessages.getString(PKG, "UserDefinedJavaClassDialog.TestFailed.DialogMessage"), e);
        return false;
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) TransPreviewProgressDialog(org.pentaho.di.ui.trans.dialog.TransPreviewProgressDialog) TransMeta(org.pentaho.di.trans.TransMeta) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) UserDefinedJavaClassMeta(org.pentaho.di.trans.steps.userdefinedjavaclass.UserDefinedJavaClassMeta) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) PreviewRowsDialog(org.pentaho.di.ui.core.dialog.PreviewRowsDialog) RowGeneratorMeta(org.pentaho.di.trans.steps.rowgenerator.RowGeneratorMeta) StepMeta(org.pentaho.di.trans.step.StepMeta) BaseStepMeta(org.pentaho.di.trans.step.BaseStepMeta) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettleException(org.pentaho.di.core.exception.KettleException) IOException(java.io.IOException) Point(org.eclipse.swt.graphics.Point) Date(java.util.Date) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) StepPluginType(org.pentaho.di.core.plugins.StepPluginType) PluginRegistry(org.pentaho.di.core.plugins.PluginRegistry) EnterTextDialog(org.pentaho.di.ui.core.dialog.EnterTextDialog) TransHopMeta(org.pentaho.di.trans.TransHopMeta) Trans(org.pentaho.di.trans.Trans)

Aggregations

UserDefinedJavaClassMeta (org.pentaho.di.trans.steps.userdefinedjavaclass.UserDefinedJavaClassMeta)3 IOException (java.io.IOException)2 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)2 StepMeta (org.pentaho.di.trans.step.StepMeta)2 UserDefinedJavaClassDef (org.pentaho.di.trans.steps.userdefinedjavaclass.UserDefinedJavaClassDef)2 ErrorDialog (org.pentaho.di.ui.core.dialog.ErrorDialog)2 File (java.io.File)1 FileWriter (java.io.FileWriter)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.singletonList (java.util.Collections.singletonList)1 Date (java.util.Date)1 List (java.util.List)1 Point (org.eclipse.swt.graphics.Point)1 Test (org.junit.Test)1 Mockito.anyString (org.mockito.Mockito.anyString)1 KettleException (org.pentaho.di.core.exception.KettleException)1 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)1