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