public void showCopyOfTechnology(
      final ViewDefinitionState view, final ComponentState state, final String[] args) {
    Long orderId = (Long) state.getFieldValue();

    if (orderId != null) {
      Entity order =
          dataDefinitionService
              .get(OrdersConstants.PLUGIN_IDENTIFIER, OrdersConstants.MODEL_ORDER)
              .get(orderId);

      if (OrderType.WITH_PATTERN_TECHNOLOGY
              .getStringValue()
              .equals(order.getField(OrderFields.ORDER_TYPE))
          && (order.getBelongsToField(OrderFields.TECHNOLOGY_PROTOTYPE) == null)) {
        state.addMessage("order.technology.patternTechnology.not.set", MessageType.INFO);

        return;
      }

      Long technologyId = order.getBelongsToField(OrderFields.TECHNOLOGY).getId();
      Map<String, Object> parameters = Maps.newHashMap();
      parameters.put("form.id", technologyId);

      String url = "../page/orders/copyOfTechnologyDetails.html";
      view.redirectTo(url, false, true, parameters);
    }
  }
 private void generateSimpleMaterialBalanceDocuments(
     final ComponentState state, final Entity simpleMaterialBalance)
     throws IOException, DocumentException {
   Entity simpleMaterialBalanceWithFileName =
       fileService.updateReportFileName(
           simpleMaterialBalance,
           "date",
           "simpleMaterialBalance.simpleMaterialBalance.report.fileName");
   simpleMaterialBalancePdfService.generateDocument(
       simpleMaterialBalanceWithFileName, state.getLocale());
   simpleMaterialBalanceXlsService.generateDocument(
       simpleMaterialBalanceWithFileName, state.getLocale());
 }
  public void copyRealizationTime(
      final ViewDefinitionState view, final ComponentState state, final String[] args) {
    FieldComponent generatedEndDateField =
        (FieldComponent) view.getComponentByReference(OrderFieldsPS.GENERATED_END_DATE);
    FieldComponent stopTimeField = (FieldComponent) view.getComponentByReference(L_STOP_TIME);

    stopTimeField.setFieldValue(generatedEndDateField.getFieldValue());

    state.performEvent(view, "save", new String[0]);
  }
  public void showStoppage(
      final ViewDefinitionState viewDefinitionState,
      final ComponentState triggerState,
      final String[] args) {
    Long orderId = (Long) triggerState.getFieldValue();

    if (orderId != null) {
      String url = "../page/stoppage/stoppage.html?context={\"order.id\":\"" + orderId + "\"}";
      viewDefinitionState.openModal(url);
    }
  }
  public void copyTimeNormsFromOperation(
      final ViewDefinitionState view,
      final ComponentState operationLookupState,
      final String[] args) {

    ComponentState operationLookup = view.getComponentByReference(OPERATION);
    if (operationLookup.getFieldValue() == null) {
      if (!OPERATION.equals(operationLookupState.getName())) {
        view.getComponentByReference("form")
            .addMessage("productionTimeNorms.messages.info.missingOperationReference", INFO);
      }
      return;
    }

    Entity operation =
        dataDefinitionService
            .get(TechnologiesConstants.PLUGIN_IDENTIFIER, TechnologiesConstants.MODEL_OPERATION)
            .get((Long) operationLookup.getFieldValue());

    applyTimeNormsFromGivenSource(view, operation, FIELDS_OPERATION);
  }
  @Test
  public void shouldntShowChangeoverIfOrderIdIsNull() {
    // given
    given(componentState.getFieldValue()).willReturn(null);

    String url = "../page/lineChangeoverNormsForOrders/lineChangeoverNormsForOrderDetails.html";

    // when
    orderDetailsListenersLCNFO.showChangeover(view, componentState, null);

    // then
    verify(view, never()).redirectTo(url, false, true, parameters);
  }
  public void showOperationsGantt(
      final ViewDefinitionState viewDefinitionState,
      final ComponentState triggerState,
      final String[] args) {
    orderId = (Long) triggerState.getFieldValue();

    if (orderId != null) {
      String url =
          "../page/ganttForOperations/ganttForOperations.html?context={\"gantt.orderId\":\""
              + orderId
              + "\"}";

      viewDefinitionState.redirectTo(url, false, true);
    }
  }
  @Transactional
  public void generateSimpleMaterialBalance(
      final ViewDefinitionState viewDefinitionState,
      final ComponentState state,
      final String[] args) {
    if (state instanceof FormComponent) {
      ComponentState generated = viewDefinitionState.getComponentByReference(L_GENERATED);
      ComponentState date = viewDefinitionState.getComponentByReference(L_DATE);
      ComponentState worker = viewDefinitionState.getComponentByReference(L_WORKER);

      Entity simpleMaterialBalance =
          dataDefinitionService
              .get(
                  SimpleMaterialBalanceConstants.PLUGIN_IDENTIFIER,
                  SimpleMaterialBalanceConstants.MODEL_SIMPLE_MATERIAL_BALANCE)
              .get((Long) state.getFieldValue());

      if (simpleMaterialBalance == null) {
        state.addMessage("qcadooView.message.entityNotFound", MessageType.FAILURE);
        return;
      } else if (StringUtils.hasText(simpleMaterialBalance.getStringField(L_FILE_NAME))) {
        state.addMessage(
            "simpleMaterialBalance.simpleMaterialBalanceDetails.window.simpleMaterialBalance.documentsWasGenerated",
            MessageType.FAILURE);
        return;
      } else if (simpleMaterialBalance
          .getHasManyField(L_SIMPLE_MATERIAL_BALANCE_ORDERS_COMPONENTS)
          .isEmpty()) {
        state.addMessage(
            "simpleMaterialBalance.simpleMaterialBalance.window.simpleMaterialBalance.missingAssosiatedOrders",
            MessageType.FAILURE);
        return;
      } else if (simpleMaterialBalance
          .getHasManyField(L_SIMPLE_MATERIAL_BALANCE_LOCATIONS_COMPONENTS)
          .isEmpty()) {
        state.addMessage(
            "simpleMaterialBalance.simpleMaterialBalance.window.simpleMaterialBalance.missingAssosiatedLocations",
            MessageType.FAILURE);
        return;
      }

      if ("0".equals(generated.getFieldValue())) {
        worker.setFieldValue(securityService.getCurrentUserName());
        generated.setFieldValue("1");
        date.setFieldValue(
            new SimpleDateFormat(DateUtils.L_DATE_TIME_FORMAT, LocaleContextHolder.getLocale())
                .format(new Date()));
      }

      state.performEvent(viewDefinitionState, "save", new String[0]);

      if (state.getFieldValue() == null || !((FormComponent) state).isValid()) {
        worker.setFieldValue(null);
        generated.setFieldValue("0");
        date.setFieldValue(null);
        return;
      }

      simpleMaterialBalance =
          dataDefinitionService
              .get(
                  SimpleMaterialBalanceConstants.PLUGIN_IDENTIFIER,
                  SimpleMaterialBalanceConstants.MODEL_SIMPLE_MATERIAL_BALANCE)
              .get((Long) state.getFieldValue());

      try {
        generateSimpleMaterialBalanceDocuments(state, simpleMaterialBalance);
        state.performEvent(viewDefinitionState, "reset", new String[0]);
      } catch (IOException e) {
        throw new IllegalStateException(e.getMessage(), e);
      } catch (DocumentException e) {
        throw new IllegalStateException(e.getMessage(), e);
      }
    }
  }
  @Transactional
  public void generateRealizationTime(
      final ViewDefinitionState viewDefinitionState,
      final ComponentState state,
      final String[] args) {
    FormComponent orderForm = (FormComponent) viewDefinitionState.getComponentByReference(L_FORM);
    FieldComponent startTimeField =
        (FieldComponent) viewDefinitionState.getComponentByReference(L_START_TIME);

    if (!StringUtils.hasText((String) startTimeField.getFieldValue())) {
      startTimeField.addMessage(L_PRODUCTION_SCHEDULING_ERROR_FIELD_REQUIRED, MessageType.FAILURE);

      return;
    }

    FieldComponent plannedQuantityField =
        (FieldComponent) viewDefinitionState.getComponentByReference(OrderFields.PLANNED_QUANTITY);
    FieldComponent productionLineLookup =
        (FieldComponent) viewDefinitionState.getComponentByReference(OrderFields.PRODUCTION_LINE);
    FieldComponent generatedEndDateField =
        (FieldComponent)
            viewDefinitionState.getComponentByReference(OrderFieldsPS.GENERATED_END_DATE);
    FieldComponent includeTpzField =
        (FieldComponent) viewDefinitionState.getComponentByReference(OrderFieldsPS.INCLUDE_TPZ);
    FieldComponent includeAdditionalTimeField =
        (FieldComponent)
            viewDefinitionState.getComponentByReference(OrderFieldsPS.INCLUDE_ADDITIONAL_TIME);

    boolean isGenerated = false;

    Entity productionLine =
        dataDefinitionService
            .get(
                ProductionLinesConstants.PLUGIN_IDENTIFIER,
                ProductionLinesConstants.MODEL_PRODUCTION_LINE)
            .get((Long) productionLineLookup.getFieldValue());

    Entity order =
        dataDefinitionService
            .get(OrdersConstants.PLUGIN_IDENTIFIER, OrdersConstants.MODEL_ORDER)
            .get(orderForm.getEntity().getId());

    // copy of technology from order
    Entity technology = order.getBelongsToField(OrderFields.TECHNOLOGY);
    Validate.notNull(technology, "technology is null");
    BigDecimal quantity =
        orderRealizationTimeService.getBigDecimalFromField(
            plannedQuantityField.getFieldValue(), viewDefinitionState.getLocale());

    // Included in work time
    Boolean includeTpz = "1".equals(includeTpzField.getFieldValue());
    Boolean includeAdditionalTime = "1".equals(includeAdditionalTimeField.getFieldValue());

    final Map<Long, BigDecimal> operationRuns = Maps.newHashMap();

    productQuantitiesService.getProductComponentQuantities(technology, quantity, operationRuns);

    OperationWorkTime workTime =
        operationWorkTimeService.estimateTotalWorkTimeForOrder(
            order, operationRuns, includeTpz, includeAdditionalTime, productionLine, true);

    fillWorkTimeFields(viewDefinitionState, workTime);

    order = getActualOrderWithChanges(order);

    int maxPathTime =
        orderRealizationTimeService.estimateMaxOperationTimeConsumptionForWorkstation(
            technology.getTreeField(TechnologyFields.OPERATION_COMPONENTS).getRoot(),
            quantity,
            includeTpz,
            includeAdditionalTime,
            productionLine);

    if (maxPathTime > OrderRealizationTimeService.MAX_REALIZATION_TIME) {
      state.addMessage("orders.validate.global.error.RealizationTimeIsToLong", MessageType.FAILURE);

      generatedEndDateField.setFieldValue(null);
    } else {
      order.setField(OrderFieldsPS.REALIZATION_TIME, maxPathTime);

      Date startTime = order.getDateField(OrderFields.DATE_FROM);

      if (startTime == null) {
        startTimeField.addMessage(
            "orders.validate.global.error.dateFromIsNull", MessageType.FAILURE);
      } else {
        Date stopTime = shiftsService.findDateToForOrder(startTime, maxPathTime);

        if (stopTime == null) {
          orderForm.addMessage("productionScheduling.timenorms.isZero", MessageType.FAILURE, false);

          generatedEndDateField.setFieldValue(null);
        } else {
          generatedEndDateField.setFieldValue(orderRealizationTimeService.setDateToField(stopTime));

          order.setField(
              OrderFieldsPS.GENERATED_END_DATE,
              orderRealizationTimeService.setDateToField(stopTime));

          scheduleOrder(order.getId());

          isGenerated = true;
        }

        orderForm.addMessage(
            "orders.dateFrom.info.dateFromSetToFirstPossible", MessageType.INFO, false);
      }

      order = order.getDataDefinition().save(order);
      orderForm.setEntity(order);
    }

    generatedEndDateField.requestComponentUpdateState();

    if (isGenerated) {
      orderForm.addMessage("productionScheduling.info.calculationGenerated", MessageType.SUCCESS);
    }
  }