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