

package com.platform.glusterfs;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import com.platform.entities.FolderNode;
import com.platform.utils.Constant;
import com.platform.utils.FileOperateHelper;
import com.platform.utils.GanymedSSH;

/**
 * <一句话功能简述> 获得GFS某个目录下的子目录
 * <功能详细描述>
 * @author    chen
 * @version   [版本号，2016年9月8日]
 * @see       [相关类/方法]
 * @since     [产品/模块版本]
 */
public class GetTreeData {
	ShowData showData = new ShowData();

	/**
	 * <一句话功能简述> 获得所以子目录
	 * <功能详细描述>
	 * @param name
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public FolderNode getDatas(String name) {
		String names[]=name.split("/");
		String only_name=names[names.length-1];
		FolderNode fileNode = new FolderNode(only_name);
		fileNode.setPath(name);
		Map<String, String> files = showData.showFolderData(name);
		if(files==null || files.size()==0){
			return fileNode;
		}
		fileNode.setIsFolder(files.size());
		List<FolderNode> list = new ArrayList<FolderNode>();
		fileNode.setChildNodes(list);
		for (Map.Entry<String, String> entry : files.entrySet()) {
			if(entry.getKey().equals("app")){
				continue;
			}
			int number = Integer.parseInt(entry.getValue());
			if (number == 1) {
				fileNode.getChildNodes().add(new FolderNode(entry.getKey(), number));
			}
			if (number > 1) {
				FolderNode temp=getDatas(name+"/"+entry.getKey());
				fileNode.getChildNodes().add(temp);
			}
		}

		return fileNode;
	}
	
	
	/**
	 * <一句话功能简述> 获得所以子目录
	 * <功能详细描述>
	 * @param name
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public FolderNode getDatasWithShell(String name) {
		if(name.endsWith("/")){
			name=name.substring(0, name.length()-1);
		}
//		String names[]=name.split("/");
//		String only_name=names[names.length-1];
		FolderNode fileNode = new FolderNode(name);
		fileNode.setPath(name);
		
//		String shellComment= new FileOperateHelper().fileReaderAndendline(Constant.fileGetTreeData);
		String shellComment= Constant.strGetTreeData;
		String sh_path="/getTreedata.sh";
		String cmd="echo -e \""+shellComment+"\" > "+sh_path+" & chmod +x "+sh_path;
		Constant.ganymedSSH.execCmdWaitAcquiescent(cmd);
//		Map<String, String> files = showData.showFolderData(name);
		List<String> files=Constant.ganymedSSH.execCmdWaitAcquiescent(sh_path+" "+name);
		if(files==null){
			return null;
		}
		if(files.size()==0){
			return fileNode;
		}
		for(String file:files){
			
		}

		return fileNode;
	}
	@Test
	public void test_getTreeData() {
	
		GetTreeData getTreeData=new GetTreeData();
//		FolderNode fileOrFolder=getTreeData.getDatas("/home/gfs_ftp_point");
		FolderNode fileOrFolder=getTreeData.getDatasWithShell("/home/gfs_ftp_point/");
		System.out.println(fileOrFolder);
	}
}
/*
class FileOrFolder {
	String name;
	int isFolder; // 1 is file and other integer is folder show children number
	List<FileOrFolder> children;

	public FileOrFolder(String name) {
		// TODO Auto-generated constructor stub
		this.name = name;
	}

	public FileOrFolder(String name, int isFolder) {
		// TODO Auto-generated constructor stub
		this.name = name;
		this.isFolder = isFolder;
	}
}

*/
