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.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.parsers.bbcode.BBCodeTranslatorUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.model.CompanyConstants;
022    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
023    import com.liferay.portlet.documentlibrary.NoSuchDirectoryException;
024    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
025    import com.liferay.portlet.messageboards.model.MBCategory;
026    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
027    import com.liferay.portlet.messageboards.model.MBDiscussion;
028    import com.liferay.portlet.messageboards.model.MBMessage;
029    import com.liferay.portlet.messageboards.model.MBMessageConstants;
030    import com.liferay.portlet.messageboards.model.MBThread;
031    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
032    import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
033    import com.liferay.portlet.trash.util.TrashUtil;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class MBMessageImpl extends MBMessageBaseImpl {
039    
040            public MBMessageImpl() {
041            }
042    
043            public String[] getAssetTagNames() throws SystemException {
044                    return AssetTagLocalServiceUtil.getTagNames(
045                            MBMessage.class.getName(), getMessageId());
046            }
047    
048            public String getAttachmentsDir() {
049                    if (_attachmentDirs == null) {
050                            _attachmentDirs =
051                                    getThreadAttachmentsDir() + StringPool.FORWARD_SLASH +
052                                            getMessageId();
053                    }
054    
055                    return _attachmentDirs;
056            }
057    
058            public String[] getAttachmentsFiles()
059                    throws PortalException, SystemException {
060    
061                    String[] fileNames = new String[0];
062    
063                    try {
064                            fileNames = DLStoreUtil.getFileNames(
065                                    getCompanyId(), CompanyConstants.SYSTEM, getAttachmentsDir());
066                    }
067                    catch (NoSuchDirectoryException nsde) {
068                    }
069    
070                    return fileNames;
071            }
072    
073            public String getBody(boolean translate) {
074                    String body = null;
075    
076                    if (translate) {
077                            body = BBCodeTranslatorUtil.getHTML(getBody());
078                    }
079                    else {
080                            body = getBody();
081                    }
082    
083                    return body;
084            }
085    
086            public MBCategory getCategory() throws PortalException, SystemException {
087                    return MBCategoryLocalServiceUtil.getCategory(getCategoryId());
088            }
089    
090            public String getDeletedAttachmentsDir() {
091                    if (_deletedAttachmentDirs == null) {
092                            _deletedAttachmentDirs =
093                                    getThreadAttachmentsDir() + StringPool.FORWARD_SLASH +
094                                            TrashUtil.TRASH_ATTACHMENTS_DIR + getMessageId();
095                    }
096    
097                    return _deletedAttachmentDirs;
098            }
099    
100            public String[] getDeletedAttachmentsFiles()
101                    throws PortalException, SystemException {
102    
103                    String[] fileNames = new String[0];
104    
105                    try {
106                            fileNames = DLStoreUtil.getFileNames(
107                                    getCompanyId(), CompanyConstants.SYSTEM,
108                                    getDeletedAttachmentsDir());
109                    }
110                    catch (NoSuchDirectoryException nsde) {
111                    }
112    
113                    return fileNames;
114            }
115    
116            public MBThread getThread() throws PortalException, SystemException {
117                    return MBThreadLocalServiceUtil.getThread(getThreadId());
118            }
119    
120            public String getThreadAttachmentsDir() {
121                    return MBMessageConstants.BASE_ATTACHMENTS_DIR + getThreadId();
122            }
123    
124            public String getWorkflowClassName() {
125                    if (isDiscussion()) {
126                            return MBDiscussion.class.getName();
127                    }
128                    else {
129                            return MBMessage.class.getName();
130                    }
131            }
132    
133            public boolean isDiscussion() {
134                    if (getCategoryId() == MBCategoryConstants.DISCUSSION_CATEGORY_ID) {
135                            return true;
136                    }
137                    else {
138                            return false;
139                    }
140            }
141    
142            public boolean isFormatBBCode() {
143                    String format = getFormat();
144    
145                    if (format.equals("bbcode")) {
146                            return true;
147                    }
148                    else {
149                            return false;
150                    }
151            }
152    
153            public boolean isReply() {
154                    return !isRoot();
155            }
156    
157            public boolean isRoot() {
158                    if (getParentMessageId() ==
159                                    MBMessageConstants.DEFAULT_PARENT_MESSAGE_ID) {
160    
161                            return true;
162                    }
163                    else {
164                            return false;
165                    }
166            }
167    
168            public void setAttachmentsDir(String attachmentsDir) {
169                    _attachmentDirs = attachmentsDir;
170            }
171    
172            private String _attachmentDirs;
173            private String _deletedAttachmentDirs;
174    
175    }