Search in sources :

Example 1 with ComponentCategory

use of org.talend.core.model.components.ComponentCategory in project tdi-studio-se by Talend.

the class TalendEditorPaletteFactory method getJobletObjectType.

private static ERepositoryObjectType getJobletObjectType(IComponentsHandler handler) {
    if (handler == null) {
        return null;
    }
    ComponentCategory category = handler.extractComponentsCategory();
    ERepositoryObjectType type = ERepositoryObjectType.JOBLET;
    if (category == null) {
        return type;
    }
    switch(category) {
        case CATEGORY_4_SPARK:
            type = ERepositoryObjectType.SPARK_JOBLET;
            break;
        case CATEGORY_4_SPARKSTREAMING:
            type = ERepositoryObjectType.SPARK_STREAMING_JOBLET;
            break;
        default:
            break;
    }
    return type;
}
Also used : ERepositoryObjectType(org.talend.core.model.repository.ERepositoryObjectType) ComponentCategory(org.talend.core.model.components.ComponentCategory)

Example 2 with ComponentCategory

use of org.talend.core.model.components.ComponentCategory in project tdi-studio-se by Talend.

the class JavaSampleCodeFactory method generateJavaRowCode.

/**
     * 
     * DOC YeXiaowei Comment method "generateJavaRowCode". Generates Java code for the tJavaRow component in DI and BD.
     * 
     * @param node
     * @return
     */
private String generateJavaRowCode(final Node node) {
    boolean isSparkNode = false;
    //$NON-NLS-1$
    String sparkMapType = "MAP";
    ComponentCategory componentCategory = ComponentCategory.getComponentCategoryFromName(node.getComponent().getType());
    String primeVlue = //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    "// code sample:\r\n" + "//\r\n" + "// multiply by 2 the row identifier\r\n" + "// output_row.id = input_row.id * 2;\r\n" + "//\r\n" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    "// lowercase the name\r\n" + //$NON-NLS-1$
    "// output_row.name = input_row.name.toLowerCase();";
    if (ComponentCategory.CATEGORY_4_SPARK == componentCategory || ComponentCategory.CATEGORY_4_SPARKSTREAMING == componentCategory) {
        isSparkNode = true;
        //$NON-NLS-1$
        sparkMapType = node.getPropertyValue("MAPTYPE").toString();
        //$NON-NLS-1$
        primeVlue = "// Please add an input to the component to generate a sample code\r\n";
    }
    if (node.getMetadataList() == null || node.getMetadataList().get(0) == null) {
        return primeVlue;
    }
    if (node.getIncomingConnections() == null || node.getIncomingConnections().isEmpty() || node.getIncomingConnections().get(0).getMetadataTable() == null) {
        return primeVlue;
    }
    IMetadataTable inputTable = node.getIncomingConnections().get(0).getMetadataTable();
    List<IMetadataColumn> inputColumns = inputTable.getListColumns();
    IMetadataTable outputTable = node.getMetadataList().get(0);
    List<IMetadataColumn> outputColumns = outputTable.getListColumns();
    if (inputColumns == null || inputColumns.isEmpty() || outputColumns == null || outputColumns.isEmpty()) {
        return primeVlue;
    }
    //$NON-NLS-1$
    String javaEnding = ";";
    //$NON-NLS-1$
    String lineSeparator = System.getProperty("line.separator");
    StringBuilder builder = new StringBuilder();
    //$NON-NLS-1$
    boolean isSelect = MessageDialog.openQuestion(null, null, Messages.getString("JavaSampleCodeFactory.askRegenerateCode"));
    if (isSelect) {
        // Add simple comment
        //$NON-NLS-1$
        builder.append(Messages.getString("JavaSampleCodeFactory.schema")).append(lineSeparator);
        int inputRowsLength = inputColumns.size();
        int ouputRowsLength = outputColumns.size();
        if (inputRowsLength == 0 || ouputRowsLength == 0) {
            return null;
        }
        if (isSparkNode && (sparkMapType.equalsIgnoreCase("FLATMAP"))) {
            //$NON-NLS-1$
            //$NON-NLS-1$
            builder.append("Output output = new Output();\r\n");
        }
        if (inputRowsLength >= ouputRowsLength) {
            for (int i = 0; i < inputRowsLength; i++) {
                String inputLabel = inputColumns.get(i).getLabel();
                String outputLabel = null;
                if (i > ouputRowsLength - 1) {
                    outputLabel = outputColumns.get(ouputRowsLength - 1).getLabel();
                } else {
                    outputLabel = outputColumns.get(i).getLabel();
                }
                if (isSparkNode) {
                    //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                    builder.append("output.").append(outputLabel).append(" = ").append("input.").append(inputLabel).append(javaEnding);
                } else {
                    //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                    builder.append("output_row.").append(outputLabel).append(" = ").append("input_row.").append(inputLabel).append(javaEnding);
                }
                builder.append(lineSeparator);
            }
        } else {
            for (int i = 0; i < ouputRowsLength; i++) {
                String outputLabel = outputColumns.get(i).getLabel();
                String inputLabel = null;
                if (i > inputRowsLength - 1) {
                    inputLabel = inputColumns.get(inputRowsLength - 1).getLabel();
                } else {
                    inputLabel = inputColumns.get(i).getLabel();
                }
                if (isSparkNode) {
                    //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                    builder.append("output.").append(outputLabel).append(" = ").append("input.").append(inputLabel).append(javaEnding);
                } else {
                    //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                    builder.append("output_row.").append(outputLabel).append(" = ").append("input_row.").append(inputLabel).append(javaEnding);
                }
                builder.append(lineSeparator);
            }
        }
        if (isSparkNode && (sparkMapType.equalsIgnoreCase("FLATMAP"))) {
            //$NON-NLS-1$
            //$NON-NLS-1$
            builder.append("outputList.add(output);\r\n");
        }
        return builder.toString();
    } else {
        return null;
    }
}
Also used : IMetadataTable(org.talend.core.model.metadata.IMetadataTable) IMetadataColumn(org.talend.core.model.metadata.IMetadataColumn) ComponentCategory(org.talend.core.model.components.ComponentCategory)

Example 3 with ComponentCategory

use of org.talend.core.model.components.ComponentCategory in project tdi-studio-se by Talend.

the class NewComponentFrameworkMigrationTask method execute.

@Override
public ExecutionResult execute(final Item item) {
    final ProcessType processType = getProcessType(item);
    ComponentCategory category = ComponentCategory.getComponentCategoryFromItem(item);
    Properties props = getPropertiesFromFile();
    IComponentConversion conversion = new IComponentConversion() {

        @Override
        public void transform(NodeType nodeType) {
            if (nodeType == null || props == null) {
                return;
            }
            boolean modified = false;
            Map<String, String> schemaParamMap = new HashMap<>();
            String currComponentName = nodeType.getComponentName();
            String newComponentName = props.getProperty(currComponentName);
            nodeType.setComponentName(newComponentName);
            IComponent component = ComponentsFactoryProvider.getInstance().get(newComponentName, category.getName());
            ComponentProperties compProperties = ComponentsUtils.getComponentProperties(newComponentName);
            FakeNode fNode = new FakeNode(component);
            for (IElementParameter param : fNode.getElementParameters()) {
                if (param instanceof GenericElementParameter) {
                    String paramName = param.getName();
                    NamedThing currNamedThing = ComponentsUtils.getGenericSchemaElement(compProperties, paramName);
                    String oldParamName = props.getProperty(currComponentName + IGenericConstants.EXP_SEPARATOR + paramName);
                    if (oldParamName != null && !(oldParamName = oldParamName.trim()).isEmpty()) {
                        if (currNamedThing instanceof Property && (GenericTypeUtils.isSchemaType((Property<?>) currNamedThing))) {
                            schemaParamMap.put(paramName, props.getProperty(currComponentName + IGenericConstants.EXP_SEPARATOR + paramName + IGenericConstants.EXP_SEPARATOR + "connector"));
                        }
                        ElementParameterType paramType = getParameterType(nodeType, oldParamName);
                        if (paramType != null) {
                            if (currNamedThing instanceof ComponentReferenceProperties) {
                                ComponentReferenceProperties refProps = (ComponentReferenceProperties) currNamedThing;
                                refProps.referenceType.setValue(ComponentReferenceProperties.ReferenceType.COMPONENT_INSTANCE);
                                refProps.componentInstanceId.setStoredValue(ParameterUtilTool.convertParameterValue(paramType));
                                refProps.componentInstanceId.setTaggedValue(IGenericConstants.ADD_QUOTES, true);
                            } else {
                                processMappedElementParameter(props, nodeType, (GenericElementParameter) param, paramType, currNamedThing);
                            }
                            ParameterUtilTool.removeParameterType(nodeType, paramType);
                            modified = true;
                        }
                        if (EParameterFieldType.SCHEMA_REFERENCE.equals(param.getFieldType())) {
                            //$NON-NLS-1$
                            String schemaTypeName = ":" + EParameterName.SCHEMA_TYPE.getName();
                            //$NON-NLS-1$
                            String repSchemaTypeName = ":" + EParameterName.REPOSITORY_SCHEMA_TYPE.getName();
                            paramType = getParameterType(nodeType, oldParamName + schemaTypeName);
                            if (paramType != null) {
                                paramType.setName(param.getName() + schemaTypeName);
                            }
                            paramType = getParameterType(nodeType, oldParamName + repSchemaTypeName);
                            if (paramType != null) {
                                paramType.setName(param.getName() + repSchemaTypeName);
                            }
                        }
                    } else {
                        processUnmappedElementParameter(props, nodeType, (GenericElementParameter) param, currNamedThing);
                    }
                } else {
                    if (EParameterFieldType.SCHEMA_REFERENCE.equals(param.getFieldType())) {
                        String paramName = param.getName();
                        schemaParamMap.put(paramName, props.getProperty(currComponentName + IGenericConstants.EXP_SEPARATOR + paramName + IGenericConstants.EXP_SEPARATOR + "connector"));
                        String oldParamName = props.getProperty(currComponentName + IGenericConstants.EXP_SEPARATOR + paramName);
                        //$NON-NLS-1$
                        String schemaTypeName = ":" + EParameterName.SCHEMA_TYPE.getName();
                        //$NON-NLS-1$
                        String repSchemaTypeName = ":" + EParameterName.REPOSITORY_SCHEMA_TYPE.getName();
                        ElementParameterType paramType = getParameterType(nodeType, oldParamName + schemaTypeName);
                        if (paramType != null) {
                            paramType.setName(param.getName() + schemaTypeName);
                        }
                        paramType = getParameterType(nodeType, oldParamName + repSchemaTypeName);
                        if (paramType != null) {
                            paramType.setName(param.getName() + repSchemaTypeName);
                        }
                    }
                }
            }
            // Migrate schemas
            Map<String, MetadataType> metadatasMap = new HashMap<>();
            EList<MetadataType> metadatas = nodeType.getMetadata();
            for (MetadataType metadataType : metadatas) {
                metadatasMap.put(metadataType.getConnector(), metadataType);
            }
            Iterator<Entry<String, String>> schemaParamIter = schemaParamMap.entrySet().iterator();
            //$NON-NLS-1$
            String uniqueName = ParameterUtilTool.getParameterValue(nodeType, "UNIQUE_NAME");
            while (schemaParamIter.hasNext()) {
                Entry<String, String> schemaParamEntry = schemaParamIter.next();
                String newParamName = schemaParamEntry.getKey();
                String connectorMapping = schemaParamEntry.getValue();
                //$NON-NLS-1$
                String oldConnector = connectorMapping.split("->")[0];
                //$NON-NLS-1$
                String newConnector = connectorMapping.split("->")[1];
                MetadataType metadataType = metadatasMap.get(oldConnector);
                if (metadataType != null) {
                    metadataType.setConnector(newConnector);
                    MetadataEmfFactory factory = new MetadataEmfFactory();
                    factory.setMetadataType(metadataType);
                    IMetadataTable metadataTable = factory.getMetadataTable();
                    Schema schema = SchemaUtils.convertTalendSchemaIntoComponentSchema(ConvertionHelper.convert(metadataTable));
                    compProperties.setValue(newParamName, schema);
                }
                if (!oldConnector.equals(newConnector)) {
                    // if connector was changed, we should update the connections
                    for (Object connectionObj : processType.getConnection()) {
                        if (connectionObj instanceof ConnectionType) {
                            ConnectionType connectionType = (ConnectionType) connectionObj;
                            if (connectionType.getSource().equals(uniqueName) && connectionType.getConnectorName().equals(oldConnector)) {
                                connectionType.setConnectorName(newConnector);
                            }
                        }
                    }
                }
            }
            for (Object connectionObj : processType.getConnection()) {
                ConnectionType connection = (ConnectionType) connectionObj;
                if (connection.getSource() != null && connection.getSource().equals(uniqueName)) {
                    if (EConnectionType.FLOW_MAIN.getName().equals(connection.getConnectorName())) {
                        connection.setConnectorName(Connector.MAIN_NAME);
                    }
                }
            }
            if (modified) {
                String serializedProperties = compProperties.toSerialized();
                if (serializedProperties != null) {
                    ElementParameterType pType = //$NON-NLS-1$
                    ParameterUtilTool.createParameterType(//$NON-NLS-1$
                    null, //$NON-NLS-1$
                    "PROPERTIES", serializedProperties);
                    nodeType.getElementParameter().add(pType);
                }
            }
        }
    };
    if (processType != null) {
        boolean modified = false;
        for (Object obj : processType.getNode()) {
            if (obj != null && obj instanceof NodeType) {
                String componentName = ((NodeType) obj).getComponentName();
                String newComponentName = props.getProperty(componentName);
                if (newComponentName == null) {
                    continue;
                }
                IComponentFilter filter = new NameComponentFilter(componentName);
                modified = ModifyComponentsAction.searchAndModify((NodeType) obj, filter, Arrays.<IComponentConversion>asList(conversion)) || modified;
            }
        }
        if (modified) {
            try {
                ProxyRepositoryFactory.getInstance().save(item, true);
                return ExecutionResult.SUCCESS_NO_ALERT;
            } catch (PersistenceException e) {
                ExceptionHandler.process(e);
                return ExecutionResult.FAILURE;
            }
        }
    }
    return ExecutionResult.NOTHING_TO_DO;
}
Also used : MetadataEmfFactory(org.talend.designer.core.model.metadata.MetadataEmfFactory) ComponentProperties(org.talend.components.api.properties.ComponentProperties) HashMap(java.util.HashMap) IComponent(org.talend.core.model.components.IComponent) Schema(org.apache.avro.Schema) NameComponentFilter(org.talend.core.model.components.filters.NameComponentFilter) Properties(java.util.Properties) ComponentProperties(org.talend.components.api.properties.ComponentProperties) ComponentReferenceProperties(org.talend.components.api.properties.ComponentReferenceProperties) ComponentCategory(org.talend.core.model.components.ComponentCategory) ProcessType(org.talend.designer.core.model.utils.emf.talendfile.ProcessType) Entry(java.util.Map.Entry) GenericElementParameter(org.talend.designer.core.generic.model.GenericElementParameter) IElementParameter(org.talend.core.model.process.IElementParameter) Property(org.talend.daikon.properties.property.Property) ConnectionType(org.talend.designer.core.model.utils.emf.talendfile.ConnectionType) EConnectionType(org.talend.core.model.process.EConnectionType) IComponentFilter(org.talend.core.model.components.filters.IComponentFilter) MetadataType(org.talend.designer.core.model.utils.emf.talendfile.MetadataType) NamedThing(org.talend.daikon.NamedThing) ElementParameterType(org.talend.designer.core.model.utils.emf.talendfile.ElementParameterType) IMetadataTable(org.talend.core.model.metadata.IMetadataTable) NodeType(org.talend.designer.core.model.utils.emf.talendfile.NodeType) PersistenceException(org.talend.commons.exception.PersistenceException) ComponentReferenceProperties(org.talend.components.api.properties.ComponentReferenceProperties) IComponentConversion(org.talend.core.model.components.conversions.IComponentConversion)

Example 4 with ComponentCategory

use of org.talend.core.model.components.ComponentCategory in project tdi-studio-se by Talend.

the class EncryptPasswordInComponentsMigrationTask method execute.

/*
     * (non-Javadoc)
     * 
     * @see org.talend.core.model.migration.AbstractItemMigrationTask#execute(org.talend.core.model.properties.Item)
     */
@Override
public ExecutionResult execute(Item item) {
    ProcessType processType = getProcessType(item);
    try {
        boolean modified = false;
        ComponentCategory category = ComponentCategory.getComponentCategoryFromItem(item);
        for (Object nodeObjectType : processType.getNode()) {
            NodeType nodeType = (NodeType) nodeObjectType;
            IComponent component = ComponentsFactoryProvider.getInstance().get(nodeType.getComponentName(), category.getName());
            if (component == null) {
                continue;
            }
            FakeNode fNode = new FakeNode(component);
            for (Object paramObjectType : nodeType.getElementParameter()) {
                ElementParameterType param = (ElementParameterType) paramObjectType;
                IElementParameter paramFromEmf = fNode.getElementParameter(param.getName());
                if (paramFromEmf == null) {
                    // might be some deprecated / removed parameter.
                    continue;
                }
                EParameterFieldType paramFromComponent = paramFromEmf.getFieldType();
                if (EParameterFieldType.PASSWORD == paramFromComponent && param.getValue() != null) {
                    boolean encrypted = true;
                    try {
                        int ind = param.getValue().lastIndexOf(PasswordEncryptUtil.ENCRYPT_KEY);
                        if (ind == -1) {
                            encrypted = false;
                        } else {
                            String value = new StringBuilder(param.getValue()).replace(ind, ind + PasswordEncryptUtil.ENCRYPT_KEY.length(), "").toString();
                            PasswordEncryptUtil.decryptPassword(value);
                        }
                    } catch (Exception e) {
                        encrypted = false;
                    }
                    if (!encrypted) {
                        param.setValue(PasswordEncryptUtil.encryptPassword(param.getValue()) + PasswordEncryptUtil.ENCRYPT_KEY);
                        modified = true;
                    }
                }
            }
        }
        if (modified) {
            ProxyRepositoryFactory factory = ProxyRepositoryFactory.getInstance();
            factory.save(item, true);
        }
    } catch (Exception e) {
        ExceptionHandler.process(e);
        return ExecutionResult.FAILURE;
    }
    return ExecutionResult.SUCCESS_NO_ALERT;
}
Also used : IComponent(org.talend.core.model.components.IComponent) ComponentCategory(org.talend.core.model.components.ComponentCategory) ElementParameterType(org.talend.designer.core.model.utils.emf.talendfile.ElementParameterType) ProcessType(org.talend.designer.core.model.utils.emf.talendfile.ProcessType) EParameterFieldType(org.talend.core.model.process.EParameterFieldType) ProxyRepositoryFactory(org.talend.core.repository.model.ProxyRepositoryFactory) NodeType(org.talend.designer.core.model.utils.emf.talendfile.NodeType) IElementParameter(org.talend.core.model.process.IElementParameter)

Example 5 with ComponentCategory

use of org.talend.core.model.components.ComponentCategory in project tdi-studio-se by Talend.

the class UnifyPasswordEncryption4ParametersInJobMigrationTask method checkNodes.

protected boolean checkNodes(Item item, ProcessType processType) throws Exception {
    boolean modified = checkNodesFromEmf(item, processType);
    if (!modified) {
        // some versions of the job doesn't have any field type saved in the job, so we will check from the existing
        // component field type
        ComponentCategory category = ComponentCategory.getComponentCategoryFromItem(item);
        for (Object nodeObjectType : processType.getNode()) {
            NodeType nodeType = (NodeType) nodeObjectType;
            IComponent component = ComponentsFactoryProvider.getInstance().get(nodeType.getComponentName(), category.getName());
            if (component == null) {
                continue;
            }
            FakeNode fNode = new FakeNode(component);
            for (Object paramObjectType : nodeType.getElementParameter()) {
                ElementParameterType param = (ElementParameterType) paramObjectType;
                IElementParameter paramFromEmf = fNode.getElementParameter(param.getName());
                if (paramFromEmf != null) {
                    if (EParameterFieldType.PASSWORD.equals(paramFromEmf.getFieldType()) && param.getValue() != null) {
                        param.setField(EParameterFieldType.PASSWORD.getName());
                        if (reencryptValueIfNeeded(param)) {
                            modified = true;
                        }
                    }
                }
            }
        }
    }
    return modified;
}
Also used : ElementParameterType(org.talend.designer.core.model.utils.emf.talendfile.ElementParameterType) IComponent(org.talend.core.model.components.IComponent) NodeType(org.talend.designer.core.model.utils.emf.talendfile.NodeType) IElementParameter(org.talend.core.model.process.IElementParameter) ComponentCategory(org.talend.core.model.components.ComponentCategory) FakeNode(org.talend.repository.model.migration.EncryptPasswordInComponentsMigrationTask.FakeNode)

Aggregations

ComponentCategory (org.talend.core.model.components.ComponentCategory)5 IComponent (org.talend.core.model.components.IComponent)3 IElementParameter (org.talend.core.model.process.IElementParameter)3 ElementParameterType (org.talend.designer.core.model.utils.emf.talendfile.ElementParameterType)3 NodeType (org.talend.designer.core.model.utils.emf.talendfile.NodeType)3 IMetadataTable (org.talend.core.model.metadata.IMetadataTable)2 ProcessType (org.talend.designer.core.model.utils.emf.talendfile.ProcessType)2 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1 Properties (java.util.Properties)1 Schema (org.apache.avro.Schema)1 PersistenceException (org.talend.commons.exception.PersistenceException)1 ComponentProperties (org.talend.components.api.properties.ComponentProperties)1 ComponentReferenceProperties (org.talend.components.api.properties.ComponentReferenceProperties)1 IComponentConversion (org.talend.core.model.components.conversions.IComponentConversion)1 IComponentFilter (org.talend.core.model.components.filters.IComponentFilter)1 NameComponentFilter (org.talend.core.model.components.filters.NameComponentFilter)1 IMetadataColumn (org.talend.core.model.metadata.IMetadataColumn)1 EConnectionType (org.talend.core.model.process.EConnectionType)1 EParameterFieldType (org.talend.core.model.process.EParameterFieldType)1