<commit_before>/*
 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     <NAME> <<EMAIL>>
 */
package org.nuxeo.drive.adapter.impl;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.nuxeo.drive.adapter.FileSystemItem;
import org.nuxeo.drive.adapter.FolderItem;
import org.nuxeo.drive.service.FileSystemItemManager;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.ClientRuntimeException;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.DocumentRef;
import org.nuxeo.ecm.core.api.IdRef;
import org.nuxeo.ecm.core.api.security.SecurityConstants;
import org.nuxeo.ecm.core.trash.TrashService;
import org.nuxeo.runtime.api.Framework;

/**
 * {@link DocumentModel} backed implementation of a {@link FileSystemItem}.
 *
 * @author <NAME>
 * @see DocumentBackedFileItem
 * @see DocumentBackedFolderItem
 */
public abstract class AbstractDocumentBackedFileSystemItem extends
        AbstractFileSystemItem {

    private static final long serialVersionUID = 1L;

    /** Backing {@link DocumentModel} attributes */
    protected String repositoryName;

    protected String docId;

    protected String docPath;

    protected String docTitle;

    protected AbstractDocumentBackedFileSystemItem(String factoryName,
            DocumentModel doc) throws ClientException {
        this(factoryName, null, doc);
        CoreSession docSession = doc.getCoreSession();
        DocumentModel parentDoc = docSession.getParentDocument(doc.getRef());
        if (parentDoc == null) {
            parentId = null;
            path = '/' + id;
        } else {
            FileSystemItem parent = getFileSystemItemAdapterService().getFileSystemItem(
                    parentDoc, true);
            parentId = parent.getId();
            path = parent.getPath() + '/' + id;
        }
    }

    protected AbstractDocumentBackedFileSystemItem(String factoryName,
            String parentId, DocumentModel doc) throws ClientException {

        super(factoryName, doc.getCoreSession().getPrincipal());

        // Backing DocumentModel attributes
        repositoryName = doc.getRepositoryName();
        docId = doc.getId();
        docPath = doc.getPathAsString();
        docTitle = doc.getTitle();

        // FileSystemItem attributes
        id = computeId(docId);
        this.parentId = parentId;
        creator = (String) doc.getPropertyValue("dc:creator");
        creationDate = (Calendar) doc.getPropertyValue("dc:created");
        lastModificationDate = (Calendar) doc.getPropertyValue("dc:modified");
        CoreSession docSession = doc.getCoreSession();
        canRename = docSession.hasPermission(doc.getRef(),
                SecurityConstants.WRITE_PROPERTIES);
        canDelete = docSession.hasPermission(doc.getRef(),
                SecurityConstants.REMOVE)
                && docSession.hasPermission(doc.getParentRef(),
                        SecurityConstants.REMOVE_CHILDREN);

        String parentPath;
        if (parentId != null) {
            // TODO: provide an alternative constructor to directly pass the
            // parent item to avoid having to this unnecessary lookup
            FileSystemItemManager fsManager = Framework.getLocalService(FileSystemItemManager.class);
            Principal principal = doc.getCoreSession().getPrincipal();
            FileSystemItem parent = fsManager.getFileSystemItemById(parentId,
                    principal);
            if (parent == null) {
                throw new ClientException("Could not find FS item with id "
                        + parentId + " for user: " + principal.getName());
            }
            parentPath = parent.getPath();
        } else {
            DocumentModel parentDoc = docSession.getParentDocument(doc.getRef());
            if (parentDoc == null) {
                parentPath = "";
            } else {
                FileSystemItem parent = getFileSystemItemAdapterService().getFileSystemItem(
                        parentDoc, true);
                if (parent == null) {
                    parentPath = "";
                } else {
                    parentPath = parent.getPath();
                }
            }
        }
        path = parentPath + '/' + id;
    }

    protected AbstractDocumentBackedFileSystemItem() {
        // Needed for JSON deserialization
    }

    /*--------------------- FileSystemItem ---------------------*/
    public void delete() throws ClientException {
        List<DocumentModel> docs = new ArrayList<DocumentModel>();
        DocumentModel doc = getDocument(getSession());
        docs.add(doc);
        getTrashService().trashDocuments(docs);
    }

    public boolean canMove(FolderItem dest) throws ClientException {
        // Check source doc deletion
        if (!canDelete) {
            return false;
        }
        // Check add children on destination doc
        AbstractDocumentBackedFileSystemItem docBackedDest = (AbstractDocumentBackedFileSystemItem) dest;<commit_msg>NXP-11205: Remove useless and costly code in FileSystemItem adaptation of a document passing a null parent id<commit_after>/*
 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     <NAME> <<EMAIL>>
 */
package org.nuxeo.drive.adapter.impl;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.nuxeo.drive.adapter.FileSystemItem;
import org.nuxeo.drive.adapter.FolderItem;
import org.nuxeo.drive.service.FileSystemItemManager;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.ClientRuntimeException;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.DocumentRef;
import org.nuxeo.ecm.core.api.IdRef;
import org.nuxeo.ecm.core.api.security.SecurityConstants;
import org.nuxeo.ecm.core.trash.TrashService;
import org.nuxeo.runtime.api.Framework;

/**
 * {@link DocumentModel} backed implementation of a {@link FileSystemItem}.
 *
 * @author <NAME>
 * @see DocumentBackedFileItem
 * @see DocumentBackedFolderItem
 */
public abstract class AbstractDocumentBackedFileSystemItem extends
        AbstractFileSystemItem {

    private static final long serialVersionUID = 1L;

    /** Backing {@link DocumentModel} attributes */
    protected String repositoryName;

    protected String docId;

    protected String docPath;

    protected String docTitle;

    protected AbstractDocumentBackedFileSystemItem(String factoryName,
            DocumentModel doc) throws ClientException {
        this(factoryName, null, doc);
        CoreSession docSession = doc.getCoreSession();
        DocumentModel parentDoc = docSession.getParentDocument(doc.getRef());
        if (parentDoc == null) {
            parentId = null;
            path = '/' + id;
        } else {
            FileSystemItem parent = getFileSystemItemAdapterService().getFileSystemItem(
                    parentDoc, true);
            parentId = parent.getId();
            path = parent.getPath() + '/' + id;
        }
    }

    protected AbstractDocumentBackedFileSystemItem(String factoryName,
            String parentId, DocumentModel doc) throws ClientException {

        super(factoryName, doc.getCoreSession().getPrincipal());

        // Backing DocumentModel attributes
        repositoryName = doc.getRepositoryName();
        docId = doc.getId();
        docPath = doc.getPathAsString();
        docTitle = doc.getTitle();

        // FileSystemItem attributes
        id = computeId(docId);
        this.parentId = parentId;
        creator = (String) doc.getPropertyValue("dc:creator");
        creationDate = (Calendar) doc.getPropertyValue("dc:created");
        lastModificationDate = (Calendar) doc.getPropertyValue("dc:modified");
        CoreSession docSession = doc.getCoreSession();
        canRename = docSession.hasPermission(doc.getRef(),
                SecurityConstants.WRITE_PROPERTIES);
        canDelete = docSession.hasPermission(doc.getRef(),
                SecurityConstants.REMOVE)
                && docSession.hasPermission(doc.getParentRef(),
                        SecurityConstants.REMOVE_CHILDREN);

        String parentPath;
        if (parentId != null) {
            // TODO: provide an alternative constructor to directly pass the
            // parent item to avoid having to this unnecessary lookup
            FileSystemItemManager fsManager = Framework.getLocalService(FileSystemItemManager.class);
            Principal principal = doc.getCoreSession().getPrincipal();
            FileSystemItem parent = fsManager.getFileSystemItemById(parentId,
                    principal);
            if (parent == null) {
                throw new ClientException("Could not find FS item with id "
                        + parentId + " for user: " + principal.getName());
            }
            parentPath = parent.getPath();
        } else {
            parentPath = "";
        }
        path = parentPath + '/' + id;
    }

    protected AbstractDocumentBackedFileSystemItem() {
        // Needed for JSON deserialization
    }

    /*--------------------- FileSystemItem ---------------------*/
    public void delete() throws ClientException {
        List<DocumentModel> docs = new ArrayList<DocumentModel>();
        DocumentModel doc = getDocument(getSession());
        docs.add(doc);
        getTrashService().trashDocuments(docs);
    }

    public boolean canMove(FolderItem dest) throws ClientException {
        // Check source doc deletion
        if (!canDelete) {
            return false;
        }
        // Check add children on destination doc
        AbstractDocumentBackedFileSystemItem docBackedDest = (AbstractDocumentBackedFileSystemItem) dest;
{'fraction_non_alphanumeric': 0.09834425036390101, 'fraction_numerical': 0.0017285298398835517, 'mean_word_length': 2.640066225165563, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4050910', 'n_tokens_mistral': 2881, 'n_tokens_neox': 2772, 'n_words': 722}