Search in sources :

Example 41 with NamedThing

use of org.talend.daikon.NamedThing 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 42 with NamedThing

use of org.talend.daikon.NamedThing in project tdi-studio-se by Talend.

the class Component method processCodegenPropInfos.

protected void processCodegenPropInfos(List<CodegenPropInfo> propList, Properties props, String fieldString) {
    for (NamedThing prop : props.getProperties()) {
        if (prop instanceof Properties) {
            if (prop instanceof ComponentReferenceProperties) {
                ComponentReferenceProperties crp = (ComponentReferenceProperties) prop;
                crp.componentInstanceId.setTaggedValue(IGenericConstants.ADD_QUOTES, true);
                crp.referenceDefinitionName.setTaggedValue(IGenericConstants.ADD_QUOTES, true);
            }
            CodegenPropInfo childPropInfo = new CodegenPropInfo();
            if (fieldString.equals("")) {
                //$NON-NLS-1$
                //$NON-NLS-1$
                childPropInfo.fieldName = "." + prop.getName();
            } else {
                //$NON-NLS-1$
                childPropInfo.fieldName = fieldString + "." + prop.getName();
            }
            childPropInfo.className = prop.getClass().getName();
            childPropInfo.props = (Properties) prop;
            propList.add(childPropInfo);
            processCodegenPropInfos(propList, childPropInfo.props, childPropInfo.fieldName);
        }
    }
}
Also used : NamedThing(org.talend.daikon.NamedThing) ComponentProperties(org.talend.components.api.properties.ComponentProperties) ComponentReferenceProperties(org.talend.components.api.properties.ComponentReferenceProperties) Properties(org.talend.daikon.properties.Properties) ComponentReferenceProperties(org.talend.components.api.properties.ComponentReferenceProperties)

Example 43 with NamedThing

use of org.talend.daikon.NamedThing in project tdi-studio-se by Talend.

the class Component method getWizardDefinition.

private ComponentWizardDefinition getWizardDefinition(ComponentProperties componentProperties) {
    ComponentWizardDefinition definition = null;
    if (componentProperties == null) {
        return null;
    }
    ComponentService service = ComponentsUtils.getComponentService();
    List<ComponentWizard> componentWizards = service.getComponentWizardsForProperties(componentProperties, null);
    for (ComponentWizard componentWizard : componentWizards) {
        definition = componentWizard.getDefinition();
        // Can we ensure it is the same wizard with metadata connection wizard by this way?
        if (definition.isTopLevel()) {
            return definition;
        }
    }
    List<NamedThing> namedThings = componentProperties.getProperties();
    for (NamedThing namedThing : namedThings) {
        if (namedThing instanceof ComponentProperties) {
            definition = getWizardDefinition((ComponentProperties) namedThing);
            if (definition != null && definition.isTopLevel()) {
                return definition;
            }
        }
    }
    return null;
}
Also used : ComponentWizard(org.talend.components.api.wizard.ComponentWizard) ComponentProperties(org.talend.components.api.properties.ComponentProperties) ComponentWizardDefinition(org.talend.components.api.wizard.ComponentWizardDefinition) ComponentService(org.talend.components.api.service.ComponentService) NamedThing(org.talend.daikon.NamedThing)

Example 44 with NamedThing

use of org.talend.daikon.NamedThing in project tdi-studio-se by Talend.

the class NewSalesforceWizardMigrationTask method updateMetadataTable.

@Override
protected boolean updateMetadataTable(Connection oldConnection, Connection genericConnection, ComponentProperties componentProperties) {
    boolean modified = false;
    if (oldConnection == null || genericConnection == null || componentProperties == null) {
        return modified;
    }
    Set<MetadataTable> tables = ConnectionHelper.getTables(oldConnection);
    Set<MetadataTable> newTables = new HashSet<>();
    newTables.addAll(tables);
    for (MetadataTable metaTable : newTables) {
        try {
            Object object = ReflectionUtils.newInstance(REFLECTION_SALESFORCE_MODULE_PROPERTIES, componentProperties.getClass().getClassLoader(), new Object[] { metaTable.getName() });
            if (object != null && object instanceof ComponentProperties) {
                ComponentProperties salesforceModuleProperties = (ComponentProperties) object;
                //$NON-NLS-1$
                salesforceModuleProperties.getProperties("connection").copyValuesFrom(componentProperties, true, false);
                //$NON-NLS-1$
                NamedThing tmp = salesforceModuleProperties.getProperty("moduleName");
                //$NON-NLS-1$
                ((Property) tmp).setTaggedValue(IGenericConstants.REPOSITORY_VALUE, "moduleName");
                ((Property) tmp).setValue(metaTable.getLabel());
                TaggedValue serializedPropsTV = CoreFactory.eINSTANCE.createTaggedValue();
                serializedPropsTV.setTag(IComponentConstants.COMPONENT_PROPERTIES_TAG);
                serializedPropsTV.setValue(salesforceModuleProperties.toSerialized());
                metaTable.getTaggedValue().add(serializedPropsTV);
                TaggedValue schemaPropertyTV = CoreFactory.eINSTANCE.createTaggedValue();
                schemaPropertyTV.setTag(IComponentConstants.COMPONENT_SCHEMA_TAG);
                schemaPropertyTV.setValue(SCHEMA_SCHEMA);
                metaTable.getTaggedValue().add(schemaPropertyTV);
                Schema schema = SchemaUtils.convertTalendSchemaIntoComponentSchema(metaTable);
                salesforceModuleProperties.setValue(SCHEMA_SCHEMA, schema);
                ((orgomg.cwm.objectmodel.core.Package) genericConnection).getOwnedElement().add(metaTable);
                modified = true;
            }
        } catch (Exception e) {
            ExceptionHandler.process(e);
        }
    }
    return modified;
}
Also used : ComponentProperties(org.talend.components.api.properties.ComponentProperties) TaggedValue(orgomg.cwm.objectmodel.core.TaggedValue) Schema(org.apache.avro.Schema) NamedThing(org.talend.daikon.NamedThing) IOException(java.io.IOException) MetadataTable(org.talend.core.model.metadata.builder.connection.MetadataTable) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject) Property(org.talend.daikon.properties.property.Property) HashSet(java.util.HashSet)

Example 45 with NamedThing

use of org.talend.daikon.NamedThing in project tdi-studio-se by Talend.

the class Salesforce620WizardMigration method updateSubProperties.

/**
     * DOC nrousseau Comment method "updateSubProperties".
     * 
     * @param properties
     * @param newProperties
     */
private void updateSubProperties(ComponentProperties properties, ComponentProperties newProperties) {
    if (newProperties == null) {
        return;
    }
    for (NamedThing nt : properties.getProperties()) {
        if (nt instanceof Property) {
            Property property = (Property) nt;
            Object storedValue = property.getStoredValue();
            if (storedValue instanceof String) {
                String stringValue = (String) storedValue;
                if (ContextParameterUtils.isContainContextParam(stringValue)) {
                    continue;
                }
                Property newProperty = (Property) newProperties.getProperty(property.getName());
                if (newProperty != null) {
                    if (GenericTypeUtils.isBooleanType(newProperty)) {
                        if (stringValue.isEmpty()) {
                            property.setValue(Boolean.FALSE);
                        } else {
                            property.setValue(new Boolean(stringValue));
                        }
                    } else if (GenericTypeUtils.isEnumType(newProperty) && (!(newProperty instanceof EnumProperty))) {
                        property.setStoredValue(TalendQuoteUtils.removeQuotes(stringValue));
                    } else if (GenericTypeUtils.isEnumType(newProperty)) {
                        List<?> propertyPossibleValues = ((Property<?>) newProperty).getPossibleValues();
                        if (propertyPossibleValues != null) {
                            for (Object possibleValue : propertyPossibleValues) {
                                if (possibleValue.toString().equals(storedValue)) {
                                    property.setStoredValue(possibleValue);
                                    break;
                                }
                            }
                        }
                    }
                }
            } else {
                Property newProperty = (Property) newProperties.getProperty(property.getName());
                if (GenericTypeUtils.isBooleanType(newProperty)) {
                    // value can only be null (so false)
                    property.setValue(Boolean.FALSE);
                }
            }
        } else if (nt instanceof ComponentProperties) {
            updateSubProperties((ComponentProperties) nt, (ComponentProperties) newProperties.getProperty(nt.getName()));
        }
    }
}
Also used : ComponentProperties(org.talend.components.api.properties.ComponentProperties) EnumProperty(org.talend.daikon.properties.property.EnumProperty) NamedThing(org.talend.daikon.NamedThing) Property(org.talend.daikon.properties.property.Property) EnumProperty(org.talend.daikon.properties.property.EnumProperty)

Aggregations

NamedThing (org.talend.daikon.NamedThing)71 SimpleNamedThing (org.talend.daikon.SimpleNamedThing)34 ArrayList (java.util.ArrayList)33 Test (org.junit.Test)21 Property (org.talend.daikon.properties.property.Property)17 ComponentProperties (org.talend.components.api.properties.ComponentProperties)15 ComponentException (org.talend.components.api.exception.ComponentException)14 Schema (org.apache.avro.Schema)11 SandboxedInstance (org.talend.daikon.sandbox.SandboxedInstance)9 ValidationResult (org.talend.daikon.properties.ValidationResult)8 Form (org.talend.daikon.properties.presentation.Form)8 List (java.util.List)7 IOException (java.io.IOException)6 PresentationItem (org.talend.daikon.properties.PresentationItem)6 Properties (org.talend.daikon.properties.Properties)6 ComponentWizard (org.talend.components.api.wizard.ComponentWizard)5 GenericElementParameter (org.talend.designer.core.generic.model.GenericElementParameter)5 PropertyPathConnector (org.talend.components.api.component.PropertyPathConnector)4 ComponentWizardDefinition (org.talend.components.api.wizard.ComponentWizardDefinition)4 SearchRecordTypeDesc (org.talend.components.netsuite.client.model.SearchRecordTypeDesc)4