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