private void removeObsoleteNodes(
     ExecutionPlanPart parentPlanPart,
     ITransactionHolder transaction,
     List<ExecutionPlanPartBean> parallelNodes,
     int parallelTasksListSize) {
   if (parentPlanPart.getId() != 0) {
     Map<Integer, ExecutionPlanPartBean> existingNodesFromUI =
         new HashMap<Integer, ExecutionPlanPartBean>();
     for (int i = 0; i < parallelTasksListSize; i++) {
       ExecutionPlanPartBean executionPlanPartBean = parallelNodes.get(i);
       if (executionPlanPartBean.getId() != null) {
         existingNodesFromUI.put(executionPlanPartBean.getId(), executionPlanPartBean);
       }
     }
     List<ExecutionPlanPart> childrenParallelPartsFromDb =
         new ArrayList<ExecutionPlanPart>(
             ExecutionPlanPartHelper.getInstance()
                 .getChildrenParts(parentPlanPart, ActionType.PARALLEL, transaction));
     final List<ExecutionPlanPart> partsToDelete = new ArrayList<ExecutionPlanPart>();
     for (ExecutionPlanPart executionPlanPartDb : childrenParallelPartsFromDb) {
       ExecutionPlanPartBean executionPlanPartBean =
           existingNodesFromUI.get(executionPlanPartDb.getId());
       if (executionPlanPartBean == null || !parentPlanPart.isUseParallel()) {
         ExecutionPlanPartHelper.getInstance().delete(executionPlanPartDb, transaction);
       }
     }
   }
 }
  public ExecutionPlanPartBean getPartBeansTree(ExecutionPlan plan) {

    List<ExecutionPlanPart> planParts = ExecutionPlanPartHelper.getInstance().findAllParts(plan);
    Map<Integer, ExecutionPlanPartBean> mapPlanPartIdToPlanPart =
        new HashMap<Integer, ExecutionPlanPartBean>();
    ExecutionPlanPartBean rootPartBean = null;
    for (ExecutionPlanPart executionPlanPart : planParts) {
      int planPartId = executionPlanPart.getId();
      ExecutionPlanPartBean executionPlanPartBean = mapPlanPartIdToPlanPart.get(planPartId);
      if (executionPlanPartBean == null) {
        executionPlanPartBean = hib2Gwt(executionPlanPart);
      }
      mapPlanPartIdToPlanPart.put(planPartId, executionPlanPartBean);

      ExecutionPlanPart parentPart = executionPlanPart.getParent();
      if (parentPart == null) {
        rootPartBean = executionPlanPartBean;
      } else {
        int parentPartId = parentPart.getId();
        ExecutionPlanPartBean parentPartBean = mapPlanPartIdToPlanPart.get(parentPartId);
        List<ExecutionPlanPartBean> childrenParts = parentPartBean.getChildrenParts();
        if (childrenParts == null) {
          childrenParts = new ArrayList<ExecutionPlanPartBean>();
          parentPartBean.setChildrenParts(childrenParts);
        }
        childrenParts.add(executionPlanPartBean);
      }
    }
    return rootPartBean;
  }
  public ExecutionPlanPartBean saveExecutionPlanPartBean(
      ExecutionPlanPartBean viewBean, User user, ITransactionHolder transactionPrm)
      throws BusinessException, ClientBusinessException {
    HibernatePersistenceAdapter persistenceAdapter = PersistenceUtil.getPersistenceAdapter();
    ExecutionPlanPart planPart = null;
    ITransactionHolder transaction = null;
    boolean isLocalTransaction = transactionPrm == null;
    try {
      if (isLocalTransaction) {
        transaction = persistenceAdapter.createTransaction();
      } else {
        transaction = transactionPrm;
      }
      planPart = gwt2Hib(viewBean, transaction);

      ActionType actionType = ActionType.get(planPart.getType());
      if (viewBean.getId() == null && actionType != ActionType.PARALLEL) {
        ExecutionPlanPart executionPlanPart =
            ExecutionPlanPartHelper.getInstance().findWithSameParentsAndType(planPart, transaction);
        if (executionPlanPart != null)
          throw new BusinessException(
              "executionPlan.part.saveError.uniqueType", planPart.getType());
      }
      if (planPart.isUseParallel()) {
        planPart.setTask(null);
        viewBean.setTaskBean(null);
      }

      ExecutionPlanPartHandler.getInstance().saveOrUpdate(planPart, user, transaction);

      viewBean.setId(planPart.getId());
      saveOrUpdateParallelParts(viewBean, planPart, user, transaction);

    } catch (BusinessException e) {
      throw e;
    } catch (Throwable ex) {
      if (isLocalTransaction && transaction != null && !transaction.isClosed()) {
        try {
          transaction.rollbackAndClose();
        } catch (DBException e1) {
          log.error(e1.getMessage(), e1);
        }
      }
      throw new SystemException(ex);
    } finally {
      if (isLocalTransaction && transaction != null && !transaction.isClosed()) {
        try {
          transaction.commitAndClose();
        } catch (Exception e) {
          log.error(e.getMessage(), e);
        }
      }
    }

    return hib2Gwt(planPart);
  }
  /*
   * (non-Javadoc)
   *
   * @see org.talend.gwtadministrator.server.remoterepositorymgt.view.AbstractViewAdapter#hib2Gwt(java.lang.Object)
   */
  @Override
  public ExecutionPlanPartBean hib2Gwt(ExecutionPlanPart hibPlanPart) {
    ExecutionPlanPartBean executionPlanPartBean = new ExecutionPlanPartBean();
    executionPlanPartBean.setId(hibPlanPart.getId());
    if (hibPlanPart.getTask() != null) {
      ExecutionTaskBean taskBean =
          ExecutionTaskViewAdapter.getInstance().hib2Gwt(hibPlanPart.getTask(), false);
      executionPlanPartBean.setTaskBean(taskBean);
    }
    ExecutionPlan executionPlan = hibPlanPart.getExecutionPlan();
    ExecutionPlanBean planBean = PlanViewAdapter.getInstance().hib2Gwt(executionPlan);
    executionPlanPartBean.setPlanBean(planBean);
    executionPlanPartBean.setType(ActionType.get(hibPlanPart.getType()));
    executionPlanPartBean.setStatus(hibPlanPart.getStatus());
    executionPlanPartBean.setStartDate(hibPlanPart.getStartDate());
    executionPlanPartBean.setEndDate(hibPlanPart.getEndDate());
    executionPlanPartBean.setUseParallel(hibPlanPart.isUseParallel());
    executionPlanPartBean.setMaxThreads(hibPlanPart.getMaxThreads());

    if (hibPlanPart.isUseParallel()) {
      List<ExecutionPlanPart> childrenParallelPartsFromDb =
          new ArrayList<ExecutionPlanPart>(
              ExecutionPlanPartHelper.getInstance()
                  .getChildrenParts(hibPlanPart, ActionType.PARALLEL));
      final List<ExecutionPlanPartBean> parallelParts = new ArrayList<ExecutionPlanPartBean>();
      for (ExecutionPlanPart executionPlanPartDb : childrenParallelPartsFromDb) {
        parallelParts.add(hib2Gwt(executionPlanPartDb));
      }
      executionPlanPartBean.setParallelParts(parallelParts);
    }

    PlanPartWrapper partWrapper = new PlanPartWrapper(hibPlanPart);
    TaskExecutionBasicStatus taskExecutionBasicStatus = partWrapper.getExecutionBasicStatus();
    if (taskExecutionBasicStatus != null) {
      executionPlanPartBean.setTaskExecutionHistoryBasicStatus(taskExecutionBasicStatus.toString());
    }

    return executionPlanPartBean;
  }
 public PartStatusBean[] getPartsStatusByIds(Integer[] ids) {
   List<PartStatusBean> statuses = new ArrayList<PartStatusBean>();
   HibernatePersistenceAdapter persistenceAdapter = PersistenceUtil.getPersistenceAdapter();
   ITransactionHolder transaction = null;
   try {
     transaction = persistenceAdapter.createTransaction();
     if (ids != null) {
       for (Integer id : ids) {
         ExecutionPlanPart part = ExecutionPlanPartHelper.getInstance().findById(id, transaction);
         if (part != null) {
           PartStatusBean bean = new PartStatusBean();
           bean.setId(part.getId());
           bean.setStatus(part.getStatus());
           PlanPartWrapper wrapper = new PlanPartWrapper(part);
           TaskExecutionBasicStatus taskExecutionBasicStatus =
               wrapper.getExecutionBasicStatus(transaction);
           if (taskExecutionBasicStatus != null) {
             bean.setTaskExecutionHistoryBasicStatus(taskExecutionBasicStatus.toString());
           }
           statuses.add(bean);
         }
       }
     }
   } catch (Throwable ex) {
     throw new SystemException(ex);
   } finally {
     if (transaction != null && !transaction.isClosed()) {
       try {
         transaction.commitAndClose();
       } catch (Exception e) {
         log.error(e.getMessage(), e);
       }
     }
   }
   return statuses.toArray(new PartStatusBean[statuses.size()]);
 }