package com.platform.http.gfs;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.base.MyException;
import com.base.PostData;
import com.google.gson.Gson;
import com.platform.http.HttpUtils;

/** 迁移数据
 * @author chen
 *
 */
public class RemoveData {
	
	public static Logger log =   Logger.getLogger ( RemoveData.class); 
	HttpUtils ht = new HttpUtils();
	
	/**
	 * -1 :error; 0: the filename is not exists ; 1:  right
	 * @param folderName
	 * @param fileName
	 * @return
	 */
	public int deleteFolder(String folderName) throws Exception{
		int reslut = 0;
		log.info("start delete "+folderName);	

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("removePath", folderName);
		//请求  web   sendPost
		String rest = ht.sendPost("data/removeData", map);
		if (null == rest || "".equals(rest)) {
			log.error(" ---    gluster is disconnect !   \n");
			return 0;
		}
		Gson gs = new Gson();
		PostData data = gs.fromJson(rest, PostData.class);
		// 1 : 成功
		double copyResult = (double) data.getData();
		reslut = (int) copyResult;
		getExcept(data);
		return reslut;
	}
	
	public int abortcopyFolder(String sourceFolderName, String destFolderName)  throws Exception{
		int reslut = 0;
		log.info(" abortcopyFolder     " + sourceFolderName + " --> " + destFolderName);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sourcePath", sourceFolderName);
		map.put("destPath", destFolderName);
		//请求web
		String rest = ht.sendPost("data/abortCopyData", map);
		if (null == rest || "".equals(rest)) {
			log.error(" ---    gluster is disconnect !   \n");
			return 0;
		}
		Gson gs = new Gson();
		PostData data = gs.fromJson(rest, PostData.class);
		// 1 : 成功
		double copyResult = (double) data.getData();
		reslut = (int) copyResult;
		getExcept(data);
		return reslut;
	}

	/** 记录异常信息
	 * @param pd
	 */
	private void getExcept(PostData pd) {
		StringBuffer sb = new StringBuffer();
		for ( MyException object : pd.getExceptionsStack()) {
			sb.append(object.getMess()).append(",");
		}
		if (!"".equals(sb.toString())) {
			log.error(sb.toString());
		}
	}
	
}
