1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.messageboards.model.impl;
24  
25  import com.liferay.documentlibrary.NoSuchDirectoryException;
26  import com.liferay.documentlibrary.service.DLServiceUtil;
27  import com.liferay.portal.PortalException;
28  import com.liferay.portal.SystemException;
29  import com.liferay.portal.kernel.log.Log;
30  import com.liferay.portal.kernel.log.LogFactoryUtil;
31  import com.liferay.portal.model.CompanyConstants;
32  import com.liferay.portlet.messageboards.model.MBCategory;
33  import com.liferay.portlet.messageboards.model.MBMessage;
34  import com.liferay.portlet.messageboards.model.MBThread;
35  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
36  import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
37  import com.liferay.portlet.messageboards.util.BBCodeUtil;
38  import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
39  
40  /**
41   * <a href="MBMessageImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   */
45  public class MBMessageImpl extends MBMessageModelImpl implements MBMessage {
46  
47      public static final long DEFAULT_PARENT_MESSAGE_ID = 0;
48  
49      public MBMessageImpl() {
50      }
51  
52      public String getAttachmentsDir() {
53          if (_attachmentDirs == null) {
54              _attachmentDirs = getThreadAttachmentsDir() + "/" + getMessageId();
55          }
56  
57          return _attachmentDirs;
58      }
59  
60      public String[] getAttachmentsFiles()
61          throws PortalException, SystemException {
62  
63          String[] fileNames = new String[0];
64  
65          try {
66              fileNames = DLServiceUtil.getFileNames(
67                  getCompanyId(), CompanyConstants.SYSTEM, getAttachmentsDir());
68          }
69          catch (NoSuchDirectoryException nsde) {
70          }
71  
72          return fileNames;
73      }
74  
75      public String getBody(boolean translate) {
76          String body = null;
77  
78          if (translate) {
79              body = BBCodeUtil.getHTML(this);
80          }
81          else {
82              body = getBody();
83          }
84  
85          return body;
86      }
87  
88      public MBCategory getCategory() {
89          MBCategory category = null;
90  
91          try {
92              if (getCategoryId() == CompanyConstants.SYSTEM) {
93                  category = MBCategoryLocalServiceUtil.getSystemCategory();
94              }
95              else {
96                  category = MBCategoryLocalServiceUtil.getCategory(
97                      getCategoryId());
98              }
99          }
100         catch (Exception e) {
101             category = new MBCategoryImpl();
102 
103             _log.error(e);
104         }
105 
106         return category;
107     }
108 
109     public String[] getTagsEntries() throws SystemException {
110         return TagsEntryLocalServiceUtil.getEntryNames(
111             MBMessage.class.getName(), getMessageId());
112     }
113 
114     public MBThread getThread() throws PortalException, SystemException {
115         return MBThreadLocalServiceUtil.getThread(getThreadId());
116     }
117 
118     public String getThreadAttachmentsDir() {
119         return "messageboards/" + getThreadId();
120     }
121 
122     public boolean isDiscussion() {
123         if (getCategoryId() == CompanyConstants.SYSTEM) {
124             return true;
125         }
126         else {
127             return false;
128         }
129     }
130 
131     public boolean isReply() {
132         return !isRoot();
133     }
134 
135     public boolean isRoot() {
136         if (getParentMessageId() == DEFAULT_PARENT_MESSAGE_ID) {
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 }