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.util.CalendarFactoryUtil;
026: import com.liferay.portal.kernel.util.ContentTypes;
027: import com.liferay.portal.kernel.util.Validator;
028: import com.liferay.portal.model.User;
029: import com.liferay.portal.model.impl.ResourceImpl;
030: import com.liferay.portlet.wiki.NoSuchPageException;
031: import com.liferay.portlet.wiki.PageContentException;
032: import com.liferay.portlet.wiki.PageTitleException;
033: import com.liferay.portlet.wiki.model.WikiNode;
034: import com.liferay.portlet.wiki.model.WikiPage;
035: import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
036: import com.liferay.portlet.wiki.service.base.WikiPageLocalServiceBaseImpl;
037: import com.liferay.portlet.wiki.util.Indexer;
038: import com.liferay.portlet.wiki.util.NodeFilter;
039: import com.liferay.portlet.wiki.util.WikiUtil;
040: import com.liferay.portlet.wiki.util.comparator.PageCreateDateComparator;
041: import com.liferay.util.MathUtil;
042:
043: import java.io.IOException;
044:
045: import java.util.ArrayList;
046: import java.util.Calendar;
047: import java.util.Collections;
048: import java.util.Date;
049: import java.util.HashSet;
050: import java.util.Iterator;
051: import java.util.List;
052: import java.util.Map;
053: import java.util.Set;
054: import java.util.regex.Matcher;
055: import java.util.regex.Pattern;
056:
057: import org.apache.commons.logging.Log;
058: import org.apache.commons.logging.LogFactory;
059:
060: /**
061: * <a href="WikiPageLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
062: *
063: * @author Brian Wing Shun Chan
064: *
065: */
066: public class WikiPageLocalServiceImpl extends
067: WikiPageLocalServiceBaseImpl {
068:
069: public WikiPage addPage(long userId, long nodeId, String title)
070: throws PortalException, SystemException {
071:
072: double version = WikiPageImpl.DEFAULT_VERSION;
073: String content = null;
074: String format = WikiPageImpl.DEFAULT_FORMAT;
075: boolean head = true;
076: String[] tagsEntries = null;
077:
078: return addPage(null, userId, nodeId, title, version, content,
079: format, head, tagsEntries);
080: }
081:
082: public WikiPage addPage(long userId, long nodeId, String title,
083: double version, String content, String format,
084: boolean head, String[] tagsEntries) throws PortalException,
085: SystemException {
086:
087: return addPage(null, userId, nodeId, title, version, content,
088: format, head, tagsEntries);
089: }
090:
091: public WikiPage addPage(String uuid, long userId, long nodeId,
092: String title, double version, String content,
093: String format, boolean head, String[] tagsEntries)
094: throws PortalException, SystemException {
095:
096: // Page
097:
098: User user = userPersistence.findByPrimaryKey(userId);
099: WikiNode node = wikiNodePersistence.findByPrimaryKey(nodeId);
100:
101: Date now = new Date();
102:
103: validate(title, nodeId, content, format);
104:
105: long pageId = counterLocalService.increment();
106:
107: long resourcePrimKey = wikiPageResourceLocalService
108: .getPageResourcePrimKey(nodeId, title);
109:
110: WikiPage page = wikiPagePersistence.create(pageId);
111:
112: page.setUuid(uuid);
113: page.setResourcePrimKey(resourcePrimKey);
114: page.setCompanyId(user.getCompanyId());
115: page.setUserId(user.getUserId());
116: page.setUserName(user.getFullName());
117: page.setCreateDate(now);
118: page.setNodeId(nodeId);
119: page.setTitle(title);
120: page.setVersion(version);
121: page.setContent(content);
122: page.setFormat(format);
123: page.setHead(head);
124:
125: wikiPagePersistence.update(page);
126:
127: // Resources
128:
129: addPageResources(page.getNode(), page, true, true);
130:
131: // Tags
132:
133: updateTagsAsset(userId, page, tagsEntries);
134:
135: // Lucene
136:
137: try {
138: Indexer.addPage(page.getCompanyId(), node.getGroupId(),
139: nodeId, title, content, tagsEntries);
140: } catch (IOException ioe) {
141: _log.error("Indexing " + pageId, ioe);
142: }
143:
144: return page;
145: }
146:
147: public void addPageResources(long nodeId, String title,
148: boolean addCommunityPermissions, boolean addGuestPermissions)
149: throws PortalException, SystemException {
150:
151: WikiNode node = wikiNodePersistence.findByPrimaryKey(nodeId);
152: WikiPage page = getPage(nodeId, title);
153:
154: addPageResources(node, page, addCommunityPermissions,
155: addGuestPermissions);
156: }
157:
158: public void addPageResources(WikiNode node, WikiPage page,
159: boolean addCommunityPermissions, boolean addGuestPermissions)
160: throws PortalException, SystemException {
161:
162: resourceLocalService.addResources(page.getCompanyId(), node
163: .getGroupId(), page.getUserId(), WikiPage.class
164: .getName(), page.getResourcePrimKey(), false,
165: addCommunityPermissions, addGuestPermissions);
166: }
167:
168: public void addPageResources(long nodeId, String title,
169: String[] communityPermissions, String[] guestPermissions)
170: throws PortalException, SystemException {
171:
172: WikiNode node = wikiNodePersistence.findByPrimaryKey(nodeId);
173: WikiPage page = getPage(nodeId, title);
174:
175: addPageResources(node, page, communityPermissions,
176: guestPermissions);
177: }
178:
179: public void addPageResources(WikiNode node, WikiPage page,
180: String[] communityPermissions, String[] guestPermissions)
181: throws PortalException, SystemException {
182:
183: resourceLocalService.addModelResources(page.getCompanyId(),
184: node.getGroupId(), page.getUserId(), WikiPage.class
185: .getName(), page.getResourcePrimKey(),
186: communityPermissions, guestPermissions);
187: }
188:
189: public void deletePage(long nodeId, String title)
190: throws PortalException, SystemException {
191:
192: List pages = wikiPagePersistence.findByN_T_H(nodeId, title,
193: true, 0, 1);
194:
195: if (pages.size() > 0) {
196: WikiPage page = (WikiPage) pages.iterator().next();
197:
198: deletePage(page);
199: }
200: }
201:
202: public void deletePage(WikiPage page) throws PortalException,
203: SystemException {
204:
205: // Lucene
206:
207: try {
208: Indexer.deletePage(page.getCompanyId(), page.getNodeId(),
209: page.getTitle());
210: } catch (IOException ioe) {
211: _log.error("Deleting index " + page.getPrimaryKey(), ioe);
212: }
213:
214: // Tags
215:
216: tagsAssetLocalService.deleteAsset(WikiPage.class.getName(),
217: page.getResourcePrimKey());
218:
219: // Message boards
220:
221: mbMessageLocalService.deleteDiscussionMessages(WikiPage.class
222: .getName(), page.getResourcePrimKey());
223:
224: // Resources
225:
226: resourceLocalService.deleteResource(page.getCompanyId(),
227: WikiPage.class.getName(),
228: ResourceImpl.SCOPE_INDIVIDUAL, page
229: .getResourcePrimKey());
230:
231: // Resource
232:
233: wikiPageResourceLocalService.deletePageResource(page
234: .getNodeId(), page.getTitle());
235:
236: // All versions
237:
238: wikiPagePersistence.removeByN_T(page.getNodeId(), page
239: .getTitle());
240: }
241:
242: public void deletePages(long nodeId) throws PortalException,
243: SystemException {
244:
245: Iterator itr = wikiPagePersistence.findByN_H(nodeId, true)
246: .iterator();
247:
248: while (itr.hasNext()) {
249: WikiPage page = (WikiPage) itr.next();
250:
251: deletePage(page);
252: }
253: }
254:
255: public List getNoAssetPages() throws SystemException {
256: return wikiPageFinder.findByNoAssets();
257: }
258:
259: public List getLinks(long nodeId, String title)
260: throws SystemException {
261: List links = new ArrayList();
262:
263: List pages = wikiPagePersistence.findByN_H(nodeId, true);
264:
265: for (int i = 0; i < pages.size(); i++) {
266: WikiPage page = (WikiPage) pages.get(i);
267:
268: if (page.getFormat().equals(
269: WikiPageImpl.CLASSIC_WIKI_FORMAT)) {
270: NodeFilter filter = WikiUtil.getFilter(nodeId);
271:
272: try {
273: WikiUtil.convert(filter, page.getContent());
274:
275: if (filter.getTitles().get(title) != null) {
276: links.add(page);
277: }
278: } catch (IOException ioe) {
279: ioe.printStackTrace();
280: }
281: }
282: }
283:
284: Collections.sort(links);
285:
286: return links;
287: }
288:
289: public List getOrphans(long nodeId) throws SystemException {
290: List pageTitles = new ArrayList();
291:
292: List pages = wikiPagePersistence.findByN_H(nodeId, true);
293:
294: for (int i = 0; i < pages.size(); i++) {
295: WikiPage page = (WikiPage) pages.get(i);
296:
297: if (page.getFormat().equals(
298: WikiPageImpl.CLASSIC_WIKI_FORMAT)) {
299: NodeFilter filter = WikiUtil.getFilter(nodeId);
300:
301: try {
302: WikiUtil.convert(filter, page.getContent());
303:
304: pageTitles.add(filter.getTitles());
305: } catch (IOException ioe) {
306: ioe.printStackTrace();
307: }
308: }
309: }
310:
311: Set notOrphans = new HashSet();
312:
313: for (int i = 0; i < pages.size(); i++) {
314: WikiPage page = (WikiPage) pages.get(i);
315:
316: for (int j = 0; j < pageTitles.size(); j++) {
317: Map titles = (Map) pageTitles.get(j);
318:
319: if (titles.get(page.getTitle()) != null) {
320: notOrphans.add(page);
321:
322: break;
323: }
324: }
325: }
326:
327: List orphans = new ArrayList();
328:
329: for (int i = 0; i < pages.size(); i++) {
330: WikiPage page = (WikiPage) pages.get(i);
331:
332: if (!notOrphans.contains(page)) {
333: orphans.add(page);
334: }
335: }
336:
337: Collections.sort(orphans);
338:
339: return orphans;
340: }
341:
342: public WikiPage getPage(long nodeId, String title)
343: throws PortalException, SystemException {
344:
345: List pages = wikiPagePersistence.findByN_T_H(nodeId, title,
346: true, 0, 1);
347:
348: if (pages.size() > 0) {
349: return (WikiPage) pages.iterator().next();
350: } else {
351: throw new NoSuchPageException();
352: }
353: }
354:
355: public WikiPage getPage(long nodeId, String title, double version)
356: throws PortalException, SystemException {
357:
358: WikiPage page = null;
359:
360: if (version == 0) {
361: page = getPage(nodeId, title);
362: } else {
363: page = wikiPagePersistence.findByN_T_V(nodeId, title,
364: version);
365: }
366:
367: return page;
368: }
369:
370: public List getPages(long nodeId, int begin, int end)
371: throws SystemException {
372:
373: return wikiPagePersistence.findByNodeId(nodeId, begin, end,
374: new PageCreateDateComparator(false));
375: }
376:
377: public List getPages(long nodeId, String title, int begin, int end)
378: throws SystemException {
379:
380: return wikiPagePersistence.findByN_T(nodeId, title, begin, end,
381: new PageCreateDateComparator(false));
382: }
383:
384: public List getPages(long nodeId, boolean head, int begin, int end)
385: throws SystemException {
386:
387: return wikiPagePersistence.findByN_H(nodeId, head, begin, end,
388: new PageCreateDateComparator(false));
389: }
390:
391: public List getPages(long nodeId, String title, boolean head,
392: int begin, int end) throws SystemException {
393:
394: return wikiPagePersistence.findByN_T_H(nodeId, title, head,
395: begin, end, new PageCreateDateComparator(false));
396: }
397:
398: public int getPagesCount(long nodeId) throws SystemException {
399: return wikiPagePersistence.countByNodeId(nodeId);
400: }
401:
402: public int getPagesCount(long nodeId, String title)
403: throws SystemException {
404:
405: return wikiPagePersistence.countByN_T(nodeId, title);
406: }
407:
408: public int getPagesCount(long nodeId, boolean head)
409: throws SystemException {
410:
411: return wikiPagePersistence.countByN_H(nodeId, head);
412: }
413:
414: public int getPagesCount(long nodeId, String title, boolean head)
415: throws SystemException {
416:
417: return wikiPagePersistence.countByN_T_H(nodeId, title, head);
418: }
419:
420: public List getRecentChanges(long nodeId, int begin, int end)
421: throws SystemException {
422:
423: Calendar cal = CalendarFactoryUtil.getCalendar();
424:
425: cal.add(Calendar.WEEK_OF_YEAR, -1);
426:
427: return wikiPageFinder.findByCreateDate(nodeId, cal.getTime(),
428: false, begin, end);
429: }
430:
431: public int getRecentChangesCount(long nodeId)
432: throws SystemException {
433: Calendar cal = CalendarFactoryUtil.getCalendar();
434:
435: cal.add(Calendar.WEEK_OF_YEAR, -1);
436:
437: return wikiPageFinder.countByCreateDate(nodeId, cal.getTime(),
438: false);
439: }
440:
441: public WikiPage revertPage(long userId, long nodeId, String title,
442: double version) throws PortalException, SystemException {
443:
444: WikiPage oldPage = getPage(nodeId, title, version);
445:
446: return updatePage(userId, nodeId, title, oldPage.getContent(),
447: oldPage.getFormat(), null);
448: }
449:
450: public WikiPage updatePage(long userId, long nodeId, String title,
451: String content, String format, String[] tagsEntries)
452: throws PortalException, SystemException {
453:
454: // Page
455:
456: User user = userPersistence.findByPrimaryKey(userId);
457: Date now = new Date();
458:
459: validate(nodeId, content, format);
460:
461: WikiPage page = getPage(nodeId, title);
462:
463: long resourcePrimKey = page.getResourcePrimKey();
464:
465: page.setHead(false);
466:
467: wikiPagePersistence.update(page);
468:
469: double oldVersion = page.getVersion();
470: double newVersion = MathUtil.format(oldVersion + 0.1, 1, 1);
471:
472: long pageId = counterLocalService.increment();
473:
474: page = wikiPagePersistence.create(pageId);
475:
476: page.setResourcePrimKey(resourcePrimKey);
477: page.setCompanyId(user.getCompanyId());
478: page.setUserId(user.getUserId());
479: page.setUserName(user.getFullName());
480: page.setCreateDate(now);
481: page.setNodeId(nodeId);
482: page.setTitle(title);
483: page.setVersion(newVersion);
484: page.setContent(content);
485: page.setFormat(format);
486: page.setHead(true);
487:
488: wikiPagePersistence.update(page);
489:
490: // Node
491:
492: WikiNode node = wikiNodePersistence.findByPrimaryKey(nodeId);
493:
494: node.setLastPostDate(now);
495:
496: wikiNodePersistence.update(node);
497:
498: // Tags
499:
500: updateTagsAsset(userId, page, tagsEntries);
501:
502: // Lucene
503:
504: try {
505: Indexer.updatePage(node.getCompanyId(), node.getGroupId(),
506: nodeId, title, content, tagsEntries);
507: } catch (IOException ioe) {
508: _log.error("Indexing " + page.getPrimaryKey(), ioe);
509: }
510:
511: return page;
512: }
513:
514: public void updateTagsAsset(long userId, WikiPage page,
515: String[] tagsEntries) throws PortalException,
516: SystemException {
517:
518: tagsAssetLocalService.updateAsset(userId, page.getNode()
519: .getGroupId(), WikiPage.class.getName(), page
520: .getResourcePrimKey(), tagsEntries, null, null, null,
521: null, ContentTypes.TEXT_HTML, page.getTitle(), null,
522: null, null, 0, 0, null, false);
523: }
524:
525: protected void validate(long nodeId, String content, String format)
526: throws PortalException {
527:
528: if (format.equals(WikiPageImpl.CLASSIC_WIKI_FORMAT)) {
529: try {
530: NodeFilter filter = WikiUtil.getFilter(nodeId);
531:
532: WikiUtil.convert(filter, content);
533: } catch (Exception e) {
534: throw new PageContentException();
535: }
536: }
537: }
538:
539: protected void validate(String title, long nodeId, String content,
540: String format) throws PortalException {
541:
542: if (Validator.isNull(title)) {
543: throw new PageTitleException();
544: }
545:
546: Pattern pattern = Pattern.compile("(((\\p{Lu}\\p{Ll}+)_?)+)");
547: Matcher matcher = pattern.matcher(title);
548:
549: if (!matcher.matches()) {
550: throw new PageTitleException();
551: }
552:
553: validate(nodeId, content, format);
554: }
555:
556: private static Log _log = LogFactory
557: .getLog(WikiPageLocalServiceImpl.class);
558:
559: }
|