001    /**
002     * Copyright (c) 2000-present 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.portal.model.impl;
016    
017    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskConstants;
018    import com.liferay.portal.kernel.backgroundtask.BackgroundTaskExecutor;
019    import com.liferay.portal.kernel.dao.orm.QueryUtil;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.repository.model.FileEntry;
022    import com.liferay.portal.kernel.repository.model.Folder;
023    import com.liferay.portal.kernel.util.ClassLoaderUtil;
024    import com.liferay.portal.kernel.util.InstanceFactory;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.kernel.workflow.WorkflowConstants;
028    import com.liferay.portal.model.Repository;
029    import com.liferay.portal.portletfilerepository.PortletFileRepositoryUtil;
030    import com.liferay.portal.service.ServiceContext;
031    import com.liferay.portal.util.PortletKeys;
032    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
033    
034    import java.util.ArrayList;
035    import java.util.List;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class BackgroundTaskImpl extends BackgroundTaskBaseImpl {
041    
042            @Override
043            public Folder addAttachmentsFolder() throws PortalException {
044                    if (_attachmentsFolderId !=
045                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
046    
047                            return PortletFileRepositoryUtil.getPortletFolder(
048                                    _attachmentsFolderId);
049                    }
050    
051                    ServiceContext serviceContext = new ServiceContext();
052    
053                    serviceContext.setAddGroupPermissions(true);
054                    serviceContext.setAddGuestPermissions(true);
055    
056                    Repository repository = PortletFileRepositoryUtil.addPortletRepository(
057                            getGroupId(), PortletKeys.BACKGROUND_TASK, serviceContext);
058    
059                    Folder folder = PortletFileRepositoryUtil.addPortletFolder(
060                            getUserId(), repository.getRepositoryId(),
061                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
062                            String.valueOf(getBackgroundTaskId()), serviceContext);
063    
064                    _attachmentsFolderId = folder.getFolderId();
065    
066                    return folder;
067            }
068    
069            @Override
070            public List<FileEntry> getAttachmentsFileEntries() throws PortalException {
071                    return getAttachmentsFileEntries(QueryUtil.ALL_POS, QueryUtil.ALL_POS);
072            }
073    
074            @Override
075            public List<FileEntry> getAttachmentsFileEntries(int start, int end)
076                    throws PortalException {
077    
078                    List<FileEntry> fileEntries = new ArrayList<>();
079    
080                    long attachmentsFolderId = getAttachmentsFolderId();
081    
082                    if (attachmentsFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
083                            fileEntries = PortletFileRepositoryUtil.getPortletFileEntries(
084                                    getGroupId(), attachmentsFolderId,
085                                    WorkflowConstants.STATUS_APPROVED, start, end, null);
086                    }
087    
088                    return fileEntries;
089            }
090    
091            @Override
092            public int getAttachmentsFileEntriesCount() throws PortalException {
093                    int attachmentsFileEntriesCount = 0;
094    
095                    long attachmentsFolderId = getAttachmentsFolderId();
096    
097                    if (attachmentsFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
098                            attachmentsFileEntriesCount =
099                                    PortletFileRepositoryUtil.getPortletFileEntriesCount(
100                                            getGroupId(), attachmentsFolderId,
101                                            WorkflowConstants.STATUS_APPROVED);
102                    }
103    
104                    return attachmentsFileEntriesCount;
105            }
106    
107            @Override
108            public long getAttachmentsFolderId() {
109                    if (_attachmentsFolderId !=
110                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
111    
112                            return _attachmentsFolderId;
113                    }
114    
115                    ServiceContext serviceContext = new ServiceContext();
116    
117                    serviceContext.setAddGroupPermissions(true);
118                    serviceContext.setAddGuestPermissions(true);
119    
120                    Repository repository =
121                            PortletFileRepositoryUtil.fetchPortletRepository(
122                                    getGroupId(), PortletKeys.BACKGROUND_TASK);
123    
124                    if (repository == null) {
125                            return DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
126                    }
127    
128                    try {
129                            Folder folder = PortletFileRepositoryUtil.getPortletFolder(
130                                    repository.getRepositoryId(),
131                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
132                                    String.valueOf(getBackgroundTaskId()));
133    
134                            _attachmentsFolderId = folder.getFolderId();
135                    }
136                    catch (Exception e) {
137                    }
138    
139                    return _attachmentsFolderId;
140            }
141    
142            @Override
143            public BackgroundTaskExecutor getBackgroundTaskExecutor() {
144                    ClassLoader classLoader = ClassLoaderUtil.getPortalClassLoader();
145    
146                    String servletContextNames = getServletContextNames();
147    
148                    if (Validator.isNotNull(servletContextNames)) {
149                            classLoader = ClassLoaderUtil.getAggregatePluginsClassLoader(
150                                    StringUtil.split(servletContextNames), false);
151                    }
152    
153                    BackgroundTaskExecutor backgroundTaskExecutor = null;
154    
155                    try {
156                            backgroundTaskExecutor =
157                                    (BackgroundTaskExecutor)InstanceFactory.newInstance(
158                                            classLoader, getTaskExecutorClassName());
159                    }
160                    catch (Exception e) {
161                    }
162    
163                    return backgroundTaskExecutor;
164            }
165    
166            @Override
167            public String getStatusLabel() {
168                    return BackgroundTaskConstants.getStatusLabel(getStatus());
169            }
170    
171            @Override
172            public boolean isInProgress() {
173                    if (getStatus() == BackgroundTaskConstants.STATUS_IN_PROGRESS) {
174                            return true;
175                    }
176    
177                    return false;
178            }
179    
180            private long _attachmentsFolderId;
181    
182    }