1   /**
2    * Copyright (c) 2000-2008 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.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.model.impl.CompanyImpl;
28  import com.liferay.portal.util.PortalUtil;
29  import com.liferay.portlet.messageboards.model.MBCategory;
30  import com.liferay.portlet.messageboards.model.MBMessage;
31  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
32  import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
33  import com.liferay.portlet.messageboards.util.BBCodeUtil;
34  import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  
39  /**
40   * <a href="MBMessageImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
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 getUserUuid() throws SystemException {
53          return PortalUtil.getUserValue(getUserId(), "uuid", _userUuid);
54      }
55  
56      public void setUserUuid(String userUuid) {
57          _userUuid = userUuid;
58      }
59  
60      public MBCategory getCategory() {
61          MBCategory category = null;
62  
63          try {
64              if (getCategoryId() == CompanyImpl.SYSTEM) {
65                  category = MBCategoryLocalServiceUtil.getSystemCategory();
66              }
67              else {
68                  category = MBCategoryLocalServiceUtil.getCategory(
69                      getCategoryId());
70              }
71          }
72          catch (Exception e) {
73              category = new MBCategoryImpl();
74  
75              _log.error(e);
76          }
77  
78          return category;
79      }
80  
81      public boolean isRoot() {
82          if (getParentMessageId() == DEFAULT_PARENT_MESSAGE_ID) {
83              return true;
84          }
85          else {
86              return false;
87          }
88      }
89  
90      public boolean isReply() {
91          return !isRoot();
92      }
93  
94      public boolean isDiscussion() {
95          if (getCategoryId() == CompanyImpl.SYSTEM) {
96              return true;
97          }
98          else {
99              return false;
100         }
101     }
102 
103     public double getPriority() throws PortalException, SystemException {
104         if (_priority == -1) {
105             _priority = MBThreadLocalServiceUtil.getThread(getThreadId()).
106                 getPriority();
107         }
108         return _priority;
109     }
110 
111     public void setPriority(double priority) {
112         _priority = priority;
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         return _attachmentDirs;
124     }
125 
126     public void setAttachmentsDir(String attachmentsDir) {
127         _attachmentDirs = attachmentsDir;
128     }
129 
130     public String getBody(boolean translated) {
131         if (translated) {
132             return BBCodeUtil.getHTML(getBody());
133         }
134         else {
135             return getBody();
136         }
137     }
138 
139     public String[] getTagsEntries() throws PortalException, SystemException {
140         return TagsEntryLocalServiceUtil.getEntryNames(
141             MBMessage.class.getName(), getMessageId());
142     }
143 
144     private static Log _log = LogFactory.getLog(MBMessageImpl.class);
145 
146     private String _userUuid;
147     private double _priority = -1;
148     private String _attachmentDirs;
149 
150 }