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.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.model.CompanyConstants;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portlet.messageboards.model.MBCategory;
32  import com.liferay.portlet.messageboards.model.MBMessage;
33  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
34  import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
35  import com.liferay.portlet.messageboards.util.BBCodeUtil;
36  import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
37  
38  import java.rmi.RemoteException;
39  
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  /**
44   * <a href="MBMessageImpl.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class MBMessageImpl extends MBMessageModelImpl implements MBMessage {
50  
51      public static final long DEFAULT_PARENT_MESSAGE_ID = 0;
52  
53      public MBMessageImpl() {
54      }
55  
56      public String getUserUuid() throws SystemException {
57          return PortalUtil.getUserValue(getUserId(), "uuid", _userUuid);
58      }
59  
60      public void setUserUuid(String userUuid) {
61          _userUuid = userUuid;
62      }
63  
64      public MBCategory getCategory() {
65          MBCategory category = null;
66  
67          try {
68              if (getCategoryId() == CompanyConstants.SYSTEM) {
69                  category = MBCategoryLocalServiceUtil.getSystemCategory();
70              }
71              else {
72                  category = MBCategoryLocalServiceUtil.getCategory(
73                      getCategoryId());
74              }
75          }
76          catch (Exception e) {
77              category = new MBCategoryImpl();
78  
79              _log.error(e);
80          }
81  
82          return category;
83      }
84  
85      public boolean isRoot() {
86          if (getParentMessageId() == DEFAULT_PARENT_MESSAGE_ID) {
87              return true;
88          }
89          else {
90              return false;
91          }
92      }
93  
94      public boolean isReply() {
95          return !isRoot();
96      }
97  
98      public boolean isDiscussion() {
99          if (getCategoryId() == CompanyConstants.SYSTEM) {
100             return true;
101         }
102         else {
103             return false;
104         }
105     }
106 
107     public String getBody(boolean translate) {
108         String body = getBody();
109 
110         if (translate) {
111             try {
112                 body = BBCodeUtil.getHTML(body);
113             }
114             catch (Exception e) {
115                 _log.error(
116                     "Could not parse message " + getMessageId() + " " +
117                         e.getMessage());
118             }
119         }
120 
121         return body;
122     }
123 
124     public String getThreadAttachmentsDir() {
125         return "messageboards/" + getThreadId();
126     }
127 
128     public String getAttachmentsDir() {
129         if (_attachmentDirs == null) {
130             _attachmentDirs = getThreadAttachmentsDir() + "/" + getMessageId();
131         }
132 
133         return _attachmentDirs;
134     }
135 
136     public void setAttachmentsDir(String attachmentsDir) {
137         _attachmentDirs = attachmentsDir;
138     }
139 
140     public String[] getAttachmentsFiles()
141         throws PortalException, SystemException {
142 
143         String[] fileNames = new String[0];
144 
145         try {
146             fileNames = DLServiceUtil.getFileNames(
147                 getCompanyId(), CompanyConstants.SYSTEM, getAttachmentsDir());
148         }
149         catch (NoSuchDirectoryException nsde) {
150         }
151         catch (RemoteException re) {
152             _log.error(re);
153         }
154 
155         return fileNames;
156     }
157 
158     public double getPriority() throws PortalException, SystemException {
159         if (_priority == -1) {
160             _priority = MBThreadLocalServiceUtil.getThread(getThreadId()).
161                 getPriority();
162         }
163 
164         return _priority;
165     }
166 
167     public void setPriority(double priority) {
168         _priority = priority;
169     }
170 
171     public String[] getTagsEntries() throws SystemException {
172         return TagsEntryLocalServiceUtil.getEntryNames(
173             MBMessage.class.getName(), getMessageId());
174     }
175 
176     private static Log _log = LogFactory.getLog(MBMessageImpl.class);
177 
178     private String _userUuid;
179     private double _priority = -1;
180     private String _attachmentDirs;
181 
182 }