001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.bookmarks.service.impl;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.util.ContentTypes;
026: import com.liferay.portal.kernel.util.OrderByComparator;
027: import com.liferay.portal.kernel.util.Validator;
028: import com.liferay.portal.model.User;
029: import com.liferay.portal.model.impl.ResourceImpl;
030: import com.liferay.portlet.bookmarks.EntryURLException;
031: import com.liferay.portlet.bookmarks.model.BookmarksEntry;
032: import com.liferay.portlet.bookmarks.model.BookmarksFolder;
033: import com.liferay.portlet.bookmarks.service.base.BookmarksEntryLocalServiceBaseImpl;
034: import com.liferay.portlet.bookmarks.util.Indexer;
035:
036: import java.io.IOException;
037:
038: import java.net.MalformedURLException;
039: import java.net.URL;
040:
041: import java.util.Date;
042: import java.util.Iterator;
043: import java.util.List;
044:
045: import org.apache.commons.logging.Log;
046: import org.apache.commons.logging.LogFactory;
047:
048: /**
049: * <a href="BookmarksEntryLocalServiceImpl.java.html"><b><i>View Source</i></b>
050: * </a>
051: *
052: * @author Brian Wing Shun Chan
053: *
054: */
055: public class BookmarksEntryLocalServiceImpl extends
056: BookmarksEntryLocalServiceBaseImpl {
057:
058: public BookmarksEntry addEntry(long userId, long folderId,
059: String name, String url, String comments,
060: String[] tagsEntries, boolean addCommunityPermissions,
061: boolean addGuestPermissions) throws PortalException,
062: SystemException {
063:
064: return addEntry(null, userId, folderId, name, url, comments,
065: tagsEntries, Boolean.valueOf(addCommunityPermissions),
066: Boolean.valueOf(addGuestPermissions), null, null);
067: }
068:
069: public BookmarksEntry addEntry(String uuid, long userId,
070: long folderId, String name, String url, String comments,
071: String[] tagsEntries, boolean addCommunityPermissions,
072: boolean addGuestPermissions) throws PortalException,
073: SystemException {
074:
075: return addEntry(uuid, userId, folderId, name, url, comments,
076: tagsEntries, Boolean.valueOf(addCommunityPermissions),
077: Boolean.valueOf(addGuestPermissions), null, null);
078: }
079:
080: public BookmarksEntry addEntry(long userId, long folderId,
081: String name, String url, String comments,
082: String[] tagsEntries, String[] communityPermissions,
083: String[] guestPermissions) throws PortalException,
084: SystemException {
085:
086: return addEntry(null, userId, folderId, name, url, comments,
087: tagsEntries, null, null, communityPermissions,
088: guestPermissions);
089: }
090:
091: public BookmarksEntry addEntry(String uuid, long userId,
092: long folderId, String name, String url, String comments,
093: String[] tagsEntries, Boolean addCommunityPermissions,
094: Boolean addGuestPermissions, String[] communityPermissions,
095: String[] guestPermissions) throws PortalException,
096: SystemException {
097:
098: // Entry
099:
100: User user = userPersistence.findByPrimaryKey(userId);
101: BookmarksFolder folder = bookmarksFolderPersistence
102: .findByPrimaryKey(folderId);
103:
104: if (Validator.isNull(name)) {
105: name = url;
106: }
107:
108: Date now = new Date();
109:
110: validate(url);
111:
112: long entryId = counterLocalService.increment();
113:
114: BookmarksEntry entry = bookmarksEntryPersistence
115: .create(entryId);
116:
117: entry.setUuid(uuid);
118: entry.setCompanyId(user.getCompanyId());
119: entry.setUserId(user.getUserId());
120: entry.setCreateDate(now);
121: entry.setModifiedDate(now);
122: entry.setFolderId(folderId);
123: entry.setName(name);
124: entry.setUrl(url);
125: entry.setComments(comments);
126:
127: bookmarksEntryPersistence.update(entry);
128:
129: // Resources
130:
131: if ((addCommunityPermissions != null)
132: && (addGuestPermissions != null)) {
133:
134: addEntryResources(folder, entry, addCommunityPermissions
135: .booleanValue(), addGuestPermissions.booleanValue());
136: } else {
137: addEntryResources(folder, entry, communityPermissions,
138: guestPermissions);
139: }
140:
141: // Tags
142:
143: updateTagsAsset(userId, entry, tagsEntries);
144:
145: // Lucene
146:
147: try {
148: Indexer
149: .addEntry(entry.getCompanyId(),
150: folder.getGroupId(), folderId, entryId,
151: name, url, comments, tagsEntries);
152: } catch (IOException ioe) {
153: _log.error("Indexing " + entryId, ioe);
154: }
155:
156: return entry;
157: }
158:
159: public void addEntryResources(long folderId, long entryId,
160: boolean addCommunityPermissions, boolean addGuestPermissions)
161: throws PortalException, SystemException {
162:
163: BookmarksFolder folder = bookmarksFolderPersistence
164: .findByPrimaryKey(folderId);
165: BookmarksEntry entry = bookmarksEntryPersistence
166: .findByPrimaryKey(entryId);
167:
168: addEntryResources(folder, entry, addCommunityPermissions,
169: addGuestPermissions);
170: }
171:
172: public void addEntryResources(BookmarksFolder folder,
173: BookmarksEntry entry, boolean addCommunityPermissions,
174: boolean addGuestPermissions) throws PortalException,
175: SystemException {
176:
177: resourceLocalService.addResources(entry.getCompanyId(), folder
178: .getGroupId(), entry.getUserId(), BookmarksEntry.class
179: .getName(), entry.getEntryId(), false,
180: addCommunityPermissions, addGuestPermissions);
181: }
182:
183: public void addEntryResources(long folderId, long entryId,
184: String[] communityPermissions, String[] guestPermissions)
185: throws PortalException, SystemException {
186:
187: BookmarksFolder folder = bookmarksFolderPersistence
188: .findByPrimaryKey(folderId);
189: BookmarksEntry entry = bookmarksEntryPersistence
190: .findByPrimaryKey(entryId);
191:
192: addEntryResources(folder, entry, communityPermissions,
193: guestPermissions);
194: }
195:
196: public void addEntryResources(BookmarksFolder folder,
197: BookmarksEntry entry, String[] communityPermissions,
198: String[] guestPermissions) throws PortalException,
199: SystemException {
200:
201: resourceLocalService.addModelResources(entry.getCompanyId(),
202: folder.getGroupId(), entry.getUserId(),
203: BookmarksEntry.class.getName(), entry.getEntryId(),
204: communityPermissions, guestPermissions);
205: }
206:
207: public void deleteEntries(long folderId) throws PortalException,
208: SystemException {
209:
210: Iterator itr = bookmarksEntryPersistence.findByFolderId(
211: folderId).iterator();
212:
213: while (itr.hasNext()) {
214: BookmarksEntry entry = (BookmarksEntry) itr.next();
215:
216: deleteEntry(entry);
217: }
218: }
219:
220: public void deleteEntry(long entryId) throws PortalException,
221: SystemException {
222:
223: BookmarksEntry entry = bookmarksEntryPersistence
224: .findByPrimaryKey(entryId);
225:
226: deleteEntry(entry);
227: }
228:
229: public void deleteEntry(BookmarksEntry entry)
230: throws PortalException, SystemException {
231:
232: // Lucene
233:
234: try {
235: Indexer.deleteEntry(entry.getCompanyId(), entry
236: .getEntryId());
237: } catch (IOException ioe) {
238: _log.error("Deleting index " + entry.getEntryId(), ioe);
239: }
240:
241: // Tags
242:
243: tagsAssetLocalService.deleteAsset(BookmarksEntry.class
244: .getName(), entry.getEntryId());
245:
246: // Resources
247:
248: resourceLocalService.deleteResource(entry.getCompanyId(),
249: BookmarksEntry.class.getName(),
250: ResourceImpl.SCOPE_INDIVIDUAL, entry.getEntryId());
251:
252: // Entry
253:
254: bookmarksEntryPersistence.remove(entry.getEntryId());
255: }
256:
257: public List getEntries(long folderId, int begin, int end)
258: throws SystemException {
259:
260: return bookmarksEntryPersistence.findByFolderId(folderId,
261: begin, end);
262: }
263:
264: public List getEntries(long folderId, int begin, int end,
265: OrderByComparator orderByComparator) throws SystemException {
266:
267: return bookmarksEntryPersistence.findByFolderId(folderId,
268: begin, end, orderByComparator);
269: }
270:
271: public int getEntriesCount(long folderId) throws SystemException {
272: return bookmarksEntryPersistence.countByFolderId(folderId);
273: }
274:
275: public BookmarksEntry getEntry(long entryId)
276: throws PortalException, SystemException {
277:
278: return bookmarksEntryPersistence.findByPrimaryKey(entryId);
279: }
280:
281: public int getFoldersEntriesCount(List folderIds)
282: throws SystemException {
283:
284: return bookmarksEntryFinder.countByFolderIds(folderIds);
285: }
286:
287: public List getGroupEntries(long groupId, int begin, int end)
288: throws SystemException {
289:
290: return bookmarksEntryFinder.findByGroupId(groupId, begin, end);
291: }
292:
293: public List getGroupEntries(long groupId, long userId, int begin,
294: int end) throws SystemException {
295:
296: if (userId <= 0) {
297: return bookmarksEntryFinder.findByGroupId(groupId, begin,
298: end);
299: } else {
300: return bookmarksEntryFinder.findByG_U(groupId, userId,
301: begin, end);
302: }
303: }
304:
305: public int getGroupEntriesCount(long groupId)
306: throws SystemException {
307: return bookmarksEntryFinder.countByGroupId(groupId);
308: }
309:
310: public int getGroupEntriesCount(long groupId, long userId)
311: throws SystemException {
312:
313: if (userId <= 0) {
314: return bookmarksEntryFinder.countByGroupId(groupId);
315: } else {
316: return bookmarksEntryFinder.countByG_U(groupId, userId);
317: }
318: }
319:
320: public List getNoAssetEntries() throws SystemException {
321: return bookmarksEntryFinder.findByNoAssets();
322: }
323:
324: public BookmarksEntry openEntry(long entryId)
325: throws PortalException, SystemException {
326:
327: BookmarksEntry entry = bookmarksEntryPersistence
328: .findByPrimaryKey(entryId);
329:
330: entry.setVisits(entry.getVisits() + 1);
331:
332: bookmarksEntryPersistence.update(entry);
333:
334: return entry;
335: }
336:
337: public BookmarksEntry updateEntry(long userId, long entryId,
338: long folderId, String name, String url, String comments,
339: String[] tagsEntries) throws PortalException,
340: SystemException {
341:
342: // Entry
343:
344: BookmarksEntry entry = bookmarksEntryPersistence
345: .findByPrimaryKey(entryId);
346:
347: BookmarksFolder folder = getFolder(entry, folderId);
348:
349: if (Validator.isNull(name)) {
350: name = url;
351: }
352:
353: validate(url);
354:
355: entry.setModifiedDate(new Date());
356: entry.setFolderId(folder.getFolderId());
357: entry.setName(name);
358: entry.setUrl(url);
359: entry.setComments(comments);
360:
361: bookmarksEntryPersistence.update(entry);
362:
363: // Tags
364:
365: updateTagsAsset(userId, entry, tagsEntries);
366:
367: // Lucene
368:
369: try {
370: Indexer.updateEntry(entry.getCompanyId(), folder
371: .getGroupId(), entry.getFolderId(), entry
372: .getEntryId(), name, url, comments, tagsEntries);
373: } catch (IOException ioe) {
374: _log.error("Indexing " + entryId, ioe);
375: }
376:
377: return entry;
378: }
379:
380: public void updateTagsAsset(long userId, BookmarksEntry entry,
381: String[] tagsEntries) throws PortalException,
382: SystemException {
383:
384: tagsAssetLocalService.updateAsset(userId, entry.getFolder()
385: .getGroupId(), BookmarksEntry.class.getName(), entry
386: .getEntryId(), tagsEntries, null, null, null, null,
387: ContentTypes.TEXT_PLAIN, entry.getName(), entry
388: .getComments(), null, entry.getUrl(), 0, 0,
389: null, false);
390: }
391:
392: protected BookmarksFolder getFolder(BookmarksEntry entry,
393: long folderId) throws PortalException, SystemException {
394:
395: if (entry.getFolderId() != folderId) {
396: BookmarksFolder oldFolder = bookmarksFolderPersistence
397: .findByPrimaryKey(entry.getFolderId());
398:
399: BookmarksFolder newFolder = bookmarksFolderPersistence
400: .fetchByPrimaryKey(folderId);
401:
402: if ((newFolder == null)
403: || (oldFolder.getGroupId() != newFolder
404: .getGroupId())) {
405:
406: folderId = entry.getFolderId();
407: }
408: }
409:
410: return bookmarksFolderPersistence.findByPrimaryKey(folderId);
411: }
412:
413: protected void validate(String url) throws PortalException {
414: if (Validator.isNull(url)) {
415: throw new EntryURLException();
416: } else {
417: try {
418: new URL(url);
419: } catch (MalformedURLException murle) {
420: throw new EntryURLException();
421: }
422: }
423: }
424:
425: private static Log _log = LogFactory
426: .getLog(BookmarksEntryLocalServiceImpl.class);
427:
428: }
|