001    /**
002     * Copyright (c) 2000-2010 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.documentlibrary.NoSuchDirectoryException;
018    import com.liferay.documentlibrary.service.DLServiceUtil;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.model.CompanyConstants;
024    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
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.messageboards.util.BBCodeUtil;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class MBMessageImpl extends MBMessageModelImpl implements MBMessage {
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 = getThreadAttachmentsDir() + "/" + getMessageId();
051                    }
052    
053                    return _attachmentDirs;
054            }
055    
056            public String[] getAttachmentsFiles()
057                    throws PortalException, SystemException {
058    
059                    String[] fileNames = new String[0];
060    
061                    try {
062                            fileNames = DLServiceUtil.getFileNames(
063                                    getCompanyId(), CompanyConstants.SYSTEM, getAttachmentsDir());
064                    }
065                    catch (NoSuchDirectoryException nsde) {
066                    }
067    
068                    return fileNames;
069            }
070    
071            public String getBody(boolean translate) {
072                    String body = null;
073    
074                    if (translate) {
075                            body = BBCodeUtil.getHTML(this);
076                    }
077                    else {
078                            body = getBody();
079                    }
080    
081                    return body;
082            }
083    
084            public MBCategory getCategory() {
085                    MBCategory category = null;
086    
087                    long categoryId = getCategoryId();
088    
089                    try {
090                            category = MBCategoryLocalServiceUtil.getCategory(categoryId);
091                    }
092                    catch (Exception e) {
093                            category = new MBCategoryImpl();
094    
095                            category.setCategoryId(getCategoryId());
096    
097                            _log.error(e);
098                    }
099    
100                    return category;
101            }
102    
103            public MBThread getThread() throws PortalException, SystemException {
104                    return MBThreadLocalServiceUtil.getThread(getThreadId());
105            }
106    
107            public String getThreadAttachmentsDir() {
108                    return "messageboards/" + getThreadId();
109            }
110    
111            public String getWorkflowClassName() {
112                    if (isDiscussion()) {
113                            return MBDiscussion.class.getName();
114                    }
115                    else {
116                            return MBMessage.class.getName();
117                    }
118            }
119    
120            public boolean isDiscussion() {
121                    if (getCategoryId() == MBCategoryConstants.DISCUSSION_CATEGORY_ID) {
122                            return true;
123                    }
124                    else {
125                            return false;
126                    }
127            }
128    
129            public boolean isReply() {
130                    return !isRoot();
131            }
132    
133            public boolean isRoot() {
134                    if (getParentMessageId() ==
135                                    MBMessageConstants.DEFAULT_PARENT_MESSAGE_ID) {
136    
137                            return true;
138                    }
139                    else {
140                            return false;
141                    }
142            }
143    
144            public void setAttachmentsDir(String attachmentsDir) {
145                    _attachmentDirs = attachmentsDir;
146            }
147    
148            private static Log _log = LogFactoryUtil.getLog(MBMessageImpl.class);
149    
150            private String _attachmentDirs;
151    
152    }