Permalink
Browse files

Relation import

  • Loading branch information...
1 parent 5d9f9b0 commit dbd283181e09509cdaf8b797b1e42173cc8da4eb @safwank committed Aug 3, 2012
Showing with 112 additions and 0 deletions.
  1. +3 −0 app.js
  2. BIN data/Relation.csv
  3. +79 −0 models/relation.js
  4. +7 −0 routes/datainit.js
  5. +1 −0 routes/index.js
  6. +11 −0 routes/relations.js
  7. +11 −0 views/relations.jade
View
3 app.js
@@ -58,6 +58,7 @@ app.get('/datainit/individualtaxreturn', routes.datainit.loadIndividualTaxReturn
app.get('/datainit/stock', routes.datainit.loadStock);
app.get('/datainit/referral', routes.datainit.loadReferral);
app.get('/datainit/financialyear', routes.datainit.loadFinancialYear);
+app.get('/datainit/relation', routes.datainit.loadRelation);
app.get('/businesses', routes.businesses.list);
@@ -78,6 +79,8 @@ app.get('/referrals', routes.referrals.list);
app.get('/financialyears', routes.financialyears.list);
+app.get('/relations', routes.relations.list);
+
app.get('/upload', routes.upload.show);
app.post('/upload', routes.upload.importCSVData);
View
Binary file not shown.
View
@@ -0,0 +1,79 @@
+// Initialization
+
+var util = require('../common/util');
+var csv = require('csv');
+var neo4j = require('neo4j');
+var db = new neo4j.GraphDatabase(process.env.NEO4J_URL || 'http://localhost:7474');
+
+// Constants
+
+var INDEX_NAME = 'nodes';
+var INDEX_KEY = 'type';
+var INDEX_VAL = 'relation';
+
+// Private "constructors"
+
+var Relation = module.exports = function Relation(_node) {
+ this._node = _node;
+};
+
+// Pass-through Relation properties
+
+util.proxyProperty(Relation, 'Id', true);
+util.proxyProperty(Relation, 'Type', true);
+
+// Private functions
+
+function loadRelation(data, index) {
+ var relation = data;
+ util.removeNullOrEmptyPropertiesIn(relation);
+ Relation.create(relation, handleCreated);
+}
+
+function handleSuccess(count) {
+ console.log('Number of relations created: ' + count);
+}
+
+function handleLoadError(error) {
+ console.log(error.message);
+}
+
+function handleCreated(error, data) {
+ if (error) console.log(error.message);
+ console.log('Created: ' + data);
+}
+
+// Public functions
+
+Relation.loadFromCSV = function() {
+ csv().fromPath('data/Relation.csv', { columns: true, trim: true })
+ .on('data', loadRelation)
+ .on('end', handleSuccess)
+ .on('error', handleLoadError);
+};
+
+Relation.create = function (data, callback) {
+ var node = db.createNode(data);
+ var relation = new Relation(node);
+
+ node.save(function (err) {
+ if (err) return callback(err);
+
+ node.index(INDEX_NAME, INDEX_KEY, INDEX_VAL, function (err) {
+ if (err) return callback(err);
+ callback(null, relation);
+ });
+ });
+};
+
+Relation.getAll = function (callback) {
+ db.getIndexedNodes(INDEX_NAME, INDEX_KEY, INDEX_VAL, function (err, nodes) {
+ if (err) return callback(null, []);
+
+ var relations = nodes.map(function (node) {
+ return new Relation(node);
+ });
+
+ callback(null, relations);
+ });
+};
View
@@ -9,6 +9,7 @@ var IndividualTaxReturn = require('../models/individualtaxreturn');
var Stock = require('../models/stock');
var Referral = require('../models/referral');
var FinancialYear = require('../models/financialyear');
+var Relation = require('../models/relation');
// GET /datainit/business
exports.loadBusiness = function (req, res, next) {
@@ -74,4 +75,10 @@ exports.loadReferral = function (req, res, next) {
exports.loadFinancialYear = function (req, res, next) {
FinancialYear.loadFromCSV();
res.redirect('/financialyears');
+};
+
+// GET /datainit/relation
+exports.loadRelation = function (req, res, next) {
+ Relation.loadFromCSV();
+ res.redirect('/relations');
};
View
@@ -13,5 +13,6 @@ exports.individualtaxreturns = require('./individualtaxreturns');
exports.stocks = require('./stocks');
exports.referrals = require('./referrals');
exports.financialyears = require('./financialyears');
+exports.relations = require('./relations');
exports.upload = require('./upload');
View
@@ -0,0 +1,11 @@
+var Relation = require('../models/relation');
+
+// GET /relations
+exports.list = function (req, res, next) {
+ Relation.getAll(function (err, relations) {
+ if (err) return next(err);
+ res.render('relations', {
+ relations: relations
+ });
+ });
+};
View
@@ -0,0 +1,11 @@
+h1 Relations
+
+if relations.length
+ p Here are the current relations:
+ ul.relations
+ for relation in relations
+ li.relation
+ p #{relation.Id} #{relation.Type}
+else
+ p There are no relations currently.
+

0 comments on commit dbd2831

Please sign in to comment.