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