001    /**
002     * Copyright (c) 2000-2013 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            @Override
063            public Object clone() {
064                    CMISFolder cmisFolder = new CMISFolder(
065                            _cmisRepository, _uuid, _folderId, _cmisFolder);
066    
067                    cmisFolder.setCompanyId(getCompanyId());
068                    cmisFolder.setFolderId(getFolderId());
069                    cmisFolder.setGroupId(getGroupId());
070    
071                    try {
072                            cmisFolder.setParentFolder(getParentFolder());
073                    }
074                    catch (Exception e) {
075                    }
076    
077                    cmisFolder.setPrimaryKey(getPrimaryKey());
078    
079                    return cmisFolder;
080            }
081    
082            public boolean containsPermission(
083                            PermissionChecker permissionChecker, String actionId)
084                    throws SystemException {
085    
086                    if (_cmisFolder.isRootFolder() &&
087                            (actionId.equals(ActionKeys.DELETE) ||
088                             actionId.equals(ActionKeys.UPDATE))) {
089    
090                            try {
091                                    Folder folder = DLAppLocalServiceUtil.getMountFolder(
092                                            getRepositoryId());
093    
094                                    DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(
095                                            folder.getFolderId());
096    
097                                    return DLFolderPermission.contains(
098                                            permissionChecker, dlFolder, actionId);
099                            }
100                            catch (PortalException pe) {
101                                    throw new SystemException(pe);
102                            }
103                    }
104                    else {
105                            return containsPermission(_cmisFolder, actionId);
106                    }
107            }
108    
109            public List<Long> getAncestorFolderIds()
110                    throws PortalException, SystemException {
111    
112                    List<Long> folderIds = new ArrayList<Long>();
113    
114                    Folder folder = this;
115    
116                    while (!folder.isRoot()) {
117                            folder = folder.getParentFolder();
118    
119                            folderIds.add(folder.getFolderId());
120                    }
121    
122                    return folderIds;
123            }
124    
125            public List<Folder> getAncestors() throws PortalException, SystemException {
126                    List<Folder> folders = new ArrayList<Folder>();
127    
128                    Folder folder = this;
129    
130                    while (!folder.isRoot()) {
131                            folder = folder.getParentFolder();
132    
133                            folders.add(folder);
134                    }
135    
136                    return folders;
137            }
138    
139            public Map<String, Serializable> getAttributes() {
140                    return new HashMap<String, Serializable>();
141            }
142    
143            @Override
144            public long getCompanyId() {
145                    return _cmisRepository.getCompanyId();
146            }
147    
148            public Date getCreateDate() {
149                    Calendar calendar = _cmisFolder.getCreationDate();
150    
151                    if (calendar != null) {
152                            return calendar.getTime();
153                    }
154                    else {
155                            return new Date();
156                    }
157            }
158    
159            public long getFolderId() {
160                    return _folderId;
161            }
162    
163            public long getGroupId() {
164                    return _cmisRepository.getGroupId();
165            }
166    
167            public Date getLastPostDate() {
168                    return getModifiedDate();
169            }
170    
171            public Object getModel() {
172                    return _cmisFolder;
173            }
174    
175            public Class<?> getModelClass() {
176                    return CMISFolder.class;
177            }
178    
179            @Override
180            public String getModelClassName() {
181                    return CMISFolder.class.getName();
182            }
183    
184            public Date getModifiedDate() {
185                    Calendar calendar = _cmisFolder.getLastModificationDate();
186    
187                    if (calendar != null) {
188                            return calendar.getTime();
189                    }
190                    else {
191                            return new Date();
192                    }
193            }
194    
195            public String getName() {
196                    if (_cmisFolder.isRootFolder()) {
197                            try {
198                                    Folder folder = DLAppLocalServiceUtil.getMountFolder(
199                                            getRepositoryId());
200    
201                                    return folder.getName();
202                            }
203                            catch (Exception e) {
204                                    _log.error(e, e);
205                            }
206                    }
207    
208                    return _cmisFolder.getName();
209            }
210    
211            @Override
212            public Folder getParentFolder() throws PortalException, SystemException {
213                    Folder parentFolder = null;
214    
215                    try {
216                            parentFolder = super.getParentFolder();
217    
218                            if (parentFolder != null) {
219                                    return parentFolder;
220                            }
221                    }
222                    catch (Exception e) {
223                    }
224    
225                    if (_cmisFolder.isRootFolder()) {
226                            Folder folder = DLAppLocalServiceUtil.getMountFolder(
227                                    getRepositoryId());
228    
229                            parentFolder = folder.getParentFolder();
230                    }
231                    else {
232                            String path = _cmisFolder.getPath();
233    
234                            path = path.substring(0, path.lastIndexOf(CharPool.SLASH));
235    
236                            if (path.length() == 0) {
237                                    path = StringPool.SLASH;
238                            }
239    
240                            Session session =
241                                    (Session)CMISRepositoryLocalServiceUtil.getSession(
242                                            getRepositoryId());
243    
244                            CmisObject parentCmisFolder = session.getObjectByPath(path);
245    
246                            parentFolder = CMISRepositoryLocalServiceUtil.toFolder(
247                                    getRepositoryId(), parentCmisFolder);
248                    }
249    
250                    setParentFolder(parentFolder);
251    
252                    return parentFolder;
253            }
254    
255            public long getParentFolderId() {
256                    try {
257                            Folder parentFolder = getParentFolder();
258    
259                            if (parentFolder != null) {
260                                    return parentFolder.getFolderId();
261                            }
262                    }
263                    catch (Exception e) {
264                            _log.error(e, e);
265                    }
266    
267                    return DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
268            }
269    
270            @Override
271            public long getPrimaryKey() {
272                    return _folderId;
273            }
274    
275            public Serializable getPrimaryKeyObj() {
276                    return getPrimaryKey();
277            }
278    
279            public long getRepositoryId() {
280                    return _cmisRepository.getRepositoryId();
281            }
282    
283            public long getUserId() {
284                    User user = getUser(_cmisFolder.getCreatedBy());
285    
286                    if (user == null) {
287                            return 0;
288                    }
289                    else {
290                            return user.getUserId();
291                    }
292            }
293    
294            public String getUserName() {
295                    User user = getUser(_cmisFolder.getCreatedBy());
296    
297                    if (user == null) {
298                            return StringPool.BLANK;
299                    }
300                    else {
301                            return user.getFullName();
302                    }
303            }
304    
305            public String getUserUuid() {
306                    User user = getUser(_cmisFolder.getCreatedBy());
307    
308                    try {
309                            return user.getUserUuid();
310                    }
311                    catch (Exception e) {
312                    }
313    
314                    return StringPool.BLANK;
315            }
316    
317            public String getUuid() {
318                    return _uuid;
319            }
320    
321            public boolean hasInheritableLock() {
322                    return false;
323            }
324    
325            public boolean hasLock() {
326                    return false;
327            }
328    
329            public boolean isDefaultRepository() {
330                    return false;
331            }
332    
333            public boolean isEscapedModel() {
334                    return false;
335            }
336    
337            public boolean isLocked() {
338                    return false;
339            }
340    
341            public boolean isMountPoint() {
342                    return false;
343            }
344    
345            public boolean isRoot() {
346                    if (getParentFolderId() == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
347                            return true;
348                    }
349                    else {
350                            return false;
351                    }
352            }
353    
354            public boolean isSupportsLocking() {
355                    return true;
356            }
357    
358            public boolean isSupportsMetadata() {
359                    return false;
360            }
361    
362            public boolean isSupportsMultipleUpload() {
363                    return false;
364            }
365    
366            public boolean isSupportsShortcuts() {
367                    return false;
368            }
369    
370            public boolean isSupportsSocial() {
371                    return false;
372            }
373    
374            public boolean isSupportsSubscribing() {
375                    return false;
376            }
377    
378            public void setCompanyId(long companyId) {
379                    _cmisRepository.setCompanyId(companyId);
380            }
381    
382            public void setCreateDate(Date date) {
383            }
384    
385            public void setFolderId(long folderId) {
386                    _folderId = folderId;
387            }
388    
389            public void setGroupId(long groupId) {
390                    _cmisRepository.setGroupId(groupId);
391            }
392    
393            public void setModifiedDate(Date date) {
394            }
395    
396            public void setPrimaryKey(long primaryKey) {
397                    setFolderId(primaryKey);
398            }
399    
400            public void setPrimaryKeyObj(Serializable primaryKeyObj) {
401                    setPrimaryKey(((Long)primaryKeyObj).longValue());
402            }
403    
404            public void setUserId(long userId) {
405            }
406    
407            public void setUserName(String userName) {
408            }
409    
410            public void setUserUuid(String userUuid) {
411            }
412    
413            public void setUuid(String uuid) {
414            }
415    
416            public Folder toEscapedModel() {
417                    return this;
418            }
419    
420            public Folder toUnescapedModel() {
421                    return this;
422            }
423    
424            @Override
425            protected CMISRepository getCmisRepository() {
426                    return _cmisRepository;
427            }
428    
429            private static Log _log = LogFactoryUtil.getLog(CMISFolder.class);
430    
431            private org.apache.chemistry.opencmis.client.api.Folder _cmisFolder;
432            private CMISRepository _cmisRepository;
433            private long _folderId;
434            private String _uuid;
435    
436    }