package CoreFS.SB04;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import CoreFS.SA01.CoreGlobalConstants;
import CoreFS.SA05.CoreFileOperation;

public class CoreXmlDesignBean  {

	// protected void createListXml(String path, Object instancelist) {
	// super.createListXml(path, instancelist);
	// }

	// public String add(String path, CoreDesignBean bean) {
	// String msg = "";
	// if(!this.isRepeat(bean.getClass(),path, "ID", bean.getID())) {
	// super.add(path, bean);
	// }else {
	// msg = "ID ÖØ¸´";
	// }
	// return msg;
	// }

	public String add(String path, CoreDesignBean bean) {
		String msg = "";
		ArrayList<CoreDesignBean> lu = new ArrayList<CoreDesignBean>();
//		if (!this.isRepeat(bean.getClass(), path, "ID", bean.getID())) {
//			lu = (ArrayList) this.findAll(path);
//			lu.add(bean);
//			this.createListXml(path, lu);
//		} else {
//			msg = "ID ÖØ¸´";
//		}
		return msg;
	}

	protected ArrayList<CoreDesignBean> findAll(String path) {
		ArrayList<CoreDesignBean> lb = new ArrayList<CoreDesignBean>();
		ArrayList<String> ls = new ArrayList<String>();
		ls = (ArrayList) this.findAllStr(path);
		lb = this.list2Beans(ls);
		return lb;
	}

	public List<String> findAllStr(String path) {
		List<String> lu = new ArrayList<String>();
		CoreFileOperation cfo = new CoreFileOperation();
		Field[] fields = CoreDesignBean.class.getDeclaredFields();
		String[] args = new String[fields.length];
		for (int i = 0; i < fields.length; i++) {
			args[i] = fields[i].getName();
			System.out.println("fields " + args[i]);
		}

		lu = cfo.ReadXml(path, CoreGlobalConstants.XMLPARENTNODENAME, args);

		return lu;

	}

	// public void delete(String path, String id) {
	// super.delete(path, CoreDesignBean.class, "ID", id);
	// }

	public void delete(String path, String id) {
		CoreFileOperation cfo = new CoreFileOperation();
		Field[] fields = CoreDesignBean.class.getDeclaredFields();
		ArrayList<String> as = new ArrayList();
		ArrayList<CoreDesignBean> lu = new ArrayList<CoreDesignBean>();
		String[] args = new String[fields.length];
		for (int i = 0; i < fields.length; i++) {
			args[i] = fields[i].getName();
			System.out.println("fields " + args[i]);
		}
		as = (ArrayList<String>) cfo.delXmlById(path,
				CoreGlobalConstants.XMLPARENTNODENAME, args, "ID", id);
		lu = this.list2Beans(as);
		this.createListXml(path, lu);

	}

	protected HashMap setMap(CoreDesignBean d) {
		HashMap hm = new HashMap();
		hm.put("ID", d.getID());
		hm.put("SERVERNAME", d.getSERVERNAME());
		hm.put("SERVERMETHODNAME", d.getSERVERMETHODNAME());
		hm.put("CLIENTNAME", d.getCLIENTNAME());
		hm.put("CLIENTMETHODNAME", d.getCLIENTMETHODNAME());
		hm.put("CLIENTCAPTION", d.getCLIENTCAPTION());
		hm.put("DESCRIPTION", d.getDESCRIPTION());
		hm.put("SOLUTION", d.getSOLUTION());
		hm.put("CLIENTPROJECT", d.getCLIENTPROJECT());
		hm.put("SERVERPROJECT", d.getSERVERPROJECT());
		return hm;

	}

	public void update(String path, CoreDesignBean bean) {
		CoreFileOperation cfo = new CoreFileOperation();
		Field[] fields = CoreDesignBean.class.getDeclaredFields();
		ArrayList<CoreDesignBean> lu = new ArrayList<CoreDesignBean>();
		ArrayList<String> as = new ArrayList();
		HashMap hm = new HashMap();
		hm = this.setMap(bean);
		String[] args = new String[fields.length];
		for (int i = 0; i < fields.length; i++) {
			args[i] = fields[i].getName();
			System.out.println("fields " + args[i]);
		}

		as = (ArrayList<String>) cfo.updateXml(path,
				CoreGlobalConstants.XMLPARENTNODENAME, args, hm, "ID", bean
						.getID());
		lu = this.list2Beans(as);
		createListXml(path, lu);
	}

	public ArrayList<CoreDesignBean> list2Beans(ArrayList ls) {
		Map m = new HashMap();
		Map rowMap = new HashMap();
		ArrayList<CoreDesignBean> lb = new ArrayList<CoreDesignBean>();
		for (int i = 0; i < ls.size(); i++) {
			if (ls.get(i) instanceof Map) {
				CoreDesignBean bean = new CoreDesignBean();
				m = (Map) ls.get(i);
				bean.setID(m.get("ID") == null ? "" : m.get("ID").toString());
				bean.setSERVERNAME(m.get("SERVERNAME") == null ? "" : m.get(
						"SERVERNAME").toString());
				bean.setSERVERMETHODNAME(m.get("SERVERMETHODNAME") == null ? ""
						: m.get("SERVERMETHODNAME").toString());
				bean.setCLIENTNAME(m.get("CLIENTNAME") == null ? "" : m.get(
						"CLIENTNAME").toString());
				bean.setCLIENTMETHODNAME(m.get("CLIENTMETHODNAME") == null ? ""
						: m.get("CLIENTMETHODNAME").toString());
				bean.setCLIENTCAPTION(m.get("CLIENTCAPTION") == null ? "" : m
						.get("CLIENTCAPTION").toString());
				bean.setDESCRIPTION(m.get("DESCRIPTION") == null ? "" : m.get(
						"DESCRIPTION").toString());
				bean.setSOLUTION(m.get("SOLUTION") == null ? "" : m.get(
						"SOLUTION").toString());
				bean.setCLIENTPROJECT(m.get("CLIENTPROJECT") == null ? "" : m
						.get("CLIENTPROJECT").toString());
				bean.setSERVERPROJECT(m.get("SERVERPROJECT") == null ? "" : m
						.get("SERVERPROJECT").toString());
				lb.add(bean);
				
			}
		}
		return lb;
	}
	
	
	public HashMap list2Map(ArrayList ls) {
           HashMap  rowMap = new HashMap();
           ArrayList<CoreDesignBean> dl = this.list2Beans(ls);
           CoreDesignBean rowBean = new CoreDesignBean();
           for(Iterator it  = dl.iterator();it.hasNext();){
        	   rowBean = (CoreDesignBean) it.next();
        	   rowMap.put(rowBean.getID(), rowBean);
           }
           return rowMap;
	}
		
	public ArrayList getRowBeanList(ArrayList ls) {
		Map m = new HashMap();
		Map rowMap = new HashMap();
		ArrayList lb = new ArrayList();
		for (int i = 0; i < ls.size(); i++) {
			if (ls.get(i) instanceof Map) {
				CoreDesignBean bean = new CoreDesignBean();
				m = (Map) ls.get(i);
				bean.setID(m.get("ID") == null ? "" : m.get("ID").toString());
				bean.setSERVERNAME(m.get("SERVERNAME") == null ? "" : m.get(
						"SERVERNAME").toString());
				bean.setSERVERMETHODNAME(m.get("SERVERMETHODNAME") == null ? ""
						: m.get("SERVERMETHODNAME").toString());
				bean.setCLIENTNAME(m.get("CLIENTNAME") == null ? "" : m.get(
						"CLIENTNAME").toString());
				bean.setCLIENTMETHODNAME(m.get("CLIENTMETHODNAME") == null ? ""
						: m.get("CLIENTMETHODNAME").toString());
				bean.setCLIENTCAPTION(m.get("CLIENTCAPTION") == null ? "" : m
						.get("CLIENTCAPTION").toString());
				bean.setDESCRIPTION(m.get("DESCRIPTION") == null ? "" : m.get(
						"DESCRIPTION").toString());
				bean.setSOLUTION(m.get("SOLUTION") == null ? "" : m.get(
						"SOLUTION").toString());
				bean.setCLIENTPROJECT(m.get("CLIENTPROJECT") == null ? "" : m
						.get("CLIENTPROJECT").toString());
				bean.setSERVERPROJECT(m.get("SERVERPROJECT") == null ? "" : m
						.get("SERVERPROJECT").toString());
				rowMap.put( m.get("ID").toString(), bean);
				lb.add(rowMap);				
			}
		}
		return lb;
	}


	public List<String> findByParamsMap(String path, String id, String caption) {
		List<String> ls = new ArrayList();
		HashMap paramMap = new HashMap();
		HashMap paramMapType = new HashMap();
		if (!"".equals(id) && id != null) {
			paramMap.put("ID", id);
			paramMapType.put("ID", "1");
		}
		if (!"".equals(caption) && caption != null) {
			paramMap.put("CLIENTCAPTION", caption);
			paramMapType.put("CLIENTCAPTION", "2");
		}
//		ls = super.findByParamsMap(CoreDesignBean.class, path, paramMap,
//				paramMapType);
		return ls;
	}

	protected void createListXml(String path, List<CoreDesignBean> lu) {

//		Element root = new Element(CoreGlobalConstants.XMLROOTNODENAME);
//		Document Doc = new Document(root);
//		Element elements = null;
//		for (int i = 0; i < lu.size(); i++) {
//			elements = new Element(CoreGlobalConstants.XMLPARENTNODENAME);
//			CoreDesignBean u = lu.get(i);
//			//super.setElementContents(elements, u);
//			root.addContent(elements);
//		}
//
//		XMLOutputter XMLOut = new XMLOutputter();
//		try {
//			XMLOut.output(Doc, new FileOutputStream(path));
//		} catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}

	public static void main(String[] args) {
		CoreXmlDesignBean test = new CoreXmlDesignBean();
		 test.testcreate();
		// test.testadd();
		// test.testdel();
		// test.testupd();
		//test.testfind();
	}

	private void testcreate() {

		ArrayList<CoreDesignBean> ls = new ArrayList<CoreDesignBean>();
		CoreDesignBean d1 = new CoreDesignBean();
		d1.setID("1");
		d1.setSERVERNAME("s1");
		d1.setSERVERMETHODNAME("sm1");
		d1.setCLIENTNAME("c1");
		d1.setCLIENTMETHODNAME("cm1");

		ls.add(d1);

		CoreDesignBean d2 = new CoreDesignBean();
		d2.setID("2");
		d2.setSERVERNAME("s2");
		d2.setSERVERMETHODNAME("sm2");
		d2.setCLIENTNAME("c2");
		d2.setCLIENTMETHODNAME("cm2");

		ls.add(d2);
		this.createListXml(CoreGlobalConstants.DESIGNBEAN_LOACLXMLCONFIGPATH,
				ls);
	}

	private void testadd() {
		CoreDesignBean d1 = new CoreDesignBean();
		d1.setID("1");
		d1.setSERVERNAME("s1");
		d1.setSERVERMETHODNAME("sm1");
		d1.setCLIENTNAME("c1");
		d1.setCLIENTMETHODNAME("cm1");
		this.add(CoreGlobalConstants.DESIGNBEAN_LOACLXMLCONFIGPATH, d1);
	}

	//
	// private ArrayList<CoreUserBean> list2Beans(ArrayList ls) {
	// Map m = new HashMap();
	// ArrayList<CoreUserBean> lb = new ArrayList<CoreUserBean>();
	// for (int i = 0; i < ls.size(); i++) {
	// if (ls.get(i) instanceof Map) {
	// CoreUserBean bean = new CoreUserBean();
	// m = (Map) ls.get(i);
	// bean
	// .setAGE(m.get("AGE") == null ? "" : m.get("AGE")
	// .toString());
	// bean.setDEPARTMENTID(m.get("DEPARTMENT") == null ? "" : m.get(
	// "DEPARTMENT").toString());
	// bean.setDESCRIPTION(m.get("DESCRIPTION") == null ? "" : m.get(
	// "DESCRIPTION").toString());
	// bean.setEMAILADRESS(m.get("EMAILADRESS") == null ? "" : m.get(
	// "EMAILADRESS").toString());
	// bean.setHOMEADRESS(m.get("HOMEADRESS") == null ? "" : m.get(
	// "HOMEADRESS").toString());
	// bean.setIPADRESS(m.get("IPADRESS") == null ? "" : m.get(
	// "IPADRESS").toString());
	// bean.setPASSWORD(m.get("PASSWORD") == null ? "" : m.get(
	// "PASSWORD").toString());
	// bean.setPHONENUMBER(m.get("PHONENUMBER") == null ? "" : m.get(
	// "PHONENUMBER").toString());
	// bean.setROLEID(m.get("ROLE") == null ? "" : m.get("ROLE")
	// .toString());
	// bean
	// .setSEX(m.get("SEX") == null ? "" : m.get("SEX")
	// .toString());
	// bean.setUSERID(m.get("USERID").toString());
	// bean.setUSERNAME(m.get("USERNAME").toString());
	// lb.add(bean);
	// }
	// }
	// return lb;
	// }
	//
	private void testdel() {
		this.delete(CoreGlobalConstants.DESIGNBEAN_LOACLXMLCONFIGPATH, "1");
	}

	private void testupd() {
		CoreDesignBean d1 = new CoreDesignBean();
		d1.setID("1");
		d1.setSERVERNAME("s22");
		d1.setSERVERMETHODNAME("sm12211");
		d1.setCLIENTNAME("c111");
		d1.setCLIENTMETHODNAME("cm1111");
		this.update(CoreGlobalConstants.DESIGNBEAN_LOACLXMLCONFIGPATH, d1);
	}

	private void testfind() {
		List ls = this.findByParamsMap(
				CoreGlobalConstants.DESIGNBEAN_LOACLXMLCONFIGPATH, "1", "x");
		System.out.println("ls.size---- " + ls.size());
	}
	//
	// private void testfind() {
	// ArrayList<CoreUserBean> ls = new ArrayList<CoreUserBean>();
	// ls = this.findAll(CoreGlobalConstants.USERBEAN_LOACLXMLCONFIGPATH);
	// System.out.println(ls.size());
	// }

}
