Search in sources :

Example 36 with SearchCriterion

use of com.qcadoo.model.api.search.SearchCriterion in project mes by qcadoo.

the class ResourceManagementServiceImpl method getResourcesForLocationCommonCodeConversion.

private List<Entity> getResourcesForLocationCommonCodeConversion(final Entity warehouse, final Entity product, final Entity additionalCode, final Entity position, final boolean resourceIrrespectiveOfConversion, final SearchOrder... searchOrders) {
    class SearchCriteriaHelper {

        private List<Entity> getAll() {
            return getAllThatSatisfies(null);
        }

        private List<Entity> getAllThatSatisfies(SearchCriterion searchCriterion) {
            SearchCriteriaBuilder scb = getSearchCriteriaForResourceForProductAndWarehouse(product, warehouse);
            if (resourceIrrespectiveOfConversion) {
                if (StringUtils.isNotEmpty(product.getStringField(ProductFields.ADDITIONAL_UNIT))) {
                    scb.add(SearchRestrictions.ne(PositionFields.CONVERSION, position.getDecimalField(PositionFields.CONVERSION)));
                } else {
                    scb.add(SearchRestrictions.ne(ResourceFields.CONVERSION, BigDecimal.ONE));
                }
            } else {
                if (StringUtils.isNotEmpty(product.getStringField(ProductFields.ADDITIONAL_UNIT))) {
                    scb.add(SearchRestrictions.eq(PositionFields.CONVERSION, position.getDecimalField(PositionFields.CONVERSION)));
                } else {
                    scb.add(SearchRestrictions.eq(ResourceFields.CONVERSION, BigDecimal.ONE));
                }
            }
            if (Objects.nonNull(position.getBelongsToField(PositionFields.BATCH))) {
                scb.add(SearchRestrictions.belongsTo(ResourceFields.BATCH, position.getBelongsToField(PositionFields.BATCH)));
            }
            scb.add(SearchRestrictions.eq(ResourceFields.BLOCKED_FOR_QUALITY_CONTROL, false));
            Optional.ofNullable(searchCriterion).ifPresent(scb::add);
            for (SearchOrder searchOrder : searchOrders) {
                scb.addOrder(searchOrder);
            }
            return scb.list().getEntities();
        }
    }
    List<Entity> resources = Lists.newArrayList();
    if (additionalCode != null) {
        resources = new SearchCriteriaHelper().getAllThatSatisfies(SearchRestrictions.belongsTo(ResourceFields.ADDITIONAL_CODE, additionalCode));
        resources.addAll(new SearchCriteriaHelper().getAllThatSatisfies(SearchRestrictions.or(SearchRestrictions.isNull(ResourceFields.ADDITIONAL_CODE), SearchRestrictions.ne("additionalCode.id", additionalCode.getId()))));
    }
    if (resources.isEmpty()) {
        resources = new SearchCriteriaHelper().getAll();
    }
    return resources;
}
Also used : Entity(com.qcadoo.model.api.Entity) SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder) SearchOrder(com.qcadoo.model.api.search.SearchOrder) SearchCriterion(com.qcadoo.model.api.search.SearchCriterion)

Example 37 with SearchCriterion

use of com.qcadoo.model.api.search.SearchCriterion in project mes by qcadoo.

the class ProductCatalogNumbersHooksTest method shouldReturnTrueWhenEntityWithGivenProductAndCompanyDoesnotExistsInDB.

@Test
public void shouldReturnTrueWhenEntityWithGivenProductAndCompanyDoesnotExistsInDB() throws Exception {
    // given
    SearchCriterion criterion1 = SearchRestrictions.eq(CATALOG_NUMBER, entity.getStringField(CATALOG_NUMBER));
    SearchCriterion criterion2 = SearchRestrictions.belongsTo(PRODUCT, entity.getBelongsToField(PRODUCT));
    given(entity.getId()).willReturn(null);
    given(criteria.add(criterion1)).willReturn(criteria);
    given(criteria.add(criterion2)).willReturn(criteria);
    given(criteria.list()).willReturn(searchResult);
    given(searchResult.getEntities()).willReturn(productCatalogNumbers);
    given(productCatalogNumbers.isEmpty()).willReturn(true);
    // when
    boolean result = productCatalogNumbersHooks.checkIfExistsCatalogNumberWithProductAndCompany(dataDefinition, entity);
    // then
    Assert.assertTrue(result);
}
Also used : SearchCriterion(com.qcadoo.model.api.search.SearchCriterion) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 38 with SearchCriterion

use of com.qcadoo.model.api.search.SearchCriterion in project mes by qcadoo.

the class ProductCatalogNumbersHooksTest method shouldReturnTrueWhenEntityWithGivenNumberAndCompanyDoesnotExistsInDB.

@Test
public void shouldReturnTrueWhenEntityWithGivenNumberAndCompanyDoesnotExistsInDB() throws Exception {
    // given
    SearchCriterion criterion1 = SearchRestrictions.eq(CATALOG_NUMBER, entity.getStringField(CATALOG_NUMBER));
    SearchCriterion criterion2 = SearchRestrictions.belongsTo(ProductCatalogNumberFields.COMPANY, company);
    given(entity.getId()).willReturn(null);
    given(criteria.add(criterion1)).willReturn(criteria);
    given(criteria.add(criterion2)).willReturn(criteria);
    given(criteria.list()).willReturn(searchResult);
    given(searchResult.getEntities()).willReturn(productCatalogNumbers);
    given(productCatalogNumbers.isEmpty()).willReturn(true);
    // when
    boolean result = productCatalogNumbersHooks.checkIfExistsCatalogNumberWithNumberAndCompany(dataDefinition, entity);
    // then
    Assert.assertTrue(result);
}
Also used : SearchCriterion(com.qcadoo.model.api.search.SearchCriterion) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 39 with SearchCriterion

use of com.qcadoo.model.api.search.SearchCriterion in project mes by qcadoo.

the class ProductCatalogNumbersHooksTest method shouldReturnFalseWhenEntityWithGivenProductAndCompanyDoesExistsInDB.

@Test
public void shouldReturnFalseWhenEntityWithGivenProductAndCompanyDoesExistsInDB() throws Exception {
    // given
    SearchCriterion criterion1 = SearchRestrictions.eq(CATALOG_NUMBER, entity.getStringField(CATALOG_NUMBER));
    SearchCriterion criterion2 = SearchRestrictions.belongsTo(PRODUCT, entity.getBelongsToField(PRODUCT));
    given(entity.getId()).willReturn(null);
    given(criteria.add(criterion1)).willReturn(criteria);
    given(criteria.add(criterion2)).willReturn(criteria);
    given(criteria.list()).willReturn(searchResult);
    given(searchResult.getEntities()).willReturn(productCatalogNumbers);
    given(productCatalogNumbers.isEmpty()).willReturn(false);
    // when
    boolean result = productCatalogNumbersHooks.checkIfExistsCatalogNumberWithProductAndCompany(dataDefinition, entity);
    // then
    Assert.assertFalse(result);
    Mockito.verify(entity).addGlobalError("productCatalogNumbers.productCatalogNumber.validationError.alreadyExistsProductForCompany");
}
Also used : SearchCriterion(com.qcadoo.model.api.search.SearchCriterion) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 40 with SearchCriterion

use of com.qcadoo.model.api.search.SearchCriterion in project mes by qcadoo.

the class ProductValidatorsTest method shouldCheckEanUniquenessReturnFalsAndMarkFieldAsInvalidIfProductsWithGivenIdExists.

@Test
public final void shouldCheckEanUniquenessReturnFalsAndMarkFieldAsInvalidIfProductsWithGivenIdExists() {
    // given
    String oldVal = "123456";
    String newVal = "654321";
    ArgumentCaptor<SearchCriterion> criterionCaptor = ArgumentCaptor.forClass(SearchCriterion.class);
    Entity resultEntity = mock(Entity.class);
    stubSearchCriteriaWith(resultEntity);
    // when
    boolean isValid = productValidators.checkEanUniqueness(dataDefinition, fieldDefinition, entity, oldVal, newVal);
    // then
    assertFalse(isValid);
    verify(entity).addError(fieldDefinition, "qcadooView.validate.field.error.duplicated");
    verify(scb).add(criterionCaptor.capture());
    assertEquals(SearchRestrictions.eq(ProductFields.EAN, newVal), criterionCaptor.getValue());
}
Also used : Entity(com.qcadoo.model.api.Entity) SearchCriterion(com.qcadoo.model.api.search.SearchCriterion) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Aggregations

SearchCriterion (com.qcadoo.model.api.search.SearchCriterion)41 Entity (com.qcadoo.model.api.Entity)16 Test (org.junit.Test)12 SearchCriteriaBuilder (com.qcadoo.model.api.search.SearchCriteriaBuilder)7 FieldDefinition (com.qcadoo.model.api.FieldDefinition)6 DataDefinition (com.qcadoo.model.api.DataDefinition)4 SearchProjection (com.qcadoo.model.api.search.SearchProjection)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 SearchResult (com.qcadoo.model.api.search.SearchResult)3 BigDecimal (java.math.BigDecimal)2 Date (java.util.Date)2 Before (org.junit.Before)2 Matchers.anyString (org.mockito.Matchers.anyString)2 ImmutableList (com.google.common.collect.ImmutableList)1 BatchNumberUniqueness (com.qcadoo.mes.advancedGenealogy.constants.BatchNumberUniqueness)1 AssignmentToShiftCriteria (com.qcadoo.mes.assignmentToShift.dataProviders.AssignmentToShiftCriteria)1 SearchDisjunction (com.qcadoo.model.api.search.SearchDisjunction)1 SearchOrder (com.qcadoo.model.api.search.SearchOrder)1 ParseException (java.text.ParseException)1 List (java.util.List)1