001/* 002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * Contributors: 016 * Nuxeo - initial API and implementation 017 */ 018 019package org.nuxeo.ecm.automation.task; 020 021import java.io.Serializable; 022import java.util.HashMap; 023import java.util.Locale; 024import java.util.Map; 025 026import net.sf.json.JSONArray; 027import net.sf.json.JSONObject; 028 029import org.nuxeo.ecm.automation.core.Constants; 030import org.nuxeo.ecm.automation.core.annotations.Context; 031import org.nuxeo.ecm.automation.core.annotations.Operation; 032import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 033import org.nuxeo.ecm.automation.core.annotations.Param; 034import org.nuxeo.ecm.core.api.Blob; 035import org.nuxeo.ecm.core.api.Blobs; 036import org.nuxeo.ecm.core.api.CoreSession; 037import org.nuxeo.ecm.platform.query.api.PageProvider; 038import org.nuxeo.ecm.platform.query.api.PageProviderService; 039import org.nuxeo.ecm.platform.task.dashboard.DashBoardItem; 040import org.nuxeo.ecm.platform.task.providers.UserTaskPageProvider; 041import org.nuxeo.ecm.platform.url.api.DocumentViewCodecManager; 042import org.nuxeo.runtime.api.Framework; 043 044/** 045 * Operation to retrieve the tasks waiting for the current user. 046 * 047 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 048 * @since 5.5 049 */ 050@Operation(id = UserTaskPageProviderOperation.ID, category = Constants.CAT_SERVICES, label = "UserTaskPageProvider", description = "Returns the tasks waiting for the current user.", addToStudio = false) 051public class UserTaskPageProviderOperation extends AbstractTaskOperation { 052 053 public static final String ID = "Workflow.UserTaskPageProvider"; 054 055 public static final String USER_TASKS_PAGE_PROVIDER = "user_tasks"; 056 057 @Param(name = "language", required = false) 058 protected String language; 059 060 @Param(name = "page", required = false) 061 protected Integer page; 062 063 @Param(name = "pageSize", required = false) 064 protected Integer pageSize; 065 066 @Context 067 protected CoreSession session; 068 069 @Context 070 protected DocumentViewCodecManager documentViewCodecManager; 071 072 @SuppressWarnings("unchecked") 073 @OperationMethod 074 public Blob run() { 075 Map<String, Serializable> props = new HashMap<String, Serializable>(); 076 props.put(UserTaskPageProvider.CORE_SESSION_PROPERTY, (Serializable) session); 077 PageProviderService pps = Framework.getLocalService(PageProviderService.class); 078 079 Long targetPage = null; 080 if (page != null) { 081 targetPage = Long.valueOf(page.longValue()); 082 } 083 Long targetPageSize = null; 084 if (pageSize != null) { 085 targetPageSize = Long.valueOf(pageSize.longValue()); 086 } 087 PageProvider<DashBoardItem> pageProvider = (PageProvider<DashBoardItem>) pps.getPageProvider( 088 USER_TASKS_PAGE_PROVIDER, null, targetPageSize, targetPage, props); 089 090 Locale locale = language != null && !language.isEmpty() ? new Locale(language) : Locale.ENGLISH; 091 092 JSONArray processes = new JSONArray(); 093 for (DashBoardItem dashBoardItem : pageProvider.getCurrentPage()) { 094 dashBoardItem.setLocale(locale); 095 JSONObject obj = dashBoardItem.asJSON(); 096 processes.add(obj); 097 } 098 099 JSONObject json = new JSONObject(); 100 json.put("isPaginable", Boolean.TRUE); 101 json.put("totalSize", Long.valueOf(pageProvider.getResultsCount())); 102 json.put("pageIndex", Long.valueOf(pageProvider.getCurrentPageIndex())); 103 json.put("pageSize", Long.valueOf(pageProvider.getPageSize())); 104 json.put("pageCount", Long.valueOf(pageProvider.getNumberOfPages())); 105 106 json.put("entries", processes); 107 return Blobs.createBlob(json.toString(), "application/json"); 108 } 109 110}