1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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  import java.rmi.RemoteException;
41  
42  /**
43   * <a href="MBMessageImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class MBMessageImpl extends MBMessageModelImpl implements MBMessage {
49  
50      public static final long DEFAULT_PARENT_MESSAGE_ID = 0;
51  
52      public MBMessageImpl() {
53      }
54  
55      public MBCategory getCategory() {
56          MBCategory category = null;
57  
58          try {
59              if (getCategoryId() == CompanyConstants.SYSTEM) {
60                  category = MBCategoryLocalServiceUtil.getSystemCategory();
61              }
62              else {
63                  category = MBCategoryLocalServiceUtil.getCategory(
64                      getCategoryId());
65              }
66          }
67          catch (Exception e) {
68              category = new MBCategoryImpl();
69  
70              _log.error(e);
71          }
72  
73          return category;
74      }
75  
76      public MBThread getThread() throws PortalException, SystemException {
77          return MBThreadLocalServiceUtil.getThread(getThreadId());
78      }
79  
80      public boolean isRoot() {
81          if (getParentMessageId() == DEFAULT_PARENT_MESSAGE_ID) {
82              return true;
83          }
84          else {
85              return false;
86          }
87      }
88  
89      public boolean isReply() {
90          return !isRoot();
91      }
92  
93      public boolean isDiscussion() {
94          if (getCategoryId() == CompanyConstants.SYSTEM) {
95              return true;
96          }
97          else {
98              return false;
99          }
100     }
101 
102     public String getBody(boolean translate) {
103         String body = null;
104 
105         if (translate) {
106             body = BBCodeUtil.getHTML(this);
107         }
108         else {
109             body = getBody();
110         }
111 
112         return body;
113     }
114 
115     public String getThreadAttachmentsDir() {
116         return "messageboards/" + getThreadId();
117     }
118 
119     public String getAttachmentsDir() {
120         if (_attachmentDirs == null) {
121             _attachmentDirs = getThreadAttachmentsDir() + "/" + getMessageId();
122         }
123 
124         return _attachmentDirs;
125     }
126 
127     public void setAttachmentsDir(String attachmentsDir) {
128         _attachmentDirs = attachmentsDir;
129     }
130 
131     public String[] getAttachmentsFiles()
132         throws PortalException, SystemException {
133 
134         String[] fileNames = new String[0];
135 
136         try {
137             fileNames = DLServiceUtil.getFileNames(
138                 getCompanyId(), CompanyConstants.SYSTEM, getAttachmentsDir());
139         }
140         catch (NoSuchDirectoryException nsde) {
141         }
142         catch (RemoteException re) {
143             _log.error(re);
144         }
145 
146         return fileNames;
147     }
148 
149     public String[] getTagsEntries() throws SystemException {
150         return TagsEntryLocalServiceUtil.getEntryNames(
151             MBMessage.class.getName(), getMessageId());
152     }
153 
154     private static Log _log = LogFactoryUtil.getLog(MBMessageImpl.class);
155 
156     private String _attachmentDirs;
157 
158 }