001/*
002 * (C) Copyright 2012-2018 Nuxeo (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.security.Principal;
022import java.util.Map;
023
024import org.apache.commons.lang3.StringUtils;
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.drive.adapter.FileSystemItem;
028import org.nuxeo.drive.adapter.FolderItem;
029import org.nuxeo.drive.adapter.impl.AbstractFileSystemItem;
030import org.nuxeo.drive.service.FileSystemItemFactory;
031import org.nuxeo.drive.service.VirtualFolderItemFactory;
032import org.nuxeo.ecm.core.api.DocumentModel;
033
034/**
035 * Base {@link FileSystemItemFactory} for a virtual {@link FolderItem}.
036 *
037 * @author Antoine Taillefer
038 */
039public abstract class AbstractVirtualFolderItemFactory implements VirtualFolderItemFactory {
040
041    private static final Log log = LogFactory.getLog(AbstractVirtualFolderItemFactory.class);
042
043    protected static final String FOLDER_NAME_PARAM = "folderName";
044
045    protected static final String DEFAULT_FOLDER_NAME = "Nuxeo Drive";
046
047    protected String name;
048
049    protected String folderName = DEFAULT_FOLDER_NAME;
050
051    @Override
052    public abstract FolderItem getVirtualFolderItem(Principal principal);
053
054    @Override
055    public String getName() {
056        return name;
057    }
058
059    @Override
060    public void setName(String name) {
061        this.name = name;
062    }
063
064    @Override
065    public void handleParameters(Map<String, String> parameters) {
066        // Look for the "folderName" parameter
067        String folderNameParam = parameters.get(FOLDER_NAME_PARAM);
068        if (!StringUtils.isEmpty(folderNameParam)) {
069            folderName = folderNameParam;
070        } else {
071            log.info(String.format(
072                    "Factory %s has no %s parameter, you can provide one in the factory contribution to avoid using the default value '%s'.",
073                    getName(), FOLDER_NAME_PARAM, DEFAULT_FOLDER_NAME));
074        }
075    }
076
077    @Override
078    public boolean isFileSystemItem(DocumentModel doc) {
079        return isFileSystemItem(doc, false);
080    }
081
082    @Override
083    public boolean isFileSystemItem(DocumentModel doc, boolean includeDeleted) {
084        return isFileSystemItem(doc, false, false);
085    }
086
087    @Override
088    public boolean isFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint) {
089        return false;
090    }
091
092    @Override
093    public FileSystemItem getFileSystemItem(DocumentModel doc) {
094        return getFileSystemItem(doc, false);
095    }
096
097    @Override
098    public FileSystemItem getFileSystemItem(DocumentModel doc, boolean includeDeleted) {
099        return getFileSystemItem(doc, false, false);
100    }
101
102    @Override
103    public FileSystemItem getFileSystemItem(DocumentModel doc, boolean includeDeleted,
104            boolean relaxSyncRootConstraint) {
105        return getFileSystemItem(doc, false, false, true);
106    }
107
108    @Override
109    public FileSystemItem getFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint,
110            boolean getLockInfo) {
111        return null;
112    }
113
114    @Override
115    public FileSystemItem getFileSystemItem(DocumentModel doc, FolderItem parentItem) {
116        return getFileSystemItem(doc, parentItem, false);
117    }
118
119    @Override
120    public FileSystemItem getFileSystemItem(DocumentModel doc, FolderItem parentItem, boolean includeDeleted) {
121        return getFileSystemItem(doc, parentItem, false, false);
122    }
123
124    @Override
125    public FileSystemItem getFileSystemItem(DocumentModel doc, FolderItem parentItem, boolean includeDeleted,
126            boolean relaxSyncRootConstraint) {
127        return getFileSystemItem(doc, parentItem, false, false, true);
128    }
129
130    @Override
131    public FileSystemItem getFileSystemItem(DocumentModel doc, FolderItem parentItem, boolean includeDeleted,
132            boolean relaxSyncRootConstraint, boolean getLockInfo) {
133        return null;
134    }
135
136    @Override
137    public boolean canHandleFileSystemItemId(String id) {
138        return (getName() + AbstractFileSystemItem.FILE_SYSTEM_ITEM_ID_SEPARATOR).equals(id);
139    }
140
141    @Override
142    public boolean exists(String id, Principal principal) {
143        if (!canHandleFileSystemItemId(id)) {
144            throw new UnsupportedOperationException(String.format(
145                    "Cannot check if a file system item exists for an id that cannot be handled from factory %s.",
146                    getName()));
147        }
148        return true;
149    }
150
151    @Override
152    public FileSystemItem getFileSystemItemById(String id, Principal principal) {
153        if (!canHandleFileSystemItemId(id)) {
154            throw new UnsupportedOperationException(String.format(
155                    "Cannot get the file system item for an id that cannot be handled from factory %s.", getName()));
156        }
157        return getVirtualFolderItem(principal);
158    }
159
160    @Override
161    public FileSystemItem getFileSystemItemById(String id, String parentId, Principal principal) {
162        return getFileSystemItemById(id, principal);
163    }
164
165    @Deprecated
166    @Override
167    public DocumentModel getDocumentByFileSystemId(String id, Principal principal) {
168        throw new UnsupportedOperationException(String.format(
169                "Cannot get document by file system item id from VirtualFolderItemFactory %s.", getName()));
170    }
171
172    @Override
173    public String getFolderName() {
174        return folderName;
175    }
176
177    @Override
178    public void setFolderName(String folderName) {
179        this.folderName = folderName;
180    }
181
182}