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