Search in sources :

Example 1 with SearchConjunction

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

the class DraftDocumentsNotificationService method countDraftDocumentsForUser.

int countDraftDocumentsForUser(Long currentUserId) {
    EntityList userLocations = userDataDefinition().get(currentUserId).getHasManyField(UserFieldsMF.USER_LOCATIONS);
    SearchConjunction conjunction = SearchRestrictions.conjunction();
    conjunction.add(eq(DocumentFields.STATE, DocumentState.DRAFT.getStringValue()));
    conjunction.add(eq(DocumentFields.ACTIVE, Boolean.TRUE));
    conjunction.add(isNull("order.id"));
    SearchCriteriaBuilder criteriaBuilder = documentDataDefinition().find();
    if (!userLocations.isEmpty()) {
        criteriaBuilder.createAlias(DocumentFields.LOCATION_FROM, "locFrom", JoinType.LEFT);
        criteriaBuilder.createAlias(DocumentFields.LOCATION_TO, "locTo", JoinType.LEFT);
        Set<Long> locationIds = userLocations.stream().map(ul -> ul.getBelongsToField(UserLocationFields.LOCATION)).map(Entity::getId).collect(Collectors.toSet());
        conjunction.add(or(in("locFrom.id", locationIds), in("locTo.id", locationIds)));
    }
    criteriaBuilder.add(conjunction);
    return criteriaBuilder.list().getTotalNumberOfEntities();
}
Also used : SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder) SearchConjunction(com.qcadoo.model.api.search.SearchConjunction) EntityList(com.qcadoo.model.api.EntityList)

Example 2 with SearchConjunction

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

the class UserLocationHooks method checkIfUserLocationAlreadyExists.

private boolean checkIfUserLocationAlreadyExists(final DataDefinition userLocationDD, final Entity userLocation) {
    Entity location = requireNonNull(userLocation.getBelongsToField(UserLocationFields.LOCATION));
    Entity user = requireNonNull(userLocation.getBelongsToField(UserLocationFields.USER));
    SearchConjunction conjunction = SearchRestrictions.conjunction();
    conjunction.add(belongsTo(UserLocationFields.LOCATION, location));
    conjunction.add(belongsTo(UserLocationFields.USER, user));
    if (userLocation.getId() != null) {
        conjunction.add(ne("id", userLocation.getId()));
    }
    boolean exists = 0 != userLocationDD.count(conjunction);
    if (exists) {
        userLocation.addError(userLocationDD.getField(UserLocationFields.LOCATION), "qcadooView.validate.field.error.invalidUniqueType");
    }
    return exists;
}
Also used : Entity(com.qcadoo.model.api.Entity) SearchConjunction(com.qcadoo.model.api.search.SearchConjunction)

Aggregations

SearchConjunction (com.qcadoo.model.api.search.SearchConjunction)2 Entity (com.qcadoo.model.api.Entity)1 EntityList (com.qcadoo.model.api.EntityList)1 SearchCriteriaBuilder (com.qcadoo.model.api.search.SearchCriteriaBuilder)1