Search in sources :

Example 81 with ProcessingException

use of org.eclipse.scout.rt.platform.exception.ProcessingException in project scout.rt by eclipse.

the class CsvSqlAdapter method importData.

public void importData(CsvSettings params) {
    CsvHelper h = new CsvHelper(params.getContentLocale(), params.getColSeparator(), params.getTextDelimiter(), "\n");
    if (params.getCsvColumnTypes() != null) {
        h.setColumnTypes(params.getCsvColumnTypes());
    }
    if (params.getCsvColumnNames() != null) {
        h.setColumnNames(params.getCsvColumnNames());
    }
    Collection<String> cols = new ArrayList<String>();
    if (params.getGroupKeyValue() != null) {
        cols.add(params.getGroupKeyColumnName());
    }
    if (params.getLineNumberColumnName() != null) {
        cols.add(params.getLineNumberColumnName());
    }
    // 
    cols.addAll(params.getCsvColumnNames());
    StringBuilder buf = new StringBuilder();
    buf.append("INSERT INTO ");
    buf.append(params.getTableName());
    buf.append("(");
    for (Iterator<String> it = cols.iterator(); it.hasNext(); ) {
        String colName = it.next();
        if (!CsvHelper.IGNORED_COLUMN_NAME.equals(colName)) {
            buf.append(colName);
            buf.append(",");
        }
    }
    buf.deleteCharAt(buf.length() - 1);
    buf.append(") VALUES (");
    int i = 0;
    for (Iterator<String> it = cols.iterator(); it.hasNext(); ) {
        String colName = it.next();
        if (!CsvHelper.IGNORED_COLUMN_NAME.equals(colName)) {
            buf.append(":v" + i);
            buf.append(",");
            i++;
        }
    }
    buf.deleteCharAt(buf.length() - 1);
    buf.append(")");
    String stm = buf.toString();
    try (FileInputStream in = new FileInputStream(params.getFile());
        Reader reader = new BomInputStreamReader(in, params.getEncoding())) {
        SqlInsertDataConsumer cons = new SqlInsertDataConsumer(stm, params.getGroupKeyValue(), params.getLineNumberColumnName() != null);
        h.importData(cons, reader, false, false, params.getHeaderRowCount(), -1, params.getAllowVariableColumnCount());
    } catch (IOException e) {
        throw new ProcessingException(e.getMessage(), e);
    }
}
Also used : ArrayList(java.util.ArrayList) BomInputStreamReader(org.eclipse.scout.rt.platform.util.BomInputStreamReader) Reader(java.io.Reader) IOException(java.io.IOException) BomInputStreamReader(org.eclipse.scout.rt.platform.util.BomInputStreamReader) FileInputStream(java.io.FileInputStream) CsvHelper(org.eclipse.scout.rt.shared.csv.CsvHelper) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 82 with ProcessingException

use of org.eclipse.scout.rt.platform.exception.ProcessingException in project scout.rt by eclipse.

the class CsvSqlAdapter method exportDataWithSql.

/**
 * Export sql select data into a file
 *
 * @param f
 *          file to write data to
 * @param contentLocale
 *          see {@link CsvHelper}
 * @param colSeparator
 *          see {@link CsvHelper}
 * @param textDelimiter
 *          see {@link CsvHelper}
 * @param sqlSelect
 *          the source sql statement
 * @param bindBase
 *          the necessary jdbc binds
 * @param csvColumnNames
 *          the names that should appear in the csv file
 * @param writeColumnNames
 *          the sql names that are exported
 * @param csvColumnTypes
 *          the csv types of the corresponding column
 * @param writeColumnTypes
 *          the sql types of the corresponding column
 */
@SuppressWarnings("squid:S00107")
public void exportDataWithSql(File f, String encoding, Locale contentLocale, String colSeparator, String textDelimiter, String sqlSelect, NVPair[] bindBase, List<String> csvColumnNames, boolean writeColumnNames, List<String> csvColumnTypes, boolean writeColumnTypes) {
    if (encoding == null) {
        encoding = StandardCharsets.UTF_8.name();
    }
    try {
        CsvSettings params = new CsvSettings();
        params.setFile(f);
        params.setEncoding(encoding);
        params.setContentLocale(contentLocale);
        params.setColSeparator((colSeparator == null) ? 0 : colSeparator.charAt(0));
        params.setTextDelimiter((textDelimiter == null) ? 0 : textDelimiter.charAt(0));
        params.setSqlSelect(sqlSelect);
        params.setBindBase(bindBase);
        params.setWriteColumnNames(writeColumnNames);
        params.setWriteColumnTypes(writeColumnTypes);
        if (csvColumnNames != null) {
            params.setCsvColumnNames(csvColumnNames);
        }
        if (csvColumnTypes != null) {
            params.setCsvColumnTypes(csvColumnTypes);
        }
        exportData(params);
    } catch (Exception e) {
        throw new ProcessingException(e.getMessage(), e);
    }
}
Also used : CsvSettings(org.eclipse.scout.rt.server.csv.CsvSettings) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) IOException(java.io.IOException) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 83 with ProcessingException

use of org.eclipse.scout.rt.platform.exception.ProcessingException in project scout.rt by eclipse.

the class CsvSqlAdapter method exportData.

/**
 * Export sql data into a file
 *
 * @param params
 */
public void exportData(CsvSettings params) {
    final CsvHelper h = new CsvHelper(params.getContentLocale(), params.getColSeparator(), params.getTextDelimiter(), "\n");
    if (params.getCsvColumnTypes() != null) {
        h.setColumnTypes(params.getCsvColumnTypes());
    }
    if (params.getCsvColumnNames() != null) {
        h.setColumnNames(params.getCsvColumnNames());
    }
    Collection<String> cols = new ArrayList<String>();
    cols.addAll(params.getCsvColumnNames());
    // prepare select statement
    String sqlText;
    Object[] base = null;
    if (params.getSqlSelect() != null) {
        sqlText = params.getSqlSelect();
        base = params.getBindBase();
    } else {
        StringBuilder buf = new StringBuilder();
        buf.append("SELECT ");
        for (Iterator<String> it = cols.iterator(); it.hasNext(); ) {
            String colName = it.next();
            buf.append(colName);
            if (it.hasNext()) {
                buf.append(",");
            }
        }
        buf.append(" FROM ");
        buf.append(params.getTableName());
        if (params.getGroupKeyValue() != null) {
            buf.append(" WHERE ");
            buf.append(params.getGroupKeyColumnName());
            buf.append("=:groupKeyColumnValue");
        }
        if (params.getLineNumberColumnName() != null) {
            buf.append(" ORDER BY ");
            buf.append(params.getLineNumberColumnName());
        }
        sqlText = buf.toString();
        if (params.getGroupKeyValue() != null) {
            base = new Object[1];
            base[0] = new NVPair("groupKeyColumnValue", params.getGroupKeyValue());
        }
    }
    try (FileOutputStream out = new FileOutputStream(params.getFile());
        Writer w = new OutputStreamWriter(out, params.getEncoding())) {
        h.exportHeaderRows(w, params.getWriteColumnNames(), params.getWriteColumnTypes());
        ISelectStreamHandler handler = new ISelectStreamHandler() {

            @Override
            public void handleRow(Connection con, PreparedStatement stm, ResultSet rs, int rowIndex, List<SqlBind> values) {
                Object[] row = new Object[values.size()];
                for (int i = 0; i < row.length; i++) {
                    row[i] = values.get(i).getValue();
                }
                h.exportDataRow(row, w, false);
            }

            @Override
            public void finished(Connection con, PreparedStatement stm, ResultSet rs, int rowCount) {
            // do nothing
            }
        };
        m_sqlService.selectStreaming(sqlText, handler, base);
    } catch (IOException e) {
        throw new ProcessingException(e.getMessage(), e);
    }
}
Also used : ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) ResultSet(java.sql.ResultSet) NVPair(org.eclipse.scout.rt.platform.holders.NVPair) CsvHelper(org.eclipse.scout.rt.shared.csv.CsvHelper) OutputStreamWriter(java.io.OutputStreamWriter) ISelectStreamHandler(org.eclipse.scout.rt.server.jdbc.ISelectStreamHandler) ArrayList(java.util.ArrayList) List(java.util.List) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 84 with ProcessingException

use of org.eclipse.scout.rt.platform.exception.ProcessingException in project scout.rt by eclipse.

the class SqlParser method tokenizeRaw.

@SuppressWarnings("squid:S1643")
private List<IToken> tokenizeRaw(List<IToken> list, Pattern p, Class<? extends IToken> tokenType, boolean transcodeDelimiters) {
    ArrayList<IToken> newList = new ArrayList<IToken>(list.size());
    for (IToken item : list) {
        if (item instanceof Raw) {
            String s = ((Raw) item).getText();
            if (transcodeDelimiters) {
                s = encodeDelimiters(s);
            }
            // extend s to start and end with an empty text (simpler regex can be used then)
            s = " " + s + " ";
            Matcher m = p.matcher(s);
            int lastEnd = 0;
            while (lastEnd < s.length() && m.find(lastEnd)) {
                String r = s.substring(lastEnd, m.start(1));
                if (transcodeDelimiters) {
                    r = decodeDelimiters(r);
                }
                newList.add(new Raw(r.trim()));
                // 
                r = m.group(1);
                if (transcodeDelimiters) {
                    r = decodeDelimiters(r);
                }
                IToken t;
                try {
                    t = tokenType.newInstance();
                } catch (Exception e) {
                    throw new ProcessingException("Could not create new instance of {}", tokenType, e);
                }
                t.setText(r);
                newList.add(t);
                // 
                lastEnd = m.end(1);
            }
            // remaining part
            if (lastEnd < s.length()) {
                String r = s.substring(lastEnd);
                if (transcodeDelimiters) {
                    r = decodeDelimiters(r);
                }
                newList.add(new Raw(r.trim()));
            }
        } else {
            newList.add(item);
        }
    }
    return newList;
}
Also used : IToken(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.IToken) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Raw(org.eclipse.scout.rt.server.jdbc.parsers.sql.SqlParserToken.Raw) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) ParseException(java.text.ParseException) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 85 with ProcessingException

use of org.eclipse.scout.rt.platform.exception.ProcessingException in project scout.rt by eclipse.

the class ServiceOperationInvokerTest method testInvokeInvalidWithSession.

// exception is handled with JUnitExceptionHandler
@Test(expected = ProcessingException.class)
public void testInvokeInvalidWithSession() {
    String exceptionMessage = "xxx";
    when(m_pingSvc.ping(any(String.class))).thenThrow(new ProcessingException(exceptionMessage));
    ServiceTunnelResponse res = invokePingService(createRunContextWithSession());
    assertProcessingException(res, exceptionMessage);
}
Also used : StringContains.containsString(org.hamcrest.core.StringContains.containsString) ServiceTunnelResponse(org.eclipse.scout.rt.shared.servicetunnel.ServiceTunnelResponse) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) Test(org.junit.Test)

Aggregations

ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)142 IOException (java.io.IOException)48 MessagingException (javax.mail.MessagingException)21 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)17 File (java.io.File)14 VetoException (org.eclipse.scout.rt.platform.exception.VetoException)12 Folder (javax.mail.Folder)10 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)9 RemoteFile (org.eclipse.scout.rt.shared.services.common.file.RemoteFile)9 NoSuchProviderException (java.security.NoSuchProviderException)8 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)8 FileInputStream (java.io.FileInputStream)7 InputStream (java.io.InputStream)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 FileOutputStream (java.io.FileOutputStream)6 Message (javax.mail.Message)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 OutputStream (java.io.OutputStream)5 HashMap (java.util.HashMap)5