All Downloads are FREE. Search and download functionalities are using the official Maven repository.

artoria.action.hutool.AbstractHutoolBigExcelExportHandler Maven / Gradle / Ivy

The newest version!
package artoria.action.hutool;

import artoria.action.handler.AbstractImportExportActionHandler;
import artoria.action.handler.ExportHandler;
import artoria.data.collect.PageArrayList;
import artoria.exception.ExceptionUtils;
import artoria.util.Assert;
import artoria.util.StringUtils;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static artoria.common.constant.Numbers.*;
import static artoria.util.ObjectUtils.cast;

/**
 * The abstract hutool big excel export action handler.
 * @param 

The export parameter type * @param The type of data obtained * @author Kahle */ public abstract class AbstractHutoolBigExcelExportHandler extends AbstractImportExportActionHandler implements ExportHandler { private static Logger log = LoggerFactory.getLogger(AbstractHutoolBigExcelExportHandler.class); public AbstractHutoolBigExcelExportHandler(String actionName) { super(actionName); } /** * Configuring the export context object. * @param context The export context */ protected abstract void configContext(ExcelExportContext

context); /** * Write the header of excel. * @param context The export context */ @Override public void preHandle(ImportExportContext

context) { ExcelExportContext

nowContext = cast(context); Map headerAliases = nowContext.getHeaderAliases(); if (MapUtil.isEmpty(headerAliases)) { return; } List headers = new ArrayList(); for (Map.Entry entry : headerAliases.entrySet()) { String key = String.valueOf(entry.getKey()); String value = entry.getValue(); value = StrUtil.isNotBlank(value) ? value : key; nowContext.getWorker().addHeaderAlias(key, value); headers.add(value); } nowContext.getWorker().setOnlyAlias(true); if (CollUtil.isNotEmpty(headers)) { nowContext.getWorker().writeHeadRow(headers); } } @Override public Object handle(ImportExportContext

context, Object cursor, List data) { // Transform context object. ExcelExportContext

nowContext = cast(context); nowContext.getWorker().write(data); return null; } @Override public ExcelExportContext

buildContext(Object... arguments) { // Create the context. ExcelExportContext

context = createContext(ExcelExportContext.class, arguments); context.setBeginTime(new Date()); context.setStatus(ONE); // Configuring context object. configContext(context); // Check of context. Assert.notBlank(context.getModule(), "Parameter \"module\" must not blank. "); // Default value processing. if (context.getAsync() == null) { context.setAsync(false); } if (StringUtils.isBlank(context.getResultMessage()) && context.getAsync()) { context.setResultMessage("Please wait for a moment while data is being exported! "); } // End. return context; } @Override public Object doExecute(AsyncSupportContext context) { // Transform context object. Assert.notNull(context, "Parameter \"context\" must not null. "); ExcelExportContext

nowContext = cast(context); try { // Set export worker. nowContext.setWorker(ExcelUtil.getBigWriter()); // Push export task information if status is null or 1. // Export status: 0 unknown, 1 will export, 2 exporting, 3 processing, 4 timeout, 5 failure, 6 success if (nowContext.getStatus() == null || nowContext.getStatus() == ONE) { nowContext.setStatus(TWO); pushTask(nowContext); } // Write excel headers. preHandle(nowContext); // Query the data and write it to excel. long successCount = ZERO; for (int pageNum = ONE, pageCount = ONE; pageNum <= pageCount; pageNum++) { // Query the data. List list = getData(nowContext, pageNum); if (CollUtil.isEmpty(list)) { break; } // Handle page count (only on the first page). // If the result is not instance of "PageArrayList", the data query is not paged. if (pageNum == ONE && list instanceof PageArrayList) { PageArrayList pageList = (PageArrayList) list; pageCount = pageList.getPageCount(); nowContext.setTotalCount(pageList.getTotal()); } // Write data to excel. handle(nowContext, pageNum, list); successCount += list.size(); } nowContext.setSuccessCount(successCount); // Calculate the count. Long totalCount = nowContext.getTotalCount(); if (totalCount == null) { nowContext.setTotalCount(totalCount = successCount); } nowContext.setFailureCount(totalCount - successCount); // Save result. save(nowContext); // Push export task information. nowContext.setEndTime(new Date()); nowContext.setStatus(SIX); pushTask(nowContext); // Get the result. nowContext.setFinish(true); return getResult(context); } catch (Exception e) { // Push export task information. nowContext.setStatus(FIVE); nowContext.setError(e); pushTask(nowContext); throw ExceptionUtils.wrap(e); } } /** * The excel export context. * @param

The export parameter type * @author Kahle */ public static class ExcelExportContext

extends ExportContext

{ /** * The worker of the export. */ private ExcelWriter worker; /** * The header aliases. */ private Map headerAliases; public ExcelWriter getWorker() { return worker; } public void setWorker(ExcelWriter worker) { this.worker = worker; } public Map getHeaderAliases() { return headerAliases; } public void setHeaderAliases(Map headerAliases) { this.headerAliases = headerAliases; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy