001/* 002 * (C) Copyright 2010 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * Contributors: 014 * Nuxeo - initial API and implementation 015 */ 016 017package org.nuxeo.ecm.automation.task; 018 019import java.io.Serializable; 020import java.util.HashMap; 021import java.util.Locale; 022import java.util.Map; 023 024import net.sf.json.JSONArray; 025import net.sf.json.JSONObject; 026 027import org.nuxeo.ecm.automation.core.Constants; 028import org.nuxeo.ecm.automation.core.annotations.Context; 029import org.nuxeo.ecm.automation.core.annotations.Operation; 030import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 031import org.nuxeo.ecm.automation.core.annotations.Param; 032import org.nuxeo.ecm.core.api.Blob; 033import org.nuxeo.ecm.core.api.Blobs; 034import org.nuxeo.ecm.core.api.CoreSession; 035import org.nuxeo.ecm.platform.query.api.PageProvider; 036import org.nuxeo.ecm.platform.query.api.PageProviderService; 037import org.nuxeo.ecm.platform.task.dashboard.DashBoardItem; 038import org.nuxeo.ecm.platform.task.providers.UserTaskPageProvider; 039import org.nuxeo.ecm.platform.url.api.DocumentViewCodecManager; 040import org.nuxeo.runtime.api.Framework; 041 042/** 043 * Operation to retrieve the tasks waiting for the current user. 044 * 045 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 046 * @since 5.5 047 */ 048@Operation(id = UserTaskPageProviderOperation.ID, category = Constants.CAT_SERVICES, label = "UserTaskPageProvider", description = "Returns the tasks waiting for the current user.", addToStudio = false) 049public class UserTaskPageProviderOperation extends AbstractTaskOperation { 050 051 public static final String ID = "Workflow.UserTaskPageProvider"; 052 053 public static final String USER_TASKS_PAGE_PROVIDER = "user_tasks"; 054 055 @Param(name = "language", required = false) 056 protected String language; 057 058 @Param(name = "page", required = false) 059 protected Integer page; 060 061 @Param(name = "pageSize", required = false) 062 protected Integer pageSize; 063 064 @Context 065 protected CoreSession session; 066 067 @Context 068 protected DocumentViewCodecManager documentViewCodecManager; 069 070 @SuppressWarnings("unchecked") 071 @OperationMethod 072 public Blob run() { 073 Map<String, Serializable> props = new HashMap<String, Serializable>(); 074 props.put(UserTaskPageProvider.CORE_SESSION_PROPERTY, (Serializable) session); 075 PageProviderService pps = Framework.getLocalService(PageProviderService.class); 076 077 Long targetPage = null; 078 if (page != null) { 079 targetPage = Long.valueOf(page.longValue()); 080 } 081 Long targetPageSize = null; 082 if (pageSize != null) { 083 targetPageSize = Long.valueOf(pageSize.longValue()); 084 } 085 PageProvider<DashBoardItem> pageProvider = (PageProvider<DashBoardItem>) pps.getPageProvider( 086 USER_TASKS_PAGE_PROVIDER, null, targetPageSize, targetPage, props); 087 088 Locale locale = language != null && !language.isEmpty() ? new Locale(language) : Locale.ENGLISH; 089 090 JSONArray processes = new JSONArray(); 091 for (DashBoardItem dashBoardItem : pageProvider.getCurrentPage()) { 092 dashBoardItem.setLocale(locale); 093 JSONObject obj = dashBoardItem.asJSON(); 094 processes.add(obj); 095 } 096 097 JSONObject json = new JSONObject(); 098 json.put("isPaginable", Boolean.TRUE); 099 json.put("totalSize", Long.valueOf(pageProvider.getResultsCount())); 100 json.put("pageIndex", Long.valueOf(pageProvider.getCurrentPageIndex())); 101 json.put("pageSize", Long.valueOf(pageProvider.getPageSize())); 102 json.put("pageCount", Long.valueOf(pageProvider.getNumberOfPages())); 103 104 json.put("entries", processes); 105 return Blobs.createBlob(json.toString(), "application/json"); 106 } 107 108}