[328] | 1 | require([ |
---|
| 2 | 'dojo/_base/json', |
---|
| 3 | 'dojo/_base/lang', |
---|
| 4 | 'dojo/_base/xhr', |
---|
| 5 | 'dojo/parser', |
---|
| 6 | 'dojo/query', |
---|
| 7 | 'dijit/registry', |
---|
| 8 | 'dojox/io/xhrPlugins', |
---|
| 9 | 'dojo/text!./docs/rft/_security.json', |
---|
| 10 | 'dojo/text!./docs/_users/rft_admin.json', |
---|
| 11 | 'dojo/text!./docs/rft/_design/default.json', |
---|
[360] | 12 | 'dojo/text!./docs/rft/q1.json', |
---|
| 13 | 'dojo/text!./docs/rft/q2.json', |
---|
| 14 | 'dojo/text!./docs/rft/q3.json', |
---|
| 15 | 'dojo/text!./docs/rft/q4.json', |
---|
[328] | 16 | 'dojo/domReady!', |
---|
| 17 | 'dijit/form/TextBox', |
---|
| 18 | 'dijit/form/Button', |
---|
| 19 | 'dijit/form/CheckBox', |
---|
| 20 | ], |
---|
| 21 | function(json,lang,xhr,parser,query,registry,xhrPlugins, |
---|
[360] | 22 | securityDoc,rft_adminDoc,design_defaultDoc,q1Doc,q2Doc,q3Doc,q4Doc){ |
---|
[328] | 23 | parser.parse(); |
---|
| 24 | |
---|
| 25 | var logNode; |
---|
| 26 | query("#log").forEach(function(n){ logNode = n; }); |
---|
| 27 | |
---|
| 28 | function log(text,overwrite) { |
---|
| 29 | if ( overwrite ) logNode.innerHTML = text |
---|
| 30 | else logNode.innerHTML = logNode.innerHTML + '\n' + text; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | var usernameInput = registry.byId('username'); |
---|
| 34 | var passwordInput = registry.byId('password'); |
---|
| 35 | var resetInput = registry.byId('reset'); |
---|
| 36 | |
---|
| 37 | usernameInput.set('value','rft_admin'); |
---|
| 38 | passwordInput.set('value','Welkom01'); |
---|
| 39 | |
---|
| 40 | log("Give CouchDB admin username & password and click 'Configure' to start.\nIf the database already exists, rft_admin password will suffice.",true); |
---|
| 41 | |
---|
| 42 | registry.byId('configure').on('click',function(){ |
---|
| 43 | log("Configuring CouchDB for RFT:",true); |
---|
| 44 | |
---|
[352] | 45 | var baseUrl = '../data'; |
---|
[328] | 46 | var username = usernameInput.get('value'); |
---|
| 47 | var password = passwordInput.get('value'); |
---|
| 48 | var reset = resetInput.get('value'); |
---|
| 49 | |
---|
| 50 | function req(method,url,body) { |
---|
| 51 | args = { |
---|
| 52 | url: baseUrl+url, |
---|
| 53 | contentType: 'application/json', |
---|
| 54 | handleAs: 'json', |
---|
| 55 | sync: true, |
---|
| 56 | error: function(err) { |
---|
| 57 | log("ERROR: "+err); |
---|
| 58 | } |
---|
| 59 | }; |
---|
| 60 | if ( !body || lang.isObject(body) ) { |
---|
| 61 | body = json.toJson(body || {}); |
---|
| 62 | } |
---|
| 63 | args.rawBody = body; |
---|
| 64 | if ( username ) { |
---|
| 65 | args.user = username; |
---|
| 66 | args.password = password; |
---|
| 67 | } |
---|
| 68 | return xhr(method,args,true); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | log("Checking CouchDB version"); |
---|
| 72 | req('GET','/') |
---|
| 73 | .then(function(res){ |
---|
| 74 | if (res.version !== "1.2.0" ) { |
---|
| 75 | log("Found "+res.version+", only tested with CouchDB 1.2.0") |
---|
| 76 | } else { |
---|
| 77 | log("CouchDB 1.2.0 found") |
---|
| 78 | } |
---|
| 79 | }); |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | var exists = false; |
---|
| 83 | log("Checking database 'rft'"); |
---|
| 84 | req('GET','/rft') |
---|
| 85 | .then(function(res){ |
---|
| 86 | log("Database 'rft' found.\nLeaving security and users as they are.") |
---|
| 87 | exists = true; |
---|
| 88 | }); |
---|
| 89 | if ( exists && reset ) { |
---|
| 90 | req('DELETE','/rft'); |
---|
| 91 | exists = false; |
---|
| 92 | } |
---|
| 93 | if ( !exists ) { |
---|
| 94 | log("Creating database 'rft'") |
---|
| 95 | req('PUT','/rft') |
---|
| 96 | |
---|
[343] | 97 | /*log("Setting _security on database 'rft'"); |
---|
[328] | 98 | req('PUT','/rft/_security', securityDoc); |
---|
| 99 | |
---|
| 100 | log("Checking 'rft_admin' user"); |
---|
| 101 | req('GET','/_users/org.couchdb.user:rft_admin') |
---|
| 102 | .then(function(){ |
---|
| 103 | log("User 'rft_admin' already exists."); |
---|
| 104 | }, function() { |
---|
| 105 | log("Creating user 'rft_admin' with password 'Welkom01'"); |
---|
| 106 | req('PUT','/_users/org.couchdb.user:rft_admin', rft_adminDoc); |
---|
[343] | 107 | });*/ |
---|
[328] | 108 | }; |
---|
| 109 | |
---|
| 110 | log("Updating design documents"); |
---|
| 111 | newDoc = json.fromJson(design_defaultDoc); |
---|
| 112 | req('GET','/rft/_design/default') |
---|
| 113 | .then(function(doc){ |
---|
| 114 | newDoc['_rev'] = doc['_rev']; |
---|
| 115 | }); |
---|
| 116 | req('PUT','/rft/_design/default', newDoc); |
---|
| 117 | |
---|
[360] | 118 | log("Uploading sample question documents"); |
---|
| 119 | req('PUT','/rft/q1', json.fromJson(q1Doc)); |
---|
| 120 | req('PUT','/rft/q2', json.fromJson(q2Doc)); |
---|
| 121 | req('PUT','/rft/q3', json.fromJson(q3Doc)); |
---|
| 122 | req('PUT','/rft/q4', json.fromJson(q4Doc)); |
---|
| 123 | |
---|
[328] | 124 | log("Done!"); |
---|
| 125 | }); |
---|
| 126 | |
---|
| 127 | }); |
---|