/**
  * This method is used to remove the <code>JobContextParameter</code> in <code>JobContext</code>,
  * using the combination of <code>sourceId</code> and <code>name</code> can identify the unique
  * <code>JobContextParameter</code>.
  *
  * @param sourceId
  * @param name
  */
 private void removeParameterFromContext(String sourceId, String name) {
   List<IContext> list = contextManager.getListContext();
   if (list != null && list.size() > 0) {
     for (int i = 0; i < list.size(); i++) {
       IContext context = list.get(i);
       List<IContextParameter> contextParameters = context.getContextParameterList();
       List<IContextParameter> movedList = new ArrayList<IContextParameter>();
       if (contextParameters != null && contextParameters.size() > 0) {
         for (int j = 0; j < contextParameters.size(); j++) {
           IContextParameter contextPara = contextParameters.get(j);
           String tempSourceId = contextPara.getSource();
           String tempParaName = contextPara.getName();
           if (tempSourceId.equals(sourceId) && tempParaName.equals(name)) {
             movedList.add(contextPara);
             contextParameters.remove(j);
             if (mapParams.get(context.getName()) != null) {
               mapParams.get(context.getName()).addAll(movedList);
               mapParams.put(context.getName(), mapParams.get(context.getName()));
             } else {
               mapParams.put(context.getName(), movedList);
             }
             break;
           }
         }
       }
     }
   }
 }
    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.gef.commands.Command#execute()
     */
    @Override
    public void execute() {
      boolean modified = false;
      if (modelManager.getContextManager() != null) {
        originalName = param.getName();
        for (IContext context : modelManager.getContextManager().getListContext()) {
          for (IContextParameter contextParameter : context.getContextParameterList()) {
            String tempSourceId = contextParameter.getSource();
            if (originalName.equals(contextParameter.getName()) && tempSourceId.equals(sourceId)) {
              contextParameter.setName(newName);

              if (contextParameter.getPrompt().equals(originalName + "?")) {
                contextParameter.setPrompt(newName + "?");
              }
              modified = true;
            }
          }
        }
        param.setName(newName);
        if (param.getPrompt().equals(originalName + "?")) {
          param.setPrompt(newName + "?");
        }
      }
      if (modified) {
        updateRelation(originalName, newName);
      }
    }
 /*
  * check same ContextParameter or not.
  */
 private boolean checkIsSameContextParameter() {
   List<ContextItem> allContextItem = ContextUtils.getAllContextItem();
   for (IContext context : process.getContextManager().getListContext()) {
     for (IContextParameter param : context.getContextParameterList()) {
       if (allContextItem != null) {
         ContextItem contextItem =
             ContextUtils.getContextItemById(allContextItem, param.getSource());
         ContextType contextType =
             ContextUtils.getContextTypeByName(contextItem, context.getName(), true);
         ContextParameterType contextParameterType =
             ContextUtils.getContextParameterTypeByName(contextType, param.getName());
         if (!ContextUtils.samePropertiesForContextParameter(param, contextParameterType)) {
           return false;
         }
         // if don't open the job to run it(not use the "Detect and update all jobs"), will show
         // UpdateDetectionDialog to update the context ,after updated the item, the
         // contextComboViewer still
         // set the old one , so need refresh.
         IContext runJobViewContext = getSelectedContext();
         if (runJobViewContext != null) {
           for (IContextParameter tempContext : runJobViewContext.getContextParameterList()) {
             if (tempContext.getName().equals(contextParameterType.getName())
                 && !ContextUtils.samePropertiesForContextParameter(
                     tempContext, contextParameterType)) {
               return false;
             }
           }
         }
       }
     }
   }
   return true;
 }
 private void checkContextGroupSource() {
   IContextManager contextManager = getContextManager();
   if (helper == null) {
     return;
   }
   if (contextManager != null) {
     helper.initHelper(contextManager);
     Map<String, Item> items = new HashMap<String, Item>();
     boolean needRefresh = false;
     for (IContextParameter param : contextManager.getDefaultContext().getContextParameterList()) {
       if (!param.isBuiltIn()) {
         String source = param.getSource();
         Item sourceItem = items.get(source);
         if (sourceItem == null) {
           sourceItem = ContextUtils.getRepositoryContextItemById(source);
         }
         if (sourceItem == null) { // source not found
           needRefresh = true;
           param.setSource(IContextParameter.BUILT_IN);
           propagateType(contextManager, param);
         } else {
           items.put(source, sourceItem);
         }
       }
     }
     if (needRefresh) {
       setModifiedFlag(contextManager);
       modelManager.refresh();
     }
   }
 }
 private void propagateType(IContextManager contextManager, IContextParameter param) {
   for (IContext context : contextManager.getListContext()) {
     IContextParameter paramToModify = context.getContextParameter(param.getName());
     paramToModify.setType(param.getType());
     paramToModify.setComment(param.getComment());
     paramToModify.setSource(param.getSource());
   }
 }
 private void initExistedParametersMap() {
   if (!isValid(manager)) {
     return;
   }
   itemNameToParametersMap.clear();
   for (IContextParameter param : manager.getDefaultContext().getContextParameterList()) {
     if (!param.isBuiltIn()) {
       final String source = param.getSource();
       Set<String> paramSet = itemNameToParametersMap.get(source);
       if (paramSet == null) {
         paramSet = new HashSet<String>();
         itemNameToParametersMap.put(source, paramSet);
       }
       paramSet.add(param.getName());
     }
   }
 }
  private void setPropertyValue(
      IContextModelManager manager,
      Object dataElement,
      String contextParaName,
      int columnIndex,
      Object newValue) {
    String currentColumnName = getColumnProperty(columnIndex);
    if (this.groupModel.isPartOfAGroup(columnIndex)) {
      String columnGroupName = this.groupModel.getColumnGroupByIndex(columnIndex).getName();
      IContextManager contextManger = manager.getContextManager();
      if (contextManger != null) {
        // change the property of context such as prompt,promptyNeeded,value etc.
        List<Object> list = new ArrayList<Object>();
        list.add(dataElement);

        IContextParameter para = null;
        para = getRealParameter(contextManger, columnGroupName, dataElement);
        if (para == null) {
          return;
        }
        Command cmd =
            new SetContextGroupParameterCommand(manager, para, currentColumnName, newValue);
        runCommand(cmd, manager);
        if (currentColumnName.equals(ContextTableConstants.COLUMN_CHECK_PROPERTY)) {
          manager.refresh();
        }
      }
    } else {
      if (currentColumnName.equals(ContextTableConstants.COLUMN_TYPE_PROPERTY)) {
        ContextTableTabParentModel parent = (ContextTableTabParentModel) dataElement;
        IContextParameter contextPara = parent.getContextParameter();
        if (contextPara.getType() == ((String) newValue)) {
          return;
        }
        String newType = getRealType((String) newValue);
        contextPara.setType(newType);

        Command cmd = new SetContextTypeCommand(manager, contextPara, newType);
        runCommand(cmd, manager);
      } else if (currentColumnName.equals(ContextTableConstants.COLUMN_NAME_PROPERTY)) {
        ContextTableTabParentModel parent = (ContextTableTabParentModel) dataElement;
        IContextParameter contextPara = parent.getContextParameter();
        String sourceId = contextPara.getSource();
        String newParaName = (String) newValue;

        if (manager.getContextManager() instanceof JobContextManager) {
          // in case joblet rename will propagate to the job,just record it
          JobContextManager contextManager = (JobContextManager) manager.getContextManager();
          contextManager.addNewName(newParaName, contextPara.getName());
          contextManager.setModified(true);
        }
        Command cmd = new SetContextNameCommand(manager, contextPara, newParaName, sourceId);
        runCommand(cmd, manager);
      } else if (currentColumnName.equals(ContextTableConstants.COLUMN_COMMENT_PROPERTY)) {
        ContextTableTabParentModel parent = (ContextTableTabParentModel) dataElement;
        IContextParameter contextPara = parent.getContextParameter();
        if (contextPara.getComment() == ((String) newValue)) {
          return;
        }
        Command cmd = new setContextCommentCommand(manager, contextPara, (String) newValue);
        runCommand(cmd, manager);
      }
    }
  }
  public static List<ContextTableTabParentModel> constructContextDatas(
      List<IContextParameter> contextDatas) {
    List<ContextTableTabParentModel> output = new ArrayList<ContextTableTabParentModel>();
    if (!contextDatas.isEmpty()) {
      int i = 0;
      for (IContextParameter para : contextDatas) {
        String sourceId = para.getSource();
        if (IContextParameter.BUILT_IN.equals(sourceId)) {
          sourceId = para.getSource();
          ContextTableTabParentModel firstLevelNode = new ContextTableTabParentModel();
          String sourceLabel = sourceId;
          ContextItem contextItem = ContextUtils.getContextItemById2(sourceId);
          if (contextItem != null) {
            sourceLabel = contextItem.getProperty().getLabel();
            final ProjectManager pm = ProjectManager.getInstance();
            if (!pm.isInCurrentMainProject(contextItem)) {
              final Project project = pm.getProject(contextItem);
              if (project != null) {
                firstLevelNode.setProjectLabel(project.getLabel());
              }
            }
          }
          firstLevelNode.setOrder(i);
          firstLevelNode.setSourceName(sourceLabel);
          firstLevelNode.setSourceId(sourceId);
          firstLevelNode.setContextParameter(para);
          output.add(firstLevelNode);
        } else {
          ContextTableTabParentModel firstLevelNode = null;
          if (sourceId != null) {
            firstLevelNode = lookupContextParentForNonBuiltinNode(sourceId, output);
            if (firstLevelNode == null) {
              firstLevelNode = new ContextTableTabParentModel();
              output.add(firstLevelNode);
              String sourceLabel = sourceId;
              // the item maybe a joblet item now
              Item contextItem = ContextUtils.getRepositoryContextItemById(sourceId);
              if (contextItem != null) {
                sourceLabel = contextItem.getProperty().getLabel();
                final ProjectManager pm = ProjectManager.getInstance();
                if (!pm.isInCurrentMainProject(contextItem)) {
                  final Project project = pm.getProject(contextItem);
                  if (project != null) {
                    firstLevelNode.setProjectLabel(project.getLabel());
                  }
                }
              }
              firstLevelNode.setSourceName(sourceLabel);
              firstLevelNode.setOrder(i);
              firstLevelNode.setSourceId(sourceId);
            }

            ContextTableTabChildModel child = new ContextTableTabChildModel();
            child.setSourceId(sourceId);
            child.setContextParameter(para);
            child.setParent(firstLevelNode);
            firstLevelNode.addChild(child);
          }
        }
        i++;
      }
    }
    return output;
  }
 private void removeChildModelInGroupBySource(ContextTableTabChildModel child) {
   IContextParameter contextPara = child.getContextParameter();
   String sourceId = contextPara.getSource();
   String contextName = contextPara.getName();
   modelManager.onContextRemoveParameter(getContextManager(), contextName, sourceId);
 }