Search in sources :

Example 66 with ContextType

use of org.talend.designer.core.model.utils.emf.talendfile.ContextType in project tdi-studio-se by Talend.

the class JSONFileOutputStep1Form method addFieldsListeners.

@Override
protected void addFieldsListeners() {
    jsonFilePath.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent event) {
            if (jsonFilePath.getResult() == null) {
                return;
            }
            String text = jsonFilePath.getText();
            if (isContextMode()) {
                ContextType contextType = ConnectionContextHelper.getContextTypeForContextMode(connectionItem.getConnection(), true);
                text = TalendQuoteUtils.removeQuotes(ConnectionContextHelper.getOriginalValue(contextType, text));
            }
            // getConnection().setJSONFilePath(PathUtils.getPortablePath(JSONXsdFilePath.getText()));
            if (JSONFileOutputStep1Form.this.tempPath == null) {
                JSONFileOutputStep1Form.this.tempPath = JSONUtil.changeJsonToXml(text);
            }
            File file = new File(text);
            if (file.exists()) {
                List<ATreeNode> treeNodes = new ArrayList<ATreeNode>();
                valid = treePopulator.populateTree(JSONUtil.changeJsonToXml(text), treeNode);
                checkFieldsValue();
                if (!valid) {
                    return;
                }
                if (treeNodes.size() > 0) {
                    treeNode = treeNodes.get(0);
                }
                updateConnection(text);
            }
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
        }
    });
    jsonFilePath.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent event) {
            String text = jsonFilePath.getText();
            if (isContextMode()) {
                ContextType contextType = ConnectionContextHelper.getContextTypeForContextMode(connectionItem.getConnection(), true);
                text = TalendQuoteUtils.removeQuotes(ConnectionContextHelper.getOriginalValue(contextType, text));
            }
            if (getConnection().getJSONFilePath() != null && !getConnection().getJSONFilePath().equals(text)) {
                getConnection().getLoop().clear();
                getConnection().getRoot().clear();
                getConnection().getGroup().clear();
                xsdPathChanged = true;
            } else {
                xsdPathChanged = false;
            }
            if (Path.fromOSString(jsonFilePath.getText()).toFile().isFile()) {
                getConnection().setJSONFilePath(PathUtils.getPortablePath(jsonFilePath.getText()));
            } else {
                getConnection().setJSONFilePath(jsonFilePath.getText());
            }
            // updateConnection(text);
            StringBuilder fileContent = new StringBuilder();
            BufferedReader in = null;
            File file = null;
            if (tempJSONPath != null && getConnection().getFileContent() != null && getConnection().getFileContent().length > 0 && !isModifing) {
                file = new File(tempJSONPath);
                if (!file.exists()) {
                    try {
                        file.createNewFile();
                    } catch (IOException e2) {
                        ExceptionHandler.process(e2);
                    }
                    FileOutputStream outStream;
                    try {
                        outStream = new FileOutputStream(file);
                        outStream.write(getConnection().getFileContent());
                        outStream.close();
                    } catch (FileNotFoundException e1) {
                        ExceptionHandler.process(e1);
                    } catch (IOException e) {
                        ExceptionHandler.process(e);
                    }
                }
            } else {
                file = new File(text);
            }
            String str;
            try {
                Charset guessCharset = CharsetToolkit.guessEncoding(file, 4096);
                in = new BufferedReader(new InputStreamReader(new FileInputStream(file), guessCharset.displayName()));
                while ((str = in.readLine()) != null) {
                    fileContent.append(str + "\n");
                    // for encoding
                    if (str.contains("encoding")) {
                        String regex = "^<\\?JSON\\s*version=\\\"[^\\\"]*\\\"\\s*encoding=\\\"([^\\\"]*)\\\"\\?>$";
                        Perl5Compiler compiler = new Perl5Compiler();
                        Perl5Matcher matcher = new Perl5Matcher();
                        Pattern pattern = null;
                        try {
                            pattern = compiler.compile(regex);
                            if (matcher.contains(str, pattern)) {
                                MatchResult matchResult = matcher.getMatch();
                                if (matchResult != null) {
                                    encoding = matchResult.group(1);
                                }
                            }
                        } catch (MalformedPatternException malE) {
                            ExceptionHandler.process(malE);
                        }
                    }
                }
                fileContentText.setText(new String(fileContent));
            } catch (Exception e) {
                String msgError = "File" + " \"" + jsonFilePath.getText().replace("\\\\", "\\") + "\"\n";
                if (e instanceof FileNotFoundException) {
                    msgError = msgError + "is not found";
                } else if (e instanceof EOFException) {
                    msgError = msgError + "have an incorrect character EOF";
                } else if (e instanceof IOException) {
                    msgError = msgError + "is locked by another soft";
                } else {
                    msgError = msgError + "doesn't exist";
                }
                fileContentText.setText(msgError);
                if (!isReadOnly()) {
                    updateStatus(IStatus.ERROR, msgError);
                }
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (Exception exception) {
                    ExceptionHandler.process(exception);
                }
            }
            if (getConnection().getEncoding() == null || "".equals(getConnection().getEncoding())) {
                getConnection().setEncoding(encoding);
                if (encoding != null && !"".equals(encoding)) {
                    encodingCombo.setText(encoding);
                } else {
                    encodingCombo.setText("UTF-8");
                }
            }
            // }
            if (file.exists()) {
                valid = treePopulator.populateTree(JSONUtil.changeJsonToXml(text), treeNode);
                updateConnection(text);
            }
            checkFieldsValue();
            isModifing = true;
        }
    });
    encodingCombo.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent e) {
            getConnection().setEncoding(encodingCombo.getText());
            checkFieldsValue();
        }
    });
    commonNodesLimitation.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent e) {
            String str = commonNodesLimitation.getText();
            if ((!str.matches("\\d+")) || (Integer.valueOf(str) < 0)) {
                commonNodesLimitation.setText(String.valueOf(treePopulator.getLimit()));
                labelLimitation.setToolTipText(MessageFormat.format(Messages.JSONLimitToolTip, commonNodesLimitation.getText()));
            } else {
                treePopulator.setLimit(Integer.valueOf(str));
                labelLimitation.setToolTipText(MessageFormat.format(Messages.JSONLimitToolTip, str));
            }
            if (JSONFileOutputStep1Form.this.tempPath == null) {
                JSONFileOutputStep1Form.this.tempPath = JSONUtil.changeJsonToXml(jsonFilePath.getText());
            }
            File file = new File(JSONFileOutputStep1Form.this.tempPath);
            if (file.exists()) {
                valid = treePopulator.populateTree(JSONFileOutputStep1Form.this.tempPath, treeNode);
            }
            checkFieldsValue();
        }
    });
    commonNodesLimitation.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
        }

        @Override
        public void focusLost(FocusEvent e) {
            commonNodesLimitation.setText(String.valueOf(TreePopulator.getLimit()));
            labelLimitation.setToolTipText(MessageFormat.format(Messages.JSONLimitToolTip, commonNodesLimitation.getText()));
        }
    });
    outputFilePath.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent e) {
            getConnection().setOutputFilePath(PathUtils.getPortablePath(outputFilePath.getText()));
            checkFieldsValue();
        }
    });
}
Also used : ModifyListener(org.eclipse.swt.events.ModifyListener) FileNotFoundException(java.io.FileNotFoundException) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MatchResult(org.apache.oro.text.regex.MatchResult) FocusEvent(org.eclipse.swt.events.FocusEvent) ModifyEvent(org.eclipse.swt.events.ModifyEvent) SelectionEvent(org.eclipse.swt.events.SelectionEvent) EOFException(java.io.EOFException) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) Pattern(org.apache.oro.text.regex.Pattern) InputStreamReader(java.io.InputStreamReader) Charset(java.nio.charset.Charset) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) PersistenceException(org.talend.commons.exception.PersistenceException) ATreeNode(org.talend.datatools.xml.utils.ATreeNode) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) RecordFile(orgomg.cwm.resource.record.RecordFile) File(java.io.File) FocusListener(org.eclipse.swt.events.FocusListener) SelectionListener(org.eclipse.swt.events.SelectionListener)

Example 67 with ContextType

use of org.talend.designer.core.model.utils.emf.talendfile.ContextType in project tdi-studio-se by Talend.

the class GenericContextUtilTest method testExportAndRevertContextForComponentProperties.

@Test
public void testExportAndRevertContextForComponentProperties() {
    //$NON-NLS-1$
    String prefix = "testConn";
    GenericConnection connection = GenericMetadataFactory.eINSTANCE.createGenericConnection();
    //$NON-NLS-1$
    TestProperties props = (TestProperties) new TestProperties("test").init();
    //$NON-NLS-1$
    props.userId.setValue("1");
    //$NON-NLS-1$
    TestNestedProperties nestedProps = (TestNestedProperties) props.getProperty("nestedProps");
    //$NON-NLS-1$
    nestedProps.userName.setValue("testUserName");
    //$NON-NLS-1$
    nestedProps.userPassword.setValue("testUserPassword");
    connection.setCompProperties(props.toSerialized());
    Set<IConnParamName> contextParams = new HashSet<>();
    //$NON-NLS-1$
    contextParams.add(createConnParamName("userId"));
    //$NON-NLS-1$
    contextParams.add(createConnParamName("nestedProps.userName"));
    //$NON-NLS-1$
    contextParams.add(createConnParamName("nestedProps.userPassword"));
    // Test export context
    GenericContextUtil.setPropertiesForContextMode(prefix, connection, contextParams);
    TestProperties deserProps = getPropertiesFromConnection(connection);
    //$NON-NLS-1$
    assertEquals("context.testConn_userId", deserProps.userId.getValue());
    //$NON-NLS-1$
    assertEquals("context.testConn_nestedProps_userName", deserProps.nestedProps.userName.getValue());
    //$NON-NLS-1$
    assertEquals("context.testConn_nestedProps_userPassword", deserProps.nestedProps.userPassword.getValue());
    assertEquals(deserProps.userId.getTaggedValue(IGenericConstants.IS_CONTEXT_MODE), true);
    assertEquals(deserProps.nestedProps.userName.getTaggedValue(IGenericConstants.IS_CONTEXT_MODE), true);
    assertEquals(deserProps.nestedProps.userPassword.getTaggedValue(IGenericConstants.IS_CONTEXT_MODE), true);
    // Test revert context
    ContextType contextType = TalendFileFactoryImpl.eINSTANCE.createContextType();
    //$NON-NLS-1$ //$NON-NLS-2$
    contextType.getContextParameter().add(createContextParameterType("testConn_userId", "1"));
    //$NON-NLS-1$ //$NON-NLS-2$
    contextType.getContextParameter().add(createContextParameterType("testConn_nestedProps_userName", "testUserName"));
    //$NON-NLS-1$ //$NON-NLS-2$
    contextType.getContextParameter().add(createContextParameterType("testConn_nestedProps_userPassword", "testUserPassword"));
    GenericContextUtil.revertPropertiesForContextMode(connection, contextType);
    deserProps = getPropertiesFromConnection(connection);
    //$NON-NLS-1$
    assertEquals("1", deserProps.userId.getValue());
    //$NON-NLS-1$
    assertEquals("testUserName", deserProps.nestedProps.userName.getValue());
    //$NON-NLS-1$
    assertEquals("testUserPassword", deserProps.nestedProps.userPassword.getValue());
    assertEquals(deserProps.userId.getTaggedValue(IGenericConstants.IS_CONTEXT_MODE), false);
    assertEquals(deserProps.nestedProps.userName.getTaggedValue(IGenericConstants.IS_CONTEXT_MODE), false);
    assertEquals(deserProps.nestedProps.userPassword.getTaggedValue(IGenericConstants.IS_CONTEXT_MODE), false);
}
Also used : ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) IConnParamName(org.talend.metadata.managment.ui.model.IConnParamName) GenericConnection(org.talend.repository.generic.model.genericMetadata.GenericConnection) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 68 with ContextType

use of org.talend.designer.core.model.utils.emf.talendfile.ContextType in project tdi-studio-se by Talend.

the class FixUnevenItemContextParametersMigrationTaskTest method createContextType.

private ContextType createContextType(String contextName, String[] paramNames) {
    ContextType context = TalendFileFactory.eINSTANCE.createContextType();
    context.setName(contextName);
    for (String paramName : paramNames) {
        ContextParameterType param = TalendFileFactory.eINSTANCE.createContextParameterType();
        param.setName(paramName);
        param.setType("id_String");
        context.getContextParameter().add(param);
    }
    return context;
}
Also used : ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) ContextParameterType(org.talend.designer.core.model.utils.emf.talendfile.ContextParameterType)

Example 69 with ContextType

use of org.talend.designer.core.model.utils.emf.talendfile.ContextType in project tdi-studio-se by Talend.

the class FixUnevenItemContextParametersMigrationTaskTest method testChangeParamList.

@Test
public void testChangeParamList() {
    testItem.getProcess().setDefaultContext("Default");
    String[] paramNames = new String[] { "new4", "new2", "new3" };
    // default
    ContextType defaultGroup = createContextType("Default", paramNames);
    testItem.getProcess().getContext().add(defaultGroup);
    // group1
    paramNames = new String[] { "new5", "new2", "new1" };
    ContextType group1 = createContextType("group1", paramNames);
    testItem.getProcess().getContext().add(group1);
    FixUnevenItemContextParametersMigrationTask migration = new FixUnevenItemContextParametersMigrationTask();
    migration.execute(testItem);
    Assert.assertEquals(((ContextParameterType) defaultGroup.getContextParameter().get(0)).getName(), "new4");
    Assert.assertEquals(((ContextParameterType) defaultGroup.getContextParameter().get(1)).getName(), "new2");
    Assert.assertEquals(((ContextParameterType) defaultGroup.getContextParameter().get(2)).getName(), "new3");
    Assert.assertEquals(((ContextParameterType) defaultGroup.getContextParameter().get(3)).getName(), "new5");
    Assert.assertEquals(((ContextParameterType) defaultGroup.getContextParameter().get(4)).getName(), "new1");
    Assert.assertEquals(((ContextParameterType) group1.getContextParameter().get(0)).getName(), "new4");
    Assert.assertEquals(((ContextParameterType) group1.getContextParameter().get(1)).getName(), "new2");
    Assert.assertEquals(((ContextParameterType) group1.getContextParameter().get(2)).getName(), "new3");
    Assert.assertEquals(((ContextParameterType) group1.getContextParameter().get(3)).getName(), "new5");
    Assert.assertEquals(((ContextParameterType) group1.getContextParameter().get(4)).getName(), "new1");
}
Also used : ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) Test(org.junit.Test)

Example 70 with ContextType

use of org.talend.designer.core.model.utils.emf.talendfile.ContextType in project tdi-studio-se by Talend.

the class UnifyPasswordEncryption4ParametersInJobMigrationTask method checkContext.

/**
     * 
     * same as UnifyPasswordEncryption4ContextMigrationTask to change the encryption.
     */
@SuppressWarnings("deprecation")
protected boolean checkContext(Item item, ProcessType processType) throws Exception {
    boolean modified = false;
    // context
    EList contextList = processType.getContext();
    for (Object o : contextList) {
        if (o instanceof ContextType) {
            List<ContextParameterType> paramTypes = ((ContextType) o).getContextParameter();
            if (paramTypes != null) {
                for (ContextParameterType param : paramTypes) {
                    String value = param.getValue();
                    if (value != null && (PasswordEncryptUtil.isPasswordType(param.getType()) || PasswordEncryptUtil.isPasswordField(param.getName()))) {
                        try {
                            String rawPassword = PasswordEncryptUtil.decryptPassword(value);
                            param.setRawValue(rawPassword);
                        } catch (Exception e) {
                            param.setRawValue(value);
                        }
                        modified = true;
                    }
                }
            }
        }
    }
    return modified;
}
Also used : ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) EList(org.eclipse.emf.common.util.EList) ContextParameterType(org.talend.designer.core.model.utils.emf.talendfile.ContextParameterType)

Aggregations

ContextType (org.talend.designer.core.model.utils.emf.talendfile.ContextType)108 ContextParameterType (org.talend.designer.core.model.utils.emf.talendfile.ContextParameterType)27 ArrayList (java.util.ArrayList)26 ContextItem (org.talend.core.model.properties.ContextItem)21 PersistenceException (org.talend.commons.exception.PersistenceException)17 File (java.io.File)16 NoSQLServerException (org.talend.repository.nosql.exceptions.NoSQLServerException)15 HadoopClusterConnection (org.talend.repository.model.hadoopcluster.HadoopClusterConnection)13 NoSQLReflectionException (org.talend.repository.nosql.exceptions.NoSQLReflectionException)13 Test (org.junit.Test)12 HashMap (java.util.HashMap)10 List (java.util.List)10 ProcessItem (org.talend.core.model.properties.ProcessItem)9 IOException (java.io.IOException)8 Map (java.util.Map)8 EList (org.eclipse.emf.common.util.EList)7 FileNotFoundException (java.io.FileNotFoundException)6 FileOutputStream (java.io.FileOutputStream)6 Property (org.talend.core.model.properties.Property)6 HashSet (java.util.HashSet)5