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.adapter.impl;
018
019import java.security.Principal;
020import java.util.Calendar;
021import java.util.List;
022
023import org.nuxeo.drive.adapter.FileItem;
024import org.nuxeo.drive.adapter.FileSystemItem;
025import org.nuxeo.drive.adapter.FolderItem;
026import org.nuxeo.drive.service.impl.NuxeoDriveManagerImpl;
027import org.nuxeo.ecm.core.api.Blob;
028
029/**
030 * Base implementation of a virtual {@link FolderItem}.
031 *
032 * @author Antoine Taillefer
033 */
034public abstract class AbstractVirtualFolderItem extends AbstractFileSystemItem implements FolderItem {
035
036    private static final long serialVersionUID = 1L;
037
038    protected boolean canCreateChild;
039
040    public AbstractVirtualFolderItem(String factoryName, Principal principal, String parentId, String parentPath,
041            String folderName) {
042        super(factoryName, principal, false);
043        this.parentId = parentId;
044        name = folderName;
045        folder = true;
046        creator = "system";
047        lastContributor = "system";
048        // The Fixed Origin of (Unix) Time
049        creationDate = Calendar.getInstance(NuxeoDriveManagerImpl.UTC);
050        creationDate.set(1970, 0, 1, 0, 0, 0);
051        lastModificationDate = this.creationDate;
052        canRename = false;
053        canDelete = false;
054        canCreateChild = false;
055        path = "/" + getId();
056        if (parentPath != null) {
057            path = parentPath + path;
058        }
059    }
060
061    protected AbstractVirtualFolderItem() {
062        // Needed for JSON deserialization
063    }
064
065    /*----------------------- FolderItem -----------------------*/
066    @Override
067    public abstract List<FileSystemItem> getChildren();
068
069    /*--------------------- FileSystemItem ---------------------*/
070    @Override
071    public void rename(String name) {
072        throw new UnsupportedOperationException("Cannot rename a virtual folder item.");
073    }
074
075    @Override
076    public void delete() {
077        throw new UnsupportedOperationException("Cannot delete a virtual folder item.");
078    }
079
080    @Override
081    public boolean canMove(FolderItem dest) {
082        return false;
083    }
084
085    @Override
086    public FileSystemItem move(FolderItem dest) {
087        throw new UnsupportedOperationException("Cannot move a virtual folder item.");
088    }
089
090    /*--------------------- FolderItem -----------------*/
091    @Override
092    public boolean getCanCreateChild() {
093        return canCreateChild;
094    }
095
096    @Override
097    public FolderItem createFolder(String name) {
098        throw new UnsupportedOperationException("Cannot create a folder in a virtual folder item.");
099    }
100
101    @Override
102    public FileItem createFile(Blob blob) {
103        throw new UnsupportedOperationException("Cannot create a file in a virtual folder item.");
104    }
105
106    /*---------- Needed for JSON deserialization ----------*/
107    protected void setCanCreateChild(boolean canCreateChild) {
108        this.canCreateChild = canCreateChild;
109    }
110
111}