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