1   /**
2    * Copyright (c) 2000-2007 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.service.impl;
24  
25  import com.liferay.counter.service.CounterLocalServiceUtil;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.kernel.search.Hits;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.lucene.LuceneFields;
32  import com.liferay.portal.lucene.LuceneUtil;
33  import com.liferay.portal.model.User;
34  import com.liferay.portal.model.impl.ResourceImpl;
35  import com.liferay.portal.service.ResourceLocalServiceUtil;
36  import com.liferay.portal.service.persistence.UserUtil;
37  import com.liferay.portal.util.PortalUtil;
38  import com.liferay.portlet.wiki.NodeNameException;
39  import com.liferay.portlet.wiki.model.WikiNode;
40  import com.liferay.portlet.wiki.model.WikiPage;
41  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
42  import com.liferay.portlet.wiki.service.base.WikiNodeLocalServiceBaseImpl;
43  import com.liferay.portlet.wiki.service.persistence.WikiNodeUtil;
44  import com.liferay.portlet.wiki.service.persistence.WikiPageUtil;
45  import com.liferay.portlet.wiki.util.Indexer;
46  import com.liferay.util.lucene.HitsImpl;
47  
48  import java.io.IOException;
49  
50  import java.util.Date;
51  import java.util.Iterator;
52  import java.util.List;
53  
54  import org.apache.commons.logging.Log;
55  import org.apache.commons.logging.LogFactory;
56  import org.apache.lucene.document.Document;
57  import org.apache.lucene.index.IndexWriter;
58  import org.apache.lucene.index.Term;
59  import org.apache.lucene.queryParser.ParseException;
60  import org.apache.lucene.search.BooleanClause;
61  import org.apache.lucene.search.BooleanQuery;
62  import org.apache.lucene.search.Searcher;
63  import org.apache.lucene.search.TermQuery;
64  
65  /**
66   * <a href="WikiNodeLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
67   *
68   * @author Brian Wing Shun Chan
69   * @author Charles May
70   *
71   */
72  public class WikiNodeLocalServiceImpl extends WikiNodeLocalServiceBaseImpl {
73  
74      public WikiNode addNode(
75              long userId, long plid, String name, String description,
76              boolean addCommunityPermissions, boolean addGuestPermissions)
77          throws PortalException, SystemException {
78  
79          return addNode(
80              userId, plid, name, description,
81              Boolean.valueOf(addCommunityPermissions),
82              Boolean.valueOf(addGuestPermissions), null, null);
83      }
84  
85      public WikiNode addNode(
86              long userId, long plid, String name, String description,
87              String[] communityPermissions, String[] guestPermissions)
88          throws PortalException, SystemException {
89  
90          return addNode(
91              userId, plid, name, description, null, null, communityPermissions,
92              guestPermissions);
93      }
94  
95      public WikiNode addNode(
96              long userId, long plid, String name, String description,
97              Boolean addCommunityPermissions, Boolean addGuestPermissions,
98              String[] communityPermissions, String[] guestPermissions)
99          throws PortalException, SystemException {
100 
101         // Node
102 
103         User user = UserUtil.findByPrimaryKey(userId);
104         long groupId = PortalUtil.getPortletGroupId(plid);
105         Date now = new Date();
106 
107         validate(name);
108 
109         long nodeId = CounterLocalServiceUtil.increment();
110 
111         WikiNode node = WikiNodeUtil.create(nodeId);
112 
113         node.setGroupId(groupId);
114         node.setCompanyId(user.getCompanyId());
115         node.setUserId(user.getUserId());
116         node.setUserName(user.getFullName());
117         node.setCreateDate(now);
118         node.setModifiedDate(now);
119         node.setName(name);
120         node.setDescription(description);
121 
122         WikiNodeUtil.update(node);
123 
124         // Resources
125 
126         if ((addCommunityPermissions != null) &&
127             (addGuestPermissions != null)) {
128 
129             addNodeResources(
130                 node, addCommunityPermissions.booleanValue(),
131                 addGuestPermissions.booleanValue());
132         }
133         else {
134             addNodeResources(node, communityPermissions, guestPermissions);
135         }
136 
137         return node;
138     }
139 
140     public void addNodeResources(
141             long nodeId, boolean addCommunityPermissions,
142             boolean addGuestPermissions)
143         throws PortalException, SystemException {
144 
145         WikiNode node = WikiNodeUtil.findByPrimaryKey(nodeId);
146 
147         addNodeResources(node, addCommunityPermissions, addGuestPermissions);
148     }
149 
150     public void addNodeResources(
151             WikiNode node, boolean addCommunityPermissions,
152             boolean addGuestPermissions)
153         throws PortalException, SystemException {
154 
155         ResourceLocalServiceUtil.addResources(
156             node.getCompanyId(), node.getGroupId(), node.getUserId(),
157             WikiNode.class.getName(), node.getNodeId(), false,
158             addCommunityPermissions, addGuestPermissions);
159     }
160 
161     public void addNodeResources(
162             long nodeId, String[] communityPermissions,
163             String[] guestPermissions)
164         throws PortalException, SystemException {
165 
166         WikiNode node = WikiNodeUtil.findByPrimaryKey(nodeId);
167 
168         addNodeResources(node, communityPermissions, guestPermissions);
169     }
170 
171     public void addNodeResources(
172             WikiNode node, String[] communityPermissions,
173             String[] guestPermissions)
174         throws PortalException, SystemException {
175 
176         ResourceLocalServiceUtil.addModelResources(
177             node.getCompanyId(), node.getGroupId(), node.getUserId(),
178             WikiNode.class.getName(), node.getNodeId(), communityPermissions,
179             guestPermissions);
180     }
181 
182     public void deleteNode(long nodeId)
183         throws PortalException, SystemException {
184 
185         WikiNode node = WikiNodeUtil.findByPrimaryKey(nodeId);
186 
187         deleteNode(node);
188     }
189 
190     public void deleteNode(WikiNode node)
191         throws PortalException, SystemException {
192 
193         // Lucene
194 
195         try {
196             Indexer.deletePages(node.getCompanyId(), node.getNodeId());
197         }
198         catch (IOException ioe) {
199             _log.error("Deleting index " + node.getNodeId(), ioe);
200         }
201         catch (ParseException pe) {
202             _log.error("Deleting index " + node.getNodeId(), pe);
203         }
204 
205         // Pages
206 
207         WikiPageLocalServiceUtil.deletePages(node.getNodeId());
208 
209         // Resources
210 
211         ResourceLocalServiceUtil.deleteResource(
212             node.getCompanyId(), WikiNode.class.getName(),
213             ResourceImpl.SCOPE_INDIVIDUAL, node.getNodeId());
214 
215         // Node
216 
217         WikiNodeUtil.remove(node.getNodeId());
218     }
219 
220     public void deleteNodes(long groupId)
221         throws PortalException, SystemException {
222 
223         Iterator itr = WikiNodeUtil.findByGroupId(groupId).iterator();
224 
225         while (itr.hasNext()) {
226             WikiNode node = (WikiNode)itr.next();
227 
228             deleteNode(node);
229         }
230     }
231 
232     public WikiNode getNode(long nodeId)
233         throws PortalException, SystemException {
234 
235         return WikiNodeUtil.findByPrimaryKey(nodeId);
236     }
237 
238     public List getNodes(long groupId) throws SystemException {
239         return WikiNodeUtil.findByGroupId(groupId);
240     }
241 
242     public List getNodes(long groupId, int begin, int end)
243         throws SystemException {
244 
245         return WikiNodeUtil.findByGroupId(groupId, begin, end);
246     }
247 
248     public int getNodesCount(long groupId) throws SystemException {
249         return WikiNodeUtil.countByGroupId(groupId);
250     }
251 
252     public void reIndex(String[] ids) throws SystemException {
253         long companyId = GetterUtil.getLong(ids[0]);
254 
255         IndexWriter writer = null;
256 
257         try {
258             writer = LuceneUtil.getWriter(companyId);
259 
260             Iterator itr1 = WikiNodeUtil.findByCompanyId(companyId).iterator();
261 
262             while (itr1.hasNext()) {
263                 WikiNode node = (WikiNode)itr1.next();
264 
265                 long nodeId = node.getNodeId();
266 
267                 Iterator itr2 = WikiPageUtil.findByNodeId(nodeId).iterator();
268 
269                 while (itr2.hasNext()) {
270                     WikiPage page = (WikiPage)itr2.next();
271 
272                     long groupId = node.getGroupId();
273                     String title = page.getTitle();
274                     String content = page.getContent();
275 
276                     try {
277                         Document doc = Indexer.getAddPageDocument(
278                             companyId, groupId, nodeId, title, content);
279 
280                         writer.addDocument(doc);
281                     }
282                     catch (Exception e1) {
283                         _log.error("Reindexing " + page.getPrimaryKey(), e1);
284                     }
285                 }
286             }
287         }
288         catch (SystemException se) {
289             throw se;
290         }
291         catch (Exception e2) {
292             throw new SystemException(e2);
293         }
294         finally {
295             try {
296                 if (writer != null) {
297                     LuceneUtil.write(companyId);
298                 }
299             }
300             catch (Exception e) {
301                 _log.error(e);
302             }
303         }
304     }
305 
306     public Hits search(
307             long companyId, long groupId, long[] nodeIds, String keywords)
308         throws SystemException {
309 
310         Searcher searcher = null;
311 
312         try {
313             HitsImpl hits = new HitsImpl();
314 
315             if (Validator.isNull(keywords)) {
316                 return hits;
317             }
318 
319             BooleanQuery contextQuery = new BooleanQuery();
320 
321             LuceneUtil.addRequiredTerm(
322                 contextQuery, LuceneFields.PORTLET_ID, Indexer.PORTLET_ID);
323 
324             if (groupId > 0) {
325                 LuceneUtil.addRequiredTerm(
326                     contextQuery, LuceneFields.GROUP_ID, groupId);
327             }
328 
329             if ((nodeIds != null) && (nodeIds.length > 0)) {
330                 BooleanQuery nodeIdsQuery = new BooleanQuery();
331 
332                 for (int i = 0; i < nodeIds.length; i++) {
333                     Term term = new Term("nodeId", String.valueOf(nodeIds[i]));
334                     TermQuery termQuery = new TermQuery(term);
335 
336                     nodeIdsQuery.add(termQuery, BooleanClause.Occur.SHOULD);
337                 }
338 
339                 contextQuery.add(nodeIdsQuery, BooleanClause.Occur.MUST);
340             }
341 
342             BooleanQuery searchQuery = new BooleanQuery();
343 
344             LuceneUtil.addTerm(searchQuery, LuceneFields.CONTENT, keywords);
345 
346             BooleanQuery fullQuery = new BooleanQuery();
347 
348             fullQuery.add(contextQuery, BooleanClause.Occur.MUST);
349             fullQuery.add(searchQuery, BooleanClause.Occur.MUST);
350 
351             searcher = LuceneUtil.getSearcher(companyId);
352 
353             hits.recordHits(searcher.search(fullQuery), searcher);
354 
355             return hits;
356         }
357         catch (Exception e) {
358             return LuceneUtil.closeSearcher(searcher, keywords, e);
359         }
360     }
361 
362     public WikiNode updateNode(long nodeId, String name, String description)
363         throws PortalException, SystemException {
364 
365         validate(name);
366 
367         WikiNode node = WikiNodeUtil.findByPrimaryKey(nodeId);
368 
369         node.setModifiedDate(new Date());
370         node.setName(name);
371         node.setDescription(description);
372 
373         WikiNodeUtil.update(node);
374 
375         return node;
376     }
377 
378     protected void validate(String name) throws PortalException {
379         if (!Validator.isName(name)) {
380             throw new NodeNameException();
381         }
382     }
383 
384     private static Log _log = LogFactory.getLog(WikiNodeLocalServiceImpl.class);
385 
386 }