<commit_before>package com.inepex.ineForm.server.tablerenderer.excel;

import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import com.inepex.ineForm.client.IneFormProperties;
import com.inepex.ineForm.shared.Nullable;
import com.inepex.ineForm.shared.descriptorext.ColRDesc;
import com.inepex.ineForm.shared.descriptorext.TableRDescBase;
import com.inepex.ineForm.shared.render.AssistedObjectTableFieldRenderer;
import com.inepex.ineForm.shared.tablerender.TableRenderer;
import com.inepex.ineom.shared.AssistedObjectHandler;
import com.inepex.ineom.shared.AssistedObjectHandlerFactory;
import com.inepex.ineom.shared.IFConsts;
import com.inepex.ineom.shared.assistedobject.AssistedObject;
import com.inepex.ineom.shared.descriptor.DescriptorStore;
import com.inepex.ineom.shared.descriptor.FDesc;
import com.inepex.ineom.shared.descriptor.Node;
import com.inepex.ineom.shared.util.SharedUtil;

public class ExcelRenderer extends TableRenderer{

	public static interface ExcelRendererFactory {
		public ExcelRenderer create(@Assisted("od") String objectDescName,
				@Assisted("td") @Nullable String tableRDescName, @Assisted Sheet shee, @Assisted boolean setCellTypest);
	}

	protected final Sheet sheet;
	protected int startRowNr;
	protected int actualRowNr;
	protected Row actualRow;
	protected int actualCellNr;
	protected Cell actualCell;
	protected boolean setCellTypes;

	@Inject
	public ExcelRenderer(DescriptorStore descStore,
			@Assisted("od") String objectDescName,
			@Assisted("td") @Nullable String tableRDescName,
			@Assisted Sheet sheet,
			AssistedObjectTableFieldRenderer fieldRenderer,
			@Assisted boolean setCellTypes
			) {
		super(descStore, objectDescName, tableRDescName, fieldRenderer);
		this.sheet = sheet;
		startRowNr = 0;
		setRenderLastFieldEnd(true);
		this.setCellTypes = setCellTypes;
	}

	public void setStartRowNr(int rowNr){
		this.startRowNr = rowNr;
	}

	@Override
	protected void renderStart() {
		actualRowNr = startRowNr;		
	}

	@Override
	protected void renderEnd() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void renderLineStart() {
		actualRow = sheet.createRow((short)actualRowNr);
		actualCellNr = 0;
	}

	@Override
	protected void renderLineEnd() {
		actualRowNr++;		
	}

	@Override
	protected void renderFieldStart() {
		actualCell = actualRow.createCell(actualCellNr);

	}

	protected void renderField(String content){
		actualCell.setCellValue(content);
	}

	@Override
	protected void renderFieldEnd() {
		actualCellNr++;		
	}

	@Override
	protected void renderHeaderStart() {
		renderLineStart();
	}

	@Override
	protected void renderHeaderEnd() {
		renderLineEnd();
	}

	protected void renderHeaderField(String content){
		renderField(content);
	}

	@Override
	protected void renderHeaderFieldEnd() {
		renderFieldEnd();	
	}

	public int getActualRowNr() {
		return actualRowNr;
	}

	@Override
	protected void renderHeaderFieldStart(ColRDesc colRDesc, FDesc fDesc) {
		renderFieldStart();
	}


	@Override
	public String render(List<AssistedObject> kvos){
		if(!setCellTypes){
			return super.render(kvos);		
		}
		AssistedObjectHandlerFactory factory = new AssistedObjectHandlerFactory(descStore);
		renderStart();
		if (renderHeader) renderHeader();

		for (AssistedObject kvo : kvos){
			renderLineStart();
			for (Node<TableRDescBase> columnNode : tableRDesc.getRootNode()
					.getChildren()) {
				renderFieldStart();
				ColRDesc colRenderDesc = (ColRDesc)columnNode.getNodeElement();
				if (!IneFormProperties.showIds && IFConsts.KEY_ID.equals(columnNode.getNodeId()))
					continue;

				String key = columnNode.getNodeId();
				
				AssistedObjectHandler kvoOrRelatedKvoChecker = factory.createHandler(kvo).getRelatedKVOMultiLevel(
						SharedUtil.listFromDotSeparated(key));
				
				fieldRenderer.setObjectAndDescriptor(kvoOrRelatedKvoChecker.getAssistedObject(), tableRDesc);
				
				if(fieldRenderer.containsCustomizer(key)){
					actualCell.setCellValue(fieldRenderer.getFieldByCustomizer(key));
				}
				else{ 
					String deepestKey = SharedUtil.deepestKey(key);
					if(SharedUtil.isMultilevelKey(key)){
						kvoOrRelatedKvoChecker = kvoOrRelatedKvoChecker.getRelatedKVOMultiLevel(SharedUtil.listFromDotSeparated(key));
					}
					
					if (colRenderDesc.getPropValue(ColRDesc.EXCEL_NUMBERFORMAT) != null) {
						Double num = kvoOrRelatedKvoChecker.getDouble(deepestKey);
						if(num != null){
							actualCell.setCellValue(num);
						}
						setDataFormatForActualCell(colRenderDesc.getPropValue(ColRDesc.EXCEL_NUMBERFORMAT));
						
					}
					else if (colRenderDesc.getPropValue(ColRDesc.EXCEL_DATETIMEFORMAT) != null) {
						Long date = kvoOrRelatedKvoChecker.getLong(deepestKey);
						if(date != null){
							actualCell.setCellValue(getGMTCalendar(date));
						}
						setDataFormatForActualCell(colRenderDesc.getPropValue(ColRDesc.EXCEL_DATETIMEFORMAT));
					}
					
				}
				cellValueSet(key, kvoOrRelatedKvoChecker);
				<commit_msg>excel bugfix: string values were not renderered correctly in exported .xls files<commit_after>package com.inepex.ineForm.server.tablerenderer.excel;

import java.util.Calendar;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import com.inepex.ineForm.client.IneFormProperties;
import com.inepex.ineForm.shared.Nullable;
import com.inepex.ineForm.shared.descriptorext.ColRDesc;
import com.inepex.ineForm.shared.descriptorext.TableRDescBase;
import com.inepex.ineForm.shared.render.AssistedObjectTableFieldRenderer;
import com.inepex.ineForm.shared.tablerender.TableRenderer;
import com.inepex.ineom.shared.AssistedObjectHandler;
import com.inepex.ineom.shared.AssistedObjectHandlerFactory;
import com.inepex.ineom.shared.IFConsts;
import com.inepex.ineom.shared.assistedobject.AssistedObject;
import com.inepex.ineom.shared.descriptor.DescriptorStore;
import com.inepex.ineom.shared.descriptor.FDesc;
import com.inepex.ineom.shared.descriptor.Node;
import com.inepex.ineom.shared.util.SharedUtil;

public class ExcelRenderer extends TableRenderer{

	public static interface ExcelRendererFactory {
		public ExcelRenderer create(@Assisted("od") String objectDescName,
				@Assisted("td") @Nullable String tableRDescName, @Assisted Sheet shee, @Assisted boolean setCellTypest);
	}

	protected final Sheet sheet;
	protected int startRowNr;
	protected int actualRowNr;
	protected Row actualRow;
	protected int actualCellNr;
	protected Cell actualCell;
	protected boolean setCellTypes;

	@Inject
	public ExcelRenderer(DescriptorStore descStore,
			@Assisted("od") String objectDescName,
			@Assisted("td") @Nullable String tableRDescName,
			@Assisted Sheet sheet,
			AssistedObjectTableFieldRenderer fieldRenderer,
			@Assisted boolean setCellTypes
			) {
		super(descStore, objectDescName, tableRDescName, fieldRenderer);
		this.sheet = sheet;
		startRowNr = 0;
		setRenderLastFieldEnd(true);
		this.setCellTypes = setCellTypes;
	}

	public void setStartRowNr(int rowNr){
		this.startRowNr = rowNr;
	}

	@Override
	protected void renderStart() {
		actualRowNr = startRowNr;		
	}

	@Override
	protected void renderEnd() {
		for(int i= 0; i <= actualCellNr; i++){
			sheet.autoSizeColumn(i);
		}
	}

	@Override
	protected void renderLineStart() {
		actualRow = sheet.createRow((short)actualRowNr);
		actualCellNr = 0;
	}

	@Override
	protected void renderLineEnd() {
		actualRowNr++;		
	}

	@Override
	protected void renderFieldStart() {
		actualCell = actualRow.createCell(actualCellNr);

	}

	protected void renderField(String content){
		actualCell.setCellValue(content);
	}

	@Override
	protected void renderFieldEnd() {
		actualCellNr++;		
	}

	@Override
	protected void renderHeaderStart() {
		renderLineStart();
	}

	@Override
	protected void renderHeaderEnd() {
		renderLineEnd();
	}

	protected void renderHeaderField(String content){
		renderField(content);
	}

	@Override
	protected void renderHeaderFieldEnd() {
		renderFieldEnd();	
	}

	public int getActualRowNr() {
		return actualRowNr;
	}

	@Override
	protected void renderHeaderFieldStart(ColRDesc colRDesc, FDesc fDesc) {
		renderFieldStart();
	}


	@Override
	public String render(List<AssistedObject> kvos){
		if(!setCellTypes){
			return super.render(kvos);		
		}
		AssistedObjectHandlerFactory factory = new AssistedObjectHandlerFactory(descStore);
		renderStart();
		if (renderHeader) renderHeader();

		for (AssistedObject kvo : kvos){
			renderLineStart();
			for (Node<TableRDescBase> columnNode : tableRDesc.getRootNode()
					.getChildren()) {
				renderFieldStart();
				ColRDesc colRenderDesc = (ColRDesc)columnNode.getNodeElement();
				if (!IneFormProperties.showIds && IFConsts.KEY_ID.equals(columnNode.getNodeId()))
					continue;

				String key = columnNode.getNodeId();
				
				AssistedObjectHandler kvoOrRelatedKvoChecker = factory.createHandler(kvo).getRelatedKVOMultiLevel(
						SharedUtil.listFromDotSeparated(key));
				
				fieldRenderer.setObjectAndDescriptor(kvoOrRelatedKvoChecker.getAssistedObject(), tableRDesc);
				
				if(fieldRenderer.containsCustomizer(key)){
					actualCell.setCellValue(fieldRenderer.getFieldByCustomizer(key));
				}
				else{ 
					String deepestKey = SharedUtil.deepestKey(key);
					if(SharedUtil.isMultilevelKey(key)){
						kvoOrRelatedKvoChecker = kvoOrRelatedKvoChecker.getRelatedKVOMultiLevel(SharedUtil.listFromDotSeparated(key));
					}
					
					if (colRenderDesc.getPropValue(ColRDesc.EXCEL_NUMBERFORMAT) != null) {
						Double num = kvoOrRelatedKvoChecker.getDouble(deepestKey);
						if(num != null){
							actualCell.setCellValue(num);
						}
						setDataFormatForActualCell(colRenderDesc.getPropValue(ColRDesc.EXCEL_NUMBERFORMAT));
						
					}
					else if (colRenderDesc.getPropValue(ColRDesc.EXCEL_DATETIMEFORMAT) != null) {
						Long date = kvoOrRelatedKvoChecker.getLong(deepestKey);
						if(date != null){
							actualCell.setCellValue(getGMTCalendar(date));
						}
						setDataFormatForActualCell(colRenderDesc.getPropValue(ColRDesc.EXCEL_DATETIMEFORMAT));
					}
					else{
						actualCell.setCellValue(fieldRenderer.getField(deepestKey));
					}
					
				}
				cellValueSet(key, kvoOrRelatedKvoChecker);
				
{'fraction_non_alphanumeric': 0.098806860551827, 'fraction_numerical': 0.00046607009694258017, 'mean_word_length': 4.987165178571429, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 7, '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': '21530', 'n_tokens_mistral': 3986, 'n_tokens_neox': 3416, 'n_words': 580}