Search in sources :

Example 1 with MappingTypeRetriever

use of org.talend.core.model.metadata.MappingTypeRetriever in project tdi-studio-se by Talend.

the class FixWrongDbTypesMigrationTask 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);
    if (processType == null) {
        return ExecutionResult.NOTHING_TO_DO;
    }
    boolean modified = false;
    for (Object nodeTypeObject : processType.getNode()) {
        NodeType nodeType = (NodeType) nodeTypeObject;
        IComponent component = ComponentsFactoryProvider.getInstance().get(nodeType.getComponentName(), ComponentCategory.CATEGORY_4_DI.getName());
        if (component == null) {
            // in case original component doesn't exist here
            continue;
        }
        FakeNode fNode = new FakeNode(component);
        IElementParameter mappingParameter = fNode.getElementParameterFromField(EParameterFieldType.MAPPING_TYPE);
        if (mappingParameter != null && mappingParameter.getValue() != null) {
            String mappingParameterValue = (String) mappingParameter.getValue();
            MappingTypeRetriever mtr = MetadataTalendType.getMappingTypeRetriever(mappingParameterValue);
            if (mtr == null) {
                continue;
            }
            for (Object metadataObject : nodeType.getMetadata()) {
                MetadataType metadataType = (MetadataType) metadataObject;
                for (Object columnObject : metadataType.getColumn()) {
                    ColumnType columnType = (ColumnType) columnObject;
                    if (columnType.getSourceType() != null && !"".equals(columnType.getSourceType())) {
                        if (mtr.isAdvicedTalendToDbType(columnType.getType(), columnType.getSourceType())) {
                            // correct type already, no need to do anything
                            continue;
                        }
                        List<MappingType> advicedTalendToDbTypes = mtr.getAdvicedTalendToDbTypes(columnType.getType());
                        if (advicedTalendToDbTypes == null) {
                            continue;
                        }
                        String dbType = columnType.getSourceType();
                        for (MappingType type : advicedTalendToDbTypes) {
                            if (type.getDbType().equalsIgnoreCase(dbType)) {
                                columnType.setSourceType(type.getDbType());
                                modified = true;
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
    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 : MappingType(org.talend.core.model.metadata.MappingType) ColumnType(org.talend.designer.core.model.utils.emf.talendfile.ColumnType) MappingTypeRetriever(org.talend.core.model.metadata.MappingTypeRetriever) IComponent(org.talend.core.model.components.IComponent) MetadataType(org.talend.designer.core.model.utils.emf.talendfile.MetadataType) ProcessType(org.talend.designer.core.model.utils.emf.talendfile.ProcessType) NodeType(org.talend.designer.core.model.utils.emf.talendfile.NodeType) PersistenceException(org.talend.commons.exception.PersistenceException) IElementParameter(org.talend.core.model.process.IElementParameter)

Aggregations

PersistenceException (org.talend.commons.exception.PersistenceException)1 IComponent (org.talend.core.model.components.IComponent)1 MappingType (org.talend.core.model.metadata.MappingType)1 MappingTypeRetriever (org.talend.core.model.metadata.MappingTypeRetriever)1 IElementParameter (org.talend.core.model.process.IElementParameter)1 ColumnType (org.talend.designer.core.model.utils.emf.talendfile.ColumnType)1 MetadataType (org.talend.designer.core.model.utils.emf.talendfile.MetadataType)1 NodeType (org.talend.designer.core.model.utils.emf.talendfile.NodeType)1 ProcessType (org.talend.designer.core.model.utils.emf.talendfile.ProcessType)1