1 | var Ack = require('./Ack'),
|
2 | HeadSegment = require('./HeadSegment'),
|
3 | Message,
|
4 | Segment = require('./Segment'),
|
5 | queryparser = require('../definitions/query'),
|
6 | _ = require('underscore'),
|
7 | __slice = [].slice,
|
8 | moment = require('moment');
|
9 |
|
10 | function getParts(message, query) {
|
11 | var selectors = [].concat(query.selectors),
|
12 | selector = selectors.shift(),
|
13 | parts = message.getParts(selector),
|
14 | val;
|
15 |
|
16 | while (!_.isEmpty(selectors)) {
|
17 | selector = selectors.shift();
|
18 | parts = _.map(parts, function(part) {
|
19 | if (part) {
|
20 | return part.getPart(selector);
|
21 | } else {
|
22 | return null;
|
23 | }
|
24 | });
|
25 | }
|
26 |
|
27 | return parts;
|
28 | }
|
29 |
|
30 | function getValues(message, query) {
|
31 | var parts = getParts(message, query);
|
32 |
|
33 | return _.map(parts, function(part) {
|
34 | if (part) {
|
35 | val = part.val();
|
36 |
|
37 | return query.toDate ? toDate(val) : val;
|
38 | } else {
|
39 | return null;
|
40 | }
|
41 | });
|
42 | }
|
43 |
|
44 | function getValue(message, query) {
|
45 | var val = _.first(getValues(message, query));
|
46 |
|
47 | return val === undefined ? null : val;
|
48 | }
|
49 |
|
50 | function toDate(val) {
|
51 | var date;
|
52 |
|
53 | if (/^\d{8,14}$/.test(val)) {
|
54 | date = moment(val, 'YYYYMMDDHHmmss');
|
55 | if (_.isNaN(date.valueOf())) {
|
56 | return null;
|
57 | } else {
|
58 | return date.toDate();
|
59 | }
|
60 | } else {
|
61 | return null;
|
62 | }
|
63 | }
|
64 |
|
65 | Message = (function() {
|
66 | function Message(raw, control) {
|
67 | var msh, segments;
|
68 |
|
69 | msh = raw[0], segments = 2 <= raw.length ? __slice.call(raw, 1) : [];
|
70 | this.errors = [];
|
71 | this.segments = [];
|
72 | this.segments.push(new HeadSegment(msh, control));
|
73 | _.each(segments, function(segment) {
|
74 | return this.segments.push(new Segment(segment, control));
|
75 | }, this);
|
76 | this.validate();
|
77 | return;
|
78 | }
|
79 |
|
80 | Message.prototype.getParts = function(name) {
|
81 | return _.filter(this.segments, function(segment) {
|
82 | return segment.name === name;
|
83 | });
|
84 | };
|
85 |
|
86 | Message.prototype.getPart = function(name) {
|
87 | return _.first(this.getParts(name));
|
88 | };
|
89 |
|
90 | Message.prototype.validate = function() {
|
91 | var _ref;
|
92 |
|
93 | if (((_ref = this.segments[0]) != null ? _ref.name : void 0) !== 'MSH') {
|
94 | this.errors.push('Missing MSH segment');
|
95 | }
|
96 | return this.valid = this.errors.length === 0;
|
97 | };
|
98 |
|
99 | Message.prototype.query = function(q) {
|
100 | var query;
|
101 |
|
102 | try {
|
103 | query = queryparser.parse(q);
|
104 |
|
105 | if (query.repeat) {
|
106 | return getValues(this, query);
|
107 | } else {
|
108 | return getValue(this, query);
|
109 | }
|
110 | } catch (e) {
|
111 | throw new Error("Bad selector '" + q + "': " + e.message);
|
112 | }
|
113 | };
|
114 |
|
115 | Message.prototype.reject = function(substitutions) {
|
116 | return this.respond('AR', substitutions);
|
117 | };
|
118 |
|
119 | Message.prototype.error = function(substitutions) {
|
120 | return this.respond('AE', substitutions);
|
121 | };
|
122 |
|
123 | Message.prototype.acknowledge = function(substitutions) {
|
124 | return this.respond('AA', substitutions);
|
125 | };
|
126 |
|
127 | Message.prototype.respond = function(code, substitutions) {
|
128 | return new Ack({
|
129 | message: this,
|
130 | code: code,
|
131 | subs: substitutions
|
132 | }).toString();
|
133 | };
|
134 |
|
135 | Message.prototype.translate = function(map, override) {
|
136 | var result;
|
137 |
|
138 | result = _.reduce(map, function(memo, val, key) {
|
139 | memo[key] = this.query(val);
|
140 | return memo;
|
141 | }, {}, this);
|
142 | return _.extend(result, override);
|
143 | };
|
144 |
|
145 | Message.prototype.toString = function() {
|
146 | return _.map(this.segments, function(segment) {
|
147 | return segment.toString();
|
148 | }).join('\r');
|
149 | };
|
150 |
|
151 | Message.prototype.replace = function(q) {
|
152 | var query = queryparser.parse(q),
|
153 | parts = getParts(this, query),
|
154 | replacements = _.toArray(arguments).slice(1);
|
155 |
|
156 | _.each(parts, function(part) {
|
157 | if (part) {
|
158 | return part.val(replacements);
|
159 | } else {
|
160 | return null;
|
161 | }
|
162 | });
|
163 | };
|
164 |
|
165 | Message.prototype.remove = function() {
|
166 | var names;
|
167 |
|
168 | names = _.map(_.toArray(arguments), function(name) {
|
169 | return new RegExp('^' + name.replace(/\*$/, '.+') + '$');
|
170 | });
|
171 |
|
172 | this.segments = _.reject(this.segments, function(segment) {
|
173 | return _.any(names, function(name) {
|
174 | return name.test(segment.name);
|
175 | });
|
176 | });
|
177 | };
|
178 |
|
179 | return Message;
|
180 |
|
181 | })();
|
182 |
|
183 | module.exports = Message;
|