protected void checkConstrain(ExecutionPlanPartBean viewBean, ITransactionHolder transaction)
     throws ClientBusinessException {
   // if node type is "After", delete OnOk and OnError nodes
   if (viewBean != null && viewBean.getParent() != null) {
     if (ActionType.AFTER.equals(viewBean.getType())) {
       ExecutionPlanPart parentPart = gwt2Hib(viewBean.getParent(), transaction);
       List<ExecutionPlanPart> parts =
           ExecutionPlanPartHelper.getInstance().findChildrenByPart(parentPart, transaction);
       if (parts != null && parts.size() > 0) {
         for (ExecutionPlanPart part : parts) {
           if (!ActionType.get(part.getType()).equals(ActionType.AFTER)) {
             ExecutionPlanPartHelper.getInstance().delete(part, transaction);
           }
         }
       }
     } else if (!ActionType.AFTER.equals(viewBean.getType())) {
       ExecutionPlanPart parentPart = gwt2Hib(viewBean.getParent(), transaction);
       List<ExecutionPlanPart> parts =
           ExecutionPlanPartHelper.getInstance().findChildrenByPart(parentPart, transaction);
       if (parts != null && parts.size() > 0) {
         for (ExecutionPlanPart part : parts) {
           if (ActionType.get(part.getType()).equals(ActionType.AFTER)) {
             ExecutionPlanPartHelper.getInstance().delete(part, transaction);
           }
         }
       }
     }
   }
 }
 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;
  }
 private void saveOrUpdateParallelParts(
     ExecutionPlanPartBean parentPlanPartBean,
     ExecutionPlanPart parentPlanPart,
     User user,
     ITransactionHolder transaction)
     throws BusinessException, ClientBusinessException {
   List<ExecutionPlanPartBean> parallelNodes = parentPlanPartBean.getParallelParts();
   if (parallelNodes == null) {
     parallelNodes = new ArrayList<ExecutionPlanPartBean>();
   }
   int parallelTasksListSize = parallelNodes.size();
   removeObsoleteNodes(parentPlanPart, transaction, parallelNodes, parallelTasksListSize);
   if (parentPlanPart.isUseParallel()) {
     for (int i = 0; i < parallelTasksListSize; i++) {
       ExecutionPlanPartBean parallelPartBean = parallelNodes.get(i);
       parallelPartBean.setParent(parentPlanPartBean);
       ExecutionTaskBean taskBean = parallelPartBean.getTaskBean();
       ActionType type = parallelPartBean.getType();
       if (ActionType.PARALLEL.equals(type) && taskBean != null) {
         saveExecutionPlanPartBean(parallelPartBean, user, transaction);
       } else {
         throw new RuntimeException(
             "Can't save a parallel node: type=" + type + ", taskId=" + taskBean.getId());
       }
     }
   }
 }
 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()]);
 }
  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#gwt2Hib(java.lang.Object,
   * org.talend.administrator.common.persistence.hibernate.ITransactionHolder)
   */
  @Override
  public ExecutionPlanPart gwt2Hib(ExecutionPlanPartBean gwtBean, ITransactionHolder transaction) {
    Action action = (gwtBean.getId() == null ? Constants.Action.ADD : Constants.Action.UPDATE);

    ExecutionPlanPart hibBean = null;
    switch (action) {
      case ADD:
        hibBean = ConductorFactory.eINSTANCE.createExecutionPlanPart();
        break;
      case UPDATE:
        hibBean = ExecutionPlanPartHelper.getInstance().findById(gwtBean.getId(), transaction);
        if (hibBean != null) {
          hibBean.setId(gwtBean.getId());
        } else {
          log.warn(
              "Can't find the plan part with the id="
                  + gwtBean.getId()
                  + ", the plan part might have already been deleted");
          return null;
        }
        break;
    }
    if (gwtBean.getPlanBean() != null) {
      ExecutionPlan executionPlan =
          ExecutionPlanHelper.getInstance().findById(gwtBean.getPlanBean().getId(), transaction);
      hibBean.setExecutionPlan(executionPlan);
    }
    if (gwtBean.getTaskBean() != null) {
      ExecutionTask executionTask =
          ExecutionTaskHelper.getInstance().findById(gwtBean.getTaskBean().getId(), transaction);
      hibBean.setTask(executionTask);
    }
    ExecutionPlanPartBean parent = gwtBean.getParent();
    if (parent != null) {
      hibBean.setParent(gwt2Hib(parent, transaction));
    }
    if (gwtBean.getType() != null) {
      hibBean.setType(gwtBean.getType().toString());
      PlanPartStatus partStatus = PlanPartStatus.get(gwtBean.getType().toString());
      hibBean.setStatus(partStatus.name());
    }
    if (gwtBean.getStatus() != null) {
      hibBean.setStatus(gwtBean.getStatus());
    }
    hibBean.setStartDate(gwtBean.getStartDate());
    hibBean.setEndDate(gwtBean.getEndDate());
    hibBean.setUseParallel(gwtBean.isUseParallel());
    hibBean.setMaxThreads(gwtBean.getMaxThreads());
    return hibBean;
  }
  /*
   * (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;
  }