package cn.autocode;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class AutoModelCONFIG {
	static String rl="\r\n";
	static String baseDir=System.getProperty("user.dir")+File.separator+"";
	static String nbaseDir=System.getProperty("user.dir")+File.separator+"";
	static String srcDir=System.getProperty("user.dir")+File.separator+"/src/";
	static String nsrcDir=System.getProperty("user.dir")+File.separator+"/src/";
	static String webDir=System.getProperty("user.dir")+File.separator+"/"+AutoCreateBeanXML_M_BASE.WEBCONFIG+"/";
	static String sourceDir=System.getProperty("user.dir")+File.separator+"/"+AutoCreateBeanXML_M_BASE.WEBCONFIG+"/source/";
	static String nwebDir=System.getProperty("user.dir")+File.separator+"/"+AutoCreateBeanXML_M_BASE.WEBCONFIG+"/";
	static String PROJECT = "";
	static String O_PROJECT = "";
	public static void initMode(String basepackage,String secondpackage,String model,String project,String localPROJECT) throws IOException {
		String basemodepack=basepackage+"."+secondpackage+"."+model;
		String wordArray[]=basemodepack.split("\\.");
		PROJECT = project;
		O_PROJECT = localPROJECT;		
		nbaseDir = nbaseDir.replaceAll(localPROJECT, project);
		nsrcDir = nsrcDir.replaceAll(localPROJECT, project);
		nwebDir = nwebDir.replaceAll(localPROJECT, project);
		String subDir=nsrcDir;
		for(int i=0;i<wordArray.length;i++){
			subDir=subDir+File.separator+wordArray[i];
			makeDirs(subDir);
		}		
	}
	
	/**
	 * 生成 sqlMapConfig XML
	 * 
	 * @param fileNameList
	 * @throws IOException
	 */
	public static void createAllMapConfig(String basePackage,String secondPackage,Map map)	throws IOException {
		String fullPackage= basePackage+"."+secondPackage;
		String wordArray[]=fullPackage.split("\\.");
		String subDir=nsrcDir;
		for(int i=0;i<wordArray.length;i++){
			subDir=subDir+File.separator+wordArray[i];
			makeDirs(subDir);
		}
		String fullFileName=subDir+File.separator+getFirstUppCaseName(secondPackage)+"-map-config.xml";
		String content=""+rl;
		String demopath = sourceDir+"/base/";
		String demofile = "demo-map-config.xml";
		FileInputStream fis = new FileInputStream(demopath + demofile); //创建文件输入
		InputStreamReader isr = new InputStreamReader(fis,"UTF-8"); //指定读取流为UTF-8编码格式
		BufferedReader br = new BufferedReader(isr);
		StringBuffer sb = new StringBuffer();
		StringBuffer ss = new StringBuffer();
		Set itt= map.keySet();
		Iterator it = itt.iterator();
		while(it.hasNext()){
			String key= (String) it.next();
			List list= (List)map.get(key);
			for(int k=0;k<list.size();k++){
				ss.append("	<sqlMap resource='"+basePackage+"/"+secondPackage+"/"+key.toLowerCase()+"/"+getFirstUppCaseName((String)list.get(k))+".xml'/>\t\r");
				ss.append("	<sqlMap resource='"+basePackage+"/"+secondPackage+"/"+key.toLowerCase()+"/"+getFirstUppCaseName((String)list.get(k))+"_ext.xml'/>\t\r");
			}
		}
		StringBuffer sbBean = new StringBuffer();
		String line = br.readLine();
		String fileName = "";		
		while (line != null) {		
			if(line.equals("RH_MAP_BEAN_CONFIG")){
				sb.append(ss);
			}else{
				sb.append(line + "\r\n");
			}
			line = br.readLine();
		}
		br.close();
		isr.close();	
		fis.close();	
		createFileUTF8(fullFileName,sb.toString());	
	}
	/**
	 * 生成 sqlMapConfig XML
	 * 
	 * @param fileNameList
	 * @throws IOException
	 */
	public static void createAllDWRConfig(String basePackage,String secondPackage,Map map)	throws IOException {
		String tablename = "";
		String tablenameL = "";
		String tablenameF = "";
		String tablenameS = "";
		StringBuffer sbb = new StringBuffer();	
		StringBuffer sbb2 = new StringBuffer();	
		Set itt= map.keySet();
		Iterator it = itt.iterator();
		while(it.hasNext()){
			String key= (String) it.next();
			List tablelist= (List)map.get(key);
			for (int i = 0; i < tablelist.size(); i++) {
				tablename = (String) tablelist.get(i);
				tablenameL = tablename.toLowerCase();
				tablenameF = getFirstUppCaseName(tablename);
				tablenameS = getFirstLowerCaseName(tablename);
				sbb.append("		<convert converter=\"bean\" match=\""+basePackage+"."+secondPackage+"."+key.toLowerCase()+"."+(tablenameF)+"Bean\" />\r\n");
				sbb2.append("		<create creator=\"new\" javascript=\""+tablenameF+"Service\">\r\n");
				sbb2.append("			<param name=\"class\" value=\""+basePackage+"."+secondPackage+"."+key.toLowerCase()+"."+tablenameF+"Service\" />\r\n");
				sbb2.append("		</create>\r\n");
			}
		}
		sbb.append("\r\n"+sbb2);
		String fullPackage= basePackage+"."+secondPackage;
		String wordArray[]=fullPackage.split("\\.");
		String subDir=nwebDir;
		for(int i=0;i<wordArray.length;i++){
			subDir=subDir+File.separator+wordArray[i];
			//makeDirs(subDir);
		}
		String fullFileName=nwebDir+"/WEB-INF/conf/"+File.separator+"dwr.xml";
		String content=""+rl;
		String demopath = sourceDir+"/"+PROJECT+"/";
		String demofile = "dwr.xml";
		FileInputStream fis = new FileInputStream(demopath + demofile); //创建文件输入
		InputStreamReader isr = new InputStreamReader(fis,"UTF-8"); //指定读取流为UTF-8编码格式
		BufferedReader br = new BufferedReader(isr);
		StringBuffer sb = new StringBuffer();
		StringBuffer sbBean = new StringBuffer();
		String line = br.readLine();
		String fileName = "";		
		while (line != null) {			
			if(line.indexOf("RH_DWR_BEAN_CONFIG")!=-1){
				sb.append(sbb + "\r\n");
			}else{
				sb.append(line + "\r\n");
			}
			line = br.readLine();
		}
		br.close();
		isr.close();	
		fis.close();	
		//System.out.println("===="+sb.toString());
		createFileUTF8(fullFileName,sb.toString());	
	}
	
	/**
	 * 生成 sqlMapConfig XML
	 * 
	 * @param fileNameList
	 * @throws IOException
	 */
	public static void createAllSpringDaoConfig(String basePackage,String secondPackage,Map map)throws IOException {
		String tablename = "";
		String tablenameL = "";
		String tablenameF = "";
		String tablenameS = "";
		StringBuffer sbb = new StringBuffer();	
		StringBuffer sbb2 = new StringBuffer();	
		
		Set itt= map.keySet();
		Iterator it = itt.iterator();
		while(it.hasNext()){
			String key= (String) it.next();
			List tablelist= (List)map.get(key);
			for (int i = 0; i < tablelist.size(); i++) {
				tablename = (String) tablelist.get(i);
				tablenameL = tablename.toLowerCase();
				tablenameF = getFirstUppCaseName(tablename);
				tablenameS = getFirstLowerCaseName(tablename);
				sbb.append("	<bean id=\""+tablenameS+"Dao\" class=\""+basePackage+"."+secondPackage+"."+key.toLowerCase()+"."+(tablenameF)+"Dao\">\r\n");
				sbb.append("		<property name=\"sqlMapClient\"><ref bean=\"sqlMapClient\" /></property>\r\n");
				sbb.append("	</bean>\r\n");
			}
		}
		sbb.append("\r\n"+sbb2);
		String fullPackage= basePackage+"."+secondPackage;
		String wordArray[]=fullPackage.split("\\.");
		String subDir=nwebDir;
		for(int i=0;i<wordArray.length;i++){
			subDir=subDir+File.separator+wordArray[i];
			//makeDirs(subDir);
		}
		String fullFileName= nwebDir +"/WEB-INF/conf/"+File.separator+"springDao.xml";
		String content=""+rl;
		String demopath = sourceDir+"/"+PROJECT+"/";
		String demofile = "springDao.xml";
		FileInputStream fis = new FileInputStream(demopath + demofile); //创建文件输入
		InputStreamReader isr = new InputStreamReader(fis,"UTF-8"); //指定读取流为UTF-8编码格式
		BufferedReader br = new BufferedReader(isr);
		StringBuffer sb = new StringBuffer();
		StringBuffer sbBean = new StringBuffer();
		String line = br.readLine();
		String fileName = "";		
		while (line != null) {			
			if(line.indexOf("RH_SPRING_DAO_CONFIG")!=-1){
				sb.append(sbb + "\r\n");
			}else{
				sb.append(line + "\r\n");
			}
			line = br.readLine();
		}
		br.close();
		isr.close();	
		fis.close();	
		//System.out.println("===="+sb.toString());
		createFileUTF8(fullFileName,sb.toString());	
	}
	
	/**
	 * 生成 sqlMapConfig XML
	 * 
	 * @param fileNameList
	 * @throws IOException
	 */
	public static void createAllSpringServiceConfig(String basePackage,String secondPackage,Map map)throws IOException {
		String tablename = "";
		String tablenameL = "";
		String tablenameF = "";
		String tablenameS = "";
		StringBuffer sbb = new StringBuffer();	
		StringBuffer sbb2 = new StringBuffer();	
		
		Set itt= map.keySet();
		Iterator it = itt.iterator();
		while(it.hasNext()){
			String key= (String) it.next();
			List tablelist= (List)map.get(key);
			for (int i = 0; i < tablelist.size(); i++) {
				tablename = (String) tablelist.get(i);
				tablenameL = tablename.toLowerCase();
				tablenameF = getFirstUppCaseName(tablename);
				tablenameS = getFirstLowerCaseName(tablename);
				sbb.append("	<bean id=\""+tablenameS+"Service\" class=\""+basePackage+"."+secondPackage+"."+key.toLowerCase()+"."+(tablenameF)+"Service\"></bean>\r\n");
			}
		}
		sbb.append("\r\n"+sbb2);
		String fullPackage= basePackage+"."+secondPackage;
		String wordArray[]=fullPackage.split("\\.");
		String subDir=nwebDir;
		for(int i=0;i<wordArray.length;i++){
			subDir=subDir+File.separator+wordArray[i];
			makeDirs(subDir);
		}
		String fullFileName=nbaseDir +File.separator+"config/spring/"+File.separator+"springService.xml";
		String content=""+rl;
		String demopath = sourceDir+"/"+PROJECT+"/";
		String demofile = "springService.xml";
		FileInputStream fis = new FileInputStream(demopath + demofile); //创建文件输入
		InputStreamReader isr = new InputStreamReader(fis,"UTF-8"); //指定读取流为UTF-8编码格式
		BufferedReader br = new BufferedReader(isr);
		StringBuffer sb = new StringBuffer();
		StringBuffer sbBean = new StringBuffer();
		String line = br.readLine();
		String fileName = "";		
		while (line != null) {			
			if(line.indexOf("RH_SPRING_SERVICE_CONFIG")!=-1){
				sb.append(sbb + "\r\n");
			}else{
				sb.append(line + "\r\n");
			}
			line = br.readLine();
		}
		br.close();
		isr.close();	
		fis.close();	
		//System.out.println("===="+sb.toString());
		createFileUTF8(fullFileName,sb.toString());	
	}
	
	public static void createModelService(String basePackage,String secondPackage,Map map) throws IOException {
		String basemodepack=basePackage+"."+secondPackage;
		String wordArray[]=basemodepack.split("\\.");
		String subDir=nsrcDir;
		for(int i=0;i<wordArray.length;i++){
			subDir=subDir+File.separator+wordArray[i];
			makeDirs(subDir);
		}
		String fullFileName = subDir+File.separator+getFirstUppCaseName(secondPackage)+"Service."+"java";
		String content1 = "";
		String content2 = "";
		String content = "";
		content1 += "package " + basemodepack+";"+rl;
		content1 += rl;
		content1 += "import cn.base.service.BaseService;"+rl;
		content+="public class "+getFirstUppCaseName(secondPackage)+"Service extends BaseService {"+rl;
		Set itt= map.keySet();
		Iterator it = itt.iterator();
		while(it.hasNext()){
			String key= (String) it.next();
			List tablelist= (List)map.get(key);
			for(int i=0;i<tablelist.size();i++){
				String tablename = (String)tablelist.get(i);
				//System.out.println(i+"========"+tablename);
				String tablenameL = tablename.toLowerCase();
				String tablenameF = getFirstUppCaseName(tablename);
				String tablenameS = getFirstLowerCaseName(tablename);
				content2 += "import "+basePackage+"."+secondPackage+"."+key.toLowerCase()+"."+tablenameF+"Dao;"+rl;
				content += "	protected "+tablenameF+"Dao get"+tablenameF+"Dao(){ "+rl;
				content += "		return ("+tablenameF+"Dao) this.getBean(\""+tablenameS+"Dao\"); "+rl;
				content += "	} "+rl;
			}
		}
		content+= "}"+rl;
		content = content1+content2+content;
		createFileUTF8(fullFileName,content);			
	}
	
	/**
	 * 生成 sqlMapConfig XML
	 * 
	 * @param fileNameList
	 * @throws IOException
	 */
	public static void createConfMapConfig(String basePackage,String secondPackage,Map map)	throws IOException {
		String fullPackage= basePackage+"."+secondPackage;
		String wordArray[]=fullPackage.split("\\.");
		String subDir=baseDir;
		for(int i=0;i<wordArray.length;i++){
			subDir=subDir+File.separator+wordArray[i];
			//makeDirs(subDir);
		}
		String fullFileName= nbaseDir +File.separator+"conf"+File.separator+"SqlMapConfig.xml";
		String content=""+rl;
		String demopath = sourceDir+"/base/";
		String demofile = "demo-SqlMapConfig.xml";
		FileInputStream fis = new FileInputStream(demopath + demofile); //创建文件输入
		InputStreamReader isr = new InputStreamReader(fis,"UTF-8"); //指定读取流为UTF-8编码格式
		BufferedReader br = new BufferedReader(isr);
		StringBuffer sb = new StringBuffer();
		StringBuffer ss = new StringBuffer();
		Set itt= map.keySet();
		Iterator it = itt.iterator();
		while(it.hasNext()){
			String key= (String) it.next();
			List list= (List)map.get(key);
			for(int k=0;k<list.size();k++){
				ss.append("	<sqlMap resource='"+basePackage+"/"+secondPackage+"/"+key.toLowerCase()+"/"+getFirstUppCaseName((String)list.get(k))+".xml'/>\t\r");
				ss.append("	<sqlMap resource='"+basePackage+"/"+secondPackage+"/"+key.toLowerCase()+"/"+getFirstUppCaseName((String)list.get(k))+"_ext.xml'/>\t\r");
			}
		}
		String line = br.readLine();
		while (line != null) {		
			if(line.indexOf("RH_MAP_BEAN_CONFIG")!=-1){
				sb.append(ss);
			}else{
				sb.append(line + "\r\n");
			}
			line = br.readLine();
		}
		br.close();
		isr.close();	
		fis.close();	
		createFileUTF8(fullFileName,sb.toString());	
	}
	
	/*
	public static void createFileGBK(String fullFileName,String sb){
		try {
			OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(fullFileName),"GBK");
	        out.write(sb.toString());
	        out.flush();
	        out.close();	     
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	*/
	
	public static void createFileUTF8(String fullFileName,String sb){
		try {
			OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(fullFileName),"UTF-8");
	        out.write(sb.toString());
	        out.flush();
	        out.close();	     
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	public static boolean makeDirs(String path){
	    File tmpFile=new File(path);
	    boolean ret=tmpFile.mkdirs();
	    return ret;
	}
	public static String getFirstUppCaseName(String name){
		String retName ="";
		if(name.length()>0){
			retName = name.substring(0, 1).toUpperCase()+name.substring(1);
		}
		return retName;
	}
	public static String getFirstLowerCaseName(String name){
		String retName ="";
		if(name.length()>0){
			retName = name.substring(0, 1).toLowerCase()+name.substring(1);
		}
		return retName;
	}
}
