@RunAsSystem public FileIngestJob createJob(FileIngestJobExecution fileIngestJobExecution) { dataService.add(FileIngestJobExecutionMetaData.ENTITY_NAME, fileIngestJobExecution); String username = fileIngestJobExecution.getUser(); Progress progress = new ProgressImpl(fileIngestJobExecution, jobExecutionUpdater, mailSender); TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); RunAsUserToken runAsAuthentication = new RunAsUserToken( "Job Execution", username, null, userDetailsService.loadUserByUsername(username).getAuthorities(), null); Entity fileIngestEntity = fileIngestJobExecution.getFileIngest(); Entity targetEntityEntity = fileIngestEntity.getEntity(FileIngestMetaData.ENTITY_META_DATA); String targetEntityName = targetEntityEntity.getString(EntityMetaDataMetaData.FULL_NAME); String url = fileIngestEntity.getString(FileIngestMetaData.URL); String loader = fileIngestEntity.getString(FileIngestMetaData.LOADER); String failureEmail = fileIngestEntity.getString(FileIngestMetaData.FAILURE_EMAIL); return new FileIngestJob( progress, transactionTemplate, runAsAuthentication, fileIngester, targetEntityName, url, loader, failureEmail, fileIngestJobExecution.getIdentifier()); }
@Override public Entity getEntity(String attributeName) { if (fetch.hasField(attributeName)) { return decoratedEntity.getEntity(attributeName); } else { return entityManager.getReference(getEntityMetaData(), getIdValue()).getEntity(attributeName); } }
@Override public <E extends Entity> E getEntity(String attributeName, Class<E> clazz) { if (fetch.hasField(attributeName)) { return decoratedEntity.getEntity(attributeName, clazz); } else { return entityManager .getReference(getEntityMetaData(), getIdValue()) .getEntity(attributeName, clazz); } }
@Test public void checkXrefEntityWrongType() { AttributeMetaData refIdAttr = when(mock(AttributeMetaData.class).getName()).thenReturn("refId").getMock(); when(refIdAttr.getDataType()).thenReturn(STRING); EntityMetaData refEntityMeta = mock(EntityMetaData.class); when(refEntityMeta.getName()).thenReturn("refEntity"); when(refEntityMeta.getIdAttribute()).thenReturn(refIdAttr); when(refEntityMeta.getAtomicAttributes()).thenReturn(asList(refIdAttr)); AttributeMetaData otherRefIdAttr = when(mock(AttributeMetaData.class).getName()).thenReturn("otherRefId").getMock(); when(otherRefIdAttr.getDataType()).thenReturn(STRING); EntityMetaData otherRefEntityMeta = mock(EntityMetaData.class); when(otherRefEntityMeta.getName()).thenReturn("otherRefEntity"); when(otherRefEntityMeta.getIdAttribute()).thenReturn(refIdAttr); when(otherRefEntityMeta.getAtomicAttributes()).thenReturn(asList(otherRefIdAttr)); String idAttrName = "id"; String xrefAttrName = "xref"; AttributeMetaData idAttr = when(mock(AttributeMetaData.class).getName()).thenReturn(idAttrName).getMock(); when(idAttr.getDataType()).thenReturn(STRING); AttributeMetaData xrefAttr = when(mock(AttributeMetaData.class).getName()).thenReturn(xrefAttrName).getMock(); when(xrefAttr.getDataType()).thenReturn(XREF); when(xrefAttr.getRefEntity()).thenReturn(refEntityMeta); EntityMetaData entityMeta = mock(EntityMetaData.class); when(entityMeta.getName()).thenReturn("entity"); when(entityMeta.getIdAttribute()).thenReturn(idAttr); when(entityMeta.getAtomicAttributes()).thenReturn(asList(idAttr, xrefAttr)); Entity refEntity0 = when(mock(Entity.class).getEntityMetaData()) .thenReturn(otherRefEntityMeta) .getMock(); // wrong // intRangeMinMeta when(refEntity0.getIdValue()).thenReturn("otherRefId0"); Entity entity0 = when(mock(Entity.class).getEntityMetaData()).thenReturn(entityMeta).getMock(); when(entity0.getIdValue()).thenReturn("id0"); when(entity0.getEntity(xrefAttrName)).thenReturn(refEntity0); Set<ConstraintViolation> constraints = entityAttributesValidator.validate(entity0, entity0.getEntityMetaData()); assertEquals(constraints.size(), 1); }
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()); } }); }
private void createEntityValuesResponseRec( Entity entity, Iterable<AttributeMetaData> attrs, Fetch fetch, Map<String, Object> responseData) { responseData.put( "_href", Href.concatEntityHref(BASE_URI, entity.getEntityMetaData().getName(), entity.getIdValue())); for (AttributeMetaData attr : attrs) // TODO performance use fetch instead of attrs { String attrName = attr.getName(); if (fetch == null || fetch.hasField(attr)) { FieldTypeEnum dataType = attr.getDataType().getEnumType(); switch (dataType) { case BOOL: responseData.put(attrName, entity.getBoolean(attrName)); break; case CATEGORICAL: case XREF: case FILE: Entity refEntity = entity.getEntity(attrName); Map<String, Object> refEntityResponse; if (refEntity != null) { Fetch refAttrFetch = fetch != null ? fetch.getFetch(attr) : null; refEntityResponse = createEntityResponse(refEntity, refAttrFetch, false); } else { refEntityResponse = null; } responseData.put(attrName, refEntityResponse); break; case CATEGORICAL_MREF: case MREF: Iterable<Entity> refEntities = entity.getEntities(attrName); List<Map<String, Object>> refEntityResponses; if (refEntities != null) { refEntityResponses = new ArrayList<Map<String, Object>>(); Fetch refAttrFetch = fetch != null ? fetch.getFetch(attrName) : null; for (Entity refEntitiesEntity : refEntities) { refEntityResponses.add( createEntityResponse(refEntitiesEntity, refAttrFetch, false)); } } else { refEntityResponses = null; } responseData.put(attrName, refEntityResponses); break; case COMPOUND: throw new RuntimeException("Invalid data type [" + dataType + "]"); case DATE: Date dateValue = entity.getDate(attrName); String dateValueStr = dateValue != null ? getDateFormat().format(dateValue) : null; responseData.put(attrName, dateValueStr); break; case DATE_TIME: Date dateTimeValue = entity.getDate(attrName); String dateTimeValueStr = dateTimeValue != null ? getDateTimeFormat().format(dateTimeValue) : null; responseData.put(attrName, dateTimeValueStr); break; case DECIMAL: responseData.put(attrName, entity.getDouble(attrName)); break; case EMAIL: case ENUM: case HTML: case HYPERLINK: case SCRIPT: case STRING: case TEXT: responseData.put(attrName, entity.getString(attrName)); break; case INT: responseData.put(attrName, entity.getInt(attrName)); break; case LONG: responseData.put(attrName, entity.getLong(attrName)); break; default: throw new RuntimeException("Unknown data type [" + dataType + "]"); } } } }