private Entity generateFormEntity(final ViewDefinitionState view) {
    DataDefinition dd =
        dataDefinitionService.get(BasicConstants.PLUGIN_IDENTIFIER, BasicConstants.MODEL_PRODUCT);

    FormComponent form = getForm(view);
    Entity formEntity = form.getEntity();
    GridComponent parentsGrid = (GridComponent) view.getComponentByReference("parents");
    if (parentsGrid.getSelectedEntities().isEmpty()) {
      return null;
    }
    Long productId = parentsGrid.getSelectedEntities().get(0).getId();

    if (productId == null) {
      throw new FormValidationException("basic.productFamiliesTree.noProductSelected");
    }

    Entity product = dd.get(productId);

    List<Entity> tree = productsFamiliesTreeService.getHierarchyProductsTree(product);
    EntityTree entityTree = EntityTreeUtilsService.getDetachedEntityTree(tree);
    formEntity.setId(productId);
    formEntity.setField(PRODUCT_FAMILY_CHILDREN_TREE, entityTree);

    return formEntity;
  }
  public void onRemoveButtonClick(
      final ViewDefinitionState viewDefinitionState,
      final ComponentState triggerState,
      final String[] args) {
    String url =
        pluginManagmentPerformer.performRemove(getPluginIdentifiersFromView(viewDefinitionState));

    if (url.contains("type=success")) {
      GridComponent grid = (GridComponent) viewDefinitionState.getComponentByReference("grid");
      grid.setSelectedEntitiesIds(new HashSet<Long>());
    }
    viewDefinitionState.openModal(url);
  }
  private void enableAddButton(ViewDefinitionState view) {
    FormComponent form = (FormComponent) view.getComponentByReference(L_FORM);
    Entity multiAssignment = form.getPersistedEntityWithIncludedFormValues();

    GridComponent workersComponent = (GridComponent) view.getComponentByReference("workers");
    WindowComponent window = (WindowComponent) view.getComponentByReference("window");
    RibbonGroup add = (RibbonGroup) window.getRibbon().getGroupByName("add");
    RibbonActionItem addMany = (RibbonActionItem) add.getItemByName("addManyWorkers");
    if (workersComponent.getEntities().isEmpty() || !multiAssignment.isValid()) {
      addMany.setEnabled(false);
      addMany.requestUpdate(true);
    } else {
      addMany.setEnabled(true);
      addMany.requestUpdate(true);
    }
  }
  public void showProductionTrackingsForProduct(
      final ViewDefinitionState view, final ComponentState state, final String[] args) {
    GridComponent productionTrackingsForProductGroupedGrid =
        (GridComponent) view.getComponentByReference(L_GRID);

    if (productionTrackingsForProductGroupedGrid.getSelectedEntities().isEmpty()) {
      return;
    }

    Entity productionTrackingsForProductGrouped =
        productionTrackingsForProductGroupedGrid.getSelectedEntities().get(0);

    StringBuilder orderNumberBuilder = new StringBuilder();

    orderNumberBuilder.append("[");
    orderNumberBuilder.append(
        productionTrackingsForProductGrouped.getStringField(
            ProductionTrackingForProductGroupedDtoFields.ORDER_NUMBER));
    orderNumberBuilder.append("]");

    StringBuilder productNumberBuilder = new StringBuilder();

    String orderNumber = orderNumberBuilder.toString();

    productNumberBuilder.append("[");
    productNumberBuilder.append(
        productionTrackingsForProductGrouped.getStringField(
            ProductionTrackingForProductGroupedDtoFields.PRODUCT_NUMBER));
    productNumberBuilder.append("]");

    String productNumber = productNumberBuilder.toString();

    Map<String, String> filters = Maps.newHashMap();
    filters.put("orderNumber", orderNumber);
    filters.put("productNumber", productNumber);

    Map<String, Object> gridOptions = Maps.newHashMap();
    gridOptions.put(L_FILTERS, filters);

    Map<String, Object> parameters = Maps.newHashMap();
    parameters.put(L_GRID_OPTIONS, gridOptions);

    parameters.put(L_WINDOW_ACTIVE_MENU, "ordersTracking.productionTrackingForProduct");

    String url = "../page/productionCounting/productionTrackingsForProductList.html";
    view.redirectTo(url, false, true, parameters);
  }
  private List<String> getPluginIdentifiersFromView(final ViewDefinitionState viewDefinitionState) {

    List<String> pluginIdentifiers = new LinkedList<String>();
    GridComponent grid = (GridComponent) viewDefinitionState.getComponentByReference("grid");

    Preconditions.checkState(grid.getSelectedEntitiesIds().size() > 0, "No record selected");

    DataDefinition pluginDataDefinition =
        dataDefinitionService.get(
            QcadooPluginConstants.PLUGIN_IDENTIFIER, QcadooPluginConstants.MODEL_PLUGIN);
    for (Long entityId : grid.getSelectedEntitiesIds()) {
      Entity pluginEntity = pluginDataDefinition.get(entityId);

      pluginIdentifiers.add(pluginEntity.getStringField("identifier"));
    }

    return pluginIdentifiers;
  }
  @Test
  public final void shouldGetSelectedEntities() {
    // given
    List<Entity> entities =
        ImmutableList.of(mockEntity(), mockEntity(), mockEntity(), mockEntity());
    given(workPlansGrid.getSelectedEntities()).willReturn(entities);

    // when
    List<Entity> result = workPlansListView.getSelectedWorkPlans();

    // then
    assertEquals(entities, result);
  }
  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();
  }
 public final void addDiscriminatorRestrictionToProductsFamilies(final ViewDefinitionState view) {
   GridComponent grid = (GridComponent) view.getComponentByReference("parents");
   grid.setCustomRestriction(customRestriction);
 }