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.util.Map;
022
023import org.apache.commons.lang3.StringUtils;
024import org.apache.logging.log4j.LogManager;
025import org.apache.logging.log4j.Logger;
026import org.nuxeo.drive.adapter.FileSystemItem;
027import org.nuxeo.drive.adapter.FolderItem;
028import org.nuxeo.drive.adapter.impl.AbstractFileSystemItem;
029import org.nuxeo.drive.service.FileSystemItemFactory;
030import org.nuxeo.drive.service.VirtualFolderItemFactory;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.NuxeoPrincipal;
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 Logger log = LogManager.getLogger(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 String getName() {
053        return name;
054    }
055
056    @Override
057    public void setName(String name) {
058        this.name = name;
059    }
060
061    @Override
062    public void handleParameters(Map<String, String> parameters) {
063        // Look for the "folderName" parameter
064        String folderNameParam = parameters.get(FOLDER_NAME_PARAM);
065        if (!StringUtils.isEmpty(folderNameParam)) {
066            folderName = folderNameParam;
067        } else {
068            log.info(
069                    "Factory {} has no {} parameter, you can provide one in the factory contribution to avoid using the default value '{}'.",
070                    this::getName, () -> FOLDER_NAME_PARAM, () -> DEFAULT_FOLDER_NAME);
071        }
072    }
073
074    @Override
075    public boolean isFileSystemItem(DocumentModel doc) {
076        return isFileSystemItem(doc, false);
077    }
078
079    @Override
080    public boolean isFileSystemItem(DocumentModel doc, boolean includeDeleted) {
081        return isFileSystemItem(doc, false, false);
082    }
083
084    @Override
085    public boolean isFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint) {
086        return false;
087    }
088
089    @Override
090    public FileSystemItem getFileSystemItem(DocumentModel doc) {
091        return getFileSystemItem(doc, false);
092    }
093
094    @Override
095    public FileSystemItem getFileSystemItem(DocumentModel doc, boolean includeDeleted) {
096        return getFileSystemItem(doc, false, false);
097    }
098
099    @Override
100    public FileSystemItem getFileSystemItem(DocumentModel doc, boolean includeDeleted,
101            boolean relaxSyncRootConstraint) {
102        return getFileSystemItem(doc, false, false, true);
103    }
104
105    @Override
106    public FileSystemItem getFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint,
107            boolean getLockInfo) {
108        return null;
109    }
110
111    @Override
112    public FileSystemItem getFileSystemItem(DocumentModel doc, FolderItem parentItem) {
113        return getFileSystemItem(doc, parentItem, false);
114    }
115
116    @Override
117    public FileSystemItem getFileSystemItem(DocumentModel doc, FolderItem parentItem, boolean includeDeleted) {
118        return getFileSystemItem(doc, parentItem, false, false);
119    }
120
121    @Override
122    public FileSystemItem getFileSystemItem(DocumentModel doc, FolderItem parentItem, boolean includeDeleted,
123            boolean relaxSyncRootConstraint) {
124        return getFileSystemItem(doc, parentItem, false, false, true);
125    }
126
127    @Override
128    public FileSystemItem getFileSystemItem(DocumentModel doc, FolderItem parentItem, boolean includeDeleted,
129            boolean relaxSyncRootConstraint, boolean getLockInfo) {
130        return null;
131    }
132
133    @Override
134    public boolean canHandleFileSystemItemId(String id) {
135        return (getName() + AbstractFileSystemItem.FILE_SYSTEM_ITEM_ID_SEPARATOR).equals(id);
136    }
137
138    @Override
139    public boolean exists(String id, NuxeoPrincipal principal) {
140        if (!canHandleFileSystemItemId(id)) {
141            throw new UnsupportedOperationException(String.format(
142                    "Cannot check if a file system item exists for an id that cannot be handled from factory %s.",
143                    getName()));
144        }
145        return true;
146    }
147
148    @Override
149    public FileSystemItem getFileSystemItemById(String id, NuxeoPrincipal principal) {
150        if (!canHandleFileSystemItemId(id)) {
151            throw new UnsupportedOperationException(String.format(
152                    "Cannot get the file system item for an id that cannot be handled from factory %s.", getName()));
153        }
154        return getVirtualFolderItem(principal);
155    }
156
157    @Override
158    public FileSystemItem getFileSystemItemById(String id, String parentId, NuxeoPrincipal principal) {
159        return getFileSystemItemById(id, principal);
160    }
161
162    @Override
163    public String getFolderName() {
164        return folderName;
165    }
166
167    @Override
168    public void setFolderName(String folderName) {
169        this.folderName = folderName;
170    }
171
172}