/***********************************************************************************
 * @@ Program Name : ReportDataSet.java Description : 보고양식에 입력받은 엑셀서식또는 한글서식의 값을 셋팅해줌. Author : 배정환 Create Date :
 *    2007-10-24 History :
 * @@
 ***********************************************************************************/

package kr.co.kihyun.beans.totsys.report;

public class ReportDataSet_back130523{

	/********* insert form: doc, table, javascript **********/
	private String originalReportForm = "";
	private StringBuffer newReportForm = null;
	private int change = 1;

	/********* insert form: item values **********/
	public String setForm(String insertForm, String copyForm) {
		originalReportForm = insertForm;
		copyForm = copyForm.toLowerCase();

		int eif = 0;

		int ecf = 0;

		String ifsub = insertForm;
		String cfsub = copyForm;
		StringBuffer iftmp;
		StringBuffer cftmp;

		newReportForm = new StringBuffer();

		for (; eif != -1;) {
			eif = ifsub.indexOf("<td");
			ecf = cfsub.indexOf("<td");
			if (eif != -1 && ecf != -1) {
				newReportForm.append(ifsub.substring(0, eif));
				ifsub = ifsub.substring(eif);
				cfsub = cfsub.substring(ecf);
				eif = ifsub.indexOf("</td>");
				ecf = cfsub.indexOf("</td>");
				if (eif == -1 || ecf == -1) {
					change = 0;
					break;
				}
				iftmp = new StringBuffer(ifsub.substring(0, eif));
				cftmp = new StringBuffer(cfsub.substring(0, ecf));
				ifsub = ifsub.substring(eif);
				cfsub = cfsub.substring(ecf);

				if ((iftmp.indexOf("<input")) == -1) {
					newReportForm.append(iftmp.toString());
				} else {
					StringBuffer value = new StringBuffer();
					for (int i = 0; i < cftmp.length(); i++) { // 입력할 폼의 해당셀에서 입력값을 추출한다.
						if (cftmp.charAt(i) == '<') {
							for (; i < cftmp.length(); i++) {
								if (cftmp.charAt(i) == '>')
									break;
							}
						} else {
							value.append(cftmp.charAt(i));
						}
					}

					String inValue = "";
					inValue = value.toString();
					if (inValue.equals("　") || inValue == null) {
						inValue = "";
					};

					inValue = inValue.replaceAll("\r\n&nbsp; ", "");

					if ((iftmp.indexOf("checkNum")) != -1) {
						if (inValue.equals("")) {
							inValue = "0";
						}
					}

					if ((iftmp.indexOf("value=")) == -1) {
						iftmp.insert((iftmp.indexOf("<input") + 6), " value='" + inValue + "' ");
					} else {
						iftmp.replace(iftmp.indexOf("value="), iftmp.indexOf(" ", iftmp.indexOf("value=")), "value='"
								+ inValue + "'");
					}
					newReportForm.append(iftmp.toString());
                    //System.out.println("iftmp.toString():::::\n"+iftmp.toString()+"\ncftmp::::::\n"+cftmp);
				}
			} else if (eif == -1 && ecf == -1) {
				newReportForm.append(ifsub);
				break;
			} else {
				change = 0;
				break;
			}
		}

		if (change == 1)
			return (newReportForm.toString());
		else
			return originalReportForm;
	}

	public int getChange() {
		return change;
	}
}
