Esempio n. 1
0
    public void autompleteSearch(final String[] args) {
      if ((belongsToFieldDefinition == null || belongsToEntityId != null)) {
        SearchCriteriaBuilder searchCriteriaBuilder = getDataDefinition().find();

        if (StringUtils.hasText(currentCode)) {
          searchCriteriaBuilder.add(
              SearchRestrictions.ilike(fieldCode, currentCode, SearchMatchMode.ANYWHERE));
        }

        if (belongsToFieldDefinition != null
            && belongsToEntityId != null
            && belongsToFieldDefinition.getType() instanceof BelongsToType) {
          BelongsToType type = (BelongsToType) belongsToFieldDefinition.getType();
          searchCriteriaBuilder.add(
              SearchRestrictions.belongsTo(
                  belongsToFieldDefinition.getName(),
                  type.getDataDefinition().get(belongsToEntityId)));
        }

        if (getDataDefinition().isActivable()) {
          if (oldSelectedEntityId == null) {
            searchCriteriaBuilder.add(SearchRestrictions.eq("active", true));
          } else {
            searchCriteriaBuilder.add(
                SearchRestrictions.or(
                    SearchRestrictions.eq("active", true),
                    SearchRestrictions.idEq(oldSelectedEntityId)));
          }
        }

        searchCriteriaBuilder.addOrder(SearchOrders.asc(fieldCode));

        if (criteriaModifier != null) {
          criteriaModifier.modifyCriteria(searchCriteriaBuilder, criteriaModifierParameter);
        }

        SearchResult results = searchCriteriaBuilder.list();

        autocompleteEntitiesNumber = results.getTotalNumberOfEntities();

        if (results.getTotalNumberOfEntities() > 25) {
          autocompleteMatches = new LinkedList<Entity>();
        } else {
          autocompleteMatches = results.getEntities();
        }
      } else {
        autocompleteMatches = new LinkedList<Entity>();
      }

      autocompleteCode = currentCode;
      requestRender();
    }
  private List<Entity> getResourcesForLocationAndProductFEFO(
      final Entity warehouse,
      final Entity product,
      final Entity additionalCode,
      final Entity position) {
    List<Entity> resources = Lists.newArrayList();

    if (additionalCode != null) {
      SearchCriteriaBuilder scb =
          getSearchCriteriaForResourceForProductAndWarehouse(product, warehouse);

      if (!StringUtils.isEmpty(product.getStringField(ProductFields.ADDITIONAL_UNIT))) {
        scb.add(
            SearchRestrictions.eq(
                PositionFields.CONVERSION, position.getDecimalField(PositionFields.CONVERSION)));
      } else {
        scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, BigDecimal.ONE));
      }

      resources =
          scb.add(SearchRestrictions.belongsTo(ResourceFields.ADDITIONAL_CODE, additionalCode))
              .addOrder(SearchOrders.asc(ResourceFields.EXPIRATION_DATE))
              .list()
              .getEntities();

      scb = getSearchCriteriaForResourceForProductAndWarehouse(product, warehouse);

      if (!StringUtils.isEmpty(product.getStringField(ProductFields.ADDITIONAL_UNIT))) {
        scb.add(
            SearchRestrictions.eq(
                PositionFields.CONVERSION, position.getDecimalField(PositionFields.CONVERSION)));
      } else {
        scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, BigDecimal.ONE));
      }

      resources.addAll(
          scb.add(
                  SearchRestrictions.or(
                      SearchRestrictions.isNull(ResourceFields.ADDITIONAL_CODE),
                      SearchRestrictions.ne("additionalCode.id", additionalCode.getId())))
              .addOrder(SearchOrders.asc(ResourceFields.EXPIRATION_DATE))
              .list()
              .getEntities());
    }

    if (resources.isEmpty()) {
      SearchCriteriaBuilder scb =
          getSearchCriteriaForResourceForProductAndWarehouse(product, warehouse);

      if (!StringUtils.isEmpty(product.getStringField(ProductFields.ADDITIONAL_UNIT))) {
        scb.add(
            SearchRestrictions.eq(
                PositionFields.CONVERSION, position.getDecimalField(PositionFields.CONVERSION)));
      } else {
        scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, BigDecimal.ONE));
      }

      resources =
          scb.addOrder(SearchOrders.asc(ResourceFields.EXPIRATION_DATE)).list().getEntities();
    }

    return resources;
  }