use of org.molgenis.data.UnknownRepositoryException in project molgenis by molgenis.
the class AttributeRequestMapperImpl method processParentAndMappedby.
private void processParentAndMappedby(Map<String, Object> attributeRequest, Map<String, Attribute> attributes, EntityType entityType) {
String attributeId = attributeRequest.get("id").toString();
for (Entry<String, Object> entry : attributeRequest.entrySet()) {
if (entry.getKey().equals("mappedByAttribute")) {
String mappedByAttrId = getStringValue(entry.getValue());
Attribute mappedByAttr = attributes.get(mappedByAttrId);
if (mappedByAttr == null) {
Repository<Attribute> repository = metaDataService.getRepository(AttributeMetadata.ATTRIBUTE_META_DATA, Attribute.class).orElseThrow(() -> new UnknownRepositoryException(AttributeMetadata.ATTRIBUTE_META_DATA));
mappedByAttr = repository.findOneById(mappedByAttrId);
}
if (mappedByAttr == null) {
throw new UnknownAttributeException(entityType, mappedByAttrId);
}
Attribute attribute = attributes.get(attributeId);
attribute.setMappedBy(mappedByAttr);
} else if (entry.getKey().equals("parent")) {
String parentId = getStringValue(entry.getValue());
Attribute parentAttr = attributes.get(parentId);
if (parentAttr == null) {
parentAttr = getAttributeFromParent(entityType.getExtends(), parentId);
}
Attribute attribute = attributes.get(attributeId);
attribute.setParent(parentAttr);
}
}
}
use of org.molgenis.data.UnknownRepositoryException in project molgenis by molgenis.
the class EmxDataProviderTest method testGetEntitiesAlternativeEntityTypeIdUnknownRepository.
@Test
void testGetEntitiesAlternativeEntityTypeIdUnknownRepository() {
String entityTypeId = "base_EntityTypeId";
EntityType entityType = when(mock(EntityType.class).getId()).thenReturn(entityTypeId).getMock();
RepositoryCollection repositoryCollection = mock(RepositoryCollection.class);
doThrow(new UnknownRepositoryException("EntityTypeId")).when(repositoryCollection).getRepository("EntityTypeId");
when(emxImportJob.getSource()).thenReturn(repositoryCollection);
assertThrows(UnknownRepositoryException.class, () -> emxDataProvider.getEntities(entityType).collect(toList()));
}
use of org.molgenis.data.UnknownRepositoryException in project molgenis by molgenis.
the class EmxDataProviderTest method testGetEntitiesUnknownRepository.
@Test
void testGetEntitiesUnknownRepository() {
String entityTypeId = "EntityTypeId";
EntityType entityType = when(mock(EntityType.class).getId()).thenReturn(entityTypeId).getMock();
RepositoryCollection repositoryCollection = mock(RepositoryCollection.class);
doThrow(new UnknownRepositoryException(entityTypeId)).when(repositoryCollection).getRepository(entityType);
when(emxImportJob.getSource()).thenReturn(repositoryCollection);
assertThrows(UnknownRepositoryException.class, () -> emxDataProvider.getEntities(entityType).collect(toList()));
}
use of org.molgenis.data.UnknownRepositoryException in project molgenis by molgenis.
the class AttributeResponseMapperImpl method getCategories.
private List<Category> getCategories(EntityType entityType) {
Repository<Entity> repository = metaDataService.getRepository(entityType).orElseThrow(() -> new UnknownRepositoryException(entityType.getId()));
Attribute idAttribute = entityType.getIdAttribute();
Attribute labelAttribute = entityType.getLabelAttribute();
return repository.query().findAll().map(entity -> Category.builder().setId(entity.get(idAttribute)).setLabel(entity.get(labelAttribute).toString()).build()).collect(toList());
}
use of org.molgenis.data.UnknownRepositoryException in project molgenis by molgenis.
the class AttributeValidator method validateDefaultValue.
void validateDefaultValue(Attribute attr, boolean validateEntityReferences) {
String value = attr.getDefaultValue();
if (value != null) {
if (attr.isUnique()) {
throw new MolgenisDataException("Unique attribute " + attr.getName() + " cannot have default value");
}
if (attr.getExpression() != null) {
throw new MolgenisDataException("Computed attribute " + attr.getName() + " cannot have default value");
}
AttributeType fieldType = attr.getDataType();
if (attr.getMaxLength() != null && value.length() > attr.getMaxLength()) {
throw new MolgenisDataException("Default value for attribute [" + attr.getName() + "] exceeds the maximum length [" + attr.getMaxLength() + "]");
}
if (fieldType == AttributeType.EMAIL) {
checkEmail(value);
}
if (fieldType == AttributeType.HYPERLINK) {
checkHyperlink(value);
}
if (fieldType == AttributeType.ENUM) {
checkEnum(attr, value);
}
// Get typed value to check if the value is of the right type.
Object typedValue;
try {
typedValue = EntityUtils.getTypedValue(value, attr, entityManager);
} catch (NumberFormatException e) {
throw new MolgenisValidationException(new ConstraintViolation(format("Invalid default value [%s] for data type [%s]", value, attr.getDataType())));
}
if (validateEntityReferences) {
if (isSingleReferenceType(attr)) {
Entity refEntity = (Entity) typedValue;
EntityType refEntityType = attr.getRefEntity();
if (dataService.getMeta().getRepository(refEntityType).orElseThrow(() -> new UnknownRepositoryException(refEntityType.getId())).query().eq(refEntityType.getIdAttribute().getName(), refEntity.getIdValue()).count() == 0) {
throw new MolgenisValidationException(new ConstraintViolation(format("Default value [%s] refers to an unknown entity", value)));
}
} else if (isMultipleReferenceType(attr)) {
@SuppressWarnings("unchecked") Iterable<Entity> refEntitiesValue = (Iterable<Entity>) typedValue;
EntityType refEntityType = attr.getRefEntity();
if (dataService.query(refEntityType.getId()).in(refEntityType.getIdAttribute().getName(), stream(refEntitiesValue).map(Entity::getIdValue).collect(toList())).count() < Iterables.size(refEntitiesValue)) {
throw new MolgenisValidationException(new ConstraintViolation(format("Default value [%s] refers to one or more unknown entities", value)));
}
}
}
}
}
Aggregations