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