fun checkIncomingLinks()

in dnq-transient-store/src/main/kotlin/com/jetbrains/teamsys/dnq/database/ConstraintsUtil.kt [59:89]


    fun checkIncomingLinks(changesTracker: TransientChangesTracker): Set<DataIntegrityViolationException> {
        return changesTracker.changedEntities
                .asSequence()
                .filter { it.isRemoved }
                .map { targetEntity ->
                    val badIncomingLinks = targetEntity.incomingLinks
                            .asSequence()
                            .mapNotNull { (linkName, linkedEntities) ->
                                var incomingLinkViolation: IncomingLinkViolation? = null
                                linkedEntities
                                        .asSequence()
                                        .filterIsInstance<TransientEntity>()
                                        .filter { sourceEntity -> !sourceEntity.isRemoved && targetEntity !in sourceEntity.getRemovedLinks(linkName) }
                                        .takeWhile { sourceEntity ->
                                            val violation = incomingLinkViolation
                                                    ?: createIncomingLinkViolation(sourceEntity, linkName)
                                                            .also { newViolation ->
                                                                incomingLinkViolation = newViolation
                                                            }
                                            violation.tryAddCause(sourceEntity)
                                        }
                                        .toList()
                                incomingLinkViolation
                            }
                            .toList()
                    targetEntity to badIncomingLinks
                }
                .filter { (_, badIncomingLinks) -> badIncomingLinks.isNotEmpty() }
                .map { (targetEntity, badIncomingLinks) -> createIncomingLinksException(targetEntity, badIncomingLinks) }
                .toCollection(HashSetDecorator())
    }