Search in sources :

Example 96 with KettleValueException

use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.

the class GetXMLDataStepAnalyzerTest method testGetXMLDataExternalResourceConsumer.

@Test
public void testGetXMLDataExternalResourceConsumer() throws Exception {
    GetXMLDataExternalResourceConsumer consumer = new GetXMLDataExternalResourceConsumer();
    StepMeta spyMeta = spy(new StepMeta("test", meta));
    when(meta.getParentStepMeta()).thenReturn(spyMeta);
    when(spyMeta.getParentTransMeta()).thenReturn(parentTransMeta);
    when(data.getStepMetaInterface()).thenReturn(meta);
    when(meta.isInFields()).thenReturn(false);
    String[] filePaths = { "/path/to/file1", "/another/path/to/file2" };
    when(meta.getFileName()).thenReturn(filePaths);
    when(parentTransMeta.environmentSubstitute(any(String[].class))).thenReturn(filePaths);
    assertFalse(consumer.isDataDriven(meta));
    Collection<IExternalResourceInfo> resources = consumer.getResourcesFromMeta(meta);
    assertFalse(resources.isEmpty());
    assertEquals(2, resources.size());
    when(meta.isInFields()).thenReturn(true);
    when(meta.getIsAFile()).thenReturn(true);
    assertTrue(consumer.isDataDriven(meta));
    assertTrue(consumer.getResourcesFromMeta(meta).isEmpty());
    when(rmi.getString(Mockito.any(Object[].class), anyString(), anyString())).thenReturn("/path/to/row/file");
    resources = consumer.getResourcesFromRow(data, rmi, new String[] { "id", "name" });
    assertFalse(resources.isEmpty());
    assertEquals(1, resources.size());
    when(rmi.getString(Mockito.any(Object[].class), anyString(), anyString())).thenThrow(new KettleValueException());
    resources = consumer.getResourcesFromRow(data, rmi, new String[] { "id", "name" });
    assertTrue(resources.isEmpty());
    assertEquals(GetXMLDataMeta.class, consumer.getMetaClass());
}
Also used : IExternalResourceInfo(org.pentaho.metaverse.api.model.IExternalResourceInfo) KettleValueException(org.pentaho.di.core.exception.KettleValueException) StepMeta(org.pentaho.di.trans.step.StepMeta) BaseStepMeta(org.pentaho.di.trans.step.BaseStepMeta) Test(org.junit.Test)

Example 97 with KettleValueException

use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.

the class GetXMLDataTest method checkRows.

/**
 * Check the 2 lists comparing the rows in order. If they are not the same fail the test.
 *
 * @param rows1
 *          set 1 of rows to compare
 * @param rows2
 *          set 2 of rows to compare
 */
public void checkRows(List<RowMetaAndData> rows1, List<RowMetaAndData> rows2) {
    int idx = 1;
    if (rows1.size() != rows2.size()) {
        fail("Number of rows is not the same: " + rows1.size() + " and " + rows2.size());
    }
    Iterator<RowMetaAndData> it1 = rows1.iterator();
    Iterator<RowMetaAndData> it2 = rows2.iterator();
    while (it1.hasNext() && it2.hasNext()) {
        RowMetaAndData rm1 = it1.next();
        RowMetaAndData rm2 = it2.next();
        Object[] r1 = rm1.getData();
        Object[] r2 = rm2.getData();
        if (rm1.size() != rm2.size()) {
            fail("row nr " + idx + " is not equal");
        }
        int[] fields = new int[r1.length];
        for (int ydx = 0; ydx < r1.length; ydx++) {
            fields[ydx] = ydx;
        }
        try {
            if (rm1.getRowMeta().compare(r1, r2, fields) != 0) {
                fail("row nr " + idx + " is not equal");
            }
        } catch (KettleValueException e) {
            fail("row nr " + idx + " is not equal");
        }
        idx++;
    }
}
Also used : RowMetaAndData(org.pentaho.di.core.RowMetaAndData) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Example 98 with KettleValueException

use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.

the class XsltTest method checkRows.

/**
 * Check the 2 lists comparing the rows in order. If they are not the same fail the test.
 *
 * @param rows1
 *          set 1 of rows to compare
 * @param rows2
 *          set 2 of rows to compare
 * @param fileNameColumn
 *          Number of the column containing the filename. This is only checked for being non-null (some systems maybe
 *          canonize names differently than we input).
 */
public void checkRows(List<RowMetaAndData> rows1, List<RowMetaAndData> rows2, int fileNameColumn) {
    int idx = 1;
    if (rows1.size() != rows2.size()) {
        fail("Number of rows is not the same: " + rows1.size() + " and " + rows2.size());
    }
    Iterator<RowMetaAndData> it1 = rows1.iterator();
    Iterator<RowMetaAndData> it2 = rows2.iterator();
    while (it1.hasNext() && it2.hasNext()) {
        RowMetaAndData rm1 = it1.next();
        RowMetaAndData rm2 = it2.next();
        Object[] r1 = rm1.getData();
        Object[] r2 = rm2.getData();
        if (rm1.size() != rm2.size()) {
            fail("row nr " + idx + " is not equal");
        }
        int[] fields = new int[r1.length];
        for (int ydx = 0; ydx < r1.length; ydx++) {
            fields[ydx] = ydx;
        }
        try {
            r1[fileNameColumn] = r2[fileNameColumn];
            if (rm1.getRowMeta().compare(r1, r2, fields) != 0) {
                fail("row nr " + idx + " is not equal");
            }
        } catch (KettleValueException e) {
            fail("row nr " + idx + " is not equal");
        }
        idx++;
    }
}
Also used : RowMetaAndData(org.pentaho.di.core.RowMetaAndData) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Example 99 with KettleValueException

use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.

the class TableView method sortTable.

public void sortTable(int sortField, boolean sortingDescending, boolean resetRowNums) {
    boolean shouldRefresh = false;
    if (this.sortfieldLast == -1 && this.sortingDescendingLast == null) {
        // first time through, so update
        shouldRefresh = true;
        this.sortfieldLast = this.sortfield;
        this.sortingDescendingLast = new Boolean(this.sortingDescending);
        this.sortfield = sortField;
        this.sortingDescending = sortingDescending;
    }
    if (sortfieldLast != this.sortfield) {
        this.sortfieldLast = this.sortfield;
        this.sortfield = sortField;
        shouldRefresh = true;
    }
    if (sortingDescendingLast != this.sortingDescending) {
        this.sortingDescendingLast = this.sortingDescending;
        this.sortingDescending = sortingDescending;
        shouldRefresh = true;
    }
    if (!shouldRefresh && table.getItemCount() == lastRowCount) {
        return;
    }
    removeEmptyRows();
    try {
        // First, get all info and put it in a Vector of Rows...
        TableItem[] items = table.getItems();
        List<Object[]> v = new ArrayList<Object[]>();
        // First create the row metadata for the grid
        // 
        final RowMetaInterface rowMeta = new RowMeta();
        // First values are the color name + value!
        rowMeta.addValueMeta(new ValueMetaString("colorname"));
        rowMeta.addValueMeta(new ValueMetaInteger("color"));
        for (int j = 0; j < table.getColumnCount(); j++) {
            ColumnInfo colInfo;
            if (j > 0) {
                colInfo = columns[j - 1];
            } else {
                colInfo = numberColumn;
            }
            ValueMetaInterface valueMeta = colInfo.getValueMeta();
            if (j == sortField) {
                valueMeta.setSortedDescending(sortingDescending);
            }
            rowMeta.addValueMeta(valueMeta);
        }
        final RowMetaInterface sourceRowMeta = rowMeta.cloneToType(ValueMetaInterface.TYPE_STRING);
        final RowMetaInterface conversionRowMeta = rowMeta.clone();
        // 
        for (int i = 0; i < sourceRowMeta.size(); i++) {
            ValueMetaInterface sourceValueMeta = sourceRowMeta.getValueMeta(i);
            sourceValueMeta.setStorageType(ValueMetaInterface.STORAGE_TYPE_NORMAL);
            ValueMetaInterface conversionMetaData = conversionRowMeta.getValueMeta(i);
            conversionMetaData.setStorageType(ValueMetaInterface.STORAGE_TYPE_NORMAL);
            // Meaning: this string comes from an Integer/Number/Date/etc.
            // 
            sourceRowMeta.getValueMeta(i).setConversionMetadata(conversionMetaData);
        }
        // 
        for (int i = 0; i < items.length; i++) {
            TableItem item = items[i];
            Object[] r = new Object[table.getColumnCount() + 2];
            // First values are the color name + value!
            Color bg = item.getBackground();
            if (!bg.equals(defaultBackgroundColor)) {
                String colorName = "bg " + bg.toString();
                r[0] = colorName;
                r[1] = new Long((bg.getRed() << 16) + (bg.getGreen() << 8) + (bg.getBlue()));
                // Save it in the used colors map!
                usedColors.put(colorName, bg);
            }
            for (int j = 0; j < table.getColumnCount(); j++) {
                String data = item.getText(j);
                if (GUIResource.getInstance().getColorBlue().equals(item.getForeground(j))) {
                    data = null;
                }
                ValueMetaInterface sourceValueMeta = sourceRowMeta.getValueMeta(j + 2);
                try {
                    r[j + 2] = sourceValueMeta.convertDataUsingConversionMetaData(data);
                } catch (Exception e) {
                    if (isShowingConversionErrorsInline()) {
                        r[j + 2] = Const.getStackTracker(e);
                    } else {
                        throw e;
                    }
                }
            }
            v.add(r);
        }
        final int[] sortIndex = new int[] { sortField + 2 };
        // Sort the vector!
        Collections.sort(v, new Comparator<Object[]>() {

            @Override
            public int compare(Object[] r1, Object[] r2) {
                try {
                    return conversionRowMeta.compare(r1, r2, sortIndex);
                } catch (KettleValueException e) {
                    throw new RuntimeException("Error comparing rows", e);
                }
            }
        });
        // Clear the table
        table.removeAll();
        // Refill the table
        for (int i = 0; i < v.size(); i++) {
            Object[] r = v.get(i);
            TableItem item = new TableItem(table, SWT.NONE);
            String colorName = (String) r[0];
            Long colorValue = (Long) r[1];
            if (colorValue != null) {
                // Get it from the map
                // 
                Color bg = usedColors.get(colorName);
                if (bg != null) {
                    item.setBackground(bg);
                }
            }
            for (int j = 2; j < r.length; j++) {
                String string = conversionRowMeta.getString(r, j);
                if (showingBlueNullValues && string == null) {
                    string = "<null>";
                    item.setForeground(j - 2, GUIResource.getInstance().getColorBlue());
                } else {
                    item.setForeground(j - 2, GUIResource.getInstance().getColorBlack());
                }
                if (string != null) {
                    item.setText(j - 2, string);
                }
            }
        }
        table.setSortColumn(table.getColumn(sortfield));
        table.setSortDirection(sortingDescending ? SWT.DOWN : SWT.UP);
        lastRowCount = table.getItemCount();
        if (resetRowNums) {
            setRowNums();
        }
    } catch (Exception e) {
        new ErrorDialog(this.getShell(), BaseMessages.getString(PKG, "TableView.ErrorDialog.title"), BaseMessages.getString(PKG, "TableView.ErrorDialog.description"), e);
    }
}
Also used : RowMeta(org.pentaho.di.core.row.RowMeta) TableItem(org.eclipse.swt.widgets.TableItem) ArrayList(java.util.ArrayList) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) ValueMetaInteger(org.pentaho.di.core.row.value.ValueMetaInteger) KettleValueException(org.pentaho.di.core.exception.KettleValueException) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Color(org.eclipse.swt.graphics.Color) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) Point(org.eclipse.swt.graphics.Point) SWTException(org.eclipse.swt.SWTException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Example 100 with KettleValueException

use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.

the class XMLInputSaxFieldRetriever method startElement.

// Event Handlers
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    // set the _counter level
    position[_counter + 1] += 1;
    _counter++;
    try {
        if (!rootFound) {
            XMLInputSaxFieldPosition el = null;
            try {
                el = pathToRootElement.get(counter);
            } catch (IndexOutOfBoundsException e) {
                throw new SAXException(e);
            }
            if ((counter == _counter) && qName.equalsIgnoreCase(el.getName())) {
                if (el.getType() == XMLInputSaxFieldPosition.XML_ELEMENT_ATT) {
                    // must throw exception
                    String att1 = attributes.getValue(el.getAttribute());
                    String att2 = el.getAttributeValue();
                    if (att1.equals(att2)) {
                        _pathToRootElement.add(new XMLInputSaxFieldPosition(qName, el.getAttribute(), el.getAttributeValue()));
                        // clone
                        if (counter == pathToRootElement.size() - 1) {
                            for (int i = 0; i < attributes.getLength(); i++) {
                                XMLInputSaxFieldPosition tempP = new XMLInputSaxFieldPosition(attributes.getQName(i), XMLInputSaxFieldPosition.XML_ATTRIBUTE, i + 1);
                                _pathToRootElement.add(tempP);
                                XMLInputSaxFieldPosition[] path = new XMLInputSaxFieldPosition[_pathToRootElement.size()];
                                _pathToRootElement.toArray(path);
                                _pathToRootElement.remove(_pathToRootElement.size() - 1);
                                XMLInputSaxField tempF = new XMLInputSaxField(tempP.getName(), path);
                                if (!fields.contains(tempF)) {
                                    fields.add(tempF);
                                }
                            }
                        }
                        counterUp();
                    } else {
                        _pathToRootElement.add(new XMLInputSaxFieldPosition(qName, XMLInputSaxFieldPosition.XML_ELEMENT_POS, position[_counter] + 1));
                    }
                } else {
                    _pathToRootElement.add(new XMLInputSaxFieldPosition(qName, XMLInputSaxFieldPosition.XML_ELEMENT_POS, position[_counter] + 1));
                    counterUp();
                }
            } else {
                _pathToRootElement.add(new XMLInputSaxFieldPosition(qName, XMLInputSaxFieldPosition.XML_ELEMENT_POS, position[_counter] + 1));
            }
        } else {
            XMLInputSaxField temp = null;
            if (attributes.getValue(meta.getDefiningAttribute(qName)) == null) {
                _pathToRootElement.add(new XMLInputSaxFieldPosition(qName, XMLInputSaxFieldPosition.XML_ELEMENT_POS, position[_counter] + 1));
                XMLInputSaxFieldPosition[] path = new XMLInputSaxFieldPosition[_pathToRootElement.size()];
                _pathToRootElement.toArray(path);
                temp = new XMLInputSaxField(naming(path), path);
            } else {
                String attribute = meta.getDefiningAttribute(qName);
                _pathToRootElement.add(new XMLInputSaxFieldPosition(qName, attribute, attributes.getValue(attribute)));
                XMLInputSaxFieldPosition[] path = new XMLInputSaxFieldPosition[_pathToRootElement.size()];
                _pathToRootElement.toArray(path);
                temp = new XMLInputSaxField(naming(path), path);
            }
            if (!fields.contains(temp)) {
                fields.add(temp);
            }
        }
    } catch (KettleValueException e) {
        log.logError(Const.getStackTracker(e));
        throw new SAXException(_counter + "," + counter + _pathToRootElement.get(_pathToRootElement.size() - 1).toString(), e);
    }
}
Also used : KettleValueException(org.pentaho.di.core.exception.KettleValueException) SAXException(org.xml.sax.SAXException)

Aggregations

KettleValueException (org.pentaho.di.core.exception.KettleValueException)127 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)35 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)35 KettleException (org.pentaho.di.core.exception.KettleException)25 KettleStepException (org.pentaho.di.core.exception.KettleStepException)17 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)16 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)13 IOException (java.io.IOException)12 Test (org.junit.Test)11 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)11 KettleFileException (org.pentaho.di.core.exception.KettleFileException)11 ParseException (java.text.ParseException)10 Date (java.util.Date)9 EOFException (java.io.EOFException)8 SQLException (java.sql.SQLException)8 ArrayList (java.util.ArrayList)8 Calendar (java.util.Calendar)8 KettleEOFException (org.pentaho.di.core.exception.KettleEOFException)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 BigDecimal (java.math.BigDecimal)6