jpc_ex.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. let JV = require('./jpc_value_define');
  2. let JpcBand = require('./jpc_band');
  3. let JpcFlowTab = require('./jpc_flow_tab');
  4. let JpcBillTab = require('./jpc_bill_tab');
  5. let JpcCrossTab = require('./jpc_cross_tab');
  6. let JpcField = require('./jpc_field');
  7. let JpcParam = require('./jpc_param');
  8. let JpcFunc = require('./jpc_function');
  9. let JpcData = require('./jpc_data');
  10. let JpcCommonHelper = require('./helper/jpc_helper_common');
  11. let $JE = require('./jpc_rte'); //Important: for self-define function execution purpose
  12. let JpcExSrv = function(){};
  13. JpcExSrv.prototype.createNew = function(){
  14. function private_buildDftItems(rptTpl, dftCollection, propArray, nodeName) {
  15. let rst = {};
  16. if (dftCollection) {
  17. for (let i = 0; i < dftCollection.length; i++) {
  18. let item = {};
  19. for (let j = 0; j < propArray.length; j++) {
  20. item[propArray[j]] = dftCollection[i][propArray[j]];
  21. }
  22. rst[dftCollection[i][JV.PROP_ID]] = item;
  23. }
  24. if (rptTpl && rptTpl[nodeName] && rptTpl[nodeName].length > 0) {
  25. for (let i = 0; i < rptTpl[nodeName].length; i++) {
  26. let rptDftItem = rptTpl[nodeName][i];
  27. if (rst[rptDftItem[JV.PROP_ID]] == undefined) {
  28. let item = {};
  29. for (let j = 0; j < propArray.length; j++) {
  30. item[propArray[j]] = rptDftItem[propArray[j]];
  31. }
  32. rst[rptDftItem[JV.PROP_ID]] = item;
  33. }
  34. }
  35. }
  36. }
  37. return rst;
  38. }
  39. function private_buildDftControls(rptTpl, dftControlCollection) {
  40. let rst = private_buildDftItems(rptTpl,dftControlCollection, JV.CONTROL_PROPS, JV.NODE_CONTROL_COLLECTION);
  41. return rst;
  42. }
  43. function private_buildDftFonts(rptTpl, dftFontCollection) {
  44. let rst = private_buildDftItems(rptTpl,dftFontCollection, JV.FONT_PROPS, JV.NODE_FONT_COLLECTION);
  45. return rst;
  46. }
  47. function private_buildDftStyles(rptTpl, dftStyleCollection) {
  48. let rst = {};
  49. if (dftStyleCollection) {
  50. for (let i = 0; i < dftStyleCollection.length; i++) {
  51. let item = {};
  52. if (dftStyleCollection[i][JV.PROP_BORDER_STYLE] && dftStyleCollection[i][JV.PROP_BORDER_STYLE].length > 0) {
  53. for (let j = 0; j < dftStyleCollection[i][JV.PROP_BORDER_STYLE].length; j++) {
  54. let borderItem = {};
  55. for (let k = 0; k < JV.BORDER_STYLE_PROPS.length; k++) {
  56. borderItem[JV.BORDER_STYLE_PROPS[k]] = dftStyleCollection[i][JV.PROP_BORDER_STYLE][j][JV.BORDER_STYLE_PROPS[k]];
  57. }
  58. item[dftStyleCollection[i][JV.PROP_BORDER_STYLE][j][JV.PROP_POSITION]] = borderItem;
  59. }
  60. }
  61. rst[dftStyleCollection[i][JV.PROP_ID]] = item;
  62. }
  63. if (rptTpl && rptTpl[JV.NODE_STYLE_COLLECTION] && rptTpl[JV.NODE_STYLE_COLLECTION].length > 0) {
  64. for (let i = 0; i < rptTpl[JV.NODE_STYLE_COLLECTION].length; i++) {
  65. let rptDftItem = rptTpl[JV.NODE_STYLE_COLLECTION][i];
  66. if (rst[rptDftItem[JV.PROP_ID]] == undefined) {
  67. let item = {};
  68. for (let j = 0; j < rptDftItem[JV.PROP_BORDER_STYLE].length; j++) {
  69. let borderItem = {};
  70. for (let k = 0; k < JV.BORDER_STYLE_PROPS.length; k++) {
  71. borderItem[JV.BORDER_STYLE_PROPS[k]] = rptDftItem[JV.PROP_BORDER_STYLE][j][JV.BORDER_STYLE_PROPS[k]];
  72. }
  73. item[rptDftItem[JV.PROP_BORDER_STYLE][j][JV.PROP_POSITION]] = borderItem;
  74. }
  75. rst[rptDftItem[JV.PROP_ID]] = item;
  76. }
  77. }
  78. }
  79. }
  80. return rst;
  81. }
  82. let JpcResult = {};
  83. //JpcResult.report_title
  84. JpcResult.initialize = function(rptTpl) {
  85. let me = this;
  86. if (rptTpl[JV.NODE_FLOW_INFO]) {
  87. me.flowTab = JpcFlowTab.createNew();
  88. me.flowTab.initialize();
  89. }
  90. if (rptTpl[JV.NODE_BILL_INFO]) {
  91. me.billTab = JpcBillTab.createNew();
  92. me.billTab.initialize();
  93. }
  94. //let dt1 = new Date();
  95. if (rptTpl[JV.NODE_CROSS_INFO]) {
  96. me.crossTab = JpcCrossTab.createNew();
  97. me.crossTab.initialize();
  98. }
  99. me.totalPages = 0;
  100. me.runTimePageData = {};
  101. me.fields = JpcField.createNew(rptTpl);
  102. me.params = JpcParam.createNew(rptTpl);
  103. me.formulas = JpcFunc.createNew(rptTpl);
  104. };
  105. JpcResult.analyzeData = function(rptTpl, dataObj, defProperties) {
  106. let me = this;
  107. //1. data object
  108. let dataHelper = JpcData.createNew();
  109. dataHelper.analyzeData(rptTpl, dataObj);
  110. //2. tab object
  111. //pre-condition: the data should be sorted in SQL/NoSQL level!
  112. //let dt1 = new Date();
  113. if (me.flowTab) {
  114. me.flowTab.sorting(rptTpl, dataObj, dataHelper.dataSeq.slice(0));
  115. }
  116. if (me.crossTab) {
  117. me.crossTab.sorting(rptTpl, dataObj, dataHelper.dataSeq.slice(0));
  118. }
  119. //let dt2 = new Date();
  120. //alert(dt2 - dt1);
  121. //3. formulas
  122. me.executeFormulas(JV.RUN_TYPE_BEFORE_PAGING, rptTpl, dataObj, me);
  123. //4. paging
  124. me.paging(rptTpl, dataObj, defProperties);
  125. //alert('analyzeData was completed!');
  126. //for garbage collection:
  127. dataHelper = null;
  128. };
  129. JpcResult.paging = function(rptTpl, dataObj, defProperties) {
  130. let me = this;
  131. if (me.flowTab) {
  132. me.totalPages = me.flowTab.preSetupPages(rptTpl, dataObj, defProperties);
  133. } else if (me.crossTab) {
  134. me.totalPages = me.crossTab.preSetupPages(rptTpl, defProperties);
  135. } else if (me.billTab) {
  136. //me.totalPages = billTab.paging();
  137. }
  138. };
  139. JpcResult.executeFormulas = function(runType, $CURRENT_TEMPLATE, $CURRENT_DATA, $CURRENT_RPT) {
  140. let me = this;
  141. for (let i = 0; i < me.formulas.length; i++) {
  142. if (me.formulas[i][JV.PROP_RUN_TYPE] === runType) {
  143. let expression = me.formulas[i][JV.PROP_EXPRESSION];
  144. if (expression) {
  145. let $ME = me.formulas[i];
  146. eval(expression);
  147. }
  148. }
  149. }
  150. };
  151. JpcResult.outputAsSimpleJSONPageArray = function(rptTpl, dataObj, startPage, endPage, defProperties) {
  152. let me = this, rst = {};
  153. if ((startPage > 0) && (startPage <= endPage) && (endPage <= me.totalPages)) {
  154. rst[JV.NODE_CONTROL_COLLECTION] = private_buildDftControls(rptTpl, (defProperties == null)?null:defProperties.ctrls);
  155. rst[JV.NODE_STYLE_COLLECTION] = private_buildDftStyles(rptTpl, (defProperties == null)?null:defProperties.styles);
  156. rst[JV.NODE_FONT_COLLECTION] = private_buildDftFonts(rptTpl, (defProperties == null)?null:defProperties.fonts);
  157. rst[JV.NODE_PAGE_INFO] = JpcCommonHelper.getPageSize(rptTpl);
  158. rst.items = [];
  159. let bands = JpcBand.createNew(rptTpl, defProperties);
  160. try {
  161. for (let page = startPage; page <= endPage; page++) {
  162. me.runTimePageData.currentPage = page;
  163. me.executeFormulas(JV.RUN_TYPE_BEFORE_OUTPUT, rptTpl, dataObj, me);
  164. rst.items.push(me.outputAsSimpleJSONPage(rptTpl, dataObj, bands, page, rst[JV.NODE_CONTROL_COLLECTION]));
  165. }
  166. if (bands[JV.BAND_PROP_MERGE_BAND]) {
  167. let mergedBand = {}, band = bands[JV.BAND_PROP_MERGE_BAND];
  168. mergedBand[JV.PROP_LEFT] = band[JV.PROP_LEFT].toFixed(0);
  169. mergedBand[JV.PROP_RIGHT] = band[JV.PROP_RIGHT].toFixed(0);
  170. mergedBand[JV.PROP_TOP] = band[JV.PROP_TOP].toFixed(0);
  171. mergedBand[JV.PROP_BOTTOM] = band[JV.PROP_BOTTOM].toFixed(0);
  172. mergedBand[JV.BAND_PROP_STYLE] = band[JV.BAND_PROP_STYLE];
  173. rst[JV.BAND_PROP_MERGE_BAND] = mergedBand;
  174. }
  175. } finally {
  176. bands = null;
  177. }
  178. }
  179. return rst;
  180. };
  181. JpcResult.outputAsSimpleJSONPage = function(rptTpl, dataObj, bands, page, controls) {
  182. let me = this, rst = null;
  183. if (me.totalPages >= page) {
  184. rst = {};
  185. rst[JV.PROP_PAGE_SEQ] = page;
  186. //rst.cells = [];
  187. if (me.flowTab) {
  188. rst.cells = me.flowTab.outputAsSimpleJSONPage(rptTpl, dataObj, page, bands, controls, me);
  189. } else if (me.crossTab) {
  190. rst.cells = me.crossTab.outputAsSimpleJSONPage(rptTpl, dataObj, page, bands, controls, me);
  191. } else if (me.billTab) {
  192. //
  193. }
  194. }
  195. return rst;
  196. };
  197. //JpcEx.rte.currentRptObj = JpcResult;
  198. return JpcResult;
  199. }
  200. module.exports = new JpcExSrv();