Index: Dev/branches/rest-dojo-ui/client/config/db.js
===================================================================
--- Dev/branches/rest-dojo-ui/client/config/db.js	(revision 360)
+++ Dev/branches/rest-dojo-ui/client/config/db.js	(revision 361)
@@ -6,12 +6,5 @@
          'dojo/query',
          'dijit/registry',
-         'dojox/io/xhrPlugins',
-         'dojo/text!./docs/rft/_security.json',
-         'dojo/text!./docs/_users/rft_admin.json',
-         'dojo/text!./docs/rft/_design/default.json',
-         'dojo/text!./docs/rft/q1.json',
-         'dojo/text!./docs/rft/q2.json',
-         'dojo/text!./docs/rft/q3.json',
-         'dojo/text!./docs/rft/q4.json',
+         'dojo/text!./docs.json',
          'dojo/domReady!',
          'dijit/form/TextBox',
@@ -19,36 +12,37 @@
          'dijit/form/CheckBox',
          ],
-    function(json,lang,xhr,parser,query,registry,xhrPlugins,
-        securityDoc,rft_adminDoc,design_defaultDoc,q1Doc,q2Doc,q3Doc,q4Doc){
+    function(json,lang,xhr,parser,query,registry,docsJson){
+    var logNode;
+    var usernameInput, passwordInput, resetInput, configButton;
+    var defaultUsername = "", defaultPassword = "";
+    var dbUrl = "../data/";
+
     parser.parse();
 
-    var logNode;
     query("#log").forEach(function(n){ logNode = n; });
+    usernameInput = registry.byId('username');
+    passwordInput = registry.byId('password');
+    resetInput = registry.byId('reset');
+    configButton = registry.byId('configure');
 
-    function log(text,overwrite) {
-        if ( overwrite ) logNode.innerHTML = text
-        else logNode.innerHTML = logNode.innerHTML + '\n' + text;
-    }
-
-    var usernameInput = registry.byId('username');
-    var passwordInput = registry.byId('password');
-    var resetInput = registry.byId('reset');
-
-    usernameInput.set('value','rft_admin');
-    passwordInput.set('value','Welkom01');
+    usernameInput.set('value',defaultUsername);
+    passwordInput.set('value',defaultPassword);
+    configButton.on('click',configure);
 
     log("Give CouchDB admin username & password and click 'Configure' to start.\nIf the database already exists, rft_admin password will suffice.",true);
 
-    registry.byId('configure').on('click',function(){
+    
+    function configure(){
         log("Configuring CouchDB for RFT:",true);
 
-        var baseUrl = '../data';
         var username = usernameInput.get('value');
         var password = passwordInput.get('value');
         var reset = resetInput.get('value');
 
+        var docs = json.fromJson(docsJson);
+
         function req(method,url,body) {
-            args = {
-                url: baseUrl+url,
+            var args = {
+                url: dbUrl+url,
                 contentType: 'application/json',
                 handleAs: 'json',
@@ -79,10 +73,9 @@
         });
 
-
         var exists = false;
         log("Checking database 'rft'");
         req('GET','/rft')
-        .then(function(res){
-            log("Database 'rft' found.\nLeaving security and users as they are.")
+        .then(function(db){
+            log("Database 'rft' found.")
             exists = true;
         });
@@ -94,34 +87,46 @@
             log("Creating database 'rft'")
             req('PUT','/rft')
-            
-            /*log("Setting _security on database 'rft'");
-            req('PUT','/rft/_security', securityDoc);
-
-            log("Checking 'rft_admin' user");
-            req('GET','/_users/org.couchdb.user:rft_admin')
-            .then(function(){
-                log("User 'rft_admin' already exists.");
-            }, function() {
-                log("Creating user 'rft_admin' with password 'Welkom01'");
-                req('PUT','/_users/org.couchdb.user:rft_admin', rft_adminDoc);
-            });*/
         };
 
-        log("Updating design documents");
-        newDoc = json.fromJson(design_defaultDoc);
-        req('GET','/rft/_design/default')
-        .then(function(doc){
-            newDoc['_rev'] = doc['_rev'];
-        });
-        req('PUT','/rft/_design/default', newDoc);
+        function processDoc(docUrl,doc){
+            var configAction = doc.__configAction;
+            delete doc.__configAction;
+            switch (configAction) {
+                case "ignore":
+                    log(docUrl+" ignored.");
+                    return false;
+                case "update":
+                    log(docUrl+" updating.");
+                    return req('GET',docUrl)
+                    .then(function(oldDoc){
+                        lang.mixin(oldDoc,doc);
+                        return req('PUT',docUrl,oldDoc);
+                    },function(){
+                        return req('PUT',docUrl,doc);
+                    });
+                case "replace":
+                default:
+                    log(docUrl+" replacing.");
+                    return req('GET',docUrl)
+                    .then(function(oldDoc){
+                        doc['_rev'] = oldDoc['_rev'];
+                        return req('PUT',docUrl,doc);
+                    },function(){
+                        return req('PUT',docUrl,doc);
+                    });
+            }
+        }
 
-        log("Uploading sample question documents");
-        req('PUT','/rft/q1', json.fromJson(q1Doc));
-        req('PUT','/rft/q2', json.fromJson(q2Doc));
-        req('PUT','/rft/q3', json.fromJson(q3Doc));
-        req('PUT','/rft/q4', json.fromJson(q4Doc));
+        for (var docUrl in docs) {
+            processDoc(docUrl,docs[docUrl]);
+        }
 
         log("Done!");
-    });
+    }
+
+    function log(text,overwrite) {
+        if ( overwrite ) logNode.innerHTML = text
+        else logNode.innerHTML = logNode.innerHTML + '\n' + text;
+    }
 
 });
