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.SearchEngineUtil;
30  import com.liferay.portal.kernel.search.SearchException;
31  import com.liferay.portal.kernel.util.ContentTypes;
32  import com.liferay.portal.kernel.util.OrderByComparator;
33  import com.liferay.portal.kernel.util.Validator;
34  import com.liferay.portal.model.ResourceConstants;
35  import com.liferay.portal.model.User;
36  import com.liferay.portlet.bookmarks.EntryURLException;
37  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
38  import com.liferay.portlet.bookmarks.model.BookmarksFolder;
39  import com.liferay.portlet.bookmarks.service.base.BookmarksEntryLocalServiceBaseImpl;
40  import com.liferay.portlet.bookmarks.util.Indexer;
41  import com.liferay.portlet.bookmarks.util.comparator.EntryModifiedDateComparator;
42  
43  import java.net.MalformedURLException;
44  import java.net.URL;
45  
46  import java.util.Date;
47  import java.util.Iterator;
48  import java.util.List;
49  
50  /**
51   * <a href="BookmarksEntryLocalServiceImpl.java.html"><b><i>View Source</i></b>
52   * </a>
53   *
54   * @author Brian Wing Shun Chan
55   */
56  public class BookmarksEntryLocalServiceImpl
57      extends BookmarksEntryLocalServiceBaseImpl {
58  
59      public BookmarksEntry addEntry(
60              long userId, long folderId, String name, String url,
61              String comments, String[] tagsEntries,
62              boolean addCommunityPermissions, boolean addGuestPermissions)
63          throws PortalException, SystemException {
64  
65          return addEntry(
66              null, userId, folderId, name, url, comments, tagsEntries,
67              Boolean.valueOf(addCommunityPermissions),
68              Boolean.valueOf(addGuestPermissions), null, null);
69      }
70  
71      public BookmarksEntry addEntry(
72              String uuid, long userId, long folderId, String name, String url,
73              String comments, String[] tagsEntries,
74              boolean addCommunityPermissions, boolean addGuestPermissions)
75          throws PortalException, SystemException {
76  
77          return addEntry(
78              uuid, userId, folderId, name, url, comments, tagsEntries,
79              Boolean.valueOf(addCommunityPermissions),
80              Boolean.valueOf(addGuestPermissions), null, null);
81      }
82  
83      public BookmarksEntry addEntry(
84              long userId, long folderId, String name, String url,
85              String comments, String[] tagsEntries,
86              String[] communityPermissions, String[] guestPermissions)
87          throws PortalException, SystemException {
88  
89          return addEntry(
90              null, userId, folderId, name, url, comments, tagsEntries, null,
91              null, communityPermissions, guestPermissions);
92      }
93  
94      public BookmarksEntry addEntry(
95              String uuid, long userId, long folderId, String name, String url,
96              String comments, String[] tagsEntries,
97              Boolean addCommunityPermissions, Boolean addGuestPermissions,
98              String[] communityPermissions, String[] guestPermissions)
99          throws PortalException, SystemException {
100 
101         // Entry
102 
103         User user = userPersistence.findByPrimaryKey(userId);
104         BookmarksFolder folder =
105             bookmarksFolderPersistence.findByPrimaryKey(folderId);
106 
107         if (Validator.isNull(name)) {
108             name = url;
109         }
110 
111         Date now = new Date();
112 
113         validate(url);
114 
115         long entryId = counterLocalService.increment();
116 
117         BookmarksEntry entry = bookmarksEntryPersistence.create(entryId);
118 
119         entry.setUuid(uuid);
120         entry.setGroupId(folder.getGroupId());
121         entry.setCompanyId(user.getCompanyId());
122         entry.setUserId(user.getUserId());
123         entry.setCreateDate(now);
124         entry.setModifiedDate(now);
125         entry.setFolderId(folderId);
126         entry.setName(name);
127         entry.setUrl(url);
128         entry.setComments(comments);
129 
130         bookmarksEntryPersistence.update(entry, false);
131 
132         // Resources
133 
134         if ((addCommunityPermissions != null) &&
135             (addGuestPermissions != null)) {
136 
137             addEntryResources(
138                 entry, addCommunityPermissions.booleanValue(),
139                 addGuestPermissions.booleanValue());
140         }
141         else {
142             addEntryResources(entry, communityPermissions, guestPermissions);
143         }
144 
145         // Tags
146 
147         updateTagsAsset(userId, entry, tagsEntries);
148 
149         // Indexer
150 
151         reIndex(entry);
152 
153         return entry;
154     }
155 
156     public void addEntryResources(
157             long entryId, boolean addCommunityPermissions,
158             boolean addGuestPermissions)
159         throws PortalException, SystemException {
160 
161         BookmarksEntry entry =
162             bookmarksEntryPersistence.findByPrimaryKey(entryId);
163 
164         addEntryResources(entry, addCommunityPermissions, addGuestPermissions);
165     }
166 
167     public void addEntryResources(
168             BookmarksEntry entry, boolean addCommunityPermissions,
169             boolean addGuestPermissions)
170         throws PortalException, SystemException {
171 
172         resourceLocalService.addResources(
173             entry.getCompanyId(), entry.getGroupId(), entry.getUserId(),
174             BookmarksEntry.class.getName(), entry.getEntryId(), false,
175             addCommunityPermissions, addGuestPermissions);
176     }
177 
178     public void addEntryResources(
179             long entryId, String[] communityPermissions,
180             String[] guestPermissions)
181         throws PortalException, SystemException {
182 
183         BookmarksEntry entry =
184             bookmarksEntryPersistence.findByPrimaryKey(entryId);
185 
186         addEntryResources(entry, communityPermissions, guestPermissions);
187     }
188 
189     public void addEntryResources(
190             BookmarksEntry entry, String[] communityPermissions,
191             String[] guestPermissions)
192         throws PortalException, SystemException {
193 
194         resourceLocalService.addModelResources(
195             entry.getCompanyId(), entry.getGroupId(), entry.getUserId(),
196             BookmarksEntry.class.getName(), entry.getEntryId(),
197             communityPermissions, guestPermissions);
198     }
199 
200     public void deleteEntries(long folderId)
201         throws PortalException, SystemException {
202 
203         Iterator<BookmarksEntry> itr = bookmarksEntryPersistence.findByFolderId(
204             folderId).iterator();
205 
206         while (itr.hasNext()) {
207             BookmarksEntry entry = itr.next();
208 
209             deleteEntry(entry);
210         }
211     }
212 
213     public void deleteEntry(long entryId)
214         throws PortalException, SystemException {
215 
216         BookmarksEntry entry =
217             bookmarksEntryPersistence.findByPrimaryKey(entryId);
218 
219         deleteEntry(entry);
220     }
221 
222     public void deleteEntry(BookmarksEntry entry)
223         throws PortalException, SystemException {
224 
225         // Indexer
226 
227         try {
228             Indexer.deleteEntry(entry.getCompanyId(), entry.getEntryId());
229         }
230         catch (SearchException se) {
231             _log.error("Deleting index " + entry.getEntryId(), se);
232         }
233 
234         // Tags
235 
236         tagsAssetLocalService.deleteAsset(
237             BookmarksEntry.class.getName(), entry.getEntryId());
238 
239         // Resources
240 
241         resourceLocalService.deleteResource(
242             entry.getCompanyId(), BookmarksEntry.class.getName(),
243             ResourceConstants.SCOPE_INDIVIDUAL, entry.getEntryId());
244 
245         // Entry
246 
247         bookmarksEntryPersistence.remove(entry);
248     }
249 
250     public List<BookmarksEntry> getEntries(long folderId, int start, int end)
251         throws SystemException {
252 
253         return bookmarksEntryPersistence.findByFolderId(folderId, start, end);
254     }
255 
256     public List<BookmarksEntry> getEntries(
257             long folderId, int start, int end,
258             OrderByComparator orderByComparator)
259         throws SystemException {
260 
261         return bookmarksEntryPersistence.findByFolderId(
262             folderId, start, end, orderByComparator);
263     }
264 
265     public int getEntriesCount(long folderId) throws SystemException {
266         return bookmarksEntryPersistence.countByFolderId(folderId);
267     }
268 
269     public BookmarksEntry getEntry(long entryId)
270         throws PortalException, SystemException {
271 
272         return bookmarksEntryPersistence.findByPrimaryKey(entryId);
273     }
274 
275     public int getFoldersEntriesCount(List<Long> folderIds)
276         throws SystemException {
277 
278         return bookmarksEntryFinder.countByFolderIds(folderIds);
279     }
280 
281     public List<BookmarksEntry> getGroupEntries(
282             long groupId, int start, int end)
283         throws SystemException {
284 
285         return bookmarksEntryPersistence.findByGroupId(
286             groupId, start, end, new EntryModifiedDateComparator());
287     }
288 
289     public List<BookmarksEntry> getGroupEntries(
290             long groupId, long userId, int start, int end)
291         throws SystemException {
292 
293         OrderByComparator orderByComparator = new EntryModifiedDateComparator();
294 
295         if (userId <= 0) {
296             return bookmarksEntryPersistence.findByGroupId(
297                 groupId, start, end, orderByComparator);
298         }
299         else {
300             return bookmarksEntryPersistence.findByG_U(
301                 groupId, userId, start, end, orderByComparator);
302         }
303     }
304 
305     public int getGroupEntriesCount(long groupId) throws SystemException {
306         return bookmarksEntryPersistence.countByGroupId(groupId);
307     }
308 
309     public int getGroupEntriesCount(long groupId, long userId)
310         throws SystemException {
311 
312         if (userId <= 0) {
313             return bookmarksEntryPersistence.countByGroupId(groupId);
314         }
315         else {
316             return bookmarksEntryPersistence.countByG_U(groupId, userId);
317         }
318     }
319 
320     public List<BookmarksEntry> getNoAssetEntries() throws SystemException {
321         return bookmarksEntryFinder.findByNoAssets();
322     }
323 
324     public BookmarksEntry openEntry(long entryId)
325         throws PortalException, SystemException {
326 
327         BookmarksEntry entry =
328             bookmarksEntryPersistence.findByPrimaryKey(entryId);
329 
330         entry.setVisits(entry.getVisits() + 1);
331 
332         bookmarksEntryPersistence.update(entry, false);
333 
334         tagsAssetLocalService.incrementViewCounter(
335             BookmarksEntry.class.getName(), entryId);
336 
337         return entry;
338     }
339 
340     public void reIndex(long entryId) throws SystemException {
341         if (SearchEngineUtil.isIndexReadOnly()) {
342             return;
343         }
344 
345         BookmarksEntry entry = bookmarksEntryPersistence.fetchByPrimaryKey(
346             entryId);
347 
348         if (entry == null) {
349             return;
350         }
351 
352         reIndex(entry);
353     }
354 
355     public void reIndex(BookmarksEntry entry) throws SystemException {
356         long companyId = entry.getCompanyId();
357         long groupId = entry.getGroupId();
358         long folderId = entry.getFolderId();
359         long entryId = entry.getEntryId();
360         String name = entry.getName();
361         String url = entry.getUrl();
362         String comments = entry.getComments();
363         Date modifiedDate = entry.getModifiedDate();
364 
365         String[] tagsEntries = tagsEntryLocalService.getEntryNames(
366             BookmarksEntry.class.getName(), entryId);
367 
368         try {
369             Indexer.updateEntry(
370                 companyId, groupId, folderId, entryId, name, url, comments,
371                 modifiedDate, tagsEntries);
372         }
373         catch (SearchException se) {
374             _log.error("Reindexing " + entryId, se);
375         }
376     }
377 
378     public BookmarksEntry updateEntry(
379             long userId, long entryId, long folderId, String name, String url,
380             String comments, String[] tagsEntries)
381         throws PortalException, SystemException {
382 
383         // Entry
384 
385         BookmarksEntry entry =
386             bookmarksEntryPersistence.findByPrimaryKey(entryId);
387 
388         BookmarksFolder folder = getFolder(entry, folderId);
389 
390         if (Validator.isNull(name)) {
391             name = url;
392         }
393 
394         validate(url);
395 
396         entry.setModifiedDate(new Date());
397         entry.setFolderId(folder.getFolderId());
398         entry.setName(name);
399         entry.setUrl(url);
400         entry.setComments(comments);
401 
402         bookmarksEntryPersistence.update(entry, false);
403 
404         // Tags
405 
406         updateTagsAsset(userId, entry, tagsEntries);
407 
408         // Indexer
409 
410         reIndex(entry);
411 
412         return entry;
413     }
414 
415     public void updateTagsAsset(
416             long userId, BookmarksEntry entry, String[] tagsEntries)
417         throws PortalException, SystemException {
418 
419         tagsAssetLocalService.updateAsset(
420             userId, entry.getGroupId(), BookmarksEntry.class.getName(),
421             entry.getEntryId(), tagsEntries, null, null, null, null,
422             ContentTypes.TEXT_PLAIN, entry.getName(), entry.getComments(), null,
423             entry.getUrl(), 0, 0, null, false);
424     }
425 
426     protected BookmarksFolder getFolder(BookmarksEntry entry, long folderId)
427         throws PortalException, SystemException {
428 
429         if (entry.getFolderId() != folderId) {
430             BookmarksFolder oldFolder =
431                 bookmarksFolderPersistence.findByPrimaryKey(
432                     entry.getFolderId());
433 
434             BookmarksFolder newFolder =
435                 bookmarksFolderPersistence.fetchByPrimaryKey(folderId);
436 
437             if ((newFolder == null) ||
438                 (oldFolder.getGroupId() != newFolder.getGroupId())) {
439 
440                 folderId = entry.getFolderId();
441             }
442         }
443 
444         return bookmarksFolderPersistence.findByPrimaryKey(folderId);
445     }
446 
447     protected void validate(String url) throws PortalException {
448         if (Validator.isNull(url)) {
449             throw new EntryURLException();
450         }
451         else {
452             try {
453                 new URL(url);
454             }
455             catch (MalformedURLException murle) {
456                 throw new EntryURLException();
457             }
458         }
459     }
460 
461     private static Log _log =
462         LogFactoryUtil.getLog(BookmarksEntryLocalServiceImpl.class);
463 
464 }