/
report.js
96 lines (82 loc) · 3.38 KB
/
report.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var _ = require('lodash'),
Entity = require('../entity'),
logger = require('../../logger');
var ReportSchema = new Entity.SchemaObject({
ReportID: { type: String, toObject: 'always' },
ReportName: { type: String, toObject: 'always' },
ReportType: { type: String, toObject: 'always' },
ReportTitles: { type: Array, arrayType: ReportTitleSchema, toObject: 'always' },
ReportDate: { type: String, toObject: 'always' },
UpdatedDateUTC: { type: String, toObject: 'always' },
Rows: { type: Array, arrayType: ReportRowSchema, toObject: 'always' }
});
var ReportTitleSchema = new Entity.SchemaObject({
ReportTitle: { type: String, toObject: 'always' }
});
var ReportRowSchema = new Entity.SchemaObject({
RowType: { type: String, toObject: 'always' },
Title: { type: String, toObject: 'always' },
Cells: { type: Array, arrayType: ReportCellSchema, toObject: 'always' }
});
var ReportCellSchema = new Entity.SchemaObject({
Value: { type: String, toObject: 'always' },
Attributes: { type: Array, arrayType: ReportAttributeSchema, toObject: 'always' }
});
var ReportAttributeSchema = new Entity.SchemaObject({
Value: { type: String, toObject: 'always' },
Id: { type: String, toObject: 'always' }
});
var Report = Entity.extend(ReportSchema, {
constructor: function(application, data, options) {
logger.debug('Report::constructor');
this.Entity.apply(this, arguments);
},
initialize: function(data, options) {},
fromXmlObj: function(obj) {
var self = this;
Object.assign(self, _.omit(obj, 'Rows', 'Cells', 'ReportTitles'));
if (obj.ReportTitles) {
var reportTitles = this.application.asArray(obj.ReportTitles.ReportTitle);
_.each(reportTitles, function(reportTitle) {
self.ReportTitles.push(reportTitle);
})
}
if (hasMoreRows(obj)) {
//Loop through the rows to find the next level down
obj.Rows.Row.forEach(function(level1Row) {
if (hasMoreRows(level1Row)) {
var level2RowsArray = [];
level1Row.Rows.Row.forEach(function(level2row) {
level2row.Cells ? level2row.Cells = extractCells(level2row) : false;
level2RowsArray.push(level2row);
});
level1Row.Rows = level2RowsArray;
}
level1Row.Cells ? level1Row.Cells = extractCells(level1Row) : false;
self.Rows.push(level1Row);
});
} else {
//No more rows, but we should check for cells in this row
if (obj.Rows && obj.Rows.Row && obj.Rows.Row.Cells && obj.Rows.Row.Cells.Cell && obj.Rows.Row.Cells.Cell.length > 0) {
obj.Rows.Row.Cells = extractCells(obj.Rows.Row);
self.Rows.push(obj.Rows.Row);
}
}
return this;
function hasMoreRows(obj) {
return obj.Rows && obj.Rows.Row && obj.Rows.Row.length >= 0;
}
function extractCells(row) {
var cells = [];
if (row.Cells) {
row.Cells.Cell.forEach(function(cell) {
cells.push(cell);
});
row.Cells = cells;
}
return cells;
}
}
});
module.exports = Report;
module.exports.ReportSchema = ReportSchema;