1 | /*
|
2 |
|
3 | ----------------------------------------------------------------------------
|
4 | | qewd-ripple: QEWD-based Middle Tier for Ripple OSI |
|
5 | | |
|
6 | | Copyright (c) 2016-17 Ripple Foundation Community Interest Company |
|
7 | | All rights reserved. |
|
8 | | |
|
9 | | http://rippleosi.org |
|
10 | | Email: code.custodian@rippleosi.org |
|
11 | | |
|
12 | | Author: Rob Tweed, M/Gateway Developments Ltd |
|
13 | | |
|
14 | | Licensed under the Apache License, Version 2.0 (the "License"); |
|
15 | | you may not use this file except in compliance with the License. |
|
16 | | You may obtain a copy of the License at |
|
17 | | |
|
18 | | http://www.apache.org/licenses/LICENSE-2.0 |
|
19 | | |
|
20 | | Unless required by applicable law or agreed to in writing, software |
|
21 | | distributed under the License is distributed on an "AS IS" BASIS, |
|
22 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
23 | | See the License for the specific language governing permissions and |
|
24 | | limitations under the License. |
|
25 | ----------------------------------------------------------------------------
|
26 |
|
27 | 8 March 2017
|
28 |
|
29 | */
|
30 |
|
31 | var openEHR = require('../openEHR/openEHR');
|
32 | var headingsLib = require('../headings/headings');
|
33 | var headingHelpers = require('../headings/headingHelpers');
|
34 | var dateTime = require('../dateTime');
|
35 | var transform = require('qewd-transform-json').transform;
|
36 |
|
37 | var headings = headingsLib.headings;
|
38 |
|
39 | /*
|
40 |
|
41 | This module fetches a heading. It's a 2-stage process
|
42 |
|
43 | If this is the first time a user has asked for this heading,
|
44 | the appropriate AQL query is sent to each of the OpenEHR machines, and the raw
|
45 | responses are saved into the QEWD session, keyed by host id name.
|
46 |
|
47 | The raw heading responses for each host name are saved as an array
|
48 |
|
49 | An index by host and array element number is also created in the QEWD Session
|
50 |
|
51 | Once in cache, the headings can then be retrieved for use in the browser:
|
52 |
|
53 | The heading records sent to the browser are first fetched from the QEWD Session
|
54 | and then transformed to the UI format using the heading module's get.transformTemplate
|
55 |
|
56 | */
|
57 |
|
58 | function getHeadingTable(patientId, heading, session, callback) {
|
59 | var q = this;
|
60 |
|
61 | var patient = session.data.$(['patients', patientId]);
|
62 | //var patient = new this.documentStore.DocumentNode('ripplePatients', [patientId]);
|
63 | var nhsNo = patientId;
|
64 | var openEHRId = patient.$('openEHRId').value
|
65 | if (openEHRId !== '') nhsNo = openEHRId;
|
66 |
|
67 | if (!headings[heading]) {
|
68 | console.log('*** ' + heading + ' has not yet been added to middle-tier processing');
|
69 | callback([]);
|
70 | return;
|
71 | }
|
72 |
|
73 | var patientHeading = patient.$(['headings', headings[heading].name]);
|
74 | //var patientHeading = new this.documentStore.DocumentNode('ripplePatients', [patientId, 'headings', headings[heading].name]);
|
75 | if (!patientHeading.exists) {
|
76 | // fetch it and cache it in the QEWD session
|
77 | var q = this;
|
78 | console.log('*** heading ' + heading + ' needs to be fetched!')
|
79 | openEHR.startSessions(function(openEHRSessions) {
|
80 | //console.log('*** sessions: ' + JSON.stringify(sessions));
|
81 | openEHR.mapNHSNo(nhsNo, openEHRSessions, function() {
|
82 | //console.log('*** NHS no mapped');
|
83 | headingsLib.getHeading.call(q, patientId, heading, session, openEHRSessions, function() {
|
84 | openEHR.stopSessions(openEHRSessions);
|
85 | // now try again!
|
86 | if (!patientHeading.exists) {
|
87 | console.log('*** No results could be returned from the OpenEHR servers for heading ' + heading);
|
88 | callback([]);
|
89 | return;
|
90 | }
|
91 | else {
|
92 | //console.log('**** trying again!');
|
93 | // Now the headings are in cache, re-run to retrieve and transform them
|
94 |
|
95 | getHeadingTable.call(q, patientId, heading, session, callback);
|
96 | }
|
97 | });
|
98 | });
|
99 | });
|
100 | return;
|
101 | }
|
102 |
|
103 | // The heading records are in the QEWD Session cache
|
104 | // Retrieve and transform them
|
105 |
|
106 | console.log('patientHeading exists for ' + nhsNo + ': heading ' + heading);
|
107 | var results = [];
|
108 |
|
109 | var template = headings[heading].get.transformTemplate;
|
110 |
|
111 | patientHeading.forEachChild(function(host, hostNode) {
|
112 |
|
113 | var helpers = headingHelpers(host, heading, 'get');
|
114 |
|
115 | hostNode.forEachChild(function(index, headingNode) {
|
116 | //console.log('**** forEachChild index = ' + index);
|
117 | var input = headingNode.getDocument();
|
118 | var output = transform(template, input, helpers);
|
119 |
|
120 | // only send the summary headings
|
121 |
|
122 | var summaryFields = headings[heading].headingTableFields;
|
123 | summaryFields.push('source');
|
124 | summaryFields.push('sourceId');
|
125 |
|
126 | var summary = {};
|
127 | summaryFields.forEach(function(fieldName) {
|
128 | summary[fieldName] = output[fieldName] || '';
|
129 | });
|
130 |
|
131 | results.push(summary);
|
132 | });
|
133 | });
|
134 | if (callback) callback(results);
|
135 | }
|
136 |
|
137 | module.exports = getHeadingTable;
|