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