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 if (parent.getId().equals(_root.getRootFolder().getId())) 120 { 121 _parent = getCmisRoot(); 122 return _parent; 123 } 124 else 125 { 126 _parent = new CMISResourcesCollection(parent, _root, null); 127 return _parent; 128 } 129 } 130 131 @Override 132 public String getParentPath() throws AmetysRepositoryException 133 { 134 return getParent().getPath(); 135 } 136 137 @Override 138 public String getPath() throws AmetysRepositoryException 139 { 140 return getParentPath() + "/" + getName(); 141 } 142 143 @Override 144 public UserIdentity getCreator() 145 { 146 // 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. 147 // getCmisDocument().getCreatedBy(); 148 return UserPopulationDAO.SYSTEM_USER_IDENTITY; 149 } 150 151 @Override 152 public InputStream getInputStream() throws AmetysRepositoryException 153 { 154 ContentStream contentStream = getCmisDocument().getContentStream(); 155 if (contentStream != null) 156 { 157 return contentStream.getStream(); 158 } 159 return null; 160 } 161 162 @Override 163 public Date getLastModified() 164 { 165 Calendar calendar = getCmisDocument().getLastModificationDate(); 166 return calendar.getTime(); 167 } 168 169 @Override 170 public long getLength() 171 { 172 return getCmisDocument().getContentStreamLength(); 173 } 174 175 176 @Override 177 public String getMimeType() throws AmetysRepositoryException 178 { 179 String mimeType = getCmisDocument().getContentStreamMimeType(); 180 181 return mimeType != null ? mimeType : "application/unknown"; 182 } 183 184 @Override 185 public String[] getKeywords() throws AmetysRepositoryException 186 { 187 return new String[0]; 188 } 189 190 @Override 191 public String getKeywordsAsString() throws AmetysRepositoryException 192 { 193 return ""; 194 } 195 196 @Override 197 public String getResourcePath() throws AmetysRepositoryException 198 { 199 return ((ExplorerNode) getParent()).getExplorerPath() + "/" + getName(); 200 } 201 202 // Dublin Core metadata. // 203 204 @Override 205 public String getDCTitle() throws AmetysRepositoryException 206 { 207 return null; 208 } 209 210 @Override 211 public String getDCCreator() throws AmetysRepositoryException 212 { 213 return null; 214 } 215 216 @Override 217 public String[] getDCSubject() throws AmetysRepositoryException 218 { 219 return null; 220 } 221 222 @Override 223 public String getDCDescription() throws AmetysRepositoryException 224 { 225 return null; 226 } 227 228 @Override 229 public String getDCPublisher() throws AmetysRepositoryException 230 { 231 return null; 232 } 233 234 @Override 235 public String getDCContributor() throws AmetysRepositoryException 236 { 237 return null; 238 } 239 240 @Override 241 public Date getDCDate() throws AmetysRepositoryException 242 { 243 return null; 244 } 245 246 @Override 247 public String getDCType() throws AmetysRepositoryException 248 { 249 return null; 250 } 251 252 @Override 253 public String getDCFormat() throws AmetysRepositoryException 254 { 255 return null; 256 } 257 258 @Override 259 public String getDCIdentifier() throws AmetysRepositoryException 260 { 261 return null; 262 } 263 264 @Override 265 public String getDCSource() throws AmetysRepositoryException 266 { 267 return null; 268 } 269 270 @Override 271 public String getDCLanguage() throws AmetysRepositoryException 272 { 273 return null; 274 } 275 276 @Override 277 public String getDCRelation() throws AmetysRepositoryException 278 { 279 return null; 280 } 281 282 @Override 283 public String getDCCoverage() throws AmetysRepositoryException 284 { 285 return null; 286 } 287 288 @Override 289 public String getDCRights() throws AmetysRepositoryException 290 { 291 return null; 292 } 293 294 public Date getCreationDate() throws AmetysRepositoryException 295 { 296 Calendar calendar = getCmisDocument().getCreationDate(); 297 return calendar.getTime(); 298 } 299 300 public UserIdentity getLastContributor() throws AmetysRepositoryException 301 { 302 return UserIdentity.stringToUserIdentity(getCmisDocument().getLastModifiedBy()); 303 } 304}