Skip to content

Commit

Permalink
Export a CSV file inside a zipfile + folder (#14).
Browse files Browse the repository at this point in the history
* public/js/index.js: Export HMIS_Data.zip expanding to subfolder and
  file `HMIS_Data/Clients.csv`, the latter now finally in CSV format.
  Remove the experimental enrollments export code, as it's not that
  useful for completing the export of Universal Data Elements (UDE).

* public/js/lib/jszip/: Add this third-party library from commit
  e3485a652 of https://github.com/Stuk/jszip, except replace upstream
  `dist/jszip.min.js` with our own made by yui-compressor.

* app/views/index.jade: Require `jszip`.
  • Loading branch information
kfogel committed Aug 5, 2015
1 parent 5a8de1e commit de55299
Show file tree
Hide file tree
Showing 123 changed files with 22,589 additions and 50 deletions.
1 change: 1 addition & 0 deletions app/views/index.jade
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ html(lang="en")
script(type='text/javascript', src='js/lib/pikaday.js')
script(type='text/javascript', src='js/lib/FileSaver.js/FileSaver.min.js')
script(type='text/javascript', src='js/lib/Blob.js/Blob.min.js')
script(type='text/javascript', src='js/lib/jszip/dist/jszip.min.js')
script(type='text/javascript', src='js/lib/sampleData.js')
script(type='text/javascript', src='js/papaparse.js')
script(type='text/javascript', src='js/index.js')
Expand Down
119 changes: 69 additions & 50 deletions public/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,6 @@ $(function() {
}

function exportAll() {
console.log("DEBUG: calling exportAll()");

/* For now we implement the downloadable-file functionality
* entirely on the browser side, even though in the long-term
* doing it in the intermediary node server is probably right.
Expand Down Expand Up @@ -435,22 +433,11 @@ $(function() {
*/

// Export all clients.
console.log("DEBUG: exporting clients");
$.ajax("/clients", {
method: "GET",
dataType: "json"
}).done(function(clients) {
console.log("DEBUG: fetched clients for export: " + JSON.stringify(clients));
var num_clients = clients.length;
console.log(" num clients: " + num_clients);
for (var i = 0; i < num_clients; i++) {
console.log(" client: " + JSON.stringify(clients[i]));
};
// Create a downloadable for clients. Note it's still
// JSON, not CSV, and the JSON is still pretty opaque --
// it's just "[object Object]" over and over. We'll
// unpack it when we're really creating CSV, of course.

// UDE columns to export to CSV (in progress):
//
// 3.13 PersonalID (3.13.1)
Expand All @@ -477,47 +464,79 @@ $(function() {
// no Veteran Status data.
// 4.41 Veteran Information

var clients_downloadable = new Blob(clients, {type: "text/plain;charset=utf-8"});
// We use the HUD 2014 standard name for this file,
// http://www.hudhdx.info/Resources/Vendors/4_0/HMISCSVSpecifications4_0FINAL.pdf
// Pages 17(bottom)-19
//
// NOTE: For some reason, this saveAs() can blank your
// console log in the Firefox Inspect Element window. If
// you comment out the saveAs(), you'll see all the
// preceding console.log() output again.
saveAs(clients_downloadable, "Client.csv");
});
console.log("DEBUG: done exporting clients");

// Export all enrollments.
console.log("DEBUG: exporting enrollments");
$.ajax("/enrollments", {
method: "GET",
dataType: "json"
}).done(function(enrollments) {
console.log("DEBUG: fetched enrollments for export: " + JSON.stringify(enrollments));
var num_enrollments = enrollments.length;
console.log(" num enrollments: " + num_enrollments);
for (var i = 0; i < num_enrollments; i++) {
console.log(" enrollment: " + JSON.stringify(enrollments[i]));
// Initialize the CSV with a header row.
var clients_csv =
'"OrganizationID",' +
'"PersonalIdentificationNumber",' +
'"LegalFirstName",' +
'"LegalMiddleName",' +
'"LegalLastName",' +
'"LegalSuffix",' +
'"SocialSecurityNumber",' +
'"SocialSecNumberQualityCode",' +
'"DateOfBirth",' +
'"DateOfBirthQualityCode",' +
'"PrimaryRace",' +
'"SecondaryRace",' +
'"Ethnicity",' +
'"Gender",' +
'"DateAdded",' +
'"DateUpdated",' +
'"UpdateOrDelete",' +
'"IdentityVerification",' +
'"ReleaseOfInformation",' +
'"ExportIDStr"\n';
for (var i = 0; i < clients.length; i++) {
c = clients[i];
// Assemble the row. Note our dates come out as
// YYYY-MM-DD, which is correct according to
// http://www.hudhdx.info/Resources/Vendors/4_0/HMISCSVSpecifications4_0FINAL.pdf
// page 9 top, even though some existing HMIS software
// exports (and presumably imports) M/D/YYYY.
var this_row = ""
+ "99" + ',' // OrganizationID
+ (c.personalId ? c.personalId : "") + ',' // PersonalIdentificationNumber
+ '"' + (c.firstName ? c.firstName : "") + '",' // LegalFirstName
+ '"' + (c.middleName ? c.middleName : "") + '",' // LegalMiddleName
+ '"' + (c.lastName ? c.lastName : "") + '",' // LegalLastName
+ '"' + (c.nameSuffix ? c.nameSuffix : "") + '",' // LegalSuffix
+ '"' + (c.ssn ? c.ssn : "") + '",' // SocialSecurityNumber
+ '"' + (c.ssnDataQuality ? c.ssnDataQuality : "") + '",' // SocialSecNumberQualityCode
+ '"' + (c.dob ? c.dob : "") + '",' // DateOfBirth
+ '"' + (c.dobDataQuality ? c.dobDataQuality : "") + '",' // DateOfBirthQualityCode
+ '"' + "" + '",' // PrimaryRace
+ '"' + "" + '",' // SecondaryRace
+ '"' + (c.ethnicity ? c.ethnicity : "") + '",' // Ethnicity
+ '"' + (c.gender ? c.gender : "") + '",' // Gender
+ '"' + (c.dateCreated ? c.dateCreated : "") + '",' // DateAdded
+ '"' + (c.dateUpdated ? c.dateUpdated : "") + '",' // DateUpdated
+ '"' + "" + '",' // UpdateOrDelete
+ "" + ',' // IdentityVerification
+ "" + ',' // ReleaseOfInformation
+ "1729" + ''; // ExportIDStr
clients_csv += this_row + "\n";
// Some other fields, from a JSON represntation of a
// client, that we may need to properly construct the
// PrimaryRace and SecondaryRace CSV fields.
//
// "amIndAKNative":0,
// "asian":0,
// "blackAfAmerican":0,
// "nativeHIOtherPacific":0,
// "white":1,
// "raceNone":8
};
// Create a downloadable for enrollments. Note it's still
// JSON, not CSV, and the JSON is still pretty opaque --
// it's just "[object Object]" over and over. We'll
// unpack it when we're really creating CSV, of course.
var enrollments_downloadable = new Blob(enrollments, {type: "text/plain;charset=utf-8"});

// Build and export the zipfile.
var zipper = new JSZip();
var folder = zipper.folder("HMIS_Data");
// We use the HUD 2014 standard name for this file,
// http://www.hudhdx.info/Resources/Vendors/4_0/HMISCSVSpecifications4_0FINAL.pdf
// Pages 19(bottom)-23(top)
//
// NOTE: For some reason, this saveAs() can blank your
// console log in the Firefox Inspect Element window. If
// you comment out the saveAs(), you'll see all the
// preceding console.log() output again.
saveAs(enrollments_downloadable, "Enrollment.csv");
// Pages 17(bottom)-19
folder.file("Client.csv", clients_csv);
var zipfile = zipper.generate({type:"blob"});
saveAs(zipfile, "HMIS_Data.zip");
});
console.log("DEBUG: done exporting enrollments");
}

function importAll() {
Expand Down
4 changes: 4 additions & 0 deletions public/js/lib/jszip/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*~
node_modules
sauce_connect.log
.c9revisions
2 changes: 2 additions & 0 deletions public/js/lib/jszip/.jshintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
test
12 changes: 12 additions & 0 deletions public/js/lib/jszip/.jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"undef": true,
"strict": true,
"sub": true,

"globals": {
"TextEncoder": false,
"TextDecoder": false
},
"browser": true,
"node": true
}
8 changes: 8 additions & 0 deletions public/js/lib/jszip/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
_config.yml
bower.json
component.json
docs
documentation
Gruntfile.js
index.html
test
11 changes: 11 additions & 0 deletions public/js/lib/jszip/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: node_js
node_js:
- '0.10'
script: npm run $COMMAND
env:
matrix:
- COMMAND=test-node
- COMMAND=test-browser
global:
- secure: MhA8GHU42X3GWTUMaqdZVvarx4BMjhQCUGNi3kvuD/iCmKVb7gMwj4jbds7AcJdsCRsRk8bBGzZs/E7HidBJMPDa5DhgLKy9EV1s42JlHq8lVzbJeWIGgrtyJvhVUkGRy2OJjnDSgh3U6elkQmvDn74jreSQc6m/yGoPFF1nqq8=
- secure: qREw6aUu2DnB+2reMuHgygSkumRiJvt7Z5Fz4uEVoraqbe65e4PGhtzypr9uIgCN43vxS2D5tAIeDbfid5VQrWFUQnrC9O5Z5qgVPsKN94zZ1tvYurXI4wRlAg58nNjkfGXWhLI3VUjjDTp5gYcMqgfe5hpEFYUPnUQkKGnaqAk=
69 changes: 69 additions & 0 deletions public/js/lib/jszip/CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
title: Changelog
layout: default
section: main
---

### v2.5.0 2015-03-10
- add support for custom mime-types (see [#199](https://github.com/Stuk/jszip/issues/199)).
- add an option to set the DEFLATE level (see [#201](https://github.com/Stuk/jszip/issues/201)).
- improve the error message with corrupted zip (see [#202](https://github.com/Stuk/jszip/issues/202)).
- add support for UNIX / DOS permissions (see [#200](https://github.com/Stuk/jszip/issues/200) and [#205](https://github.com/Stuk/jszip/issues/205)).

### v2.4.0 2014-07-24
- update pako to 0.2.5 (see [#156](https://github.com/Stuk/jszip/issues/156)).
- make JSZip work in a Firefox addon context (see [#151](https://github.com/Stuk/jszip/issues/151)).
- add an option (`createFolders`) to control the subfolder generation (see [#154](https://github.com/Stuk/jszip/issues/154)).
- allow `Buffer` polyfill in the browser (see [#139](https://github.com/Stuk/jszip/issues/139)).

### v2.3.0 2014-06-18
- don't generate subfolders (see [#130](https://github.com/Stuk/jszip/issues/130)).
- add comment support (see [#134](https://github.com/Stuk/jszip/issues/134)).
- on `ZipObject#options`, the attributes `date` and `dir` have been deprecated and are now on `ZipObject` (see [the upgrade guide](http://stuk.github.io/jszip/documentation/upgrade_guide.html)).
- on `ZipObject#options`, the attributes `base64` and `binary` have been deprecated (see [the upgrade guide](http://stuk.github.io/jszip/documentation/upgrade_guide.html)).
- deprecate internal functions exposed in the public API (see [#123](https://github.com/Stuk/jszip/issues/123)).
- improve UTF-8 support (see [#142](https://github.com/Stuk/jszip/issues/142)).

### v2.2.2, 2014-05-01
- update pako to v0.2.1, fix an error when decompressing some files (see [#126](https://github.com/Stuk/jszip/issues/126)).

### v2.2.1, 2014-04-23
- fix unreadable generated file on Windows 8 (see [#112](https://github.com/Stuk/jszip/issues/112)).
- replace zlibjs with pako.

### v2.2.0, 2014-02-25
- make the `new` operator optional before the `JSZip` constructor (see [#93](https://github.com/Stuk/jszip/pull/93)).
- update zlibjs to v0.2.0.

### v2.1.1, 2014-02-13
- use the npm package for zlib.js instead of the github url.

### v2.1.0, 2014-02-06
- split the files and use Browserify to generate the final file (see [#74](https://github.com/Stuk/jszip/pull/74))
- packaging change : instead of 4 files (jszip.js, jszip-load.js, jszip-inflate.js, jszip-deflate.js) we now have 2 files : dist/jszip.js and dist/jszip.min.js
- add component/bower support
- rename variable: 'byte' is a reserved word (see [#76](https://github.com/Stuk/jszip/pull/76))
- add support for the unicode path extra field (see [#82](https://github.com/Stuk/jszip/pull/82))
- ensure that the generated files have a header with the licenses (see [#80](https://github.com/Stuk/jszip/pull/80))

# v2.0.0, 2013-10-20

- `JSZipBase64` has been renamed to `JSZip.base64`.
- The `data` attribute on the object returned by `zip.file(name)` has been removed. Use `asText()`, `asBinary()`, `asUint8Array()`, `asArrayBuffer()` or `asNodeBuffer()`.

- [Fix issue with Android browser](https://github.com/Stuk/jszip/pull/60)

- The compression/decompression methods now give their input type with the `compressInputType` and `uncompressInputType` attributes.
- Lazily decompress data when needed and [improve performance in general](https://github.com/Stuk/jszip/pull/56)
- [Add support for `Buffer` in Node.js](https://github.com/Stuk/jszip/pull/57).
- Package for CommonJS/npm.

### v1.0.1, 2013-03-04

- Fixed an issue when generating a compressed zip file with empty files or folders, see #33.
- With bad data (null or undefined), asText/asBinary/asUint8Array/asArrayBuffer methods now return an empty string, see #36.

# v1.0.0, 2013-02-14

- First release after a long period without version.

Loading

0 comments on commit de55299

Please sign in to comment.