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