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

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

The newest version!
package artoria.action.hutool;

import artoria.action.handler.AbstractImportExportActionHandler;
import artoria.action.handler.ImportHandler;
import artoria.data.bean.BeanUtils;
import artoria.exception.ExceptionUtils;
import artoria.util.Assert;
import artoria.util.CollectionUtils;
import artoria.util.MapUtils;
import artoria.util.StringUtils;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
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 small excel import action handler.
 * This class only supports small amount of data in excel files.
 * @param 

The import parameter type * @param The type of data parsed * @author Kahle */ public abstract class AbstractHutoolSmallExcelImportHandler extends AbstractImportExportActionHandler implements ImportHandler { private static Logger log = LoggerFactory.getLogger(AbstractHutoolSmallExcelImportHandler.class); public AbstractHutoolSmallExcelImportHandler(String actionName) { super(actionName); } protected InputStream parseFile(ExcelImportContext context) { Object fileData = context.getFileData(); Assert.notNull(fileData, "Parameter \"fileData\" must not null. "); try { if (fileData instanceof MultipartFile) { MultipartFile file = (MultipartFile) fileData; context.setFilename(file.getOriginalFilename()); context.setContentType(file.getContentType()); context.setFileSize(file.getSize()); return file.getInputStream(); } else if (fileData instanceof File) { File file = (File) fileData; context.setFilename(file.getName()); return new FileInputStream(file); } else { throw new IllegalArgumentException( "The type of parameter \"fileData\" is not supported. " ); } } catch (Exception e) { throw ExceptionUtils.wrap(e); } } /** * Configuring the import context object. * @param context The import context */ protected abstract void configContext(ExcelImportContext context); @Override public List parseData(ImportContext

context, Object cursor, Object rawData) { // Transform context object. ExcelImportContext nowContext = cast(context); // The data conversion. Class exchangeClass = nowContext.getExchangeClass(); return BeanUtils.beanToBeanInList((List) rawData, exchangeClass); } @Override public ExcelImportContext buildContext(Object... arguments) { // Create the context. ExcelImportContext context = createContext(ExcelImportContext.class, arguments); context.setBeginTime(new Date()); context.setStatus(ONE); // Parse file data. Object fileData = context.getFileData(); Assert.notNull(fileData, "Parameter \"fileData\" must not null. "); InputStream inputStream = parseFile(context); // Configuring context object. configContext(context); // Check of context. Assert.notBlank(context.getModule(), "Parameter \"module\" must not blank. "); Class exchangeClass = context.getExchangeClass(); Assert.notNull(exchangeClass, "Parameter \"exchangeClass\" must not null. "); // 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 the data is being imported! "); } // Read the data. ExcelReader excelReader = ExcelUtil.getReader(inputStream); if (MapUtils.isNotEmpty(context.getHeaderAliases())) { excelReader.setHeaderAlias(context.getHeaderAliases()); } List> mapList = excelReader.readAll(); context.setRawData(mapList); // End. return context; } @Override public Object doExecute(AsyncSupportContext context) { // Transform context object. Assert.notNull(context, "Parameter \"context\" must not null. "); ExcelImportContext nowContext = cast(context); try { // Push import task information if status is null or 1. // Import status: 0 unknown, 1 will import, 2 importing, 3 processing, 4 timeout, 5 failure, 6 success if (nowContext.getStatus() == null || nowContext.getStatus() == ONE) { nowContext.setStatus(TWO); pushTask(nowContext); } // Why not use something like paging? // Because "ExcelReader" already loads the entire file into memory. // So the current class only supports small amount of data in excel files. // So the data read logic is directly read all at once. List rawData = nowContext.getRawData(); List parsedData = parseData(nowContext, null, rawData); if (nowContext.getTotalCount() == null) { nowContext.setTotalCount(0L); if (CollectionUtils.isNotEmpty(parsedData)) { Integer size = parsedData.size(); nowContext.setTotalCount(size.longValue()); } } // Perform data processing, such as storing to a database. handle(nowContext, null, parsedData); // Save the result of the import. save(nowContext); // Push import task information. // Task status is assigned by specific processing logic. if (nowContext.getEndTime() == null) { nowContext.setEndTime(new Date()); } pushTask(nowContext); // Get the result. nowContext.setFinish(true); return getResult(context); } catch (Exception e) { // Push import task information. nowContext.setStatus(FIVE); nowContext.setError(e); pushTask(nowContext); throw ExceptionUtils.wrap(e); } } /** * The excel import context. * @param

The type of the import parameter * @param The type of data parsed * @author Kahle */ public static class ExcelImportContext extends ImportContext

{ private Map headerAliases; private Class exchangeClass; private List rawData; public Map getHeaderAliases() { return headerAliases; } public void setHeaderAliases(Map headerAliases) { this.headerAliases = headerAliases; } public Class getExchangeClass() { return exchangeClass; } public void setExchangeClass(Class exchangeClass) { this.exchangeClass = exchangeClass; } public List getRawData() { return rawData; } public void setRawData(List rawData) { this.rawData = rawData; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy