jpc_ex.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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(false);
  89. }
  90. if (rptTpl[JV.NODE_FLOW_INFO_EX]) {
  91. me.flowTabEx = JpcFlowTab.createNew();
  92. me.flowTabEx.initialize(true);
  93. }
  94. if (rptTpl[JV.NODE_BILL_INFO]) {
  95. me.billTab = JpcBillTab.createNew();
  96. me.billTab.initialize();
  97. }
  98. //let dt1 = new Date();
  99. if (rptTpl[JV.NODE_CROSS_INFO]) {
  100. me.crossTab = JpcCrossTab.createNew();
  101. me.crossTab.initialize();
  102. }
  103. me.totalPages = 0;
  104. me.exTotalPages = 0;
  105. me.runTimePageData = {};
  106. me.fields = JpcField.createNew(rptTpl);
  107. me.params = JpcParam.createNew(rptTpl);
  108. me.formulas = JpcFunc.createNew(rptTpl);
  109. };
  110. JpcResult.analyzeData = function(rptTpl, dataObj, defProperties) {
  111. let me = this;
  112. //1. data object
  113. let dataHelper = JpcData.createNew();
  114. dataHelper.analyzeData(rptTpl, dataObj);
  115. //2. tab object
  116. //pre-condition: the data should be sorted in SQL/NoSQL level!
  117. //let dt1 = new Date();
  118. if (me.flowTab) {
  119. me.flowTab.sorting(rptTpl, dataObj, dataHelper.dataSeq.slice(0));
  120. if (me.flowTabEx) {
  121. me.flowTabEx.sorting(rptTpl, dataObj, dataHelper.exDataSeq.slice(0));
  122. }
  123. }
  124. if (me.crossTab) {
  125. me.crossTab.sorting(rptTpl, dataObj, dataHelper.dataSeq.slice(0));
  126. }
  127. //let dt2 = new Date();
  128. //alert(dt2 - dt1);
  129. //3. formulas
  130. me.executeFormulas(JV.RUN_TYPE_BEFORE_PAGING, rptTpl, dataObj, me);
  131. //4. paging
  132. me.paging(rptTpl, dataObj, defProperties);
  133. //alert('analyzeData was completed!');
  134. //for garbage collection:
  135. dataHelper = null;
  136. };
  137. JpcResult.paging = function(rptTpl, dataObj, defProperties) {
  138. let me = this;
  139. if (me.flowTab) {
  140. me.totalPages = me.flowTab.preSetupPages(rptTpl, dataObj, defProperties);
  141. if (me.flowTabEx) {
  142. me.exTotalPages = me.flowTabEx.preSetupPages(rptTpl, dataObj, defProperties);
  143. //console.log('ad-hoc flow pages: ' + me.exTotalPages);
  144. }
  145. me.totalPages += me.exTotalPages;
  146. } else if (me.crossTab) {
  147. me.totalPages = me.crossTab.preSetupPages(rptTpl, defProperties);
  148. } else if (me.billTab) {
  149. me.totalPages = me.billTab.paging();
  150. }
  151. };
  152. JpcResult.executeFormulas = function(runType, $CURRENT_TEMPLATE, $CURRENT_DATA, $CURRENT_RPT) {
  153. let me = this;
  154. for (let i = 0; i < me.formulas.length; i++) {
  155. if (me.formulas[i][JV.PROP_RUN_TYPE] === runType) {
  156. let expression = me.formulas[i][JV.PROP_EXPRESSION];
  157. if (expression) {
  158. let $ME = me.formulas[i];
  159. eval(expression);
  160. }
  161. }
  162. }
  163. };
  164. JpcResult.outputAsSimpleJSONPageArray = function(rptTpl, dataObj, startPage, endPage, defProperties) {
  165. let me = this, rst = {};
  166. if ((startPage > 0) && (startPage <= endPage) && (endPage <= me.totalPages)) {
  167. rst[JV.NODE_CONTROL_COLLECTION] = private_buildDftControls(rptTpl, (defProperties == null)?null:defProperties.ctrls);
  168. rst[JV.NODE_STYLE_COLLECTION] = private_buildDftStyles(rptTpl, (defProperties == null)?null:defProperties.styles);
  169. rst[JV.NODE_FONT_COLLECTION] = private_buildDftFonts(rptTpl, (defProperties == null)?null:defProperties.fonts);
  170. rst[JV.NODE_PAGE_INFO] = {};
  171. rst[JV.NODE_PAGE_INFO][JV.NODE_MAIN_INFO_RPT_NAME] = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MAIN_INFO_RPT_NAME];
  172. rst[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE] = JpcCommonHelper.getPageSize(rptTpl);
  173. rst[JV.NODE_PAGE_INFO][JV.NODE_MARGINS] = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS];
  174. rst.items = [];
  175. let bands = JpcBand.createNew(rptTpl, defProperties);
  176. try {
  177. for (let page = startPage; page <= endPage; page++) {
  178. me.runTimePageData.currentPage = page;
  179. me.executeFormulas(JV.RUN_TYPE_BEFORE_OUTPUT, rptTpl, dataObj, me);
  180. rst.items.push(me.outputAsSimpleJSONPage(rptTpl, dataObj, bands, page, rst[JV.NODE_CONTROL_COLLECTION]));
  181. }
  182. if (bands[JV.BAND_PROP_MERGE_BAND]) {
  183. let mergedBand = {}, band = bands[JV.BAND_PROP_MERGE_BAND];
  184. mergedBand[JV.PROP_LEFT] = parseInt(band[JV.PROP_LEFT].toFixed(0));
  185. mergedBand[JV.PROP_RIGHT] = parseInt(band[JV.PROP_RIGHT].toFixed(0));
  186. mergedBand[JV.PROP_TOP] = parseInt(band[JV.PROP_TOP].toFixed(0));
  187. mergedBand[JV.PROP_BOTTOM] = parseInt(band[JV.PROP_BOTTOM].toFixed(0));
  188. mergedBand[JV.BAND_PROP_STYLE] = band[JV.BAND_PROP_STYLE];
  189. rst[JV.BAND_PROP_MERGE_BAND] = mergedBand;
  190. }
  191. } catch(exception) {
  192. console.log(exception);
  193. } finally {
  194. bands = null;
  195. }
  196. }
  197. return rst;
  198. };
  199. JpcResult.outputAsSimpleJSONPage = function(rptTpl, dataObj, bands, page, controls) {
  200. let me = this, rst = null;
  201. if (me.totalPages >= page) {
  202. rst = {};
  203. rst[JV.PROP_PAGE_SEQ] = page;
  204. //rst.cells = [];
  205. if (me.flowTab) {
  206. if (me.totalPages - me.exTotalPages >= page) {
  207. rst.cells = me.flowTab.outputAsSimpleJSONPage(rptTpl, dataObj, page, bands, controls, me);
  208. } else {
  209. rst.cells = me.flowTabEx.outputAsSimpleJSONPage(rptTpl, dataObj, page - (me.totalPages - me.exTotalPages), bands, controls, me);
  210. }
  211. } else if (me.crossTab) {
  212. rst.cells = me.crossTab.outputAsSimpleJSONPage(rptTpl, dataObj, page, bands, controls, me);
  213. } else if (me.billTab) {
  214. //
  215. }
  216. }
  217. return rst;
  218. };
  219. //JpcEx.rte.currentRptObj = JpcResult;
  220. return JpcResult;
  221. }
  222. module.exports = new JpcExSrv();