<commit_before>package org.onetwo.common.excel;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.onetwo.common.excel.data.ExcelValueParser;
import org.onetwo.common.excel.data.RowContextData;
import org.onetwo.common.excel.data.SheetData;
import org.onetwo.common.excel.data.WorkbookData;
import org.onetwo.common.log.MyLoggerFactory;
import org.onetwo.common.profiling.UtilTimerStack;
import org.onetwo.common.utils.Assert;
import org.onetwo.common.utils.LangUtils;
import org.onetwo.common.utils.StringUtils;
import org.slf4j.Logger;

/****
 * 单sheet生成器
 * @author weishao
 *
 */
public class POIExcelGeneratorImpl extends AbstractWorkbookExcelGenerator implements PoiExcelGenerator{
	
	private Logger logger = MyLoggerFactory.getLogger(this.getClass());

	protected TemplateModel tempalte;
	// protected Object root;
//	protected Map context;
//	protected Workbook workbook;
	protected WorkbookData workbookData;
//	protected HSSFSheet sheet;
//	protected int rowIndex;
//	protected int cellIndex;

//	private ExcelValueParser excelValueParser;
	
	private Map<String, RowProcessor> rowProcessors;
	
	private PropertyStringParser propertyStringParser = ExcelUtils.DEFAULT_PROPERTY_STRING_PARSER;
	
	public POIExcelGeneratorImpl() {
		this.workbookData = new WorkbookData(null, new HSSFWorkbook(), new DefaultExcelValueParser(null));
		this.init();
	}

	public POIExcelGeneratorImpl(TemplateModel template){
		this(template, null);
	}
	public POIExcelGeneratorImpl(TemplateModel template, Map<String, Object> context) {
		this(new WorkbookData(null, new HSSFWorkbook(), new DefaultExcelValueParser(context)), template);
	}
	public POIExcelGeneratorImpl(WorkbookData workbook, TemplateModel template) {
		this.workbookData = workbook;
		this.tempalte = template;
//		this.excelValueParser = excelValueParser;
		this.init();
	}
	
	final protected void init(){
		RowProcessor titleProcessor;
		RowProcessor iteratorProcessor;
		RowProcessor rowProcessor;
		
		rowProcessor = new DefaultRowProcessor(this);
		titleProcessor = new DefaultRowProcessor(this, new DefaultCellStyleBuilder(this){

			@Override
			public String getFont(FieldModel field) {
				return field.getHeaderFont();
			}

			@Override
			public String getStyle(FieldModel field) {
				return field.getHeaderStyle();
			}
			
			
		}){
			@Override
			public Object getDefaultFieldValue(FieldModel field) {
				return field.getLabel();
			}
		};
		iteratorProcessor = new SmartIteratorRowProcessor(this, titleProcessor);
//		iteratorProcessor = new ExtIteratorRowProcessor(this, titleProcessor);
		
		rowProcessors = new HashMap<String, RowProcessor>();
		rowProcessors.put(RowModel.Type.ROW_KEY, rowProcessor);
		rowProcessors.put(RowModel.Type.HEADER_KEY, titleProcessor);
		rowProcessors.put(RowModel.Type.TITLE_KEY, titleProcessor);
		rowProcessors.put(RowModel.Type.ITERATOR_KEY, iteratorProcessor);
	}

	public ExcelValueParser getExcelValueParser() {
		return this.workbookData.getExcelValueParser();
	}

	/*public void setExcelValueParser(ExcelValueParser excelValueParser) {
		this.excelValueParser = excelValueParser;
	}*/

	public Workbook getWorkbook() {
		return this.workbookData.getWorkbook();
	}

	@Override
	public void generateIt() {
//		this.initWorkbookData();
		
		boolean createSheet = true;
		if(StringUtils.isNotBlank(tempalte.getCondition())){
			createSheet = (Boolean)workbookData.parseValue(tempalte.getCondition());
		}
		if(!createSheet){
			logger.info("condition[ {} = {} ], ignore create sheet.", tempalte.getCondition(), createSheet);
			return ;
		}
		ExportDatasource ds = null;
		if(StringUtils.isBlank(tempalte.getDatasource())){
			ds = new ListExportDatasource(this.workbookData, tempalte, Collections.EMPTY_LIST);
		}else{
			Object dsObj = workbookData.parseValue(tempalte.getDatasource());
			if(dsObj instanceof ExportDatasource){
				ds = (ExportDatasource) dsObj;
			}else{
				List<?> datalist = LangUtils.asList(dsObj);
				ds = new ListExportDatasource(this.workbookData, tempalte, datalist);
			}
		}
		
		
		if(tempalte.isMultiSheet()){
			List<?> datalist = null;
			int i = 0;
			while(LangUtils.isNotEmpty( (datalist = ds.getSheetDataList(i)) )){
				logger.info("{} sheet, data size: {}", i, datalist.size());
				this.generateSheet(ds.getSheetLabel(i), datalist);
				i++;
			}
		}else{
			this.generateSheet(ds.getSheetLabel(0), ds.getSheetDataList(0));
		}
		
	}
	
	private void generateSheet(String sheetname, List<?> datalist){
		Sheet sheet = getWorkbook().createSheet(sheetname);
		int sheetIndex = getWorkbook().getSheetIndex(sheet);
		this.getWorkbookData().getWorkbookListener().afterCreateSheet(sheet, sheetIndex);
		<commit_msg>modify poi excel generator<commit_after>package org.onetwo.common.excel;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.onetwo.common.excel.data.ExcelValueParser;
import org.onetwo.common.excel.data.RowContextData;
import org.onetwo.common.excel.data.SheetData;
import org.onetwo.common.excel.data.WorkbookData;
import org.onetwo.common.log.MyLoggerFactory;
import org.onetwo.common.profiling.UtilTimerStack;
import org.onetwo.common.utils.Assert;
import org.onetwo.common.utils.LangUtils;
import org.onetwo.common.utils.StringUtils;
import org.slf4j.Logger;

/****
 * 单sheet生成器
 * @author weishao
 *
 */
public class POIExcelGeneratorImpl extends AbstractWorkbookExcelGenerator implements PoiExcelGenerator{
	
	private Logger logger = MyLoggerFactory.getLogger(this.getClass());

	protected TemplateModel tempalte;
	// protected Object root;
//	protected Map context;
//	protected Workbook workbook;
	protected WorkbookData workbookData;
//	protected HSSFSheet sheet;
//	protected int rowIndex;
//	protected int cellIndex;

//	private ExcelValueParser excelValueParser;
	
	private Map<String, RowProcessor> rowProcessors;
	
	private PropertyStringParser propertyStringParser = ExcelUtils.DEFAULT_PROPERTY_STRING_PARSER;
	
	public POIExcelGeneratorImpl() {
		this.workbookData = new WorkbookData(null, new HSSFWorkbook(), new DefaultExcelValueParser(null));
		this.init();
	}

	public POIExcelGeneratorImpl(TemplateModel template){
		this(template, null);
	}
	public POIExcelGeneratorImpl(TemplateModel template, Map<String, Object> context) {
		this(new WorkbookData(null, new HSSFWorkbook(), new DefaultExcelValueParser(context)), template);
	}
	public POIExcelGeneratorImpl(WorkbookData workbook, TemplateModel template) {
		this.workbookData = workbook;
		this.tempalte = template;
//		this.excelValueParser = excelValueParser;
		this.init();
	}
	
	final protected void init(){
		RowProcessor titleProcessor;
		RowProcessor iteratorProcessor;
		RowProcessor rowProcessor;
		
		rowProcessor = new DefaultRowProcessor(this);
		titleProcessor = new DefaultRowProcessor(this, new DefaultCellStyleBuilder(this){

			@Override
			public String getFont(FieldModel field) {
				return field.getHeaderFont();
			}

			@Override
			public String getStyle(FieldModel field) {
				return field.getHeaderStyle();
			}
			
			
		}){
			@Override
			public Object getDefaultFieldValue(FieldModel field) {
				return field.getLabel();
			}
		};
		iteratorProcessor = new SmartIteratorRowProcessor(this, titleProcessor);
//		iteratorProcessor = new ExtIteratorRowProcessor(this, titleProcessor);
		
		rowProcessors = new HashMap<String, RowProcessor>();
		rowProcessors.put(RowModel.Type.ROW_KEY, rowProcessor);
		rowProcessors.put(RowModel.Type.HEADER_KEY, titleProcessor);
		rowProcessors.put(RowModel.Type.TITLE_KEY, titleProcessor);
		rowProcessors.put(RowModel.Type.ITERATOR_KEY, iteratorProcessor);
	}

	public ExcelValueParser getExcelValueParser() {
		return this.workbookData.getExcelValueParser();
	}

	/*public void setExcelValueParser(ExcelValueParser excelValueParser) {
		this.excelValueParser = excelValueParser;
	}*/

	public Workbook getWorkbook() {
		return this.workbookData.getWorkbook();
	}

	@Override
	public void generateIt() {
//		this.initWorkbookData();
		
		boolean createSheet = true;
		if(StringUtils.isNotBlank(tempalte.getCondition())){
			createSheet = (Boolean)workbookData.parseValue(tempalte.getCondition());
		}
		if(!createSheet){
			logger.info("condition[ {} = {} ], ignore create sheet.", tempalte.getCondition(), createSheet);
			return ;
		}
		ExportDatasource ds = null;
		if(StringUtils.isBlank(tempalte.getDatasource())){
			ds = new ListExportDatasource(this.workbookData, tempalte, Collections.EMPTY_LIST);
		}else{
			Object dsObj = workbookData.parseValue(tempalte.getDatasource());
			if(dsObj instanceof ExportDatasource){
				ds = (ExportDatasource) dsObj;
			}else{
				List<?> datalist = LangUtils.asList(dsObj);
				ds = new ListExportDatasource(this.workbookData, tempalte, datalist);
			}
		}
		
		
		if(tempalte.isMultiSheet()){
			List<?> datalist = null;
			int i = 0;
//			boolean createAtleastOneSheet = false;
			while(LangUtils.isNotEmpty( (datalist = ds.getSheetDataList(i)) )){
				logger.info("{} sheet, data size: {}", i, datalist.size());
				this.generateSheet(ds.getSheetLabel(i), datalist);
				i++;
			}
			//如果一个sheet也没有，创建一个空的
			if(i==0){
				this.generateSheet(ds.getSheetLabel(i), Collections.EMPTY_LIST);
			}
		}else{
			this.generateSheet(ds.getSheetLabel(0), ds.getSheetDataList(0));
		}
		
	}
	
	private void generateSheet(String sheetname, List<?> datalist){
		Sheet sheet = getWorkbook().createSheet(sheetname);
		int sheetIndex = getWorkbook().getSheetIndex(sheet);
		this.getWorkbookData().getWorkbookListener().afterCreateSheet(sheet, sheetIndex);
		
{'fraction_non_alphanumeric': 0.11257412805307065, 'fraction_numerical': 0.0009046135289978892, 'mean_word_length': 5.234335839598997, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1376946', 'n_tokens_mistral': 3482, 'n_tokens_neox': 3108, 'n_words': 580}