nuts-foundation/nuts-discovery

View on GitHub
src/main/kotlin/nl/nuts/discovery/store/CustomNodeRepository.kt

Summary

Maintainability
A
0 mins
Test Coverage
/*
 *     Nuts discovery service for Corda network creation
 *     Copyright (C) 2020 Nuts community
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

package nl.nuts.discovery.store

import nl.nuts.discovery.DiscoveryException
import nl.nuts.discovery.store.entity.Node
import org.springframework.stereotype.Repository
import javax.transaction.Transactional

/**
 * custom repo for overriding save method
 */
@Transactional
@Repository("customNodeRepository")
class CustomNodeRepository(private val delegate: NodeRepository): NodeRepository by delegate {
    /**
     * Custom implementation that replaces node based on CN/Name
     */
    override fun <S : Node?> save(entity: S): S {
        if (entity?.name == null) {
            throw DiscoveryException("name can't be null")
        }
        val node = delegate.findByName(entity.name!!) ?: return delegate.save(entity)

        return delegate.save(node.apply {
            hash = entity.hash
            raw = entity.raw
        }) as S
    }
}