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.Validator;
022 import com.liferay.portal.model.ResourceConstants;
023 import com.liferay.portal.model.User;
024 import com.liferay.portal.service.ServiceContext;
025 import com.liferay.portlet.bookmarks.FolderNameException;
026 import com.liferay.portlet.bookmarks.model.BookmarksEntry;
027 import com.liferay.portlet.bookmarks.model.BookmarksFolder;
028 import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
029 import com.liferay.portlet.bookmarks.service.base.BookmarksFolderLocalServiceBaseImpl;
030
031 import java.util.ArrayList;
032 import java.util.Date;
033 import java.util.List;
034
035
039 public class BookmarksFolderLocalServiceImpl
040 extends BookmarksFolderLocalServiceBaseImpl {
041
042 public BookmarksFolder addFolder(
043 long userId, long parentFolderId, String name, String description,
044 ServiceContext serviceContext)
045 throws PortalException, SystemException {
046
047
048
049 User user = userPersistence.findByPrimaryKey(userId);
050 long groupId = serviceContext.getScopeGroupId();
051 parentFolderId = getParentFolderId(groupId, parentFolderId);
052 Date now = new Date();
053
054 validate(name);
055
056 long folderId = counterLocalService.increment();
057
058 BookmarksFolder folder = bookmarksFolderPersistence.create(folderId);
059
060 folder.setUuid(serviceContext.getUuid());
061 folder.setGroupId(groupId);
062 folder.setCompanyId(user.getCompanyId());
063 folder.setUserId(user.getUserId());
064 folder.setUserName(user.getFullName());
065 folder.setCreateDate(serviceContext.getCreateDate(now));
066 folder.setModifiedDate(serviceContext.getModifiedDate(now));
067 folder.setParentFolderId(parentFolderId);
068 folder.setName(name);
069 folder.setDescription(description);
070 folder.setExpandoBridgeAttributes(serviceContext);
071
072 bookmarksFolderPersistence.update(folder, false);
073
074
075
076 resourceLocalService.addModelResources(folder, serviceContext);
077
078 return folder;
079 }
080
081 public void deleteFolder(BookmarksFolder folder)
082 throws PortalException, SystemException {
083
084
085
086 List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
087 folder.getGroupId(), folder.getFolderId());
088
089 for (BookmarksFolder curFolder : folders) {
090 deleteFolder(curFolder);
091 }
092
093
094
095 bookmarksFolderPersistence.remove(folder);
096
097
098
099 resourceLocalService.deleteResource(
100 folder, ResourceConstants.SCOPE_INDIVIDUAL);
101
102
103
104 bookmarksEntryLocalService.deleteEntries(
105 folder.getGroupId(), folder.getFolderId());
106
107
108
109 expandoValueLocalService.deleteValues(
110 BookmarksFolder.class.getName(), folder.getFolderId());
111 }
112
113 public void deleteFolder(long folderId)
114 throws PortalException, SystemException {
115
116 BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
117 folderId);
118
119 deleteFolder(folder);
120 }
121
122 public void deleteFolders(long groupId)
123 throws PortalException, SystemException {
124
125 List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
126 groupId, BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID);
127
128 for (BookmarksFolder folder : folders) {
129 deleteFolder(folder);
130 }
131 }
132
133 public List<BookmarksFolder> getCompanyFolders(
134 long companyId, int start, int end)
135 throws SystemException {
136
137 return bookmarksFolderPersistence.findByCompanyId(
138 companyId, start, end);
139 }
140
141 public int getCompanyFoldersCount(long companyId) throws SystemException {
142 return bookmarksFolderPersistence.countByCompanyId(companyId);
143 }
144
145 public BookmarksFolder getFolder(long folderId)
146 throws PortalException, SystemException {
147
148 return bookmarksFolderPersistence.findByPrimaryKey(folderId);
149 }
150
151 public List<BookmarksFolder> getFolders(long groupId)
152 throws SystemException {
153
154 return bookmarksFolderPersistence.findByGroupId(groupId);
155 }
156
157 public List<BookmarksFolder> getFolders(long groupId, long parentFolderId)
158 throws SystemException {
159
160 return bookmarksFolderPersistence.findByG_P(groupId, parentFolderId);
161 }
162
163 public List<BookmarksFolder> getFolders(
164 long groupId, long parentFolderId, int start, int end)
165 throws SystemException {
166
167 return bookmarksFolderPersistence.findByG_P(
168 groupId, parentFolderId, start, end);
169 }
170
171 public int getFoldersCount(long groupId, long parentFolderId)
172 throws SystemException {
173
174 return bookmarksFolderPersistence.countByG_P(groupId, parentFolderId);
175 }
176
177 public void getSubfolderIds(
178 List<Long> folderIds, long groupId, long folderId)
179 throws SystemException {
180
181 List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
182 groupId, folderId);
183
184 for (BookmarksFolder folder : folders) {
185 folderIds.add(folder.getFolderId());
186
187 getSubfolderIds(
188 folderIds, folder.getGroupId(), folder.getFolderId());
189 }
190 }
191
192 public BookmarksFolder updateFolder(
193 long folderId, long parentFolderId, String name, String description,
194 boolean mergeWithParentFolder, ServiceContext serviceContext)
195 throws PortalException, SystemException {
196
197
198
199 BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
200 folderId);
201
202 parentFolderId = getParentFolderId(folder, parentFolderId);
203
204 if (mergeWithParentFolder && (folderId != parentFolderId)) {
205 mergeFolders(folder, parentFolderId);
206
207 return folder;
208 }
209
210
211
212 validate(name);
213
214 folder.setModifiedDate(serviceContext.getModifiedDate(null));
215 folder.setParentFolderId(parentFolderId);
216 folder.setName(name);
217 folder.setDescription(description);
218 folder.setExpandoBridgeAttributes(serviceContext);
219
220 bookmarksFolderPersistence.update(folder, false);
221
222 return folder;
223 }
224
225 protected long getParentFolderId(
226 BookmarksFolder folder, long parentFolderId)
227 throws SystemException {
228
229 if (parentFolderId ==
230 BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
231
232 return parentFolderId;
233 }
234
235 if (folder.getFolderId() == parentFolderId) {
236 return folder.getParentFolderId();
237 }
238 else {
239 BookmarksFolder parentFolder =
240 bookmarksFolderPersistence.fetchByPrimaryKey(parentFolderId);
241
242 if ((parentFolder == null) ||
243 (folder.getGroupId() != parentFolder.getGroupId())) {
244
245 return folder.getParentFolderId();
246 }
247
248 List<Long> subfolderIds = new ArrayList<Long>();
249
250 getSubfolderIds(
251 subfolderIds, folder.getGroupId(), folder.getFolderId());
252
253 if (subfolderIds.contains(parentFolderId)) {
254 return folder.getParentFolderId();
255 }
256
257 return parentFolderId;
258 }
259 }
260
261 protected long getParentFolderId(long groupId, long parentFolderId)
262 throws SystemException {
263
264 if (parentFolderId !=
265 BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
266
267 BookmarksFolder parentFolder =
268 bookmarksFolderPersistence.fetchByPrimaryKey(parentFolderId);
269
270 if ((parentFolder == null) ||
271 (groupId != parentFolder.getGroupId())) {
272
273 parentFolderId =
274 BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID;
275 }
276 }
277
278 return parentFolderId;
279 }
280
281 protected void mergeFolders(BookmarksFolder fromFolder, long toFolderId)
282 throws PortalException, SystemException {
283
284 List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
285 fromFolder.getGroupId(), fromFolder.getFolderId());
286
287 for (BookmarksFolder folder : folders) {
288 mergeFolders(folder, toFolderId);
289 }
290
291 List<BookmarksEntry> entries = bookmarksEntryPersistence.findByG_F(
292 fromFolder.getGroupId(), fromFolder.getFolderId());
293
294 for (BookmarksEntry entry : entries) {
295 entry.setFolderId(toFolderId);
296
297 bookmarksEntryPersistence.update(entry, false);
298
299 Indexer indexer = IndexerRegistryUtil.getIndexer(
300 BookmarksEntry.class);
301
302 indexer.reindex(entry);
303 }
304
305 deleteFolder(fromFolder);
306 }
307
308 protected void validate(String name) throws PortalException {
309 if ((Validator.isNull(name)) || (name.indexOf("\\\\") != -1) ||
310 (name.indexOf("
311
312 throw new FolderNameException();
313 }
314 }
315
316 }