001    /**
002     * Copyright (c) 2000-2012 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.workflow.WorkflowConstants;
027    import com.liferay.portal.model.ResourceConstants;
028    import com.liferay.portal.model.User;
029    import com.liferay.portal.service.ServiceContext;
030    import com.liferay.portal.util.PropsValues;
031    import com.liferay.portlet.asset.model.AssetEntry;
032    import com.liferay.portlet.asset.model.AssetLinkConstants;
033    import com.liferay.portlet.asset.util.AssetUtil;
034    import com.liferay.portlet.journal.DuplicateFolderNameException;
035    import com.liferay.portlet.journal.FolderNameException;
036    import com.liferay.portlet.journal.model.JournalArticle;
037    import com.liferay.portlet.journal.model.JournalFolder;
038    import com.liferay.portlet.journal.model.JournalFolderConstants;
039    import com.liferay.portlet.journal.service.base.JournalFolderLocalServiceBaseImpl;
040    
041    import java.util.ArrayList;
042    import java.util.Date;
043    import java.util.List;
044    
045    /**
046     * @author Juan Fernández
047     */
048    public class JournalFolderLocalServiceImpl
049            extends JournalFolderLocalServiceBaseImpl {
050    
051            public JournalFolder addFolder(
052                            long userId, long groupId, long parentFolderId, String name,
053                            String description, ServiceContext serviceContext)
054                    throws PortalException, SystemException {
055    
056                    // Folder
057    
058                    User user = userPersistence.findByPrimaryKey(userId);
059                    parentFolderId = getParentFolderId(groupId, parentFolderId);
060                    Date now = new Date();
061    
062                    validateFolder(0, groupId, parentFolderId, name);
063    
064                    long folderId = counterLocalService.increment();
065    
066                    JournalFolder folder = journalFolderPersistence.create(folderId);
067    
068                    folder.setUuid(serviceContext.getUuid());
069                    folder.setGroupId(groupId);
070                    folder.setCompanyId(user.getCompanyId());
071                    folder.setUserId(user.getUserId());
072                    folder.setUserName(user.getFullName());
073                    folder.setCreateDate(serviceContext.getCreateDate(now));
074                    folder.setModifiedDate(serviceContext.getModifiedDate(now));
075                    folder.setParentFolderId(parentFolderId);
076                    folder.setName(name);
077                    folder.setDescription(description);
078                    folder.setExpandoBridgeAttributes(serviceContext);
079    
080                    journalFolderPersistence.update(folder);
081    
082                    // Resources
083    
084                    resourceLocalService.addModelResources(folder, serviceContext);
085    
086                    // Asset
087    
088                    updateAsset(
089                            userId, folder, serviceContext.getAssetCategoryIds(),
090                            serviceContext.getAssetTagNames(),
091                            serviceContext.getAssetLinkEntryIds());
092    
093                    return folder;
094            }
095    
096            @Indexable(type = IndexableType.DELETE)
097            public JournalFolder deleteFolder(JournalFolder folder)
098                    throws PortalException, SystemException {
099    
100                    // Folders
101    
102                    List<JournalFolder> folders = journalFolderPersistence.findByG_P(
103                            folder.getGroupId(), folder.getFolderId());
104    
105                    for (JournalFolder curFolder : folders) {
106                            deleteFolder(curFolder);
107                    }
108    
109                    // Folder
110    
111                    journalFolderPersistence.remove(folder);
112    
113                    // Resources
114    
115                    resourceLocalService.deleteResource(
116                            folder, ResourceConstants.SCOPE_INDIVIDUAL);
117    
118                    // Entries
119    
120                    journalArticleLocalService.deleteArticles(
121                            folder.getGroupId(), folder.getFolderId());
122    
123                    // Expando
124    
125                    expandoValueLocalService.deleteValues(
126                            JournalFolder.class.getName(), folder.getFolderId());
127    
128                    return folder;
129            }
130    
131            @Indexable(type = IndexableType.DELETE)
132            public JournalFolder deleteFolder(long folderId)
133                    throws PortalException, SystemException {
134    
135                    JournalFolder folder = journalFolderPersistence.findByPrimaryKey(
136                            folderId);
137    
138                    return deleteFolder(folder);
139            }
140    
141            public void deleteFolders(long groupId)
142                    throws PortalException, SystemException {
143    
144                    List<JournalFolder> folders = journalFolderPersistence.findByG_P(
145                            groupId, JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID);
146    
147                    for (JournalFolder folder : folders) {
148                            journalFolderLocalService.deleteFolder(folder);
149                    }
150            }
151    
152            public JournalFolder fetchFolder(long groupId, String name)
153                    throws SystemException {
154    
155                    return journalFolderPersistence.fetchByG_N(groupId, name);
156            }
157    
158            public List<JournalFolder> getCompanyFolders(
159                            long companyId, int start, int end)
160                    throws SystemException {
161    
162                    return journalFolderPersistence.findByCompanyId(companyId, start, end);
163            }
164    
165            public int getCompanyFoldersCount(long companyId) throws SystemException {
166                    return journalFolderPersistence.countByCompanyId(companyId);
167            }
168    
169            public JournalFolder getFolder(long folderId)
170                    throws PortalException, SystemException {
171    
172                    return journalFolderPersistence.findByPrimaryKey(folderId);
173            }
174    
175            public List<JournalFolder> getFolders(long groupId) throws SystemException {
176                    return journalFolderPersistence.findByGroupId(groupId);
177            }
178    
179            public List<JournalFolder> getFolders(long groupId, long parentFolderId)
180                    throws SystemException {
181    
182                    return journalFolderPersistence.findByG_P(groupId, parentFolderId);
183            }
184    
185            public List<JournalFolder> getFolders(
186                            long groupId, long parentFolderId, int start, int end)
187                    throws SystemException {
188    
189                    return journalFolderPersistence.findByG_P(
190                            groupId, parentFolderId, start, end);
191            }
192    
193            public List<Object> getFoldersAndArticles(
194                            long groupId, long folderId, int start, int end,
195                            OrderByComparator obc)
196                    throws SystemException {
197    
198                    QueryDefinition queryDefinition = new QueryDefinition(
199                            WorkflowConstants.STATUS_ANY, start, end, obc);
200    
201                    return journalFolderFinder.findF_A_ByG_F(
202                            groupId, folderId, queryDefinition);
203            }
204    
205            public int getFoldersAndArticlesCount(
206                            long groupId, List<Long> folderIds, int status)
207                    throws SystemException {
208    
209                    QueryDefinition queryDefinition = new QueryDefinition(status);
210    
211                    if (folderIds.size() <= PropsValues.SQL_DATA_MAX_PARAMETERS) {
212                            return journalArticleFinder.countByG_F(
213                                    groupId, folderIds, queryDefinition);
214                    }
215                    else {
216                            int start = 0;
217                            int end = PropsValues.SQL_DATA_MAX_PARAMETERS;
218    
219                            int articlesCount = journalArticleFinder.countByG_F(
220                                    groupId, folderIds.subList(start, end), queryDefinition);
221    
222                            folderIds.subList(start, end).clear();
223    
224                            articlesCount += getFoldersAndArticlesCount(
225                                    groupId, folderIds, status);
226    
227                            return articlesCount;
228                    }
229            }
230    
231            public int getFoldersAndArticlesCount(long groupId, long folderId)
232                    throws SystemException {
233    
234                    return journalFolderFinder.countF_A_ByG_F(
235                            groupId, folderId,
236                            new QueryDefinition(WorkflowConstants.STATUS_ANY));
237            }
238    
239            public int getFoldersCount(long groupId, long parentFolderId)
240                    throws SystemException {
241    
242                    return journalFolderPersistence.countByG_P(groupId, parentFolderId);
243            }
244    
245            public List<JournalFolder> getNoAssetFolders() throws SystemException {
246                    return journalFolderFinder.findF_ByNoAssets();
247            }
248    
249            public void getSubfolderIds(
250                            List<Long> folderIds, long groupId, long folderId)
251                    throws SystemException {
252    
253                    List<JournalFolder> folders = journalFolderPersistence.findByG_P(
254                            groupId, folderId);
255    
256                    for (JournalFolder folder : folders) {
257                            folderIds.add(folder.getFolderId());
258    
259                            getSubfolderIds(
260                                    folderIds, folder.getGroupId(), folder.getFolderId());
261                    }
262            }
263    
264            @Indexable(type = IndexableType.REINDEX)
265            public JournalFolder moveFolder(
266                            long folderId, long parentFolderId, ServiceContext serviceContext)
267                    throws PortalException, SystemException {
268    
269                    JournalFolder folder = journalFolderPersistence.findByPrimaryKey(
270                            folderId);
271    
272                    parentFolderId = getParentFolderId(folder, parentFolderId);
273    
274                    validateFolder(
275                            folder.getFolderId(), folder.getGroupId(), parentFolderId,
276                            folder.getName());
277    
278                    folder.setModifiedDate(serviceContext.getModifiedDate(null));
279                    folder.setParentFolderId(parentFolderId);
280                    folder.setExpandoBridgeAttributes(serviceContext);
281    
282                    journalFolderPersistence.update(folder);
283    
284                    return folder;
285            }
286    
287            public void updateAsset(
288                            long userId, JournalFolder folder, long[] assetCategoryIds,
289                            String[] assetTagNames, long[] assetLinkEntryIds)
290                    throws PortalException, SystemException {
291    
292                    AssetEntry assetEntry = assetEntryLocalService.updateEntry(
293                            userId, folder.getGroupId(), folder.getCreateDate(),
294                            folder.getModifiedDate(), JournalFolder.class.getName(),
295                            folder.getFolderId(), folder.getUuid(), 0, assetCategoryIds,
296                            assetTagNames, true, null, null, null, ContentTypes.TEXT_PLAIN,
297                            folder.getName(), folder.getDescription(), null, null, null, 0, 0,
298                            null, false);
299    
300                    assetLinkLocalService.updateLinks(
301                            userId, assetEntry.getEntryId(), assetLinkEntryIds,
302                            AssetLinkConstants.TYPE_RELATED);
303            }
304    
305            @Indexable(type = IndexableType.REINDEX)
306            public JournalFolder updateFolder(
307                            long userId, long folderId, long parentFolderId, String name,
308                            String description, boolean mergeWithParentFolder,
309                            ServiceContext serviceContext)
310                    throws PortalException, SystemException {
311    
312                    // Merge folders
313    
314                    JournalFolder folder = journalFolderPersistence.findByPrimaryKey(
315                            folderId);
316    
317                    parentFolderId = getParentFolderId(folder, parentFolderId);
318    
319                    if (mergeWithParentFolder && (folderId != parentFolderId)) {
320                            mergeFolders(folder, parentFolderId);
321    
322                            return folder;
323                    }
324    
325                    // Folder
326    
327                    validateFolder(folderId, folder.getGroupId(), parentFolderId, name);
328    
329                    folder.setModifiedDate(serviceContext.getModifiedDate(null));
330                    folder.setParentFolderId(parentFolderId);
331                    folder.setName(name);
332                    folder.setDescription(description);
333                    folder.setExpandoBridgeAttributes(serviceContext);
334    
335                    journalFolderPersistence.update(folder);
336    
337                    // Asset
338    
339                    updateAsset(
340                            userId, folder, serviceContext.getAssetCategoryIds(),
341                            serviceContext.getAssetTagNames(),
342                            serviceContext.getAssetLinkEntryIds());
343    
344                    return folder;
345            }
346    
347            protected long getParentFolderId(JournalFolder folder, long parentFolderId)
348                    throws SystemException {
349    
350                    if (parentFolderId == JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
351                            return parentFolderId;
352                    }
353    
354                    if (folder.getFolderId() == parentFolderId) {
355                            return folder.getParentFolderId();
356                    }
357                    else {
358                            JournalFolder parentFolder =
359                                    journalFolderPersistence.fetchByPrimaryKey(parentFolderId);
360    
361                            if ((parentFolder == null) ||
362                                    (folder.getGroupId() != parentFolder.getGroupId())) {
363    
364                                    return folder.getParentFolderId();
365                            }
366    
367                            List<Long> subfolderIds = new ArrayList<Long>();
368    
369                            getSubfolderIds(
370                                    subfolderIds, folder.getGroupId(), folder.getFolderId());
371    
372                            if (subfolderIds.contains(parentFolderId)) {
373                                    return folder.getParentFolderId();
374                            }
375    
376                            return parentFolderId;
377                    }
378            }
379    
380            protected long getParentFolderId(long groupId, long parentFolderId)
381                    throws SystemException {
382    
383                    if (parentFolderId != JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
384                            JournalFolder parentFolder =
385                                    journalFolderPersistence.fetchByPrimaryKey(parentFolderId);
386    
387                            if ((parentFolder == null) ||
388                                    (groupId != parentFolder.getGroupId())) {
389    
390                                    parentFolderId =
391                                            JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID;
392                            }
393                    }
394    
395                    return parentFolderId;
396            }
397    
398            protected void mergeFolders(JournalFolder fromFolder, long toFolderId)
399                    throws PortalException, SystemException {
400    
401                    List<JournalFolder> folders = journalFolderPersistence.findByG_P(
402                            fromFolder.getGroupId(), fromFolder.getFolderId());
403    
404                    for (JournalFolder folder : folders) {
405                            mergeFolders(folder, toFolderId);
406                    }
407    
408                    List<JournalArticle> articles = journalArticlePersistence.findByG_F(
409                            fromFolder.getGroupId(), fromFolder.getFolderId());
410    
411                    for (JournalArticle article : articles) {
412                            article.setFolderId(toFolderId);
413    
414                            journalArticlePersistence.update(article);
415    
416                            Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
417                                    JournalArticle.class);
418    
419                            indexer.reindex(article);
420                    }
421    
422                    journalFolderLocalService.deleteFolder(fromFolder);
423            }
424    
425            protected void validateFolder(
426                            long folderId, long groupId, long parentFolderId, String name)
427                    throws PortalException, SystemException {
428    
429                    validateFolderName(name);
430    
431                    JournalFolder folder = journalFolderPersistence.fetchByG_P_N(
432                            groupId, parentFolderId, name);
433    
434                    if ((folder != null) && (folder.getFolderId() != folderId)) {
435                            throw new DuplicateFolderNameException(name);
436                    }
437            }
438    
439            protected void validateFolderName(String name) throws PortalException {
440                    if (!AssetUtil.isValidWord(name)) {
441                            throw new FolderNameException();
442                    }
443    
444                    if (name.contains("\\\\") || name.contains("//")) {
445                            throw new FolderNameException();
446                    }
447            }
448    
449    }