001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Antoine Taillefer <ataillefer@nuxeo.com>
016 */
017package org.nuxeo.drive.service.impl;
018
019import java.security.Principal;
020import java.util.Map;
021
022import org.apache.commons.lang.StringUtils;
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.drive.adapter.FileSystemItem;
026import org.nuxeo.drive.adapter.FolderItem;
027import org.nuxeo.drive.adapter.impl.AbstractFileSystemItem;
028import org.nuxeo.drive.service.FileSystemItemFactory;
029import org.nuxeo.drive.service.VirtualFolderItemFactory;
030import org.nuxeo.ecm.core.api.DocumentModel;
031
032/**
033 * Base {@link FileSystemItemFactory} for a virtual {@link FolderItem}.
034 *
035 * @author Antoine Taillefer
036 */
037public abstract class AbstractVirtualFolderItemFactory implements VirtualFolderItemFactory {
038
039    private static final Log log = LogFactory.getLog(AbstractVirtualFolderItemFactory.class);
040
041    protected static final String FOLDER_NAME_PARAM = "folderName";
042
043    protected static final String DEFAULT_FOLDER_NAME = "Nuxeo Drive";
044
045    protected String name;
046
047    protected String folderName = DEFAULT_FOLDER_NAME;
048
049    @Override
050    public abstract FolderItem getVirtualFolderItem(Principal principal);
051
052    @Override
053    public String getName() {
054        return name;
055    }
056
057    @Override
058    public void setName(String name) {
059        this.name = name;
060    }
061
062    @Override
063    public void handleParameters(Map<String, String> parameters) {
064        // Look for the "folderName" parameter
065        String folderNameParam = parameters.get(FOLDER_NAME_PARAM);
066        if (!StringUtils.isEmpty(folderNameParam)) {
067            folderName = folderNameParam;
068        } else {
069            log.info(String.format(
070                    "Factory %s has no %s parameter, you can provide one in the factory contribution to avoid using the default value '%s'.",
071                    getName(), FOLDER_NAME_PARAM, DEFAULT_FOLDER_NAME));
072        }
073    }
074
075    @Override
076    public boolean isFileSystemItem(DocumentModel doc) {
077        return isFileSystemItem(doc, false);
078    }
079
080    @Override
081    public boolean isFileSystemItem(DocumentModel doc, boolean includeDeleted) {
082        return isFileSystemItem(doc, false, false);
083    }
084
085    @Override
086    public boolean isFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint) {
087        return false;
088    }
089
090    @Override
091    public FileSystemItem getFileSystemItem(DocumentModel doc) {
092        return getFileSystemItem(doc, false);
093    }
094
095    @Override
096    public FileSystemItem getFileSystemItem(DocumentModel doc, boolean includeDeleted) {
097        return getFileSystemItem(doc, false, false);
098    }
099
100    @Override
101    public FileSystemItem getFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint) {
102        return null;
103    }
104
105    @Override
106    public FileSystemItem getFileSystemItem(DocumentModel doc, FolderItem parentItem) {
107        return getFileSystemItem(doc, parentItem, false);
108    }
109
110    @Override
111    public FileSystemItem getFileSystemItem(DocumentModel doc, FolderItem parentItem, boolean includeDeleted) {
112        return getFileSystemItem(doc, parentItem, false, false);
113    }
114
115    @Override
116    public FileSystemItem getFileSystemItem(DocumentModel doc, FolderItem parentItem, boolean includeDeleted,
117            boolean relaxSyncRootConstraint) {
118        return null;
119    }
120
121    @Override
122    public boolean canHandleFileSystemItemId(String id) {
123        return (getName() + AbstractFileSystemItem.FILE_SYSTEM_ITEM_ID_SEPARATOR).equals(id);
124    }
125
126    @Override
127    public boolean exists(String id, Principal principal) {
128        if (!canHandleFileSystemItemId(id)) {
129            throw new UnsupportedOperationException(String.format(
130                    "Cannot check if a file system item exists for an id that cannot be handled from factory %s.",
131                    getName()));
132        }
133        return true;
134    }
135
136    @Override
137    public FileSystemItem getFileSystemItemById(String id, Principal principal) {
138        if (!canHandleFileSystemItemId(id)) {
139            throw new UnsupportedOperationException(String.format(
140                    "Cannot get the file system item for an id that cannot be handled from factory %s.", getName()));
141        }
142        return getVirtualFolderItem(principal);
143    }
144
145    @Override
146    public FileSystemItem getFileSystemItemById(String id, String parentId, Principal principal) {
147        return getFileSystemItemById(id, principal);
148    }
149
150    @Deprecated
151    @Override
152    public DocumentModel getDocumentByFileSystemId(String id, Principal principal) {
153        throw new UnsupportedOperationException(String.format(
154                "Cannot get document by file system item id from VirtualFolderItemFactory %s.", getName()));
155    }
156
157    @Override
158    public String getFolderName() {
159        return folderName;
160    }
161
162    @Override
163    public void setFolderName(String folderName) {
164        this.folderName = folderName;
165    }
166
167}