001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Antoine Taillefer <ataillefer@nuxeo.com>
018 */
019package org.nuxeo.drive.service.impl;
020
021import java.util.List;
022
023import org.apache.logging.log4j.LogManager;
024import org.apache.logging.log4j.Logger;
025import org.nuxeo.drive.adapter.FileItem;
026import org.nuxeo.drive.adapter.FileSystemItem;
027import org.nuxeo.drive.adapter.FolderItem;
028import org.nuxeo.drive.adapter.RootlessItemException;
029import org.nuxeo.drive.adapter.ScrollFileSystemItemList;
030import org.nuxeo.drive.service.FileSystemItemAdapterService;
031import org.nuxeo.drive.service.FileSystemItemManager;
032import org.nuxeo.ecm.core.api.Blob;
033import org.nuxeo.ecm.core.api.NuxeoException;
034import org.nuxeo.ecm.core.api.NuxeoPrincipal;
035import org.nuxeo.runtime.api.Framework;
036
037/**
038 * Default implementation of the {@link FileSystemItemManager}.
039 *
040 * @author Antoine Taillefer
041 */
042public class FileSystemItemManagerImpl implements FileSystemItemManager {
043
044    private static final Logger log = LogManager.getLogger(FileSystemItemManagerImpl.class);
045
046    /*------------- Read operations ----------------*/
047    @Override
048    public FolderItem getTopLevelFolder(NuxeoPrincipal principal) {
049        return getFileSystemItemAdapterService().getTopLevelFolderItemFactory().getTopLevelFolderItem(principal);
050    }
051
052    @Override
053    public boolean exists(String id, NuxeoPrincipal principal) {
054        return getFileSystemItemAdapterService().getFileSystemItemFactoryForId(id).exists(id, principal);
055    }
056
057    @Override
058    public FileSystemItem getFileSystemItemById(String id, NuxeoPrincipal principal) {
059        try {
060            return getFileSystemItemAdapterService().getFileSystemItemFactoryForId(id).getFileSystemItemById(id,
061                    principal);
062        } catch (RootlessItemException e) {
063            log.debug("RootlessItemException thrown while trying to get file system item with id {}, returning null.",
064                    id);
065            return null;
066        }
067    }
068
069    @Override
070    public FileSystemItem getFileSystemItemById(String id, String parentId, NuxeoPrincipal principal) {
071        try {
072            return getFileSystemItemAdapterService().getFileSystemItemFactoryForId(id).getFileSystemItemById(id,
073                    parentId, principal);
074        } catch (RootlessItemException e) {
075            log.debug(
076                    "RootlessItemException thrown while trying to get file system item with id {} and parent id {}, returning null.",
077                    id, parentId);
078            return null;
079        }
080    }
081
082    @Override
083    public List<FileSystemItem> getChildren(String id, NuxeoPrincipal principal) {
084        FileSystemItem fileSystemItem = getFileSystemItemById(id, principal);
085        if (fileSystemItem == null) {
086            throw new NuxeoException(String.format(
087                    "Cannot get the children of file system item with id %s because it doesn't exist.", id));
088        }
089        if (!(fileSystemItem instanceof FolderItem)) {
090            throw new NuxeoException(String.format(
091                    "Cannot get the children of file system item with id %s because it is not a folder.", id));
092        }
093        FolderItem folderItem = (FolderItem) fileSystemItem;
094        return folderItem.getChildren();
095    }
096
097    @Override
098    public ScrollFileSystemItemList scrollDescendants(String id, NuxeoPrincipal principal, String scrollId,
099            int batchSize, long keepAlive) {
100        FileSystemItem fileSystemItem = getFileSystemItemById(id, principal);
101        if (fileSystemItem == null) {
102            throw new NuxeoException(String.format(
103                    "Cannot get the descendants of file system item with id %s because it doesn't exist.", id));
104        }
105        if (!(fileSystemItem instanceof FolderItem)) {
106            throw new NuxeoException(String.format(
107                    "Cannot get the descendants of file system item with id %s because it is not a folder.", id));
108        }
109        FolderItem folderItem = (FolderItem) fileSystemItem;
110        return folderItem.scrollDescendants(scrollId, batchSize, keepAlive);
111    }
112
113    @Override
114    public boolean canMove(String srcId, String destId, NuxeoPrincipal principal) {
115        FileSystemItem srcFsItem = getFileSystemItemById(srcId, principal);
116        if (srcFsItem == null) {
117            return false;
118        }
119        FileSystemItem destFsItem = getFileSystemItemById(destId, principal);
120        if (!(destFsItem instanceof FolderItem)) {
121            return false;
122        }
123        return srcFsItem.canMove((FolderItem) destFsItem);
124    }
125
126    /*------------- Write operations ---------------*/
127    @Override
128    public FolderItem createFolder(String parentId, String name, NuxeoPrincipal principal, boolean overwrite) {
129        FileSystemItem parentFsItem = getFileSystemItemById(parentId, principal);
130        if (parentFsItem == null) {
131            throw new NuxeoException(String.format(
132                    "Cannot create a folder in file system item with id %s because it doesn't exist.", parentId));
133        }
134        if (!(parentFsItem instanceof FolderItem)) {
135            throw new NuxeoException(String.format(
136                    "Cannot create a folder in file system item with id %s because it is not a folder but is: %s",
137                    parentId, parentFsItem));
138        }
139        FolderItem parentFolder = (FolderItem) parentFsItem;
140        return parentFolder.createFolder(name, overwrite);
141    }
142
143    @Override
144    public FileItem createFile(String parentId, Blob blob, NuxeoPrincipal principal, boolean overwrite) {
145        FileSystemItem parentFsItem = getFileSystemItemById(parentId, principal);
146        if (parentFsItem == null) {
147            throw new NuxeoException(String.format(
148                    "Cannot create a file in file system item with id %s because it doesn't exist.", parentId));
149        }
150        if (!(parentFsItem instanceof FolderItem)) {
151            throw new NuxeoException(String.format(
152                    "Cannot create a file in file system item with id %s because it is not a folder but is: %s",
153                    parentId, parentFsItem));
154        }
155        FolderItem parentFolder = (FolderItem) parentFsItem;
156        return parentFolder.createFile(blob, overwrite);
157    }
158
159    @Override
160    public FileItem updateFile(String id, Blob blob, NuxeoPrincipal principal) {
161        FileSystemItem fsItem = getFileSystemItemById(id, principal);
162        return updateFile(fsItem, blob);
163    }
164
165    @Override
166    public FileItem updateFile(String id, String parentId, Blob blob, NuxeoPrincipal principal) {
167        FileSystemItem fsItem = getFileSystemItemById(id, parentId, principal);
168        return updateFile(fsItem, blob);
169    }
170
171    @Override
172    public void delete(String id, NuxeoPrincipal principal) {
173        FileSystemItem fsItem = getFileSystemItemById(id, principal);
174        delete(fsItem);
175    }
176
177    @Override
178    public void delete(String id, String parentId, NuxeoPrincipal principal) {
179        FileSystemItem fsItem = getFileSystemItemById(id, parentId, principal);
180        delete(fsItem);
181    }
182
183    @Override
184    public FileSystemItem rename(String id, String name, NuxeoPrincipal principal) {
185        FileSystemItem fsItem = getFileSystemItemById(id, principal);
186        if (fsItem == null) {
187            throw new NuxeoException(
188                    String.format("Cannot rename file system item with id %s because it doesn't exist.", id));
189        }
190        fsItem.rename(name);
191        return fsItem;
192    }
193
194    @Override
195    public FileSystemItem move(String srcId, String destId, NuxeoPrincipal principal) {
196        FileSystemItem srcFsItem = getFileSystemItemById(srcId, principal);
197        if (srcFsItem == null) {
198            throw new NuxeoException(
199                    String.format("Cannot move file system item with id %s because it doesn't exist.", srcId));
200        }
201        FileSystemItem destFsItem = getFileSystemItemById(destId, principal);
202        if (destFsItem == null) {
203            throw new NuxeoException(String.format(
204                    "Cannot move a file system item to file system item with id %s because it doesn't exist.", destId));
205        }
206        if (!(destFsItem instanceof FolderItem)) {
207            throw new NuxeoException(String.format(
208                    "Cannot move a file system item to file system item with id %s because it is not a folder.",
209                    destId));
210        }
211        return srcFsItem.move((FolderItem) destFsItem);
212    }
213
214    /*------------- Protected ---------------*/
215    protected FileSystemItemAdapterService getFileSystemItemAdapterService() {
216        return Framework.getService(FileSystemItemAdapterService.class);
217    }
218
219    protected FileItem updateFile(FileSystemItem fsItem, Blob blob) {
220        if (fsItem == null) {
221            throw new NuxeoException("Cannot update the content of file system item because it doesn't exist.");
222        }
223        if (!(fsItem instanceof FileItem)) {
224            throw new NuxeoException(
225                    String.format("Cannot update the content of file system item with id %s because it is not a file.",
226                            fsItem.getId()));
227        }
228        FileItem file = (FileItem) fsItem;
229        file.setBlob(blob);
230        return file;
231    }
232
233    protected void delete(FileSystemItem fsItem) {
234        if (fsItem == null) {
235            throw new NuxeoException("Cannot delete file system item because it doesn't exist.");
236        }
237        fsItem.delete();
238    }
239
240}