Search in sources :

Example 1 with JobContext

use of org.talend.core.model.context.JobContext in project tdi-studio-se by Talend.

the class UpdateContextParameterCommand method doExecute.

private void doExecute() {
    if (result == null) {
        return;
    }
    Object job = result.getJob();
    if (job == null) {
        return;
    }
    if (job instanceof IProcess2) {
        IProcess2 process = (IProcess2) job;
        ContextParameterMap deleteParameters = new ContextParameterMap();
        Object updateObject = result.getUpdateObject();
        List<IContext> listContext = process.getContextManager().getListContext();
        if (updateObject instanceof Set) {
            Set<String> names = (Set<String>) updateObject;
            if (result.getResultType() == EUpdateResult.ADD && result.isChecked()) {
                // check parameters that have been added to repository context group
                checkNewRepositoryParameters(process, names);
                return;
            }
            for (IContext context : listContext) {
                for (IContextParameter param : context.getContextParameterList()) {
                    ContextItem item = null;
                    if (names != null && names.contains(param.getName())) {
                        switch(result.getResultType()) {
                            case DELETE:
                                item = (ContextItem) result.getParameter();
                                if (item != null && item.getProperty().getId().equals(param.getSource()) && result.isChecked()) {
                                    // delete it later
                                    deleteParameters.addParameter(context, param);
                                } else {
                                    param.setSource(IContextParameter.BUILT_IN);
                                }
                                break;
                            case UPDATE:
                                item = (ContextItem) result.getParameter();
                                if (item != null && item.getProperty().getId().equals(param.getSource()) && result.isChecked()) {
                                    ContextUtils.updateParameterFromRepository(item, param, context.getName());
                                } else {
                                    param.setSource(IContextParameter.BUILT_IN);
                                }
                                break;
                            case RENAME:
                                List<Object> parameter = (List<Object>) result.getParameter();
                                if (parameter.size() >= 3) {
                                    item = (ContextItem) parameter.get(0);
                                    String sourceId = item.getProperty().getId();
                                    String oldName = (String) parameter.get(1);
                                    String newName = (String) parameter.get(2);
                                    if (oldName.equals(param.getName()) && sourceId.equals(param.getSource())) {
                                        if (newName != null) {
                                            param.setName(newName);
                                            ContextUtils.updateParameterFromRepository(item, param, context.getName());
                                        }
                                    }
                                }
                                break;
                            // built-in
                            case BUIL_IN:
                            default:
                                param.setSource(IContextParameter.BUILT_IN);
                                break;
                        }
                    }
                }
            }
        }
        if (updateObject instanceof JobContext) {
            if (result.getResultType() == EUpdateResult.ADD && result.getUpdateType() == EUpdateItemType.CONTEXT_GROUP && result.isChecked()) {
                IContext context = (IContext) updateObject;
                String name = context.getName();
                if (!listContext.contains(context) && result.getParameter() instanceof ContextItem) {
                    ContextItem item = (ContextItem) result.getParameter();
                    JobContext newContext = new JobContext(name);
                    List<IContextParameter> newParamList = new ArrayList<IContextParameter>();
                    newContext.setContextParameterList(newParamList);
                    JobContextParameter param = null;
                    // add other context params to the new group
                    for (IContextParameter contextParam : process.getContextManager().getDefaultContext().getContextParameterList()) {
                        boolean exist = false;
                        for (IContextParameter existParam : context.getContextParameterList()) {
                            if (contextParam.getName().equals(existParam.getName())) {
                                exist = true;
                            }
                        }
                        if (exist) {
                            continue;
                        }
                        param = (JobContextParameter) contextParam.clone();
                        param.setContext(newContext);
                        newParamList.add(param);
                    }
                    // current context params for the new group
                    for (int i = 0; i < context.getContextParameterList().size(); i++) {
                        IContextParameter oldParam = context.getContextParameterList().get(i);
                        param = (JobContextParameter) oldParam.clone();
                        param.setSource(item.getProperty().getId());
                        param.setContext(newContext);
                        newParamList.add(param);
                    }
                    listContext.add(newContext);
                }
            } else if (result.getResultType() == EUpdateResult.DELETE && result.getUpdateType() == EUpdateItemType.CONTEXT_GROUP && result.isChecked()) {
                IContext context = (IContext) updateObject;
                if (result.getParameter() instanceof ContextItem) {
                    ContextItem item = (ContextItem) result.getParameter();
                    List<IContext> listC = new ArrayList<IContext>(listContext);
                    for (IContext con : listC) {
                        if (con.getName().equals(context.getName())) {
                            for (IContextParameter oldParam : con.getContextParameterList()) {
                                if (item.getProperty().getId().equals(oldParam.getSource())) {
                                    listContext.remove(con);
                                }
                            }
                        }
                    }
                }
                return;
            } else if (result.getResultType() == EUpdateResult.RENAME && result.getUpdateType() == EUpdateItemType.CONTEXT_GROUP && result.isChecked()) {
                IContext context = (IContext) updateObject;
                IContextManager contextManager = process.getContextManager();
                Map<IContext, String> renameGroupContext = ((JobContextManager) contextManager).getRenameGroupContext();
                String oldName = renameGroupContext.get(context);
                if (result.getParameter() instanceof ContextItem) {
                    ContextItem item = (ContextItem) result.getParameter();
                    for (IContext con : listContext) {
                        if (con.getName().equals(oldName)) {
                            for (IContextParameter oldParam : con.getContextParameterList()) {
                                if (item.getProperty().getId().equals(oldParam.getSource())) {
                                    con.setName(context.getName());
                                }
                            }
                        }
                    }
                }
            }
            return;
        }
        // delete parameters
        deleteParameters.removeFromContext();
        // update parameters
        if (result.getResultType() == EUpdateResult.RENAME) {
            List<Object> parameter = (List<Object>) result.getParameter();
            if (parameter.size() >= 3) {
                String oldName = (String) parameter.get(1);
                String newName = (String) parameter.get(2);
                UpdateContextVariablesHelper.updateProcessForRenamed(process, oldName, newName);
                // tRunJob parameters rename
                IEditorReference[] reference = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
                List<IProcess2> processes = RepositoryPlugin.getDefault().getDesignerCoreService().getOpenedProcess(reference);
                Map<String, String> renamedMap = new HashMap<String, String>();
                renamedMap.put(newName, oldName);
                UpdateRunJobComponentContextHelper.updateOpenedJobRunJobComponentReference(processes, renamedMap, process.getId(), null);
                try {
                    // perhaps, need optimize.
                    UpdateRunJobComponentContextHelper.updateItemRunJobComponentReference(DesignerPlugin.getDefault().getProxyRepositoryFactory(), renamedMap, process.getId(), null);
                } catch (PersistenceException e) {
                    ExceptionHandler.process(e);
                }
            }
        }
    }
}
Also used : ContextItem(org.talend.core.model.properties.ContextItem) IContext(org.talend.core.model.process.IContext) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IContextParameter(org.talend.core.model.process.IContextParameter) IEditorReference(org.eclipse.ui.IEditorReference) ArrayList(java.util.ArrayList) List(java.util.List) JobContext(org.talend.core.model.context.JobContext) IContextManager(org.talend.core.model.process.IContextManager) JobContextParameter(org.talend.core.model.context.JobContextParameter) IProcess2(org.talend.core.model.process.IProcess2) PersistenceException(org.talend.commons.exception.PersistenceException) JobContextManager(org.talend.core.model.context.JobContextManager)

Example 2 with JobContext

use of org.talend.core.model.context.JobContext in project tdi-studio-se by Talend.

the class JSONConnectionContextHelper method checkAndAddContextVariables.

public static void checkAndAddContextVariables(ContextItem item, IContextManager contextManager, Set<String> addedVars, Set<String> contextGoupNameSet) {
    EList context = item.getContext();
    Map<String, List<ContextParameterTypeImpl>> map = new HashMap<String, List<ContextParameterTypeImpl>>();
    Iterator iterator = context.iterator();
    while (iterator.hasNext()) {
        Object obj = iterator.next();
        if (obj instanceof ContextTypeImpl) {
            ContextTypeImpl contextTypeImpl = (ContextTypeImpl) obj;
            String name = contextTypeImpl.getName();
            EList contextParameters = contextTypeImpl.getContextParameter();
            Iterator contextParas = contextParameters.iterator();
            List<ContextParameterTypeImpl> list = new ArrayList<ContextParameterTypeImpl>();
            while (contextParas.hasNext()) {
                ContextParameterTypeImpl contextParameterType = (ContextParameterTypeImpl) contextParas.next();
                list.add(contextParameterType);
            }
            map.put(name, list);
        }
    }
    if (map.isEmpty()) {
        return;
    }
    String defaultContextName = item.getDefaultContext();
    Set<String> existGroupNameSet = new HashSet<String>();
    for (IContext con : contextManager.getListContext()) {
        existGroupNameSet.add(con.getName());
    }
    if (contextGoupNameSet.isEmpty()) {
        contextGoupNameSet.add(defaultContextName);
    }
    Set<String> alreadyUpdateNameSet = new HashSet<String>();
    for (String key : map.keySet()) {
        for (String groupName : contextGoupNameSet) {
            boolean isExtraGroup = false;
            for (String existGroup : existGroupNameSet) {
                if (key.equals(existGroup)) {
                    isExtraGroup = true;
                    alreadyUpdateNameSet.add(existGroup);
                    break;
                }
            }
            if (key.equals(groupName) || isExtraGroup) {
                List<ContextParameterTypeImpl> list = map.get(key);
                JobContext jobContext = new JobContext(key);
                boolean isExistContext = false;
                if (isExtraGroup) {
                    for (IContext con : contextManager.getListContext()) {
                        if (key.equals(con.getName())) {
                            if (con instanceof JobContext) {
                                jobContext = (JobContext) con;
                                isExistContext = true;
                                break;
                            }
                        }
                    }
                }
                setContextParameter(item, addedVars, list, jobContext);
                if (!isExistContext) {
                    contextManager.getListContext().add(jobContext);
                }
                break;
            }
        }
    }
    // if job context group is not in current add's context,then update context group value to default group value
    existGroupNameSet.removeAll(alreadyUpdateNameSet);
    List<ContextParameterTypeImpl> list = map.get(defaultContextName);
    if (list == null) {
        return;
    }
}
Also used : IContext(org.talend.core.model.process.IContext) HashMap(java.util.HashMap) ContextParameterTypeImpl(org.talend.designer.core.model.utils.emf.talendfile.impl.ContextParameterTypeImpl) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject) ContextTypeImpl(org.talend.designer.core.model.utils.emf.talendfile.impl.ContextTypeImpl) JobContext(org.talend.core.model.context.JobContext) HashSet(java.util.HashSet)

Example 3 with JobContext

use of org.talend.core.model.context.JobContext in project tdi-studio-se by Talend.

the class JSONConnectionContextHelper method checkAndAddContextsVarDND.

/**
     * 
     * wzhang Comment method "checkAndAddContextsVarDND".
     * 
     * @param item
     * @param contextManager
     */
public static void checkAndAddContextsVarDND(ContextItem item, IContextManager contextManager) {
    EList context = item.getContext();
    Map<String, List<ContextParameterTypeImpl>> map = new HashMap<String, List<ContextParameterTypeImpl>>();
    Iterator iterator = context.iterator();
    while (iterator.hasNext()) {
        Object obj = iterator.next();
        if (obj instanceof ContextTypeImpl) {
            ContextTypeImpl contextTypeImpl = (ContextTypeImpl) obj;
            String name = contextTypeImpl.getName();
            EList contextParameters = contextTypeImpl.getContextParameter();
            Iterator contextParas = contextParameters.iterator();
            List<ContextParameterTypeImpl> list = new ArrayList<ContextParameterTypeImpl>();
            while (contextParas.hasNext()) {
                ContextParameterTypeImpl contextParameterType = (ContextParameterTypeImpl) contextParas.next();
                list.add(contextParameterType);
            }
            map.put(name, list);
        }
    }
    if (map.isEmpty()) {
        return;
    }
    contextManager.getListContext().clear();
    String defaultContextName = item.getDefaultContext();
    for (String key : map.keySet()) {
        List<ContextParameterTypeImpl> list = map.get(key);
        JobContext jobContext = new JobContext(key);
        for (ContextParameterTypeImpl contextImpl : list) {
            JobContextParameter contextParam = new JobContextParameter();
            ContextUtils.updateParameter(contextImpl, contextParam);
            contextParam.setSource(item.getProperty().getId());
            contextParam.setContext(jobContext);
            jobContext.getContextParameterList().add(contextParam);
        }
        contextManager.getListContext().add(jobContext);
        if (key.equals(defaultContextName)) {
            contextManager.setDefaultContext(jobContext);
        }
    }
}
Also used : HashMap(java.util.HashMap) ContextParameterTypeImpl(org.talend.designer.core.model.utils.emf.talendfile.impl.ContextParameterTypeImpl) ArrayList(java.util.ArrayList) JobContextParameter(org.talend.core.model.context.JobContextParameter) EList(org.eclipse.emf.common.util.EList) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject) ContextTypeImpl(org.talend.designer.core.model.utils.emf.talendfile.impl.ContextTypeImpl) JobContext(org.talend.core.model.context.JobContext)

Example 4 with JobContext

use of org.talend.core.model.context.JobContext in project tdi-studio-se by Talend.

the class PostgresGenerationManagerTest method testBuildSqlSelect.

@Test
public void testBuildSqlSelect() {
    // without context
    String schema = "school";
    String main_table = "classInfo";
    String lookup_table = "scoreInfo";
    init(schema, main_table, lookup_table);
    PostgresGenerationManager manager = new PostgresGenerationManager();
    String query = manager.buildSqlSelect(component, "grade");
    assertNotNull(query);
    query = query.replaceAll("[\\s]", "");
    String expectedQuery = "\"SELECT" + "\\\"school\\\".\\\"classInfo\\\".\\\"id\\\",\\\"school\\\".\\\"classInfo\\\".\\\"name\\\",\\\"school\\\".\\\"classInfo\\\".\\\"classNum\\\",\\\"school\\\".\\\"scoreInfo\\\".\\\"score\\\"" + "FROM\\\"school\\\".\\\"classInfo\\\",\\\"school\\\".\\\"scoreInfo\\\"" + "WHERE\\\"school\\\".\\\"classInfo\\\".\\\"id\\\"=3\"";
    assertEquals(expectedQuery, query);
    // with context
    schema = "context.schema";
    main_table = "context.main_table";
    lookup_table = "context.lookup";
    init(schema, main_table, lookup_table);
    JobContext newContext = new JobContext("Default");
    List<IContextParameter> newParamList = new ArrayList<IContextParameter>();
    newContext.setContextParameterList(newParamList);
    JobContextParameter param = new JobContextParameter();
    param.setName("schema");
    newParamList.add(param);
    param = new JobContextParameter();
    param.setName("main_table");
    newParamList.add(param);
    param = new JobContextParameter();
    param.setName("lookup");
    newParamList.add(param);
    component.getProcess().getContextManager().setDefaultContext(newContext);
    query = manager.buildSqlSelect(component, "grade");
    query = query.replaceAll("[\\s]", "");
    expectedQuery = "\"SELECT\"+context.schema+\".\"+context.main_table+\".\\\"id\\\",\"+context.schema+\".\"+" + "context.main_table+\".\\\"name\\\",\"+context.schema+\".\"+context.main_table+\".\\\"classNum\\\",\"+" + "context.schema+\".\"+context.lookup+\".\\\"score\\\"FROM\"+context.schema+\".\"+" + "context.main_table+\",\"+context.schema+\".\"+context.lookup+\"WHERE\"+context.schema+\".\"+" + "context.main_table+\".\\\"id\\\"=3\"";
    assertEquals(expectedQuery, query);
}
Also used : JobContextParameter(org.talend.core.model.context.JobContextParameter) ArrayList(java.util.ArrayList) JobContext(org.talend.core.model.context.JobContext) IContextParameter(org.talend.core.model.process.IContextParameter) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)4 JobContext (org.talend.core.model.context.JobContext)4 HashMap (java.util.HashMap)3 List (java.util.List)3 JobContextParameter (org.talend.core.model.context.JobContextParameter)3 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 EList (org.eclipse.emf.common.util.EList)2 IContext (org.talend.core.model.process.IContext)2 IContextParameter (org.talend.core.model.process.IContextParameter)2 IRepositoryViewObject (org.talend.core.model.repository.IRepositoryViewObject)2 ContextParameterTypeImpl (org.talend.designer.core.model.utils.emf.talendfile.impl.ContextParameterTypeImpl)2 ContextTypeImpl (org.talend.designer.core.model.utils.emf.talendfile.impl.ContextTypeImpl)2 Set (java.util.Set)1 IEditorReference (org.eclipse.ui.IEditorReference)1 Test (org.junit.Test)1 PersistenceException (org.talend.commons.exception.PersistenceException)1 JobContextManager (org.talend.core.model.context.JobContextManager)1 IContextManager (org.talend.core.model.process.IContextManager)1 IProcess2 (org.talend.core.model.process.IProcess2)1