001
014
015 package com.liferay.portlet.bookmarks.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.search.Indexer;
020 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
021 import com.liferay.portal.kernel.util.ArrayUtil;
022 import com.liferay.portal.kernel.util.ContentTypes;
023 import com.liferay.portal.kernel.util.OrderByComparator;
024 import com.liferay.portal.kernel.util.Validator;
025 import com.liferay.portal.model.ResourceConstants;
026 import com.liferay.portal.model.User;
027 import com.liferay.portal.service.ServiceContext;
028 import com.liferay.portlet.asset.model.AssetEntry;
029 import com.liferay.portlet.asset.model.AssetLinkConstants;
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.model.BookmarksFolderConstants;
034 import com.liferay.portlet.bookmarks.service.base.BookmarksEntryLocalServiceBaseImpl;
035 import com.liferay.portlet.bookmarks.util.comparator.EntryModifiedDateComparator;
036
037 import java.util.Date;
038 import java.util.Iterator;
039 import java.util.List;
040
041
045 public class BookmarksEntryLocalServiceImpl
046 extends BookmarksEntryLocalServiceBaseImpl {
047
048 public BookmarksEntry addEntry(
049 long userId, long groupId, long folderId, String name, String url,
050 String description, ServiceContext serviceContext)
051 throws PortalException, SystemException {
052
053
054
055 User user = userPersistence.findByPrimaryKey(userId);
056
057 if (Validator.isNull(name)) {
058 name = url;
059 }
060
061 Date now = new Date();
062
063 validate(url);
064
065 long entryId = counterLocalService.increment();
066
067 BookmarksEntry entry = bookmarksEntryPersistence.create(entryId);
068
069 entry.setUuid(serviceContext.getUuid());
070 entry.setGroupId(groupId);
071 entry.setCompanyId(user.getCompanyId());
072 entry.setUserId(user.getUserId());
073 entry.setUserName(user.getFullName());
074 entry.setCreateDate(serviceContext.getCreateDate(now));
075 entry.setModifiedDate(serviceContext.getModifiedDate(now));
076 entry.setFolderId(folderId);
077 entry.setName(name);
078 entry.setUrl(url);
079 entry.setDescription(description);
080 entry.setExpandoBridgeAttributes(serviceContext);
081
082 bookmarksEntryPersistence.update(entry, false);
083
084
085
086 resourceLocalService.addModelResources(entry, serviceContext);
087
088
089
090 updateAsset(
091 userId, entry, serviceContext.getAssetCategoryIds(),
092 serviceContext.getAssetTagNames(),
093 serviceContext.getAssetLinkEntryIds());
094
095
096
097 Indexer indexer = IndexerRegistryUtil.getIndexer(BookmarksEntry.class);
098
099 indexer.reindex(entry);
100
101 return entry;
102 }
103
104 public void deleteEntries(long groupId, long folderId)
105 throws PortalException, SystemException {
106
107 Iterator<BookmarksEntry> itr = bookmarksEntryPersistence.findByG_F(
108 groupId, folderId).iterator();
109
110 while (itr.hasNext()) {
111 BookmarksEntry entry = itr.next();
112
113 deleteEntry(entry);
114 }
115 }
116
117 public void deleteEntry(BookmarksEntry entry)
118 throws PortalException, SystemException {
119
120
121
122 bookmarksEntryPersistence.remove(entry);
123
124
125
126 resourceLocalService.deleteResource(
127 entry, ResourceConstants.SCOPE_INDIVIDUAL);
128
129
130
131 assetEntryLocalService.deleteEntry(
132 BookmarksEntry.class.getName(), entry.getEntryId());
133
134
135
136 expandoValueLocalService.deleteValues(
137 BookmarksEntry.class.getName(), entry.getEntryId());
138
139
140
141 Indexer indexer = IndexerRegistryUtil.getIndexer(BookmarksEntry.class);
142
143 indexer.delete(entry);
144 }
145
146 public void deleteEntry(long entryId)
147 throws PortalException, SystemException {
148
149 BookmarksEntry entry =
150 bookmarksEntryPersistence.findByPrimaryKey(entryId);
151
152 deleteEntry(entry);
153 }
154
155 public List<BookmarksEntry> getEntries(
156 long groupId, long folderId, int start, int end)
157 throws SystemException {
158
159 return bookmarksEntryPersistence.findByG_F(
160 groupId, folderId, start, end);
161 }
162
163 public List<BookmarksEntry> getEntries(
164 long groupId, long folderId, int start, int end,
165 OrderByComparator orderByComparator)
166 throws SystemException {
167
168 return bookmarksEntryPersistence.findByG_F(
169 groupId, folderId, start, end, orderByComparator);
170 }
171
172 public int getEntriesCount(long groupId, long folderId)
173 throws SystemException {
174
175 return bookmarksEntryPersistence.countByG_F(groupId, folderId);
176 }
177
178 public BookmarksEntry getEntry(long entryId)
179 throws PortalException, SystemException {
180
181 return bookmarksEntryPersistence.findByPrimaryKey(entryId);
182 }
183
184 public int getFoldersEntriesCount(long groupId, List<Long> folderIds)
185 throws SystemException {
186
187 return bookmarksEntryPersistence.countByG_F(
188 groupId,
189 ArrayUtil.toArray(folderIds.toArray(new Long[folderIds.size()])));
190 }
191
192 public List<BookmarksEntry> getGroupEntries(
193 long groupId, int start, int end)
194 throws SystemException {
195
196 return bookmarksEntryPersistence.findByGroupId(
197 groupId, start, end, new EntryModifiedDateComparator());
198 }
199
200 public List<BookmarksEntry> getGroupEntries(
201 long groupId, long userId, int start, int end)
202 throws SystemException {
203
204 OrderByComparator orderByComparator = new EntryModifiedDateComparator();
205
206 if (userId <= 0) {
207 return bookmarksEntryPersistence.findByGroupId(
208 groupId, start, end, orderByComparator);
209 }
210 else {
211 return bookmarksEntryPersistence.findByG_U(
212 groupId, userId, start, end, orderByComparator);
213 }
214 }
215
216 public int getGroupEntriesCount(long groupId) throws SystemException {
217 return bookmarksEntryPersistence.countByGroupId(groupId);
218 }
219
220 public int getGroupEntriesCount(long groupId, long userId)
221 throws SystemException {
222
223 if (userId <= 0) {
224 return bookmarksEntryPersistence.countByGroupId(groupId);
225 }
226 else {
227 return bookmarksEntryPersistence.countByG_U(groupId, userId);
228 }
229 }
230
231 public List<BookmarksEntry> getNoAssetEntries() throws SystemException {
232 return bookmarksEntryFinder.findByNoAssets();
233 }
234
235 public BookmarksEntry openEntry(long userId, long entryId)
236 throws PortalException, SystemException {
237
238 BookmarksEntry entry =
239 bookmarksEntryPersistence.findByPrimaryKey(entryId);
240
241 entry.setVisits(entry.getVisits() + 1);
242
243 bookmarksEntryPersistence.update(entry, false);
244
245 assetEntryLocalService.incrementViewCounter(
246 userId, BookmarksEntry.class.getName(), entryId, 1);
247
248 return entry;
249 }
250
251 public void updateAsset(
252 long userId, BookmarksEntry entry, long[] assetCategoryIds,
253 String[] assetTagNames, long[] assetLinkEntryIds)
254 throws PortalException, SystemException {
255
256 AssetEntry assetEntry = assetEntryLocalService.updateEntry(
257 userId, entry.getGroupId(), BookmarksEntry.class.getName(),
258 entry.getEntryId(), entry.getUuid(), 0, assetCategoryIds,
259 assetTagNames, true, null, null, null, null,
260 ContentTypes.TEXT_PLAIN, entry.getName(), entry.getDescription(),
261 null, entry.getUrl(), null, 0, 0, null, false);
262
263 assetLinkLocalService.updateLinks(
264 userId, assetEntry.getEntryId(), assetLinkEntryIds,
265 AssetLinkConstants.TYPE_RELATED);
266 }
267
268 public BookmarksEntry updateEntry(
269 long userId, long entryId, long groupId, long folderId, String name,
270 String url, String description, ServiceContext serviceContext)
271 throws PortalException, SystemException {
272
273
274
275 BookmarksEntry entry =
276 bookmarksEntryPersistence.findByPrimaryKey(entryId);
277
278 if (Validator.isNull(name)) {
279 name = url;
280 }
281
282 validate(url);
283
284 entry.setModifiedDate(serviceContext.getModifiedDate(null));
285 entry.setFolderId(folderId);
286 entry.setName(name);
287 entry.setUrl(url);
288 entry.setDescription(description);
289 entry.setExpandoBridgeAttributes(serviceContext);
290
291 bookmarksEntryPersistence.update(entry, false);
292
293
294
295 updateAsset(
296 userId, entry, serviceContext.getAssetCategoryIds(),
297 serviceContext.getAssetTagNames(),
298 serviceContext.getAssetLinkEntryIds());
299
300
301
302 Indexer indexer = IndexerRegistryUtil.getIndexer(BookmarksEntry.class);
303
304 indexer.reindex(entry);
305
306 return entry;
307 }
308
309 protected long getFolder(BookmarksEntry entry, long folderId)
310 throws SystemException {
311
312 if ((entry.getFolderId() != folderId) &&
313 (folderId != BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
314
315 BookmarksFolder newFolder =
316 bookmarksFolderPersistence.fetchByPrimaryKey(folderId);
317
318 if ((newFolder == null) ||
319 (entry.getGroupId() != newFolder.getGroupId())) {
320
321 folderId = entry.getFolderId();
322 }
323 }
324
325 return folderId;
326 }
327
328 protected void validate(String url) throws PortalException {
329 if (!Validator.isUrl(url)) {
330 throw new EntryURLException();
331 }
332 }
333
334 }