<?php

/**
 * OpenKM, Open Knowledge Management System S.L.  (http://www.openkm.com)
 * Copyright (c) 2006-2018
 *
 * No bytes were intentionally harmed during the development of this application.
 *
 * This program is commercial software; you can use it under the terms of the
 * EULA - OpenKM SDK End User License Agreement as published by OpenKM Knowledge Management System S.L.
 *
 * 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
 * EULA - OpenKM SDK End User License Agreement for more details:
 * http://docs.openkm.com/kcenter/view/licenses/eula-openkm-sdk-end-user-license-agreement.html
 */

namespace openkm\impl;

use openkm\bean\Document;
use openkm\bean\Version;
use openkm\bean\LockInfo;
use openkm\definition\BaseDocument;
use openkm\util\UriHelper;
use Httpful\Request;
use Httpful\Mime;
use openkm\util\Status;
use openkm\util\BeanHelper;
use openkm\util\TypeException;
use openkm\exception\UnsupportMimeTypeException;
use openkm\exception\FileSizeExceededException;
use openkm\exception\VirusDetectedException;
use openkm\exception\AccessDeniedException;
use openkm\exception\PathNotFoundException;
use openkm\exception\RepositoryException;
use openkm\exception\DatabaseException;
use Httpful\Exception\ConnectionErrorException;
use openkm\exception\UnknowException;
use openkm\exception\ItemExistsException;
use openkm\exception\ExtensionException;
use openkm\exception\AutomationException;
use openkm\exception\LockException;
use openkm\exception\UserQuotaExceededException;
use openkm\exception\IOException;
use openkm\exception\VersionException;
use openkm\exception\ConversionException;
use openkm\exception\PrincipalAdapterException;
use openkm\exception\DocumentException;
use openkm\exception\TemplateException;
use openkm\exception\DocumentTemplateException;
use openkm\exception\ParseException;
use openkm\exception\NoSuchGroupException;
use openkm\exception\NoSuchPropertyException;
use openkm\exception\InterruptedException;
use openkm\util\FormatUtils;

/**
 * DocumentImpl
 *
 * @author sochoa
 */
class DocumentImpl extends ClientImpl implements BaseDocument {

    /**
     * Create a new document
     * 
     * @param string $uuid The destination uuid of the Folder o Record
     * @param string $name The name document
     * @param string $content Recommend using file_get_contents — Reads entire file into a string
     * @return Document Return as a result an object Document with the properties of the created document.
     * @throws ConnectionErrorException when unable to parse or communicate with the server
     * @throws AccessDeniedException
     * @throws IOException
     * @throws UnsupportMimeTypeException
     * @throws FileSizeExceededException
     * @throws UserQuotaExceededException
     * @throws VirusDetectedException
     * @throws ItemExistsException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws ExtensionException
     * @throws AutomationException
     * @throws UnknowException
     */
    public function createDocument($uuid, $name, $content) {

        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::DOCUMENT_POST_CREATE, ['uuid' => $uuid]));
            $params = [];

            $param1 = [];
            $param1['name'] = 'nodeClass';
            $param1['body'] = 0;
            $params[] = $param1;

            $files = [];
            $file = [];
            $file['name'] = 'content';
            $file['body'] = $content;
            $file['fileName'] = $name;
            $files[] = $file;

            $body = [];
            $body['params'] = $params;
            $body['files'] = $files;

            $client = Request::post($uri);
            $client->setCurlPostFile(true);
            $client->sends(Mime::UPLOAD);
            $client->body($body);
            $client->addHeader('Authorization', $this->authorizationToken);
            $client->expects(Mime::JSON);

            $response = $client->send();
            if ($response->code == Status::OK) {
                return $this->phpDocument($response->body);
            } else {
                $restException = new \openkm\bean\RestException();
                $restException = $this->phpRestException($response->body);
                if ($restException->getSimpleName() != '') {
                    if ($restException->getSimpleName() == TypeException::IO_EXCEPTION) {
                        throw new IOException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::UNSUPPORTED_MIME_TYPE_EXCEPTION) {
                        throw new UnsupportMimeTypeException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::FILE_SIZE_EXCEEDED_EXCEPTION) {
                        throw new FileSizeExceededException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::USER_QUOTA_EXCEEDED_EXCEPTION) {
                        throw new UserQuotaExceededException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::VIRUS_DETECTED_EXCEPTION) {
                        throw new VirusDetectedException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::ITEM_EXISTS_EXCEPTION) {
                        throw new ItemExistsException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::PATH_NOT_FOUND_EXCEPTION) {
                        throw new PathNotFoundException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::REPOSITORY_EXCEPTION) {
                        throw new RepositoryException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::DATABASE_EXCEPTION) {
                        throw new DatabaseException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::EXTENSION_EXCEPTION) {
                        throw new ExtensionException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::AUTOMATION_EXCEPTION) {
                        throw new AutomationException($restException->getMessage());
                    } else {
                        throw new UnknowException($restException->getMessage());
                    }
                } else {
                    throw new UnknowException($restException->getMessage());
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    /**
     * Delete a document.
     * 
     * When a document is deleted is automatically moved to /okm:trash/userId folder.
     * 
     * @param string $uuid The uuid or path of the Document
     * @throws ConnectionErrorException
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws LockException
     * @throws ExtensionException
     * @throws AutomationException
     * @throws UnknowException
     */
    public function deleteDocument($uuid) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::DOCUMENT_PUT_DELETE, ['uuid' => $uuid]));
            $client = Request::put($uri);
            $response = $this->getClient($client);
            if ($response->code != Status::OK) {
                $restException = new \openkm\bean\RestException();
                $restException = $this->phpRestException($response->body);
                if ($restException->getSimpleName() != '') {
                    if ($restException->getSimpleName() == TypeException::PATH_NOT_FOUND_EXCEPTION) {
                        throw new PathNotFoundException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::REPOSITORY_EXCEPTION) {
                        throw new RepositoryException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::DATABASE_EXCEPTION) {
                        throw new DatabaseException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::LOCK_EXCEPTION) {
                        throw new LockException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::EXTENSION_EXCEPTION) {
                        throw new ExtensionException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::AUTOMATION_EXCEPTION) {
                        throw new AutomationException($restException->getMessage());
                    } else {
                        throw new UnknowException($error . ': ' . $msg);
                    }
                } else {
                    throw new UnknowException("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    /**
     * Get document properties
     * 
     * @param string $uuid The uuid or path of the Document
     * @return Document Return the document properties.
     * @throws ConnectionErrorException 
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws UnknowException
     */
    public function getDocumentProperties($uuid) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::DOCUMENT_GET_PROPERTIES, ['uuid' => $uuid]));
            $client = Request::get($uri);
            $response = $this->getClient($client);
            if ($response->code == Status::OK) {
                return $this->phpDocument($response->body);
            } else {
                $restException = new \openkm\bean\RestException();
                $restException = $this->phpRestException($response->body);
                if ($restException->getSimpleName() != '') {
                    if ($restException->getSimpleName() == TypeException::PATH_NOT_FOUND_EXCEPTION) {
                        throw new PathNotFoundException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::REPOSITORY_EXCEPTION) {
                        throw new RepositoryException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::DATABASE_EXCEPTION) {
                        throw new DatabaseException($restException->getMessage());
                    } else {
                        throw new UnknowException($restException->getMessage());
                    }
                } else {
                    throw new UnknowException("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    /**
     * Get content
     * 
     * @param string $uuid The uuid or path of the Document
     * @return string Retrieve document content - binary data - of the actual document version
     * @throws ConnectionErrorException 
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws UnknowException
     */
    public function getContent($uuid) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::DOCUMENT_GET_CONTENT, ['uuid' => $uuid]));
            $client = Request::get($uri);
            $response = $this->getClientWithUploadResponse($client);
            if ($response->code == Status::OK) {
                return $response->body;
            } else {
                $restException = new \openkm\bean\RestException();
                $restException = $this->phpRestException(json_decode($response->body));
                if ($restException->getSimpleName() != '') {
                    if ($restException->getSimpleName() == TypeException::PATH_NOT_FOUND_EXCEPTION) {
                        throw new PathNotFoundException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::REPOSITORY_EXCEPTION) {
                        throw new RepositoryException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::DATABASE_EXCEPTION) {
                        throw new DatabaseException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::IO_EXCEPTION) {
                        throw new IOException($restException->getMessage());
                    } else {
                        throw new UnknowException($restException->getMessage());
                    }
                } else {
                    throw new UnknowException("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    /**
     * Get content by version
     * 
     * @param string $uuid The uuid or path of the Document
     * @param string $versionId The number version of the document
     * @return string Retrieve document content - binary data - of the actual document version
     * @throws ConnectionErrorException
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws IOException
     * @throws UnknowException
     */
    public function getContentByVersion($uuid, $versionId) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::DOCUMENT_GET_CONTENT_BY_VERSION, ['uuid' => $uuid, 'versionId' => $versionId]));
            $client = Request::get($uri);
            $response = $this->getClientWithUploadResponse($client);
            if ($response->code == Status::OK) {
                return $response->body;
            } else {
                $restException = new \openkm\bean\RestException();
                $restException = $this->phpRestException($response->body);
                if ($restException->getSimpleName() != '') {
                    if ($restException->getSimpleName() == TypeException::PATH_NOT_FOUND_EXCEPTION) {
                        throw new PathNotFoundException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::REPOSITORY_EXCEPTION) {
                        throw new RepositoryException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::DATABASE_EXCEPTION) {
                        throw new DatabaseException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::IO_EXCEPTION) {
                        throw new IOException($restException->getMessage());
                    } else {
                        throw new UnknowException($restException->getMessage());
                    }
                } else {
                    throw new UnknowException("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    /**
     * Get extracted text
     * 
     * @param string $uuid The uuid or path of the Document
     * @return string Return an String with the extracted text by text extractor process.
     * @throws ConnectionErrorException
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws IOException
     * @throws UnknowException
     */
    public function getExtractedText($uuid) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::DOCUMENT_GET_EXTRACTED_TEXT, ['uuid' => $uuid]));
            $client = Request::get($uri);
            $response = $this->getClientWithPlainResponse($client);
            if ($response->code == Status::OK) {
                return (string) $response->body;
            } else {
                $restException = new \openkm\bean\RestException();
                $restException = $this->phpRestException($response->body);
                if ($restException->getSimpleName() != '') {
                    if ($restException->getSimpleName() == TypeException::PATH_NOT_FOUND_EXCEPTION) {
                        throw new PathNotFoundException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::REPOSITORY_EXCEPTION) {
                        throw new RepositoryException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::DATABASE_EXCEPTION) {
                        throw new DatabaseException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::IO_EXCEPTION) {
                        throw new IOException($restException->getMessage());
                    } else {
                        throw new UnknowException($restException->getMessage());
                    }
                } else {
                    throw new UnknowException("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    /**
     * Get Thumbnail
     * 
     * @param string $uuid The uuid or path of the Document     
     * Each thumbnail type has its own image dimensions.
     * 
     * @return string Return thumbnail image data.
     * @throws ConnectionErrorException
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws ConversionException
     * @throws IOException
     * @throws AutomationException
     * @throws UnknowException
     */
    public function getThumbnail($uuid) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::DOCUMENT_GET_THUMBNAIL, ['uuid' => $uuid]));
            $client = Request::get($uri);
            $response = $this->getClientWithUploadResponse($client);
            if ($response->code == Status::OK) {
                return $response->body;
            } else {
                $restException = new \openkm\bean\RestException();
                $restException = $this->phpRestException($response->body);
                if ($restException->getSimpleName() != '') {
                    if ($restException->getSimpleName() == TypeException::PATH_NOT_FOUND_EXCEPTION) {
                        throw new PathNotFoundException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::REPOSITORY_EXCEPTION) {
                        throw new RepositoryException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::DATABASE_EXCEPTION) {
                        throw new DatabaseException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::CONVERSION_EXCEPTION) {
                        throw new ConversionException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::IO_EXCEPTION) {
                        throw new IOException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::AUTOMATION_EXCEPTION) {
                        throw new AutomationException($restException->getMessage());
                    } else {
                        throw new UnknowException($restException->getMessage());
                    }
                } else {
                    throw new UnknowException("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    /**
     * Get Document children
     * 
     * @param string $uuid The uuid or path of the Folder or a record node.
     * @return array Return a list of all documents which their parent is fldId.
     * @throws ConnectionErrorException 
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws UnknowException
     */
    public function getDocumentChildren($uuid) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::DOCUMENT_GET_CHILDREN, ['uuid' => $uuid]));
            $client = Request::get($uri);
            $response = $this->getClient($client);
            if ($response->code == Status::OK) {
                $documents = [];
                foreach ($response->body as $documentJSON) {
                    $documents[] = $this->phpDocument($documentJSON);
                }
                return $documents;
            } else {
                $restException = new \openkm\bean\RestException();
                $restException = $this->phpRestException($response->body);
                if ($restException->getSimpleName() != '') {
                    if ($restException->getSimpleName() == TypeException::PATH_NOT_FOUND_EXCEPTION) {
                        throw new PathNotFoundException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::REPOSITORY_EXCEPTION) {
                        throw new RepositoryException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::DATABASE_EXCEPTION) {
                        throw new DatabaseException($restException->getMessage());
                    } else {
                        throw new UnknowException($restException->getMessage());
                    }
                } else {
                    throw new UnknowException("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    /**
     * Change the name of a document.
     * 
     * @param string $uuid The uuid or path of the Document
     * @param string $newName The new name for the Document
     * @return Document Returns the Document
     * @throws ConnectionErrorException when unable to parse or communicate with the server
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws ItemExistsException
     * @throws UnknowException
     */
    public function renameDocument($uuid, $newName) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::DOCUMENT_PUT_RENAME, ['uuid' => $uuid]));
            $client = Request::put($uri);

            $params = [];
            $params['newName'] = $newName;
            $response = $this->getClientWithṔarams($client, $params);
            if ($response->code == Status::OK) {
                return $this->phpDocument($response->body);
            } else {
                $restException = new \openkm\bean\RestException();
                $restException = $this->phpRestException($response->body);
                if ($restException->getSimpleName() != '') {
                    if ($restException->getSimpleName() == TypeException::PATH_NOT_FOUND_EXCEPTION) {
                        throw new PathNotFoundException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::REPOSITORY_EXCEPTION) {
                        throw new RepositoryException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::DATABASE_EXCEPTION) {
                        throw new DatabaseException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::ITEM_EXISTS_EXCEPTION) {
                        throw new ItemExistsException($restException->getMessage());
                    } else {
                        throw new UnknowException($restException->getMessage());
                    }
                } else {
                    throw new UnknowException("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    /**
     * Move document into some folder or record.
     * 
     * @param string $uuid The uuid or path of the Document
     * @param string $dstId The uuid or path of the Folder or Record
     * @throws ConnectionErrorException when unable to parse or communicate with the server
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws LockException
     * @throws ExtensionException
     * @throws ItemExistsException
     * @throws AutomationException
     * @throws UnknowException
     */
    public function moveDocument($uuid, $dstId) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::DOCUMENT_PUT_MOVE, ['uuid' => $uuid, 'dstId' => $dstId]));
            $client = Request::put($uri);
            $response = $this->getClient($client);
            if ($response->code != Status::OK) {
                $restException = new \openkm\bean\RestException();
                $restException = $this->phpRestException($response->body);
                if ($restException->getSimpleName() != '') {
                    if ($restException->getSimpleName() == TypeException::PATH_NOT_FOUND_EXCEPTION) {
                        throw new PathNotFoundException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::REPOSITORY_EXCEPTION) {
                        throw new RepositoryException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::DATABASE_EXCEPTION) {
                        throw new DatabaseException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::LOCK_EXCEPTION) {
                        throw new LockException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::EXTENSION_EXCEPTION) {
                        throw new ExtensionException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::ITEM_EXISTS_EXCEPTION) {
                        throw new ItemExistsException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::AUTOMATION_EXCEPTION) {
                        throw new AutomationException($restException->getMessage());
                    } else {
                        throw new UnknowException($restException->getMessage());
                    }
                } else {
                    throw new UnknowException("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    /**
     * Copy a document into some folder or record.
     * Only the binary data and the security grants are copyed to destionation, the metadata, keywords, etc. of the document are not copyed.See "extendedDocumentCopy" method for this feature.
     * 
     * @param string $uuid The uuid or path of the Document
     * @param string $dstId The uuid or path of the Folder or Record
     * @param string $newName The new name for the Document. Value is null, document will preservate the same name.
     * @throws ConnectionErrorException when unable to parse or communicate with the server
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws ExtensionException
     * @throws ItemExistsException
     * @throws AutomationException
     * @throws IOException
     * @throws UnknowException
     */
    public function copyDocument($uuid, $dstId, $newName) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::DOCUMENT_POST_COPY, ['uuid' => $uuid, 'dstId' => $dstId]));            
            $client = Request::post($uri);
            
            $params = [];
            $params['newName'] = $newName;           
            $response = $this->getClientWithṔarams($client, $params);
            echo '<pre>';
            var_dump($response->body);
            if ($response->code != Status::OK) {
                return $this->phpDocument($response->body);
            } else {
                $restException = new \openkm\bean\RestException();
                $restException = $this->phpRestException($response->body);
                if ($restException->getSimpleName() != '') {
                    if ($restException->getSimpleName() == TypeException::PATH_NOT_FOUND_EXCEPTION) {
                        throw new PathNotFoundException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::REPOSITORY_EXCEPTION) {
                        throw new RepositoryException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::DATABASE_EXCEPTION) {
                        throw new DatabaseException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::EXTENSION_EXCEPTION) {
                        throw new ExtensionException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::ITEM_EXISTS_EXCEPTION) {
                        throw new ItemExistsException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::AUTOMATION_EXCEPTION) {
                        throw new AutomationException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::IO_EXCEPTION) {
                        throw new IOException($restException->getMessage());
                    } else {
                        throw new UnknowException($restException->getMessage());
                    }
                } else {
                    throw new UnknowException("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    /**
     * Is valid Document
     * 
     * @param string $uuid The uuid or path of the Document
     * @return bool Return a boolean that indicate if the node is a document or not.
     * @throws ConnectionErrorException
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws UnknowException
     */
    public function isValidDocument($uuid) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::DOCUMENT_GET_IS_VALID, ['uuid' => $uuid]));
            $client = Request::get($uri);
            $response = $this->getClientWithHTMLResponse($client);
            if ($response->code == Status::OK) {
                if ($response->body == 'true') {
                    return true;
                } else {
                    return false;
                }
            } else {
                $restException = new \openkm\bean\RestException();
                $restException = $this->phpRestException(json_decode($response->body));
                if ($restException->getSimpleName() != '') {
                    if ($restException->getSimpleName() == TypeException::PATH_NOT_FOUND_EXCEPTION) {
                        throw new PathNotFoundException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::ACCESS_DENIED_EXCEPTION) {
                        throw new AccessDeniedException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::REPOSITORY_EXCEPTION) {
                        throw new RepositoryException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::DATABASE_EXCEPTION) {
                        throw new DatabaseException($restException->getMessage());
                    } else {
                        throw new UnknowException($restException->getMessage());
                    }
                } else {
                    throw new UnknowException("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }
}

?>
