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