if (isSingleReferenceType(attr) && isSingleReferenceType(updatedAttr)) { return; if (isMultipleReferenceType(attr) && isMultipleReferenceType(updatedAttr)) { return; if (isSingleReferenceType(attr) && !isReferenceType(updatedAttr)) { dropForeignKey(entityType, attr); if (!isReferenceType(attr) && isSingleReferenceType(updatedAttr)) { createForeignKey(entityType, updatedAttr);
@Override public Iterable<String> getEntityTypeIds() { return dataService .query(ENTITY_TYPE_META_DATA, EntityType.class) .eq(BACKEND, POSTGRESQL) .fetch(getEntityTypeFetch()) .findAll() .map(EntityType::getId) ::iterator; }
public boolean hasSelfReferences() { return EntityTypeUtils.hasSelfReferences(getTarget()); } }
private Multimap<String, Attribute> getDefaultValueMap(EntityType entityType) { Multimap<String, Attribute> defaultValueMultiMap = LinkedHashMultimap.create(); dataService .query(ATTRIBUTE_META_DATA, Attribute.class) .eq(REF_ENTITY_TYPE, entityType.getIdValue()) .and() .not() .eq(DEFAULT_VALUE, null) .findAll() .forEach( attribute -> { if (EntityTypeUtils.isSingleReferenceType(attribute)) { Entity defaultEntityValue = (Entity) getDefaultTypedValue(attribute); defaultValueMultiMap.put(defaultEntityValue.getIdValue().toString(), attribute); } else if (EntityTypeUtils.isMultipleReferenceType(attribute)) { @SuppressWarnings("unchecked") Iterable<Entity> defaultEntitiesValue = (Iterable<Entity>) getDefaultTypedValue(attribute); defaultEntitiesValue.forEach( defaultEntityValue -> defaultValueMultiMap.put( defaultEntityValue.getIdValue().toString(), attribute)); } }); return defaultValueMultiMap; }
private static List<Object> convert(List<Object> xLabels) { return xLabels .stream() .map( xLabel -> { Object value; if (xLabel instanceof Entity) { Map<String, Object> valueMap = new HashMap<>(); Entity entity = (Entity) xLabel; for (Attribute attr : entity.getEntityType().getAtomicAttributes()) { if (!isReferenceType(attr)) { Object attributeValue = entity.get(attr.getName()); valueMap.put( attr.getName(), attributeValue != null ? attributeValue.toString() : null); } } value = valueMap; } else { value = xLabel; } return value; }) .collect(Collectors.toList()); }
boolean isXrefOrCategorialDataType(Attribute attribute) { return isSingleReferenceType(attribute); }
private static boolean hasJunctionTable(Attribute attribute) { return isMultipleReferenceType(attribute) && attribute.getDataType() != AttributeType.ONE_TO_MANY; }
if (isStringType(attr) || isTextType(attr)) { result.append(' ').append(columnName); } else {
EntityTypeUtils.buildFullName( repositoryToCopyFrom.getEntityType().getPackage(), request.getNewEntityName()); if (dataService.hasRepository(newFullName)) {
if (isSingleReferenceType(readonlyAttr)) { if (value != null) { value = ((Entity) value).getIdValue(); existingValue = ((Entity) existingValue).getIdValue(); } else if (isMultipleReferenceType(readonlyAttr)) { value = stream(entity.getEntities(readonlyAttr.getName()).spliterator(), false)
.filter(attr -> isReferenceType(attr) && attr.getExpression() == null) .collect(toList()); } else { .filter( attr -> isReferenceType(attr) && attr.getExpression() == null && isDifferentBackend(backend, attr))
private static List<String> getSqlTableConstraints(EntityType entityType, Attribute attr) { List<String> tableConstraints = Lists.newArrayList(); if (attr.getName().equals(entityType.getIdAttribute().getName())) { tableConstraints.add(getSqlConstraintPrimaryKey(entityType, attr)); } else { if (isSingleReferenceType(attr) && isPersistedInPostgreSql(attr.getRefEntity())) { tableConstraints.add(getSqlForeignKey(entityType, attr)); } if (attr.isUnique()) { tableConstraints.add(getSqlUniqueKey(entityType, attr)); } if (attr.getDataType() == ENUM) { tableConstraints.add(getSqlCheckConstraint(entityType, attr)); } } return tableConstraints; }
static boolean generateSqlColumnDefaultConstraint(Attribute attr) { return attr.getDefaultValue() != null && !EntityTypeUtils.isMultipleReferenceType(attr); }
Object value = dehydratedEntity.get(name); if (value != null) { if (isMultipleReferenceType(attribute)) { } else if (isSingleReferenceType(attribute)) { value = entityManager.getReference(attribute.getRefEntity(), value);
if (isReferenceType(mappingTargetAttribute)) { String mappingTargetRefEntityName = mappingTargetAttribute.getRefEntity().getId(); String targetRepositoryRefEntityName = targetRepositoryAttribute.getRefEntity().getId();
Object attrValue = entity.get(uniqueAttr.getName()); if (attrValue != null) { if (isSingleReferenceType(uniqueAttr)) { attrValue = ((Entity) attrValue).getIdValue();
/** * Returns whether this attribute is stored in the entity table or another table such as a * junction table or referenced entity table. * * @param attr attribute * @return whether this attribute is stored in another table than the entity table */ private static boolean isPersistedInOtherTable(Attribute attr) { boolean bidirectionalOneToMany = attr.getDataType() == ONE_TO_MANY && attr.isMappedBy(); return isMultipleReferenceType(attr) || bidirectionalOneToMany; }
@Override public Iterator<Repository<Entity>> iterator() { return dataService .query(ENTITY_TYPE_META_DATA, EntityType.class) .eq(BACKEND, POSTGRESQL) .and() .eq(IS_ABSTRACT, false) .fetch(getEntityTypeFetch()) .findAll() .map(this::getRepository) .iterator(); }
private long applyMappingsInternal( MappingTarget mappingTarget, Repository<Entity> targetRepo, Progress progress, int depth) { progress.status("Applying mappings to repository [" + targetRepo.getEntityType().getId() + "]"); long result = applyMappingsToRepositories(mappingTarget, targetRepo, progress, depth); if (hasSelfReferences(targetRepo.getEntityType())) { progress.status( "Self reference found, applying the mapping for a second time to set references"); applyMappingsToRepositories(mappingTarget, targetRepo, progress, depth); } progress.status( "Done applying mappings to repository [" + targetRepo.getEntityType().getId() + "]"); return result; }
/** * Updates foreign keys based on referenced entity changes. * * @param entityType entity meta data * @param attr current attribute * @param updatedAttr updated attribute */ private void updateRefEntity(EntityType entityType, Attribute attr, Attribute updatedAttr) { if (isSingleReferenceType(attr) && isSingleReferenceType(updatedAttr)) { dropForeignKey(entityType, attr); if (attr.getRefEntity().getIdAttribute().getDataType() != updatedAttr.getRefEntity().getIdAttribute().getDataType()) { updateColumnDataType(entityType, updatedAttr); } createForeignKey(entityType, updatedAttr); } else if (isMultipleReferenceType(attr) && isMultipleReferenceType(updatedAttr)) { throw new MolgenisDataException( format( "Updating entity [%s] attribute [%s] referenced entity from [%s] to [%s] not allowed for type [%s]", entityType.getId(), attr.getName(), attr.getRefEntity().getId(), updatedAttr.getRefEntity().getId(), updatedAttr.getDataType().toString())); } }