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;
}
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);
}
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);
}
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");
}
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());
}
Aggregations