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.wiki.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.util.Validator;
30  import com.liferay.portal.model.CompanyConstants;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portal.util.PropsKeys;
33  import com.liferay.portal.util.PropsUtil;
34  import com.liferay.portlet.wiki.model.WikiNode;
35  import com.liferay.portlet.wiki.model.WikiPage;
36  import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
37  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
38  
39  import java.rmi.RemoteException;
40  
41  import java.util.ArrayList;
42  import java.util.List;
43  
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  
47  /**
48   * <a href="WikiPageImpl.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   * @author Jorge Ferrer
52   *
53   */
54  public class WikiPageImpl extends WikiPageModelImpl implements WikiPage {
55  
56      public static final String FRONT_PAGE =
57          PropsUtil.get(PropsKeys.WIKI_FRONT_PAGE_NAME);
58  
59      public static final double DEFAULT_VERSION = 1.0;
60  
61      public static final String DEFAULT_FORMAT =
62          PropsUtil.get(PropsKeys.WIKI_FORMATS_DEFAULT);
63  
64      public static final String[] FORMATS =
65          PropsUtil.getArray(PropsKeys.WIKI_FORMATS);
66  
67      public static final String MOVED = "Moved";
68  
69      public static final String NEW = "New";
70  
71      public static final String REVERTED = "Reverted";
72  
73      public WikiPageImpl() {
74      }
75  
76      public String getUserUuid() throws SystemException {
77          return PortalUtil.getUserValue(getUserId(), "uuid", _userUuid);
78      }
79  
80      public void setUserUuid(String userUuid) {
81          _userUuid = userUuid;
82      }
83  
84      public WikiNode getNode() {
85          WikiNode node = null;
86  
87          try {
88              node = WikiNodeLocalServiceUtil.getNode(getNodeId());
89          }
90          catch (Exception e) {
91              node = new WikiNodeImpl();
92  
93              _log.error(e);
94          }
95  
96          return node;
97      }
98  
99      public WikiPage getParentPage() {
100         if (Validator.isNull(getParentTitle())) {
101             return null;
102         }
103 
104         WikiPage page = null;
105 
106         try {
107             page = WikiPageLocalServiceUtil.getPage(
108                 getNodeId(), getParentTitle());
109         }
110         catch (Exception e) {
111             _log.error(e);
112         }
113 
114         return page;
115     }
116 
117     public List<WikiPage> getParentPages() {
118         List<WikiPage> parentPages = new ArrayList<WikiPage>();
119 
120         WikiPage parentPage = getParentPage();
121 
122         if (parentPage != null) {
123             parentPages.addAll(parentPage.getParentPages());
124             parentPages.add(parentPage);
125         }
126 
127         return parentPages;
128     }
129 
130     public List<WikiPage> getChildPages() {
131         List<WikiPage> pages = null;
132 
133         try {
134             pages = WikiPageLocalServiceUtil.getChildren(
135                 getNodeId(), true, getTitle());
136         }
137         catch (Exception e) {
138             pages = new ArrayList<WikiPage>();
139 
140             _log.error(e);
141         }
142 
143         return pages;
144     }
145 
146     public WikiPage getRedirectPage() {
147         if (Validator.isNull(getRedirectTitle())) {
148             return null;
149         }
150 
151         WikiPage page = null;
152 
153         try {
154             page = WikiPageLocalServiceUtil.getPage(
155                 getNodeId(), getRedirectTitle());
156         }
157         catch (Exception e) {
158             _log.error(e);
159         }
160 
161         return page;
162     }
163 
164     public String getAttachmentsDir() {
165         if (_attachmentDirs == null) {
166             _attachmentDirs = "wiki/" + getResourcePrimKey();
167         }
168 
169         return _attachmentDirs;
170     }
171 
172     public void setAttachmentsDir(String attachmentsDir) {
173         _attachmentDirs = attachmentsDir;
174     }
175 
176     public String[] getAttachmentsFiles()
177         throws PortalException, SystemException {
178 
179         String[] fileNames = new String[0];
180 
181         try {
182             fileNames = DLServiceUtil.getFileNames(
183                 getCompanyId(), CompanyConstants.SYSTEM, getAttachmentsDir());
184         }
185         catch (NoSuchDirectoryException nsde) {
186         }
187         catch (RemoteException re) {
188             _log.error(re);
189         }
190 
191         return fileNames;
192     }
193 
194     private static Log _log = LogFactory.getLog(WikiPageImpl.class);
195 
196     private String _userUuid;
197     private String _attachmentDirs;
198 
199 }