public void setGenerateButtonState(
      final ViewDefinitionState state,
      final Locale locale,
      final String plugin,
      final String entityName) {
    WindowComponent window = (WindowComponent) state.getComponentByReference("window");
    FormComponent form = (FormComponent) state.getComponentByReference("form");
    RibbonActionItem generateButton =
        window.getRibbon().getGroupByName("generate").getItemByName("generate");
    RibbonActionItem deleteButton =
        window.getRibbon().getGroupByName("actions").getItemByName("delete");

    if (form.getEntityId() == null) {
      generateButton.setMessage("recordNotCreated");
      generateButton.setEnabled(false);
      deleteButton.setMessage(null);
      deleteButton.setEnabled(false);
    } else {

      Entity simpleMaterialBalanceEntity =
          dataDefinitionService.get(plugin, entityName).get(form.getEntityId());

      if (simpleMaterialBalanceEntity.getField(L_GENERATED) == null) {
        simpleMaterialBalanceEntity.setField(L_GENERATED, "0");
      }

      if ("1".equals(simpleMaterialBalanceEntity.getField(L_GENERATED))) {
        generateButton.setMessage("orders.ribbon.message.recordAlreadyGenerated");
        generateButton.setEnabled(false);
        deleteButton.setMessage("orders.ribbon.message.recordAlreadyGenerated");
        deleteButton.setEnabled(false);
      } else {
        generateButton.setMessage(null);
        generateButton.setEnabled(true);
        deleteButton.setMessage(null);
        deleteButton.setEnabled(true);
      }
    }
    generateButton.requestUpdate(true);
    deleteButton.requestUpdate(true);
    window.requestRibbonRender();
  }
  public void setGridGenerateButtonState(
      final ViewDefinitionState state,
      final Locale locale,
      final String plugin,
      final String entityName) {
    WindowComponent window = (WindowComponent) state.getComponentByReference("window");
    GridComponent grid = (GridComponent) state.getComponentByReference("grid");
    RibbonActionItem deleteButton =
        window.getRibbon().getGroupByName("actions").getItemByName("delete");

    if (grid.getSelectedEntitiesIds() == null || grid.getSelectedEntitiesIds().size() == 0) {
      deleteButton.setMessage(null);
      deleteButton.setEnabled(false);
    } else {
      boolean canDelete = true;
      for (Long entityId : grid.getSelectedEntitiesIds()) {
        Entity simpleMaterialBalanceEntity =
            dataDefinitionService.get(plugin, entityName).get(entityId);

        if ((Boolean) simpleMaterialBalanceEntity.getField(L_GENERATED)) {
          canDelete = false;
          break;
        }
      }
      if (canDelete) {
        deleteButton.setMessage(null);
        deleteButton.setEnabled(true);
      } else {
        deleteButton.setMessage("orders.ribbon.message.selectedRecordAlreadyGenerated");
        deleteButton.setEnabled(false);
      }
    }

    deleteButton.requestUpdate(true);
    window.requestRibbonRender();
  }