package com.example.blogerwriter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.os.Environment;

public class VersionTransfer {
	
	private Context mContext;
	
	public VersionTransfer(Context context){
		mContext=context;
	}
	public void versionTransfer(){
		String sdpath=Environment.getExternalStorageDirectory().getAbsolutePath();
		String fileName="blog_isaid";
		ArrayList<HashMap<String, String>> datas=readText(sdpath,fileName);
		transferToDB(datas);
	}
	public void transferToDB(ArrayList<HashMap<String, String>> datas){
		
		DBHelper dbHelper=new DBHelper(mContext);
		dbHelper.open();
		for(HashMap<String, String> m:datas){
			dbHelper.insertData(m.get(DBHelper.KEY_DATA), m.get(DBHelper.KEY_DATE));
		}
	}
	public ArrayList<HashMap<String, String>> readText(String path,String filename){
		ArrayList<HashMap<String, String>> datas=new ArrayList<HashMap<String,String>>();
		File isaidFile=new File(path, filename);
		try {
			String str="";
			BufferedReader bReader=new BufferedReader(new FileReader(isaidFile));
			while((str=bReader.readLine())!=null){
				HashMap<String, String> mp=new HashMap<String, String>();
				mp.put(DBHelper.KEY_DATA, str);
				str=bReader.readLine();
				mp.put(DBHelper.KEY_DATE, str);
				datas.add(mp);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return datas;
	}

}
