001    /**
002     * Copyright (c) 2000-2012 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.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.messageboards.model.MBCategory;
029    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
030    import com.liferay.portlet.messageboards.model.MBDiscussion;
031    import com.liferay.portlet.messageboards.model.MBMessage;
032    import com.liferay.portlet.messageboards.model.MBMessageConstants;
033    import com.liferay.portlet.messageboards.model.MBThread;
034    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
035    import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
036    
037    import java.util.List;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class MBMessageImpl extends MBMessageBaseImpl {
043    
044            public MBMessageImpl() {
045            }
046    
047            public String[] getAssetTagNames() throws SystemException {
048                    return AssetTagLocalServiceUtil.getTagNames(
049                            MBMessage.class.getName(), getMessageId());
050            }
051    
052            public List<FileEntry> getAttachmentsFileEntries()
053                    throws PortalException, SystemException {
054    
055                    return getAttachmentsFileEntries(QueryUtil.ALL_POS, QueryUtil.ALL_POS);
056            }
057    
058            public List<FileEntry> getAttachmentsFileEntries(int start, int end)
059                    throws PortalException, SystemException {
060    
061                    return PortletFileRepositoryUtil.getPortletFileEntries(
062                            getGroupId(), getAttachmentsFolderId(),
063                            WorkflowConstants.STATUS_APPROVED, start, end, null);
064            }
065    
066            public int getAttachmentsFileEntriesCount()
067                    throws PortalException, SystemException {
068    
069                    return PortletFileRepositoryUtil.getPortletFileEntriesCount(
070                            getGroupId(), getAttachmentsFolderId(),
071                            WorkflowConstants.STATUS_APPROVED);
072            }
073    
074            public long getAttachmentsFolderId()
075                    throws PortalException, SystemException {
076    
077                    if (_attachmentsFolderId > 0) {
078                            return _attachmentsFolderId;
079                    }
080    
081                    ServiceContext serviceContext = new ServiceContext();
082    
083                    serviceContext.setAddGroupPermissions(true);
084                    serviceContext.setAddGuestPermissions(true);
085    
086                    long repositoryId = PortletFileRepositoryUtil.getPortletRepositoryId(
087                            getGroupId(), PortletKeys.MESSAGE_BOARDS, serviceContext);
088    
089                    Folder folder = PortletFileRepositoryUtil.getPortletFolder(
090                            getUserId(), repositoryId, getThreadAttachmentsFolderId(),
091                            String.valueOf(getMessageId()), serviceContext);
092    
093                    _attachmentsFolderId = folder.getFolderId();
094    
095                    return _attachmentsFolderId;
096            }
097    
098            public String getBody(boolean translate) {
099                    String body = null;
100    
101                    if (translate) {
102                            body = BBCodeTranslatorUtil.getHTML(getBody());
103                    }
104                    else {
105                            body = getBody();
106                    }
107    
108                    return body;
109            }
110    
111            public MBCategory getCategory() throws PortalException, SystemException {
112                    return MBCategoryLocalServiceUtil.getCategory(getCategoryId());
113            }
114    
115            public List<FileEntry> getDeletedAttachmentsFileEntries()
116                    throws PortalException, SystemException {
117    
118                    return getDeletedAttachmentsFileEntries(
119                            QueryUtil.ALL_POS, QueryUtil.ALL_POS);
120            }
121    
122            public List<FileEntry> getDeletedAttachmentsFileEntries(int start, int end)
123                    throws PortalException, SystemException {
124    
125                    return PortletFileRepositoryUtil.getPortletFileEntries(
126                            getGroupId(), getAttachmentsFolderId(),
127                            WorkflowConstants.STATUS_IN_TRASH, start, end, null);
128            }
129    
130            public int getDeletedAttachmentsFileEntriesCount()
131                    throws PortalException, SystemException {
132    
133                    return PortletFileRepositoryUtil.getPortletFileEntriesCount(
134                            getGroupId(), getAttachmentsFolderId(),
135                            WorkflowConstants.STATUS_IN_TRASH);
136            }
137    
138            public MBThread getThread() throws PortalException, SystemException {
139                    return MBThreadLocalServiceUtil.getThread(getThreadId());
140            }
141    
142            public long getThreadAttachmentsFolderId()
143                    throws PortalException, SystemException {
144    
145                    return getThread().getAttachmentsFolderId();
146            }
147    
148            public String getWorkflowClassName() {
149                    if (isDiscussion()) {
150                            return MBDiscussion.class.getName();
151                    }
152                    else {
153                            return MBMessage.class.getName();
154                    }
155            }
156    
157            public boolean isDiscussion() {
158                    if (getCategoryId() == MBCategoryConstants.DISCUSSION_CATEGORY_ID) {
159                            return true;
160                    }
161                    else {
162                            return false;
163                    }
164            }
165    
166            public boolean isFormatBBCode() {
167                    String format = getFormat();
168    
169                    if (format.equals("bbcode")) {
170                            return true;
171                    }
172                    else {
173                            return false;
174                    }
175            }
176    
177            public boolean isInTrashThread() throws PortalException, SystemException {
178                    MBThread thread = getThread();
179    
180                    if (thread.isInTrash() || thread.isInTrashContainer()) {
181                            return true;
182                    }
183                    else {
184                            return false;
185                    }
186            }
187    
188            public boolean isReply() {
189                    return !isRoot();
190            }
191    
192            public boolean isRoot() {
193                    if (getParentMessageId() ==
194                                    MBMessageConstants.DEFAULT_PARENT_MESSAGE_ID) {
195    
196                            return true;
197                    }
198                    else {
199                            return false;
200                    }
201            }
202    
203            public void setAttachmentsFolderId(long attachmentsFolderId) {
204                    _attachmentsFolderId = attachmentsFolderId;
205            }
206    
207            private long _attachmentsFolderId;
208    
209    }