var fs = require('fs');


var mongoDriver = require('./../../build/server/mongoDriver.js');
var config = require('./../../build/server/config.js');

var universalDaoModule = require('./../../build/server/UniversalDao.js');
var schemaRegistryModule = require('./../../build/server/schemaRegistry.js');

var universalDaoControllerModule = require('./../../build/server/UniversalDaoController.js');

var path = require('path');
var async = require('async');


var util = require('util');
var stream = require('stream');


var schema=null;

process.argv.forEach(function(val, index, array) {
	console.log(index + ': ' + val);
});

console.log('file to process', process.argv[2]);

if (process.argv[2]) {
 	schema =  process.argv[2];
}

mongoDriver.init(config.mongoDbURI, function(err) {
	if (err) {
		throw err;
	}

	var schemasListPaths = JSON.parse(
		fs.readFileSync(path.join(config.paths.schemas, '_index.json')))
		.map(function(item) {
			return path.join(config.paths.schemas, item);
	});

	var schemaRegistry = new schemaRegistryModule.SchemaRegistry({schemasList:schemasListPaths});
	var udc = new universalDaoControllerModule.UniversalDaoController(mongoDriver, schemaRegistry);

	go(udc,schema);

});



function go(udc,schema) {

	var req={params:{schema:schema},body:{}};
	req.perm={'Registry - read':true};

	var res=function (){
		this.send=function (code ,data){
			console.log(code,data);
		};
		this.status=function (status){
			console.log(status);
			return this;
		};
		this.json=function(data){
			iterateData(data,udc);
		};
	};

	req.perm={"Registry - read": true};
	util.inherits(res, require('stream').Writable);
	res= new res();
	udc.searchBySchema(req,res);

}


function iterateData(data,udc){

	data.map(function (item){

		saveItem(item,udc);

	});

}

function saveItem(item,udc){

		var req={currentUser:{id:-1},params:{schema:schema},body:item};
			var res=function (){

			this.send=function (code ,data){
			console.log("save res",code,data);
			};

			this.status=function (status){
				console.log(status);
				return this;
			};

			this.json=function(data){
				console.log('.');
			};

	};

	util.inherits(res, require('stream').Writable);
	res= new res();
	req.perm={"Registry - write": true};

	udc.saveBySchema(req,res);


}

@@ -725,16 +725,35 @@
 %5B2%5D;%0A%7D%0A%0A
+var itemsSaved=0;%0A%0A
 mongoDri
@@ -1214,16 +1214,119 @@
 c,schema
+,function(err)%7B if(err)%7Bconsole.log(err)%7D mongoDriver.close(); console.log('Items saved',itemsSaved); %7D
 );%0A%0A%7D);%0A
@@ -1350,16 +1350,25 @@
 c,schema
+,callback
 ) %7B%0A%0A%09va
@@ -1403,16 +1403,29 @@
 %7D,body:%7B
+limit:1000000
 %7D%7D;%0A%09req
@@ -1675,16 +1675,25 @@
 data,udc
+,callback
 );%0A%09%09%7D;%0A
@@ -1868,65 +1868,217 @@
 ,udc
-)%7B%0A%0A%09data.map(function (item)%7B%0A%0A%09%09saveItem(item,udc);%0A%0A%09%7D
+,callback)%7B%0A%0A%09console.log('Data fully read',data.length);%0A%09var toCall=data.map(function (item)%7B%0A%0A%09%09return function(callback) %7B%0A%09%09%09saveItem(item,udc,callback);%0A%09%09%7D;%0A%09%7D);%0A%0A%09async.parallelLimit(toCall,10,callback
 );%0A%0A
@@ -2106,16 +2106,25 @@
 item,udc
+,callback
 )%7B%0A%0A%09%09va
@@ -2241,32 +2241,33 @@
 code ,data)%7B%0A%09%09%09
+ 
 console.log(%22sav
@@ -2285,16 +2285,49 @@
 ,data);%0A
+%09%09%09itemsSaved++;%0A%09%09%09 callback();%0A
 %09%09%09%7D;%0A%0A%09
@@ -2459,16 +2459,51 @@
 g('.');%0A
+%09%09%09  itemsSaved++;%0A%09%09%09 callback();%0A
 %09%09%09%7D;%0A%0A%09

change limit to 100k of touch entities.