1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.bookmarks.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.search.BooleanClauseOccur;
30  import com.liferay.portal.kernel.search.BooleanQuery;
31  import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
32  import com.liferay.portal.kernel.search.Field;
33  import com.liferay.portal.kernel.search.Hits;
34  import com.liferay.portal.kernel.search.SearchEngineUtil;
35  import com.liferay.portal.kernel.search.TermQuery;
36  import com.liferay.portal.kernel.search.TermQueryFactoryUtil;
37  import com.liferay.portal.kernel.util.GetterUtil;
38  import com.liferay.portal.kernel.util.Validator;
39  import com.liferay.portal.model.ResourceConstants;
40  import com.liferay.portal.model.User;
41  import com.liferay.portal.util.PortalUtil;
42  import com.liferay.portlet.bookmarks.FolderNameException;
43  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
44  import com.liferay.portlet.bookmarks.model.BookmarksFolder;
45  import com.liferay.portlet.bookmarks.model.impl.BookmarksFolderImpl;
46  import com.liferay.portlet.bookmarks.service.base.BookmarksFolderLocalServiceBaseImpl;
47  import com.liferay.portlet.bookmarks.util.Indexer;
48  
49  import java.util.ArrayList;
50  import java.util.Date;
51  import java.util.List;
52  
53  /**
54   * <a href="BookmarksFolderLocalServiceImpl.java.html"><b><i>View Source</i></b>
55   * </a>
56   *
57   * @author Brian Wing Shun Chan
58   */
59  public class BookmarksFolderLocalServiceImpl
60      extends BookmarksFolderLocalServiceBaseImpl {
61  
62      public BookmarksFolder addFolder(
63              long userId, long plid, long parentFolderId, String name,
64              String description, boolean addCommunityPermissions,
65              boolean addGuestPermissions)
66          throws PortalException, SystemException {
67  
68          return addFolder(
69              null, userId, plid, parentFolderId, name, description,
70              Boolean.valueOf(addCommunityPermissions),
71              Boolean.valueOf(addGuestPermissions), null, null);
72      }
73  
74      public BookmarksFolder addFolder(
75              String uuid, long userId, long plid, long parentFolderId,
76              String name, String description, boolean addCommunityPermissions,
77              boolean addGuestPermissions)
78          throws PortalException, SystemException {
79  
80          return addFolder(
81              uuid, userId, plid, parentFolderId, name, description,
82              Boolean.valueOf(addCommunityPermissions),
83              Boolean.valueOf(addGuestPermissions), null, null);
84      }
85  
86      public BookmarksFolder addFolder(
87              long userId, long plid, long parentFolderId, String name,
88              String description, String[] communityPermissions,
89              String[] guestPermissions)
90          throws PortalException, SystemException {
91  
92          return addFolder(
93              null, userId, plid, parentFolderId, name, description, null, null,
94              communityPermissions, guestPermissions);
95      }
96  
97      public BookmarksFolder addFolder(
98              String uuid, long userId, long plid, long parentFolderId,
99              String name, String description, Boolean addCommunityPermissions,
100             Boolean addGuestPermissions, String[] communityPermissions,
101             String[] guestPermissions)
102         throws PortalException, SystemException {
103 
104         // Folder
105 
106         User user = userPersistence.findByPrimaryKey(userId);
107         long groupId = PortalUtil.getScopeGroupId(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.create(folderId);
116 
117         folder.setUuid(uuid);
118         folder.setGroupId(groupId);
119         folder.setCompanyId(user.getCompanyId());
120         folder.setUserId(user.getUserId());
121         folder.setCreateDate(now);
122         folder.setModifiedDate(now);
123         folder.setParentFolderId(parentFolderId);
124         folder.setName(name);
125         folder.setDescription(description);
126 
127         bookmarksFolderPersistence.update(folder, false);
128 
129         // Resources
130 
131         if ((addCommunityPermissions != null) &&
132             (addGuestPermissions != null)) {
133 
134             addFolderResources(
135                 folder, addCommunityPermissions.booleanValue(),
136                 addGuestPermissions.booleanValue());
137         }
138         else {
139             addFolderResources(folder, communityPermissions, guestPermissions);
140         }
141 
142         return folder;
143     }
144 
145     public void addFolderResources(
146             long folderId, boolean addCommunityPermissions,
147             boolean addGuestPermissions)
148         throws PortalException, SystemException {
149 
150         BookmarksFolder folder =
151             bookmarksFolderPersistence.findByPrimaryKey(folderId);
152 
153         addFolderResources(
154             folder, addCommunityPermissions, addGuestPermissions);
155     }
156 
157     public void addFolderResources(
158             BookmarksFolder folder, boolean addCommunityPermissions,
159             boolean addGuestPermissions)
160         throws PortalException, SystemException {
161 
162         resourceLocalService.addResources(
163             folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
164             BookmarksFolder.class.getName(), folder.getFolderId(), false,
165             addCommunityPermissions, addGuestPermissions);
166     }
167 
168     public void addFolderResources(
169             long folderId, String[] communityPermissions,
170             String[] guestPermissions)
171         throws PortalException, SystemException {
172 
173         BookmarksFolder folder =
174             bookmarksFolderPersistence.findByPrimaryKey(folderId);
175 
176         addFolderResources(folder, communityPermissions, guestPermissions);
177     }
178 
179     public void addFolderResources(
180             BookmarksFolder folder, String[] communityPermissions,
181             String[] guestPermissions)
182         throws PortalException, SystemException {
183 
184         resourceLocalService.addModelResources(
185             folder.getCompanyId(), folder.getGroupId(), folder.getUserId(),
186             BookmarksFolder.class.getName(), folder.getFolderId(),
187             communityPermissions, guestPermissions);
188     }
189 
190     public void deleteFolder(long folderId)
191         throws PortalException, SystemException {
192 
193         BookmarksFolder folder =
194             bookmarksFolderPersistence.findByPrimaryKey(folderId);
195 
196         deleteFolder(folder);
197     }
198 
199     public void deleteFolder(BookmarksFolder folder)
200         throws PortalException, SystemException {
201 
202         // Folders
203 
204         List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
205             folder.getGroupId(), folder.getFolderId());
206 
207         for (BookmarksFolder curFolder : folders) {
208             deleteFolder(curFolder);
209         }
210 
211         // Entries
212 
213         bookmarksEntryLocalService.deleteEntries(folder.getFolderId());
214 
215         // Resources
216 
217         resourceLocalService.deleteResource(
218             folder.getCompanyId(), BookmarksFolder.class.getName(),
219             ResourceConstants.SCOPE_INDIVIDUAL, folder.getFolderId());
220 
221         // Folder
222 
223         bookmarksFolderPersistence.remove(folder);
224     }
225 
226     public void deleteFolders(long groupId)
227         throws PortalException, SystemException {
228 
229         List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
230             groupId, BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID);
231 
232         for (BookmarksFolder folder : folders) {
233             deleteFolder(folder);
234         }
235     }
236 
237     public BookmarksFolder getFolder(long folderId)
238         throws PortalException, SystemException {
239 
240         return bookmarksFolderPersistence.findByPrimaryKey(folderId);
241     }
242 
243     public List<BookmarksFolder> getFolders(
244             long groupId, long parentFolderId, int start, int end)
245         throws SystemException {
246 
247         return bookmarksFolderPersistence.findByG_P(
248             groupId, parentFolderId, start, end);
249     }
250 
251     public int getFoldersCount(long groupId, long parentFolderId)
252         throws SystemException {
253 
254         return bookmarksFolderPersistence.countByG_P(groupId, parentFolderId);
255     }
256 
257     public void getSubfolderIds(
258             List<Long> folderIds, long groupId, long folderId)
259         throws SystemException {
260 
261         List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
262             groupId, folderId);
263 
264         for (BookmarksFolder folder : folders) {
265             folderIds.add(folder.getFolderId());
266 
267             getSubfolderIds(
268                 folderIds, folder.getGroupId(), folder.getFolderId());
269         }
270     }
271 
272     public void reIndex(String[] ids) throws SystemException {
273         if (SearchEngineUtil.isIndexReadOnly()) {
274             return;
275         }
276 
277         long companyId = GetterUtil.getLong(ids[0]);
278 
279         try {
280             reIndexFolders(companyId);
281         }
282         catch (SystemException se) {
283             throw se;
284         }
285         catch (Exception e) {
286             throw new SystemException(e);
287         }
288     }
289 
290     public Hits search(
291             long companyId, long groupId, long[] folderIds, String keywords,
292             int start, int end)
293         throws SystemException {
294 
295         try {
296             BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
297 
298             contextQuery.addRequiredTerm(Field.PORTLET_ID, Indexer.PORTLET_ID);
299 
300             if (groupId > 0) {
301                 contextQuery.addRequiredTerm(Field.GROUP_ID, groupId);
302             }
303 
304             if ((folderIds != null) && (folderIds.length > 0)) {
305                 BooleanQuery folderIdsQuery = BooleanQueryFactoryUtil.create();
306 
307                 for (long folderId : folderIds) {
308                     TermQuery termQuery = TermQueryFactoryUtil.create(
309                         "folderId", folderId);
310 
311                     folderIdsQuery.add(termQuery, BooleanClauseOccur.SHOULD);
312                 }
313 
314                 contextQuery.add(folderIdsQuery, BooleanClauseOccur.MUST);
315             }
316 
317             BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
318 
319             if (Validator.isNotNull(keywords)) {
320                 searchQuery.addTerm(Field.TITLE, keywords);
321                 searchQuery.addTerm(Field.TAGS_ENTRIES, keywords, true);
322                 searchQuery.addTerm(Field.URL, keywords);
323                 searchQuery.addTerm(Field.COMMENTS, keywords);
324             }
325 
326             BooleanQuery fullQuery = BooleanQueryFactoryUtil.create();
327 
328             fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
329 
330             if (searchQuery.clauses().size() > 0) {
331                 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
332             }
333 
334             return SearchEngineUtil.search(companyId, fullQuery, start, end);
335         }
336         catch (Exception e) {
337             throw new SystemException(e);
338         }
339     }
340 
341     public BookmarksFolder updateFolder(
342             long folderId, long parentFolderId, String name,
343             String description, boolean mergeWithParentFolder)
344         throws PortalException, SystemException {
345 
346         // Folder
347 
348         BookmarksFolder folder =
349             bookmarksFolderPersistence.findByPrimaryKey(folderId);
350 
351         parentFolderId = getParentFolderId(folder, parentFolderId);
352 
353         validate(name);
354 
355         folder.setModifiedDate(new Date());
356         folder.setParentFolderId(parentFolderId);
357         folder.setName(name);
358         folder.setDescription(description);
359 
360         bookmarksFolderPersistence.update(folder, false);
361 
362         // Merge folders
363 
364         if (mergeWithParentFolder && (folderId != parentFolderId) &&
365             (parentFolderId != BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID)) {
366 
367             mergeFolders(folder, parentFolderId);
368         }
369 
370         return folder;
371     }
372 
373     protected long getParentFolderId(long groupId, long parentFolderId)
374         throws SystemException {
375 
376         if (parentFolderId != BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
377             BookmarksFolder parentFolder =
378                 bookmarksFolderPersistence.fetchByPrimaryKey(parentFolderId);
379 
380             if ((parentFolder == null) ||
381                 (groupId != parentFolder.getGroupId())) {
382 
383                 parentFolderId = BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID;
384             }
385         }
386 
387         return parentFolderId;
388     }
389 
390     protected long getParentFolderId(
391             BookmarksFolder folder, long parentFolderId)
392         throws SystemException {
393 
394         if (parentFolderId == BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
395             return parentFolderId;
396         }
397 
398         if (folder.getFolderId() == parentFolderId) {
399             return folder.getParentFolderId();
400         }
401         else {
402             BookmarksFolder parentFolder =
403                 bookmarksFolderPersistence.fetchByPrimaryKey(parentFolderId);
404 
405             if ((parentFolder == null) ||
406                 (folder.getGroupId() != parentFolder.getGroupId())) {
407 
408                 return folder.getParentFolderId();
409             }
410 
411             List<Long> subfolderIds = new ArrayList<Long>();
412 
413             getSubfolderIds(
414                 subfolderIds, folder.getGroupId(), folder.getFolderId());
415 
416             if (subfolderIds.contains(parentFolderId)) {
417                 return folder.getParentFolderId();
418             }
419 
420             return parentFolderId;
421         }
422     }
423 
424     protected void mergeFolders(BookmarksFolder fromFolder, long toFolderId)
425         throws PortalException, SystemException {
426 
427         List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
428                 fromFolder.getGroupId(), fromFolder.getFolderId());
429 
430         for (BookmarksFolder folder : folders) {
431             mergeFolders(folder, toFolderId);
432         }
433 
434         List<BookmarksEntry> entries = bookmarksEntryPersistence.findByFolderId(
435             fromFolder.getFolderId());
436 
437         for (BookmarksEntry entry : entries) {
438             entry.setFolderId(toFolderId);
439 
440             bookmarksEntryPersistence.update(entry, false);
441 
442             bookmarksEntryLocalService.reIndex(entry);
443         }
444 
445         deleteFolder(fromFolder);
446     }
447 
448     protected void reIndexFolders(long companyId) throws SystemException {
449         int folderCount = bookmarksFolderPersistence.countByCompanyId(
450             companyId);
451 
452         int folderPages = folderCount / Indexer.DEFAULT_INTERVAL;
453 
454         for (int i = 0; i <= folderPages; i++) {
455             int folderStart = (i * Indexer.DEFAULT_INTERVAL);
456             int folderEnd = folderStart + Indexer.DEFAULT_INTERVAL;
457 
458             reIndexFolders(companyId, folderStart, folderEnd);
459         }
460     }
461 
462     protected void reIndexFolders(
463             long companyId, int folderStart, int folderEnd)
464         throws SystemException {
465 
466         List<BookmarksFolder> folders =
467             bookmarksFolderPersistence.findByCompanyId(
468                 companyId, folderStart, folderEnd);
469 
470         for (BookmarksFolder folder : folders) {
471             long folderId = folder.getFolderId();
472 
473             int entryCount = bookmarksEntryPersistence.countByFolderId(
474                 folderId);
475 
476             int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;
477 
478             for (int i = 0; i <= entryPages; i++) {
479                 int entryStart = (i * Indexer.DEFAULT_INTERVAL);
480                 int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;
481 
482                 reIndexEntries(folderId, entryStart, entryEnd);
483             }
484         }
485     }
486 
487     protected void reIndexEntries(long folderId, int entryStart, int entryEnd)
488         throws SystemException {
489 
490         List<BookmarksEntry> entries = bookmarksEntryPersistence.findByFolderId(
491             folderId, entryStart, entryEnd);
492 
493         for (BookmarksEntry entry : entries) {
494             bookmarksEntryLocalService.reIndex(entry);
495         }
496     }
497 
498     protected void validate(String name) throws PortalException {
499         if ((Validator.isNull(name)) || (name.indexOf("\\\\") != -1) ||
500             (name.indexOf("//") != -1)) {
501 
502             throw new FolderNameException();
503         }
504     }
505 
506     private static Log _log =
507         LogFactoryUtil.getLog(BookmarksFolderLocalServiceImpl.class);
508 
509 }