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.portal.util.PortalUtil;
33  import com.liferay.portlet.messageboards.model.MBCategory;
34  import com.liferay.portlet.messageboards.model.MBMessage;
35  import com.liferay.portlet.messageboards.model.MBThread;
36  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
37  import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
38  import com.liferay.portlet.messageboards.util.BBCodeUtil;
39  import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
40  
41  import java.rmi.RemoteException;
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 MBThread getThread() throws PortalException, SystemException {
86          return MBThreadLocalServiceUtil.getThread(getThreadId());
87      }
88  
89      public boolean isRoot() {
90          if (getParentMessageId() == DEFAULT_PARENT_MESSAGE_ID) {
91              return true;
92          }
93          else {
94              return false;
95          }
96      }
97  
98      public boolean isReply() {
99          return !isRoot();
100     }
101 
102     public boolean isDiscussion() {
103         if (getCategoryId() == CompanyConstants.SYSTEM) {
104             return true;
105         }
106         else {
107             return false;
108         }
109     }
110 
111     public String getBody(boolean translate) {
112         String body = null;
113 
114         if (translate) {
115             body = BBCodeUtil.getHTML(this);
116         }
117         else {
118             body = getBody();
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 String[] getTagsEntries() throws SystemException {
159         return TagsEntryLocalServiceUtil.getEntryNames(
160             MBMessage.class.getName(), getMessageId());
161     }
162 
163     private static Log _log = LogFactoryUtil.getLog(MBMessageImpl.class);
164 
165     private String _userUuid;
166     private String _attachmentDirs;
167 
168 }