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.journal.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.search.Indexable;
021    import com.liferay.portal.kernel.search.IndexableType;
022    import com.liferay.portal.kernel.search.Indexer;
023    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
024    import com.liferay.portal.kernel.util.ContentTypes;
025    import com.liferay.portal.kernel.util.OrderByComparator;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.UnicodeProperties;
028    import com.liferay.portal.kernel.workflow.WorkflowConstants;
029    import com.liferay.portal.model.ResourceConstants;
030    import com.liferay.portal.model.User;
031    import com.liferay.portal.service.ServiceContext;
032    import com.liferay.portal.util.PropsValues;
033    import com.liferay.portlet.asset.model.AssetEntry;
034    import com.liferay.portlet.asset.model.AssetLinkConstants;
035    import com.liferay.portlet.asset.util.AssetUtil;
036    import com.liferay.portlet.journal.DuplicateFolderNameException;
037    import com.liferay.portlet.journal.FolderNameException;
038    import com.liferay.portlet.journal.model.JournalArticle;
039    import com.liferay.portlet.journal.model.JournalFolder;
040    import com.liferay.portlet.journal.model.JournalFolderConstants;
041    import com.liferay.portlet.journal.service.base.JournalFolderLocalServiceBaseImpl;
042    import com.liferay.portlet.social.model.SocialActivityConstants;
043    import com.liferay.portlet.trash.model.TrashEntry;
044    import com.liferay.portlet.trash.util.TrashUtil;
045    
046    import java.util.ArrayList;
047    import java.util.Date;
048    import java.util.List;
049    
050    /**
051     * @author Juan Fernández
052     */
053    public class JournalFolderLocalServiceImpl
054            extends JournalFolderLocalServiceBaseImpl {
055    
056            public JournalFolder addFolder(
057                            long userId, long groupId, long parentFolderId, String name,
058                            String description, ServiceContext serviceContext)
059                    throws PortalException, SystemException {
060    
061                    // Folder
062    
063                    User user = userPersistence.findByPrimaryKey(userId);
064                    parentFolderId = getParentFolderId(groupId, parentFolderId);
065                    Date now = new Date();
066    
067                    validateFolder(0, groupId, parentFolderId, name);
068    
069                    long folderId = counterLocalService.increment();
070    
071                    JournalFolder folder = journalFolderPersistence.create(folderId);
072    
073                    folder.setUuid(serviceContext.getUuid());
074                    folder.setGroupId(groupId);
075                    folder.setCompanyId(user.getCompanyId());
076                    folder.setUserId(user.getUserId());
077                    folder.setUserName(user.getFullName());
078                    folder.setCreateDate(serviceContext.getCreateDate(now));
079                    folder.setModifiedDate(serviceContext.getModifiedDate(now));
080                    folder.setParentFolderId(parentFolderId);
081                    folder.setName(name);
082                    folder.setDescription(description);
083                    folder.setExpandoBridgeAttributes(serviceContext);
084    
085                    journalFolderPersistence.update(folder);
086    
087                    // Resources
088    
089                    resourceLocalService.addModelResources(folder, serviceContext);
090    
091                    // Asset
092    
093                    updateAsset(
094                            userId, folder, serviceContext.getAssetCategoryIds(),
095                            serviceContext.getAssetTagNames(),
096                            serviceContext.getAssetLinkEntryIds());
097    
098                    return folder;
099            }
100    
101            @Indexable(type = IndexableType.DELETE)
102            public JournalFolder deleteFolder(JournalFolder folder)
103                    throws PortalException, SystemException {
104    
105                    return deleteFolder(folder, true);
106            }
107    
108            @Indexable(type = IndexableType.DELETE)
109            public JournalFolder deleteFolder(
110                            JournalFolder folder, boolean includeTrashedEntries)
111                    throws PortalException, SystemException {
112    
113                    // Folders
114    
115                    List<JournalFolder> folders = journalFolderPersistence.findByG_P(
116                            folder.getGroupId(), folder.getFolderId());
117    
118                    for (JournalFolder curFolder : folders) {
119                            if (includeTrashedEntries || !curFolder.isInTrash()) {
120                                    deleteFolder(curFolder);
121                            }
122                    }
123    
124                    // Folder
125    
126                    journalFolderPersistence.remove(folder);
127    
128                    // Resources
129    
130                    resourceLocalService.deleteResource(
131                            folder, ResourceConstants.SCOPE_INDIVIDUAL);
132    
133                    // Entries
134    
135                    journalArticleLocalService.deleteArticles(
136                            folder.getGroupId(), folder.getFolderId(), includeTrashedEntries);
137    
138                    // Asset
139    
140                    assetEntryLocalService.deleteEntry(
141                            JournalFolder.class.getName(), folder.getFolderId());
142    
143                    // Expando
144    
145                    expandoValueLocalService.deleteValues(
146                            JournalFolder.class.getName(), folder.getFolderId());
147    
148                    // Trash
149    
150                    trashEntryLocalService.deleteEntry(
151                            JournalFolder.class.getName(), folder.getFolderId());
152    
153                    return folder;
154            }
155    
156            @Indexable(type = IndexableType.DELETE)
157            public JournalFolder deleteFolder(long folderId)
158                    throws PortalException, SystemException {
159    
160                    JournalFolder folder = journalFolderPersistence.findByPrimaryKey(
161                            folderId);
162    
163                    return deleteFolder(folder, true);
164            }
165    
166            @Indexable(type = IndexableType.DELETE)
167            public JournalFolder deleteFolder(
168                            long folderId, boolean includeTrashedEntries)
169                    throws PortalException, SystemException {
170    
171                    JournalFolder folder = journalFolderPersistence.findByPrimaryKey(
172                            folderId);
173    
174                    return deleteFolder(folder, includeTrashedEntries);
175            }
176    
177            public void deleteFolders(long groupId)
178                    throws PortalException, SystemException {
179    
180                    List<JournalFolder> folders = journalFolderPersistence.findByG_P(
181                            groupId, JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID);
182    
183                    for (JournalFolder folder : folders) {
184                            journalFolderLocalService.deleteFolder(folder);
185                    }
186            }
187    
188            public JournalFolder fetchFolder(
189                            long groupId, long parentFolderId, String name)
190                    throws SystemException {
191    
192                    return journalFolderPersistence.fetchByG_P_N(
193                                    groupId, parentFolderId, name);
194            }
195    
196            public JournalFolder fetchFolder(long groupId, String name)
197                    throws SystemException {
198    
199                    return journalFolderPersistence.fetchByG_N(groupId, name);
200            }
201    
202            public List<JournalFolder> getCompanyFolders(
203                            long companyId, int start, int end)
204                    throws SystemException {
205    
206                    return journalFolderPersistence.findByCompanyId(companyId, start, end);
207            }
208    
209            public int getCompanyFoldersCount(long companyId) throws SystemException {
210                    return journalFolderPersistence.countByCompanyId(companyId);
211            }
212    
213            public JournalFolder getFolder(long folderId)
214                    throws PortalException, SystemException {
215    
216                    return journalFolderPersistence.findByPrimaryKey(folderId);
217            }
218    
219            public List<JournalFolder> getFolders(long groupId) throws SystemException {
220                    return journalFolderPersistence.findByGroupId(groupId);
221            }
222    
223            public List<JournalFolder> getFolders(long groupId, long parentFolderId)
224                    throws SystemException {
225    
226                    return getFolders(
227                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED);
228            }
229    
230            public List<JournalFolder> getFolders(
231                            long groupId, long parentFolderId, int status)
232                    throws SystemException {
233    
234                    return journalFolderPersistence.findByG_P_S(
235                            groupId, parentFolderId, status);
236            }
237    
238            public List<JournalFolder> getFolders(
239                            long groupId, long parentFolderId, int start, int end)
240                    throws SystemException {
241    
242                    return getFolders(
243                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED, start,
244                            end);
245            }
246    
247            public List<JournalFolder> getFolders(
248                            long groupId, long parentFolderId, int status, int start, int end)
249                    throws SystemException {
250    
251                    return journalFolderPersistence.findByG_P_S(
252                                    groupId, parentFolderId, status, start, end);
253            }
254    
255            public List<Object> getFoldersAndArticles(long groupId, long folderId)
256                    throws SystemException {
257    
258                    QueryDefinition queryDefinition = new QueryDefinition(
259                            WorkflowConstants.STATUS_ANY);
260    
261                    return journalFolderFinder.findF_A_ByG_F(
262                            groupId, folderId, queryDefinition);
263            }
264    
265            public List<Object> getFoldersAndArticles(
266                            long groupId, long folderId, int start, int end,
267                            OrderByComparator obc)
268                    throws SystemException {
269    
270                    QueryDefinition queryDefinition = new QueryDefinition(
271                            WorkflowConstants.STATUS_ANY, start, end, obc);
272    
273                    return journalFolderFinder.findF_A_ByG_F(
274                            groupId, folderId, queryDefinition);
275            }
276    
277            public int getFoldersAndArticlesCount(
278                            long groupId, List<Long> folderIds, int status)
279                    throws SystemException {
280    
281                    QueryDefinition queryDefinition = new QueryDefinition(status);
282    
283                    if (folderIds.size() <= PropsValues.SQL_DATA_MAX_PARAMETERS) {
284                            return journalArticleFinder.countByG_F(
285                                    groupId, folderIds, queryDefinition);
286                    }
287                    else {
288                            int start = 0;
289                            int end = PropsValues.SQL_DATA_MAX_PARAMETERS;
290    
291                            int articlesCount = journalArticleFinder.countByG_F(
292                                    groupId, folderIds.subList(start, end), queryDefinition);
293    
294                            folderIds.subList(start, end).clear();
295    
296                            articlesCount += getFoldersAndArticlesCount(
297                                    groupId, folderIds, status);
298    
299                            return articlesCount;
300                    }
301            }
302    
303            public int getFoldersAndArticlesCount(long groupId, long folderId)
304                    throws SystemException {
305    
306                    return journalFolderFinder.countF_A_ByG_F(
307                            groupId, folderId,
308                            new QueryDefinition(WorkflowConstants.STATUS_ANY));
309            }
310    
311            public int getFoldersCount(long groupId, long parentFolderId)
312                    throws SystemException {
313    
314                    return getFoldersCount(
315                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED);
316            }
317    
318            public int getFoldersCount(long groupId, long parentFolderId, int status)
319                    throws SystemException {
320    
321                    return journalFolderPersistence.countByG_P_S(
322                            groupId, parentFolderId, status);
323            }
324    
325            public List<JournalFolder> getNoAssetFolders() throws SystemException {
326                    return journalFolderFinder.findF_ByNoAssets();
327            }
328    
329            public void getSubfolderIds(
330                            List<Long> folderIds, long groupId, long folderId)
331                    throws SystemException {
332    
333                    List<JournalFolder> folders = journalFolderPersistence.findByG_P(
334                            groupId, folderId);
335    
336                    for (JournalFolder folder : folders) {
337                            folderIds.add(folder.getFolderId());
338    
339                            getSubfolderIds(
340                                    folderIds, folder.getGroupId(), folder.getFolderId());
341                    }
342            }
343    
344            @Indexable(type = IndexableType.REINDEX)
345            public JournalFolder moveFolder(
346                            long folderId, long parentFolderId, ServiceContext serviceContext)
347                    throws PortalException, SystemException {
348    
349                    JournalFolder folder = journalFolderPersistence.findByPrimaryKey(
350                            folderId);
351    
352                    parentFolderId = getParentFolderId(folder, parentFolderId);
353    
354                    validateFolder(
355                            folder.getFolderId(), folder.getGroupId(), parentFolderId,
356                            folder.getName());
357    
358                    folder.setModifiedDate(serviceContext.getModifiedDate(null));
359                    folder.setParentFolderId(parentFolderId);
360                    folder.setExpandoBridgeAttributes(serviceContext);
361    
362                    journalFolderPersistence.update(folder);
363    
364                    return folder;
365            }
366    
367            public JournalFolder moveFolderFromTrash(
368                            long userId, long folderId, long parentFolderId,
369                            ServiceContext serviceContext)
370                    throws PortalException, SystemException {
371    
372                    JournalFolder folder = journalFolderPersistence.findByPrimaryKey(
373                            folderId);
374    
375                    if (folder.isInTrash()) {
376                            restoreFolderFromTrash(userId, folderId);
377                    }
378                    else {
379                            updateStatus(userId, folder, WorkflowConstants.STATUS_APPROVED);
380                    }
381    
382                    return journalFolderLocalService.moveFolder(
383                            folderId, parentFolderId, serviceContext);
384            }
385    
386            public void moveFolderToTrash(long userId, long folderId)
387                    throws PortalException, SystemException {
388    
389                    JournalFolder folder = journalFolderPersistence.findByPrimaryKey(
390                            folderId);
391    
392                    folder = updateStatus(
393                            userId, folder, WorkflowConstants.STATUS_IN_TRASH);
394    
395                    TrashEntry trashEntry = trashEntryLocalService.getEntry(
396                            JournalFolder.class.getName(), folder.getFolderId());
397    
398                    String trashTitle = TrashUtil.getTrashTitle(trashEntry.getEntryId());
399    
400                    folder.setName(trashTitle);
401    
402                    journalFolderPersistence.update(folder);
403    
404                    // Social
405    
406                    socialActivityCounterLocalService.disableActivityCounters(
407                            JournalFolder.class.getName(), folder.getFolderId());
408    
409                    socialActivityLocalService.addActivity(
410                            userId, folder.getGroupId(), JournalFolder.class.getName(),
411                            folder.getFolderId(), SocialActivityConstants.TYPE_MOVE_TO_TRASH,
412                            StringPool.BLANK, 0);
413            }
414    
415            public void restoreFolderFromTrash(long userId, long folderId)
416                    throws PortalException, SystemException {
417    
418                    JournalFolder folder = journalFolderPersistence.findByPrimaryKey(
419                            folderId);
420    
421                    folder.setName(TrashUtil.getOriginalTitle(folder.getName()));
422    
423                    journalFolderPersistence.update(folder);
424    
425                    TrashEntry trashEntry = trashEntryLocalService.getEntry(
426                            JournalFolder.class.getName(), folderId);
427    
428                    updateStatus(userId, folder, trashEntry.getStatus());
429    
430                    // Social
431    
432                    socialActivityCounterLocalService.enableActivityCounters(
433                            JournalFolder.class.getName(), folder.getFolderId());
434    
435                    socialActivityLocalService.addActivity(
436                            userId, folder.getGroupId(), JournalFolder.class.getName(),
437                            folder.getFolderId(),
438                            SocialActivityConstants.TYPE_RESTORE_FROM_TRASH, StringPool.BLANK,
439                            0);
440            }
441    
442            public void updateAsset(
443                            long userId, JournalFolder folder, long[] assetCategoryIds,
444                            String[] assetTagNames, long[] assetLinkEntryIds)
445                    throws PortalException, SystemException {
446    
447                    AssetEntry assetEntry = assetEntryLocalService.updateEntry(
448                            userId, folder.getGroupId(), folder.getCreateDate(),
449                            folder.getModifiedDate(), JournalFolder.class.getName(),
450                            folder.getFolderId(), folder.getUuid(), 0, assetCategoryIds,
451                            assetTagNames, true, null, null, null, ContentTypes.TEXT_PLAIN,
452                            folder.getName(), folder.getDescription(), null, null, null, 0, 0,
453                            null, false);
454    
455                    assetLinkLocalService.updateLinks(
456                            userId, assetEntry.getEntryId(), assetLinkEntryIds,
457                            AssetLinkConstants.TYPE_RELATED);
458            }
459    
460            @Indexable(type = IndexableType.REINDEX)
461            public JournalFolder updateFolder(
462                            long userId, long folderId, long parentFolderId, String name,
463                            String description, boolean mergeWithParentFolder,
464                            ServiceContext serviceContext)
465                    throws PortalException, SystemException {
466    
467                    // Merge folders
468    
469                    JournalFolder folder = journalFolderPersistence.findByPrimaryKey(
470                            folderId);
471    
472                    parentFolderId = getParentFolderId(folder, parentFolderId);
473    
474                    if (mergeWithParentFolder && (folderId != parentFolderId)) {
475                            mergeFolders(folder, parentFolderId);
476    
477                            return folder;
478                    }
479    
480                    // Folder
481    
482                    validateFolder(folderId, folder.getGroupId(), parentFolderId, name);
483    
484                    folder.setModifiedDate(serviceContext.getModifiedDate(null));
485                    folder.setParentFolderId(parentFolderId);
486                    folder.setName(name);
487                    folder.setDescription(description);
488                    folder.setExpandoBridgeAttributes(serviceContext);
489    
490                    journalFolderPersistence.update(folder);
491    
492                    // Asset
493    
494                    updateAsset(
495                            userId, folder, serviceContext.getAssetCategoryIds(),
496                            serviceContext.getAssetTagNames(),
497                            serviceContext.getAssetLinkEntryIds());
498    
499                    return folder;
500            }
501    
502            public JournalFolder updateStatus(
503                            long userId, JournalFolder folder, int status)
504                    throws PortalException, SystemException {
505    
506                    User user = userPersistence.findByPrimaryKey(userId);
507    
508                    int oldStatus = folder.getStatus();
509    
510                    folder.setStatus(status);
511                    folder.setStatusByUserId(userId);
512                    folder.setStatusByUserName(user.getFullName());
513                    folder.setStatusDate(new Date());
514    
515                    journalFolderPersistence.update(folder);
516    
517                    // Folders and entries
518    
519                    List<Object> foldersAndEntries =
520                            journalFolderLocalService.getFoldersAndArticles(
521                                    folder.getGroupId(), folder.getFolderId());
522    
523                    updateDependentStatus(foldersAndEntries, status);
524    
525                    if (status == WorkflowConstants.STATUS_APPROVED) {
526    
527                            // Asset
528    
529                            assetEntryLocalService.updateVisible(
530                                    JournalFolder.class.getName(), folder.getFolderId(), true);
531    
532                            // Social
533    
534                            socialActivityCounterLocalService.enableActivityCounters(
535                                    JournalFolder.class.getName(), folder.getFolderId());
536                    }
537                    else if (status == WorkflowConstants.STATUS_IN_TRASH) {
538    
539                            // Asset
540    
541                            assetEntryLocalService.updateVisible(
542                                    JournalFolder.class.getName(), folder.getFolderId(), false);
543    
544                            // Social
545    
546                            socialActivityCounterLocalService.disableActivityCounters(
547                                    JournalFolder.class.getName(), folder.getFolderId());
548                    }
549    
550                    // Trash
551    
552                    if (oldStatus == WorkflowConstants.STATUS_IN_TRASH) {
553                            trashEntryLocalService.deleteEntry(
554                                    JournalFolder.class.getName(), folder.getFolderId());
555                    }
556                    else if (status == WorkflowConstants.STATUS_IN_TRASH) {
557                            UnicodeProperties typeSettingsProperties = new UnicodeProperties();
558    
559                            typeSettingsProperties.put("title", folder.getName());
560    
561                            trashEntryLocalService.addTrashEntry(
562                                    userId, folder.getGroupId(), JournalFolder.class.getName(),
563                                    folder.getFolderId(), oldStatus, null, typeSettingsProperties);
564                    }
565    
566                    // Index
567    
568                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
569                            JournalFolder.class);
570    
571                    indexer.reindex(folder);
572    
573                    return folder;
574            }
575    
576            protected long getParentFolderId(JournalFolder folder, long parentFolderId)
577                    throws SystemException {
578    
579                    if (parentFolderId == JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
580                            return parentFolderId;
581                    }
582    
583                    if (folder.getFolderId() == parentFolderId) {
584                            return folder.getParentFolderId();
585                    }
586                    else {
587                            JournalFolder parentFolder =
588                                    journalFolderPersistence.fetchByPrimaryKey(parentFolderId);
589    
590                            if ((parentFolder == null) ||
591                                    (folder.getGroupId() != parentFolder.getGroupId())) {
592    
593                                    return folder.getParentFolderId();
594                            }
595    
596                            List<Long> subfolderIds = new ArrayList<Long>();
597    
598                            getSubfolderIds(
599                                    subfolderIds, folder.getGroupId(), folder.getFolderId());
600    
601                            if (subfolderIds.contains(parentFolderId)) {
602                                    return folder.getParentFolderId();
603                            }
604    
605                            return parentFolderId;
606                    }
607            }
608    
609            protected long getParentFolderId(long groupId, long parentFolderId)
610                    throws SystemException {
611    
612                    if (parentFolderId != JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
613                            JournalFolder parentFolder =
614                                    journalFolderPersistence.fetchByPrimaryKey(parentFolderId);
615    
616                            if ((parentFolder == null) ||
617                                    (groupId != parentFolder.getGroupId())) {
618    
619                                    parentFolderId =
620                                            JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID;
621                            }
622                    }
623    
624                    return parentFolderId;
625            }
626    
627            protected void mergeFolders(JournalFolder fromFolder, long toFolderId)
628                    throws PortalException, SystemException {
629    
630                    List<JournalFolder> folders = journalFolderPersistence.findByG_P(
631                            fromFolder.getGroupId(), fromFolder.getFolderId());
632    
633                    for (JournalFolder folder : folders) {
634                            mergeFolders(folder, toFolderId);
635                    }
636    
637                    List<JournalArticle> articles = journalArticlePersistence.findByG_F(
638                            fromFolder.getGroupId(), fromFolder.getFolderId());
639    
640                    for (JournalArticle article : articles) {
641                            article.setFolderId(toFolderId);
642    
643                            journalArticlePersistence.update(article);
644    
645                            Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
646                                    JournalArticle.class);
647    
648                            indexer.reindex(article);
649                    }
650    
651                    journalFolderLocalService.deleteFolder(fromFolder);
652            }
653    
654            protected void updateDependentStatus(
655                            List<Object> foldersAndEntries, int status)
656                    throws PortalException, SystemException {
657    
658                    for (Object object : foldersAndEntries) {
659                            if (object instanceof JournalArticle) {
660                                    JournalArticle article = (JournalArticle)object;
661    
662                                    if (status == WorkflowConstants.STATUS_IN_TRASH) {
663    
664                                            // Asset
665    
666                                            if (article.getStatus() ==
667                                                            WorkflowConstants.STATUS_APPROVED) {
668    
669                                                    assetEntryLocalService.updateVisible(
670                                                            JournalArticle.class.getName(),
671                                                            article.getResourcePrimKey(), false);
672                                            }
673    
674                                            // Social
675    
676                                            socialActivityCounterLocalService.disableActivityCounters(
677                                                    JournalArticle.class.getName(),
678                                                    article.getResourcePrimKey());
679    
680                                            if (article.getStatus() ==
681                                                            WorkflowConstants.STATUS_PENDING) {
682    
683                                                    article.setStatus(WorkflowConstants.STATUS_DRAFT);
684    
685                                                    journalArticlePersistence.update(article);
686                                            }
687                                    }
688                                    else {
689    
690                                            // Asset
691    
692                                            if (article.getStatus() ==
693                                                            WorkflowConstants.STATUS_APPROVED) {
694    
695                                                    assetEntryLocalService.updateVisible(
696                                                            JournalArticle.class.getName(),
697                                                            article.getResourcePrimKey(), true);
698                                            }
699    
700                                            // Social
701    
702                                            socialActivityCounterLocalService.enableActivityCounters(
703                                                    JournalArticle.class.getName(),
704                                                    article.getResourcePrimKey());
705                                    }
706    
707                                    // Workflow
708    
709                                    if (status != WorkflowConstants.STATUS_APPROVED) {
710                                            List<JournalArticle> articleVersions =
711                                                    journalArticlePersistence.findByG_A(
712                                                            article.getGroupId(), article.getArticleId());
713    
714                                            for (JournalArticle curArticle : articleVersions) {
715                                                    if (!curArticle.isPending()) {
716                                                            continue;
717                                                    }
718    
719                                                    curArticle.setStatus(WorkflowConstants.STATUS_DRAFT);
720    
721                                                    journalArticlePersistence.update(curArticle);
722    
723                                                    workflowInstanceLinkLocalService.
724                                                            deleteWorkflowInstanceLink(
725                                                                    curArticle.getCompanyId(),
726                                                                    curArticle.getGroupId(),
727                                                                    JournalArticle.class.getName(),
728                                                                    curArticle.getId());
729                                            }
730                                    }
731    
732                                    // Indexer
733    
734                                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
735                                            JournalArticle.class);
736    
737                                    indexer.reindex(article);
738                            }
739                            else if (object instanceof JournalFolder) {
740                                    JournalFolder folder = (JournalFolder)object;
741    
742                                    if (folder.isInTrash()) {
743                                            continue;
744                                    }
745    
746                                    // Folders and articles
747    
748                                    List<Object> curFoldersAndEntries = getFoldersAndArticles(
749                                            folder.getGroupId(), folder.getFolderId());
750    
751                                    updateDependentStatus(curFoldersAndEntries, status);
752    
753                                    if (status == WorkflowConstants.STATUS_IN_TRASH) {
754    
755                                            // Asset
756    
757                                            assetEntryLocalService.updateVisible(
758                                                    JournalFolder.class.getName(), folder.getFolderId(),
759                                                    false);
760    
761                                            // Social
762    
763                                            socialActivityCounterLocalService.disableActivityCounters(
764                                                    JournalFolder.class.getName(), folder.getFolderId());
765                                    }
766                                    else {
767    
768                                            // Asset
769    
770                                            assetEntryLocalService.updateVisible(
771                                                    JournalFolder.class.getName(), folder.getFolderId(),
772                                                    true);
773    
774                                            // Social
775    
776                                            socialActivityCounterLocalService.enableActivityCounters(
777                                                    JournalFolder.class.getName(), folder.getFolderId());
778                                    }
779    
780                                    // Index
781    
782                                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
783                                            JournalFolder.class);
784    
785                                    indexer.reindex(folder);
786                            }
787                    }
788            }
789    
790            protected void validateFolder(
791                            long folderId, long groupId, long parentFolderId, String name)
792                    throws PortalException, SystemException {
793    
794                    validateFolderName(name);
795    
796                    JournalFolder folder = journalFolderPersistence.fetchByG_P_N(
797                            groupId, parentFolderId, name);
798    
799                    if ((folder != null) && (folder.getFolderId() != folderId)) {
800                            throw new DuplicateFolderNameException(name);
801                    }
802            }
803    
804            protected void validateFolderName(String name) throws PortalException {
805                    if (!AssetUtil.isValidWord(name)) {
806                            throw new FolderNameException();
807                    }
808    
809                    if (name.contains("\\\\") || name.contains("//")) {
810                            throw new FolderNameException();
811                    }
812            }
813    
814    }