001/* 002 * (C) Copyright 2012-2018 Nuxeo (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 <[email protected]> 018 */ 019package org.nuxeo.drive.adapter.impl; 020 021import java.util.Calendar; 022 023import org.apache.commons.lang3.StringUtils; 024import org.nuxeo.drive.adapter.FileSystemItem; 025import org.nuxeo.drive.service.FileSystemItemAdapterService; 026import org.nuxeo.ecm.core.api.Lock; 027import org.nuxeo.ecm.core.api.NuxeoPrincipal; 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 public static final String FILE_SYSTEM_ITEM_PATH_SEPARATOR = "/"; 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 protected Lock lockInfo; 066 067 /** Internal attributes */ 068 protected String factoryName; 069 070 protected String path; 071 072 protected NuxeoPrincipal principal; 073 074 /** 075 * Needed for JSON serialization/deserialization since we don't serialize the principal 076 */ 077 protected String userName; 078 079 protected AbstractFileSystemItem(String factoryName, NuxeoPrincipal principal, boolean relaxSyncRootConstraint) { 080 this.factoryName = factoryName; 081 this.principal = principal; 082 this.userName = principal.getName(); 083 if (relaxSyncRootConstraint) { 084 // Don't include factory name in id as in this case the document can 085 // be adapted by different factories depending on the principal. 086 // Typically a document that is a sync root for a user but a 087 // subfolder of a sync root for another one. 088 // See https://jira.nuxeo.com/browse/NXP-16038 089 this.id = StringUtils.EMPTY; 090 } else { 091 this.id = this.factoryName + FILE_SYSTEM_ITEM_ID_SEPARATOR; 092 } 093 } 094 095 protected AbstractFileSystemItem() { 096 // Needed for JSON deserialization 097 } 098 099 /*--------------------- FileSystemItem ---------------------*/ 100 @Override 101 public String getId() { 102 return id; 103 } 104 105 @Override 106 public String getPath() { 107 return path; 108 } 109 110 @Override 111 public String getParentId() { 112 return parentId; 113 } 114 115 @Override 116 public String getName() { 117 return name; 118 } 119 120 @Override 121 public boolean isFolder() { 122 return folder; 123 } 124 125 @Override 126 public String getCreator() { 127 return creator; 128 } 129 130 @Override 131 public String getLastContributor() { 132 return lastContributor; 133 } 134 135 @Override 136 public Calendar getCreationDate() { 137 return creationDate; 138 } 139 140 @Override 141 public Calendar getLastModificationDate() { 142 return lastModificationDate; 143 } 144 145 @Override 146 public boolean getCanRename() { 147 return canRename; 148 } 149 150 @Override 151 public boolean getCanDelete() { 152 return canDelete; 153 } 154 155 @Override 156 public Lock getLockInfo() { 157 return lockInfo; 158 } 159 160 /*---------- Needed for JSON serialization ----------*/ 161 public String getUserName() { 162 return userName; 163 } 164 165 /*--------------------- Comparable -------------*/ 166 @Override 167 public int compareTo(FileSystemItem other) { 168 if (StringUtils.isEmpty(getName()) && StringUtils.isEmpty(other.getName())) { 169 return 0; 170 } 171 if (StringUtils.isEmpty(getName()) && !StringUtils.isEmpty(other.getName())) { 172 return -1; 173 } 174 if (!StringUtils.isEmpty(getName()) && StringUtils.isEmpty(other.getName())) { 175 return 1; 176 } 177 return getName().compareTo(other.getName()); 178 } 179 180 /*--------------------- Object -----------------*/ 181 @Override 182 public boolean equals(Object obj) { 183 if (this == obj) { 184 return true; 185 } 186 if (!(obj instanceof FileSystemItem)) { 187 return false; 188 } 189 return getId().equals(((FileSystemItem) obj).getId()); 190 } 191 192 @Override 193 public int hashCode() { 194 return getId().hashCode(); 195 } 196 197 @Override 198 public String toString() { 199 return String.format("%s(id=\"%s\", name=\"%s\")", getClass().getSimpleName(), getId(), getName()); 200 } 201 202 /*--------------------- Protected ---------------------*/ 203 protected FileSystemItemAdapterService getFileSystemItemAdapterService() { 204 return Framework.getService(FileSystemItemAdapterService.class); 205 } 206 207 /*---------- Needed for JSON deserialization ----------*/ 208 protected void setId(String id) { 209 this.id = id; 210 } 211 212 protected void setPath(String path) { 213 this.path = path; 214 } 215 216 protected void setParentId(String parentId) { 217 this.parentId = parentId; 218 } 219 220 protected void setName(String name) { 221 this.name = name; 222 } 223 224 protected void setFolder(boolean isFolder) { 225 this.folder = isFolder; 226 } 227 228 protected void setCreator(String creator) { 229 this.creator = creator; 230 } 231 232 protected void setLastContributor(String lastContributor) { 233 this.lastContributor = lastContributor; 234 } 235 236 protected void setCreationDate(Calendar creationDate) { 237 this.creationDate = creationDate; 238 } 239 240 protected void setLastModificationDate(Calendar lastModificationDate) { 241 this.lastModificationDate = lastModificationDate; 242 } 243 244 protected void setCanRename(boolean canRename) { 245 this.canRename = canRename; 246 } 247 248 protected void setCanDelete(boolean canDelete) { 249 this.canDelete = canDelete; 250 } 251 252 protected void setLockInfo(Lock lockInfo) { 253 this.lockInfo = lockInfo; 254 } 255 256 protected void setUserName(String userName) { 257 this.userName = userName; 258 this.principal = Framework.getService(UserManager.class).getPrincipal(userName); 259 } 260}