Search in sources :

Example 6 with DelimitedFileConnection

use of org.talend.core.model.metadata.builder.connection.DelimitedFileConnection in project tdq-studio-se by Talend.

the class ColumnSetIndicatorEvaluator method evaluateByDelimitedFile.

/**
 * orgnize EList 'objectLs' for DelimitedFile connection.
 *
 * @param sqlStatement
 * @param returnCode
 * @return
 */
private ReturnCode evaluateByDelimitedFile(String sqlStatement, ReturnCode returnCode) {
    DelimitedFileConnection fileConnection = (DelimitedFileConnection) analysis.getContext().getConnection();
    String path = JavaSqlFactory.getURL(fileConnection);
    String rowSeparator = JavaSqlFactory.getRowSeparatorValue(fileConnection);
    IPath iPath = new Path(path);
    File file = iPath.toFile();
    if (!file.exists()) {
        // $NON-NLS-1$
        returnCode.setReturnCode(Messages.getString("ColumnSetIndicatorEvaluator.FileNotFound", file.getName()), false);
        return returnCode;
    }
    CSVReader csvReader = null;
    try {
        List<ModelElement> analysisElementList = this.analysis.getContext().getAnalysedElements();
        EMap<Indicator, AnalyzedDataSet> indicToRowMap = analysis.getResults().getIndicToRowMap();
        indicToRowMap.clear();
        if (Escape.CSV.equals(fileConnection.getEscapeType())) {
            // use CsvReader to parse.
            csvReader = FileUtils.createCsvReader(file, fileConnection);
            this.useCsvReader(csvReader, file, fileConnection, analysisElementList);
        } else {
            // use TOSDelimitedReader in FileInputDelimited to parse.
            FileInputDelimited fileInputDelimited = AnalysisExecutorHelper.createFileInputDelimited(fileConnection);
            long currentRow = JavaSqlFactory.getHeadValue(fileConnection);
            int columsCount = 0;
            while (fileInputDelimited.nextRecord()) {
                if (!continueRun()) {
                    break;
                }
                currentRow++;
                if (columsCount == 0) {
                    columsCount = fileInputDelimited.getColumnsCountOfCurrentRow();
                }
                String[] rowValues = new String[columsCount];
                for (int i = 0; i < columsCount; i++) {
                    rowValues[i] = fileInputDelimited.get(i);
                }
                orgnizeObjectsToHandel(path, rowValues, currentRow, analysisElementList, rowSeparator);
            }
            // TDQ-5851~
            fileInputDelimited.close();
        }
    } catch (Exception e) {
        log.error(e, e);
        returnCode.setReturnCode(e.getMessage(), false);
    } finally {
        if (csvReader != null) {
            try {
                csvReader.close();
            } catch (IOException e) {
                log.error(e, e);
            }
        }
    }
    return returnCode;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) CSVReader(com.talend.csv.CSVReader) AnalyzedDataSet(org.talend.dataquality.analysis.AnalyzedDataSet) DelimitedFileConnection(org.talend.core.model.metadata.builder.connection.DelimitedFileConnection) IOException(java.io.IOException) UniqueCountIndicator(org.talend.dataquality.indicators.UniqueCountIndicator) AllMatchIndicator(org.talend.dataquality.indicators.columnset.AllMatchIndicator) RowCountIndicator(org.talend.dataquality.indicators.RowCountIndicator) Indicator(org.talend.dataquality.indicators.Indicator) DistinctCountIndicator(org.talend.dataquality.indicators.DistinctCountIndicator) SimpleStatIndicator(org.talend.dataquality.indicators.columnset.SimpleStatIndicator) DuplicateCountIndicator(org.talend.dataquality.indicators.DuplicateCountIndicator) SQLException(java.sql.SQLException) IOException(java.io.IOException) ModelElement(orgomg.cwm.objectmodel.core.ModelElement) FileInputDelimited(org.talend.fileprocess.FileInputDelimited) File(java.io.File)

Example 7 with DelimitedFileConnection

use of org.talend.core.model.metadata.builder.connection.DelimitedFileConnection in project tdq-studio-se by Talend.

the class TreeMessageInfoDialog method getLabelProvider.

protected ILabelProvider getLabelProvider() {
    if (labelProvider == null) {
        labelProvider = new LabelProvider() {

            @Override
            public String getText(Object obj) {
                if (obj == null) {
                    // $NON-NLS-1$
                    return "";
                }
                if (obj instanceof ImpactNode) {
                    return ((ImpactNode) obj).toString();
                } else if (obj instanceof IFile) {
                    IFile file = (IFile) obj;
                    ModelElement modelElement = ModelElementFileFactory.getModelElement(file);
                    // MOD msjian TDQ-5909: modify to displayName
                    return modelElement != null ? PropertyHelper.getProperty(modelElement).getDisplayName() : file.getName();
                } else if (obj instanceof RepositoryViewObject) {
                    // Added 20130226 TDQ-6899 show the name for Jrxml object (which has no related ModelElement)
                    return ((IRepositoryViewObject) obj).getLabel();
                } else if (obj instanceof MetadataTable) {
                    // ADD TDQ-7146: for all type's connection table/view node
                    return ((ModelElement) obj).getName();
                }
                Property property = PropertyHelper.getProperty((ModelElement) obj);
                return property == null ? ((ModelElement) obj).getName() : property.getDisplayName();
            }

            @Override
            public Image getImage(Object obj) {
                ModelElement modelElement = null;
                if (obj instanceof ModelElement) {
                    modelElement = (ModelElement) obj;
                } else if (obj instanceof ImpactNode) {
                    modelElement = ((ImpactNode) obj).getNodeElement();
                } else if (obj instanceof IFile) {
                    modelElement = ModelElementFileFactory.getModelElement((IFile) obj);
                } else if (obj instanceof RepositoryViewObject) {
                    // Added 20130226 TDQ-6899 show the name for Jrxml object (which has no related ModelElement)
                    return ImageLib.getImage(ImageLib.JRXML_ICON);
                }
                // ~
                if (modelElement == null) {
                    if (((ImpactNode) obj).getNode() != null) {
                        return ImageLib.getImage(ImageLib.JRXML_ICON);
                    }
                    return super.getImage(obj);
                }
                Image modelElementImage = null;
                String imgName = null;
                if (modelElement instanceof Analysis) {
                    imgName = ImageLib.ANALYSIS_OBJECT;
                } else if (modelElement instanceof TdReport) {
                    imgName = ImageLib.REPORT_OBJECT;
                } else if (modelElement instanceof DatabaseConnection) {
                    imgName = ImageLib.CONNECTION;
                } else if (modelElement instanceof DelimitedFileConnection) {
                    imgName = ImageLib.FILE_DELIMITED;
                } else if (modelElement instanceof Pattern) {
                    imgName = ImageLib.PATTERN_REG;
                } else if (modelElement instanceof IndicatorDefinition) {
                    // IndicatorDefinition
                    if (modelElement instanceof DQRule) {
                        imgName = ImageLib.DQ_RULE;
                    } else if (modelElement instanceof MatchRuleDefinition) {
                        imgName = ImageLib.MATCH_RULE_ICON;
                    } else {
                        imgName = ImageLib.IND_DEFINITION;
                    }
                // TDQ-8550~
                } else if (modelElement instanceof MetadataTable) {
                    imgName = ImageLib.TABLE;
                }
                if (imgName != null) {
                    modelElementImage = ImageLib.getImage(imgName);
                }
                // add lock icon on the image
                if (modelElementImage != null) {
                    if (modelElement != null) {
                        Property property = PropertyHelper.getProperty(modelElement);
                        if (property != null) {
                            Item item = property.getItem();
                            if (item != null) {
                                if (ProxyRepositoryManager.getInstance().isLockByUserOwn(item)) {
                                    modelElementImage = ImageLib.createLockedByOwnIcon(imgName);
                                } else if (ProxyRepositoryManager.getInstance().isLockByOthers(item)) {
                                    modelElementImage = ImageLib.createLockedByOtherIcon(imgName);
                                }
                            }
                        }
                    }
                    return modelElementImage;
                }
                return super.getImage(obj);
            }
        };
    }
    return labelProvider;
}
Also used : Pattern(org.talend.dataquality.domain.pattern.Pattern) IFile(org.eclipse.core.resources.IFile) ImpactNode(org.talend.dataprofiler.core.ui.dialog.message.DeleteModelElementConfirmDialog.ImpactNode) TdReport(org.talend.dataquality.reports.TdReport) DelimitedFileConnection(org.talend.core.model.metadata.builder.connection.DelimitedFileConnection) MatchRuleDefinition(org.talend.dataquality.rules.MatchRuleDefinition) Image(org.eclipse.swt.graphics.Image) IndicatorDefinition(org.talend.dataquality.indicators.definition.IndicatorDefinition) DQRule(org.talend.dataquality.rules.DQRule) ModelElement(orgomg.cwm.objectmodel.core.ModelElement) Item(org.talend.core.model.properties.Item) Analysis(org.talend.dataquality.analysis.Analysis) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject) RepositoryViewObject(org.talend.core.model.repository.RepositoryViewObject) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) ILabelProvider(org.eclipse.jface.viewers.ILabelProvider) LabelProvider(org.eclipse.jface.viewers.LabelProvider) Property(org.talend.core.model.properties.Property) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject) RepositoryViewObject(org.talend.core.model.repository.RepositoryViewObject)

Example 8 with DelimitedFileConnection

use of org.talend.core.model.metadata.builder.connection.DelimitedFileConnection in project tdq-studio-se by Talend.

the class FileSamplingDataSourceTest method testGetRecord.

@Test
public void testGetRecord() throws Exception {
    fileDataSource = new FileSamplingDataSource();
    DelimitedFileConnection fileConnection = ConnectionPackage.eINSTANCE.getConnectionFactory().createDelimitedFileConnection();
    // $NON-NLS-1$
    URL fileUrl = this.getClass().getResource("simple_data.csv");
    UnitTestBuildHelper.getDefault().initFileConnection(fileUrl, fileConnection);
    fileDataSource.setDataSource(fileConnection);
    int[] pos = { 0, 1, 2, 3 };
    ((FileSamplingDataSource) fileDataSource).setColumnPositions(pos);
    // CSVReader csvReader = createCsvReader(new File(getClass().getClassLoader()
    // .getResource("org/talend/cwm/db/connection/datasource/simple_data.csv").getFile()));
    int idx = 0;
    while (fileDataSource.hasNext()) {
        String value = getString(fileDataSource.getRecord());
        System.out.println(value);
        if (0 == idx) {
            // $NON-NLS-1$
            assertEquals("-24,male,4000,2010-10-23,", value);
        } else if (1 == idx) {
            // $NON-NLS-1$
            assertEquals("-50.0,male,2000,2011-02-02 12:10:00,", value);
        } else if (5 == idx) {
            // $NON-NLS-1$
            assertEquals(",female,4000,02/01/2008,", value);
        } else if (9 == idx) {
            // $NON-NLS-1$
            assertEquals("a str,male,30000,2004-12-20 00:00:00,", value);
        }
        idx++;
    }
}
Also used : DelimitedFileConnection(org.talend.core.model.metadata.builder.connection.DelimitedFileConnection) URL(java.net.URL) Test(org.junit.Test)

Example 9 with DelimitedFileConnection

use of org.talend.core.model.metadata.builder.connection.DelimitedFileConnection in project tdq-studio-se by Talend.

the class TOPRepositoryServiceTest method testRemoveAliasInSQLExplorer.

/**
 * Test method for
 * {@link org.talend.dataprofiler.core.service.TOPRepositoryService#removeAliasInSQLExplorer(org.talend.repository.model.IRepositoryNode)}
 * .
 */
public void testRemoveAliasInSQLExplorer() {
    IRepositoryNode child = mock(IRepositoryNode.class);
    IRepositoryViewObject obj = mock(IRepositoryViewObject.class);
    when(child.getObject()).thenReturn(obj);
    Property pro = mock(Property.class);
    when(obj.getProperty()).thenReturn(pro);
    ConnectionItem item = mock(ConnectionItem.class);
    when(pro.getItem()).thenReturn(item);
    MDMConnection mdm = mock(MDMConnection.class);
    DelimitedFileConnection dfile = mock(DelimitedFileConnection.class);
    DatabaseConnection dbc = mock(DatabaseConnection.class);
    when(item.getConnection()).thenReturn(dbc).thenReturn(dfile).thenReturn(mdm);
    List<ModelElement> dependencyClients = new ArrayList<ModelElement>();
    ModelElement m1 = mock(ModelElement.class);
    dependencyClients.add(m1);
    PowerMockito.mockStatic(EObjectHelper.class);
    when(EObjectHelper.getDependencyClients(dbc)).thenReturn(dependencyClients);
    when(EObjectHelper.getDependencyClients(mdm)).thenReturn(dependencyClients);
    when(EObjectHelper.getDependencyClients(dfile)).thenReturn(dependencyClients);
    Assert.assertFalse(tdqRepService.removeAliasInSQLExplorer(child));
    Assert.assertFalse(tdqRepService.removeAliasInSQLExplorer(child));
    Assert.assertFalse(tdqRepService.removeAliasInSQLExplorer(child));
}
Also used : ModelElement(orgomg.cwm.objectmodel.core.ModelElement) IRepositoryNode(org.talend.repository.model.IRepositoryNode) ConnectionItem(org.talend.core.model.properties.ConnectionItem) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject) ArrayList(java.util.ArrayList) DelimitedFileConnection(org.talend.core.model.metadata.builder.connection.DelimitedFileConnection) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) Property(org.talend.core.model.properties.Property) MDMConnection(org.talend.core.model.metadata.builder.connection.MDMConnection)

Example 10 with DelimitedFileConnection

use of org.talend.core.model.metadata.builder.connection.DelimitedFileConnection in project tdq-studio-se by Talend.

the class WorkbenchUtilsTest method testUpdateDependAnalysisOfDelimitedFile_3.

// test for no column remained
@Test
public void testUpdateDependAnalysisOfDelimitedFile_3() throws IOException, URISyntaxException, PartInitException, BusinessException {
    // create a file connection
    DelimitedFileConnection fileConnection = ConnectionPackage.eINSTANCE.getConnectionFactory().createDelimitedFileConnection();
    // $NON-NLS-1$
    URL fileUrl = this.getClass().getResource("file1");
    MetadataTable metadataTable = UnitTestBuildHelper.initFileConnection(fileUrl, fileConnection);
    UnitTestBuildHelper.initColumns(metadataTable);
    IFile file = WorkspaceUtils.fileToIFile(new File(FileLocator.toFileURL(fileUrl).toURI()));
    IPath itemPath = file.getFullPath();
    Property connectionProperty = PropertiesFactory.eINSTANCE.createProperty();
    connectionProperty.setAuthor(((RepositoryContext) CoreRuntimePlugin.getInstance().getContext().getProperty(Context.REPOSITORY_CONTEXT_KEY)).getUser());
    connectionProperty.setVersion(VersionUtils.DEFAULT_VERSION);
    // $NON-NLS-1$
    connectionProperty.setStatusCode("");
    connectionProperty.setLabel("file3");
    DelimitedFileConnectionItem connectionItem = PropertiesFactory.eINSTANCE.createDelimitedFileConnectionItem();
    connectionItem.setProperty(connectionProperty);
    connectionItem.setConnection(fileConnection);
    try {
        ProxyRepositoryFactory.getInstance().create(connectionItem, itemPath.removeFirstSegments(3).removeLastSegments(1));
    } catch (PersistenceException e) {
        Assert.fail(e.getMessage());
    }
    // create an analysis which use the columns in the file connection
    Analysis analysis = UnitTestBuildHelper.createRealAnalysis("Ana01", null, false);
    AnalysisContext context = AnalysisPackage.eINSTANCE.getAnalysisFactory().createAnalysisContext();
    context.setConnection(fileConnection);
    analysis.setContext(context);
    context.getAnalysedElements().addAll(metadataTable.getColumns());
    DependenciesHandler.getInstance().setDependencyOn(analysis, fileConnection);
    ElementWriterFactory.getInstance().createAnalysisWrite().save(analysis);
    // change the file's schema
    List<MetadataColumn> tempNewColumns = new ArrayList<MetadataColumn>();
    MetadataColumn name = ConnectionPackage.eINSTANCE.getConnectionFactory().createMetadataColumn();
    name.setName("name2");
    name.setLabel("name2");
    tempNewColumns.add(name);
    MetadataColumn country = ConnectionPackage.eINSTANCE.getConnectionFactory().createMetadataColumn();
    country.setName("country2");
    country.setLabel("country2");
    tempNewColumns.add(country);
    MetadataColumn country1 = ConnectionPackage.eINSTANCE.getConnectionFactory().createMetadataColumn();
    country1.setName("country1");
    country1.setLabel("country1");
    tempNewColumns.add(country1);
    metadataTable.getFeature().clear();
    metadataTable.getFeature().addAll(tempNewColumns);
    // before compare, the analysis has 5 analyzed elements
    Assert.assertEquals(5, analysis.getContext().getAnalysedElements().size());
    // call the tested method
    WorkbenchUtils.reloadMetadataOfDelimitedFile(metadataTable);
    List<MetadataColumn> afterCompareColumns = metadataTable.getColumns();
    // check the columns
    Assert.assertEquals(3, afterCompareColumns.size());
    Assert.assertEquals("name2", afterCompareColumns.get(0).getLabel());
    Assert.assertEquals("country2", afterCompareColumns.get(1).getLabel());
    Assert.assertEquals("country1", afterCompareColumns.get(2).getLabel());
    metadataTable.getColumns().clear();
    metadataTable.getColumns().addAll(afterCompareColumns);
    WorkbenchUtils.impactExistingAnalyses(fileConnection);
    // check the depended analysis
    EList<Dependency> clientDependencies = fileConnection.getSupplierDependency();
    for (Dependency dep : clientDependencies) {
        for (ModelElement mod : dep.getClient()) {
            if (!(mod instanceof Analysis)) {
                continue;
            }
            Analysis ana = (Analysis) mod;
            // assert the column with same name still in the analysis
            Assert.assertNotNull(ana.getContext().getAnalysedElements());
            // should be: only 2 with same name remained
            Assert.assertEquals(0, ana.getContext().getAnalysedElements().size());
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) ArrayList(java.util.ArrayList) DelimitedFileConnection(org.talend.core.model.metadata.builder.connection.DelimitedFileConnection) AnalysisContext(org.talend.dataquality.analysis.AnalysisContext) Dependency(orgomg.cwm.objectmodel.core.Dependency) URL(java.net.URL) MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) ModelElement(orgomg.cwm.objectmodel.core.ModelElement) Analysis(org.talend.dataquality.analysis.Analysis) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable) PersistenceException(org.talend.commons.exception.PersistenceException) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Property(org.talend.core.model.properties.Property) DelimitedFileConnectionItem(org.talend.core.model.properties.DelimitedFileConnectionItem) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

DelimitedFileConnection (org.talend.core.model.metadata.builder.connection.DelimitedFileConnection)22 ModelElement (orgomg.cwm.objectmodel.core.ModelElement)10 File (java.io.File)9 DatabaseConnection (org.talend.core.model.metadata.builder.connection.DatabaseConnection)9 IPath (org.eclipse.core.runtime.IPath)8 MetadataColumn (org.talend.core.model.metadata.builder.connection.MetadataColumn)8 MetadataTable (org.talend.core.model.metadata.builder.connection.MetadataTable)8 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 Property (org.talend.core.model.properties.Property)7 ConnectionItem (org.talend.core.model.properties.ConnectionItem)6 Analysis (org.talend.dataquality.analysis.Analysis)6 IFile (org.eclipse.core.resources.IFile)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 URL (java.net.URL)4 AnalysisContext (org.talend.dataquality.analysis.AnalysisContext)4 Indicator (org.talend.dataquality.indicators.Indicator)4 BufferedWriter (java.io.BufferedWriter)3 FileWriter (java.io.FileWriter)3 IOException (java.io.IOException)3