001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.wiki.service.impl;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.search.Hits;
026: import com.liferay.portal.kernel.util.GetterUtil;
027: import com.liferay.portal.kernel.util.Validator;
028: import com.liferay.portal.lucene.LuceneFields;
029: import com.liferay.portal.lucene.LuceneUtil;
030: import com.liferay.portal.model.User;
031: import com.liferay.portal.model.impl.ResourceImpl;
032: import com.liferay.portal.util.PortalUtil;
033: import com.liferay.portlet.wiki.NodeNameException;
034: import com.liferay.portlet.wiki.model.WikiNode;
035: import com.liferay.portlet.wiki.model.WikiPage;
036: import com.liferay.portlet.wiki.service.base.WikiNodeLocalServiceBaseImpl;
037: import com.liferay.portlet.wiki.util.Indexer;
038: import com.liferay.util.lucene.HitsImpl;
039:
040: import java.io.IOException;
041:
042: import java.util.Date;
043: import java.util.Iterator;
044: import java.util.List;
045:
046: import org.apache.commons.logging.Log;
047: import org.apache.commons.logging.LogFactory;
048: import org.apache.lucene.document.Document;
049: import org.apache.lucene.index.IndexWriter;
050: import org.apache.lucene.index.Term;
051: import org.apache.lucene.queryParser.ParseException;
052: import org.apache.lucene.search.BooleanClause;
053: import org.apache.lucene.search.BooleanQuery;
054: import org.apache.lucene.search.Searcher;
055: import org.apache.lucene.search.TermQuery;
056:
057: /**
058: * <a href="WikiNodeLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
059: *
060: * @author Brian Wing Shun Chan
061: * @author Charles May
062: *
063: */
064: public class WikiNodeLocalServiceImpl extends
065: WikiNodeLocalServiceBaseImpl {
066:
067: public WikiNode addNode(long userId, long plid, String name,
068: String description, boolean addCommunityPermissions,
069: boolean addGuestPermissions) throws PortalException,
070: SystemException {
071:
072: return addNode(null, userId, plid, name, description, Boolean
073: .valueOf(addCommunityPermissions), Boolean
074: .valueOf(addGuestPermissions), null, null);
075: }
076:
077: public WikiNode addNode(String uuid, long userId, long plid,
078: String name, String description,
079: boolean addCommunityPermissions, boolean addGuestPermissions)
080: throws PortalException, SystemException {
081:
082: return addNode(uuid, userId, plid, name, description, Boolean
083: .valueOf(addCommunityPermissions), Boolean
084: .valueOf(addGuestPermissions), null, null);
085: }
086:
087: public WikiNode addNode(long userId, long plid, String name,
088: String description, String[] communityPermissions,
089: String[] guestPermissions) throws PortalException,
090: SystemException {
091:
092: return addNode(null, userId, plid, name, description, null,
093: null, communityPermissions, guestPermissions);
094: }
095:
096: public WikiNode addNode(String uuid, long userId, long plid,
097: String name, String description,
098: Boolean addCommunityPermissions,
099: Boolean addGuestPermissions, String[] communityPermissions,
100: String[] guestPermissions) throws PortalException,
101: SystemException {
102:
103: // Node
104:
105: User user = userPersistence.findByPrimaryKey(userId);
106: long groupId = PortalUtil.getPortletGroupId(plid);
107: Date now = new Date();
108:
109: validate(name);
110:
111: long nodeId = counterLocalService.increment();
112:
113: WikiNode node = wikiNodePersistence.create(nodeId);
114:
115: node.setUuid(uuid);
116: node.setGroupId(groupId);
117: node.setCompanyId(user.getCompanyId());
118: node.setUserId(user.getUserId());
119: node.setUserName(user.getFullName());
120: node.setCreateDate(now);
121: node.setModifiedDate(now);
122: node.setName(name);
123: node.setDescription(description);
124:
125: wikiNodePersistence.update(node);
126:
127: // Resources
128:
129: if ((addCommunityPermissions != null)
130: && (addGuestPermissions != null)) {
131:
132: addNodeResources(node, addCommunityPermissions
133: .booleanValue(), addGuestPermissions.booleanValue());
134: } else {
135: addNodeResources(node, communityPermissions,
136: guestPermissions);
137: }
138:
139: return node;
140: }
141:
142: public void addNodeResources(long nodeId,
143: boolean addCommunityPermissions, boolean addGuestPermissions)
144: throws PortalException, SystemException {
145:
146: WikiNode node = wikiNodePersistence.findByPrimaryKey(nodeId);
147:
148: addNodeResources(node, addCommunityPermissions,
149: addGuestPermissions);
150: }
151:
152: public void addNodeResources(WikiNode node,
153: boolean addCommunityPermissions, boolean addGuestPermissions)
154: throws PortalException, SystemException {
155:
156: resourceLocalService.addResources(node.getCompanyId(), node
157: .getGroupId(), node.getUserId(), WikiNode.class
158: .getName(), node.getNodeId(), false,
159: addCommunityPermissions, addGuestPermissions);
160: }
161:
162: public void addNodeResources(long nodeId,
163: String[] communityPermissions, String[] guestPermissions)
164: throws PortalException, SystemException {
165:
166: WikiNode node = wikiNodePersistence.findByPrimaryKey(nodeId);
167:
168: addNodeResources(node, communityPermissions, guestPermissions);
169: }
170:
171: public void addNodeResources(WikiNode node,
172: String[] communityPermissions, String[] guestPermissions)
173: throws PortalException, SystemException {
174:
175: resourceLocalService.addModelResources(node.getCompanyId(),
176: node.getGroupId(), node.getUserId(), WikiNode.class
177: .getName(), node.getNodeId(),
178: communityPermissions, guestPermissions);
179: }
180:
181: public void deleteNode(long nodeId) throws PortalException,
182: SystemException {
183:
184: WikiNode node = wikiNodePersistence.findByPrimaryKey(nodeId);
185:
186: deleteNode(node);
187: }
188:
189: public void deleteNode(WikiNode node) throws PortalException,
190: SystemException {
191:
192: // Lucene
193:
194: try {
195: Indexer.deletePages(node.getCompanyId(), node.getNodeId());
196: } catch (IOException ioe) {
197: _log.error("Deleting index " + node.getNodeId(), ioe);
198: } catch (ParseException pe) {
199: _log.error("Deleting index " + node.getNodeId(), pe);
200: }
201:
202: // Pages
203:
204: wikiPageLocalService.deletePages(node.getNodeId());
205:
206: // Resources
207:
208: resourceLocalService.deleteResource(node.getCompanyId(),
209: WikiNode.class.getName(),
210: ResourceImpl.SCOPE_INDIVIDUAL, node.getNodeId());
211:
212: // Node
213:
214: wikiNodePersistence.remove(node.getNodeId());
215: }
216:
217: public void deleteNodes(long groupId) throws PortalException,
218: SystemException {
219:
220: Iterator itr = wikiNodePersistence.findByGroupId(groupId)
221: .iterator();
222:
223: while (itr.hasNext()) {
224: WikiNode node = (WikiNode) itr.next();
225:
226: deleteNode(node);
227: }
228: }
229:
230: public WikiNode getNode(long nodeId) throws PortalException,
231: SystemException {
232:
233: return wikiNodePersistence.findByPrimaryKey(nodeId);
234: }
235:
236: public List getNodes(long groupId) throws SystemException {
237: return wikiNodePersistence.findByGroupId(groupId);
238: }
239:
240: public List getNodes(long groupId, int begin, int end)
241: throws SystemException {
242:
243: return wikiNodePersistence.findByGroupId(groupId, begin, end);
244: }
245:
246: public int getNodesCount(long groupId) throws SystemException {
247: return wikiNodePersistence.countByGroupId(groupId);
248: }
249:
250: public void reIndex(String[] ids) throws SystemException {
251: if (LuceneUtil.INDEX_READ_ONLY) {
252: return;
253: }
254:
255: long companyId = GetterUtil.getLong(ids[0]);
256:
257: IndexWriter writer = null;
258:
259: try {
260: writer = LuceneUtil.getWriter(companyId);
261:
262: Iterator itr1 = wikiNodePersistence.findByCompanyId(
263: companyId).iterator();
264:
265: while (itr1.hasNext()) {
266: WikiNode node = (WikiNode) itr1.next();
267:
268: long nodeId = node.getNodeId();
269:
270: Iterator itr2 = wikiPagePersistence
271: .findByNodeId(nodeId).iterator();
272:
273: while (itr2.hasNext()) {
274: WikiPage page = (WikiPage) itr2.next();
275:
276: long groupId = node.getGroupId();
277: String title = page.getTitle();
278: String content = page.getContent();
279:
280: String[] tagsEntries = tagsEntryLocalService
281: .getEntryNames(WikiPage.class.getName(),
282: page.getResourcePrimKey());
283:
284: try {
285: Document doc = Indexer.getAddPageDocument(
286: companyId, groupId, nodeId, title,
287: content, tagsEntries);
288:
289: writer.addDocument(doc);
290: } catch (Exception e1) {
291: _log.error(
292: "Reindexing " + page.getPrimaryKey(),
293: e1);
294: }
295: }
296: }
297: } catch (SystemException se) {
298: throw se;
299: } catch (Exception e2) {
300: throw new SystemException(e2);
301: } finally {
302: try {
303: if (writer != null) {
304: LuceneUtil.write(companyId);
305: }
306: } catch (Exception e) {
307: _log.error(e);
308: }
309: }
310: }
311:
312: public Hits search(long companyId, long groupId, long[] nodeIds,
313: String keywords) throws SystemException {
314:
315: Searcher searcher = null;
316:
317: try {
318: HitsImpl hits = new HitsImpl();
319:
320: BooleanQuery contextQuery = new BooleanQuery();
321:
322: LuceneUtil.addRequiredTerm(contextQuery,
323: LuceneFields.PORTLET_ID, Indexer.PORTLET_ID);
324:
325: if (groupId > 0) {
326: LuceneUtil.addRequiredTerm(contextQuery,
327: LuceneFields.GROUP_ID, groupId);
328: }
329:
330: if ((nodeIds != null) && (nodeIds.length > 0)) {
331: BooleanQuery nodeIdsQuery = new BooleanQuery();
332:
333: for (int i = 0; i < nodeIds.length; i++) {
334: Term term = new Term("nodeId", String
335: .valueOf(nodeIds[i]));
336: TermQuery termQuery = new TermQuery(term);
337:
338: nodeIdsQuery.add(termQuery,
339: BooleanClause.Occur.SHOULD);
340: }
341:
342: contextQuery
343: .add(nodeIdsQuery, BooleanClause.Occur.MUST);
344: }
345:
346: BooleanQuery searchQuery = new BooleanQuery();
347:
348: if (Validator.isNotNull(keywords)) {
349: LuceneUtil.addTerm(searchQuery, LuceneFields.TITLE,
350: keywords);
351: LuceneUtil.addTerm(searchQuery, LuceneFields.CONTENT,
352: keywords);
353: LuceneUtil.addTerm(searchQuery, LuceneFields.TAG_ENTRY,
354: keywords);
355: }
356:
357: BooleanQuery fullQuery = new BooleanQuery();
358:
359: fullQuery.add(contextQuery, BooleanClause.Occur.MUST);
360:
361: if (searchQuery.clauses().size() > 0) {
362: fullQuery.add(searchQuery, BooleanClause.Occur.MUST);
363: }
364:
365: searcher = LuceneUtil.getSearcher(companyId);
366:
367: hits.recordHits(searcher.search(fullQuery), searcher);
368:
369: return hits;
370: } catch (Exception e) {
371: return LuceneUtil.closeSearcher(searcher, keywords, e);
372: }
373: }
374:
375: public WikiNode updateNode(long nodeId, String name,
376: String description) throws PortalException, SystemException {
377:
378: validate(name);
379:
380: WikiNode node = wikiNodePersistence.findByPrimaryKey(nodeId);
381:
382: node.setModifiedDate(new Date());
383: node.setName(name);
384: node.setDescription(description);
385:
386: wikiNodePersistence.update(node);
387:
388: return node;
389: }
390:
391: protected void validate(String name) throws PortalException {
392: if (!Validator.isName(name)) {
393: throw new NodeNameException();
394: }
395: }
396:
397: private static Log _log = LogFactory
398: .getLog(WikiNodeLocalServiceImpl.class);
399:
400: }
|