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