<commit_before>package solutions.digamma.damas.jcr.content

import solutions.digamma.damas.auth.AccessRight
import solutions.digamma.damas.common.InternalStateException
import solutions.digamma.damas.common.UnsupportedActionException
import solutions.digamma.damas.common.WorkspaceException
import solutions.digamma.damas.content.File
import solutions.digamma.damas.content.Folder
import solutions.digamma.damas.content.Metadata
import solutions.digamma.damas.jcr.auth.Permissions
import solutions.digamma.damas.jcr.common.Exceptions
import solutions.digamma.damas.jcr.model.InvalidNodeTypeException
import solutions.digamma.damas.jcr.model.JcrBaseEntity
import solutions.digamma.damas.jcr.model.JcrCreated
import solutions.digamma.damas.jcr.model.JcrModifiable
import solutions.digamma.damas.jcr.names.TypeNamespace
import java.lang.UnsupportedOperationException
import java.net.URI
import javax.jcr.ItemExistsException
import javax.jcr.Node
import javax.jcr.RepositoryException
import javax.jcr.nodetype.NodeType

/**
 * JCR-based implementation convert file abstract type.
 *
 * @param node              JCR node to capture state
 *
 * @author <NAME>
 */
internal abstract class JcrFile
@Throws(WorkspaceException::class)
protected constructor(node: Node) : JcrBaseEntity(node),
        JcrCreated, JcrModifiable, File {

    @Throws(InternalStateException::class)
    override fun checkCompatibility() {
        this.checkTypeCompatibility(TypeNamespace.FILE)
        Exceptions.check {
            this.node.path.startsWith(ROOT_PATH) ||
                throw InternalStateException("Node not in content root.")
        }
    }

    override fun getName(): String {
        return Exceptions.uncheck { this.node.name }
    }

    override fun setName(value: String) {
        Exceptions.uncheck {
            /* Use node's path since paths don't end with a slash.
             */
            val destination = URI
                    .create(this.node.path)
                    .resolve(value)
                    .path
            this.move(destination)
        }
    }

    override fun getParent(): Folder? =
        Exceptions.uncheck { this.node.parent }?.let { JcrFolder.of(it) }

    override fun setParent(value: Folder) {
        this.parentId = value.id ?:
                throw UnsupportedOperationException("Parent ID is null.")
    }

    override fun getParentId(): String =
        Exceptions.uncheck { this.node.parent.identifier }

    override fun setParentId(value: String) = Exceptions.uncheck {
        val path = this.session
                .getNodeByIdentifier(value)
                .path + "/"
        val destination = URI
                .create(path)
                .resolve(this.node.name)
                .path
        this.move(destination)
    }

    override fun getPath(): String = Exceptions.uncheck {
        URI.create(JcrFile.ROOT_PATH)
                .relativize(URI.create(this.node.path)).path
    }

    override fun getMetadata(): Metadata? = null

    override fun setMetadata(metadata: Metadata) {}

    @Throws(RepositoryException::class)
    private fun move(path: String) {
        try {
            this.node.session.move(this.node.path, path)<commit_msg>Add a slash to the beging of file's path<commit_after>package solutions.digamma.damas.jcr.content

import solutions.digamma.damas.auth.AccessRight
import solutions.digamma.damas.common.InternalStateException
import solutions.digamma.damas.common.UnsupportedActionException
import solutions.digamma.damas.common.WorkspaceException
import solutions.digamma.damas.content.File
import solutions.digamma.damas.content.Folder
import solutions.digamma.damas.content.Metadata
import solutions.digamma.damas.jcr.auth.Permissions
import solutions.digamma.damas.jcr.common.Exceptions
import solutions.digamma.damas.jcr.model.InvalidNodeTypeException
import solutions.digamma.damas.jcr.model.JcrBaseEntity
import solutions.digamma.damas.jcr.model.JcrCreated
import solutions.digamma.damas.jcr.model.JcrModifiable
import solutions.digamma.damas.jcr.names.TypeNamespace
import java.lang.UnsupportedOperationException
import java.net.URI
import javax.jcr.ItemExistsException
import javax.jcr.Node
import javax.jcr.RepositoryException
import javax.jcr.nodetype.NodeType

/**
 * JCR-based implementation convert file abstract type.
 *
 * @param node              JCR node to capture state
 *
 * @author <NAME>
 */
internal abstract class JcrFile
@Throws(WorkspaceException::class)
protected constructor(node: Node) : JcrBaseEntity(node),
        JcrCreated, JcrModifiable, File {

    @Throws(InternalStateException::class)
    override fun checkCompatibility() {
        this.checkTypeCompatibility(TypeNamespace.FILE)
        Exceptions.check {
            this.node.path.startsWith(ROOT_PATH) ||
                throw InternalStateException("Node not in content root.")
        }
    }

    override fun getName(): String {
        return Exceptions.uncheck { this.node.name }
    }

    override fun setName(value: String) {
        Exceptions.uncheck {
            /* Use node's path since paths don't end with a slash.
             */
            val destination = URI
                    .create(this.node.path)
                    .resolve(value)
                    .path
            this.move(destination)
        }
    }

    override fun getParent(): Folder? =
        Exceptions.uncheck { this.node.parent }?.let { JcrFolder.of(it) }

    override fun setParent(value: Folder) {
        this.parentId = value.id ?:
                throw UnsupportedOperationException("Parent ID is null.")
    }

    override fun getParentId(): String =
        Exceptions.uncheck { this.node.parent.identifier }

    override fun setParentId(value: String) = Exceptions.uncheck {
        val path = this.session
                .getNodeByIdentifier(value)
                .path
        val destination = URI
                .create("$path/")
                .resolve(this.node.name)
                .path
        this.move(destination)
    }

    override fun getPath(): String = Exceptions.uncheck {
        val path = URI.create(JcrFile.ROOT_PATH)
                .relativize(URI.create(this.node.path)).path
        "/$path"
    }

    override fun getMetadata(): Metadata? = null

    override fun setMetadata(metadata: Metadata) {}

    @Throws(RepositoryException::class)
    private fun move(path: String) {
        try {
            this.node.session.move(this.node.path, path)
{'fraction_non_alphanumeric': 0.0935618341824919, 'fraction_numerical': 0.0, 'mean_word_length': 2.744508670520231, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '424034', 'n_tokens_mistral': 1829, 'n_tokens_neox': 1785, 'n_words': 364}