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