var pg = require('pg');
var config = require('../config.js');
var pgMap = require('../model/pgMap.js');
var debug = require('debug')('OSMBC:logModule');


module.exports.log = function log(object,callback) {
	debug("log");
  pg.connect(config.pgstring, function(err, client, pgdone) {
  	if (err) {
  		pgdone();
  		return (callback(err));
  	}
    object.timestamp = new Date();
  	var query = client.query("insert into changes (data) values ($1) ", [object]);
  	query.on('end',function (result) {  		
  		pgdone();
  		callback();
  	})
  })
}


module.exports.find = function(obj1,obj2,callback) {
  debug('find');
  pgMap.find(this,obj1,obj2,callback);
}
module.exports.findById = function(id,callback) {
  debug('findById');
  pgMap.findById(id,this,callback);
}

module.exports.create = function() {
  return {};
}

function createTable(cb) {
  debug('createTable');
  createString = 'CREATE TABLE changes (  id bigserial NOT NULL,  data json,  \
                  CONSTRAINT changes_pkey PRIMARY KEY (id) ) WITH (  OIDS=FALSE);'
  createView = '';
  pgMap.createTable('changes',createString,createView,cb)
}

function dropTable(cb) {
  debug('dropTable');
  pgMap.dropTable('changes',cb);
}

module.exports.table = "changes";
module.exports.createTable = createTable;
module.exports.dropTable = dropTable;
@@ -13,24 +13,54 @@
 uire('pg');%0A
+var async = require('async');%0A
 var config =
@@ -180,78 +180,958 @@
 ');%0A
-%0A%0Amodule.exports.log = function log(object,callback) %7B%0A%09debug(%22log%22);%0A
+var should = require('should');%0A%0Avar articleModule = require('../model/article.js');%0Avar blogModule = require('../model/blog.js');%0Avar userModule = require('../model/user.js');%0A%0A%0Amodule.exports.log = function log(object,callback) %7B%0A%09debug(%22log%22);%0A  async.series(%5B%0A    function checkOid(callback) %7B%0A      %0A      if (typeof(object.oid)!=%22object%22) return callback();%0A%0A      var table = oid.table;%0A      var reference = oid.r;%0A      var module;%0A%0A      switch (table) %7B%0A        case %22article%22: module = articleModule;break;%0A        case %22blog%22: module = blogModule;break;%0A        case %22user%22: module = userModule;break;%0A        default: module = null;%0A      %7D%0A      pgMap.find(module,reference,function(err,result) %7B%0A        if (err) return callback(err);%0A        if (result ==null) return callback(new Error(%22Object Id Not Found in Log Module%22));%0A        object.oid = result.id;%0A        callback(); %0A      %7D)%0A    %7D,%0A    function saveData(callback) %7B%0A    
   pg
@@ -1188,16 +1188,20 @@
 done) %7B%0A
+    
   %09if (e
@@ -1206,16 +1206,20 @@
 (err) %7B%0A
+    
   %09%09pgdo
@@ -1220,24 +1220,28 @@
 %09%09pgdone();%0A
+    
   %09%09return (
@@ -1262,11 +1262,19 @@
 ;%0A  
-%09%7D%0A
+    %09%7D%0A    
     
@@ -1304,16 +1304,20 @@
 Date();%0A
+    
   %09var q
@@ -1390,16 +1390,20 @@
 ject%5D);%0A
+    
   %09query
@@ -1436,16 +1436,20 @@
 ) %7B  %09%09%0A
+    
   %09%09pgdo
@@ -1454,16 +1454,20 @@
 done();%0A
+    
   %09%09call
@@ -1478,16 +1478,20 @@
 ();%0A
+    
   %09%7D)%0A  
 %7D)%0A%7D
@@ -1486,17 +1486,67 @@
   %09%7D)%0A  
-%7D
+    %7D)%0A    %7D%5D,%0A    function(err) %7Bcallback(err)%7D%0A  
 )%0A%7D%0A%0A%0Amo

Prepare logging for references in importfile.