Search in sources :

Example 6 with TypedReturnCode

use of org.talend.utils.sugars.TypedReturnCode in project tdq-studio-se by Talend.

the class ColumnAnalysisSqlExecutor method getIndicatorDefinition.

/**
 * DOC talend Comment method "getIndicatorDefinition".
 *
 * @param indicator
 * @return
 */
private TypedReturnCode<IndicatorDefinition> getIndicatorDefinition(Indicator indicator) {
    TypedReturnCode<IndicatorDefinition> rt = new TypedReturnCode<IndicatorDefinition>(true);
    IndicatorDefinition indicatorDefinition;
    String label = indicator.getIndicatorDefinition().getLabel();
    if (label == null || PluginConstant.EMPTY_STRING.equals(label)) {
        indicatorDefinition = indicator.getIndicatorDefinition();
    } else {
        indicatorDefinition = DefinitionHandler.getInstance().getIndicatorDefinition(label);
    }
    // TDQ-10559: when the indicator is in reference project, the above can get NOTHING
    if (indicatorDefinition == null) {
        indicatorDefinition = indicator.getIndicatorDefinition();
        if (indicatorDefinition.eIsProxy()) {
            indicatorDefinition = (IndicatorDefinition) EObjectHelper.resolveObject(indicatorDefinition);
        }
    }
    if (indicatorDefinition == null) {
        traceError(Messages.getString("ColumnAnalysisSqlExecutor.INTERNALERROR", // $NON-NLS-1$
        AnalysisExecutorHelper.getIndicatorName(indicator)));
        rt.setOk(false);
    }
    rt.setObject(indicatorDefinition);
    return rt;
}
Also used : TypedReturnCode(org.talend.utils.sugars.TypedReturnCode) IndicatorDefinition(org.talend.dataquality.indicators.definition.IndicatorDefinition)

Example 7 with TypedReturnCode

use of org.talend.utils.sugars.TypedReturnCode in project tdq-studio-se by Talend.

the class ColumnAnalysisSqlExecutor method getColumnName.

/**
 * DOC talend Comment method "getColumnName".
 *
 * @param indicator
 * @param tdColumn
 * @return
 */
private TypedReturnCode<String> getColumnName(Indicator indicator, TdColumn tdColumn) {
    TypedReturnCode<String> rt = new TypedReturnCode<String>(true);
    String colName = getQuotedColumnName(tdColumn);
    if (!belongToSameSchemata(tdColumn)) {
        StringBuffer buf = new StringBuffer();
        for (orgomg.cwm.objectmodel.core.Package schema : schemata.values()) {
            // $NON-NLS-1$
            buf.append(schema.getName() + " ");
        }
        traceError(Messages.getString("ColumnAnalysisSqlExecutor.COLUMNNOTBELONGTOEXISTSCHEMA", colName, // $NON-NLS-1$
        buf.toString().trim()));
        rt.setOk(false);
        return rt;
    }
    colName = dbms().castColumn4ColumnAnalysisSqlExecutor(indicator, tdColumn, colName);
    rt.setObject(colName);
    return rt;
}
Also used : TypedReturnCode(org.talend.utils.sugars.TypedReturnCode) Package(orgomg.cwm.objectmodel.core.Package)

Example 8 with TypedReturnCode

use of org.talend.utils.sugars.TypedReturnCode in project tdq-studio-se by Talend.

the class ColumnSetAnalysisExecutor method runAnalysis.

/*
     * (non-Jsdoc)
     * 
     * @see org.talend.dq.analysis.AnalysisExecutor#runAnalysis(org.talend.dataquality.analysis.Analysis,
     * java.lang.String)
     */
@Override
protected boolean runAnalysis(Analysis analysis, String sqlStatement) {
    ColumnSetIndicatorEvaluator eval = createIndicatorEvaluator(analysis);
    eval.setMonitor(getMonitor());
    // --- add indicators
    EList<Indicator> indicators = analysis.getResults().getIndicators();
    for (Indicator indicator : indicators) {
        if (ColumnsetPackage.eINSTANCE.getColumnSetMultiValueIndicator().isSuperTypeOf(indicator.eClass())) {
            ColumnSetMultiValueIndicator colSetMultValIndicator = (ColumnSetMultiValueIndicator) indicator;
            colSetMultValIndicator.prepare();
            eval.storeIndicator(indicator.getName(), colSetMultValIndicator);
        }
    }
    TypedReturnCode<java.sql.Connection> connection = null;
    // MOD yyi 2011-02-22 17871:delimitefile
    if (!isDelimitedFile) {
        connection = initConnection(analysis, eval);
        if (!connection.isOk()) {
            return false;
        }
    }
    // when to close connection
    boolean closeAtTheEnd = true;
    ReturnCode rc = eval.evaluateIndicators(sqlStatement, closeAtTheEnd);
    // close connection
    if (connection != null) {
        if (POOLED_CONNECTION) {
            // release the pooled connection
            resetConnectionPool(analysis);
        } else {
            ConnectionUtils.closeConnection(connection.getObject());
        }
    }
    if (!rc.isOk()) {
        traceError(rc.getMessage());
    }
    if (getMonitor() != null) {
        getMonitor().worked(compIndicatorsWorked);
    }
    return rc.isOk();
}
Also used : ColumnSetIndicatorEvaluator(org.talend.dq.indicators.ColumnSetIndicatorEvaluator) TypedReturnCode(org.talend.utils.sugars.TypedReturnCode) ReturnCode(org.talend.utils.sugars.ReturnCode) Connection(java.sql.Connection) ColumnSetMultiValueIndicator(org.talend.dataquality.indicators.columnset.ColumnSetMultiValueIndicator) ColumnSetMultiValueIndicator(org.talend.dataquality.indicators.columnset.ColumnSetMultiValueIndicator) RegexpMatchingIndicator(org.talend.dataquality.indicators.RegexpMatchingIndicator) Indicator(org.talend.dataquality.indicators.Indicator) AllMatchIndicator(org.talend.dataquality.indicators.columnset.AllMatchIndicator)

Example 9 with TypedReturnCode

use of org.talend.utils.sugars.TypedReturnCode in project tdq-studio-se by Talend.

the class MatchAnalysisExecutor method execute.

/*
     * (non-Javadoc)
     * 
     * @see org.talend.dq.analysis.IAnalysisExecutor#execute(org.talend.dataquality.analysis.Analysis)
     */
public ReturnCode execute(Analysis analysis) {
    assert analysis != null;
    // --- preconditions
    ReturnCode rc = AnalysisExecutorHelper.check(analysis);
    if (!rc.isOk()) {
        AnalysisExecutorHelper.setExecutionInfoInAnalysisResult(analysis, rc.isOk(), rc.getMessage());
        return rc;
    }
    // --- creation time
    final long startime = AnalysisExecutorHelper.setExecutionDateInAnalysisResult(analysis);
    EList<Indicator> indicators = analysis.getResults().getIndicators();
    RecordMatchingIndicator recordMatchingIndicator = null;
    BlockKeyIndicator blockKeyIndicator = null;
    for (Indicator ind : indicators) {
        if (ind instanceof RecordMatchingIndicator) {
            recordMatchingIndicator = (RecordMatchingIndicator) ind;
        } else if (ind instanceof BlockKeyIndicator) {
            blockKeyIndicator = (BlockKeyIndicator) ind;
        }
    }
    if (recordMatchingIndicator == null || blockKeyIndicator == null) {
        rc.setOk(Boolean.FALSE);
        // $NON-NLS-1$
        rc.setMessage(Messages.getString("MatchAnalysisExecutor.noIndicators"));
        AnalysisExecutorHelper.setExecutionInfoInAnalysisResult(analysis, rc.isOk(), rc.getMessage());
        return rc;
    }
    List<ModelElement> anlayzedElements = analysis.getContext().getAnalysedElements();
    if (anlayzedElements == null || anlayzedElements.size() == 0) {
        rc.setOk(Boolean.FALSE);
        // $NON-NLS-1$
        rc.setMessage(Messages.getString("MatchAnalysisExecutor.EmptyAnalyzedElement"));
        AnalysisExecutorHelper.setExecutionInfoInAnalysisResult(analysis, rc.isOk(), rc.getMessage());
        return rc;
    }
    // TDQ-9664 msjian: check the store on disk path.
    Boolean isStoreOnDisk = TaggedValueHelper.getValueBoolean(SQLExecutor.STORE_ON_DISK_KEY, analysis);
    if (isStoreOnDisk) {
        String tempDataPath = TaggedValueHelper.getValueString(SQLExecutor.TEMP_DATA_DIR, analysis);
        File file = new File(tempDataPath);
        if (!file.exists() || !file.isDirectory()) {
            rc.setOk(Boolean.FALSE);
            // $NON-NLS-1$
            rc.setMessage(Messages.getString("MatchAnalysisExecutor.InvalidPath", file.getPath()));
            AnalysisExecutorHelper.setExecutionInfoInAnalysisResult(analysis, rc.isOk(), rc.getMessage());
            return rc;
        }
    }
    // TDQ-9664~
    Map<MetadataColumn, String> columnMap = getColumn2IndexMap(anlayzedElements);
    ISQLExecutor sqlExecutor = getSQLExectutor(analysis, recordMatchingIndicator, columnMap);
    if (sqlExecutor == null) {
        rc.setOk(Boolean.FALSE);
        // $NON-NLS-1$
        rc.setMessage(Messages.getString("MatchAnalysisExecutor.noSqlExecutor"));
        AnalysisExecutorHelper.setExecutionInfoInAnalysisResult(analysis, rc.isOk(), rc.getMessage());
        return rc;
    }
    if (getMonitor() != null) {
        getMonitor().worked(20);
    }
    // Set schema for match key.
    TypedReturnCode<MatchGroupResultConsumer> returnCode = new TypedReturnCode<MatchGroupResultConsumer>();
    MetadataColumn[] completeColumnSchema = AnalysisRecordGroupingUtils.getCompleteColumnSchema(columnMap);
    String[] colSchemaString = new String[completeColumnSchema.length];
    int idx = 0;
    for (MetadataColumn metadataCol : completeColumnSchema) {
        colSchemaString[idx++] = metadataCol.getName();
    }
    recordMatchingIndicator.setMatchRowSchema(colSchemaString);
    recordMatchingIndicator.reset();
    MatchGroupResultConsumer matchResultConsumer = createMatchGroupResultConsumer(recordMatchingIndicator);
    if (sqlExecutor.isStoreOnDisk()) {
        // need to execute the query
        try {
            sqlExecutor.executeQuery(analysis.getContext().getConnection(), analysis.getContext().getAnalysedElements());
        } catch (SQLException e) {
            log.error(e, e);
            rc.setOk(Boolean.FALSE);
            rc.setMessage(e.getMessage());
            AnalysisExecutorHelper.setExecutionInfoInAnalysisResult(analysis, rc.isOk(), rc.getMessage());
            return rc;
        }
        try {
            TypedReturnCode<Object> result = StoreOnDiskUtils.getDefault().executeWithStoreOnDisk(columnMap, recordMatchingIndicator, blockKeyIndicator, sqlExecutor.getStoreOnDiskHandler(), matchResultConsumer);
            if (result != null) {
                returnCode.setObject((MatchGroupResultConsumer) result.getObject());
                returnCode.setOk(result.isOk());
                returnCode.setMessage(result.getMessage());
            }
        } catch (Exception e) {
            log.error(e, e);
            returnCode.setMessage(e.getMessage());
            returnCode.setOk(false);
        }
    } else {
        // Added TDQ-9320 , use the result set iterator to replace the list of result in the memory.
        try {
            Iterator<Record> resultSetIterator = sqlExecutor.getResultSetIterator(analysis.getContext().getConnection(), anlayzedElements);
            BlockAndMatchManager bAndmManager = new BlockAndMatchManager(resultSetIterator, matchResultConsumer, columnMap, recordMatchingIndicator, blockKeyIndicator);
            bAndmManager.run();
        } catch (SQLException e) {
            log.error(e, e);
            rc.setOk(Boolean.FALSE);
            rc.setMessage(e.getMessage());
            AnalysisExecutorHelper.setExecutionInfoInAnalysisResult(analysis, rc.isOk(), rc.getMessage());
            return rc;
        } catch (BusinessException e) {
            log.error(e, e);
            rc.setOk(Boolean.FALSE);
            rc.setMessage(e.getMessage());
            AnalysisExecutorHelper.setExecutionInfoInAnalysisResult(analysis, rc.isOk(), rc.getMessage());
            return rc;
        }
    }
    if (!returnCode.isOk()) {
        rc.setOk(returnCode.isOk());
        rc.setMessage(returnCode.getMessage());
    }
    if (getMonitor() != null) {
        getMonitor().worked(20);
    }
    if (isLowMemory) {
        // $NON-NLS-1$
        rc.setMessage(Messages.getString("Evaluator.OutOfMomory", usedMemory));
    }
    // nodify the master page
    refreshTableWithMatchFullResult(analysis);
    // --- set metadata information of analysis
    AnalysisExecutorHelper.setExecutionInfoInAnalysisResult(analysis, rc.isOk(), rc.getMessage());
    // --- compute execution duration
    if (this.continueRun()) {
        long endtime = System.currentTimeMillis();
        final ExecutionInformations resultMetadata = analysis.getResults().getResultMetadata();
        resultMetadata.setExecutionDuration((int) (endtime - startime));
        resultMetadata.setOutThreshold(false);
    }
    if (getMonitor() != null) {
        getMonitor().worked(20);
    }
    return rc;
}
Also used : BlockKeyIndicator(org.talend.dataquality.indicators.columnset.BlockKeyIndicator) ISQLExecutor(org.talend.cwm.db.connection.ISQLExecutor) SQLException(java.sql.SQLException) ModelElement(orgomg.cwm.objectmodel.core.ModelElement) MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) BusinessException(org.talend.commons.exception.BusinessException) Record(org.talend.dataquality.matchmerge.Record) TypedReturnCode(org.talend.utils.sugars.TypedReturnCode) ReturnCode(org.talend.utils.sugars.ReturnCode) RecordMatchingIndicator(org.talend.dataquality.indicators.columnset.RecordMatchingIndicator) RecordMatchingIndicator(org.talend.dataquality.indicators.columnset.RecordMatchingIndicator) Indicator(org.talend.dataquality.indicators.Indicator) BlockKeyIndicator(org.talend.dataquality.indicators.columnset.BlockKeyIndicator) SQLException(java.sql.SQLException) BusinessException(org.talend.commons.exception.BusinessException) ExecutionInformations(org.talend.dataquality.analysis.ExecutionInformations) MatchGroupResultConsumer(org.talend.dataquality.record.linkage.grouping.MatchGroupResultConsumer) TypedReturnCode(org.talend.utils.sugars.TypedReturnCode) BlockAndMatchManager(org.talend.dq.analysis.match.BlockAndMatchManager) File(java.io.File)

Example 10 with TypedReturnCode

use of org.talend.utils.sugars.TypedReturnCode in project tdq-studio-se by Talend.

the class RowMatchingAnalysisExecutor method runAnalysis.

/*
     * (non-Javadoc)
     * 
     * @see org.talend.dq.analysis.AnalysisExecutor#runAnalysis(org.talend.dataquality.analysis.Analysis,
     * java.lang.String)
     */
@Override
protected boolean runAnalysis(Analysis analysis, String sqlStatement) {
    boolean isSuccess = Boolean.TRUE;
    TypedReturnCode<java.sql.Connection> trc = this.getConnectionBeforeRun(analysis);
    if (!trc.isOk()) {
        log.error(trc.getMessage());
        setError(trc.getMessage());
        traceError(Messages.getString("FunctionalDependencyExecutor.CANNOTEXECUTEANALYSIS", analysis.getName(), // $NON-NLS-1$
        trc.getMessage()));
        return Boolean.FALSE;
    }
    Connection connection = trc.getObject();
    try {
        // execute the sql statement for each indicator
        EList<Indicator> indicators = analysis.getResults().getIndicators();
        EList<Indicator> deactivatedIndicators = analysis.getParameters().getDeactivatedIndicators();
        for (Indicator indicator : indicators) {
            if (deactivatedIndicators.contains(indicator)) {
                // do not evaluate this indicator
                continue;
            }
            // set the connection's catalog
            if (this.catalogOrSchema != null && needChangeCatalog(connection)) {
                // check whether null argument can be given
                changeCatalog(this.catalogOrSchema, connection);
            }
            Expression query = dbms().getInstantiatedExpression(indicator);
            if (query == null) {
                traceError(// $NON-NLS-1$//$NON-NLS-2$
                "Query not executed for indicator: \"" + AnalysisExecutorHelper.getIndicatorName(indicator) + "\" " + // $NON-NLS-1$
                "query is null");
                isSuccess = Boolean.FALSE;
                continue;
            }
            try {
                Boolean isExecSuccess = executeQuery(indicator, connection, query);
                indicator.setComputed(true);
                if (!isExecSuccess) {
                    traceError(// $NON-NLS-1$//$NON-NLS-2$
                    "Query not executed for indicator: \"" + AnalysisExecutorHelper.getIndicatorName(indicator) + "\" " + "SQL query: " + // $NON-NLS-1$
                    query.getBody());
                    isSuccess = Boolean.FALSE;
                    continue;
                }
            } catch (AnalysisExecutionException e) {
                traceError(e.getMessage());
                isSuccess = Boolean.FALSE;
                continue;
            }
        }
    } finally {
        ReturnCode rc = closeConnection(analysis, connection);
        if (!rc.isOk()) {
            isSuccess = Boolean.FALSE;
        }
    }
    return isSuccess;
}
Also used : TypedReturnCode(org.talend.utils.sugars.TypedReturnCode) ReturnCode(org.talend.utils.sugars.ReturnCode) Expression(orgomg.cwm.objectmodel.core.Expression) AnalysisExecutionException(org.talend.cwm.exception.AnalysisExecutionException) Connection(java.sql.Connection) Indicator(org.talend.dataquality.indicators.Indicator) RowMatchingIndicator(org.talend.dataquality.indicators.columnset.RowMatchingIndicator)

Aggregations

TypedReturnCode (org.talend.utils.sugars.TypedReturnCode)34 ReturnCode (org.talend.utils.sugars.ReturnCode)19 Connection (org.talend.core.model.metadata.builder.connection.Connection)10 SQLException (java.sql.SQLException)7 Indicator (org.talend.dataquality.indicators.Indicator)7 ModelElement (orgomg.cwm.objectmodel.core.ModelElement)7 Connection (java.sql.Connection)6 ArrayList (java.util.ArrayList)5 IMetadataConnection (org.talend.core.model.metadata.IMetadataConnection)5 Dependency (orgomg.cwm.objectmodel.core.Dependency)5 File (java.io.File)4 DatabaseMetaData (java.sql.DatabaseMetaData)4 EObject (org.eclipse.emf.ecore.EObject)4 MetadataFillFactory (org.talend.metadata.managment.model.MetadataFillFactory)4 DatabaseConnection (org.talend.core.model.metadata.builder.connection.DatabaseConnection)3 TdColumn (org.talend.cwm.relational.TdColumn)3 Expression (orgomg.cwm.objectmodel.core.Expression)3 CSVReader (com.talend.csv.CSVReader)2 IOException (java.io.IOException)2 Date (java.util.Date)2