package com.siginfo.controller.base;

import java.io.Writer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class BeanToXml {
	
	private List<Map<String,Object>> list;

	public List<Map<String,Object>> getList() {
		
		return list;
	}

	public void setList(List<Map<String,Object>> list) {
		
		this.list = list;
	}
	
	public String getListXml () {
		
		Map<String,Object> map = null;
		String result = "";
		String field = "";
		
		for (int i = 0; i < list.size(); i ++) {
		
			result += "<list>";
			map = (HashMap<String, Object>)list.get(i);
			Iterator<String> it = map.keySet().iterator();
			
			while (it.hasNext()) {
			
				field = it.next();
				result += "<" + field + ">";
				result += map.get(field);
				result += "</" + field + ">";
			}
			
			result += "</list>";
		}
		
		return result;
	}
	
	
	public void writeListXml (Writer writer) {
		
		Map<String,Object> map = null;
		String field = "";
		Element root = new Element("content");
		
		if (list != null && list.size() > 0) {
			
			for (int i = 0; i < list.size(); i ++) {
				
				Element row = new Element("list");
				root.addContent(row);
				map = (HashMap<String, Object>)list.get(i);
				Iterator<String> it = map.keySet().iterator();
				
				while (it.hasNext()) {
				
					field = it.next();
					Element cols = new Element(field);
					String value = "";
					
					if (map.get(field) != null && map.get(field).toString().length() > 0) {
						value = map.get(field).toString(); 
						cols.setText( value );						
					}
					
					row.addContent(cols);
				}
			}
		} 
		
		else {
		
			root.setText("NULL");
		}
		
		try {
			
			Document doc = new Document (root);
			XMLOutputter xout = new XMLOutputter();
			Format format = Format.getPrettyFormat();
			format.setEncoding("UTF-8");
			xout.setFormat(format);
			xout.output(doc, writer);
		} 
		
		catch (Exception e) {
			
			e.printStackTrace();
		}
		
	}
	
	public void writeListXml4Paging (Writer writer, boolean NEED_PAGING) {
		
		Map<String,Object> map = null;
		String field = "";
		Element root = new Element("content");
		
		if (list != null && list.size() > 0) {
			
			for (int i = 0; i < list.size(); i ++) {
				
				Element row = null;
				
				if(NEED_PAGING == true) {
					
					if(i == list.size() - 1) {
						
						row = new Element("pageInfo");
					}
					
					else {
						
						row = new Element("list");
					}
				}
				
				else {
					
					row = new Element("list");
				}
				
				root.addContent(row);
				map = (HashMap<String, Object>)list.get(i);
				Iterator<String> it = map.keySet().iterator();
				
				while (it.hasNext()) {
				
					field = it.next();
					Element cols = new Element(field);
					String value = "";
					
					if (map.get(field) != null && map.get(field).toString().length() > 0) {
						value = map.get(field).toString(); 
						cols.setText( value );						
					}
					
					row.addContent(cols);
				}
			}
		} 
		
		else {
		
			root.setText("NULL");
		}
		
		try {
			
			Document doc = new Document (root);
			XMLOutputter xout = new XMLOutputter();
			Format format = Format.getPrettyFormat();
			format.setEncoding("UTF-8");
			xout.setFormat(format);
			xout.output(doc, writer);
		} 
		
		catch (Exception e) {
			
			e.printStackTrace();
		}
	}
}
