Search in sources :

Example 1 with SearchCriteriaBuilder

use of com.qcadoo.model.api.search.SearchCriteriaBuilder in project qcadoo by qcadoo.

the class CustomTranslationModelHooks method checkIfCustomTranslationIsUnique.

public boolean checkIfCustomTranslationIsUnique(final DataDefinition customTranslationDD, final Entity customTranslation) {
    String pluginIdentifier = customTranslation.getStringField(PLUGIN_IDENTIFIER);
    String locale = customTranslation.getStringField(LOCALE);
    String key = customTranslation.getStringField(KEY);
    SearchCriteriaBuilder searchCriteriaBuilder = customTranslationDD.find().add(SearchRestrictions.eq(PLUGIN_IDENTIFIER, pluginIdentifier)).add(SearchRestrictions.eq(LOCALE, locale)).add(SearchRestrictions.eq(KEY, key));
    if (customTranslation.getId() != null) {
        searchCriteriaBuilder.add(SearchRestrictions.ne("id", customTranslation.getId()));
    }
    SearchResult searchResult = searchCriteriaBuilder.list();
    if (!searchResult.getEntities().isEmpty()) {
        customTranslation.addError(customTranslationDD.getField(KEY), "customTranslation.customTranslation.error.customTranslationIsntUnique");
        return false;
    }
    return true;
}
Also used : SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder) SearchResult(com.qcadoo.model.api.search.SearchResult)

Example 2 with SearchCriteriaBuilder

use of com.qcadoo.model.api.search.SearchCriteriaBuilder in project qcadoo by qcadoo.

the class DataDefinitionImpl method count.

@Override
public long count(final SearchCriterion criterion) {
    final String countAlias = "count";
    SearchCriteriaBuilder scb = find();
    if (criterion != null) {
        scb.add(criterion);
    }
    scb.setProjection(alias(rowCount(), countAlias));
    scb.addOrder(asc(countAlias));
    Entity countProjection = scb.setMaxResults(1).uniqueResult();
    return (Long) countProjection.getField(countAlias);
}
Also used : Entity(com.qcadoo.model.api.Entity) SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder)

Example 3 with SearchCriteriaBuilder

use of com.qcadoo.model.api.search.SearchCriteriaBuilder in project qcadoo by qcadoo.

the class DictionaryItemValidators method checkIfDictionaryItemIsUnique.

private boolean checkIfDictionaryItemIsUnique(final DataDefinition dictionaryItemDD, final Entity dictionaryItem, final String fieldName) {
    Entity dictionary = dictionaryItem.getBelongsToField(DictionaryItemFields.DICTIONARY);
    String fieldValue = dictionaryItem.getStringField(fieldName);
    SearchCriteriaBuilder searchCriteriaBuilder = dictionaryItemDD.find().add(SearchRestrictions.belongsTo(DictionaryItemFields.DICTIONARY, dictionary)).add(SearchRestrictions.eq(fieldName, fieldValue));
    if (Objects.nonNull(dictionaryItem.getId())) {
        searchCriteriaBuilder.add(SearchRestrictions.idNe(dictionaryItem.getId()));
    }
    SearchResult searchResult = searchCriteriaBuilder.list();
    return searchResult.getEntities().isEmpty();
}
Also used : Entity(com.qcadoo.model.api.Entity) SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder) SearchResult(com.qcadoo.model.api.search.SearchResult)

Example 4 with SearchCriteriaBuilder

use of com.qcadoo.model.api.search.SearchCriteriaBuilder in project qcadoo by qcadoo.

the class EntityListImplTest method shouldReturnCriteriaBuilder.

@Test
public void shouldReturnCriteriaBuilder() throws Exception {
    // given
    BelongsToType fieldType = mock(BelongsToType.class);
    InternalDataDefinition dataDefinition = mock(InternalDataDefinition.class, RETURNS_DEEP_STUBS);
    given(fieldType.getDataDefinition()).willReturn(dataDefinition);
    FieldDefinition fieldDefinition = mock(FieldDefinition.class);
    given(fieldDefinition.getType()).willReturn(fieldType);
    given(fieldDefinition.getName()).willReturn("field");
    given(dataDefinition.getField("hasMany")).willReturn(fieldDefinition);
    SearchCriteriaBuilder searchCriteriaBuilder = mock(SearchCriteriaBuilder.class);
    given(dataDefinition.find().createAlias(fieldDefinition.getName(), fieldDefinition.getName()).add(SearchRestrictions.eq(fieldDefinition.getName() + ".id", 1L))).willReturn(searchCriteriaBuilder);
    EntityList list = new EntityListImpl(dataDefinition, "hasMany", 1L);
    // then
    assertEquals(searchCriteriaBuilder, list.find());
}
Also used : SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder) BelongsToType(com.qcadoo.model.api.types.BelongsToType) FieldDefinition(com.qcadoo.model.api.FieldDefinition) EntityList(com.qcadoo.model.api.EntityList) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition) Test(org.junit.Test)

Example 5 with SearchCriteriaBuilder

use of com.qcadoo.model.api.search.SearchCriteriaBuilder in project qcadoo by qcadoo.

the class UnitConversionModelServiceImpl method find.

@Override
@Transactional(readOnly = true)
public List<Entity> find(final String unit, final CustomRestriction customRestriction) {
    final CustomRestriction unitMatchingRestriction = new ConversionMatchingUnitRestriction(unit, customRestriction);
    final SearchCriteriaBuilder searchCriteriaBuilder = getDataDefinition().find();
    unitMatchingRestriction.addRestriction(searchCriteriaBuilder);
    return searchCriteriaBuilder.list().getEntities();
}
Also used : SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder) CustomRestriction(com.qcadoo.model.api.search.CustomRestriction) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

SearchCriteriaBuilder (com.qcadoo.model.api.search.SearchCriteriaBuilder)176 Entity (com.qcadoo.model.api.Entity)82 DataDefinition (com.qcadoo.model.api.DataDefinition)26 Autowired (org.springframework.beans.factory.annotation.Autowired)19 Service (org.springframework.stereotype.Service)19 DataDefinitionService (com.qcadoo.model.api.DataDefinitionService)18 EntityList (com.qcadoo.model.api.EntityList)17 SearchRestrictions (com.qcadoo.model.api.search.SearchRestrictions)17 Collectors (java.util.stream.Collectors)17 SearchResult (com.qcadoo.model.api.search.SearchResult)16 SearchCriterion (com.qcadoo.model.api.search.SearchCriterion)14 Objects (java.util.Objects)14 FilterValueHolder (com.qcadoo.view.api.components.lookup.FilterValueHolder)11 BigDecimal (java.math.BigDecimal)11 Set (java.util.Set)11 UserFieldsMF (com.qcadoo.mes.materialFlow.constants.UserFieldsMF)10 UserLocationFields (com.qcadoo.mes.materialFlow.constants.UserLocationFields)10 SecurityService (com.qcadoo.security.api.SecurityService)10 QcadooSecurityConstants (com.qcadoo.security.constants.QcadooSecurityConstants)10 CustomRestriction (com.qcadoo.model.api.search.CustomRestriction)8