public void addRefEntityId(String name, Object idValue) { HugeSet<Object> refEntityIds = refEntitiesIds.get(name); // only add entity id if this validation run requires entity if (refEntityIds != null) { refEntityIds.add(idValue); } }
private void validateEntityValueReferences(Entity entity, ValidationResource validationResource) { validationResource .getRefAttrs() .forEach( refAttr -> { HugeSet<Object> refEntityIds = validationResource.getRefEntitiesIds().get(refAttr.getRefEntity().getName()); Iterable<Entity> refEntities; if (refAttr.getDataType() instanceof XrefField) { Entity refEntity = entity.getEntity(refAttr.getName()); if (refEntity != null) { refEntities = singleton(refEntity); } else { refEntities = emptyList(); } } else { refEntities = entity.getEntities(refAttr.getName()); } for (Entity refEntity : refEntities) { if (!refEntityIds.contains(refEntity.getIdValue())) { boolean selfReference = entity.getEntityMetaData().getName().equals(refAttr.getRefEntity().getName()); if (!(selfReference && entity.getIdValue().equals(refEntity.getIdValue()))) { String message = String.format( "Unknown xref value '%s' for attribute '%s' of entity '%s'.", DataConverter.toString(refEntity.getIdValue()), refAttr.getName(), getName()); ConstraintViolation constraintViolation = new ConstraintViolation( message, refAttr, Long.valueOf(validationResource.getRow())); validationResource.addViolation(constraintViolation); } } validationResource.addRefEntityId(getName(), refEntity.getIdValue()); } }); }
@Override public void close() { if (refEntitiesIds != null) { for (HugeSet<Object> refEntityIds : refEntitiesIds.values()) { try { refEntityIds.close(); } catch (IOException e) { throw new RuntimeException(e); } } } if (uniqueAttrsValues != null) { for (HugeMap<Object, Object> uniqueAttrValues : uniqueAttrsValues.values()) { try { uniqueAttrValues.close(); } catch (IOException e) { throw new RuntimeException(e); } } } }
private void initReferenceValidation(ValidationResource validationResource) { // get reference attrs List<AttributeMetaData> refAttrs = StreamSupport.stream(getEntityMetaData().getAtomicAttributes().spliterator(), false) .filter( attr -> (attr.getDataType() instanceof XrefField || attr.getDataType() instanceof MrefField) && attr.getExpression() == null) .collect(Collectors.toList()); // get referenced entity ids if (!refAttrs.isEmpty()) { Map<String, HugeSet<Object>> refEntitiesIds = new HashMap<>(); refAttrs.forEach( refAttr -> { EntityMetaData refEntityMeta = refAttr.getRefEntity(); String refEntityName = refEntityMeta.getName(); HugeSet<Object> refEntityIds = refEntitiesIds.get(refEntityName); if (refEntityIds == null) { refEntityIds = new HugeSet<>(); refEntitiesIds.put(refEntityName, refEntityIds); Query q = new QueryImpl() .fetch(new Fetch().field(refEntityMeta.getIdAttribute().getName())); for (Iterator<Entity> it = dataService.findAll(refEntityName, q).iterator(); it.hasNext(); ) { refEntityIds.add(it.next().getIdValue()); } } }); validationResource.setRefEntitiesIds(refEntitiesIds); } validationResource.setRefAttrs(refAttrs); }