QueryManagerGeoHelper.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2017
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. /**
  8. * A Utility class that processes Location Data from Query Results data rows. Currently there are
  9. * mainly two types of locations information, regions and points, corresponding to the regions
  10. * and points related slots on tiledmap widget.
  11. *
  12. * */
  13. define(['underscore', '../../../../lib/@waca/dashboard-common/dist/query/FacetDataObject'], function (_, FacetData) {
  14. 'use strict';
  15. /**
  16. * @private
  17. */
  18. var _instance = null;
  19. /**
  20. *@constructor
  21. */
  22. var Helper = function Helper() {};
  23. //return the column indexes for each types of location slots IDs
  24. Helper.prototype._getGeoDataColumnIndexes = function (fields) {
  25. if (!fields) {
  26. return;
  27. }
  28. var result = {};
  29. var aLocationsSlotIDs = ['featureId', 'featureIdRefinement', 'pointId', 'pointIdRefinement'];
  30. var _fLocationFieldIndex = function _fLocationFieldIndex(slotId, index, result) {
  31. if (aLocationsSlotIDs.indexOf(slotId) >= 0) {
  32. result[slotId] = index;
  33. }
  34. };
  35. _.each(fields, function (field, index) {
  36. _fLocationFieldIndex(field.id, index, result);
  37. });
  38. return result;
  39. };
  40. /**
  41. * @param aDataRows {Array} data rows returned from query resutls
  42. * @param fields {Array} fields inside the query spec
  43. *
  44. * @return a JSON object that has location feature IDs and its refinement information.
  45. * The sample response below shows Bejing, Shanghai are lowest level Feature IDs, while
  46. * it's refinement location feature is Thailand.
  47. *
  48. * {
  49. "regions": [
  50. {
  51. "name": "Thailand",
  52. "children": ["Bangkok"]
  53. }, {
  54. "name": "China",
  55. "children": [
  56. "Beijing", "Shanghai"
  57. ]
  58. }, {
  59. "name": "Japan",
  60. "children": ["Tokyo"]
  61. }
  62. ],
  63. "points": [
  64. {
  65. "name": "Alberta",
  66. "children": ["Calgary"]
  67. }, {
  68. "name": "Ontario",
  69. "children": [
  70. "Ottawa", "Toronto"
  71. ]
  72. }, {
  73. "name": "Quebec",
  74. "children": ["Montreal"]
  75. }
  76. ]
  77. }
  78. * */
  79. Helper.prototype.getGeoRequestPayload = function (aDataRows, fields) {
  80. var oGeoColumnIndexes = this._getGeoDataColumnIndexes(fields);
  81. if (oGeoColumnIndexes.featureId === -1 && oGeoColumnIndexes.pointId === -1) {
  82. return [];
  83. }
  84. var aPointLocations = [];
  85. var aRegionLocations = [];
  86. for (var i = 0; i < aDataRows.length; i++) {
  87. this._addToLocationArray([oGeoColumnIndexes.featureId, oGeoColumnIndexes.featureIdRefinement], aDataRows, i, aRegionLocations);
  88. this._addToLocationArray([oGeoColumnIndexes.pointId, oGeoColumnIndexes.pointIdRefinement], aDataRows, i, aPointLocations);
  89. }
  90. return aRegionLocations;
  91. };
  92. Helper.prototype._addToLocationArray = function (aLocationLevelIndexes, aDataRows, rowIndex, aLocations) {
  93. //TODO: When RAVE2 slots has parental refinement reference, we will pass in the regions
  94. //and points location IDs array automatically
  95. var childLocationIndex = aLocationLevelIndexes[0];
  96. var parentLocationIndex = aLocationLevelIndexes[1];
  97. var sChildLocationId = new FacetData(aDataRows[rowIndex][childLocationIndex]).displayValue;
  98. if (parentLocationIndex) {
  99. var sChildLocationIdRefinement = new FacetData(aDataRows[rowIndex][parentLocationIndex]).displayValue;
  100. if (sChildLocationIdRefinement && sChildLocationIdRefinement.length > 0) {
  101. var oDataItem = this.findDataItem(aLocations, sChildLocationIdRefinement);
  102. if (!oDataItem) {
  103. oDataItem = { 'name': sChildLocationIdRefinement, 'children': [] };
  104. aLocations.push(oDataItem);
  105. }
  106. if (sChildLocationId && !_.contains(oDataItem.children, sChildLocationId)) {
  107. oDataItem.children.push({ name: sChildLocationId });
  108. }
  109. } else {
  110. if (sChildLocationId && sChildLocationId.length > 0) {
  111. aLocations.push({ name: sChildLocationId });
  112. }
  113. }
  114. } else if (sChildLocationId && sChildLocationId.length > 0) {
  115. aLocations.push({ name: sChildLocationId });
  116. }
  117. };
  118. Helper.prototype.findDataItem = function (aLocations, sFeatureId) {
  119. return _.find(aLocations, function (location) {
  120. return location.name === sFeatureId;
  121. });
  122. };
  123. var _singleton = {
  124. getInstance: function getInstance() {
  125. if (!_instance) {
  126. _instance = new Helper();
  127. }
  128. return _instance;
  129. }
  130. };
  131. return _singleton.getInstance();
  132. });
  133. //# sourceMappingURL=QueryManagerGeoHelper.js.map