Search in sources :

Example 11 with ContextParameterType

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

the class FixUnevenItemContextParametersMigrationTask method execute.

@SuppressWarnings("unchecked")
@Override
public ExecutionResult execute(Item item) {
    EList<ContextType> contexts = null;
    String defualtGroupName = null;
    if (item instanceof ProcessItem) {
        // process, process_mr, process_storm, route, routelet.
        ProcessItem processItem = (ProcessItem) item;
        contexts = processItem.getProcess().getContext();
        defualtGroupName = processItem.getProcess().getDefaultContext();
    } else if (item instanceof JobletProcessItem) {
        JobletProcessItem jobletItem = (JobletProcessItem) item;
        contexts = jobletItem.getJobletProcess().getContext();
        defualtGroupName = jobletItem.getJobletProcess().getDefaultContext();
    } else if (item instanceof ContextItem) {
        ContextItem contextItem = (ContextItem) item;
        contexts = contextItem.getContext();
        defualtGroupName = contextItem.getDefaultContext();
    }
    try {
        ContextType defaultGroup = null;
        for (ContextType context : contexts) {
            if (context.getName().equals(defualtGroupName)) {
                defaultGroup = context;
                break;
            }
        }
        if (defaultGroup == null && contexts.size() > 0) {
            defaultGroup = contexts.get(0);
        }
        boolean contextChanged = false;
        if (defaultGroup != null) {
            Map<String, ContextParameterType> paramMap = new HashMap<String, ContextParameterType>();
            List<String> paramNameList = new ArrayList<String>();
            paramMap.putAll(collectDefaultGroupParams(defaultGroup, paramNameList));
            for (ContextType context : contexts) {
                if (context == defaultGroup) {
                    continue;
                }
                Map<String, ContextParameterType> otherGroupParam = collectDefaultGroupParams(context, paramNameList);
                for (String paramName : otherGroupParam.keySet()) {
                    if (!paramMap.containsKey(paramName)) {
                        paramMap.put(paramName, otherGroupParam.get(paramName));
                    }
                }
            }
            // make sure all groups have the same param list
            for (ContextType context : contexts) {
                EList<ContextParameterType> params = context.getContextParameter();
                List<String> paramNames = new ArrayList<String>(paramNameList);
                for (ContextParameterType param : params) {
                    if (paramNames.contains(param.getName())) {
                        paramNames.remove(param.getName());
                    }
                }
                if (!paramNames.isEmpty()) {
                    contextChanged = true;
                    for (String paramToAdd : paramNames) {
                        ContextParameterType toAdd = paramMap.get(paramToAdd);
                        context.getContextParameter().add(EcoreUtil.copy(toAdd));
                    }
                }
            }
            // change param order if needed
            for (ContextType context : contexts) {
                EList<ContextParameterType> params = context.getContextParameter();
                List<ContextParameterType> copyOfParam = new ArrayList<ContextParameterType>(params);
                for (int i = 0; i < copyOfParam.size(); i++) {
                    ContextParameterType param = copyOfParam.get(i);
                    int indexOf = paramNameList.indexOf(param.getName());
                    if (i != indexOf) {
                        contextChanged = true;
                        params.remove(param);
                        params.add(indexOf, param);
                    }
                }
            }
            // make sure params in different groups have the same repository id and type as default group
            for (ContextType context : contexts) {
                EList<ContextParameterType> params = context.getContextParameter();
                for (ContextParameterType param : params) {
                    ContextParameterType paramDefault = paramMap.get(param.getName());
                    if (!paramDefault.getType().equals(param.getType())) {
                        contextChanged = true;
                        param.setType(paramDefault.getType());
                    }
                    if (paramDefault.getRepositoryContextId() == null && param.getRepositoryContextId() != null || (paramDefault.getRepositoryContextId() != null && !paramDefault.getRepositoryContextId().equals(param.getRepositoryContextId()))) {
                        contextChanged = true;
                        param.setRepositoryContextId(paramDefault.getRepositoryContextId());
                    }
                }
            }
        }
        if (contextChanged) {
            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 : ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) ContextItem(org.talend.core.model.properties.ContextItem) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProcessItem(org.talend.core.model.properties.ProcessItem) JobletProcessItem(org.talend.core.model.properties.JobletProcessItem) JobletProcessItem(org.talend.core.model.properties.JobletProcessItem) PersistenceException(org.talend.commons.exception.PersistenceException) ContextParameterType(org.talend.designer.core.model.utils.emf.talendfile.ContextParameterType)

Example 12 with ContextParameterType

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

the class FixUnevenItemContextParametersMigrationTask method collectDefaultGroupParams.

private Map<String, ContextParameterType> collectDefaultGroupParams(ContextType contextType, List<String> paramNames) {
    EList<ContextParameterType> params = contextType.getContextParameter();
    Map<String, ContextParameterType> paramMap = new HashMap<String, ContextParameterType>();
    for (ContextParameterType param : params) {
        paramMap.put(param.getName(), param);
        if (!paramNames.contains(param.getName())) {
            paramNames.add(param.getName());
        }
    }
    return paramMap;
}
Also used : HashMap(java.util.HashMap) ContextParameterType(org.talend.designer.core.model.utils.emf.talendfile.ContextParameterType)

Example 13 with ContextParameterType

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

the class JSONFileStep3Form method getContextJSONPath.

/**
     * getContextJSONPath.
     * 
     * @return String
     */
private String getContextJSONPath(JSONFileConnection connection) {
    //$NON-NLS-1$
    String contextJSONPath = "";
    if (ConnectionContextHelper.getContextTypeForContextMode(connection, connection.getContextName()) == null) {
        return null;
    }
    EList eList = ConnectionContextHelper.getContextTypeForContextMode(connection, connection.getContextName()).getContextParameter();
    for (int i = 0; i < eList.size(); i++) {
        ContextParameterType parameterType = (ContextParameterType) eList.get(i);
        if (parameterType.getPrompt().contains("JSONFilePath")) {
            //$NON-NLS-1$
            contextJSONPath = parameterType.getValue();
        }
    }
    return contextJSONPath;
}
Also used : EList(org.eclipse.emf.common.util.EList) ContextParameterType(org.talend.designer.core.model.utils.emf.talendfile.ContextParameterType)

Example 14 with ContextParameterType

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

the class UpdateContextParameterCommand method checkNewRepositoryParameters.

/**
     * DOC hcw Comment method "checkAddedParameters".
     * 
     * @param process
     * @param names
     */
private void checkNewRepositoryParameters(IProcess2 process, Set<String> names) {
    // add context in repository
    ContextManagerHelper helper = new ContextManagerHelper(process.getContextManager());
    Set<ContextItem> contextItemList = helper.getContextItems();
    ContextItem item = (ContextItem) result.getParameter();
    // this job contains the repository context group
    if (contextItemList.contains(item)) {
        ContextType contextType = getDefaultContextType(item);
        for (String paramName : names) {
            ContextParameterType contextParameterType = ContextUtils.getContextParameterTypeByName(contextType, paramName);
            // check if there is a parameter with same name
            // IContextParameter paramExisted = helper.getExistedContextParameter(contextParameterType.getName());
            // if (paramExisted == null) {
            helper.addContextParameterType(contextParameterType);
        // }
        }
    }
    ConnectionContextHelper.showContextGroupDialog(process, item, process.getContextManager(), names);
}
Also used : ContextItem(org.talend.core.model.properties.ContextItem) ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) ContextManagerHelper(org.talend.core.ui.context.ContextManagerHelper) ContextParameterType(org.talend.designer.core.model.utils.emf.talendfile.ContextParameterType)

Example 15 with ContextParameterType

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

the class ReviseInvalidContextNamesMigrationTask method execute.

@Override
public ExecutionResult execute(Item item) {
    boolean modified = false;
    if (item instanceof ContextItem) {
        ContextItem contextItem = (ContextItem) item;
        List<ContextType> contextTypes = contextItem.getContext();
        for (ContextType contextType : contextTypes) {
            List<ContextParameterType> contextParameterTypes = contextType.getContextParameter();
            for (ContextParameterType contextParameterType : contextParameterTypes) {
                String name = contextParameterType.getName();
                if (!ContextParameterUtils.isValidParameterName(name)) {
                    String newName = ContextParameterUtils.getValidParameterName(name);
                    contextParameterType.setName(newName);
                    modified = true;
                }
            }
        }
    }
    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 : ContextItem(org.talend.core.model.properties.ContextItem) ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) PersistenceException(org.talend.commons.exception.PersistenceException) ContextParameterType(org.talend.designer.core.model.utils.emf.talendfile.ContextParameterType)

Aggregations

ContextParameterType (org.talend.designer.core.model.utils.emf.talendfile.ContextParameterType)25 ContextType (org.talend.designer.core.model.utils.emf.talendfile.ContextType)19 ContextItem (org.talend.core.model.properties.ContextItem)8 PersistenceException (org.talend.commons.exception.PersistenceException)7 ArrayList (java.util.ArrayList)6 ProcessItem (org.talend.core.model.properties.ProcessItem)5 HashMap (java.util.HashMap)4 EList (org.eclipse.emf.common.util.EList)4 List (java.util.List)3 JobletProcessItem (org.talend.core.model.properties.JobletProcessItem)3 File (java.io.File)2 Test (org.junit.Test)2 IContext (org.talend.core.model.process.IContext)2 IContextParameter (org.talend.core.model.process.IContextParameter)2 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Element (org.dom4j.Element)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1