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.bookmarks.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.bookmarks.FolderNameException;
034: import com.liferay.portlet.bookmarks.model.BookmarksEntry;
035: import com.liferay.portlet.bookmarks.model.BookmarksFolder;
036: import com.liferay.portlet.bookmarks.model.impl.BookmarksFolderImpl;
037: import com.liferay.portlet.bookmarks.service.base.BookmarksFolderLocalServiceBaseImpl;
038: import com.liferay.portlet.bookmarks.util.Indexer;
039: import com.liferay.util.lucene.HitsImpl;
040:
041: import java.util.ArrayList;
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.search.BooleanClause;
052: import org.apache.lucene.search.BooleanQuery;
053: import org.apache.lucene.search.Searcher;
054: import org.apache.lucene.search.TermQuery;
055:
056: /**
057: * <a href="BookmarksFolderLocalServiceImpl.java.html"><b><i>View Source</i></b>
058: * </a>
059: *
060: * @author Brian Wing Shun Chan
061: *
062: */
063: public class BookmarksFolderLocalServiceImpl extends
064: BookmarksFolderLocalServiceBaseImpl {
065:
066: public BookmarksFolder addFolder(long userId, long plid,
067: long parentFolderId, String name, String description,
068: boolean addCommunityPermissions, boolean addGuestPermissions)
069: throws PortalException, SystemException {
070:
071: return addFolder(null, userId, plid, parentFolderId, name,
072: description, Boolean.valueOf(addCommunityPermissions),
073: Boolean.valueOf(addGuestPermissions), null, null);
074: }
075:
076: public BookmarksFolder addFolder(String uuid, long userId,
077: long plid, long parentFolderId, String name,
078: String description, boolean addCommunityPermissions,
079: boolean addGuestPermissions) throws PortalException,
080: SystemException {
081:
082: return addFolder(uuid, userId, plid, parentFolderId, name,
083: description, Boolean.valueOf(addCommunityPermissions),
084: Boolean.valueOf(addGuestPermissions), null, null);
085: }
086:
087: public BookmarksFolder addFolder(long userId, long plid,
088: long parentFolderId, String name, String description,
089: String[] communityPermissions, String[] guestPermissions)
090: throws PortalException, SystemException {
091:
092: return addFolder(null, userId, plid, parentFolderId, name,
093: description, null, null, communityPermissions,
094: guestPermissions);
095: }
096:
097: public BookmarksFolder addFolder(String uuid, long userId,
098: long plid, long parentFolderId, String name,
099: String description, Boolean addCommunityPermissions,
100: Boolean addGuestPermissions, String[] communityPermissions,
101: String[] guestPermissions) throws PortalException,
102: SystemException {
103:
104: // Folder
105:
106: User user = userPersistence.findByPrimaryKey(userId);
107: long groupId = PortalUtil.getPortletGroupId(plid);
108: parentFolderId = getParentFolderId(groupId, parentFolderId);
109: Date now = new Date();
110:
111: validate(name);
112:
113: long folderId = counterLocalService.increment();
114:
115: BookmarksFolder folder = bookmarksFolderPersistence
116: .create(folderId);
117:
118: folder.setUuid(uuid);
119: folder.setGroupId(groupId);
120: folder.setCompanyId(user.getCompanyId());
121: folder.setUserId(user.getUserId());
122: folder.setCreateDate(now);
123: folder.setModifiedDate(now);
124: folder.setParentFolderId(parentFolderId);
125: folder.setName(name);
126: folder.setDescription(description);
127:
128: bookmarksFolderPersistence.update(folder);
129:
130: // Resources
131:
132: if ((addCommunityPermissions != null)
133: && (addGuestPermissions != null)) {
134:
135: addFolderResources(folder, addCommunityPermissions
136: .booleanValue(), addGuestPermissions.booleanValue());
137: } else {
138: addFolderResources(folder, communityPermissions,
139: guestPermissions);
140: }
141:
142: return folder;
143: }
144:
145: public void addFolderResources(long folderId,
146: boolean addCommunityPermissions, boolean addGuestPermissions)
147: throws PortalException, SystemException {
148:
149: BookmarksFolder folder = bookmarksFolderPersistence
150: .findByPrimaryKey(folderId);
151:
152: addFolderResources(folder, addCommunityPermissions,
153: addGuestPermissions);
154: }
155:
156: public void addFolderResources(BookmarksFolder folder,
157: boolean addCommunityPermissions, boolean addGuestPermissions)
158: throws PortalException, SystemException {
159:
160: resourceLocalService.addResources(folder.getCompanyId(), folder
161: .getGroupId(), folder.getUserId(),
162: BookmarksFolder.class.getName(), folder.getFolderId(),
163: false, addCommunityPermissions, addGuestPermissions);
164: }
165:
166: public void addFolderResources(long folderId,
167: String[] communityPermissions, String[] guestPermissions)
168: throws PortalException, SystemException {
169:
170: BookmarksFolder folder = bookmarksFolderPersistence
171: .findByPrimaryKey(folderId);
172:
173: addFolderResources(folder, communityPermissions,
174: guestPermissions);
175: }
176:
177: public void addFolderResources(BookmarksFolder folder,
178: String[] communityPermissions, String[] guestPermissions)
179: throws PortalException, SystemException {
180:
181: resourceLocalService.addModelResources(folder.getCompanyId(),
182: folder.getGroupId(), folder.getUserId(),
183: BookmarksFolder.class.getName(), folder.getFolderId(),
184: communityPermissions, guestPermissions);
185: }
186:
187: public void deleteFolder(long folderId) throws PortalException,
188: SystemException {
189:
190: BookmarksFolder folder = bookmarksFolderPersistence
191: .findByPrimaryKey(folderId);
192:
193: deleteFolder(folder);
194: }
195:
196: public void deleteFolder(BookmarksFolder folder)
197: throws PortalException, SystemException {
198:
199: // Folders
200:
201: Iterator itr = bookmarksFolderPersistence.findByG_P(
202: folder.getGroupId(), folder.getFolderId()).iterator();
203:
204: while (itr.hasNext()) {
205: BookmarksFolder curFolder = (BookmarksFolder) itr.next();
206:
207: deleteFolder(curFolder);
208: }
209:
210: // Entries
211:
212: bookmarksEntryLocalService.deleteEntries(folder.getFolderId());
213:
214: // Resources
215:
216: resourceLocalService.deleteResource(folder.getCompanyId(),
217: BookmarksFolder.class.getName(),
218: ResourceImpl.SCOPE_INDIVIDUAL, folder.getFolderId());
219:
220: // Folder
221:
222: bookmarksFolderPersistence.remove(folder.getFolderId());
223: }
224:
225: public void deleteFolders(long groupId) throws PortalException,
226: SystemException {
227:
228: Iterator itr = bookmarksFolderPersistence.findByG_P(groupId,
229: BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID)
230: .iterator();
231:
232: while (itr.hasNext()) {
233: BookmarksFolder folder = (BookmarksFolder) itr.next();
234:
235: deleteFolder(folder);
236: }
237: }
238:
239: public BookmarksFolder getFolder(long folderId)
240: throws PortalException, SystemException {
241:
242: return bookmarksFolderPersistence.findByPrimaryKey(folderId);
243: }
244:
245: public List getFolders(long groupId, long parentFolderId,
246: int begin, int end) throws SystemException {
247:
248: return bookmarksFolderPersistence.findByG_P(groupId,
249: parentFolderId, begin, end);
250: }
251:
252: public int getFoldersCount(long groupId, long parentFolderId)
253: throws SystemException {
254:
255: return bookmarksFolderPersistence.countByG_P(groupId,
256: parentFolderId);
257: }
258:
259: public void getSubfolderIds(List folderIds, long groupId,
260: long folderId) throws SystemException {
261:
262: Iterator itr = bookmarksFolderPersistence.findByG_P(groupId,
263: folderId).iterator();
264:
265: while (itr.hasNext()) {
266: BookmarksFolder folder = (BookmarksFolder) itr.next();
267:
268: folderIds.add(new Long(folder.getFolderId()));
269:
270: getSubfolderIds(folderIds, folder.getGroupId(), folder
271: .getFolderId());
272: }
273: }
274:
275: public void reIndex(String[] ids) throws SystemException {
276: if (LuceneUtil.INDEX_READ_ONLY) {
277: return;
278: }
279:
280: long companyId = GetterUtil.getLong(ids[0]);
281:
282: IndexWriter writer = null;
283:
284: try {
285: writer = LuceneUtil.getWriter(companyId);
286:
287: Iterator itr1 = bookmarksFolderPersistence.findByCompanyId(
288: companyId).iterator();
289:
290: while (itr1.hasNext()) {
291: BookmarksFolder folder = (BookmarksFolder) itr1.next();
292:
293: long folderId = folder.getFolderId();
294:
295: Iterator itr2 = bookmarksEntryPersistence
296: .findByFolderId(folderId).iterator();
297:
298: while (itr2.hasNext()) {
299: BookmarksEntry entry = (BookmarksEntry) itr2.next();
300:
301: long groupId = folder.getGroupId();
302: long entryId = entry.getEntryId();
303: String title = entry.getName();
304: String content = entry.getUrl();
305: String description = entry.getComments();
306:
307: String[] tagsEntries = tagsEntryLocalService
308: .getEntryNames(BookmarksEntry.class
309: .getName(), entryId);
310:
311: try {
312: Document doc = Indexer.getAddEntryDocument(
313: companyId, groupId, folderId, entryId,
314: title, content, description,
315: tagsEntries);
316:
317: writer.addDocument(doc);
318: } catch (Exception e1) {
319: _log.error("Reindexing " + entryId, e1);
320: }
321: }
322: }
323: } catch (SystemException se) {
324: throw se;
325: } catch (Exception e2) {
326: throw new SystemException(e2);
327: } finally {
328: try {
329: if (writer != null) {
330: LuceneUtil.write(companyId);
331: }
332: } catch (Exception e) {
333: _log.error(e);
334: }
335: }
336: }
337:
338: public Hits search(long companyId, long groupId, long[] folderIds,
339: String keywords) throws SystemException {
340:
341: Searcher searcher = null;
342:
343: try {
344: HitsImpl hits = new HitsImpl();
345:
346: BooleanQuery contextQuery = new BooleanQuery();
347:
348: LuceneUtil.addRequiredTerm(contextQuery,
349: LuceneFields.PORTLET_ID, Indexer.PORTLET_ID);
350:
351: if (groupId > 0) {
352: LuceneUtil.addRequiredTerm(contextQuery,
353: LuceneFields.GROUP_ID, groupId);
354: }
355:
356: if ((folderIds != null) && (folderIds.length > 0)) {
357: BooleanQuery folderIdsQuery = new BooleanQuery();
358:
359: for (int i = 0; i < folderIds.length; i++) {
360: Term term = new Term("folderId", String
361: .valueOf(folderIds[i]));
362: TermQuery termQuery = new TermQuery(term);
363:
364: folderIdsQuery.add(termQuery,
365: BooleanClause.Occur.SHOULD);
366: }
367:
368: contextQuery.add(folderIdsQuery,
369: BooleanClause.Occur.MUST);
370: }
371:
372: BooleanQuery searchQuery = new BooleanQuery();
373:
374: if (Validator.isNotNull(keywords)) {
375: LuceneUtil.addTerm(searchQuery, LuceneFields.TITLE,
376: keywords);
377: LuceneUtil.addTerm(searchQuery, LuceneFields.CONTENT,
378: keywords);
379: LuceneUtil.addTerm(searchQuery,
380: LuceneFields.DESCRIPTION, keywords);
381: LuceneUtil.addTerm(searchQuery, LuceneFields.TAG_ENTRY,
382: keywords);
383: }
384:
385: BooleanQuery fullQuery = new BooleanQuery();
386:
387: fullQuery.add(contextQuery, BooleanClause.Occur.MUST);
388:
389: if (searchQuery.clauses().size() > 0) {
390: fullQuery.add(searchQuery, BooleanClause.Occur.MUST);
391: }
392:
393: searcher = LuceneUtil.getSearcher(companyId);
394:
395: hits.recordHits(searcher.search(fullQuery), searcher);
396:
397: return hits;
398: } catch (Exception e) {
399: return LuceneUtil.closeSearcher(searcher, keywords, e);
400: }
401: }
402:
403: public BookmarksFolder updateFolder(long folderId,
404: long parentFolderId, String name, String description,
405: boolean mergeWithParentFolder) throws PortalException,
406: SystemException {
407:
408: // Folder
409:
410: BookmarksFolder folder = bookmarksFolderPersistence
411: .findByPrimaryKey(folderId);
412:
413: parentFolderId = getParentFolderId(folder, parentFolderId);
414:
415: validate(name);
416:
417: folder.setModifiedDate(new Date());
418: folder.setParentFolderId(parentFolderId);
419: folder.setName(name);
420: folder.setDescription(description);
421:
422: bookmarksFolderPersistence.update(folder);
423:
424: // Merge folders
425:
426: if (mergeWithParentFolder
427: && (folderId != parentFolderId)
428: && (parentFolderId != BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID)) {
429:
430: mergeFolders(folder, parentFolderId);
431: }
432:
433: return folder;
434: }
435:
436: protected long getParentFolderId(long groupId, long parentFolderId)
437: throws SystemException {
438:
439: if (parentFolderId != BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
440: BookmarksFolder parentFolder = bookmarksFolderPersistence
441: .fetchByPrimaryKey(parentFolderId);
442:
443: if ((parentFolder == null)
444: || (groupId != parentFolder.getGroupId())) {
445:
446: parentFolderId = BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID;
447: }
448: }
449:
450: return parentFolderId;
451: }
452:
453: protected long getParentFolderId(BookmarksFolder folder,
454: long parentFolderId) throws SystemException {
455:
456: if (parentFolderId == BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
457: return parentFolderId;
458: }
459:
460: if (folder.getFolderId() == parentFolderId) {
461: return folder.getParentFolderId();
462: } else {
463: BookmarksFolder parentFolder = bookmarksFolderPersistence
464: .fetchByPrimaryKey(parentFolderId);
465:
466: if ((parentFolder == null)
467: || (folder.getGroupId() != parentFolder
468: .getGroupId())) {
469:
470: return folder.getParentFolderId();
471: }
472:
473: List subfolderIds = new ArrayList();
474:
475: getSubfolderIds(subfolderIds, folder.getGroupId(), folder
476: .getFolderId());
477:
478: if (subfolderIds.contains(new Long(parentFolderId))) {
479: return folder.getParentFolderId();
480: }
481:
482: return parentFolderId;
483: }
484: }
485:
486: protected void mergeFolders(BookmarksFolder fromFolder,
487: long toFolderId) throws PortalException, SystemException {
488:
489: Iterator itr = bookmarksFolderPersistence.findByG_P(
490: fromFolder.getGroupId(), fromFolder.getFolderId())
491: .iterator();
492:
493: while (itr.hasNext()) {
494: BookmarksFolder folder = (BookmarksFolder) itr.next();
495:
496: mergeFolders(folder, toFolderId);
497: }
498:
499: itr = bookmarksEntryPersistence.findByFolderId(
500: fromFolder.getFolderId()).iterator();
501:
502: while (itr.hasNext()) {
503:
504: // Entry
505:
506: BookmarksEntry entry = (BookmarksEntry) itr.next();
507:
508: entry.setFolderId(toFolderId);
509:
510: bookmarksEntryPersistence.update(entry);
511: }
512:
513: bookmarksFolderPersistence.remove(fromFolder.getFolderId());
514: }
515:
516: protected void validate(String name) throws PortalException {
517: if ((Validator.isNull(name)) || (name.indexOf("\\\\") != -1)
518: || (name.indexOf("//") != -1)) {
519:
520: throw new FolderNameException();
521: }
522: }
523:
524: private static Log _log = LogFactory
525: .getLog(BookmarksFolderLocalServiceImpl.class);
526:
527: }
|