// Test the integrity of the JSON stored in the database by trying to load each project

var paper = require('paper');
paper.setup(new paper.Canvas(1920, 1080));
var ueberDB = require("ueberDB");
var db = new ueberDB.database("dirty", {"filename" : "var/dirty.db"});

var projects = {};

db.init(function (err) {
  if(err) {
    console.error(err);
  }
  db.findKeys("*", "", function(err, rooms){
    var length = rooms.length;
    // begin for each
    for (var i = 0; i < length; i++){
      var room = rooms[i];
      projects[room] = {};
      projects[room].project = new paper.Project(paper.projects[0].view);
      projects[room].external_paths = {};
      loadFromDB(room);
    }
  });
});

// Try to load room from database
function loadFromDB(room) {
  if (projects[room] && projects[room].project) {
    var project = projects[room].project;
    db.init(function (err) {
      if(err) {
        console.error(err);
      }
      db.get(room, function(err, value) {
        if (value && project && project instanceof paper.Project && project.activeLayer) {
          // Clear default layer as importing JSON adds a new layer.
          // We want the project to always only have one layer.
          project.activeLayer.remove();
          project.importJSON(value.project);
          console.log("Loaded room", room);
          projects[room].destroy; // remove it
          console.log("Destroyed room", room);
        }
        db.close(function(){});
      });
    });
  } else {
    // loadError(socket);
  }
}

@@ -369,10 +369,12 @@
 *%22, 
-%22%22
+null
 , fu
@@ -424,16 +424,74 @@
 length;%0A
+    console.log(%22Analyzing JSON for %22, length, %22 rooms%22);%0A
     // b
@@ -708,24 +708,76 @@
 paths = %7B%7D;%0A
+      console.log(%22attempting to load room # %22, i);%0A
       loadFr
@@ -805,16 +805,17 @@
 );%0A%7D);%0A%0A
+%0A
 // Try t
@@ -964,91 +964,8 @@
 ct;%0A
-    db.init(function (err) %7B%0A      if(err) %7B%0A        console.error(err);%0A      %7D%0A  
     
@@ -996,26 +996,24 @@
 r, value) %7B%0A
-  
       if (va
@@ -1097,18 +1097,16 @@
         
- 
- 
 // Clear
@@ -1164,18 +1164,16 @@
         
-  
 // We wa
@@ -1214,26 +1214,24 @@
  one layer.%0A
-  
         proj
@@ -1260,26 +1260,24 @@
 ();%0A        
-  
 project.impo
@@ -1299,34 +1299,32 @@
 oject);%0A        
-  
 console.log(%22Loa
@@ -1327,24 +1327,25 @@
 %22Loaded room
+ 
 %22, room);%0A  
@@ -1346,26 +1346,24 @@
 m);%0A        
- 
- 
 projects%5Broo
@@ -1387,18 +1387,16 @@
 move it%0A
-  
         
@@ -1442,96 +1442,15 @@
     
-  %7D%0A        db.close(function()%7B%7D);%0A      %7D);%0A    %7D);%0A  %7D else %7B%0A    // loadError(socket
+%7D%0A    %7D
 );%0A 
@@ -1454,8 +1454,9 @@
 ;%0A  %7D%0A%7D%0A
+%0A

debug into test JSON file