use of org.molgenis.data.meta.model.PackageMetadata.PACKAGE in project molgenis by molgenis.
the class EntityTypeValidator method validateOwnAttributes.
/**
* Validates the attributes owned by this entity:
* 1) validates that the parent entity doesn't have attributes with the same name
* 2) validates that this entity doesn't have attributes with the same name
* 3) validates that this entity has attributes defined at all
*
* @param entityType entity meta data
* @throws MolgenisValidationException if an attribute is owned by another entity or a parent attribute has the same name
*/
private static void validateOwnAttributes(EntityType entityType) {
// Validate that entity has attributes
if (asStream(entityType.getAllAttributes()).collect(toList()).isEmpty()) {
throw new MolgenisValidationException(new ConstraintViolation(format("Entity [%s] does not contain any attributes. Did you use the correct package+entity name combination in both the entities as well as the attributes sheet?", entityType.getId())));
}
// Validate that entity does not contain multiple attributes with the same name
Multimap<String, Attribute> attrMultiMap = asStream(entityType.getAllAttributes()).collect(MultimapCollectors.toArrayListMultimap(Attribute::getName, Function.identity()));
attrMultiMap.keySet().forEach(attrName -> {
if (attrMultiMap.get(attrName).size() > 1) {
throw new MolgenisValidationException(new ConstraintViolation(format("Entity [%s] contains multiple attributes with name [%s]", entityType.getId(), attrName)));
}
});
// Validate that entity attributes with same name do no exist in parent entity
EntityType extendsEntityType = entityType.getExtends();
if (extendsEntityType != null) {
Map<String, Attribute> extendsAllAttrMap = stream(extendsEntityType.getAllAttributes().spliterator(), false).collect(toMap(Attribute::getName, Function.identity(), (u, v) -> {
throw new IllegalStateException(String.format("Duplicate key %s", u));
}, LinkedHashMap::new));
entityType.getOwnAllAttributes().forEach(attr -> {
if (extendsAllAttrMap.containsKey(attr.getName())) {
throw new MolgenisValidationException(new ConstraintViolation(format("An attribute with name [%s] already exists in entity [%s] or one of its parents", attr.getName(), extendsEntityType.getId())));
}
});
}
}
use of org.molgenis.data.meta.model.PackageMetadata.PACKAGE in project molgenis by molgenis.
the class SystemEntityTypePersister method injectExistingPackageIdentifiers.
private void injectExistingPackageIdentifiers(List<Package> systemPackages) {
Map<String, Package> existingPackageMap = dataService.findAll(PACKAGE, Package.class).collect(toMap(Package::getId, pack -> pack));
systemPackages.forEach(pack -> {
Package existingPackage = existingPackageMap.get(pack.getId());
if (existingPackage != null) {
pack.setId(existingPackage.getId());
}
});
}
use of org.molgenis.data.meta.model.PackageMetadata.PACKAGE in project molgenis by molgenis.
the class PlatformIT method testDeletePackage.
@WithMockUser(username = USERNAME)
@Test(singleThreaded = true)
public void testDeletePackage() {
populateUserPermissions();
runAsSystem(() -> {
MetaDataService metadataService = dataService.getMeta();
Package parentPackage = packageFactory.create("parent").setLabel("parent");
Package subPackage = packageFactory.create("parent_sub").setLabel("sub").setParent(parentPackage);
metadataService.upsertPackages(Stream.of(parentPackage, subPackage));
EntityType entityTypeInSubPackage = testHarness.createDynamicRefEntityType("entityInSub", subPackage);
EntityType entityTypeInParentPackage = testHarness.createDynamicTestEntityType("entityInParent", parentPackage, entityTypeInSubPackage);
metadataService.upsertEntityTypes(asList(entityTypeInSubPackage, entityTypeInParentPackage));
List<Entity> entities = createAndAdd(entityTypeInParentPackage, entityTypeInSubPackage, 5);
Set<Entity> refEntities = entities.stream().map(e -> e.getEntity(ATTR_XREF)).collect(toSet());
assertPresent(entityTypeInParentPackage, entities);
assertPresent(entityTypeInSubPackage, newArrayList(refEntities));
dataService.deleteById(PACKAGE, "parent");
assertNull(metadataService.getPackage("parent"));
assertNull(metadataService.getPackage("parent_sub"));
entities.forEach(this::assertNotPresent);
refEntities.forEach(this::assertNotPresent);
});
}
use of org.molgenis.data.meta.model.PackageMetadata.PACKAGE in project molgenis by molgenis.
the class PlatformIT method testDeletePackageWithOneToMany.
@WithMockUser(username = USERNAME)
@Test(singleThreaded = true)
public void testDeletePackageWithOneToMany() {
populateUserPermissions();
runAsSystem(() -> {
MetaDataService metadataService = dataService.getMeta();
Package package_ = packageFactory.create("package_onetomany").setLabel("package");
metadataService.upsertPackages(Stream.of(package_));
EntityType refEntityType = testHarness.createDynamicRefEntityType("entityType_onetomany", package_);
EntityType entityType = testHarness.createDynamicTestEntityType("refEntityType_onetomany", package_, refEntityType);
Attribute oneToManyAttribute = attributeFactory.create("onetomany").setName("onetomany").setDataType(AttributeType.ONE_TO_MANY).setRefEntity(entityType).setMappedBy(entityType.getAttribute(ATTR_XREF));
refEntityType.addAttribute(oneToManyAttribute);
metadataService.upsertEntityTypes(asList(refEntityType, entityType));
List<Entity> entities = createAndAdd(entityType, refEntityType, 5);
Set<Entity> refEntities = entities.stream().map(e -> e.getEntity(ATTR_XREF)).collect(toSet());
assertPresent(entityType, entities);
assertPresent(refEntityType, newArrayList(refEntities));
dataService.deleteById(PACKAGE, "package_onetomany");
assertNull(metadataService.getPackage("package_onetomany"));
assertNull(dataService.getEntityType(entityType.getId()));
assertNull(dataService.getEntityType(refEntityType.getId()));
entities.forEach(this::assertNotPresent);
refEntities.forEach(this::assertNotPresent);
});
}
Aggregations