/* Xr - Decompiled by JODE
 * Visit http://jode.sourceforge.net/
 */
package com.report;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Properties;
import java.util.Vector;

import javax.swing.JDialog;

import com.coolservlets.beans.method.genMethod;

import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class Xr extends JDialog
{
    private int aPageLineCount;
    private Properties clusters = new Properties();
    private String ReportTitle;
    private String category;
    private String[] carddata;
    private String[][] ReportData;
    private String covertag;
    private byte[] ExeclModule = null;
    private byte[] excelreport = null;
    private String SheetName = null;
    HashMap HmReportModule = new HashMap();
    int DataCloums = 0;
    Vector vModuleKey = new Vector();
    int DataFontSize = 8;
    int DataLineHeight = 225;
    int TitleRows = 0;
    private byte[] BytesExcelModule = null;
    
    public static void main(String[] ars) throws IOException {
	HashMap hm = new HashMap();
	hm.put("$DATE", "2012-12");
	hm.put("$CGY", "MAXADMIN");
	hm.put("$AA", "111");
	hm.put("$BB", "222");
	hm.put("$CC", "3333");
	hm.put("$DD", "4444");
	hm.put("$EE", "5555");
	hm.put("$FF", "66666");
	hm.put("$ITEMNAME", "7777");
	hm.put("$ITEMGG", "888");
	hm.put("$CC1", "9999");
	hm.put("$DD1", "000");
	hm.put("$EE1", "eeeee");
	hm.put("$FF1", "qqqq");
	hm.put("$CC2", "ssss");
	hm.put("$DD2", "dddd");
	hm.put("$EE2", "fff");
	hm.put("$FF2", "gggg");
	FileOutputStream fos = new FileOutputStream("d:\\dd.xls");
	fos.write(getBytesRkd(hm));
	fos.close();
    }
    public static byte[] getBytesRkd(HashMap hm) {
	byte[] filebyte = new byte[1024];
	long tmptime = System.currentTimeMillis();
	try {
	    Xr excelReport = new Xr();
	    excelReport.setBytesExcelModule("rukudan.xls");
	    System.out.println(new StringBuilder("\nsetBytesExcel0:").append
				   (System.currentTimeMillis() - tmptime)
				   .toString());
	    tmptime = System.currentTimeMillis();
	    Properties clusters = new Properties();
	    Vector vector = null;
	    Object[] tmp = hm.keySet().toArray();
	    for (int i = 0; i < tmp.length; i++) {
		vector = new Vector();
		vector.addElement(hm.get(tmp[i]));
		clusters.put(tmp[i], vector);
	    }
	    excelReport.setClusters(clusters);
	    System.out.println(new StringBuilder("\nsetBytesExcel1:").append
				   (System.currentTimeMillis() - tmptime)
				   .toString());
	    tmptime = System.currentTimeMillis();
	    excelReport.replaceExcelModuleTitleExt();
	    System.out.println(new StringBuilder("\nsetBytesExcel2:").append
				   (System.currentTimeMillis() - tmptime)
				   .toString());
	    tmptime = System.currentTimeMillis();
	    ByteArrayInputStream hinput
		= new ByteArrayInputStream(excelReport.BytesExcelModule);
	    Workbook wb = Workbook.getWorkbook(hinput);
	    WorkbookSettings settings = new WorkbookSettings();
	    settings.setWriteAccess(null);
	    ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    WritableWorkbook workbook
		= Workbook.createWorkbook(baos, wb, settings);
	    WritableSheet sheet = workbook.getSheet(0);
	    sheet.getSettings().setSelected(true);
	    WritableFont smallfont = new WritableFont(WritableFont.ARIAL, 9,
						      WritableFont.NO_BOLD);
	    WritableCellFormat smallformatleft
		= new WritableCellFormat(smallfont);
	    smallformatleft.setAlignment(Alignment.LEFT);
	    smallformatleft.setVerticalAlignment(VerticalAlignment.CENTRE);
	    smallformatleft.setWrap(true);
	    smallformatleft.setBorder(Border.ALL, BorderLineStyle.THIN);
	    WritableCellFormat smallformatright
		= new WritableCellFormat(smallfont);
	    smallformatright.setAlignment(Alignment.RIGHT);
	    smallformatright.setVerticalAlignment(VerticalAlignment.CENTRE);
	    smallformatright.setWrap(true);
	    smallformatright.setBorder(Border.ALL, BorderLineStyle.THIN);
	    WritableCellFormat smallformatcenter
		= new WritableCellFormat(smallfont);
	    smallformatcenter.setAlignment(Alignment.CENTRE);
	    smallformatcenter.setVerticalAlignment(VerticalAlignment.CENTRE);
	    smallformatcenter.setWrap(true);
	    smallformatcenter.setBorder(Border.ALL, BorderLineStyle.THIN);
	    System.out.println(new StringBuilder("\nsetBytesExcel4:").append
				   (System.currentTimeMillis() - tmptime)
				   .toString());
	    tmptime = System.currentTimeMillis();
	    workbook.write();
	    workbook.close();
	    wb.close();
	    filebyte = baos.toByteArray();
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return filebyte;
    }
    
    public static byte[] replaceModuleByValue(byte[] contentBytes,
					      String[] value) {
	byte[] is;
	try {
	    ByteArrayInputStream hinput
		= new ByteArrayInputStream(contentBytes);
	    Workbook wb = Workbook.getWorkbook(hinput);
	    WorkbookSettings settings = new WorkbookSettings();
	    settings.setWriteAccess(null);
	    ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    WritableWorkbook wwb = Workbook.createWorkbook(baos, wb, settings);
	    WritableSheet ws = wwb.getSheet(0);
	    ws.getSettings().setSelected(true);
	    int MaxColumns = ws.getColumns();
	    int MaxRows = ws.getRows();
	    int iR = 0;
	    for (int row = 0; row < MaxRows; row++) {
		for (int j = 0; j < MaxColumns; j++) {
		    WritableCell wc = ws.getWritableCell(j, row);
		    if (wc.getType() == CellType.LABEL) {
			Label l = (Label) wc;
			String ls = l.getString();
			if (ls.equalsIgnoreCase("***")) {
			    l.setString(value[iR]);
			    iR++;
			}
		    }
		}
	    }
	    wwb.write();
	    wwb.close();
	    wb.close();
	    is = baos.toByteArray();
	} catch (Exception e) {
	    e.printStackTrace();
	    return null;
	}
	return is;
    }
    
    public static byte[] replaceExcelModuleTitle(byte[] contentBytes,
						 String[] value, String flag) {
	byte[] is;
	try {
	    ByteArrayInputStream hinput
		= new ByteArrayInputStream(contentBytes);
	    Workbook wb = Workbook.getWorkbook(hinput);
	    WorkbookSettings settings = new WorkbookSettings();
	    settings.setWriteAccess(null);
	    ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    WritableWorkbook wwb = Workbook.createWorkbook(baos, wb, settings);
	    WritableSheet ws = wwb.getSheet(0);
	    ws.getSettings().setSelected(true);
	    int MaxColumns = ws.getColumns();
	    int MaxRows = ws.getRows();
	    int iR = 0;
	    boolean ss = false;
	    for (int row = 0; row < MaxRows; row++) {
		for (int j = 0; j < MaxColumns; j++) {
		    ss = false;
		    WritableCell wc = ws.getWritableCell(j, row);
		    if (wc.getType() == CellType.LABEL) {
			Label l = (Label) wc;
			String ls = l.getString();
			if (ls.indexOf(flag, 0) > -1) {
			    String st = genMethod.replaceValueStr(ls, flag,
								  value[iR]);
			    l.setString(st);
			    iR++;
			    ss = true;
			    break;
			}
		    }
		}
		if (ss)
		    break;
	    }
	    wwb.write();
	    wwb.close();
	    wb.close();
	    is = baos.toByteArray();
	} catch (Exception e) {
	    e.printStackTrace();
	    return null;
	}
	return is;
    }
    
    public static byte[] replaceModuleByValue(byte[] contentBytes,
					      String[] value, String modflag) {
	byte[] is;
	try {
	    ByteArrayInputStream hinput
		= new ByteArrayInputStream(contentBytes);
	    Workbook wb = Workbook.getWorkbook(hinput);
	    WorkbookSettings settings = new WorkbookSettings();
	    settings.setWriteAccess(null);
	    ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    WritableWorkbook wwb = Workbook.createWorkbook(baos, wb, settings);
	    WritableSheet ws = wwb.getSheet(0);
	    ws.getSettings().setSelected(true);
	    String[] aRow = genMethod.split(modflag, ";");
	    int loop = aRow.length;
	    for (int i = 0; i < loop; i++) {
		String rows = aRow[i];
		String[] lArray = genMethod.split(rows, ",");
		WritableCell wc
		    = ws.getWritableCell(Integer.parseInt(lArray[1]),
					 Integer.parseInt(lArray[0]));
		if (wc.getType() == CellType.LABEL) {
		    Label l = (Label) wc;
		    l.setString(value[i]);
		}
	    }
	    wwb.write();
	    wwb.close();
	    wb.close();
	    is = baos.toByteArray();
	} catch (Exception e) {
	    e.printStackTrace();
	    return null;
	}
	return is;
    }
    
    public static String getValuesByBytes(byte[] contentBytes,
					  String locations) {
	String rs = "";
	String string;
	try {
	    ByteArrayInputStream input
		= new ByteArrayInputStream(contentBytes);
	    WorkbookSettings workbookSettings = new WorkbookSettings();
	    workbookSettings.setEncoding("ISO-8859-1");
	    Workbook workbook = Workbook.getWorkbook(input, workbookSettings);
	    Sheet sheet = workbook.getSheet(0);
	    String[] aRow = genMethod.split(locations, ";");
	    int loop = aRow.length;
	    for (int i = 0; i < loop; i++) {
		String rows = aRow[i];
		String[] lArray = genMethod.split(rows, ",");
		String content
		    = sheet.getCell(Integer.parseInt(lArray[1]),
				    Integer.parseInt(lArray[0])).getContents();
		rs = new StringBuilder(rs).append(content).append("@@")
			 .toString();
	    }
	    string = rs;
	} catch (Exception e) {
	    e.printStackTrace();
	    return null;
	}
	return string;
    }
    
    public static String getModuleFlag(byte[] contentBytes) {
	String moduleflag = "";
	String string;
	try {
	    ByteArrayInputStream input
		= new ByteArrayInputStream(contentBytes);
	    WorkbookSettings workbookSettings = new WorkbookSettings();
	    workbookSettings.setEncoding("ISO-8859-1");
	    Workbook rw = Workbook.getWorkbook(input, workbookSettings);
	    ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    WritableWorkbook wwb = Workbook.createWorkbook(baos, rw);
	    WritableSheet ws = wwb.getSheet(0);
	    int MaxColumns = ws.getColumns();
	    int MaxRows = ws.getRows();
	    int iR = 0;
	    for (int row = 0; row < MaxRows; row++) {
		for (int j = 0; j < MaxColumns; j++) {
		    WritableCell wc = ws.getWritableCell(j, row);
		    if (wc.getType() == CellType.LABEL) {
			Label l = (Label) wc;
			String ls = l.getString();
			if (ls.equalsIgnoreCase("***")) {
			    String st = new StringBuilder
					    (Integer.toString(row)).append
					    (",").append
					    (Integer.toString(j)).toString();
			    moduleflag = new StringBuilder(moduleflag).append
					     (st).append
					     (";").toString();
			}
		    }
		}
	    }
	    wwb.close();
	    rw.close();
	    string = moduleflag;
	} catch (Exception e) {
	    e.printStackTrace();
	    return null;
	}
	return string;
    }
    
    public static String getModuleFlag(byte[] contentBytes, String Flag) {
	String moduleflag = "";
	String string;
	try {
	    ByteArrayInputStream input
		= new ByteArrayInputStream(contentBytes);
	    WorkbookSettings workbookSettings = new WorkbookSettings();
	    workbookSettings.setEncoding("ISO-8859-1");
	    Workbook rw = Workbook.getWorkbook(input, workbookSettings);
	    ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    WritableWorkbook wwb = Workbook.createWorkbook(baos, rw);
	    WritableSheet ws = wwb.getSheet(0);
	    int MaxColumns = ws.getColumns();
	    int MaxRows = ws.getRows();
	    int iR = 0;
	    for (int row = 0; row < MaxRows; row++) {
		for (int j = 0; j < MaxColumns; j++) {
		    WritableCell wc = ws.getWritableCell(j, row);
		    if (wc.getType() == CellType.LABEL) {
			Label l = (Label) wc;
			String ls = l.getString();
			if (ls.equalsIgnoreCase(Flag)) {
			    String st = new StringBuilder
					    (Integer.toString(row)).append
					    (",").append
					    (Integer.toString(j)).toString();
			    moduleflag = new StringBuilder(moduleflag).append
					     (st).append
					     (";").toString();
			}
		    }
		}
	    }
	    wwb.close();
	    rw.close();
	    string = moduleflag;
	} catch (Exception e) {
	    e.printStackTrace();
	    return null;
	}
	return string;
    }
    
    private static boolean isHasComment(Cell cell) {
	String text = "";
	try {
	    text = cell.getCellFeatures().getComment().toString();
	} catch (Exception e) {
	    return false;
	}
	return true;
    }
    
    public void replaceExcelModuleTitleExt() {
	try {
	    ByteArrayInputStream hinput
		= new ByteArrayInputStream(BytesExcelModule);
	    Workbook wb = Workbook.getWorkbook(hinput);
	    WorkbookSettings settings = new WorkbookSettings();
	    settings.setWriteAccess(null);
	    ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    WritableWorkbook wwb = Workbook.createWorkbook(baos, wb, settings);
	    WritableSheet ws = wwb.getSheet(0);
	    ws.getSettings().setSelected(true);
	    int MaxColumns = ws.getColumns();
	    int MaxRows = ws.getRows();
	    int iR = 0;
	    boolean tt = false;
	    for (int row = 0; row < MaxRows; row++) {
		for (int j = 0; j < MaxColumns; j++) {
		    WritableCell wc = ws.getWritableCell(j, row);
		    if (wc.getType() == CellType.LABEL) {
			tt = false;
			Label l = (Label) wc;
			String ls = l.getString();
			String st = "";
			if (ls.startsWith("$")) {
			    Enumeration ee = clusters.propertyNames();
			    while (ee.hasMoreElements()) {
				String keyList = (String) ee.nextElement();
				if (ls.indexOf(keyList, 0) > -1) {
				    Vector cluList
					= (Vector) clusters.get(keyList);
				    int plp = cluList.size();
				    String[] rplValue = new String[plp];
				    for (int j1 = 0; j1 < plp; j1++) {
					String s
					    = (String) cluList.elementAt(j1);
					rplValue[j1] = s;
					st = (genMethod.replaceValueStr
					      (ls, keyList, rplValue[j1]));
				    }
				    l.setString(st);
				}
			    }
			}
		    }
		}
	    }
	    wwb.write();
	    wwb.close();
	    wb.close();
	    BytesExcelModule = baos.toByteArray();
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
    
    public void setBytesExcelModuleTitleExt() throws Exception {
	Enumeration ee = clusters.propertyNames();
	while (ee.hasMoreElements()) {
	    String keyList = (String) ee.nextElement();
	    Vector cluList = (Vector) clusters.get(keyList);
	    int plp = cluList.size();
	    String[] rplValue = new String[plp];
	    for (int j = 0; j < plp; j++) {
		String s = (String) cluList.elementAt(j);
		rplValue[j] = s;
	    }
	    BytesExcelModule
		= replaceExcelModuleTitle(BytesExcelModule, rplValue, keyList);
	}
    }
    
    public void setBytesExcelModuleTitle() throws Exception {
	Enumeration ee = clusters.propertyNames();
	while (ee.hasMoreElements()) {
	    String keyList = (String) ee.nextElement();
	    Vector cluList = (Vector) clusters.get(keyList);
	    int plp = cluList.size();
	    String[] rplValue = new String[plp];
	    for (int j = 0; j < plp; j++) {
		String s = (String) cluList.elementAt(j);
		rplValue[j] = s;
	    }
	    BytesExcelModule
		= replaceExcelModuleTitle(BytesExcelModule, rplValue, keyList);
	}
    }
    
    public void setAPageLineCount(int p_aPageLineCount) {
	aPageLineCount = p_aPageLineCount;
    }
    
    public void setClusters(Properties p_clusters) {
	clusters = p_clusters;
    }
    
    public void setReportTitle(String reportTitle) {
	ReportTitle = reportTitle;
    }
    
    public void setCategory(String p_Category) {
	category = p_Category;
    }
    
    public void setCarddata(String[] p_carddata) {
	carddata = p_carddata;
    }
    
    public void setReportData(String[][] reportdata) {
	ReportData = reportdata;
    }
    
    public void setCovertag(String p_covertag) {
	covertag = p_covertag;
    }
    
    public void setModuleBytes(byte[] vExcelModule) {
	ExeclModule = vExcelModule;
    }
    
    public void setBytesExcelModule(String ExcelModule) {
	try {
	    String modflm = "d:\\rukudan.xls";
	    File modulefile = new File(modflm);
	    FileInputStream fin = new FileInputStream(modulefile);
	    byte[] contentBytes = new byte[fin.available()];
	    fin.read(contentBytes);
	    BytesExcelModule = contentBytes;
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
    
    public void setSheetName(String vSheetname) {
	SheetName = vSheetname;
    }
    
    public void setDataFontSize(int dataFontSize) {
	DataFontSize = dataFontSize;
    }
    
    public void setDataLineHeight(int dataLineHeight) {
	DataLineHeight = dataLineHeight;
    }
    
    public String loadConfigValue(String name, String key) throws Exception {
	String rc = null;
	ClassLoader cl = this.getClass().getClassLoader();
	InputStream in;
	if (cl != null)
	    in = cl.getResourceAsStream(name);
	else
	    in = ClassLoader.getSystemResourceAsStream(name);
	if (in == null)
	    throw new Exception(new StringBuilder("configuration file '")
				    .append
				    (name).append
				    ("' not found").toString());
	Properties m_ConfigProperties = new Properties();
	m_ConfigProperties.load(in);
	rc = consume(m_ConfigProperties, key);
	if (in != null) {
	    try {
		in.close();
	    } catch (Exception exception) {
		/* empty */
	    }
	}
	return rc;
    }
    
    private String consume(Properties p, String key) {
	String s = null;
	if (p != null && key != null) {
	    s = p.getProperty(key);
	    if (s != null)
		p.remove(key);
	}
	return s;
    }
    
    private boolean SaveStreamToFile(byte[] excelModuleBytes, String path) {
	try {
	    FileOutputStream fos = new FileOutputStream(path);
	    fos.write(excelModuleBytes);
	    fos.close();
	} catch (Exception ex) {
	    ex.printStackTrace();
	}
	return true;
    }
}
