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