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