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.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   */
46  public class MBMessageImpl extends MBMessageModelImpl implements MBMessage {
47  
48      public static final long DEFAULT_PARENT_MESSAGE_ID = 0;
49  
50      public MBMessageImpl() {
51      }
52  
53      public String getUserUuid() throws SystemException {
54          return PortalUtil.getUserValue(getUserId(), "uuid", _userUuid);
55      }
56  
57      public void setUserUuid(String userUuid) {
58          _userUuid = userUuid;
59      }
60  
61      public MBCategory getCategory() {
62          MBCategory category = null;
63  
64          try {
65              if (getCategoryId() == CompanyConstants.SYSTEM) {
66                  category = MBCategoryLocalServiceUtil.getSystemCategory();
67              }
68              else {
69                  category = MBCategoryLocalServiceUtil.getCategory(
70                      getCategoryId());
71              }
72          }
73          catch (Exception e) {
74              category = new MBCategoryImpl();
75  
76              _log.error(e);
77          }
78  
79          return category;
80      }
81  
82      public boolean isRoot() {
83          if (getParentMessageId() == DEFAULT_PARENT_MESSAGE_ID) {
84              return true;
85          }
86          else {
87              return false;
88          }
89      }
90  
91      public boolean isReply() {
92          return !isRoot();
93      }
94  
95      public boolean isDiscussion() {
96          if (getCategoryId() == CompanyConstants.SYSTEM) {
97              return true;
98          }
99          else {
100             return false;
101         }
102     }
103 
104     public String getBody(boolean translate) {
105         String body = null;
106 
107         if (translate) {
108             body = BBCodeUtil.getHTML(this);
109         }
110         else {
111             body = getBody();
112         }
113 
114         return body;
115     }
116 
117     public String getThreadAttachmentsDir() {
118         return "messageboards/" + getThreadId();
119     }
120 
121     public String getAttachmentsDir() {
122         if (_attachmentDirs == null) {
123             _attachmentDirs = getThreadAttachmentsDir() + "/" + getMessageId();
124         }
125 
126         return _attachmentDirs;
127     }
128 
129     public void setAttachmentsDir(String attachmentsDir) {
130         _attachmentDirs = attachmentsDir;
131     }
132 
133     public String[] getAttachmentsFiles()
134         throws PortalException, SystemException {
135 
136         String[] fileNames = new String[0];
137 
138         try {
139             fileNames = DLServiceUtil.getFileNames(
140                 getCompanyId(), CompanyConstants.SYSTEM, getAttachmentsDir());
141         }
142         catch (NoSuchDirectoryException nsde) {
143         }
144 
145         return fileNames;
146     }
147 
148     public double getPriority() throws PortalException, SystemException {
149         if (_priority == -1) {
150             _priority = MBThreadLocalServiceUtil.getThread(getThreadId()).
151                 getPriority();
152         }
153 
154         return _priority;
155     }
156 
157     public void setPriority(double priority) {
158         _priority = priority;
159     }
160 
161     public String[] getTagsEntries() throws SystemException {
162         return TagsEntryLocalServiceUtil.getEntryNames(
163             MBMessage.class.getName(), getMessageId());
164     }
165 
166     private static Log _log = LogFactoryUtil.getLog(MBMessageImpl.class);
167 
168     private String _userUuid;
169     private double _priority = -1;
170     private String _attachmentDirs;
171 
172 }