<?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\Folder;
use openkm\bean\ContentInfo;
use openkm\definition\BaseFolder;
use openkm\util\UriHelper;
use Httpful\Request;
use openkm\util\Status;
use openkm\util\TypeException;
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\util\FormatUtils;
use openkm\util\BeanHelper;
use openkm\bean\TemplateParams;

/**
 * FolderImpl
 *
 * @author sochoa
 */
class FolderImpl extends ClientImpl implements BaseFolder {

    /**
     * Create Folder
     * 
     * @param string $uuid uuid of the Folder
     * @param string $name name of the Folder
     * @return Folder Create a new folder and return as a result an object Folder.
     * @throws ConnectionErrorException 
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws ItemExistsException
     * @throws ExtensionException
     * @throws AutomationException
     * @throws UnknowException
     */
    public function createFolder($uuid, $name) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::FOLDER_POST_CREATE, ['uuid' => $uuid]));
            $client = Request::post($uri);
            $client->body($name);
            $response = $this->getClient($client);
            if ($response->code == Status::OK) {
                return $this->phpFolder($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 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("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    /**
     * Get the properties of the folder
     * @param string $uuid The uuid of the Folder
     * @return Folder $folder Return the folder properties
     * @throws ConnectionErrorException 
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws UnknowException
     */
    public function getFolderProperties($uuid) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::FOLDER_GET_PROPERTIES, ['uuid' => $uuid]));
            $client = Request::get($uri);
            $response = $this->getClient($client);
            if ($response->code == Status::OK) {
                return $this->phpFolder($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());
        }
    }

    /**
     * Delete Folder
     * @param string $uuid The uuid of the Folder
     * @throws ConnectionErrorException 
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws LockException
     * @throws UnknowException
     */
    public function deleteFolder($uuid) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::FOLDER_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 {
                        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());
        }
    }

    /**
     * Rename Folder
     * @param string $uuid The uuid of the Folder
     * @param string $newName The new name for the folder 
     * @return \openkm\bean\Folder 
     * @throws ConnectionErrorException 
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws UnknowException
     */
    public function renameFolder($uuid, $newName) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::FOLDER_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->phpFolder($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 folder into some folder or record.
     * @param string $fldId The uuid or path of the Folder
     * @param string $dstId The uuid or path of the Folder or Record
     * @throws ConnectionErrorException 
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws LockException
     * @throws UnknowException
     */
    public function moveFolder($uuid, $dstId) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::FOLDER_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 {
                        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 Folder Children
     * @param string $uuid The uuid or path of the Folder or Record node
     * @return array Return an array of all Folder their parent is fldId
     * @throws ConnectionErrorException 
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws UnknowException
     */
    public function getFolderChildren($uuid) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::FOLDER_GET_CHILDREN, ['uuid' => $uuid]));
            $client = Request::get($uri);
            $response = $this->getClient($client);
            if ($response->code == Status::OK) {
                $folders = [];
                foreach ($response->body as $folderXML) {
                    $folders[] = $this->phpFolderComplete($folderXML);
                }
                return $folders;
            } 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());
        }
    }

    /**
     * Is valid Folder
     * 
     * @param string $uuid the uuid or paht of the Folder
     * @return boolean Return a boolean that indicate if the node is a folder or not.
     * @throws ConnectionErrorException 
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws UnknowException
     */
    public function isValidFolder($uuid) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::FOLDER_GET_IS_VALID, ['uuid' => $uuid]));
            $client = Request::get($uri);
            $response = $this->getClientWithHTMLResponse($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 {
                        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 Folder Path
     * @param string $uuid The uuid of de Folder
     * @return string Convert folder UUID to folder path.
     * @throws ConnectionErrorException 
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws UnknowException
     */
    public function getFolderPath($uuid) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::FOLDER_GET_PATH, ['uuid' => $uuid]));
            $client = Request::get($uri);
            $response = $this->getClientWithHTMLResponse($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 {
                        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 folder into some folder or record.
     * Only the security grants are copyed to destination, the metadata, keywords etc... of the folder are not copyed. 
     * See "extendedFolderCopy" method for this feature.
     * 
     * @param string $uuid The uuid of the Folder
     * @param string $dstId The uuid of the Folder or Record
     * @param string $newName The new name for the Folder. Value is null, folder will preservate the same name.
     * @throws ConnectionErrorException 
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws ItemExistsException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws AutomationException
     * @throws UserQuotaExceededException
     * @throws IOException
     * @throws UnknowException
     */
    public function copyFolder($uuid, $dstId, $newName) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::FOLDER_POST_COPY, ['uuid' => $uuid, 'dstId' => $dstId]));
            $client = Request::post($uri);
            $params = [];
            $params['newName'] = $newName;
            $response = $this->getClientWithṔarams($client, $params);
            if ($response->code == Status::OK) {
                return $this->phpFolder($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::ITEM_EXISTS_EXCEPTION) {
                        throw new ItemExistsException($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::AUTOMATION_EXCEPTION) {
                        throw new AutomationException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::USER_QUOTA_EXCEEDED_EXCEPTION) {
                        throw new UserQuotaExceededException($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());
        }
    }

    /**
     * Copy a folder with associated data into some folder or record.
     * 
     * By default only the binary data and the security grants, the metadata, keywords etc... of the folder are not copyed.      
     * Additional:
     * 
     * > When category paramater is true the original values of the categories will be copied.
     * > When keywords paramater is true the original values of the keywords will be copied.
     * > When propertyGroups paramater is true the original values of the metadata groups will be copied.
     * > When notes paramater is true the original values of the notes will be copied.
     * > When wiki paramater is true the original values of the wiki will be copied.
     * 
     * @param string $uuid The uuid or path of the Folder
     * @param string $dstId The uuid or path of the Folder or Record
     * @param string $newName The new name for the Folder. Value is null, folder will preservate the same name.
     * @param boolean $categories
     * @param boolean $keywords
     * @param boolean $propertyGroups
     * @param boolean $notes
     * @param boolean $security     
     * @throws ConnectionErrorException 
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws ItemExistsException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws AutomationException
     * @throws UserQuotaExceededException
     * @throws IOException
     * @throws UnknowException
     */
    public function extendedFolderCopy($uuid, $dstId, $newName, $categories, $keywords, $propertyGroups, $notes, $security) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::FOLDER_POST_EXTENDED_COPY, ['uuid' => $uuid, 'dstId' => $dstId]));

            $params = [];
            $params['newName'] = $newName;
            $params['categories'] = $categories;
            $params['keywords'] = $keywords;
            $params['propertyGroups'] = $propertyGroups;
            $params['notes'] = $notes;
            $params['security'] = $security;

            $client = Request::post($uri);
            $response = $this->getClientWithṔarams($client, $params);
            if ($response->code == Status::OK) {
                return $this->phpFolder($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::ITEM_EXISTS_EXCEPTION) {
                        throw new ItemExistsException($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::AUTOMATION_EXCEPTION) {
                        throw new AutomationException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::USER_QUOTA_EXCEEDED_EXCEPTION) {
                        throw new UserQuotaExceededException($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());
        }
    }

    public function createFolderFromTemplate($uuid, $dstPath, $language, $categories, $keywords, $propertyGroups, $notes, $properties) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::FOLDER_POST_CREATE_FROM_TEMPLATE, ['uuid' => $uuid]));

            $templateParams = new TemplateParams();
            $templateParams->setDstPath($dstPath);
            $templateParams->setLanguage($language);
            $templateParams->setCategories($categories);
            $templateParams->setKeywords($keywords);
            $templateParams->setPropertyGroups($propertyGroups);
            $templateParams->setNotes($notes);
            $templateParams->setProperties(BeanHelper::copyToGroupPropertyList($properties));

            $client = Request::post($uri);
            $response = $this->getClientWithBody($client, $templateParams);
            if ($response->code == Status::OK) {
                return $this->phpFolder($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::ITEM_EXISTS_EXCEPTION) {
                        throw new ItemExistsException($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::AUTOMATION_EXCEPTION) {
                        throw new AutomationException($restException->getMessage());
                    } else if ($restException->getSimpleName() == TypeException::USER_QUOTA_EXCEEDED_EXCEPTION) {
                        throw new UserQuotaExceededException($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());
        }
    }

    /**
     * The ContentInfo object retrives information about:
     * > The number of folders into.
     * > The number of documents into.
     * > The number of records into.
     * > The number of mails into.
     * > The size in bytes of all objects into the folder.                    
     * 
     * @param string $uuid The uuid of the Folder
     * @return ContentInfo Return and object ContentInfo with information about folder.
     * @throws ConnectionErrorException 
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws UnknowException
     */
    public function getContentInfo($uuid) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::FOLDER_GET_CONTENT_INFO, ['uuid' => $uuid]));
            $client = Request::get($uri);
            $response = $this->getClient($client);
            if ($response->code == Status::OK) {
                return $this->phpContentInfo($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());
        }
    }

    /**
     * Folder is definitely removed from repository.
     * 
     * Usually you will purge folders into /okm:trash/userId - the personal trash user locations - but is possible to directly purge any folder from the whole repository.
     * 
     * When a folder is purged only will be able to be restored from a previously repository backup. The purge action remove the folder definitely from the repository.
     * 
     * @param string $uuid The uuid of the Folder
     * @throws ConnectionErrorException 
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws LockException
     * @throws UnknowException
     */
    public function purgeFolder($uuid) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::FOLDER_DEL_PURGE, ['uuid' => $uuid]));
            $client = Request::delete($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 {
                        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());
        }
    }

    /**
     * Set Style Folder
     * @param string $uuid The uuid of the Folder
     * @param long $styleId The id of the style
     * @throws ConnectionErrorException 
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws VersionException
     * @throws UnknowException
     */
    public function setStyle($uuid, $styleId) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::FOLDER_PUT_SET_STYLE, ['uuid' => $uuid]));
            $client = Request::put($uri);
            $params = [];
            $params['styleId'] = $styleId;
            $response = $this->getClientWithṔarams($client, $params);
            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::VERSION_EXCEPTION) {
                        throw new VersionException($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());
        }
    }

    /**
     * Create missing folders.
     * 
     * @param string $fldPath Path of the Folder
     * @throws ConnectionErrorException
     * @throws AccessDeniedException
     * @throws PathNotFoundException
     * @throws RepositoryException
     * @throws DatabaseException
     * @throws ItemExistsException
     * @throws ExtensionException
     * @throws AutomationException
     * @throws UnknowException
     */
    public function createMissingFolders($fldPath) {
        try {
            $uri = UriHelper::getUri($this->host, UriHelper::FOLDER_POST_CREATE_MISSING_FOLDERS);
            $client = Request::post($uri);
            $params = [];
            $params['fldPath'] = $fldPath;
            $response = $this->getClientWithṔarams($client, $params);
            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::ITEM_EXISTS_EXCEPTION) {
                        throw new ItemExistsException($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("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    public function setFolderDescription($uuid, $description) {
        try {
            $uri = UriHelper::getUri($this->host, FormatUtils::format(UriHelper::FOLDER_PUT_SET_DESCRIPTION, ['uuid' => $uuid]));
            $client = Request::put($uri);
            $params = [];
            $params['description'] = $description;
            $response = $this->getClientWithṔarams($client, $params);
            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::VERSION_EXCEPTION) {
                        throw new VersionException($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());
        }
    }

}

?>
