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