<%@page import="com.udapsoft.waf.common.web.ibsheet7.IBSheet7TD"%>
<%@page import="com.udapsoft.waf.common.web.ibsheet7.IBSheet7TR"%>
<%@page import="com.udapsoft.waf.common.web.ibsheet7.IBSheet7Data"%>
<%@page import="com.udapsoft.waf.system.HandlerStorage"%>
<%@ page import="kr.co.hsnc.common.util.*" %>
<%@ page import="kr.co.hsnc.common.sql.*" %>
<%@ page import="kr.co.hsnc.common.message.WAFMessage" %>
<%@ page contentType="text/html; charset=utf-8" %>
<jsp:useBean id="ctx" class="com.udapsoft.waf.system.context.SessionContext" scope="session" />
<%
	ctx.init(request, response);
    HandlerStorage storage = ctx.getHandlerStorage();
    String msg = storage.getMessage();
    ValueObject params = storage.getParams();
    WAFMessage wafMessage = WAFMessage.getInstance();
    wafMessage.setLocale(ctx.getLocale());

    boolean success = storage.getSuccess();
    IBSheet7Data sheetData = storage.getIBSheet7Data();
%>
<?xml version="1.0"  ?>

<%	if( !success ) { %>
  <ERROR>
      <MESSAGE><![CDATA[<%= storage.getMessage() %>]]></MESSAGE>
      <ETC-DATA>
          <ETC KEY="detail_message"><![CDATA[<%= storage.getDetailMessage() %>]]></ETC>
      </ETC-DATA>
  </ERROR>  
<%	}
	else { %>

<SHEET>

<%	if( sheetData != null ) { %>

<%		if( sheetData.getAttribute("TOTAL").equals("") ) { %>
  	<DATA TOTAL="<%= sheetData.getRowSetSize() %>"
<%		}
		else { %>
  	<DATA TOTAL="<%= sheetData.getAttribute("TOTAL") %>"
<%		} %>
<%		ValueObject dataAttr = sheetData.getAttributes(); %>
<%      dataAttr.remove("TOTAL"); %>
<%      for( int attrIndex = 0 ; attrIndex < dataAttr.size() ; attrIndex++ ) { %>
		<%= dataAttr.getKey(attrIndex) %>="<%= dataAttr.get(attrIndex) %>"
<%		} %>
<%= ">" %>
<%		for( int i = 0 ; i < sheetData.getRowSetSize() ; i++ ) { %>
<%IBSheet7TR sheetTR = sheetData.sheetTR;%>
<%      	ValueObject trAttr = sheetTR.getAttributes(); %>
<%		 	int columnSize = sheetTR.getSheetTDSize(); %>
    	<TR
<%      	for( int attrIndex = 0 ; attrIndex < trAttr.size() ; attrIndex++ ) { %>
		    <%= trAttr.getKey(attrIndex) %>="<%= trAttr.get(attrIndex) %>"
<%			} %>
<%= ">" %>
<%			for( int j = 0 ; j < columnSize ; j++ ) { %>
      		<TD
<%IBSheet7TD sheetTD = sheetTR.getSheetTD(j);%>
<%              ValueObject tdAttr = sheetTD.getAttributes(); %>
<%      		for( int attrIndex = 0 ; attrIndex < tdAttr.size() ; attrIndex++ ) { %>
		    	<%= tdAttr.getKey(attrIndex) %>="<%= tdAttr.get(attrIndex) %>"
<%				} %>
<%				if( sheetTD.isBlankValue ) { %>
				<%= ">" %></TD>
<%				}
				else { %>
<%					RowSet rowSet = sheetData.getRowSet(sheetTD.rowSetKey); %>
<%					if( rowSet != null ) { %>
<%						String temp = "";
						if( sheetTD.rowSetColumnIndex < rowSet.getColumnSize() ) {
							temp = rowSet.getRow(i).get(sheetTD.rowSetColumnIndex);		
							if( !"".equals(sheetTD.currencyType) ) {
								double amount = rowSet.getRow(i).getDouble(sheetTD.rowSetColumnIndex);
								if( sheetTD.currencyType.equalsIgnoreCase("KRW") )
									temp = formatCurrency(amount, "###,###", "￦");
								else if( sheetTD.currencyType.equalsIgnoreCase("USD") )
									temp = formatCurrency(amount, "###,##0,##", "$");
								else if( sheetTD.currencyType.equalsIgnoreCase("EUR") )
									temp = formatCurrency(amount, "###,##0,##", "");
								else if( sheetTD.currencyType.equalsIgnoreCase("JPY") )
									temp = formatCurrency(amount, "###,##0,##", "");
								else if( sheetTD.currencyType.equalsIgnoreCase("SAR") )
									temp = formatCurrency(amount, "###,##0,##", "");
							}
						}
						else {
							temp = sheetTD.rowSetColumnIndex + " column is not exist(rowset key name: " + sheetTD.rowSetKey + ", rowset column size:" + rowSet.getColumnSize() + ", column index:" + sheetTD.rowSetColumnIndex + ")";
						}
%>
      			<%= ">" %><![CDATA[<%= temp %>]]></TD>
<%					}
					else { %>
				<%= ">" %></TD>
<%  				} %>
<%				} %>
<%			} %>
    	</TR>
<%		} %>
  	</DATA>
<%	} %>
</SHEET>
<%	} %>






<%!
	private static String formatCurrency(double amount, String pattern, String currencySymbol) {
		java.text.DecimalFormatSymbols symbol = new java.text.DecimalFormatSymbols();
	    symbol.setCurrencySymbol(currencySymbol.trim());
	    java.text.DecimalFormat df = new java.text.DecimalFormat(pattern, symbol);
	    return currencySymbol + df.format(amount);
	}
%>