001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.repository.cmis.model;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.repository.model.Folder;
022    import com.liferay.portal.kernel.util.CharPool;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.repository.cmis.CMISRepository;
026    import com.liferay.portal.security.permission.ActionKeys;
027    import com.liferay.portal.security.permission.PermissionChecker;
028    import com.liferay.portal.service.CMISRepositoryLocalServiceUtil;
029    import com.liferay.portlet.documentlibrary.model.DLFolder;
030    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
031    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
032    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
033    import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
034    
035    import java.io.Serializable;
036    
037    import java.util.ArrayList;
038    import java.util.Calendar;
039    import java.util.Date;
040    import java.util.HashMap;
041    import java.util.List;
042    import java.util.Map;
043    
044    import org.apache.chemistry.opencmis.client.api.CmisObject;
045    import org.apache.chemistry.opencmis.client.api.Session;
046    
047    /**
048     * @author Alexander Chow
049     */
050    public class CMISFolder extends CMISModel implements Folder {
051    
052            public CMISFolder(
053                    CMISRepository cmisRepository, String uuid, long folderId,
054                    org.apache.chemistry.opencmis.client.api.Folder cmisFolder) {
055    
056                    _cmisRepository = cmisRepository;
057                    _uuid = uuid;
058                    _folderId = folderId;
059                    _cmisFolder = cmisFolder;
060            }
061    
062            public boolean containsPermission(
063                            PermissionChecker permissionChecker, String actionId)
064                    throws SystemException {
065    
066                    if (_cmisFolder.isRootFolder() &&
067                            (actionId.equals(ActionKeys.DELETE) ||
068                             actionId.equals(ActionKeys.UPDATE))) {
069    
070                            try {
071                                    Folder folder = DLAppLocalServiceUtil.getMountFolder(
072                                            getRepositoryId());
073    
074                                    DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(
075                                            folder.getFolderId());
076    
077                                    return DLFolderPermission.contains(
078                                            permissionChecker, dlFolder, actionId);
079                            }
080                            catch (PortalException pe) {
081                                    throw new SystemException(pe);
082                            }
083                    }
084                    else {
085                            return containsPermission(_cmisFolder, actionId);
086                    }
087            }
088    
089            public List<Long> getAncestorFolderIds()
090                    throws PortalException, SystemException {
091    
092                    List<Long> folderIds = new ArrayList<Long>();
093    
094                    Folder folder = this;
095    
096                    while (!folder.isRoot()) {
097                            folder = folder.getParentFolder();
098    
099                            folderIds.add(folder.getFolderId());
100                    }
101    
102                    return folderIds;
103            }
104    
105            public List<Folder> getAncestors() throws PortalException, SystemException {
106                    List<Folder> folders = new ArrayList<Folder>();
107    
108                    Folder folder = this;
109    
110                    while (!folder.isRoot()) {
111                            folder = folder.getParentFolder();
112    
113                            folders.add(folder);
114                    }
115    
116                    return folders;
117            }
118    
119            public Map<String, Serializable> getAttributes() {
120                    return new HashMap<String, Serializable>();
121            }
122    
123            @Override
124            public long getCompanyId() {
125                    return _cmisRepository.getCompanyId();
126            }
127    
128            public Date getCreateDate() {
129                    Calendar calendar = _cmisFolder.getCreationDate();
130    
131                    if (calendar != null) {
132                            return calendar.getTime();
133                    }
134                    else {
135                            return new Date();
136                    }
137            }
138    
139            public long getFolderId() {
140                    return _folderId;
141            }
142    
143            public long getGroupId() {
144                    return _cmisRepository.getGroupId();
145            }
146    
147            public Date getLastPostDate() {
148                    return getModifiedDate();
149            }
150    
151            public Object getModel() {
152                    return _cmisFolder;
153            }
154    
155            public Class<?> getModelClass() {
156                    return DLFolder.class;
157            }
158    
159            @Override
160            public String getModelClassName() {
161                    return DLFolder.class.getName();
162            }
163    
164            public Date getModifiedDate() {
165                    Calendar calendar = _cmisFolder.getLastModificationDate();
166    
167                    if (calendar != null) {
168                            return calendar.getTime();
169                    }
170                    else {
171                            return new Date();
172                    }
173            }
174    
175            public String getName() {
176                    if (_cmisFolder.isRootFolder()) {
177                            try {
178                                    Folder folder = DLAppLocalServiceUtil.getMountFolder(
179                                            getRepositoryId());
180    
181                                    return folder.getName();
182                            }
183                            catch (Exception e) {
184                                    _log.error(e, e);
185                            }
186                    }
187    
188                    return _cmisFolder.getName();
189            }
190    
191            @Override
192            public Folder getParentFolder() throws PortalException, SystemException {
193                    Folder parentFolder = null;
194    
195                    try {
196                            parentFolder = super.getParentFolder();
197    
198                            if (parentFolder != null) {
199                                    return parentFolder;
200                            }
201                    }
202                    catch (Exception e) {
203                    }
204    
205                    if (_cmisFolder.isRootFolder()) {
206                            Folder folder = DLAppLocalServiceUtil.getMountFolder(
207                                    getRepositoryId());
208    
209                            parentFolder = folder.getParentFolder();
210                    }
211                    else {
212                            String path = _cmisFolder.getPath();
213    
214                            path = path.substring(0, path.lastIndexOf(CharPool.SLASH));
215    
216                            if (path.length() == 0) {
217                                    path = StringPool.SLASH;
218                            }
219    
220                            Session session =
221                                    (Session)CMISRepositoryLocalServiceUtil.getSession(
222                                            getRepositoryId());
223    
224                            CmisObject parentCmisFolder = session.getObjectByPath(path);
225    
226                            parentFolder = CMISRepositoryLocalServiceUtil.toFolder(
227                                    getRepositoryId(), parentCmisFolder);
228                    }
229    
230                    setParentFolder(parentFolder);
231    
232                    return parentFolder;
233            }
234    
235            public long getParentFolderId() {
236                    try {
237                            Folder parentFolder = getParentFolder();
238    
239                            if (parentFolder != null) {
240                                    return parentFolder.getFolderId();
241                            }
242                    }
243                    catch (Exception e) {
244                            _log.error(e, e);
245                    }
246    
247                    return DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
248            }
249    
250            @Override
251            public long getPrimaryKey() {
252                    return _folderId;
253            }
254    
255            public Serializable getPrimaryKeyObj() {
256                    return getPrimaryKey();
257            }
258    
259            public long getRepositoryId() {
260                    return _cmisRepository.getRepositoryId();
261            }
262    
263            public long getUserId() {
264                    User user = getUser(_cmisFolder.getCreatedBy());
265    
266                    if (user == null) {
267                            return 0;
268                    }
269                    else {
270                            return user.getUserId();
271                    }
272            }
273    
274            public String getUserName() {
275                    User user = getUser(_cmisFolder.getCreatedBy());
276    
277                    if (user == null) {
278                            return StringPool.BLANK;
279                    }
280                    else {
281                            return user.getFullName();
282                    }
283            }
284    
285            public String getUserUuid() {
286                    User user = getUser(_cmisFolder.getCreatedBy());
287    
288                    try {
289                            return user.getUserUuid();
290                    }
291                    catch (Exception e) {
292                    }
293    
294                    return StringPool.BLANK;
295            }
296    
297            public String getUuid() {
298                    return _uuid;
299            }
300    
301            public boolean hasInheritableLock() {
302                    return false;
303            }
304    
305            public boolean hasLock() {
306                    return false;
307            }
308    
309            public boolean isDefaultRepository() {
310                    return false;
311            }
312    
313            public boolean isEscapedModel() {
314                    return false;
315            }
316    
317            public boolean isLocked() {
318                    return false;
319            }
320    
321            public boolean isMountPoint() {
322                    return false;
323            }
324    
325            public boolean isRoot() {
326                    if (getParentFolderId() == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
327                            return true;
328                    }
329                    else {
330                            return false;
331                    }
332            }
333    
334            public boolean isSupportsLocking() {
335                    return true;
336            }
337    
338            public boolean isSupportsMetadata() {
339                    return false;
340            }
341    
342            public boolean isSupportsMultipleUpload() {
343                    return false;
344            }
345    
346            public boolean isSupportsShortcuts() {
347                    return false;
348            }
349    
350            public boolean isSupportsSocial() {
351                    return false;
352            }
353    
354            public boolean isSupportsSubscribing() {
355                    return false;
356            }
357    
358            public void setCompanyId(long companyId) {
359                    _cmisRepository.setCompanyId(companyId);
360            }
361    
362            public void setCreateDate(Date date) {
363            }
364    
365            public void setFolderId(long folderId) {
366                    _folderId = folderId;
367            }
368    
369            public void setGroupId(long groupId) {
370                    _cmisRepository.setGroupId(groupId);
371            }
372    
373            public void setModifiedDate(Date date) {
374            }
375    
376            public void setPrimaryKey(long primaryKey) {
377                    setFolderId(primaryKey);
378            }
379    
380            public void setPrimaryKeyObj(Serializable primaryKeyObj) {
381                    setPrimaryKey(((Long)primaryKeyObj).longValue());
382            }
383    
384            public void setUserId(long userId) {
385            }
386    
387            public void setUserName(String userName) {
388            }
389    
390            public void setUserUuid(String userUuid) {
391            }
392    
393            public Folder toEscapedModel() {
394                    return this;
395            }
396    
397            public Folder toUnescapedModel() {
398                    return this;
399            }
400    
401            @Override
402            protected CMISRepository getCmisRepository() {
403                    return _cmisRepository;
404            }
405    
406            private static Log _log = LogFactoryUtil.getLog(CMISFolder.class);
407    
408            private org.apache.chemistry.opencmis.client.api.Folder _cmisFolder;
409            private CMISRepository _cmisRepository;
410            private long _folderId;
411            private String _uuid;
412    
413    }