/*******************************************************************************
  * Copyright (c) 2012 Megha Nidhi Dahal and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/epl-v10.html
  *
  * Contributors:
  *    Megha Nidhi Dahal - initial API and implementation and/or initial documentation
  *    Actuate Corporation - more efficient xlsx processing;
  *         support of timestamp, datetime, time, and date data types
  *******************************************************************************/

package org.eclipse.birt.report.data.oda.excel.impl.util;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.birt.report.data.oda.excel.ExcelODAConstants;

public class XlsxRowCallBack implements RowCallBack {
	private ArrayList<String[]> xlsxRowData = new ArrayList<String[]>();
	private int rowNum;

	public void handleRow(List<Object> values) {
		++rowNum;

		if (values == null || values.size() == 0) {
			return;
		}
		String[] valArray = new String[values.size()];
		values.toArray(valArray);
		if( rowNum == 1 ){
			for( int i=0; i<valArray.length; i++ ){
				if(valArray[i].length() == 0){
					valArray[i] = "column_"+i;
				}
			}
		}

		xlsxRowData.add(valArray);
	}

	public XlsxRowCallBack() {}

	public ArrayList<String> initArrayList(String[] strings) {
		ArrayList<String> list = new ArrayList<String>();
		for (String i : strings) {
			list.add(i);
		}
		return list;
	}

	public int getMaxRowsInSheet() {
		return (xlsxRowData.size());
	}

	public ArrayList<String> getRow(int rownum) {
		return (initArrayList(xlsxRowData.get(rownum)));
	}

	// Need this function because there is no easy way of determining the number of columns in xlsx with SAX Parser.
	// This function expands all the previous data rows with blanks.
	public void columnExpansion(int newColumnCount){
		for(int i=0;i<xlsxRowData.size();i++)
		{
			String[] currentRow =(String[])xlsxRowData.get(i);
			if( currentRow.length < newColumnCount){
				String[] newRow = new String[newColumnCount];
				//put in fake column names
				if( i==0 ){
					for (int j=0; j< newRow.length; j++){
						newRow[j] = "column_"+j;
					}
				}else{
					for (int j=0; j< newRow.length; j++) newRow[j] = ExcelODAConstants.EMPTY_STRING;
				}
				for (int g=0; g< currentRow.length; g++) newRow[g] = currentRow[g];
				xlsxRowData.remove(i);
				xlsxRowData.add(i, newRow);
			}
		}
	}

}
@@ -1005,29 +1005,8 @@
 %3E();
-%0A%09private int rowNum;
 %0A%0A%09p
@@ -1052,20 +1052,8 @@
 s) %7B
-%0A%09%09++rowNum;
 %0A%0A%09%09
@@ -1160,16 +1160,16 @@
 ize()%5D;%0A
+
 %09%09values
@@ -1192,155 +1192,8 @@
 y);%0A
-%09%09if( rowNum == 1 )%7B%0A%09%09%09for( int i=0; i%3CvalArray.length; i++ )%7B%0A%09%09%09%09if(valArray%5Bi%5D.length() == 0)%7B%0A%09%09%09%09%09valArray%5Bi%5D = %22column_%22+i;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%0A
 %09%09xl

Fix 51441 When edit Excel DataSet,Column value is wrong if the Excel file's column has no value.