private void clearAndDisabledFields(
     final ViewDefinitionState view, final Set<String> fieldNames) {
   for (String fieldName : fieldNames) {
     FieldComponent currencyField = (FieldComponent) view.getComponentByReference(fieldName);
     currencyField.setFieldValue(L_EMPTY);
     currencyField.setEnabled(false);
   }
 }
  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 fillTitleLabel(final ViewDefinitionState viewDefinitionState) {
    FieldComponent title = (FieldComponent) viewDefinitionState.getComponentByReference("title");
    Entity order =
        dataDefinitionService
            .get(OrdersConstants.PLUGIN_IDENTIFIER, OrdersConstants.MODEL_ORDER)
            .get(orderId);
    String number = order.getField("number").toString();
    String name = order.getField("name").toString();

    title.setFieldValue(name + " - " + number);
    title.requestComponentUpdateState();
  }
  public void setOccupationTypeToDefault(final ViewDefinitionState view) {
    FormComponent staffAssignmentToShiftForm = (FormComponent) view.getComponentByReference("form");
    FieldComponent occupationType = (FieldComponent) view.getComponentByReference(OCCUPATION_TYPE);

    if ((staffAssignmentToShiftForm.getEntityId() == null)
        && (occupationType.getFieldValue() == null)) {
      Entity dictionaryItem = findDictionaryItemByTechnicalCode(WORK_ON_LINE.getStringValue());

      if (dictionaryItem != null) {
        String occupationTypeName = dictionaryItem.getStringField(NAME);

        occupationType.setFieldValue(occupationTypeName);
        occupationType.requestComponentUpdateState();
      }
    }
  }
  public void fillUnitField(
      final ViewDefinitionState viewDefinitionState,
      final String fieldName,
      final boolean inProduct) {
    checkArgument(viewDefinitionState != null, L_VIEW_DEFINITION_STATE_IS_NULL);

    FormComponent form = (FormComponent) viewDefinitionState.getComponentByReference(L_FORM);
    FieldComponent unitField =
        (FieldComponent) viewDefinitionState.getComponentByReference(fieldName);
    unitField.setFieldValue(L_EMPTY);
    unitField.setEnabled(false);
    if (form == null || form.getEntityId() == null) {
      return;
    }

    Long productId = null;

    if (inProduct) {
      productId = (Long) form.getFieldValue();
    } else {
      FieldComponent productField =
          (FieldComponent) viewDefinitionState.getComponentByReference("product");

      if (productField == null) {
        return;
      }

      productId = (Long) productField.getFieldValue();
    }

    Entity product =
        dataDefinitionService
            .get(BasicConstants.PLUGIN_IDENTIFIER, BasicConstants.MODEL_PRODUCT)
            .get(productId);

    if (product == null) {
      return;
    }

    String unit = product.getStringField(UNIT);

    fillField(unitField, unit);
  }
  public void enabledFieldForExternalID(final ViewDefinitionState view) {
    FieldComponent nominalCost = (FieldComponent) view.getComponentByReference(L_NOMINAL_COST);
    FormComponent form = (FormComponent) view.getComponentByReference(L_FORM);
    if (form.getEntityId() == null) {
      return;
    }
    Entity entity =
        dataDefinitionService
            .get(BasicConstants.PLUGIN_IDENTIFIER, BasicConstants.MODEL_PRODUCT)
            .get(form.getEntityId());

    if (entity == null) {
      return;
    }
    String externalNumber = entity.getStringField("externalNumber");

    if (externalNumber != null) {
      nominalCost.setEnabled(true);
    }
  }
  public void setFieldsEnabledWhenTypeIsSpecific(final ViewDefinitionState view) {
    FieldComponent occupationType = (FieldComponent) view.getComponentByReference(OCCUPATION_TYPE);

    if (occupationType.getFieldValue() != null) {
      Entity dictionaryItem = findDictionaryItemByName(occupationType.getFieldValue().toString());
      if (dictionaryItem == null) {
        setFieldsEnabled(view, false, false);
      } else {
        String occupationTypeTechnicalCode = dictionaryItem.getStringField(TECHNICAL_CODE);

        if (WORK_ON_LINE.getStringValue().equals(occupationTypeTechnicalCode)) {
          setFieldsEnabled(view, true, false);
        } else if (OTHER_CASE.getStringValue().equals(occupationTypeTechnicalCode)) {
          setFieldsEnabled(view, false, true);
        } else {
          setFieldsEnabled(view, false, false);
        }
      }
    }
  }
  void applyTimeNormsFromGivenSource(
      final ViewDefinitionState view, final Entity source, final Iterable<String> fields) {
    checkArgument(source != null, "source entity is null");
    FieldComponent component = null;

    for (String fieldName : fields) {
      component = (FieldComponent) view.getComponentByReference(fieldName);
      component.setFieldValue(source.getField(fieldName));
    }

    if (source.getField(NEXT_OPERATION_AFTER_PRODUCED_TYPE) == null) {
      view.getComponentByReference(NEXT_OPERATION_AFTER_PRODUCED_TYPE).setFieldValue("01all");
    }

    if (source.getField(PRODUCTION_IN_ONE_CYCLE) == null) {
      view.getComponentByReference(PRODUCTION_IN_ONE_CYCLE).setFieldValue("1");
    }

    if (source.getField(NEXT_OPERATION_AFTER_PRODUCED_QUANTITY) == null) {
      view.getComponentByReference(NEXT_OPERATION_AFTER_PRODUCED_QUANTITY).setFieldValue("0");
    }
  }
  private void setFieldsEnabled(
      final ViewDefinitionState view,
      final boolean visibleOrRequiredProductionLine,
      final boolean visibleOrRequiredOccupationTypeName) {
    FieldComponent productionLine = (FieldComponent) view.getComponentByReference(PRODUCTION_LINE);
    FieldComponent occupationTypeName =
        (FieldComponent) view.getComponentByReference(OCCUPATION_TYPE_NAME);

    productionLine.setVisible(visibleOrRequiredProductionLine);
    productionLine.setRequired(visibleOrRequiredProductionLine);
    productionLine.requestComponentUpdateState();
    occupationTypeName.setVisible(visibleOrRequiredOccupationTypeName);
    occupationTypeName.setRequired(visibleOrRequiredOccupationTypeName);
    occupationTypeName.requestComponentUpdateState();
  }
  private void fillWorkTimeFields(
      final ViewDefinitionState view, final OperationWorkTime workTime) {
    FieldComponent laborWorkTimeField =
        (FieldComponent) view.getComponentByReference(OrderFieldsPS.LABOR_WORK_TIME);
    FieldComponent machineWorkTimeField =
        (FieldComponent) view.getComponentByReference(OrderFieldsPS.MACHINE_WORK_TIME);

    laborWorkTimeField.setFieldValue(workTime.getLaborWorkTime());
    machineWorkTimeField.setFieldValue(workTime.getMachineWorkTime());

    laborWorkTimeField.requestComponentUpdateState();
    machineWorkTimeField.requestComponentUpdateState();
  }
 public void onProductionInOneCycleCheckboxChange(
     final ViewDefinitionState viewDefinitionState,
     final ComponentState state,
     final String[] args) {
   FieldComponent areProductQuantitiesDivisible =
       (FieldComponent)
           viewDefinitionState.getComponentByReference(ARE_PRODUCT_QUANTITIES_DIVISIBLE);
   FieldComponent isTjDivisible =
       (FieldComponent) viewDefinitionState.getComponentByReference(IS_TJ_DIVISIBLE);
   if ("1".equals(areProductQuantitiesDivisible.getFieldValue())) {
     isTjDivisible.setEnabled(true);
   } else {
     isTjDivisible.setEnabled(false);
     isTjDivisible.setFieldValue("0");
   }
 }
  public void changeNextOperationAfterProducedTypeNorm(
      final ViewDefinitionState viewDefinitionState,
      final ComponentState state,
      final String[] args) {
    FieldComponent nextOperationAfterProducedType =
        (FieldComponent)
            viewDefinitionState.getComponentByReference(NEXT_OPERATION_AFTER_PRODUCED_TYPE);
    FieldComponent nextOperationAfterProducedQuantity =
        (FieldComponent)
            viewDefinitionState.getComponentByReference(NEXT_OPERATION_AFTER_PRODUCED_QUANTITY);
    FieldComponent nextOperationAfterProducedQuantityUNIT =
        (FieldComponent)
            viewDefinitionState.getComponentByReference(
                TechnologyOperCompTNFOFields.NEXT_OPERATION_AFTER_PRODUCED_QUANTITY_UNIT);

    Boolean visibilityValue = "02specified".equals(nextOperationAfterProducedType.getFieldValue());
    nextOperationAfterProducedQuantity.setVisible(visibilityValue);
    nextOperationAfterProducedQuantity.setEnabled(visibilityValue);
    nextOperationAfterProducedQuantityUNIT.setVisible(visibilityValue);
  }
  private boolean isGenerated(final ViewDefinitionState view) {
    FieldComponent generatedEndDateField =
        (FieldComponent) view.getComponentByReference(L_GENERATED_END_DATE);

    return !StringUtils.isEmpty((String) generatedEndDateField.getFieldValue());
  }
  @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);
    }
  }
 public void fillField(final FieldComponent fieldComponent, final String fieldValue) {
   checkArgument(fieldComponent != null, "fieldComponent is null");
   fieldComponent.setFieldValue(fieldValue);
   fieldComponent.requestComponentUpdateState();
 }