001/* 002 * Copyright 2010 Anyware Services 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 */ 016package org.ametys.plugins.explorer.cmis; 017 018import java.io.InputStream; 019import java.util.Calendar; 020import java.util.Date; 021 022import org.apache.chemistry.opencmis.client.api.Document; 023import org.apache.chemistry.opencmis.client.api.Folder; 024import org.apache.chemistry.opencmis.commons.data.ContentStream; 025 026import org.ametys.core.user.UserIdentity; 027import org.ametys.core.user.population.UserPopulationDAO; 028import org.ametys.plugins.explorer.ExplorerNode; 029import org.ametys.plugins.explorer.resources.Resource; 030import org.ametys.plugins.repository.AmetysObject; 031import org.ametys.plugins.repository.AmetysRepositoryException; 032 033/** 034 * Implementation of an {@link Resource}, backed by a server CMIS.<br> 035 */ 036public class CMISResource implements Resource 037{ 038 private final Document _cmisDocument; 039 private final CMISRootResourcesCollection _root; 040 private AmetysObject _parent; 041 042 /** 043 * Creates a {@link CMISResource} 044 * @param document The CMIS document 045 * @param root The root of the virtual tree 046 * @param parent the parent {@link AmetysObject} if known 047 */ 048 public CMISResource(Document document, CMISRootResourcesCollection root, AmetysObject parent) 049 { 050 _cmisDocument = document; 051 _root = root; 052 _parent = parent; 053 } 054 055 /** 056 * Retrieves the {@link CMISRootResourcesCollection} 057 * @return the {@link CMISRootResourcesCollection} 058 */ 059 public CMISRootResourcesCollection getCmisRoot () 060 { 061 return _root; 062 } 063 064 /** 065 * Retrieves the {@link Folder} 066 * @return the {@link Folder} 067 */ 068 public Document getCmisDocument () 069 { 070 return _cmisDocument; 071 } 072 073 @Override 074 public String getId() throws AmetysRepositoryException 075 { 076 return getCmisRoot().getId() + "/" + getCmisDocument().getId(); 077 } 078 079 @Override 080 public String getName() 081 { 082 String fileName = getCmisDocument().getName(); 083 084 if (fileName == null) 085 { 086 fileName = getCmisDocument().getContentStreamFileName(); 087 } 088 if (fileName == null) 089 { 090 ContentStream cs = getCmisDocument().getContentStream(); 091 if (cs != null) 092 { 093 fileName = cs.getFileName(); 094 } 095 else 096 { 097 throw new AmetysRepositoryException("contentStream is empty on CMIS Document '" + getCmisDocument().getId() + "'"); 098 } 099 100 if (fileName == null) 101 { 102 throw new AmetysRepositoryException("fileName is null on CMIS Document '" + getCmisDocument().getId() + "'"); 103 } 104 } 105 return fileName; 106 } 107 108 @Override 109 @SuppressWarnings("unchecked") 110 public AmetysObject getParent() throws AmetysRepositoryException 111 { 112 if (_parent != null) 113 { 114 return _parent; 115 } 116 117 Folder parent = getCmisDocument().getParents().iterator().next(); 118 119 // Do not rely on the root folder property of CMIS. 120 // the root can be a mount point and there for not being a root folder from the CMIS perspective 121 if (parent.getId().equals(_root.getRootFolder().getId())) 122 { 123 _parent = getCmisRoot(); 124 return _parent; 125 } 126 else 127 { 128 _parent = new CMISResourcesCollection(parent, _root, null); 129 return _parent; 130 } 131 } 132 133 @Override 134 public String getParentPath() throws AmetysRepositoryException 135 { 136 return getParent().getPath(); 137 } 138 139 @Override 140 public String getPath() throws AmetysRepositoryException 141 { 142 return getParentPath() + "/" + getName(); 143 } 144 145 @Override 146 public UserIdentity getCreator() 147 { 148 // FIXME We cannot convert the CMIS remote user to an Ametys user. We need to know the population. If not found, return the system user. 149 // getCmisDocument().getCreatedBy(); 150 return UserPopulationDAO.SYSTEM_USER_IDENTITY; 151 } 152 153 @Override 154 public InputStream getInputStream() throws AmetysRepositoryException 155 { 156 ContentStream contentStream = getCmisDocument().getContentStream(); 157 if (contentStream != null) 158 { 159 return contentStream.getStream(); 160 } 161 return null; 162 } 163 164 @Override 165 public Date getLastModified() 166 { 167 Calendar calendar = getCmisDocument().getLastModificationDate(); 168 return calendar.getTime(); 169 } 170 171 @Override 172 public long getLength() 173 { 174 return getCmisDocument().getContentStreamLength(); 175 } 176 177 178 @Override 179 public String getMimeType() throws AmetysRepositoryException 180 { 181 String mimeType = getCmisDocument().getContentStreamMimeType(); 182 183 return mimeType != null ? mimeType : "application/unknown"; 184 } 185 186 @Override 187 public String[] getKeywords() throws AmetysRepositoryException 188 { 189 return new String[0]; 190 } 191 192 @Override 193 public String getKeywordsAsString() throws AmetysRepositoryException 194 { 195 return ""; 196 } 197 198 @Override 199 public String getResourcePath() throws AmetysRepositoryException 200 { 201 return ((ExplorerNode) getParent()).getExplorerPath() + "/" + getName(); 202 } 203 204 // Dublin Core metadata. // 205 206 @Override 207 public String getDCTitle() throws AmetysRepositoryException 208 { 209 return null; 210 } 211 212 @Override 213 public String getDCCreator() throws AmetysRepositoryException 214 { 215 return null; 216 } 217 218 @Override 219 public String[] getDCSubject() throws AmetysRepositoryException 220 { 221 return null; 222 } 223 224 @Override 225 public String getDCDescription() throws AmetysRepositoryException 226 { 227 return null; 228 } 229 230 @Override 231 public String getDCPublisher() throws AmetysRepositoryException 232 { 233 return null; 234 } 235 236 @Override 237 public String getDCContributor() throws AmetysRepositoryException 238 { 239 return null; 240 } 241 242 @Override 243 public Date getDCDate() throws AmetysRepositoryException 244 { 245 return null; 246 } 247 248 @Override 249 public String getDCType() throws AmetysRepositoryException 250 { 251 return null; 252 } 253 254 @Override 255 public String getDCFormat() throws AmetysRepositoryException 256 { 257 return null; 258 } 259 260 @Override 261 public String getDCIdentifier() throws AmetysRepositoryException 262 { 263 return null; 264 } 265 266 @Override 267 public String getDCSource() throws AmetysRepositoryException 268 { 269 return null; 270 } 271 272 @Override 273 public String getDCLanguage() throws AmetysRepositoryException 274 { 275 return null; 276 } 277 278 @Override 279 public String getDCRelation() throws AmetysRepositoryException 280 { 281 return null; 282 } 283 284 @Override 285 public String getDCCoverage() throws AmetysRepositoryException 286 { 287 return null; 288 } 289 290 @Override 291 public String getDCRights() throws AmetysRepositoryException 292 { 293 return null; 294 } 295 296 public Date getCreationDate() throws AmetysRepositoryException 297 { 298 Calendar calendar = getCmisDocument().getCreationDate(); 299 return calendar.getTime(); 300 } 301 302 public UserIdentity getLastContributor() throws AmetysRepositoryException 303 { 304 return UserIdentity.stringToUserIdentity(getCmisDocument().getLastModifiedBy()); 305 } 306}