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.IOException; 022import java.io.Serializable; 023import java.util.ArrayList; 024import java.util.HashMap; 025import java.util.LinkedHashMap; 026import java.util.List; 027import java.util.Locale; 028import java.util.Map; 029 030import org.nuxeo.ecm.automation.core.Constants; 031import org.nuxeo.ecm.automation.core.annotations.Context; 032import org.nuxeo.ecm.automation.core.annotations.Operation; 033import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 034import org.nuxeo.ecm.automation.core.annotations.Param; 035import org.nuxeo.ecm.core.api.Blob; 036import org.nuxeo.ecm.core.api.Blobs; 037import org.nuxeo.ecm.core.api.CoreSession; 038import org.nuxeo.ecm.platform.query.api.PageProvider; 039import org.nuxeo.ecm.platform.query.api.PageProviderService; 040import org.nuxeo.ecm.platform.task.dashboard.DashBoardItem; 041import org.nuxeo.ecm.platform.task.providers.UserTaskPageProvider; 042import org.nuxeo.ecm.platform.url.api.DocumentViewCodecManager; 043import org.nuxeo.runtime.api.Framework; 044 045/** 046 * Operation to retrieve the tasks waiting for the current user. 047 * 048 * @author <a href="mailto:[email protected]">Thomas Roger</a> 049 * @since 5.5 050 */ 051@Operation(id = UserTaskPageProviderOperation.ID, category = Constants.CAT_SERVICES, label = "UserTaskPageProvider", description = "Returns the tasks waiting for the current user.", addToStudio = false) 052public class UserTaskPageProviderOperation extends AbstractTaskOperation { 053 054 public static final String ID = "Workflow.UserTaskPageProvider"; 055 056 public static final String USER_TASKS_PAGE_PROVIDER = "user_tasks"; 057 058 @Param(name = "language", required = false) 059 protected String language; 060 061 @Param(name = "page", required = false) 062 protected Integer page; 063 064 @Param(name = "pageSize", required = false) 065 protected Integer pageSize; 066 067 @Context 068 protected CoreSession session; 069 070 @Context 071 protected DocumentViewCodecManager documentViewCodecManager; 072 073 @SuppressWarnings("unchecked") 074 @OperationMethod 075 public Blob run() throws IOException { 076 Map<String, Serializable> props = new HashMap<>(); 077 props.put(UserTaskPageProvider.CORE_SESSION_PROPERTY, (Serializable) session); 078 PageProviderService pps = Framework.getService(PageProviderService.class); 079 080 Long targetPage = null; 081 if (page != null) { 082 targetPage = Long.valueOf(page.longValue()); 083 } 084 Long targetPageSize = null; 085 if (pageSize != null) { 086 targetPageSize = Long.valueOf(pageSize.longValue()); 087 } 088 PageProvider<DashBoardItem> pageProvider = (PageProvider<DashBoardItem>) pps.getPageProvider( 089 USER_TASKS_PAGE_PROVIDER, null, targetPageSize, targetPage, props); 090 091 Locale locale = language != null && !language.isEmpty() ? new Locale(language) : Locale.ENGLISH; 092 093 List<Map<String, Object>> processes = new ArrayList<>(); 094 for (DashBoardItem dashBoardItem : pageProvider.getCurrentPage()) { 095 dashBoardItem.setLocale(locale); 096 Map<String, Object> obj = dashBoardItem.asMap(); 097 processes.add(obj); 098 } 099 100 Map<String, Object> json = new LinkedHashMap<>(); 101 json.put("isPaginable", Boolean.TRUE); 102 json.put("totalSize", Long.valueOf(pageProvider.getResultsCount())); 103 json.put("pageIndex", Long.valueOf(pageProvider.getCurrentPageIndex())); 104 json.put("pageSize", Long.valueOf(pageProvider.getPageSize())); 105 json.put("pageCount", Long.valueOf(pageProvider.getNumberOfPages())); 106 107 json.put("entries", processes); 108 return Blobs.createJSONBlobFromValue(json); 109 } 110 111}