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