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.security.Principal;
022import java.util.Map;
023
024import org.apache.commons.lang.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, boolean relaxSyncRootConstraint) {
104        return getFileSystemItem(doc, false, false, true);
105    }
106
107    @Override
108    public FileSystemItem getFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint,
109            boolean getLockInfo) {
110        return null;
111    }
112
113    @Override
114    public FileSystemItem getFileSystemItem(DocumentModel doc, FolderItem parentItem) {
115        return getFileSystemItem(doc, parentItem, false);
116    }
117
118    @Override
119    public FileSystemItem getFileSystemItem(DocumentModel doc, FolderItem parentItem, boolean includeDeleted) {
120        return getFileSystemItem(doc, parentItem, false, false);
121    }
122
123    @Override
124    public FileSystemItem getFileSystemItem(DocumentModel doc, FolderItem parentItem, boolean includeDeleted,
125            boolean relaxSyncRootConstraint) {
126        return getFileSystemItem(doc, parentItem, false, false, true);
127    }
128
129    @Override
130    public FileSystemItem getFileSystemItem(DocumentModel doc, FolderItem parentItem, boolean includeDeleted,
131            boolean relaxSyncRootConstraint, boolean getLockInfo) {
132        return null;
133    }
134
135    @Override
136    public boolean canHandleFileSystemItemId(String id) {
137        return (getName() + AbstractFileSystemItem.FILE_SYSTEM_ITEM_ID_SEPARATOR).equals(id);
138    }
139
140    @Override
141    public boolean exists(String id, Principal principal) {
142        if (!canHandleFileSystemItemId(id)) {
143            throw new UnsupportedOperationException(String.format(
144                    "Cannot check if a file system item exists for an id that cannot be handled from factory %s.",
145                    getName()));
146        }
147        return true;
148    }
149
150    @Override
151    public FileSystemItem getFileSystemItemById(String id, Principal principal) {
152        if (!canHandleFileSystemItemId(id)) {
153            throw new UnsupportedOperationException(String.format(
154                    "Cannot get the file system item for an id that cannot be handled from factory %s.", getName()));
155        }
156        return getVirtualFolderItem(principal);
157    }
158
159    @Override
160    public FileSystemItem getFileSystemItemById(String id, String parentId, Principal principal) {
161        return getFileSystemItemById(id, principal);
162    }
163
164    @Deprecated
165    @Override
166    public DocumentModel getDocumentByFileSystemId(String id, Principal principal) {
167        throw new UnsupportedOperationException(String.format(
168                "Cannot get document by file system item id from VirtualFolderItemFactory %s.", getName()));
169    }
170
171    @Override
172    public String getFolderName() {
173        return folderName;
174    }
175
176    @Override
177    public void setFolderName(String folderName) {
178        this.folderName = folderName;
179    }
180
181}