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; 032import org.ametys.plugins.repository.metadata.UnknownMetadataException; 033 034/** 035 * Implementation of an {@link Resource}, backed by a server CMIS.<br> 036 */ 037public class CMISResource implements Resource 038{ 039 private final Document _cmisDocument; 040 private final CMISRootResourcesCollection _root; 041 private AmetysObject _parent; 042 043 /** 044 * Creates a {@link CMISResource} 045 * @param document The CMIS document 046 * @param root The root of the virtual tree 047 * @param parent the parent {@link AmetysObject} if known 048 */ 049 public CMISResource(Document document, CMISRootResourcesCollection root, AmetysObject parent) 050 { 051 _cmisDocument = document; 052 _root = root; 053 _parent = parent; 054 } 055 056 /** 057 * Retrieves the {@link CMISRootResourcesCollection} 058 * @return the {@link CMISRootResourcesCollection} 059 */ 060 public CMISRootResourcesCollection getCmisRoot () 061 { 062 return _root; 063 } 064 065 /** 066 * Retrieves the {@link Folder} 067 * @return the {@link Folder} 068 */ 069 public Document getCmisDocument () 070 { 071 return _cmisDocument; 072 } 073 074 @Override 075 public String getId() throws AmetysRepositoryException 076 { 077 return getCmisRoot().getId() + "/" + getCmisDocument().getId(); 078 } 079 080 @Override 081 public String getName() 082 { 083 return getCmisDocument().getContentStreamFileName(); 084 } 085 086 @Override 087 @SuppressWarnings("unchecked") 088 public AmetysObject getParent() throws AmetysRepositoryException 089 { 090 if (_parent != null) 091 { 092 return _parent; 093 } 094 095 Folder parent = getCmisDocument().getParents().iterator().next(); 096 097 if (parent.getId().equals(_root.getRootFolder().getId())) 098 { 099 _parent = getCmisRoot(); 100 return _parent; 101 } 102 else 103 { 104 _parent = new CMISResourcesCollection(parent, _root, null); 105 return _parent; 106 } 107 } 108 109 @Override 110 public String getParentPath() throws AmetysRepositoryException 111 { 112 return getParent().getPath(); 113 } 114 115 @Override 116 public String getPath() throws AmetysRepositoryException 117 { 118 return getParentPath() + "/" + getName(); 119 } 120 121 @Override 122 public UserIdentity getCreator() 123 { 124 // 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. 125 // getCmisDocument().getCreatedBy(); 126 return UserPopulationDAO.SYSTEM_USER_IDENTITY; 127 } 128 129 @Override 130 public InputStream getInputStream() throws AmetysRepositoryException 131 { 132 ContentStream contentStream = getCmisDocument().getContentStream(); 133 if (contentStream != null) 134 { 135 return contentStream.getStream(); 136 } 137 return null; 138 } 139 140 @Override 141 public Date getLastModified() 142 { 143 Calendar calendar = getCmisDocument().getLastModificationDate(); 144 return calendar.getTime(); 145 } 146 147 @Override 148 public long getLength() 149 { 150 return getCmisDocument().getContentStreamLength(); 151 } 152 153 154 @Override 155 public String getMimeType() throws AmetysRepositoryException 156 { 157 String mimeType = getCmisDocument().getContentStreamMimeType(); 158 159 return mimeType != null ? mimeType : "application/unknown"; 160 } 161 162 @Override 163 public String[] getKeywords() throws AmetysRepositoryException 164 { 165 return new String[0]; 166 } 167 168 @Override 169 public String getKeywordsAsString() throws AmetysRepositoryException 170 { 171 return ""; 172 } 173 174 @Override 175 public String getResourcePath() throws AmetysRepositoryException 176 { 177 return ((ExplorerNode) getParent()).getExplorerPath() + "/" + getName(); 178 } 179 180 // Dublin Core metadata. // 181 182 @Override 183 public String getDCTitle() throws AmetysRepositoryException 184 { 185 return null; 186 } 187 188 @Override 189 public String getDCCreator() throws AmetysRepositoryException 190 { 191 return null; 192 } 193 194 @Override 195 public String[] getDCSubject() throws AmetysRepositoryException 196 { 197 return null; 198 } 199 200 @Override 201 public String getDCDescription() throws AmetysRepositoryException 202 { 203 return null; 204 } 205 206 @Override 207 public String getDCPublisher() throws AmetysRepositoryException 208 { 209 return null; 210 } 211 212 @Override 213 public String getDCContributor() throws AmetysRepositoryException 214 { 215 return null; 216 } 217 218 @Override 219 public Date getDCDate() throws AmetysRepositoryException 220 { 221 return null; 222 } 223 224 @Override 225 public String getDCType() throws AmetysRepositoryException 226 { 227 return null; 228 } 229 230 @Override 231 public String getDCFormat() throws AmetysRepositoryException 232 { 233 return null; 234 } 235 236 @Override 237 public String getDCIdentifier() throws AmetysRepositoryException 238 { 239 return null; 240 } 241 242 @Override 243 public String getDCSource() throws AmetysRepositoryException 244 { 245 return null; 246 } 247 248 @Override 249 public String getDCLanguage() throws AmetysRepositoryException 250 { 251 return null; 252 } 253 254 @Override 255 public String getDCRelation() throws AmetysRepositoryException 256 { 257 return null; 258 } 259 260 @Override 261 public String getDCCoverage() throws AmetysRepositoryException 262 { 263 return null; 264 } 265 266 @Override 267 public String getDCRights() throws AmetysRepositoryException 268 { 269 return null; 270 } 271 272 public Date getCreationDate() throws UnknownMetadataException, AmetysRepositoryException 273 { 274 Calendar calendar = getCmisDocument().getCreationDate(); 275 return calendar.getTime(); 276 } 277 278 public UserIdentity getLastContributor() throws UnknownMetadataException, AmetysRepositoryException 279 { 280 return UserIdentity.stringToUserIdentity(getCmisDocument().getLastModifiedBy()); 281 } 282}