Skip to content

Commit d27236d

Browse files
committed
Now resources are fetched from subcollections also
1 parent cb05d76 commit d27236d

3 files changed

Lines changed: 61 additions & 18 deletions

File tree

app.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ app.get('/prepare-starter-data', function(req, res) {
3737
res.render('starter_data', {jsonForSelectingDataFromUI: jsonForSelectingDataFromUI});
3838
});
3939

40-
// app.get('/replication-erase', function(req, res){
41-
// var replicator = require('./replicator')(couchSource, couchTarget, databases);
42-
// replicator.deleteDatabases(res);
43-
// });
44-
4540
var server = app.listen(3000, function() {
4641
console.log('Listening on port %d', server.address().port);
4742
});
@@ -79,8 +74,6 @@ io.sockets.on('connection', function (socketInst) {
7974
console.log("app.js:: socketInst.on('socketSearchResourceRequest'):: final callback error");
8075
console.log(err);
8176
} else {
82-
// result contains resource doc info for the searched name
83-
// console.log(result);
8477
var sockResponseData = {arrMatchingResources: result}; // result might have 0 records in it
8578
socketInst.emit('socketSearchResourceResponse', sockResponseData);
8679
}
@@ -97,8 +90,6 @@ io.sockets.on('connection', function (socketInst) {
9790
console.log("app.js:: socketInst.on('socketSearchCourseRequest'):: final callback error");
9891
console.log(err);
9992
} else {
100-
// result contains course doc info for the searched name
101-
// console.log(result);
10293
var sockResponseData = {arrMatchingCourses: result}; // result might have 0 records in it
10394
socketInst.emit('socketSearchCourseResponse', sockResponseData);
10495
}
@@ -108,7 +99,7 @@ io.sockets.on('connection', function (socketInst) {
10899
socketInst.on('fetchResourcesForThisCollection', function(collectionId, collectionName) {
109100
waterfall([
110101
function(callback){
111-
dao.fetchResourcesPointingToThisCollection(collectionId, collectionName, callback);
102+
dao.fetchResourcesPointingToThisCollectionAndItsSubCollections(collectionId, collectionName, callback);
112103
}
113104
], function (err, result) {
114105
var dataForChosenCollection = {err: null, data: null, collectionName: collectionName};

services/dbInteractions.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,65 @@ module.exports = function (localCouchServer, sourceCouchServer) {
463463
});
464464
};
465465

466+
functions.fetchResourcesPointingToThisCollectionAndItsSubCollections = function(collectionId, collectionName, callback) {
467+
var subCollectionsDb = sourceCouchDb.db.use('collectionlist');
468+
var resourcesDb = sourceCouchDb.db.use('resources');
469+
var subCollectionsViewkeys = [collectionId];
470+
var resourcesViewkeys = [collectionId];
471+
var arrResources = [];
472+
var arrSubCollection = [];
473+
474+
475+
// Check if major-collection have any resources
476+
resourcesDb.view('bell', 'listCollection',{keys: resourcesViewkeys, include_docs: true}, function(err, resp) {
477+
if (err) {
478+
console.log("dbInteractions.js:: fetchResourcesPointingToThisCollectionAndItsSubCollections:: error fetching docs of db: " + 'resources'); console.log(err);
479+
callback(err);
480+
} else {
481+
var resourceIdAndTitle;
482+
console.log("fetched resources count for collection: " + collectionName + " (" + collectionId + "): " + resp.rows.length);
483+
resp.rows.forEach( function(resourceDocContainer) {
484+
resourceIdAndTitle = {id: resourceDocContainer.doc._id, name: resourceDocContainer.doc.title};
485+
console.log("resourceId: " + resourceIdAndTitle.id);
486+
arrResources.push(resourceIdAndTitle);
487+
});
488+
}
489+
});
490+
491+
// Include all resources from all sub-collections
492+
subCollectionsDb.view('bell', 'subCategoriesByMajorCategory',{keys: subCollectionsViewkeys, include_docs: true}, function(err, resp) {
493+
if (err) {
494+
console.log("dbInteractions.js:: fetchResourcesPointingToThisCollectionAndItsSubCollections:: error fetching docs of db: " + 'collectionlist'); console.log(err);
495+
callback(err);
496+
} else {
497+
console.log("fetched sub-collections count for collection: " + collectionName + " (" + collectionId + "): " + resp.rows.length);
498+
if(resp.rows.length == 0)
499+
callback(null, arrResources);
500+
resp.rows.forEach( function(subCollectionDocContainer) {
501+
arrSubCollection.push(subCollectionDocContainer.doc._id)
502+
resourcesViewkeys = [subCollectionDocContainer.doc._id]
503+
// To-Do: First push all subcollections in arrSubCollection and then iterate on each to get resources from them
504+
resourcesDb.view('bell', 'listCollection',{keys: resourcesViewkeys, include_docs: true}, function(err, resp){
505+
if (err) {
506+
console.log("dbInteractions.js:: fetchResourcesPointingToThisCollectionAndItsSubCollections:: error fetching docs of db: " + 'resources'); console.log(err);
507+
callback(err);
508+
} else {
509+
var resourceIdAndTitle;
510+
console.log("fetched resources count for collection: " + subCollectionDocContainer.doc.CollectionName + " (" + subCollectionDocContainer.doc._id + "): " + resp.rows.length);
511+
resp.rows.forEach( function(resourceDocContainer) {
512+
resourceIdAndTitle = {id: resourceDocContainer.doc._id, name: resourceDocContainer.doc.title};
513+
console.log("resourceId: " + resourceIdAndTitle.id);
514+
arrResources.push(resourceIdAndTitle);
515+
});
516+
callback(null, arrResources);
517+
}
518+
});
519+
});
520+
521+
}
522+
});
523+
};
524+
466525
// this method has not been used yet. it was just copied from another file to check if things were working or not in the beginning
467526
functions.writeToFile = function(fileNamePostfix, data, successMsg, callback) {
468527
var filename = "./" + "design_docs" + "/" + "design_doc_" + fileNamePostfix + ".txt";

views/starter_data.html

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
<head>
33
<title>Installer Data Generator</title>
44

5-
65
<!-- Starter Data Builder CS Files -->
76
<link rel="stylesheet" href="css/style.css">
87
<link rel="stylesheet" href="css/jquery-ui-dialog.css">
@@ -19,7 +18,6 @@
1918
<script src="js/jquery.spin.js"></script>
2019
<script src="js/jquery-ui.min.js"></script>
2120

22-
2321
<!-- Foundation JS Files -->
2422
<script src="js/foundation.min.js"></script>
2523
<script src="js/modernizr.js"></script>
@@ -41,7 +39,7 @@
4139
<div class="row">
4240
<div class="large-5 large-offset-5 columns">
4341
<select id="selectCouchSource" name="selectCouchSource">
44-
<% var sourceCouchOptions = ['Options', 'http://somaliabell.ole.org:5987', 'http://127.0.0.1:5984']
42+
<% var sourceCouchOptions = ['Options', 'http://openbell.ole.org:5984', 'http://somaliabell.ole.org:5987', 'http://127.0.0.1:5984']
4543
for(var i = 0; i < sourceCouchOptions.length; i++) { %>
4644
<option value="<%= sourceCouchOptions[i]%>"><%= sourceCouchOptions[i]%></option>
4745
<% } %>
@@ -99,7 +97,6 @@ <h3 id="selectResourcesHead"></h3>
9997
</td>
10098
</tr>
10199
<br><br>
102-
<!--add another row in table which will prvide collection selection interface-->
103100
<tr>
104101
<td>
105102
<div id="collectionsPanel" name="collectionsPanel">
@@ -123,18 +120,14 @@ <h3 id="selectCollectionMemberResourcesHead"></h3>
123120
</tr>
124121
<tr>
125122
<td>
126-
<!-- <input class="button" type="button" id="submitPanel" name="submitPanel" value="Prepare Starter Data"> -->
127123
<div id="submitPanel" name="submitPanel"></div>
128124
</td>
129125
</tr>
130126
</table>
131127
</div>
132128
</div>
133129
</div>
134-
<!--<div id="dataSelectionPanel" name="dataSelectionPanel">-->
135-
<!--<input type="button" id="hmm" name="hmm" onclick="testing()" value="Delete and Copy Db files to dest folder">-->
136130
<div id="socketRespFromServer"></div>
137-
<!--</div>-->
138131
<script src="js/script_starter_data.js"></script>
139132
</body>
140133

0 commit comments

Comments
 (0)