1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.wiki.model.impl;
21  
22  import com.liferay.documentlibrary.NoSuchDirectoryException;
23  import com.liferay.documentlibrary.service.DLServiceUtil;
24  import com.liferay.portal.PortalException;
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.CompanyConstants;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portal.util.PropsKeys;
32  import com.liferay.portal.util.PropsUtil;
33  import com.liferay.portlet.wiki.model.WikiNode;
34  import com.liferay.portlet.wiki.model.WikiPage;
35  import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
36  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
37  
38  import java.rmi.RemoteException;
39  
40  import java.util.ArrayList;
41  import java.util.List;
42  
43  /**
44   * <a href="WikiPageImpl.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   * @author Jorge Ferrer
48   *
49   */
50  public class WikiPageImpl extends WikiPageModelImpl implements WikiPage {
51  
52      public static final String FRONT_PAGE =
53          PropsUtil.get(PropsKeys.WIKI_FRONT_PAGE_NAME);
54  
55      public static final double DEFAULT_VERSION = 1.0;
56  
57      public static final String DEFAULT_FORMAT =
58          PropsUtil.get(PropsKeys.WIKI_FORMATS_DEFAULT);
59  
60      public static final String[] FORMATS =
61          PropsUtil.getArray(PropsKeys.WIKI_FORMATS);
62  
63      public static final String MOVED = "Moved";
64  
65      public static final String NEW = "New";
66  
67      public static final String REVERTED = "Reverted";
68  
69      public WikiPageImpl() {
70      }
71  
72      public String getUserUuid() throws SystemException {
73          return PortalUtil.getUserValue(getUserId(), "uuid", _userUuid);
74      }
75  
76      public void setUserUuid(String userUuid) {
77          _userUuid = userUuid;
78      }
79  
80      public WikiNode getNode() {
81          WikiNode node = null;
82  
83          try {
84              node = WikiNodeLocalServiceUtil.getNode(getNodeId());
85          }
86          catch (Exception e) {
87              node = new WikiNodeImpl();
88  
89              _log.error(e);
90          }
91  
92          return node;
93      }
94  
95      public WikiPage getParentPage() {
96          if (Validator.isNull(getParentTitle())) {
97              return null;
98          }
99  
100         WikiPage page = null;
101 
102         try {
103             page = WikiPageLocalServiceUtil.getPage(
104                 getNodeId(), getParentTitle());
105         }
106         catch (Exception e) {
107             _log.error(e);
108         }
109 
110         return page;
111     }
112 
113     public List<WikiPage> getParentPages() {
114         List<WikiPage> parentPages = new ArrayList<WikiPage>();
115 
116         WikiPage parentPage = getParentPage();
117 
118         if (parentPage != null) {
119             parentPages.addAll(parentPage.getParentPages());
120             parentPages.add(parentPage);
121         }
122 
123         return parentPages;
124     }
125 
126     public List<WikiPage> getChildPages() {
127         List<WikiPage> pages = null;
128 
129         try {
130             pages = WikiPageLocalServiceUtil.getChildren(
131                 getNodeId(), true, getTitle());
132         }
133         catch (Exception e) {
134             pages = new ArrayList<WikiPage>();
135 
136             _log.error(e);
137         }
138 
139         return pages;
140     }
141 
142     public WikiPage getRedirectPage() {
143         if (Validator.isNull(getRedirectTitle())) {
144             return null;
145         }
146 
147         WikiPage page = null;
148 
149         try {
150             page = WikiPageLocalServiceUtil.getPage(
151                 getNodeId(), getRedirectTitle());
152         }
153         catch (Exception e) {
154             _log.error(e);
155         }
156 
157         return page;
158     }
159 
160     public String getAttachmentsDir() {
161         if (_attachmentDirs == null) {
162             _attachmentDirs = "wiki/" + getResourcePrimKey();
163         }
164 
165         return _attachmentDirs;
166     }
167 
168     public void setAttachmentsDir(String attachmentsDir) {
169         _attachmentDirs = attachmentsDir;
170     }
171 
172     public String[] getAttachmentsFiles()
173         throws PortalException, SystemException {
174 
175         String[] fileNames = new String[0];
176 
177         try {
178             fileNames = DLServiceUtil.getFileNames(
179                 getCompanyId(), CompanyConstants.SYSTEM, getAttachmentsDir());
180         }
181         catch (NoSuchDirectoryException nsde) {
182         }
183         catch (RemoteException re) {
184             _log.error(re);
185         }
186 
187         return fileNames;
188     }
189 
190     private static Log _log = LogFactoryUtil.getLog(WikiPageImpl.class);
191 
192     private String _userUuid;
193     private String _attachmentDirs;
194 
195 }