Search in sources :

Example 91 with ReturnCode

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

the class DelimitedFileIndicatorEvaluator method executeSqlQuery.

@Override
protected ReturnCode executeSqlQuery(String sqlStatement) {
    ReturnCode returnCode = new ReturnCode(true);
    if (delimitedFileconnection == null) {
        delimitedFileconnection = (DelimitedFileConnection) analysis.getContext().getConnection();
    }
    if (delimitedFileconnection.isContextMode()) {
        IRepositoryContextService service = CoreRuntimePlugin.getInstance().getRepositoryContextService();
        delimitedFileconnection = (DelimitedFileConnection) service.cloneOriginalValueConnection(delimitedFileconnection);
    }
    String path = JavaSqlFactory.getURL(delimitedFileconnection);
    IPath iPath = new Path(path);
    File file = iPath.toFile();
    if (!file.exists()) {
        // $NON-NLS-1$
        returnCode.setReturnCode(Messages.getString("DelimitedFileIndicatorEvaluator.CanNotFindFile"), false);
        return returnCode;
    }
    List<ModelElement> analysisElementList = this.analysis.getContext().getAnalysedElements();
    EMap<Indicator, AnalyzedDataSet> indicToRowMap = analysis.getResults().getIndicToRowMap();
    indicToRowMap.clear();
    List<MetadataColumn> columnElementList = new ArrayList<MetadataColumn>();
    for (int i = 0; i < analysisElementList.size(); i++) {
        MetadataColumn mColumn = (MetadataColumn) analysisElementList.get(i);
        MetadataTable mTable = ColumnHelper.getColumnOwnerAsMetadataTable(mColumn);
        columnElementList = mTable == null ? columnElementList : mTable.getColumns();
        if (!columnElementList.isEmpty()) {
            break;
        }
    }
    ReturnCode readDataReturnCode = new ReturnCode(true);
    // use CsvReader to parse.
    if (Escape.CSV.equals(delimitedFileconnection.getEscapeType())) {
        readDataReturnCode = useCsvReader(file, analysisElementList, columnElementList, indicToRowMap);
    } else {
        readDataReturnCode = useDelimitedReader(analysisElementList, columnElementList, indicToRowMap);
    }
    // handle error message
    if (!readDataReturnCode.isOk()) {
        Display.getDefault().asyncExec(new Runnable() {

            public void run() {
                MessageDialog.openWarning(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), // $NON-NLS-1$
                Messages.getString("DelimitedFileIndicatorEvaluator.badlyForm.Title"), // $NON-NLS-1$
                Messages.getString("DelimitedFileIndicatorEvaluator.badlyForm.Message"));
            }
        });
    }
    // Added yyin 20120608 TDQ-3589
    for (MetadataColumn col : columnElementList) {
        List<Indicator> indicators = getIndicators(col.getLabel());
        for (Indicator indicator : indicators) {
            if (indicator instanceof DuplicateCountIndicator) {
                AnalyzedDataSet analyzedDataSet = indicToRowMap.get(indicator);
                if (analyzedDataSet == null) {
                    analyzedDataSet = AnalysisFactory.eINSTANCE.createAnalyzedDataSet();
                    indicToRowMap.put(indicator, analyzedDataSet);
                    analyzedDataSet.setDataCount(analysis.getParameters().getMaxNumberRows());
                    analyzedDataSet.setRecordSize(0);
                }
                // indicator.finalizeComputation();
                addResultToIndicatorToRowMap(indicator, indicToRowMap);
            }
        }
    }
    return returnCode;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) DuplicateCountIndicator(org.talend.dataquality.indicators.DuplicateCountIndicator) ReturnCode(org.talend.utils.sugars.ReturnCode) IPath(org.eclipse.core.runtime.IPath) AnalyzedDataSet(org.talend.dataquality.analysis.AnalyzedDataSet) ArrayList(java.util.ArrayList) IRepositoryContextService(org.talend.core.IRepositoryContextService) UniqueCountIndicator(org.talend.dataquality.indicators.UniqueCountIndicator) Indicator(org.talend.dataquality.indicators.Indicator) DuplicateCountIndicator(org.talend.dataquality.indicators.DuplicateCountIndicator) RowCountIndicator(org.talend.dataquality.indicators.RowCountIndicator) ModelElement(orgomg.cwm.objectmodel.core.ModelElement) MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable) File(java.io.File)

Example 92 with ReturnCode

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

the class DelimitedFileIndicatorEvaluator method handleByARow.

private ReturnCode handleByARow(String[] rowValues, long currentRow, List<ModelElement> analysisElementList, List<MetadataColumn> columnElementList, EMap<Indicator, AnalyzedDataSet> indicToRowMap) {
    ReturnCode returnCode = new ReturnCode(true);
    Object object = null;
    int maxNumberRows = analysis.getParameters().getMaxNumberRows();
    int recordIncrement = 0;
    element: for (int i = 0; i < analysisElementList.size(); i++) {
        MetadataColumn mColumn = (MetadataColumn) analysisElementList.get(i);
        Integer position = ColumnHelper.getColumnIndex(mColumn);
        // warning with a file of badly form
        if (position == null || position >= rowValues.length) {
            log.warn(// $NON-NLS-1$
            Messages.getString(// $NON-NLS-1$
            "DelimitedFileIndicatorEvaluator.incorrectData", mColumn.getLabel(), currentRow, delimitedFileconnection.getFilePath()));
            returnCode.setOk(false);
            continue;
        }
        object = TalendTypeConvert.convertToObject(mColumn.getTalendType(), rowValues[position], mColumn.getPattern());
        List<Indicator> indicators = getIndicators(mColumn.getLabel());
        for (Indicator indicator : indicators) {
            if (!continueRun()) {
                break element;
            }
            // bug 19036,to irregularly data,still compute for RowCountIndicator
            if (object == null && !(indicator instanceof RowCountIndicator)) {
                continue element;
            }
            // Added yyin 20120608 TDQ-3589
            if (indicator instanceof DuplicateCountIndicator) {
                ((DuplicateCountIndicator) indicator).handle(object, rowValues);
            } else {
                // ~
                indicator.handle(object);
            }
            AnalyzedDataSet analyzedDataSet = indicToRowMap.get(indicator);
            if (analyzedDataSet == null) {
                analyzedDataSet = AnalysisFactory.eINSTANCE.createAnalyzedDataSet();
                indicToRowMap.put(indicator, analyzedDataSet);
                analyzedDataSet.setDataCount(maxNumberRows);
                analyzedDataSet.setRecordSize(0);
            }
            // see IndicatorEvaluator line 166, the logic is almost the same
            if (analysis.getParameters().isStoreData()) {
                if (indicator.mustStoreRow()) {
                    List<Object[]> valueObjectList = initDataSet(indicator, indicToRowMap, object);
                    recordIncrement = valueObjectList.size();
                    List<Object> inputRowList = new ArrayList<Object>();
                    for (int j = 0; j < rowValues.length; j++) {
                        Object newobject = rowValues[j];
                        if (indicator.isUsedMapDBMode()) {
                            inputRowList.add(newobject == null ? PluginConstant.NULL_STRING : newobject);
                            continue;
                        } else {
                            if (recordIncrement < maxNumberRows) {
                                if (recordIncrement < valueObjectList.size()) {
                                    valueObjectList.get(recordIncrement)[j] = newobject;
                                } else {
                                    Object[] valueObject = new Object[rowValues.length];
                                    valueObject[j] = newobject;
                                    valueObjectList.add(valueObject);
                                }
                            } else {
                                break;
                            }
                        }
                    }
                    if (indicator.isUsedMapDBMode()) {
                        MapDBUtils.handleDrillDownData(object, inputRowList, indicator);
                    }
                } else if (indicator instanceof UniqueCountIndicator && analysis.getResults().getIndicToRowMap().get(indicator).getData() != null) {
                    List<Object[]> removeValueObjectList = analysis.getResults().getIndicToRowMap().get(indicator).getData();
                    if (columnElementList.size() == 0) {
                        continue;
                    }
                    int offsetting = columnElementList.indexOf(indicator.getAnalyzedElement());
                    for (Object[] dataObject : removeValueObjectList) {
                        // Added yyin 20120611 TDQ5279
                        if (object instanceof Integer) {
                            if (object.equals(Integer.parseInt((String) dataObject[offsetting]))) {
                                removeValueObjectList.remove(dataObject);
                                break;
                            }
                        }
                        // ~
                        if (dataObject[offsetting].equals(object)) {
                            removeValueObjectList.remove(dataObject);
                            break;
                        }
                    }
                }
            }
        }
    }
    return returnCode;
}
Also used : DuplicateCountIndicator(org.talend.dataquality.indicators.DuplicateCountIndicator) ReturnCode(org.talend.utils.sugars.ReturnCode) AnalyzedDataSet(org.talend.dataquality.analysis.AnalyzedDataSet) UniqueCountIndicator(org.talend.dataquality.indicators.UniqueCountIndicator) UniqueCountIndicator(org.talend.dataquality.indicators.UniqueCountIndicator) Indicator(org.talend.dataquality.indicators.Indicator) DuplicateCountIndicator(org.talend.dataquality.indicators.DuplicateCountIndicator) RowCountIndicator(org.talend.dataquality.indicators.RowCountIndicator) MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) ArrayList(java.util.ArrayList) List(java.util.List) RowCountIndicator(org.talend.dataquality.indicators.RowCountIndicator)

Example 93 with ReturnCode

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

the class ColumnSetIndicatorEvaluator method evaluateIndicators.

@Override
public ReturnCode evaluateIndicators(String sqlStatement, boolean closeConnection) {
    ReturnCode returnCode = super.evaluateIndicators(sqlStatement, closeConnection);
    storeDataSet();
    return returnCode;
}
Also used : ReturnCode(org.talend.utils.sugars.ReturnCode)

Example 94 with ReturnCode

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

the class ConnectionEvaluator method executeSqlQuery.

/*
     * (non-Javadoc)
     * 
     * @see org.talend.dq.indicators.Evaluator#executeSqlQuery(java.lang.String)
     * 
     * Note that the given statement is not used.
     */
@Override
protected ReturnCode executeSqlQuery(String sqlStatement) throws SQLException {
    // assert this.getAnalyzedElements().size() == 1 : "Invalid number of analyzed elements: "
    // + this.getAnalyzedElements().size();
    ReturnCode ok = new ReturnCode(true);
    dataProvider = this.getDataManager();
    if (this.elementToIndicators.values().isEmpty()) {
        // $NON-NLS-1$
        String msg = Messages.getString("Evaluator.NoInidcator1");
        log.error(msg);
        ok.setReturnCode(msg, false);
        return ok;
    }
    elementIndics = this.elementToIndicators.values().iterator().next();
    if (elementIndics.isEmpty()) {
        // $NON-NLS-1$
        String msg = Messages.getString("Evaluator.NoInidcator2", dataProvider);
        log.error(msg);
        ok.setReturnCode(msg, false);
        return ok;
    }
    ConnectionIndicator connectionIndicator = getConnectionIndicator();
    this.resetCounts(connectionIndicator);
    List<Catalog> catalogs = ConnectionHelper.getCatalogs(dataProvider);
    if (isTos(dataProvider)) {
        cleanUpCatalog(catalogs);
    }
    if (this.getMonitor() != null) {
        this.getMonitor().beginTask("Analyze catalogs", 100);
    }
    int temp = 0;
    if (catalogs.isEmpty()) {
        // no catalog, only schemata
        List<Schema> schemata = ConnectionHelper.getSchema(dataProvider);
        // MOD yyi 2009-11-30 10187
        for (Schema tdSchema : schemata) {
            if (!checkSchema(tdSchema)) {
                // $NON-NLS-1$
                ok.setReturnCode(Messages.getString("Evaluator.schemaNotExist", tdSchema.getName()), false);
                return ok;
            }
        }
        // for (Schema tdSchema : schemata) {
        for (int i = 0; i < schemata.size(); i++) {
            Schema tdSchema = schemata.get(i);
            if (this.getMonitor() != null) {
                this.getMonitor().setTaskName(Messages.getString("ColumnAnalysisSqlExecutor.AnalyzedElement", Messages.getString("ColumnAnalysisSqlExecutor.AnalyzedElementSchema", tdSchema.getName())));
            }
            evalSchemaIndic(tdSchema, ok);
            if (this.getMonitor() != null) {
                int current = (i + 1) * 100 / schemata.size();
                if (current > temp) {
                    this.getMonitor().worked(current - temp);
                    temp = current;
                }
            }
        }
    } else {
        // MOD yyi 2009-11-30 10187
        for (Catalog tdCatalog : catalogs) {
            if (!checkCatalog(tdCatalog.getName())) {
                // $NON-NLS-1$
                ok.setReturnCode(Messages.getString("Evaluator.catalogNotExist", tdCatalog.getName()), false);
                return ok;
            }
        }
        // for (Catalog tdCatalog : catalogs) {
        for (int i = 0; i < catalogs.size(); i++) {
            if (this.continueRun()) {
                Catalog tdCatalog = catalogs.get(i);
                String catName = tdCatalog.getName();
                if (this.getMonitor() != null) {
                    this.getMonitor().setTaskName(Messages.getString("ColumnAnalysisSqlExecutor.AnalyzedElement", Messages.getString("ColumnAnalysisSqlExecutor.AnalyzedElementCatalog", catName)));
                }
                if (dbms().supportCatalogSelection()) {
                    try {
                        connection.setCatalog(catName);
                    } catch (SQLException e) {
                        // $NON-NLS-1$
                        log.warn("Exception while executing SQL query " + sqlStatement, e);
                    }
                }
                CatalogIndicator catalogIndic = SchemaFactory.eINSTANCE.createCatalogIndicator();
                // MOD xqliu 2009-1-21 feature 4715
                DefinitionHandler.getInstance().setDefaultIndicatorDefinition(catalogIndic);
                List<Schema> schemas = CatalogHelper.getSchemas(tdCatalog);
                if (schemas.isEmpty()) {
                    // no schema
                    evalCatalogIndic(catalogIndic, tdCatalog, ok);
                } else {
                    catalogIndic.setAnalyzedElement(tdCatalog);
                    // --- create SchemaIndicator for each pair of catalog schema
                    for (Schema tdSchema : schemas) {
                        if (this.continueRun()) {
                            if (this.getMonitor() != null) {
                                this.getMonitor().setTaskName(Messages.getString("ColumnAnalysisSqlExecutor.AnalyzedElement", Messages.getString("ColumnAnalysisSqlExecutor.AnalyzedElementCatalog", catName) + ", " + Messages.getString("ColumnAnalysisSqlExecutor.AnalyzedElementSchema", tdSchema.getName())));
                            }
                            // --- create SchemaIndicator for each catalog
                            SchemaIndicator schemaIndic = SchemaFactory.eINSTANCE.createSchemaIndicator();
                            // MOD xqliu 2009-1-21 feature 4715
                            DefinitionHandler.getInstance().setDefaultIndicatorDefinition(schemaIndic);
                            evalSchemaIndicLow(catalogIndic, schemaIndic, tdCatalog, tdSchema, ok);
                        }
                    }
                    catalogIndic.setSchemaCount(schemas.size());
                }
                if (this.getMonitor() != null) {
                    int current = (i + 1) * 100 / catalogs.size();
                    if (current > temp) {
                        this.getMonitor().worked(current - temp);
                        temp = current;
                    }
                }
            }
        }
        if (this.getMonitor() != null) {
            this.getMonitor().done();
        }
    }
    if (log.isDebugEnabled()) {
        printCounts(connectionIndicator);
    }
    return ok;
}
Also used : SchemaIndicator(org.talend.dataquality.indicators.schema.SchemaIndicator) ReturnCode(org.talend.utils.sugars.ReturnCode) SQLException(java.sql.SQLException) Schema(orgomg.cwm.resource.relational.Schema) CatalogIndicator(org.talend.dataquality.indicators.schema.CatalogIndicator) ConnectionIndicator(org.talend.dataquality.indicators.schema.ConnectionIndicator) Catalog(orgomg.cwm.resource.relational.Catalog)

Example 95 with ReturnCode

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

the class ReportFileHelper method deleteRepOutputFolder.

/**
 * delete the related output folder of report.
 *
 * @param reportFile
 * @throws PersistenceException
 */
public static ReturnCode deleteRepOutputFolder(final IFile reportFile) throws PersistenceException {
    final ReturnCode rc = new ReturnCode(Boolean.TRUE);
    RepositoryWorkUnit repositoryWorkUnit = new RepositoryWorkUnit(ProjectManager.getInstance().getCurrentProject(), // $NON-NLS-1$
    "deleteRepOutputFolder") {

        @Override
        protected void run() throws LoginException, PersistenceException {
            IFolder reportOutputFolder = ReportFileHelper.getOutputFolder(reportFile);
            if (reportOutputFolder != null && reportOutputFolder.exists()) {
                try {
                    IContainer parent = reportOutputFolder.getParent();
                    // refresh the parent at begin
                    if (parent != null) {
                        parent.refreshLocal(IResource.DEPTH_INFINITE, null);
                    }
                    // delete folder
                    File file = WorkspaceUtils.ifolderToFile(reportOutputFolder);
                    if (file != null && file.exists()) {
                        deleteFolder(file);
                    }
                    // refresh the parent at end
                    if (parent != null) {
                        parent.refreshLocal(IResource.DEPTH_INFINITE, null);
                    }
                } catch (CoreException e) {
                    log.warn(e);
                }
            } else {
                rc.setOk(Boolean.FALSE);
            }
        }
    };
    repositoryWorkUnit.setAvoidUnloadResources(true);
    ProxyRepositoryFactory.getInstance().executeRepositoryWorkUnit(repositoryWorkUnit);
    repositoryWorkUnit.throwPersistenceExceptionIfAny();
    return rc;
}
Also used : ReturnCode(org.talend.utils.sugars.ReturnCode) CoreException(org.eclipse.core.runtime.CoreException) RepositoryWorkUnit(org.talend.repository.RepositoryWorkUnit) IContainer(org.eclipse.core.resources.IContainer) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IFolder(org.eclipse.core.resources.IFolder)

Aggregations

ReturnCode (org.talend.utils.sugars.ReturnCode)175 ArrayList (java.util.ArrayList)42 TypedReturnCode (org.talend.utils.sugars.TypedReturnCode)42 Test (org.junit.Test)29 File (java.io.File)26 Connection (org.talend.core.model.metadata.builder.connection.Connection)26 Indicator (org.talend.dataquality.indicators.Indicator)25 ModelElement (orgomg.cwm.objectmodel.core.ModelElement)20 DatabaseConnection (org.talend.core.model.metadata.builder.connection.DatabaseConnection)18 Analysis (org.talend.dataquality.analysis.Analysis)18 Property (org.talend.core.model.properties.Property)17 IFile (org.eclipse.core.resources.IFile)16 IRepositoryNode (org.talend.repository.model.IRepositoryNode)16 IFolder (org.eclipse.core.resources.IFolder)15 SQLException (java.sql.SQLException)14 PersistenceException (org.talend.commons.exception.PersistenceException)11 TdColumn (org.talend.cwm.relational.TdColumn)11 IOException (java.io.IOException)10 EObject (org.eclipse.emf.ecore.EObject)10 IMetadataConnection (org.talend.core.model.metadata.IMetadataConnection)10