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.portlet.messageboards.model.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.repository.model.Folder;
020    import com.liferay.portal.model.Lock;
021    import com.liferay.portal.model.Repository;
022    import com.liferay.portal.portletfilerepository.PortletFileRepositoryUtil;
023    import com.liferay.portal.service.LockLocalServiceUtil;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portal.util.PortletKeys;
026    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
027    import com.liferay.portlet.messageboards.model.MBCategory;
028    import com.liferay.portlet.messageboards.model.MBMessage;
029    import com.liferay.portlet.messageboards.model.MBThread;
030    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
031    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Mika Koivisto
036     */
037    public class MBThreadImpl extends MBThreadBaseImpl {
038    
039            public MBThreadImpl() {
040            }
041    
042            public Folder addAttachmentsFolder()
043                    throws PortalException, SystemException {
044    
045                    if (_attachmentsFolderId !=
046                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
047    
048                            return PortletFileRepositoryUtil.getPortletFolder(
049                                    _attachmentsFolderId);
050                    }
051    
052                    ServiceContext serviceContext = new ServiceContext();
053    
054                    serviceContext.setAddGroupPermissions(true);
055                    serviceContext.setAddGuestPermissions(true);
056    
057                    Repository repository = PortletFileRepositoryUtil.addPortletRepository(
058                            getGroupId(), PortletKeys.MESSAGE_BOARDS, serviceContext);
059    
060                    MBMessage message = MBMessageLocalServiceUtil.getMessage(
061                            getRootMessageId());
062    
063                    Folder folder = PortletFileRepositoryUtil.addPortletFolder(
064                            message.getUserId(), repository.getRepositoryId(),
065                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
066                            String.valueOf(getThreadId()), serviceContext);
067    
068                    _attachmentsFolderId = folder.getFolderId();
069    
070                    return folder;
071            }
072    
073            public long getAttachmentsFolderId() throws SystemException {
074                    if (_attachmentsFolderId !=
075                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
076    
077                            return _attachmentsFolderId;
078                    }
079    
080                    ServiceContext serviceContext = new ServiceContext();
081    
082                    serviceContext.setAddGroupPermissions(true);
083                    serviceContext.setAddGuestPermissions(true);
084    
085                    Repository repository =
086                            PortletFileRepositoryUtil.fetchPortletRepository(
087                                    getGroupId(), PortletKeys.MESSAGE_BOARDS);
088    
089                    if (repository == null) {
090                            return DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
091                    }
092    
093                    try {
094                            Folder folder = PortletFileRepositoryUtil.getPortletFolder(
095                                    getUserId(), repository.getRepositoryId(),
096                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
097                                    String.valueOf(getThreadId()), serviceContext);
098    
099                            _attachmentsFolderId = folder.getFolderId();
100                    }
101                    catch (Exception e) {
102                    }
103    
104                    return _attachmentsFolderId;
105            }
106    
107            public Lock getLock() {
108                    try {
109                            return LockLocalServiceUtil.getLock(
110                                    MBThread.class.getName(), getThreadId());
111                    }
112                    catch (Exception e) {
113                    }
114    
115                    return null;
116            }
117    
118            public MBCategory getTrashContainer() {
119                    try {
120                            MBCategory category = MBCategoryLocalServiceUtil.getCategory(
121                                    getCategoryId());
122    
123                            if (category.isInTrash()) {
124                                    return category;
125                            }
126    
127                            return category.getTrashContainer();
128                    }
129                    catch (Exception e) {
130                            return null;
131                    }
132            }
133    
134            public boolean hasLock(long userId) {
135                    try {
136                            return LockLocalServiceUtil.hasLock(
137                                    userId, MBThread.class.getName(), getThreadId());
138                    }
139                    catch (Exception e) {
140                    }
141    
142                    return false;
143            }
144    
145            public boolean isInTrashContainer() {
146                    if (getTrashContainer() != null) {
147                            return true;
148                    }
149                    else {
150                            return false;
151                    }
152            }
153    
154            public boolean isLocked() {
155                    try {
156                            if (isInTrash() || isInTrashContainer()) {
157                                    return true;
158                            }
159    
160                            return LockLocalServiceUtil.isLocked(
161                                    MBThread.class.getName(), getThreadId());
162                    }
163                    catch (Exception e) {
164                    }
165    
166                    return false;
167            }
168    
169            private long _attachmentsFolderId;
170    
171    }