Search in sources :

Example 6 with ImportObject

use of org.talend.dataprofiler.core.ui.action.actions.ImportObject in project tdq-studio-se by Talend.

the class ImportFactoryTest method testImportParserRuleToStuctureEmptyWithoutDoubleQuote.

/**
 * Test method for
 * {@link org.talend.dataprofiler.core.pattern.ImportFactory#importParserRuleToStucture(org.talend.dataprofiler.core.ui.action.actions.ImportObject, org.eclipse.core.resources.IFolder, boolean, boolean, java.lang.String)}
 * Empty Without Double Quote.
 */
@Test
public void testImportParserRuleToStuctureEmptyWithoutDoubleQuote() throws IOException {
    // the parser rule name, use it to get the imported file name
    // $NON-NLS-1$
    String parserRuleName = "parserRule2";
    // $NON-NLS-1$
    String parserRuleFileName = parserRuleName + "_0.1.rules";
    // the expect expression's values: name and body will add double quote after import, lang will not add double
    // quote after import
    // $NON-NLS-1$
    String name = "\"\"";
    // $NON-NLS-1$
    String lang = "";
    // $NON-NLS-1$
    String body = "\"\"";
    // $NON-NLS-1$
    File dict = new File("");
    // $NON-NLS-1$
    File importFile = new File(dict.getAbsolutePath() + File.separator + "paser_rule.csv");
    if (importFile.exists()) {
        importFile.delete();
    }
    importFile.createNewFile();
    // set the csv file's head
    // $NON-NLS-1$
    String content = "\"Label\"\t\"Purpose\"\t\"Description\"\t\"Author\"\t\"Relative_Path\"\t\"Name\"\t\"Type\"\t\"Value\"\n";
    // set the csv file's content: the clumn's value is empty without double quote
    content += // $NON-NLS-1$
    "\"" + parserRuleName + // $NON-NLS-1$
    "\"\t\"\"\t\"\"\t\"trunk@talend.com\"\t\"\"\t\"\"\t\"\"\t\"\"";
    BufferedWriter output = new BufferedWriter(new FileWriter(importFile));
    output.write(content);
    output.close();
    List<File> pJarfilesMock = new ArrayList<File>();
    ImportObject importObject = ImportObject.createImportObject(importFile, pJarfilesMock);
    IFolder parserRuleFolder = ResourceManager.getOneFolder(EResourceConstant.RULES_PARSER);
    boolean skip = false;
    boolean rename = true;
    // $NON-NLS-1$
    String importItemName = "importItemName";
    List<ReturnCode> importParserRuleToStucture = ImportFactory.importParserRuleToStucture(importObject, parserRuleFolder, skip, rename, importItemName);
    for (ReturnCode rc : importParserRuleToStucture) {
        assertTrue(rc.isOk());
    }
    // the imported parser rule's name is a
    IFile parserRuleFile = parserRuleFolder.getFile(parserRuleFileName);
    ParserRule parserRule = (ParserRule) DQRuleResourceFileHelper.getInstance().findDQRule(parserRuleFile);
    List<TdExpression> expressions = parserRule.getExpression();
    for (TdExpression expression : expressions) {
        assertTrue(name.equals(expression.getName()));
        assertTrue(lang.equals(expression.getLanguage()));
        assertTrue(body.equals(expression.getBody()));
    }
}
Also used : ParserRule(org.talend.dataquality.rules.ParserRule) ReturnCode(org.talend.utils.sugars.ReturnCode) IFile(org.eclipse.core.resources.IFile) TdExpression(org.talend.cwm.relational.TdExpression) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) BufferedWriter(java.io.BufferedWriter) ImportObject(org.talend.dataprofiler.core.ui.action.actions.ImportObject) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IFolder(org.eclipse.core.resources.IFolder) Test(org.junit.Test)

Example 7 with ImportObject

use of org.talend.dataprofiler.core.ui.action.actions.ImportObject in project tdq-studio-se by Talend.

the class ImportFactoryTest method testImportIndicatorToStucture.

/**
 * Test method for
 * {@link org.talend.dataprofiler.core.pattern.ImportFactory#importIndicatorToStucture(org.talend.dataprofiler.core.ui.action.actions.ImportObject, org.eclipse.core.resources.IFolder, boolean, boolean, java.lang.String)}
 * normal condition: the import file's extension is csv.
 */
@Test
public void testImportIndicatorToStucture() throws Exception {
    // $NON-NLS-1$
    File importFile = createImportFile(UDI_NAME, StringUtilities.getRandomString(8) + ".csv");
    assertTrue(importFile.exists());
    assertTrue(importFile.isFile());
    assertTrue(importFile.length() > 0);
    List<File> pJarfiles = new ArrayList<File>();
    ImportObject importObject = ImportObject.createImportObject(importFile, pJarfiles);
    IFolder udiFolder = ResourceManager.getUDIFolder();
    boolean skip = false;
    boolean rename = true;
    List<ReturnCode> rc = ImportFactory.importIndicatorToStucture(importObject, udiFolder, skip, rename, UDI_NAME);
    assertTrue(rc.size() == 1);
    assertTrue(rc.get(0).isOk());
    IndicatorDefinition indicatorDefinition = null;
    RootContainer<String, IRepositoryViewObject> tdqViewObjects = ProxyRepositoryFactory.getInstance().getTdqRepositoryViewObjects(ERepositoryObjectType.TDQ_USERDEFINE_INDICATORS, ERepositoryObjectType.getFolderName(ERepositoryObjectType.TDQ_USERDEFINE_INDICATORS));
    for (IRepositoryViewObject viewObject : tdqViewObjects.getMembers()) {
        IndicatorDefinition indDefTemp = ((TDQIndicatorDefinitionItem) viewObject.getProperty().getItem()).getIndicatorDefinition();
        if (UDI_NAME.equals(indDefTemp.getName())) {
            indicatorDefinition = indDefTemp;
            break;
        }
    }
    assertNotNull(indicatorDefinition);
    if (indicatorDefinition != null) {
        EList<IndicatorDefinitionParameter> indDefParas = indicatorDefinition.getIndicatorDefinitionParameter();
        assertFalse(indDefParas.isEmpty());
        // String paraString = StringUtils.replace(IND_DEF_PARA_STRING, "\"\"", "\""); //$NON-NLS-1$ //$NON-NLS-2$
        for (IndicatorDefinitionParameter indDefPara : indDefParas) {
            String temp = indDefPara.getKey() + UDIHelper.PARA_SEPARATE_1 + indDefPara.getValue() + UDIHelper.PARA_SEPARATE_2;
            assertTrue(IND_DEF_PARA_STRING.indexOf(temp) > -1);
        }
    }
    importFile.delete();
}
Also used : ReturnCode(org.talend.utils.sugars.ReturnCode) TDQIndicatorDefinitionItem(org.talend.dataquality.properties.TDQIndicatorDefinitionItem) ArrayList(java.util.ArrayList) IndicatorDefinition(org.talend.dataquality.indicators.definition.IndicatorDefinition) ImportObject(org.talend.dataprofiler.core.ui.action.actions.ImportObject) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject) IndicatorDefinitionParameter(org.talend.dataquality.indicators.definition.IndicatorDefinitionParameter) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IFolder(org.eclipse.core.resources.IFolder) Test(org.junit.Test)

Example 8 with ImportObject

use of org.talend.dataprofiler.core.ui.action.actions.ImportObject in project tdq-studio-se by Talend.

the class ImportFactoryTest method testImportIndicatorToStuctureImportFileIsEmpty.

/**
 * Test method for
 * {@link org.talend.dataprofiler.core.pattern.ImportFactory#importIndicatorToStucture(org.talend.dataprofiler.core.ui.action.actions.ImportObject, org.eclipse.core.resources.IFolder, boolean, boolean, java.lang.String)}
 * non-normal condition: the import file is empty.
 */
@Test
public void testImportIndicatorToStuctureImportFileIsEmpty() throws Exception {
    File importFile = new File(// $NON-NLS-1$ //$NON-NLS-2$
    System.getProperty("java.io.tmpdir") + File.separator + StringUtilities.getRandomString(8) + ".csv");
    if (!importFile.exists()) {
        if (!importFile.getParentFile().exists()) {
            importFile.getParentFile().mkdirs();
        }
        importFile.createNewFile();
    }
    List<File> pJarfiles = new ArrayList<File>();
    ImportObject importObject = ImportObject.createImportObject(importFile, pJarfiles);
    IFolder udiFolder = ResourceManager.getUDIFolder();
    boolean skip = false;
    boolean rename = true;
    List<ReturnCode> rc = ImportFactory.importIndicatorToStucture(importObject, udiFolder, skip, rename, UDI_NAME);
    assertTrue(rc.size() == 1);
    assertFalse(rc.get(0).isOk());
    importFile.delete();
}
Also used : ImportObject(org.talend.dataprofiler.core.ui.action.actions.ImportObject) ReturnCode(org.talend.utils.sugars.ReturnCode) ArrayList(java.util.ArrayList) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IFolder(org.eclipse.core.resources.IFolder) Test(org.junit.Test)

Example 9 with ImportObject

use of org.talend.dataprofiler.core.ui.action.actions.ImportObject in project tdq-studio-se by Talend.

the class ImportFactory method importToStucture.

/**
 * DOC xqliu Comment method "importToStucture".
 *
 * @param importObject
 * @param selectionFolder
 * @param type
 * @param skip
 * @param rename
 * @param importItemName
 * @return
 */
public static List<ReturnCode> importToStucture(ImportObject importObject, IFolder selectionFolder, ExpressionType type, boolean skip, boolean rename, String importItemName) {
    List<ReturnCode> importEvent = new ArrayList<ReturnCode>();
    // MOD qiongli 2012-12-20 TDQ-5899(issue 2),should get all patterns from Pattern folder.
    Set<String> names = PatternUtilities.getNestFolderPatternNames(new HashSet<String>(), ResourceManager.getPatternFolder());
    File importFile = importObject.getObjFile();
    String fileExtName = getFileExtName(importFile);
    if (FileUtils.isCSV(fileExtName)) {
        try {
            CSVReader reader = FileUtils.createCSVReader(importFile, FileUtils.TEXT_QUAL, FileUtils.TEXT_QUAL);
            reader.setSkipEmptyRecords(true);
            reader.readHeaders();
            while (reader.readNext()) {
                String name = reader.get(PatternToExcelEnum.Label.getLiteral());
                if (names.contains(name)) {
                    if (skip) {
                        importEvent.add(new ReturnCode(DefaultMessagesImpl.getString("ImportFactory.patternInported", name), // $NON-NLS-1$
                        false));
                        continue;
                    }
                    if (rename) {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        name = name + "(" + new Date() + ")";
                    }
                }
                PatternParameters patternParameters = new ImportFactory().new PatternParameters();
                patternParameters.name = name;
                patternParameters.auther = reader.get(PatternToExcelEnum.Author.getLiteral());
                patternParameters.description = reader.get(PatternToExcelEnum.Description.getLiteral());
                patternParameters.purpose = reader.get(PatternToExcelEnum.Purpose.getLiteral());
                patternParameters.relativePath = reader.get(PatternToExcelEnum.RelativePath.getLiteral());
                for (PatternLanguageType languagetype : PatternLanguageType.values()) {
                    String cellStr = reader.get(languagetype.getExcelEnum().getLiteral());
                    if (cellStr != null && !cellStr.equals("")) {
                        // $NON-NLS-1$
                        patternParameters.regex.put(languagetype.getLiteral(), cellStr);
                    }
                }
                try {
                    TypedReturnCode<Object> create = createAndStorePattern(patternParameters, selectionFolder, type);
                    if (create.isOk()) {
                        names.add(name);
                        importEvent.add(new ReturnCode(// $NON-NLS-1$
                        DefaultMessagesImpl.getString(// $NON-NLS-1$
                        "ImportFactory.importPattern", // $NON-NLS-1$
                        name, selectionFolder.getProjectRelativePath().toString()), true));
                    } else {
                        throw new TalendInternalPersistenceException(create.getMessage());
                    }
                } catch (Exception e) {
                    // $NON-NLS-1$
                    importEvent.add(new ReturnCode(DefaultMessagesImpl.getString("ImportFactory.SaveFailed", name), false));
                }
            }
            reader.close();
        } catch (Exception e) {
            log.error(e, e);
            // $NON-NLS-1$
            importEvent.add(new ReturnCode(DefaultMessagesImpl.getString("ImportFactory.importFailed"), false));
        }
    }
    importObject.copyJarFiles();
    // ADD xqliu 2012-04-27 TDQ-5149
    checkImportEvent(importItemName, importEvent);
    // ~ TDQ-5149
    return importEvent;
}
Also used : TypedReturnCode(org.talend.utils.sugars.TypedReturnCode) ReturnCode(org.talend.utils.sugars.ReturnCode) CSVReader(com.talend.csv.CSVReader) ArrayList(java.util.ArrayList) Date(java.util.Date) CoreException(org.eclipse.core.runtime.CoreException) TalendInternalPersistenceException(org.talend.core.exception.TalendInternalPersistenceException) IOException(java.io.IOException) TalendInternalPersistenceException(org.talend.core.exception.TalendInternalPersistenceException) ImportObject(org.talend.dataprofiler.core.ui.action.actions.ImportObject) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 10 with ImportObject

use of org.talend.dataprofiler.core.ui.action.actions.ImportObject in project tdq-studio-se by Talend.

the class ImportFactory method importIndicatorToStucture.

/**
 * DOC xqliu Comment method "importIndicatorToStucture".
 *
 * @param importObject
 * @param selectionFolder
 * @param skip
 * @param rename
 * @param importItemName
 * @return
 */
public static List<ReturnCode> importIndicatorToStucture(ImportObject importObject, IFolder selectionFolder, boolean skip, boolean rename, String importItemName) {
    List<ReturnCode> information = new ArrayList<ReturnCode>();
    Set<String> names = UDIHelper.getAllIndicatorNames(selectionFolder);
    File importFile = importObject.getObjFile();
    String fileExtName = getFileExtName(importFile);
    if (FileUtils.isCSV(fileExtName)) {
        String name = PluginConstant.EMPTY_STRING;
        try {
            CSVReader reader = FileUtils.createCSVReader(importFile, FileUtils.TEXT_QUAL, FileUtils.TEXT_QUAL);
            reader.setSkipEmptyRecords(true);
            reader.readHeaders();
            // $NON-NLS-1$
            java.text.SimpleDateFormat simpleDateFormat = new java.text.SimpleDateFormat("yyyyMMddHHmmssSSS");
            while (reader.readNext()) {
                name = reader.get(PatternToExcelEnum.Label.getLiteral());
                if (names.contains(name)) {
                    if (skip) {
                        // $NON-NLS-1$
                        information.add(new ReturnCode(DefaultMessagesImpl.getString("ImportFactory.Imported", name), false));
                        continue;
                    }
                    if (rename) {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        name = name + "(" + simpleDateFormat.format(new Date()) + Math.random() + ")";
                    }
                }
                UDIParameters udiParameters = new ImportFactory().new UDIParameters();
                udiParameters.name = name;
                udiParameters.auther = reader.get(PatternToExcelEnum.Author.getLiteral());
                udiParameters.description = reader.get(PatternToExcelEnum.Description.getLiteral());
                udiParameters.purpose = reader.get(PatternToExcelEnum.Purpose.getLiteral());
                udiParameters.relativePath = reader.get(PatternToExcelEnum.RelativePath.getLiteral());
                udiParameters.category = reader.get(PatternToExcelEnum.Category.getLiteral());
                udiParameters.javaClassName = reader.get(PatternToExcelEnum.JavaClassName.getLiteral());
                udiParameters.javaJarPath = reader.get(PatternToExcelEnum.JavaJarPath.getLiteral());
                String[] headers = reader.getHeaders();
                String[] columnsValue = reader.getValues();
                HashMap<String, String> record = new HashMap<String, String>();
                for (int i = 0; i < headers.length; i++) {
                    // TDQ-6714: fix when there is no regexp for the imported udi (csv) file.
                    if (i >= columnsValue.length) {
                        // $NON-NLS-1$
                        record.put(headers[i], "");
                    } else {
                        if (columnsValue[i] != null && columnsValue[i].length() > 0) {
                            record.put(headers[i], columnsValue[i]);
                        }
                    }
                }
                for (PatternLanguageType languagetype : PatternLanguageType.values()) {
                    String cellStr = record.get(languagetype.getExcelEnum().getLiteral());
                    if (cellStr != null && !cellStr.equals("\"\"")) {
                        // $NON-NLS-1$
                        udiParameters.regex.put(languagetype.getLiteral(), trimQuote(cellStr));
                    }
                }
                udiParameters.setParaMap(buildIndDefPara(record));
                TypedReturnCode<Object> create = createAndStoreUDI(udiParameters, selectionFolder);
                if (create.isOk()) {
                    names.add(name);
                    // add the suscess message to display.
                    information.add(new ReturnCode(// $NON-NLS-1$
                    DefaultMessagesImpl.getString(// $NON-NLS-1$
                    "ImportFactory.importedSucess", ((TDQItem) create.getObject()).getProperty().getDisplayName(), selectionFolder.getProjectRelativePath().toString()), true));
                } else {
                    throw new TalendInternalPersistenceException(create.getMessage());
                }
            }
            reader.close();
        } catch (Exception e) {
            log.error(e, e);
            // $NON-NLS-1$
            information.add(new ReturnCode(DefaultMessagesImpl.getString("ImportFactory.importedFailed", name), false));
        }
    }
    // MOD qiongli 2011-11-28 TDQ-4038.consider to import the definition file.
    if (FactoriesUtil.DEFINITION.equalsIgnoreCase(fileExtName)) {
        String propFilePath = importFile.getPath().replaceFirst(PluginConstant.DOT_STRING + fileExtName, PluginConstant.DOT_STRING + FactoriesUtil.PROPERTIES_EXTENSION);
        File propFile = new File(propFilePath);
        // just import the definition file which have the realted Property file.
        if (!propFile.exists()) {
            return information;
        }
        String name = importFile.getName();
        try {
            if (names.contains(name)) {
                if (skip) {
                    // $NON-NLS-1$
                    information.add(new ReturnCode(DefaultMessagesImpl.getString("ImportFactory.Imported", name), false));
                    return information;
                }
                if (rename) {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    name = name + "(" + new Date() + Math.random() + ")";
                }
            }
            IFile elementFile = selectionFolder.getFile(name);
            if (!elementFile.exists()) {
                elementFile.create(new FileInputStream(importFile), false, null);
                ModelElement modelElement = ModelElementFileFactory.getModelElement(elementFile);
                if (modelElement != null) {
                    ElementWriterFactory.getInstance().createIndicatorDefinitionWriter().create(modelElement, selectionFolder);
                    DefinitionHandler.getInstance().reloadIndicatorsDefinitions();
                    names.add(name);
                    information.add(new ReturnCode(// $NON-NLS-1$
                    DefaultMessagesImpl.getString(// $NON-NLS-1$
                    "ImportFactory.importedSucess", name), true));
                }
            }
        } catch (Exception e) {
            log.error(e);
            // $NON-NLS-1$
            information.add(new ReturnCode(DefaultMessagesImpl.getString("ImportFactory.importedFailed", name), false));
        }
    }
    importObject.copyJarFiles();
    // ADD xqliu 2012-04-27 TDQ-5149
    checkImportEvent(importItemName, information);
    // ~ TDQ-5149
    return information;
}
Also used : IFile(org.eclipse.core.resources.IFile) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ModelElement(orgomg.cwm.objectmodel.core.ModelElement) TypedReturnCode(org.talend.utils.sugars.TypedReturnCode) ReturnCode(org.talend.utils.sugars.ReturnCode) CSVReader(com.talend.csv.CSVReader) Date(java.util.Date) CoreException(org.eclipse.core.runtime.CoreException) TalendInternalPersistenceException(org.talend.core.exception.TalendInternalPersistenceException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) TalendInternalPersistenceException(org.talend.core.exception.TalendInternalPersistenceException) TDQItem(org.talend.core.model.properties.TDQItem) ImportObject(org.talend.dataprofiler.core.ui.action.actions.ImportObject) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Aggregations

File (java.io.File)10 ArrayList (java.util.ArrayList)10 IFile (org.eclipse.core.resources.IFile)10 ImportObject (org.talend.dataprofiler.core.ui.action.actions.ImportObject)10 ReturnCode (org.talend.utils.sugars.ReturnCode)10 IFolder (org.eclipse.core.resources.IFolder)8 Test (org.junit.Test)8 BufferedWriter (java.io.BufferedWriter)4 FileWriter (java.io.FileWriter)4 TdExpression (org.talend.cwm.relational.TdExpression)4 ParserRule (org.talend.dataquality.rules.ParserRule)4 CSVReader (com.talend.csv.CSVReader)2 IOException (java.io.IOException)2 Date (java.util.Date)2 CoreException (org.eclipse.core.runtime.CoreException)2 TalendInternalPersistenceException (org.talend.core.exception.TalendInternalPersistenceException)2 TypedReturnCode (org.talend.utils.sugars.TypedReturnCode)2 FileInputStream (java.io.FileInputStream)1 HashMap (java.util.HashMap)1 TDQItem (org.talend.core.model.properties.TDQItem)1