1 var express = require('express');
2 var router = express.Router();
3 var exec = require('child_process').exec;
4 var util = require('util');
5 var fs = require('fs');
6 var dbRoutes = require('./dbRoutes');
7 var csp = require('./csp');
8 var multer = require('multer');
9 var bodyParser = require('body-parser');
10 var sax = require('sax'),strict=true,parser = sax.parser(strict);
11 var async = require('async');
12 var l_ = require('lodash');
15 // used for file upload button, retain original file name
16 //router.use(bodyParser());
17 router.use(bodyParser.urlencoded({
21 //var upload = multer({ dest: process.cwd() + '/uploads/', rename: function(fieldname,filename){ return filename; } });
24 var storage = multer.diskStorage({
25 destination: function (req, file, cb) {
26 cb(null, process.cwd() + '/uploads/')
28 filename: function (req, file, cb) {
29 cb(null, file.originalname )
38 //router.use(express.json());
39 //router.use(express.urlencoded());
40 //router.use(multer({ dest: './uploads/' }));
43 var selectNetworkProfile = "SELECT network_type,technology FROM NETWORK_PROFILE ORDER BY network_type";
45 var selectNbVlanRange = "SELECT vlan_plan_id,plan_type,purpose,LPAD(range_start,4,0) range_start,LPAD(range_end,4,0) range_end,generated from VLAN_RANGES ORDER BY vlan_plan_id";
47 var selectNbVlanPool = "SELECT aic_site_id,availability_zone,vlan_plan_id,plan_type,purpose,LPAD(vlan_id,4,0) vlan_id,status FROM VLAN_POOL ORDER BY aic_site_id,availability_zone,vlan_plan_id,vlan_id";
49 router.get('/getNetworkProfile', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
50 dbRoutes.getTable(req,res,selectNetworkProfile,'gamma/networkProfile',{code:'', msg:''}, req.session.loggedInAdmin);
52 router.get('/getNbVlanRange', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
53 dbRoutes.getTable(req,res,selectNbVlanRange,'gamma/nbVlanRange',{code:'', msg:''}, req.session.loggedInAdmin);
57 router.get('/deleteSite', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
59 var privilegeObj = req.session.loggedInAdmin;
61 tasks.push(function(callback) {
62 dbRoutes.deleteSite(req,res,callback);
64 async.series(tasks, function(err,result){
65 var msgArray = new Array();
68 dbRoutes.getAicSite(req,res,{code:'failure', msg:msgArray},privilegeObj);
72 msgArray.push('Row successfully deleted from AIC_SITE table.');
73 dbRoutes.getAicSite(req,res,{code:'success', msg:msgArray},privilegeObj);
80 router.get('/deleteSwitch', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
82 var privilegeObj = req.session.loggedInAdmin;
84 tasks.push(function(callback) {
85 dbRoutes.deleteSwitch(req,res,callback);
87 async.series(tasks, function(err,result){
88 var msgArray = new Array();
91 dbRoutes.getAicSwitch(req,res,{code:'failure', msg:msgArray},privilegeObj);
95 msgArray.push('Row successfully deleted from AIC_SWITCH table.');
96 dbRoutes.getAicSwitch(req,res,{code:'success', msg:msgArray},privilegeObj);
102 // DELETE AIC_AVAIL_ZONE_POOL
103 router.get('/deleteZone', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
105 var privilegeObj = req.session.loggedInAdmin;
107 tasks.push(function(callback) {
108 dbRoutes.deleteZone(req,res,callback);
110 async.series(tasks, function(err,result){
111 var msgArray = new Array();
114 dbRoutes.getAicAvailZone(req,res,{code:'failure', msg:msgArray},privilegeObj);
118 msgArray.push('Row successfully deleted from AIC_AVAIL_ZONE_POOL table.');
119 dbRoutes.getAicAvailZone(req,res,{code:'success', msg:msgArray},privilegeObj);
125 // DELETE VLAN_ID_POOL
126 router.get('/deleteVlanPool', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
128 var privilegeObj = req.session.loggedInAdmin;
130 tasks.push(function(callback) {
131 dbRoutes.deleteVlanPool(req,res,callback);
133 async.series(tasks, function(err,result){
134 var msgArray = new Array();
137 dbRoutes.getVlanPool(req,res,{code:'failure', msg:msgArray},privilegeObj);
141 msgArray.push('Row successfully deleted from VLAN_ID_POOL table.');
142 dbRoutes.getVlanPool(req,res,{code:'success', msg:msgArray},privilegeObj);
149 router.get('/deleteVpePool', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
151 var privilegeObj = req.session.loggedInAdmin;
153 tasks.push(function(callback) {
154 dbRoutes.deleteVpePool(req,res,callback);
156 async.series(tasks, function(err,result){
157 var msgArray = new Array();
160 dbRoutes.getVpePool(req,res,{code:'failure', msg:msgArray},privilegeObj);
164 msgArray.push('Row successfully deleted from VPE_POOL table.');
165 dbRoutes.getVpePool(req,res,{code:'success', msg:msgArray},privilegeObj);
173 router.get('/deleteVplspePool', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
175 var privilegeObj = req.session.loggedInAdmin;
177 tasks.push(function(callback) {
178 dbRoutes.deleteVplspePool(req,res,callback);
180 async.series(tasks, function(err,result){
181 var msgArray = new Array();
184 dbRoutes.getVplspePool(req,res,{code:'failure', msg:msgArray},privilegeObj);
188 msgArray.push('Row successfully deleted from VPLSPE_POOL table.');
189 dbRoutes.getVplspePool(req,res,{code:'success', msg:msgArray},privilegeObj);
196 router.post('/addServiceHoming', csp.checkAuth, dbRoutes.checkDB, function(req,res){
198 var privilegeObj = req.session.loggedInAdmin;
200 tasks.push( function(callback) { dbRoutes.addWebServiceHoming(req,res,callback); } );
201 async.series(tasks, function(err,result){
202 var msgArray = new Array();
205 dbRoutes.getServiceHoming(req,res,{code:'failure', msg:msgArray},privilegeObj);
209 msgArray.push('Successfully added SERVICE_HOMING');
210 dbRoutes.getServiceHoming(req,res,{code:'success', msg:msgArray},privilegeObj);
217 // gamma - updateProvStatus
218 router.post('/updateProvStatus', csp.checkAuth, dbRoutes.checkDB, function(req,res){
221 var privilegeObj = req.session.loggedInAdmin;
223 tasks.push( function(callback) { dbRoutes.updateProvStatus(req,res,callback); } );
224 async.series(tasks, function(err,result){
225 var msgArray = new Array();
228 dbRoutes.getVpePool(req,res,{code:'failure', msg:msgArray},privilegeObj);
232 msgArray.push('Successfully updated Provisioning Status');
233 dbRoutes.getVpePool(req,res,{code:'success', msg:msgArray},privilegeObj);
239 // gamma - updateAicSite
240 router.post('/updateAicSite', csp.checkAuth, dbRoutes.checkDB, function(req,res){
242 var privilegeObj = req.session.loggedInAdmin;
244 tasks.push( function(callback) { dbRoutes.updateAicSite(req,res,callback); } );
245 async.series(tasks, function(err,result){
246 var msgArray = new Array();
249 dbRoutes.getAicSite(req,res,{code:'failure', msg:msgArray},privilegeObj);
253 msgArray.push('Successfully updated AIC_SITE table.');
254 dbRoutes.getAicSite(req,res,{code:'success', msg:msgArray},privilegeObj);
260 // gamma - updateAicSwitch
261 router.post('/updateAicSwitch', csp.checkAuth, dbRoutes.checkDB, function(req,res){
263 var privilegeObj = req.session.loggedInAdmin;
265 tasks.push( function(callback) { dbRoutes.updateAicSwitch(req,res,callback); } );
266 async.series(tasks, function(err,result){
267 var msgArray = new Array();
270 dbRoutes.getAicSwitch(req,res,{code:'failure', msg:msgArray},privilegeObj);
274 msgArray.push('Successfully updated AIC_SWITCH table.');
275 dbRoutes.getAicSwitch(req,res,{code:'success', msg:msgArray},privilegeObj);
281 // gamma - updateAicAvailZone
282 router.post('/updateAicAvailZone', csp.checkAuth, dbRoutes.checkDB, function(req,res){
283 var privilegeObj = req.session.loggedInAdmin;
285 tasks.push( function(callback) { dbRoutes.updateAicAvailZone(req,res,callback); } );
286 async.series(tasks, function(err,result){
287 var msgArray = new Array();
290 dbRoutes.getAicAvailZone(req,res,{code:'failure', msg:msgArray},privilegeObj);
294 msgArray.push('Successfully updated AIC_AVAIL_ZONE_POOL table.');
295 dbRoutes.getAicAvailZone(req,res,{code:'success', msg:msgArray},privilegeObj);
301 // gamma - updateVlanPool
302 router.post('/updateVlanPool', csp.checkAuth, dbRoutes.checkDB, function(req,res){
304 var privilegeObj = req.session.loggedInAdmin;
306 tasks.push( function(callback) { dbRoutes.updateVlanPool(req,res,callback); } );
307 async.series(tasks, function(err,result){
308 var msgArray = new Array();
311 dbRoutes.getVlanPool(req,res,{code:'failure', msg:msgArray},privilegeObj);
315 msgArray.push('Successfully updated VLAN_ID_POOL table.');
316 dbRoutes.getVlanPool(req,res,{code:'success', msg:msgArray},privilegeObj);
322 // gamma - updateVpePool
323 router.post('/updateVpePool', csp.checkAuth, dbRoutes.checkDB, function(req,res){
324 var privilegeObj = req.session.loggedInAdmin;
326 tasks.push( function(callback) { dbRoutes.updateVpePool(req,res,callback); } );
327 async.series(tasks, function(err,result){
328 var msgArray = new Array();
331 dbRoutes.getVpePool(req,res,{code:'failure', msg:msgArray},privilegeObj);
335 msgArray.push('Successfully updated VPE_POOL table.');
336 dbRoutes.getVpePool(req,res,{code:'success', msg:msgArray},privilegeObj);
342 // gamma - updateVplspePool
343 router.post('/updateVplspePool', csp.checkAuth, dbRoutes.checkDB, function(req,res){
345 var privilegeObj = req.session.loggedInAdmin;
347 tasks.push( function(callback) { dbRoutes.updateVplspePool(req,res,callback); } );
348 async.series(tasks, function(err,result){
349 var msgArray = new Array();
352 dbRoutes.getVplspePool(req,res,{code:'failure', msg:msgArray},privilegeObj);
356 msgArray.push('Successfully updated VPLSPE_POOL table.');
357 dbRoutes.getVplspePool(req,res,{code:'success', msg:msgArray},privilegeObj);
364 // gamma - updateServiceHoming
365 router.post('/updateServiceHoming', csp.checkAuth, dbRoutes.checkDB, function(req,res){
367 var privilegeObj = req.session.loggedInAdmin;
369 tasks.push( function(callback) { dbRoutes.updateServiceHoming(req,res,callback); } );
370 async.series(tasks, function(err,result){
371 var msgArray = new Array();
374 dbRoutes.getServiceHoming(req,res,{code:'failure', msg:msgArray},privilegeObj);
378 msgArray.push('Successfully updated SERVICE_HOMING table.');
379 dbRoutes.getServiceHoming(req,res,{code:'success', msg:msgArray},privilegeObj);
386 router.post('/uploadVLAN', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res, next){
388 var msgArray = new Array();
389 var privilegeObj = req.session.loggedInAdmin;
391 if(req.file.originalname){
392 if (req.file.originalname.size == 0) {
393 dbRoutes.getVlanPool(req,res,{code:'danger', msg:'There was an error uploading the file, please try again.'},privilegeObj);
396 fs.exists(req.file.path, function(exists) {
399 var str = req.file.originalname;
401 // check for valid filename format
402 var tagpos = str.search("_ctag_pool");
403 var csvpos = str.search(".csv");
404 if(tagpos != 13 || csvpos != 23){
405 var msgArray = new Array();
406 msgArray.push('Not a valid filename, format must be pp_YYYYMMDDHH_ctag_pool.csv');
407 dbRoutes.getVlanPool(req,res,{code:'failure', msg:msgArray},privilegeObj);
412 var csv = require('csv');
414 // the job of the parser is to convert a CSV file
415 // to a list of rows (array of rows)
416 var parser = csv.parse({
417 columns: function(line) {
418 // By defining this callback, we get handed the
419 // first line of the spreadsheet. Which we'll
420 // ignore and effectively skip this line from processing
422 skip_empty_lines: true
427 var transformer = csv.transform(function(data){
428 // this will get row by row data, so for example,
429 //logger.debug(data[0]+','+data[1]+','+data[2]);
430 f[row] = new Array();
431 for ( col=0; col<data.length; col++ )
433 f[row][col] = data[col];
439 // called when done with processing the CSV
440 transformer.on("finish", function() {
442 var funcArray = new Array();
444 function createFunction(lrow,res)
446 return function(callback) { dbRoutes.addVLAN(lrow,res,callback); }
449 // loop for each row and create an array of callbacks for async.parallelLimit
450 // had to create a function above 'createFunction' to get
451 for (var x=0; x<f.length; x++)
453 funcArray.push( createFunction(f[x],res) );
456 // make db calls in parrallel
457 //async.parallelLimit(funcArray, 5, function(err,result){
458 async.series(funcArray, function(err,result){
461 dbRoutes.getVlanPool(req,res, result,privilegeObj);
465 // result array has an entry in it, success entries are blank, figure out
466 // how many are not blank, aka errors.
468 for(var i=0;i<result.length;i++){
469 if ( result[i].length > 0 )
475 var rowsProcessed = f.length - rowError;
476 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
479 result = {code:'failure', msg:result};
483 result = {code:'success', msg:result};
485 dbRoutes.getVlanPool(req,res,result,privilegeObj);
492 var stream = fs.createReadStream(req.file.path, "utf8");
493 stream.pipe(parser).pipe(transformer);
497 console.error('error:'+ex);
499 msgArray.push('There was an error uploading the file. '+ex);
500 dbRoutes.getVlanPool(req,res, {code:'danger', msg:msgArray}, privilegeObj);
506 msgArray.push('There was an error uploading the file.');
507 dbRoutes.getVlanPool(req,res, {code:'danger', msg:msgArray}, privilegeObj);
514 msgArray.push('There was an error uploading the file.');
515 dbRoutes.getVlanPool(req,res, {code:'danger', msg:msgArray}, privilegeObj);
523 router.post('/uploadAicSite', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res){
525 var msgArray = new Array();
526 var privilegeObj = req.session.loggedInAdmin;
528 if(req.file.originalname){
529 if (req.file.originalname.size == 0) {
530 dbRoutes.getAicSite(req,res, {code:'danger', msg:'There was an error uploading the file, please try again.'}, privilegeObj);
533 fs.exists(req.file.path, function(exists) {
536 var str = req.file.originalname;
538 // check for valid filename format
539 var tagpos = str.search("_site");
540 var csvpos = str.search(".csv");
541 if(tagpos != 13 || csvpos != 18){
543 msgArray.push('Not a valid filename, format must be pp_YYYYMMDDHH_site.csv');
544 dbRoutes.getAicSite(req,res,{code:'failure', msg:msgArray},privilegeObj);
549 var csv = require('csv');
551 // the job of the parser is to convert a CSV file
552 // to a list of rows (array of rows)
553 var parser = csv.parse({
554 columns: function(line) {
555 // By defining this callback, we get handed the
556 // first line of the spreadsheet. Which we'll
557 // ignore and effectively skip this line from processing
559 skip_empty_lines: true
564 var transformer = csv.transform(function(data){
565 // this will get row by row data, so for example,
566 //logger.debug(data[0]+','+data[1]+','+data[2]);
568 // build an array of rows
569 f[row] = new Array();
570 for ( col=0; col<data.length; col++ )
572 f[row][col] = data[col];
577 // called when done with processing the CSV
578 transformer.on("finish", function() {
580 var funcArray = new Array();
582 function createFunction(lrow,res)
584 return function(callback) { dbRoutes.addAicSite(lrow,res,callback); }
587 // loop for each row and create an array of callbacks for async.parallelLimit
588 // had to create a function above 'createFunction' to get
589 for (var x=0; x<f.length; x++)
591 funcArray.push( createFunction(f[x],res) );
594 // make db calls in parrallel
595 async.parallelLimit(funcArray, 50, function(err,result){
598 dbRoutes.getAicSite(req,res, result,privilegeObj);
602 // result array has an entry in it, success entries are blank, figure out
603 // how many are not blank, aka errors.
605 for(var i=0;i<result.length;i++){
606 if ( result[i].length > 0 )
612 var rowsProcessed = f.length - rowError;
613 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
616 result = {code:'failure', msg:result};
620 result = {code:'success', msg:result};
622 dbRoutes.getAicSite(req,res,result,privilegeObj);
628 var stream = fs.createReadStream(req.file.path, "utf8");
629 stream.pipe(parser).pipe(transformer);
634 msgArray.push('There was an error uploading the file. '+ex);
635 dbRoutes.getAicSite(req,res,{code:'danger', msg:msgArray},privilegeObj);
641 msgArray.push('There was an error uploading the file.');
642 dbRoutes.getAicSite(req,res,{code:'danger', msg:msgArray},privilegeObj);
649 msgArray.push('There was an error uploading the file.');
650 dbRoutes.getAicSite(req,res,{code:'danger', msg:msgArray},privilegeObj);
656 router.post('/uploadAicSwitch', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res){
658 var msgArray = new Array();
659 var privilegeObj = req.session.loggedInAdmin;
661 if(req.file.originalname){
662 if (req.file.originalname.size == 0) {
663 dbRoutes.getAicSwitch(req,res,{code:'danger', msg:'There was an error uploading the file, please try again.'},privilegeObj);
666 fs.exists(req.file.path, function(exists) {
670 var str = req.file.orignalname;
672 // check for valid filename format
673 var tagpos = str.search("_switch");
674 var csvpos = str.search(".csv");
675 if(tagpos != 13 || csvpos != 20){
677 msgArray.push('Not a valid filename, format must be pp_YYYYMMDDHH_switch.csv');
678 dbRoutes.getAicSwitch(req,res,{code:'failure', msg:msgArray},privilegeObj);
683 var csv = require('csv');
685 // the job of the parser is to convert a CSV file
686 // to a list of rows (array of rows)
687 var parser = csv.parse({
688 columns: function(line) {
689 // By defining this callback, we get handed the
690 // first line of the spreadsheet. Which we'll
691 // ignore and effectively skip this line from processing
693 skip_empty_lines: true
698 var transformer = csv.transform(function(data){
699 // this will get row by row data, so for example,
700 //logger.debug(data[0]+','+data[1]+','+data[2]);
702 // build an array of rows
703 f[row] = new Array();
704 for ( col=0; col<data.length; col++ )
706 f[row][col] = data[col];
711 // called when done with processing the CSV
712 transformer.on("finish", function() {
714 var funcArray = new Array();
716 function createFunction(lrow,res)
718 return function(callback) { dbRoutes.addAicSwitch(lrow,res,callback); }
721 // loop for each row and create an array of callbacks for async.parallelLimit
722 // had to create a function above 'createFunction' to get
723 for (var x=0; x<f.length; x++)
725 funcArray.push( createFunction(f[x],res) );
728 // make db calls in parrallel
729 async.parallelLimit(funcArray, 50, function(err,result){
732 dbRoutes.getAicSwitch(req,res,result,privilegeObj);
736 // result array has an entry in it, success entries are blank, figure out
737 // how many are not blank, aka errors.
739 for(var i=0;i<result.length;i++){
740 if ( result[i].length > 0 )
746 var rowsProcessed = f.length - rowError;
747 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
750 result = {code:'failure', msg:result};
754 result = {code:'success', msg:result};
756 dbRoutes.getAicSwitch(req,res,result,privilegeObj);
762 var stream = fs.createReadStream(req.file.path, "utf8");
763 stream.pipe(parser).pipe(transformer);
768 msgArray.push('There was an error uploading the file. '+ex);
769 dbRoutes.getAicSwitch(req,res,{code:'danger', msg:msgArray},privilegeObj);
775 msgArray.push('There was an error uploading the file.');
776 dbRoutes.getAicSwitch(req,res,{code:'danger', msg:msgArray},privilegeObj);
783 msgArray.push('There was an error uploading the file.');
784 dbRoutes.getAicSwitch(req,res,{code:'danger', msg:msgArray},privilegeObj);
791 router.post('/uploadAicAvailZone', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res){
793 var msgArray = new Array();
794 var privilegeObj = req.session.loggedInAdmin;
796 if(req.file.originalname){
797 if (req.file.originalname.size == 0) {
798 dbRoutes.getAicAvailZone(req,res,{code:'failure', msg:'There was an error uploading the file, please try again.'},privilegeObj);
801 fs.exists(req.file.path, function(exists) {
805 var str = req.file.originalname;
807 // check for valid filename format
808 var tagpos = str.search("_availabilityzone");
809 var csvpos = str.search(".csv");
810 if(tagpos != 13 || csvpos != 30){
812 msgArray.push('Not a valid filename, format must be pp_YYYYMMDDHH_availabilityzone.csv');
813 dbRoutes.getAicAvailZone(req,res,{code:'failure', msg:msgArray},privilegeObj);
818 var csv = require('csv');
820 // the job of the parser is to convert a CSV file
821 // to a list of rows (array of rows)
822 var parser = csv.parse({
823 columns: function(line) {
824 // By defining this callback, we get handed the
825 // first line of the spreadsheet. Which we'll
826 // ignore and effectively skip this line from processing
828 skip_empty_lines: true
833 var transformer = csv.transform(function(data){
834 // this will get row by row data, so for example,
835 //logger.debug(data[0]+','+data[1]+','+data[2]);
837 // build an array of rows
838 f[row] = new Array();
839 for ( col=0; col<data.length; col++ )
841 f[row][col] = data[col];
846 // called when done with processing the CSV
847 transformer.on("finish", function() {
849 var funcArray = new Array();
851 function createFunction(lrow,res)
853 return function(callback) { dbRoutes.addAicAvailZone(lrow,res,callback); }
856 // loop for each row and create an array of callbacks for async.parallelLimit
857 // had to create a function above 'createFunction' to get
858 for (var x=0; x<f.length; x++)
860 funcArray.push( createFunction(f[x],res) );
863 // make db calls in parrallel
864 async.parallelLimit(funcArray, 50, function(err,result){
867 dbRoutes.getAicAvailZone(req,res,result,privilegeObj);
871 // result array has an entry in it, success entries are blank, figure out
872 // how many are not blank, aka errors.
874 for(var i=0;i<result.length;i++){
875 if ( result[i].length > 0 )
881 var rowsProcessed = f.length - rowError;
882 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
885 result = {code:'failure', msg:result};
889 result = {code:'success', msg:result};
891 dbRoutes.getAicAvailZone(req,res,result,privilegeObj);
897 var stream = fs.createReadStream(req.file.path, "utf8");
898 stream.pipe(parser).pipe(transformer);
903 msgArray.push('There was an error uploading the file. '+ex);
904 dbRoutes.getAicAvailZone(req,res,{code:'danger', msg:msgArray},privilegeObj);
910 msgArray.push('There was an error uploading the file.');
911 dbRoutes.getAicAvailZone(req,res,{code:'danger', msg:msgArray},privilegeObj);
918 msgArray.push('There was an error uploading the file.');
919 dbRoutes.getAicAvailZone(req,res,{code:'danger', msg:msgArray},privilegeObj);
926 router.post('/uploadVpePool', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res){
928 var msgArray = new Array();
929 var privilegeObj = req.session.loggedInAdmin;
931 if(req.file.originalname){
932 if (req.file.originalname.size == 0) {
933 dbRoutes.getVpePool(req,res,{code:'failure', msg:'There was an error uploading the file, please try again.'},privilegeObj);
936 fs.exists(req.file.path, function(exists) {
940 var str = req.file.originalname;
942 // check for valid filename format
943 var tagpos = str.search("_vpe");
944 var csvpos = str.search(".csv");
945 if(tagpos != 13 || csvpos != 17){
947 msgArray.push('Not a valid filename, format must be pp_YYYYMMDDHH_vpe.csv');
948 var resultObj = {code:'failure', msg:msgArray};
949 dbRoutes.getVpePool(req,res,resultObj,privilegeObj);
954 var csv = require('csv');
956 // the job of the parser is to convert a CSV file
957 // to a list of rows (array of rows)
958 var parser = csv.parse({
959 columns: function(line) {
960 // By defining this callback, we get handed the
961 // first line of the spreadsheet. Which we'll
962 // ignore and effectively skip this line from processing
964 skip_empty_lines: true
969 var transformer = csv.transform(function(data){
970 // this will get row by row data, so for example,
971 //logger.debug(data[0]+','+data[1]+','+data[2]);
973 // build an array of rows
974 f[row] = new Array();
975 for ( col=0; col<data.length; col++ )
977 f[row][col] = data[col];
982 // called when done with processing the CSV
983 transformer.on("finish", function() {
985 var funcArray = new Array();
987 function createFunction(lrow,res)
989 return function(callback) { dbRoutes.addVpePool(lrow,res,callback); }
992 // loop for each row and create an array of callbacks for async.parallelLimit
993 // had to create a function above 'createFunction' to get
994 for (var x=0; x<f.length; x++)
996 funcArray.push( createFunction(f[x],res) );
999 // make db calls in parrallel
1000 async.parallelLimit(funcArray, 50, function(err,result){
1003 dbRoutes.getVpePool(req,res,result,privilegeObj);
1007 // result array has an entry in it, success entries are blank, figure out
1008 // how many are not blank, aka errors.
1010 for(var i=0;i<result.length;i++){
1011 if ( result[i].length > 0 )
1017 var rowsProcessed = f.length - rowError;
1018 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
1021 result = {code:'failure', msg:result};
1025 result = {code:'success', msg:result};
1027 dbRoutes.getVpePool(req,res,result,privilegeObj);
1033 var stream = fs.createReadStream(req.file.path, "utf8");
1034 stream.pipe(parser).pipe(transformer);
1038 msgArray.length = 0;
1039 msgArray.push('There was an error uploading the file. '+ex);
1040 dbRoutes.getVpePool(req,res,{code:'danger', msg:msgArray},privilegeObj);
1045 msgArray.length = 0;
1046 msgArray.push('There was an error uploading the file.');
1047 dbRoutes.getVpePool(req,res,{code:'danger', msg:msgArray},privilegeObj);
1053 msgArray.length = 0;
1054 msgArray.push('There was an error uploading the file.');
1055 dbRoutes.getVpePool(req,res,{code:'danger', msg:msgArray},privilegeObj);
1062 router.post('/uploadVplspePool', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res){
1064 var msgArray = new Array();
1065 var privilegeObj = req.session.loggedInAdmin;
1067 if(req.file.originalname){
1068 if (req.file.originalname.size == 0) {
1069 dbRoutes.getVplspePool(req,res,{code:'failure', msg:'There was an error uploading the file, please try again.'},privilegeObj);
1072 fs.exists(req.file.path, function(exists) {
1076 var str = req.file.originalname;
1078 // check for valid filename format
1079 var tagpos = str.search("_vpls");
1080 var csvpos = str.search(".csv");
1081 if(tagpos != 13 || csvpos != 18){
1082 msgArray.length = 0;
1083 msgArray.push('Not a valid filename, format must be pp_YYYYMMDDHH_vpls.csv');
1084 dbRoutes.getVplspePool(req,res,{code:'failure', msg:msgArray},privilegeObj);
1089 var csv = require('csv');
1091 // the job of the parser is to convert a CSV file
1092 // to a list of rows (array of rows)
1093 var parser = csv.parse({
1094 columns: function(line) {
1095 // By defining this callback, we get handed the
1096 // first line of the spreadsheet. Which we'll
1097 // ignore and effectively skip this line from processing
1099 skip_empty_lines: true
1103 var f = new Array();
1104 var transformer = csv.transform(function(data){
1105 // this will get row by row data, so for example,
1106 //logger.debug(data[0]+','+data[1]+','+data[2]);
1108 // build an array of rows
1109 f[row] = new Array();
1110 for ( col=0; col<data.length; col++ )
1112 f[row][col] = data[col];
1117 // called when done with processing the CSV
1118 transformer.on("finish", function() {
1120 var funcArray = new Array();
1122 function createFunction(lrow,res)
1124 return function(callback) { dbRoutes.addVplspePool(lrow,res,callback); }
1126 // loop for each row and create an array of callbacks for async.parallelLimit
1127 // had to create a function above 'createFunction' to get
1128 for (var x=0; x<f.length; x++)
1130 funcArray.push( createFunction(f[x],res) );
1133 // make db calls in parrallel
1134 async.parallelLimit(funcArray, 50, function(err,result){
1137 dbRoutes.getVplspePool(req,res,result,privilegeObj);
1141 // result array has an entry in it, success entries are blank, figure out
1142 // how many are not blank, aka errors.
1144 for(var i=0;i<result.length;i++){
1145 if ( result[i].length > 0 )
1150 var rowsProcessed = f.length - rowError;
1151 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
1154 result = {code:'failure', msg:result};
1158 result = {code:'success', msg:result};
1160 dbRoutes.getVplspePool(req,res,result,privilegeObj);
1166 var stream = fs.createReadStream(req.file.path, "utf8");
1167 stream.pipe(parser).pipe(transformer);
1171 msgArray.length = 0;
1172 msgArray.push('There was an error uploading the file. '+ex);
1173 dbRoutes.getVplspePool(req,res,{code:'danger', msg:msgArray},privilegeObj);
1178 msgArray.length = 0;
1179 msgArray.push('There was an error uploading the file.');
1180 dbRoutes.getVplspePool(req,res,{code:'danger', msg:msgArray},privilegeObj);
1186 msgArray.length = 0;
1187 msgArray.push('There was an error uploading the file.');
1188 dbRoutes.getVplspePool(req,res,{code:'danger', msg:msgArray},privilegeObj);
1195 router.post('/uploadServiceHoming', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res)
1197 var msgArray = new Array();
1198 var privilegeObj = req.session.loggedInAdmin;
1200 if(req.file.originalname)
1202 if (req.file.originalname.size == 0) {
1203 dbRoutes.getServiceHoming(req,res,{code:'failure', msg:'There was an error uploading the file, please try again.'},privilegeObj);
1206 fs.exists(req.file.path, function(exists)
1210 var str = req.file.originalname;
1212 // check for valid filename format
1213 var csvpos = str.search(".csv");
1214 if( (l_.startsWith(str,'aichoming_') != true) || csvpos != 18)
1216 msgArray.length = 0;
1217 msgArray.push('Not a valid filename, format must be aichoming_mmddYYYY.csv');
1218 //msgArray.push('Not a valid filename, format must be pp_YYYYMMDDHH_vpls.csv');
1219 dbRoutes.getServiceHoming(req,res,{code:'failure', msg:msgArray},privilegeObj);
1225 var csv = require('csv');
1227 // the job of the parser is to convert a CSV file
1228 // to a list of rows (array of rows)
1229 var parser = csv.parse({
1230 columns: function(line) {
1231 // By defining this callback, we get handed the
1232 // first line of the spreadsheet. Which we'll
1233 // ignore and effectively skip this line from processing
1235 skip_empty_lines: true
1239 var f = new Array();
1240 var csvrows = new Array();
1241 var transformer = csv.transform(function(data){
1242 // this will get row by row data, so for example,
1243 //logger.debug(data[0]+','+data[1]+','+data[2]);
1245 // build an array of rows
1246 f[row] = new Array();
1247 for ( col=0; col<data.length; col++ )
1249 f[row][col] = data[col];
1254 // called when done with processing the CSV
1255 transformer.on("finish", function()
1257 var funcArray = new Array();
1259 function createFunction(lrow,res)
1261 return function(callback) { dbRoutes.addServiceHoming(lrow,req,res,callback); }
1263 funcArray.push(function(callback) {
1264 dbRoutes.saveServiceHoming(req,res,callback);
1266 // loop for each row and create an array of callbacks for async.parallelLimit
1267 // had to create a function above 'createFunction' to get
1268 for (var x=0; x<f.length; x++)
1270 funcArray.push( createFunction(f[x],res) );
1273 // make db calls in series
1274 async.series(funcArray, function(err,result)
1278 result = {code:'failure', msg:result};
1279 dbRoutes.getServiceHoming(req,res,result,privilegeObj);
1283 { // result array has an entry in it, success entries are blank, figure out
1284 // how many are not blank, aka errors.
1286 for(var i=0;i<result.length;i++)
1288 if ( result[i].length > 0 )
1293 var rowsProcessed = f.length - rowError;
1294 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
1297 result = {code:'failure', msg:result};
1301 result = {code:'success', msg:result};
1303 dbRoutes.getServiceHoming(req,res,result,privilegeObj);
1309 var stream = fs.createReadStream(req.file.path, "utf8");
1310 stream.pipe(parser).pipe(transformer);
1314 msgArray.length = 0;
1315 msgArray.push('There was an error uploading the file. '+ex);
1316 dbRoutes.getServiceHoming(req,res,{code:'danger', msg:msgArray},privilegeObj);
1321 msgArray.length = 0;
1322 msgArray.push('There was an error uploading the file.');
1323 dbRoutes.getServiceHoming(req,res,{code:'danger', msg:msgArray},privilegeObj);
1330 msgArray.length = 0;
1331 msgArray.push('There was an error uploading the file.');
1332 dbRoutes.getServiceHoming(req,res,{code:'danger', msg:msgArray},privilegeObj);
1337 function removeNL(s) {
1339 ** Remove NewLine, CarriageReturn and Tab characters from a String
1340 ** s string to be processed
1341 ** returns new string
1344 for (i=0; i < s.length; i++)
1346 if (s.charAt(i) != '\n' &&
1347 s.charAt(i) != '\r' &&
1348 s.charAt(i) != '\t')
1355 function padLeft(nr, n, str){
1356 return Array(n-String(nr).length+1).join(str||'0')+nr;
1360 module.exports = router;