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;
021
022import org.apache.commons.lang.StringUtils;
023import org.nuxeo.drive.adapter.FileSystemItem;
024import org.nuxeo.drive.adapter.FolderItem;
025import org.nuxeo.drive.service.FileSystemItemAdapterService;
026import org.nuxeo.ecm.platform.usermanager.UserManager;
027import org.nuxeo.runtime.api.Framework;
028
029/**
030 * Base class for {@link FileSystemItem} implementations.
031 *
032 * @author Antoine Taillefer
033 * @see AbstractDocumentBackedFileSystemItem
034 * @see DefaultTopLevelFolderItem
035 */
036public abstract class AbstractFileSystemItem implements FileSystemItem {
037
038    public static final String FILE_SYSTEM_ITEM_ID_SEPARATOR = "#";
039
040    private static final long serialVersionUID = 1L;
041
042    /** {@link FileSystemItem} attributes */
043    protected String id;
044
045    protected String parentId;
046
047    protected String name;
048
049    protected boolean folder;
050
051    protected String creator;
052
053    protected String lastContributor;
054
055    protected Calendar creationDate;
056
057    protected Calendar lastModificationDate;
058
059    protected boolean canRename;
060
061    protected boolean canDelete;
062
063    /** Internal attributes */
064    protected String factoryName;
065
066    protected String path;
067
068    // Must not be serialized => transient
069    protected transient Principal principal;
070
071    /**
072     * Needed for JSON serialization/deserialization since we don't serialize the principal
073     */
074    protected String userName;
075
076    protected AbstractFileSystemItem(String factoryName, Principal principal, boolean relaxSyncRootConstraint) {
077        this.factoryName = factoryName;
078        this.principal = principal;
079        this.userName = principal.getName();
080        if (relaxSyncRootConstraint) {
081            // Don't include factory name in id as in this case the document can
082            // be adapted by different factories depending on the principal.
083            // Typically a document that is a sync root for a user but a
084            // subfolder of a sync root for another one.
085            // See https://jira.nuxeo.com/browse/NXP-16038
086            this.id = StringUtils.EMPTY;
087        } else {
088            this.id = this.factoryName + FILE_SYSTEM_ITEM_ID_SEPARATOR;
089        }
090    }
091
092    protected AbstractFileSystemItem() {
093        // Needed for JSON deserialization
094    }
095
096    /*--------------------- FileSystemItem ---------------------*/
097    @Override
098    public abstract void rename(String name);
099
100    @Override
101    public abstract void delete();
102
103    @Override
104    public abstract boolean canMove(FolderItem dest);
105
106    @Override
107    public abstract FileSystemItem move(FolderItem dest);
108
109    @Override
110    public String getId() {
111        return id;
112    }
113
114    @Override
115    public String getPath() {
116        return path;
117    }
118
119    @Override
120    public String getParentId() {
121        return parentId;
122    }
123
124    @Override
125    public String getName() {
126        return name;
127    }
128
129    @Override
130    public boolean isFolder() {
131        return folder;
132    }
133
134    @Override
135    public String getCreator() {
136        return creator;
137    }
138
139    @Override
140    public String getLastContributor() {
141        return lastContributor;
142    }
143
144    @Override
145    public Calendar getCreationDate() {
146        return creationDate;
147    }
148
149    @Override
150    public Calendar getLastModificationDate() {
151        return lastModificationDate;
152    }
153
154    @Override
155    public boolean getCanRename() {
156        return canRename;
157    }
158
159    @Override
160    public boolean getCanDelete() {
161        return canDelete;
162    }
163
164    /*---------- Needed for JSON serialization ----------*/
165    public String getUserName() {
166        return userName;
167    }
168
169    /*--------------------- Comparable -------------*/
170    @Override
171    public int compareTo(FileSystemItem other) {
172        if (StringUtils.isEmpty(getName()) && StringUtils.isEmpty(other.getName())) {
173            return 0;
174        }
175        if (StringUtils.isEmpty(getName()) && !StringUtils.isEmpty(other.getName())) {
176            return -1;
177        }
178        if (!StringUtils.isEmpty(getName()) && StringUtils.isEmpty(other.getName())) {
179            return 1;
180        }
181        return getName().compareTo(other.getName());
182    }
183
184    /*--------------------- Object -----------------*/
185    @Override
186    public boolean equals(Object obj) {
187        if (this == obj) {
188            return true;
189        }
190        if (!(obj instanceof FileSystemItem)) {
191            return false;
192        }
193        return getId().equals(((FileSystemItem) obj).getId());
194    }
195
196    @Override
197    public int hashCode() {
198        return getId().hashCode();
199    }
200
201    @Override
202    public String toString() {
203        return String.format("%s(id=\"%s\", name=\"%s\")", getClass().getSimpleName(), getId(), getName());
204    }
205
206    /*--------------------- Protected ---------------------*/
207    protected FileSystemItemAdapterService getFileSystemItemAdapterService() {
208        return Framework.getLocalService(FileSystemItemAdapterService.class);
209    }
210
211    /*---------- Needed for JSON deserialization ----------*/
212    protected void setId(String id) {
213        this.id = id;
214    }
215
216    protected void setPath(String path) {
217        this.path = path;
218    }
219
220    protected void setParentId(String parentId) {
221        this.parentId = parentId;
222    }
223
224    protected void setName(String name) {
225        this.name = name;
226    }
227
228    protected void setFolder(boolean isFolder) {
229        this.folder = isFolder;
230    }
231
232    protected void setCreator(String creator) {
233        this.creator = creator;
234    }
235
236    protected void setLastContributor(String lastContributor) {
237        this.lastContributor = lastContributor;
238    }
239
240    protected void setCreationDate(Calendar creationDate) {
241        this.creationDate = creationDate;
242    }
243
244    protected void setLastModificationDate(Calendar lastModificationDate) {
245        this.lastModificationDate = lastModificationDate;
246    }
247
248    protected void setCanRename(boolean canRename) {
249        this.canRename = canRename;
250    }
251
252    protected void setCanDelete(boolean canDelete) {
253        this.canDelete = canDelete;
254    }
255
256    protected void setUserName(String userName) {
257        this.userName = userName;
258        this.principal = Framework.getLocalService(UserManager.class).getPrincipal(userName);
259    }
260}