package com.csc.library.entry;

import java.util.HashMap;
import java.util.Iterator;

import com.csc.library.components.ComponentManager;
import com.csc.library.database.ChildManager;
import com.csc.library.database.FieldManager;
import com.csc.library.database.RecordManager;
import com.csc.library.session.DbRecord;
import com.csc.library.session.DbTable;
import com.csc.library.utilities.MyLog;
public class ChildEntry extends SingleEntry {

	String help_return, obj, recCount;
	int space = 0;

	public ChildEntry() {
		this.type = "childEntry";
	}
	
	private void clearChildData() {
		try {
			Iterator it = this.myRec.getChildTable().keySet().iterator();
			while (it.hasNext()) {
				String key = (String) it.next();
				ChildManager ch = (ChildManager) this.myRec.getChildTable().get(key);
			
				Iterator itChild = ch.getChildTable().keySet().iterator();
				while (itChild.hasNext()) {
					String keyChild = (String) itChild.next();
					ChildManager chChild = (ChildManager) ch.getChildTable().get(keyChild);
					chChild.clearField();
				}            	        	
				ch.clearField();
			}
		} catch (Exception e) {
			MyLog.error(this, this.getUProfile(), e);
		}
	}

	public void getAndset() {
		try {
			String nameField;
			if ((this.getValue("gorec") != null) && (!this.getValue("gorec").equals(""))) {
				recCount = this.getValue("gorec");

				this.myTab.first();
				this.myRec = this.myTab.getCurrentRecordManager(recCount);
			} else {
				if ((!this.getValue("ack").equals("")) && (this.getValue("ack").equals("add"))) {
					this.myRec = (DbRecord) this.myTab.getNewRecord();
					this.clearChildData();
				} // if
			} // end if
		} catch (Exception ex) {
			MyLog.error(this, this.getUProfile(), ex);
		}
	}

	public void dataManager(String objName, HashMap param) {
          this.param = param;
          try {
            this.myTab.setParam(this.param);
          }
          catch (Exception ex) {
			MyLog.error(this, this.getUProfile(), ex);
          }
        }

	public String process() {
		String nameField;
		RecordManager rec;
		try {
			this.cmd = this.getValue("cmdchild");
			if ((this.cmd != null) && (this.cmd.length() > 0)) {
				this.setValue();
				this.cmd = this.getValue("cmdchild");
				if (this.cmd.equalsIgnoreCase("save") && (this.getValue("ack").equals("add"))) {
					this.myTab.putNewRecord(this.myRec);
					String  errorMsg=myRec.getString("error_msg");
					if(errorMsg == null || errorMsg.equals("")){
						this.myRec=this.myTab.getNewRecord();
						this.clearChildData();						
					}
				}
				if (this.cmd.equalsIgnoreCase("save") && (this.getValue("ack").equals("edit"))) {
					//myTab.deleteRecord(Integer.parseInt(recCount));
					//this.myTab.putNewRecord(this.myRec,Integer.parseInt(recCount));
//					this.myTab.getCurrentRecordManager(recCount);
					this.myTab.putNewRecord(this.myRec,this.myTab.recNo());
//					this.myRec=this.myTab.getNewRecord();
//					this.clearChildData();
				} else {
					if (this.cmd.equalsIgnoreCase("del")) {
						myTab.deleteRecord(recCount);
					}
				}
				this.cmd = this.getValue("cmd");
				if ((this.cmd != null) && (this.cmd.length() > 0)) {
					if (this.cmd.equalsIgnoreCase("save")) {
						this.myRec.save();
					} else {
						if (this.cmd.equalsIgnoreCase("delete"))
							this.myRec.delete();
					}
				}
			} // end if
		} catch (Exception ex) {
			MyLog.error(this, this.getUProfile(), ex);
		}
		return "";
	}

	public DbTable getTable(String Name) {
		return this.myTab;
	}
	
	@Override
	public void setChannel(HashMap param) {
	}
	
	@Override
    public ComponentManager getComponent(String nameField) {
        FieldManager fm = null;
        try {
        	/**
        	 * clone ����������ա����� component �ҡ ��������ռš�з��Ѻ�١
        	 */
            fm = (FieldManager)this.myRec.getField(nameField).clone(this.getUProfile());
            fm.setProperty("his_table", this.myRec.getTableName());
            fm.setProperty("his_refer", this.refer);
            if(fm.isKey()!='0'&& this.getValue("ack").equals("edit")){
            	fm.setProperty("readonly","true");
            	fm.setHelp("");
            }else{
            	fm.setProperty("readonly","false");
            }
            if(!fm.getHELP_RETURN_FIELD()){
            	fm.setHELP_RETURN_FIELD(true);
            }
        } catch (Exception e) {
			MyLog.error(this, this.getUProfile(), e);
        }
        return (ComponentManager) fm;
    }
	
}