All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kotlinx.dnq.link.XdManyToManyLink.kt Maven / Gradle / Ivy

/**
 * Copyright 2006 - 2018 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kotlinx.dnq.link

import jetbrains.exodus.entitystore.Entity
import jetbrains.exodus.query.metadata.AssociationEndCardinality
import jetbrains.exodus.query.metadata.AssociationEndType
import kotlinx.dnq.XdEntity
import kotlinx.dnq.XdEntityType
import kotlinx.dnq.query.XdMutableQuery
import kotlinx.dnq.query.isNotEmpty
import kotlinx.dnq.util.reattach
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
import kotlin.reflect.KProperty1

open class XdManyToManyLink(
        val entityType: XdEntityType,
        override val oppositeField: KProperty1>,
        dbPropertyName: String?,
        dbOppositePropertyName: String?,
        onDeletePolicy: OnDeletePolicy,
        onTargetDeletePolicy: OnDeletePolicy,
        required: Boolean
) : ReadOnlyProperty>, XdLink(
        entityType,
        dbPropertyName,
        dbOppositePropertyName,
        if (required) AssociationEndCardinality._1_n else AssociationEndCardinality._0_n,
        AssociationEndType.UndirectedAssociationEnd,
        onDeletePolicy,
        onTargetDeletePolicy
) {

    override fun getValue(thisRef: R, property: KProperty<*>): XdMutableQuery {
        return object : XdMutableQuery(entityType) {
            override val entityIterable: Iterable
                get() = thisRef.reattach().getLinks(property.dbName)

            override fun add(entity: T) {
                thisRef.reattach().createManyToMany(property.dbName, dbOppositePropertyName ?: oppositeField.name, entity.reattach())
            }

            override fun remove(entity: T) {
                thisRef.reattach().deleteLink(property.dbName, entity.reattach())
                entity.reattach().deleteLink(dbOppositePropertyName ?: oppositeField.name, thisRef.reattach())
            }

            override fun clear() {
                thisRef.reattach().clearManyToMany(property.dbName, dbOppositePropertyName ?: oppositeField.name)
            }
        }
    }

    override fun isDefined(thisRef: R, property: KProperty<*>): Boolean {
        return getValue(thisRef, property).isNotEmpty
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy