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