001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.bookmarks.service.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryDefinition;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.json.JSONFactoryUtil;
021    import com.liferay.portal.kernel.json.JSONObject;
022    import com.liferay.portal.kernel.search.Indexable;
023    import com.liferay.portal.kernel.search.IndexableType;
024    import com.liferay.portal.kernel.search.Indexer;
025    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
026    import com.liferay.portal.kernel.systemevent.SystemEvent;
027    import com.liferay.portal.kernel.util.ContentTypes;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.kernel.workflow.WorkflowConstants;
030    import com.liferay.portal.model.ResourceConstants;
031    import com.liferay.portal.model.SystemEventConstants;
032    import com.liferay.portal.model.User;
033    import com.liferay.portal.service.ServiceContext;
034    import com.liferay.portlet.asset.model.AssetEntry;
035    import com.liferay.portlet.asset.model.AssetLinkConstants;
036    import com.liferay.portlet.bookmarks.FolderNameException;
037    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
038    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
039    import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
040    import com.liferay.portlet.bookmarks.service.base.BookmarksFolderLocalServiceBaseImpl;
041    import com.liferay.portlet.social.model.SocialActivityConstants;
042    import com.liferay.portlet.trash.model.TrashEntry;
043    import com.liferay.portlet.trash.model.TrashVersion;
044    
045    import java.util.ArrayList;
046    import java.util.Date;
047    import java.util.List;
048    
049    /**
050     * @author Brian Wing Shun Chan
051     * @author Wesley Gong
052     */
053    public class BookmarksFolderLocalServiceImpl
054            extends BookmarksFolderLocalServiceBaseImpl {
055    
056            @Override
057            public BookmarksFolder addFolder(
058                            long userId, long parentFolderId, String name, String description,
059                            ServiceContext serviceContext)
060                    throws PortalException, SystemException {
061    
062                    // Folder
063    
064                    User user = userPersistence.findByPrimaryKey(userId);
065                    long groupId = serviceContext.getScopeGroupId();
066                    parentFolderId = getParentFolderId(groupId, parentFolderId);
067                    Date now = new Date();
068    
069                    validate(name);
070    
071                    long folderId = counterLocalService.increment();
072    
073                    BookmarksFolder folder = bookmarksFolderPersistence.create(folderId);
074    
075                    folder.setUuid(serviceContext.getUuid());
076                    folder.setGroupId(groupId);
077                    folder.setCompanyId(user.getCompanyId());
078                    folder.setUserId(user.getUserId());
079                    folder.setUserName(user.getFullName());
080                    folder.setCreateDate(serviceContext.getCreateDate(now));
081                    folder.setModifiedDate(serviceContext.getModifiedDate(now));
082                    folder.setParentFolderId(parentFolderId);
083                    folder.setTreePath(folder.buildTreePath());
084                    folder.setName(name);
085                    folder.setDescription(description);
086                    folder.setExpandoBridgeAttributes(serviceContext);
087    
088                    bookmarksFolderPersistence.update(folder);
089    
090                    // Resources
091    
092                    resourceLocalService.addModelResources(folder, serviceContext);
093    
094                    // Asset
095    
096                    updateAsset(
097                            userId, folder, serviceContext.getAssetCategoryIds(),
098                            serviceContext.getAssetTagNames(),
099                            serviceContext.getAssetLinkEntryIds());
100    
101                    return folder;
102            }
103    
104            @Indexable(type = IndexableType.DELETE)
105            @Override
106            @SystemEvent(
107                    action = SystemEventConstants.ACTION_SKIP, send = false,
108                    type = SystemEventConstants.TYPE_DELETE)
109            public BookmarksFolder deleteFolder(BookmarksFolder folder)
110                    throws PortalException, SystemException {
111    
112                    return deleteFolder(folder, true);
113            }
114    
115            @Indexable(type = IndexableType.DELETE)
116            @Override
117            @SystemEvent(
118                    action = SystemEventConstants.ACTION_SKIP, send = false,
119                    type = SystemEventConstants.TYPE_DELETE)
120            public BookmarksFolder deleteFolder(
121                            BookmarksFolder folder, boolean includeTrashedEntries)
122                    throws PortalException, SystemException {
123    
124                    List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
125                            folder.getGroupId(), folder.getFolderId());
126    
127                    for (BookmarksFolder curFolder : folders) {
128                            if (includeTrashedEntries || !curFolder.isInTrash()) {
129                                    deleteFolder(curFolder);
130                            }
131                    }
132    
133                    // Folder
134    
135                    bookmarksFolderPersistence.remove(folder);
136    
137                    // Resources
138    
139                    resourceLocalService.deleteResource(
140                            folder, ResourceConstants.SCOPE_INDIVIDUAL);
141    
142                    // Entries
143    
144                    bookmarksEntryLocalService.deleteEntries(
145                            folder.getGroupId(), folder.getFolderId(), includeTrashedEntries);
146    
147                    // Asset
148    
149                    assetEntryLocalService.deleteEntry(
150                            BookmarksFolder.class.getName(), folder.getFolderId());
151    
152                    // Expando
153    
154                    expandoRowLocalService.deleteRows(folder.getFolderId());
155    
156                    // Subscriptions
157    
158                    subscriptionLocalService.deleteSubscriptions(
159                            folder.getCompanyId(), BookmarksFolder.class.getName(),
160                            folder.getFolderId());
161    
162                    // Trash
163    
164                    trashEntryLocalService.deleteEntry(
165                            BookmarksFolder.class.getName(), folder.getFolderId());
166    
167                    return folder;
168            }
169    
170            @Indexable(type = IndexableType.DELETE)
171            @Override
172            public BookmarksFolder deleteFolder(long folderId)
173                    throws PortalException, SystemException {
174    
175                    BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
176                            folderId);
177    
178                    return bookmarksFolderLocalService.deleteFolder(folder);
179            }
180    
181            @Indexable(type = IndexableType.DELETE)
182            @Override
183            public BookmarksFolder deleteFolder(
184                            long folderId, boolean includeTrashedEntries)
185                    throws PortalException, SystemException {
186    
187                    BookmarksFolder folder = bookmarksFolderLocalService.getFolder(
188                            folderId);
189    
190                    return bookmarksFolderLocalService.deleteFolder(
191                            folder, includeTrashedEntries);
192            }
193    
194            @Override
195            public void deleteFolders(long groupId)
196                    throws PortalException, SystemException {
197    
198                    List<BookmarksFolder> folders =
199                            bookmarksFolderPersistence.findByGroupId(groupId);
200    
201                    for (BookmarksFolder folder : folders) {
202                            bookmarksFolderLocalService.deleteFolder(folder);
203                    }
204            }
205    
206            @Override
207            public List<BookmarksFolder> getCompanyFolders(
208                            long companyId, int start, int end)
209                    throws SystemException {
210    
211                    return bookmarksFolderPersistence.findByCompanyId(
212                            companyId, start, end);
213            }
214    
215            @Override
216            public int getCompanyFoldersCount(long companyId) throws SystemException {
217                    return bookmarksFolderPersistence.countByCompanyId(companyId);
218            }
219    
220            @Override
221            public BookmarksFolder getFolder(long folderId)
222                    throws PortalException, SystemException {
223    
224                    return bookmarksFolderPersistence.findByPrimaryKey(folderId);
225            }
226    
227            @Override
228            public List<BookmarksFolder> getFolders(long groupId)
229                    throws SystemException {
230    
231                    return bookmarksFolderPersistence.findByGroupId(groupId);
232            }
233    
234            @Override
235            public List<BookmarksFolder> getFolders(long groupId, long parentFolderId)
236                    throws SystemException {
237    
238                    return bookmarksFolderPersistence.findByG_P(groupId, parentFolderId);
239            }
240    
241            @Override
242            public List<BookmarksFolder> getFolders(
243                            long groupId, long parentFolderId, int start, int end)
244                    throws SystemException {
245    
246                    return getFolders(
247                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED, start,
248                            end);
249            }
250    
251            @Override
252            public List<BookmarksFolder> getFolders(
253                            long groupId, long parentFolderId, int status, int start, int end)
254                    throws SystemException {
255    
256                    return bookmarksFolderPersistence.findByG_P_S(
257                            groupId, parentFolderId, status, start, end);
258            }
259    
260            @Override
261            public List<Object> getFoldersAndEntries(long groupId, long folderId)
262                    throws SystemException {
263    
264                    return getFoldersAndEntries(
265                            groupId, folderId, WorkflowConstants.STATUS_ANY);
266            }
267    
268            @Override
269            public List<Object> getFoldersAndEntries(
270                            long groupId, long folderId, int status)
271                    throws SystemException {
272    
273                    QueryDefinition queryDefinition = new QueryDefinition(status);
274    
275                    return bookmarksFolderFinder.findF_E_ByG_F(
276                            groupId, folderId, queryDefinition);
277            }
278    
279            @Override
280            public List<Object> getFoldersAndEntries(
281                            long groupId, long folderId, int status, int start, int end)
282                    throws SystemException {
283    
284                    QueryDefinition queryDefinition = new QueryDefinition(
285                            status, start, end, null);
286    
287                    return bookmarksFolderFinder.findF_E_ByG_F(
288                            groupId, folderId, queryDefinition);
289            }
290    
291            @Override
292            public int getFoldersAndEntriesCount(
293                            long groupId, long folderId, int status)
294                    throws SystemException {
295    
296                    QueryDefinition queryDefinition = new QueryDefinition(status);
297    
298                    return bookmarksFolderFinder.countF_E_ByG_F(
299                            groupId, folderId, queryDefinition);
300            }
301    
302            @Override
303            public int getFoldersCount(long groupId, long parentFolderId)
304                    throws SystemException {
305    
306                    return getFoldersCount(
307                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED);
308            }
309    
310            @Override
311            public int getFoldersCount(long groupId, long parentFolderId, int status)
312                    throws SystemException {
313    
314                    return bookmarksFolderPersistence.countByG_P_S(
315                            groupId, parentFolderId, status);
316            }
317    
318            @Override
319            public List<BookmarksFolder> getNoAssetFolders() throws SystemException {
320                    return bookmarksFolderFinder.findByNoAssets();
321            }
322    
323            @Override
324            public void getSubfolderIds(
325                            List<Long> folderIds, long groupId, long folderId)
326                    throws SystemException {
327    
328                    List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
329                            groupId, folderId);
330    
331                    for (BookmarksFolder folder : folders) {
332                            folderIds.add(folder.getFolderId());
333    
334                            getSubfolderIds(
335                                    folderIds, folder.getGroupId(), folder.getFolderId());
336                    }
337            }
338    
339            @Indexable(type = IndexableType.REINDEX)
340            @Override
341            public BookmarksFolder moveFolder(long folderId, long parentFolderId)
342                    throws PortalException, SystemException {
343    
344                    BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
345                            folderId);
346    
347                    folder.setParentFolderId(parentFolderId);
348                    folder.setTreePath(folder.buildTreePath());
349    
350                    bookmarksFolderPersistence.update(folder);
351    
352                    return folder;
353            }
354    
355            @Override
356            public BookmarksFolder moveFolderFromTrash(
357                            long userId, long folderId, long parentFolderId)
358                    throws PortalException, SystemException {
359    
360                    BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
361                            folderId);
362    
363                    TrashEntry trashEntry = folder.getTrashEntry();
364    
365                    if (trashEntry.isTrashEntry(BookmarksFolder.class, folderId)) {
366                            restoreFolderFromTrash(userId, folderId);
367                    }
368                    else {
369    
370                            // Folder
371    
372                            TrashVersion trashVersion =
373                                    trashVersionLocalService.fetchVersion(
374                                            trashEntry.getEntryId(), BookmarksFolder.class.getName(),
375                                            folderId);
376    
377                            int status = WorkflowConstants.STATUS_APPROVED;
378    
379                            if (trashVersion != null) {
380                                    status = trashVersion.getStatus();
381                            }
382    
383                            updateStatus(userId, folder, status);
384    
385                            // Trash
386    
387                            if (trashVersion != null) {
388                                    trashVersionLocalService.deleteTrashVersion(trashVersion);
389                            }
390    
391                            // Folders and entries
392    
393                            List<Object> foldersAndEntries =
394                                    bookmarksFolderLocalService.getFoldersAndEntries(
395                                            folder.getGroupId(), folder.getFolderId(),
396                                            WorkflowConstants.STATUS_IN_TRASH);
397    
398                            restoreDependentsFromTrash(
399                                    foldersAndEntries, trashEntry.getEntryId());
400                    }
401    
402                    return bookmarksFolderLocalService.moveFolder(folderId, parentFolderId);
403            }
404    
405            @Indexable(type = IndexableType.REINDEX)
406            @Override
407            public BookmarksFolder moveFolderToTrash(long userId, long folderId)
408                    throws PortalException, SystemException {
409    
410                    // Folder
411    
412                    BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
413                            folderId);
414    
415                    int oldStatus = folder.getStatus();
416    
417                    folder = updateStatus(
418                            userId, folder, WorkflowConstants.STATUS_IN_TRASH);
419    
420                    // Trash
421    
422                    TrashEntry trashEntry = trashEntryLocalService.addTrashEntry(
423                            userId, folder.getGroupId(), BookmarksFolder.class.getName(),
424                            folder.getFolderId(), folder.getUuid(), null, oldStatus, null,
425                            null);
426    
427                    // Folders and entries
428    
429                    List<Object> foldersAndEntries =
430                            bookmarksFolderLocalService.getFoldersAndEntries(
431                                    folder.getGroupId(), folder.getFolderId());
432    
433                    moveDependentsToTrash(foldersAndEntries, trashEntry.getEntryId());
434    
435                    // Social
436    
437                    JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
438    
439                    extraDataJSONObject.put("title", folder.getName());
440    
441                    socialActivityLocalService.addActivity(
442                            userId, folder.getGroupId(), BookmarksFolder.class.getName(),
443                            folder.getFolderId(), SocialActivityConstants.TYPE_MOVE_TO_TRASH,
444                            extraDataJSONObject.toString(), 0);
445    
446                    return folder;
447            }
448    
449            @Override
450            public void rebuildTree(long companyId)
451                    throws PortalException, SystemException {
452    
453                    List<BookmarksFolder> folders = bookmarksFolderPersistence.findByC_NotS(
454                            companyId, WorkflowConstants.STATUS_IN_TRASH);
455    
456                    for (BookmarksFolder folder : folders) {
457                            folder.setTreePath(folder.buildTreePath());
458    
459                            bookmarksFolderPersistence.update(folder);
460                    }
461            }
462    
463            @Indexable(type = IndexableType.REINDEX)
464            @Override
465            public void restoreFolderFromTrash(long userId, long folderId)
466                    throws PortalException, SystemException {
467    
468                    // Folder
469    
470                    BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
471                            folderId);
472    
473                    TrashEntry trashEntry = trashEntryLocalService.getEntry(
474                            BookmarksFolder.class.getName(), folderId);
475    
476                    updateStatus(userId, folder, trashEntry.getStatus());
477    
478                    // Folders and entries
479    
480                    List<Object> foldersAndEntries =
481                            bookmarksFolderLocalService.getFoldersAndEntries(
482                                    folder.getGroupId(), folder.getFolderId(),
483                                    WorkflowConstants.STATUS_IN_TRASH);
484    
485                    restoreDependentsFromTrash(foldersAndEntries, trashEntry.getEntryId());
486    
487                    // Trash
488    
489                    trashEntryLocalService.deleteEntry(trashEntry.getEntryId());
490    
491                    // Social
492    
493                    JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
494    
495                    extraDataJSONObject.put("title", folder.getName());
496    
497                    socialActivityLocalService.addActivity(
498                            userId, folder.getGroupId(), BookmarksFolder.class.getName(),
499                            folder.getFolderId(),
500                            SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
501                            extraDataJSONObject.toString(), 0);
502            }
503    
504            @Override
505            public void subscribeFolder(long userId, long groupId, long folderId)
506                    throws PortalException, SystemException {
507    
508                    if (folderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
509                            folderId = groupId;
510                    }
511    
512                    subscriptionLocalService.addSubscription(
513                            userId, groupId, BookmarksFolder.class.getName(), folderId);
514            }
515    
516            @Override
517            public void unsubscribeFolder(long userId, long groupId, long folderId)
518                    throws PortalException, SystemException {
519    
520                    if (folderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
521                            folderId = groupId;
522                    }
523    
524                    subscriptionLocalService.deleteSubscription(
525                            userId, BookmarksFolder.class.getName(), folderId);
526            }
527    
528            @Override
529            public void updateAsset(
530                            long userId, BookmarksFolder folder, long[] assetCategoryIds,
531                            String[] assetTagNames, long[] assetLinkEntryIds)
532                    throws PortalException, SystemException {
533    
534                    AssetEntry assetEntry = assetEntryLocalService.updateEntry(
535                            userId, folder.getGroupId(), folder.getCreateDate(),
536                            folder.getModifiedDate(), BookmarksFolder.class.getName(),
537                            folder.getFolderId(), folder.getUuid(), 0, assetCategoryIds,
538                            assetTagNames, true, null, null, null, ContentTypes.TEXT_PLAIN,
539                            folder.getName(), folder.getDescription(), null, null, null, 0, 0,
540                            null, false);
541    
542                    assetLinkLocalService.updateLinks(
543                            userId, assetEntry.getEntryId(), assetLinkEntryIds,
544                            AssetLinkConstants.TYPE_RELATED);
545            }
546    
547            @Indexable(type = IndexableType.REINDEX)
548            @Override
549            public BookmarksFolder updateFolder(
550                            long userId, long folderId, long parentFolderId, String name,
551                            String description, boolean mergeWithParentFolder,
552                            ServiceContext serviceContext)
553                    throws PortalException, SystemException {
554    
555                    // Merge folders
556    
557                    BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
558                            folderId);
559    
560                    parentFolderId = getParentFolderId(folder, parentFolderId);
561    
562                    if (mergeWithParentFolder && (folderId != parentFolderId)) {
563                            mergeFolders(folder, parentFolderId);
564    
565                            return folder;
566                    }
567    
568                    // Folder
569    
570                    validate(name);
571    
572                    folder.setModifiedDate(serviceContext.getModifiedDate(null));
573                    folder.setParentFolderId(parentFolderId);
574                    folder.setTreePath(folder.buildTreePath());
575                    folder.setName(name);
576                    folder.setDescription(description);
577                    folder.setExpandoBridgeAttributes(serviceContext);
578    
579                    bookmarksFolderPersistence.update(folder);
580    
581                    // Asset
582    
583                    updateAsset(
584                            userId, folder, serviceContext.getAssetCategoryIds(),
585                            serviceContext.getAssetTagNames(),
586                            serviceContext.getAssetLinkEntryIds());
587    
588                    return folder;
589            }
590    
591            @Override
592            public BookmarksFolder updateStatus(
593                            long userId, BookmarksFolder folder, int status)
594                    throws PortalException, SystemException {
595    
596                    // Folder
597    
598                    User user = userPersistence.findByPrimaryKey(userId);
599    
600                    folder.setStatus(status);
601                    folder.setStatusByUserId(userId);
602                    folder.setStatusByUserName(user.getFullName());
603                    folder.setStatusDate(new Date());
604    
605                    bookmarksFolderPersistence.update(folder);
606    
607                    // Asset
608    
609                    if (status == WorkflowConstants.STATUS_APPROVED) {
610                            assetEntryLocalService.updateVisible(
611                                    BookmarksFolder.class.getName(), folder.getFolderId(), true);
612                    }
613                    else if (status == WorkflowConstants.STATUS_IN_TRASH) {
614                            assetEntryLocalService.updateVisible(
615                                    BookmarksFolder.class.getName(), folder.getFolderId(), false);
616                    }
617    
618                    // Index
619    
620                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
621                            BookmarksFolder.class);
622    
623                    indexer.reindex(folder);
624    
625                    return folder;
626            }
627    
628            protected long getParentFolderId(
629                            BookmarksFolder folder, long parentFolderId)
630                    throws SystemException {
631    
632                    if (parentFolderId ==
633                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
634    
635                            return parentFolderId;
636                    }
637    
638                    if (folder.getFolderId() == parentFolderId) {
639                            return folder.getParentFolderId();
640                    }
641    
642                    BookmarksFolder parentFolder =
643                            bookmarksFolderPersistence.fetchByPrimaryKey(parentFolderId);
644    
645                    if ((parentFolder == null) ||
646                            (folder.getGroupId() != parentFolder.getGroupId())) {
647    
648                            return folder.getParentFolderId();
649                    }
650    
651                    List<Long> subfolderIds = new ArrayList<Long>();
652    
653                    getSubfolderIds(
654                            subfolderIds, folder.getGroupId(), folder.getFolderId());
655    
656                    if (subfolderIds.contains(parentFolderId)) {
657                            return folder.getParentFolderId();
658                    }
659    
660                    return parentFolderId;
661            }
662    
663            protected long getParentFolderId(long groupId, long parentFolderId)
664                    throws SystemException {
665    
666                    if (parentFolderId !=
667                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
668    
669                            BookmarksFolder parentFolder =
670                                    bookmarksFolderPersistence.fetchByPrimaryKey(parentFolderId);
671    
672                            if ((parentFolder == null) ||
673                                    (groupId != parentFolder.getGroupId())) {
674    
675                                    parentFolderId =
676                                            BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID;
677                            }
678                    }
679    
680                    return parentFolderId;
681            }
682    
683            protected void mergeFolders(BookmarksFolder fromFolder, long toFolderId)
684                    throws PortalException, SystemException {
685    
686                    List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
687                            fromFolder.getGroupId(), fromFolder.getFolderId());
688    
689                    for (BookmarksFolder folder : folders) {
690                            mergeFolders(folder, toFolderId);
691                    }
692    
693                    List<BookmarksEntry> entries = bookmarksEntryPersistence.findByG_F(
694                            fromFolder.getGroupId(), fromFolder.getFolderId());
695    
696                    for (BookmarksEntry entry : entries) {
697                            entry.setFolderId(toFolderId);
698    
699                            bookmarksEntryPersistence.update(entry);
700    
701                            Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
702                                    BookmarksEntry.class);
703    
704                            indexer.reindex(entry);
705                    }
706    
707                    bookmarksFolderLocalService.deleteFolder(fromFolder);
708            }
709    
710            protected void moveDependentsToTrash(
711                            List<Object> foldersAndEntries, long trashEntryId)
712                    throws PortalException, SystemException {
713    
714                    for (Object object : foldersAndEntries) {
715                            if (object instanceof BookmarksEntry) {
716    
717                                    // Entry
718    
719                                    BookmarksEntry entry = (BookmarksEntry)object;
720    
721                                    if (entry.isInTrash()) {
722                                            continue;
723                                    }
724    
725                                    int oldStatus = entry.getStatus();
726    
727                                    entry.setStatus(WorkflowConstants.STATUS_IN_TRASH);
728    
729                                    bookmarksEntryPersistence.update(entry);
730    
731                                    // Trash
732    
733                                    int status = oldStatus;
734    
735                                    if (oldStatus == WorkflowConstants.STATUS_PENDING) {
736                                            status = WorkflowConstants.STATUS_DRAFT;
737                                    }
738    
739                                    if (oldStatus != WorkflowConstants.STATUS_APPROVED) {
740                                            trashVersionLocalService.addTrashVersion(
741                                                    trashEntryId, BookmarksEntry.class.getName(),
742                                                    entry.getEntryId(), status);
743                                    }
744    
745                                    // Asset
746    
747                                    assetEntryLocalService.updateVisible(
748                                            BookmarksEntry.class.getName(), entry.getEntryId(), false);
749    
750                                    // Indexer
751    
752                                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
753                                            BookmarksEntry.class);
754    
755                                    indexer.reindex(entry);
756                            }
757                            else if (object instanceof BookmarksFolder) {
758    
759                                    // Folder
760    
761                                    BookmarksFolder folder = (BookmarksFolder)object;
762    
763                                    if (folder.isInTrash()) {
764                                            continue;
765                                    }
766    
767                                    int oldStatus = folder.getStatus();
768    
769                                    folder.setStatus(WorkflowConstants.STATUS_IN_TRASH);
770    
771                                    bookmarksFolderPersistence.update(folder);
772    
773                                    // Trash
774    
775                                    if (oldStatus != WorkflowConstants.STATUS_APPROVED) {
776                                            trashVersionLocalService.addTrashVersion(
777                                                    trashEntryId, BookmarksEntry.class.getName(),
778                                                    folder.getFolderId(), oldStatus);
779                                    }
780    
781                                    // Folders and entries
782    
783                                    List<Object> curFoldersAndEntries = getFoldersAndEntries(
784                                            folder.getGroupId(), folder.getFolderId());
785    
786                                    moveDependentsToTrash(curFoldersAndEntries, trashEntryId);
787    
788                                    // Asset
789    
790                                    assetEntryLocalService.updateVisible(
791                                            BookmarksFolder.class.getName(), folder.getFolderId(),
792                                            false);
793    
794                                    // Index
795    
796                                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
797                                            BookmarksFolder.class);
798    
799                                    indexer.reindex(folder);
800                            }
801                    }
802            }
803    
804            protected void restoreDependentsFromTrash(
805                            List<Object> foldersAndEntries, long trashEntryId)
806                    throws PortalException, SystemException {
807    
808                    for (Object object : foldersAndEntries) {
809                            if (object instanceof BookmarksEntry) {
810    
811                                    // Entry
812    
813                                    BookmarksEntry entry = (BookmarksEntry)object;
814    
815                                    TrashEntry trashEntry = trashEntryLocalService.fetchEntry(
816                                            BookmarksEntry.class.getName(), entry.getEntryId());
817    
818                                    if (trashEntry != null) {
819                                            continue;
820                                    }
821    
822                                    TrashVersion trashVersion =
823                                            trashVersionLocalService.fetchVersion(
824                                                    trashEntryId, BookmarksEntry.class.getName(),
825                                                    entry.getEntryId());
826    
827                                    int oldStatus = WorkflowConstants.STATUS_APPROVED;
828    
829                                    if (trashVersion != null) {
830                                            oldStatus = trashVersion.getStatus();
831                                    }
832    
833                                    entry.setStatus(oldStatus);
834    
835                                    bookmarksEntryPersistence.update(entry);
836    
837                                    // Trash
838    
839                                    if (trashVersion != null) {
840                                            trashVersionLocalService.deleteTrashVersion(trashVersion);
841                                    }
842    
843                                    // Asset
844    
845                                    if (oldStatus == WorkflowConstants.STATUS_APPROVED) {
846                                            assetEntryLocalService.updateVisible(
847                                                    BookmarksEntry.class.getName(), entry.getEntryId(),
848                                                    true);
849                                    }
850    
851                                    // Indexer
852    
853                                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
854                                            BookmarksEntry.class);
855    
856                                    indexer.reindex(entry);
857                            }
858                            else if (object instanceof BookmarksFolder) {
859    
860                                    // Folder
861    
862                                    BookmarksFolder folder = (BookmarksFolder)object;
863    
864                                    TrashEntry trashEntry = trashEntryLocalService.fetchEntry(
865                                            BookmarksFolder.class.getName(), folder.getFolderId());
866    
867                                    if (trashEntry != null) {
868                                            continue;
869                                    }
870    
871                                    TrashVersion trashVersion =
872                                            trashVersionLocalService.fetchVersion(
873                                                    trashEntryId, BookmarksFolder.class.getName(),
874                                                    folder.getFolderId());
875    
876                                    int oldStatus = WorkflowConstants.STATUS_APPROVED;
877    
878                                    if (trashVersion != null) {
879                                            oldStatus = trashVersion.getStatus();
880                                    }
881    
882                                    folder.setStatus(oldStatus);
883    
884                                    bookmarksFolderPersistence.update(folder);
885    
886                                    // Folders and entries
887    
888                                    List<Object> curFoldersAndEntries = getFoldersAndEntries(
889                                            folder.getGroupId(), folder.getFolderId());
890    
891                                    restoreDependentsFromTrash(curFoldersAndEntries, trashEntryId);
892    
893                                    // Trash
894    
895                                    if (trashVersion != null) {
896                                            trashVersionLocalService.deleteTrashVersion(trashVersion);
897                                    }
898    
899                                    // Asset
900    
901                                    assetEntryLocalService.updateVisible(
902                                            BookmarksFolder.class.getName(), folder.getFolderId(),
903                                            true);
904    
905                                    // Index
906    
907                                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
908                                            BookmarksFolder.class);
909    
910                                    indexer.reindex(folder);
911                            }
912                    }
913            }
914    
915            protected void validate(String name) throws PortalException {
916                    if (Validator.isNull(name) || name.contains("\\\\") ||
917                            name.contains("//")) {
918    
919                            throw new FolderNameException();
920                    }
921            }
922    
923    }