001    /**
002     * Copyright (c) 2000-present 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.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.parsers.bbcode.BBCodeTranslatorUtil;
020    import com.liferay.portal.kernel.repository.model.FileEntry;
021    import com.liferay.portal.kernel.repository.model.Folder;
022    import com.liferay.portal.kernel.workflow.WorkflowConstants;
023    import com.liferay.portal.model.Repository;
024    import com.liferay.portal.portletfilerepository.PortletFileRepositoryUtil;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
027    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
028    import com.liferay.portlet.messageboards.constants.MBConstants;
029    import com.liferay.portlet.messageboards.model.MBCategory;
030    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
031    import com.liferay.portlet.messageboards.model.MBDiscussion;
032    import com.liferay.portlet.messageboards.model.MBMessage;
033    import com.liferay.portlet.messageboards.model.MBMessageConstants;
034    import com.liferay.portlet.messageboards.model.MBThread;
035    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
036    import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
037    
038    import java.util.ArrayList;
039    import java.util.List;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     */
044    public class MBMessageImpl extends MBMessageBaseImpl {
045    
046            @Override
047            public Folder addAttachmentsFolder() throws PortalException {
048                    if (_attachmentsFolderId !=
049                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
050    
051                            return PortletFileRepositoryUtil.getPortletFolder(
052                                    _attachmentsFolderId);
053                    }
054    
055                    ServiceContext serviceContext = new ServiceContext();
056    
057                    serviceContext.setAddGroupPermissions(true);
058                    serviceContext.setAddGuestPermissions(true);
059    
060                    Repository repository = PortletFileRepositoryUtil.addPortletRepository(
061                            getGroupId(), MBConstants.SERVICE_NAME, serviceContext);
062    
063                    MBThread thread = getThread();
064    
065                    Folder threadFolder = thread.addAttachmentsFolder();
066    
067                    Folder folder = PortletFileRepositoryUtil.addPortletFolder(
068                            getUserId(), repository.getRepositoryId(),
069                            threadFolder.getFolderId(), String.valueOf(getMessageId()),
070                            serviceContext);
071    
072                    _attachmentsFolderId = folder.getFolderId();
073    
074                    return folder;
075            }
076    
077            @Override
078            public String[] getAssetTagNames() {
079                    return AssetTagLocalServiceUtil.getTagNames(
080                            MBMessage.class.getName(), getMessageId());
081            }
082    
083            @Override
084            public List<FileEntry> getAttachmentsFileEntries() throws PortalException {
085                    return getAttachmentsFileEntries(QueryUtil.ALL_POS, QueryUtil.ALL_POS);
086            }
087    
088            @Override
089            public List<FileEntry> getAttachmentsFileEntries(int start, int end)
090                    throws PortalException {
091    
092                    List<FileEntry> fileEntries = new ArrayList<>();
093    
094                    long attachmentsFolderId = getAttachmentsFolderId();
095    
096                    if (attachmentsFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
097                            fileEntries = PortletFileRepositoryUtil.getPortletFileEntries(
098                                    getGroupId(), attachmentsFolderId,
099                                    WorkflowConstants.STATUS_APPROVED, start, end, null);
100                    }
101    
102                    return fileEntries;
103            }
104    
105            @Override
106            public int getAttachmentsFileEntriesCount() throws PortalException {
107                    int attachmentsFileEntriesCount = 0;
108    
109                    long attachmentsFolderId = getAttachmentsFolderId();
110    
111                    if (attachmentsFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
112                            attachmentsFileEntriesCount =
113                                    PortletFileRepositoryUtil.getPortletFileEntriesCount(
114                                            getGroupId(), attachmentsFolderId,
115                                            WorkflowConstants.STATUS_APPROVED);
116                    }
117    
118                    return attachmentsFileEntriesCount;
119            }
120    
121            @Override
122            public long getAttachmentsFolderId() throws PortalException {
123                    if (_attachmentsFolderId !=
124                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
125    
126                            return _attachmentsFolderId;
127                    }
128    
129                    ServiceContext serviceContext = new ServiceContext();
130    
131                    serviceContext.setAddGroupPermissions(true);
132                    serviceContext.setAddGuestPermissions(true);
133    
134                    Repository repository =
135                            PortletFileRepositoryUtil.fetchPortletRepository(
136                                    getGroupId(), MBConstants.SERVICE_NAME);
137    
138                    long threadAttachmetsFolderId = getThreadAttachmentsFolderId();
139    
140                    if ((repository == null) ||
141                            (threadAttachmetsFolderId ==
142                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
143    
144                            return DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
145                    }
146    
147                    try {
148                            Folder folder = PortletFileRepositoryUtil.getPortletFolder(
149                                    repository.getRepositoryId(), threadAttachmetsFolderId,
150                                    String.valueOf(getMessageId()));
151    
152                            _attachmentsFolderId = folder.getFolderId();
153                    }
154                    catch (Exception e) {
155                    }
156    
157                    return _attachmentsFolderId;
158            }
159    
160            @Override
161            public String getBody(boolean translate) {
162                    String body = null;
163    
164                    if (translate) {
165                            body = BBCodeTranslatorUtil.getHTML(getBody());
166                    }
167                    else {
168                            body = getBody();
169                    }
170    
171                    return body;
172            }
173    
174            @Override
175            public MBCategory getCategory() throws PortalException {
176                    return MBCategoryLocalServiceUtil.getCategory(getCategoryId());
177            }
178    
179            @Override
180            public List<FileEntry> getDeletedAttachmentsFileEntries()
181                    throws PortalException {
182    
183                    return getDeletedAttachmentsFileEntries(
184                            QueryUtil.ALL_POS, QueryUtil.ALL_POS);
185            }
186    
187            @Override
188            public List<FileEntry> getDeletedAttachmentsFileEntries(int start, int end)
189                    throws PortalException {
190    
191                    List<FileEntry> fileEntries = new ArrayList<>();
192    
193                    long attachmentsFolderId = getAttachmentsFolderId();
194    
195                    if (attachmentsFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
196                            fileEntries = PortletFileRepositoryUtil.getPortletFileEntries(
197                                    getGroupId(), attachmentsFolderId,
198                                    WorkflowConstants.STATUS_IN_TRASH, start, end, null);
199                    }
200    
201                    return fileEntries;
202            }
203    
204            @Override
205            public int getDeletedAttachmentsFileEntriesCount() throws PortalException {
206                    int deletedAttachmentsFileEntriesCount = 0;
207    
208                    long attachmentsFolderId = getAttachmentsFolderId();
209    
210                    if (attachmentsFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
211                            deletedAttachmentsFileEntriesCount =
212                                    PortletFileRepositoryUtil.getPortletFileEntriesCount(
213                                            getGroupId(), attachmentsFolderId,
214                                            WorkflowConstants.STATUS_IN_TRASH);
215                    }
216    
217                    return deletedAttachmentsFileEntriesCount;
218            }
219    
220            @Override
221            public MBThread getThread() throws PortalException {
222                    return MBThreadLocalServiceUtil.getThread(getThreadId());
223            }
224    
225            @Override
226            public long getThreadAttachmentsFolderId() throws PortalException {
227                    return getThread().getAttachmentsFolderId();
228            }
229    
230            @Override
231            public String getWorkflowClassName() {
232                    if (isDiscussion()) {
233                            return MBDiscussion.class.getName();
234                    }
235                    else {
236                            return MBMessage.class.getName();
237                    }
238            }
239    
240            @Override
241            public boolean isDiscussion() {
242                    if (getCategoryId() == MBCategoryConstants.DISCUSSION_CATEGORY_ID) {
243                            return true;
244                    }
245                    else {
246                            return false;
247                    }
248            }
249    
250            @Override
251            public boolean isFormatBBCode() {
252                    String format = getFormat();
253    
254                    if (format.equals("bbcode")) {
255                            return true;
256                    }
257                    else {
258                            return false;
259                    }
260            }
261    
262            @Override
263            public boolean isReply() {
264                    return !isRoot();
265            }
266    
267            @Override
268            public boolean isRoot() {
269                    if (getParentMessageId() ==
270                                    MBMessageConstants.DEFAULT_PARENT_MESSAGE_ID) {
271    
272                            return true;
273                    }
274                    else {
275                            return false;
276                    }
277            }
278    
279            @Override
280            public void setAttachmentsFolderId(long attachmentsFolderId) {
281                    _attachmentsFolderId = attachmentsFolderId;
282            }
283    
284            private long _attachmentsFolderId;
285    
286    }