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