Example usage for java.io FileNotFoundException equals

List of usage examples for java.io FileNotFoundException equals

Introduction

In this page you can find the example usage for java.io FileNotFoundException equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

From source file:org.seasar.dbflute.logic.replaceschema.loaddata.impl.DfDelimiterDataWriterImpl.java

public void writeData(DfDelimiterDataResultInfo resultInfo) throws IOException {
    _log.info("/= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ");
    _log.info("writeData(" + _fileName + ")");
    _log.info("= = = = = = =/");
    FileInputStream fis = null;//from  w  w  w.j  a v a2s  .co m
    InputStreamReader ir = null;
    BufferedReader br = null;

    final String dataDirectory = Srl.substringLastFront(_fileName, "/");
    final LoggingInsertType loggingInsertType = getLoggingInsertType(dataDirectory);
    final String tableDbName;
    {
        String tmp = _fileName.substring(_fileName.lastIndexOf("/") + 1, _fileName.lastIndexOf("."));
        if (tmp.indexOf("-") >= 0) {
            tmp = tmp.substring(tmp.indexOf("-") + "-".length());
        }
        tableDbName = tmp;
    }
    final Map<String, DfColumnMeta> columnInfoMap = getColumnMetaMap(tableDbName);
    if (columnInfoMap.isEmpty()) {
        throwTableNotFoundException(_fileName, tableDbName);
    }

    // process before handling table
    beforeHandlingTable(tableDbName, columnInfoMap);

    String lineString = null;
    String preContinueString = null;
    String executedSql = null;
    final List<String> columnNameList = new ArrayList<String>();
    final List<String> additionalColumnList = new ArrayList<String>();
    final List<String> valueList = new ArrayList<String>();

    final File dataFile = new File(_fileName);
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        fis = new FileInputStream(dataFile);
        ir = new InputStreamReader(fis, _encoding);
        br = new BufferedReader(ir);

        FirstLineInfo firstLineInfo = null;
        int loopIndex = -1;
        int rowNumber = 0;
        int addedBatchSize = 0;
        while (true) {
            ++loopIndex;

            lineString = br.readLine();
            if (lineString == null) {
                break;
            }

            // /- - - - - - - - - - - - - - - - - - - - - -
            // initialize column definition from first line
            // - - - - - - - - - -/
            if (loopIndex == 0) {
                firstLineInfo = getFirstLineInfo(_delimiter, lineString);
                columnNameList.addAll(firstLineInfo.getColumnNameList());
                if (columnNameList.isEmpty()) {
                    throwDelimiterDataColumnDefNotFoundException(_fileName, tableDbName);
                }
                final StringSet columnSet = StringSet.createAsFlexible();
                columnSet.addAll(columnNameList);
                for (String defaultColumn : _defaultValueMap.keySet()) {
                    if (columnSet.contains(defaultColumn)) {
                        continue;
                    }
                    additionalColumnList.add(defaultColumn);
                }
                columnNameList.addAll(additionalColumnList);
                continue;
            }

            // /- - - - - - - - - - - - - - -
            // analyze values in line strings
            // - - - - - - - - - -/
            lineString = filterLineString(lineString);
            {
                if (preContinueString != null && !preContinueString.equals("")) {
                    lineString = preContinueString + "\n" + lineString;
                }
                final ValueLineInfo valueLineInfo = arrangeValueList(lineString, _delimiter);
                final List<String> ls = valueLineInfo.getValueList();
                if (valueLineInfo.isContinueNextLine()) {
                    preContinueString = ls.remove(ls.size() - 1);
                    valueList.addAll(ls);
                    continue;
                }
                valueList.addAll(ls);
            }
            // *one record is prepared here

            // /- - - - - - - - - - - - - -
            // check definition differences
            // - - - - - - - - - -/
            if (isDifferentColumnValueCount(firstLineInfo, valueList)) {
                String msg = "The count of values wasn't correct:";
                msg = msg + " column=" + firstLineInfo.getColumnNameList().size();
                msg = msg + " value=" + valueList.size();
                msg = msg + " -> " + valueList;
                resultInfo.registerWarningFile(_fileName, msg);

                // clear temporary variables
                valueList.clear();
                preContinueString = null;
                continue;
            }
            // *valid record is prepared here
            ++rowNumber;

            // /- - - - - - - - - - - - - - - -
            // process registration to database
            // - - - - - - - - - -/
            final DfDelimiterDataWriteSqlBuilder sqlBuilder = new DfDelimiterDataWriteSqlBuilder();
            sqlBuilder.setTableDbName(tableDbName);
            sqlBuilder.setColumnInfoMap(columnInfoMap);
            sqlBuilder.setColumnNameList(columnNameList);
            sqlBuilder.setValueList(valueList);
            sqlBuilder.setNotFoundColumnMap(resultInfo.getNotFoundColumnMap());
            sqlBuilder.setConvertValueMap(_convertValueMap);
            sqlBuilder.setDefaultValueMap(_defaultValueMap);
            sqlBuilder.setBindTypeProvider(new DfColumnBindTypeProvider() {
                public Class<?> provideBindType(String tableName, DfColumnMeta columnMeta) {
                    return getBindType(tableName, columnMeta);
                }
            });
            if (conn == null) {
                conn = _dataSource.getConnection();
            }
            if (ps == null) {
                executedSql = sqlBuilder.buildSql();
                ps = conn.prepareStatement(executedSql);
            }
            final Map<String, Object> columnValueMap = sqlBuilder.setupParameter();
            handleLoggingInsert(tableDbName, columnNameList, columnValueMap, loggingInsertType, rowNumber);

            int bindCount = 1;
            final Set<Entry<String, Object>> entrySet = columnValueMap.entrySet();
            for (Entry<String, Object> entry : entrySet) {
                final String columnName = entry.getKey();
                final Object obj = entry.getValue();

                // /- - - - - - - - - - - - - - - - - -
                // process Null (against Null Headache)
                // - - - - - - - - - -/
                if (processNull(tableDbName, columnName, obj, ps, bindCount, columnInfoMap)) {
                    bindCount++;
                    continue;
                }

                // /- - - - - - - - - - - - - - -
                // process NotNull and NotString
                // - - - - - - - - - -/
                // If the value is not null and the value has the own type except string,
                // It registers the value to statement by the type.
                if (processNotNullNotString(tableDbName, columnName, obj, conn, ps, bindCount, columnInfoMap)) {
                    bindCount++;
                    continue;
                }

                // /- - - - - - - - - - - - - - - - - -
                // process NotNull and StringExpression
                // - - - - - - - - - -/
                final String value = (String) obj;
                processNotNullString(dataFile, tableDbName, columnName, value, conn, ps, bindCount,
                        columnInfoMap);
                bindCount++;
            }
            if (isMergedSuppressBatchUpdate(dataDirectory)) {
                ps.execute();
            } else {
                ps.addBatch();
                ++addedBatchSize;
                if (addedBatchSize == 100000) {
                    // this is supported in only delimiter data writer
                    // because delimiter data can treat large data
                    ps.executeBatch(); // to avoid OutOfMemory
                    ps.clearBatch(); // for next batch
                    addedBatchSize = 0;
                }
            }
            // *one record is finished here

            // clear temporary variables
            // if an exception occurs from execute() or addBatch(),
            // this valueList is to be information for debug
            valueList.clear();
            preContinueString = null;
        }
        if (ps != null && addedBatchSize > 0) {
            ps.executeBatch();
        }
        noticeLoadedRowSize(tableDbName, rowNumber);
        checkImplicitClassification(dataFile, tableDbName, columnNameList, conn);
    } catch (FileNotFoundException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    } catch (SQLException e) {
        final SQLException nextEx = e.getNextException();
        if (nextEx != null && !e.equals(nextEx)) { // focus on next exception
            _log.warn("*Failed to register: " + e.getMessage());
            String msg = buildRegExpMessage(_fileName, tableDbName, executedSql, valueList, nextEx);
            throw new DfDelimiterDataRegistrationFailureException(msg, nextEx); // switch!
        } else {
            String msg = buildRegExpMessage(_fileName, tableDbName, executedSql, valueList, e);
            throw new DfDelimiterDataRegistrationFailureException(msg, e);
        }
    } catch (RuntimeException e) {
        String msg = buildRegExpMessage(_fileName, tableDbName, executedSql, valueList, e);
        throw new DfDelimiterDataRegistrationFailureException(msg, e);
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            if (ir != null) {
                ir.close();
            }
            if (br != null) {
                br.close();
            }
        } catch (java.io.IOException ignored) {
            _log.warn("File-close threw the exception: ", ignored);
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException ignored) {
                _log.info("Statement.close() threw the exception!", ignored);
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException ignored) {
                _log.info("Connection.close() threw the exception!", ignored);
            }
        }
        // process after (finally) handling table
        finallyHandlingTable(tableDbName, columnInfoMap);
    }
}