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