@esri/solution-common 4.1.2 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (261) hide show
  1. package/dist/cjs/completeItem.d.ts +29 -29
  2. package/dist/cjs/completeItem.js +81 -81
  3. package/dist/cjs/create-hub-request-options.d.ts +29 -29
  4. package/dist/cjs/create-hub-request-options.js +63 -63
  5. package/dist/cjs/deleteHelpers/deleteEmptyGroups.d.ts +24 -24
  6. package/dist/cjs/deleteHelpers/deleteEmptyGroups.js +41 -41
  7. package/dist/cjs/deleteHelpers/deleteGroupIfEmpty.d.ts +27 -27
  8. package/dist/cjs/deleteHelpers/deleteGroupIfEmpty.js +96 -96
  9. package/dist/cjs/deleteHelpers/deleteSolutionContents.d.ts +38 -38
  10. package/dist/cjs/deleteHelpers/deleteSolutionContents.js +129 -129
  11. package/dist/cjs/deleteHelpers/deleteSolutionFolder.d.ts +29 -29
  12. package/dist/cjs/deleteHelpers/deleteSolutionFolder.js +78 -78
  13. package/dist/cjs/deleteHelpers/deleteSolutionItem.d.ts +30 -30
  14. package/dist/cjs/deleteHelpers/deleteSolutionItem.js +53 -53
  15. package/dist/cjs/deleteHelpers/index.d.ts +22 -22
  16. package/dist/cjs/deleteHelpers/index.js +25 -25
  17. package/dist/cjs/deleteHelpers/reconstructBuildOrderIds.d.ts +27 -27
  18. package/dist/cjs/deleteHelpers/reconstructBuildOrderIds.js +33 -33
  19. package/dist/cjs/deleteHelpers/removeItems.d.ts +34 -34
  20. package/dist/cjs/deleteHelpers/removeItems.js +111 -111
  21. package/dist/cjs/deleteHelpers/reportProgress.d.ts +27 -27
  22. package/dist/cjs/deleteHelpers/reportProgress.js +45 -45
  23. package/dist/cjs/deleteSolution.d.ts +55 -55
  24. package/dist/cjs/deleteSolution.js +106 -106
  25. package/dist/cjs/dependencies.d.ts +26 -26
  26. package/dist/cjs/dependencies.js +170 -170
  27. package/dist/cjs/featureServiceHelpers.d.ts +791 -791
  28. package/dist/cjs/featureServiceHelpers.js +2420 -2420
  29. package/dist/cjs/generalHelpers.d.ts +392 -385
  30. package/dist/cjs/generalHelpers.js +857 -854
  31. package/dist/cjs/generalHelpers.js.map +1 -1
  32. package/dist/cjs/get-subscription-info.d.ts +27 -27
  33. package/dist/cjs/get-subscription-info.js +38 -38
  34. package/dist/cjs/getDeletableSolutionInfo.d.ts +29 -29
  35. package/dist/cjs/getDeletableSolutionInfo.js +52 -52
  36. package/dist/cjs/getItemTypeAbbrev.d.ts +19 -19
  37. package/dist/cjs/getItemTypeAbbrev.js +184 -184
  38. package/dist/cjs/getSolutionSummary.d.ts +27 -27
  39. package/dist/cjs/getSolutionSummary.js +100 -100
  40. package/dist/cjs/index.d.ts +43 -44
  41. package/dist/cjs/index.js +46 -47
  42. package/dist/cjs/index.js.map +1 -1
  43. package/dist/cjs/interfaces.d.ts +1334 -1334
  44. package/dist/cjs/interfaces.js +74 -74
  45. package/dist/cjs/interfaces.js.map +1 -1
  46. package/dist/cjs/libConnectors.d.ts +73 -73
  47. package/dist/cjs/libConnectors.js +114 -114
  48. package/dist/cjs/migrations/apply-schema.d.ts +24 -24
  49. package/dist/cjs/migrations/apply-schema.js +35 -35
  50. package/dist/cjs/migrations/is-legacy-solution.d.ts +24 -24
  51. package/dist/cjs/migrations/is-legacy-solution.js +39 -39
  52. package/dist/cjs/migrations/upgrade-three-dot-one.d.ts +27 -27
  53. package/dist/cjs/migrations/upgrade-three-dot-one.js +48 -48
  54. package/dist/cjs/migrations/upgrade-three-dot-zero.d.ts +27 -27
  55. package/dist/cjs/migrations/upgrade-three-dot-zero.js +42 -42
  56. package/dist/cjs/migrations/upgrade-two-dot-five.d.ts +24 -24
  57. package/dist/cjs/migrations/upgrade-two-dot-five.js +72 -72
  58. package/dist/cjs/migrations/upgrade-two-dot-four.d.ts +24 -24
  59. package/dist/cjs/migrations/upgrade-two-dot-four.js +71 -71
  60. package/dist/cjs/migrations/upgrade-two-dot-one.d.ts +7 -7
  61. package/dist/cjs/migrations/upgrade-two-dot-one.js +38 -38
  62. package/dist/cjs/migrations/upgrade-two-dot-seven.d.ts +23 -23
  63. package/dist/cjs/migrations/upgrade-two-dot-seven.js +57 -57
  64. package/dist/cjs/migrations/upgrade-two-dot-six.d.ts +27 -27
  65. package/dist/cjs/migrations/upgrade-two-dot-six.js +60 -60
  66. package/dist/cjs/migrations/upgrade-two-dot-three.d.ts +23 -23
  67. package/dist/cjs/migrations/upgrade-two-dot-three.js +54 -54
  68. package/dist/cjs/migrations/upgrade-two-dot-two.d.ts +23 -23
  69. package/dist/cjs/migrations/upgrade-two-dot-two.js +57 -57
  70. package/dist/cjs/migrations/upgrade-two-dot-zero.d.ts +44 -44
  71. package/dist/cjs/migrations/upgrade-two-dot-zero.js +94 -94
  72. package/dist/cjs/migrator.d.ts +25 -25
  73. package/dist/cjs/migrator.js +76 -76
  74. package/dist/cjs/resourceHelpers.d.ts +191 -191
  75. package/dist/cjs/resourceHelpers.js +383 -390
  76. package/dist/cjs/resourceHelpers.js.map +1 -1
  77. package/dist/cjs/resources/add-resource-from-blob.d.ts +26 -26
  78. package/dist/cjs/resources/add-resource-from-blob.js +51 -51
  79. package/dist/cjs/resources/addMetadataFromBlob.d.ts +25 -25
  80. package/dist/cjs/resources/addMetadataFromBlob.js +42 -42
  81. package/dist/cjs/resources/convert-item-resource-to-storage-resource.d.ts +32 -32
  82. package/dist/cjs/resources/convert-item-resource-to-storage-resource.js +69 -69
  83. package/dist/cjs/resources/convert-storage-resource-to-item-resource.d.ts +29 -29
  84. package/dist/cjs/resources/convert-storage-resource-to-item-resource.js +69 -69
  85. package/dist/cjs/resources/copyAssociatedFiles.d.ts +67 -67
  86. package/dist/cjs/resources/copyAssociatedFiles.js +301 -301
  87. package/dist/cjs/resources/copyDataIntoItem.d.ts +33 -33
  88. package/dist/cjs/resources/copyDataIntoItem.js +61 -62
  89. package/dist/cjs/resources/copyDataIntoItem.js.map +1 -1
  90. package/dist/cjs/resources/copyMetadataIntoItem.d.ts +26 -26
  91. package/dist/cjs/resources/copyMetadataIntoItem.js +45 -45
  92. package/dist/cjs/resources/copyResourceIntoZip.d.ts +33 -33
  93. package/dist/cjs/resources/copyResourceIntoZip.js +77 -77
  94. package/dist/cjs/resources/copyZipIntoItem.d.ts +25 -25
  95. package/dist/cjs/resources/copyZipIntoItem.js +53 -53
  96. package/dist/cjs/resources/createCopyResults.d.ts +25 -25
  97. package/dist/cjs/resources/createCopyResults.js +35 -35
  98. package/dist/cjs/resources/get-blob.d.ts +26 -26
  99. package/dist/cjs/resources/get-blob.js +26 -26
  100. package/dist/cjs/resources/getItemResourcesFilesFromPaths.d.ts +24 -24
  101. package/dist/cjs/resources/getItemResourcesFilesFromPaths.js +48 -48
  102. package/dist/cjs/resources/getItemResourcesPaths.d.ts +26 -26
  103. package/dist/cjs/resources/getItemResourcesPaths.js +72 -72
  104. package/dist/cjs/resources/index.d.ts +29 -29
  105. package/dist/cjs/resources/index.js +32 -32
  106. package/dist/cjs/resources/solution-resource.d.ts +35 -35
  107. package/dist/cjs/resources/solution-resource.js +30 -30
  108. package/dist/cjs/resources/solution-resource.js.map +1 -1
  109. package/dist/cjs/resources/transform-resource-paths-to-solution-resources.d.ts +56 -56
  110. package/dist/cjs/resources/transform-resource-paths-to-solution-resources.js +145 -145
  111. package/dist/cjs/restHelpers.d.ts +586 -585
  112. package/dist/cjs/restHelpers.js +1888 -1883
  113. package/dist/cjs/restHelpers.js.map +1 -1
  114. package/dist/cjs/restHelpersGet.d.ts +288 -288
  115. package/dist/cjs/restHelpersGet.js +803 -803
  116. package/dist/cjs/sharing/index.d.ts +16 -16
  117. package/dist/cjs/sharing/index.js +19 -19
  118. package/dist/cjs/sharing/share-item-to-groups.d.ts +26 -26
  119. package/dist/cjs/sharing/share-item-to-groups.js +43 -43
  120. package/dist/cjs/templatization.d.ts +139 -139
  121. package/dist/cjs/templatization.js +313 -313
  122. package/dist/cjs/trackingHelpers.d.ts +116 -116
  123. package/dist/cjs/trackingHelpers.js +216 -216
  124. package/dist/cjs/velocityHelpers.d.ts +57 -57
  125. package/dist/cjs/velocityHelpers.js +134 -134
  126. package/dist/cjs/workforceHelpers.d.ts +115 -115
  127. package/dist/cjs/workforceHelpers.js +746 -746
  128. package/dist/cjs/workforceHelpers.js.map +1 -1
  129. package/dist/esm/completeItem.d.ts +29 -29
  130. package/dist/esm/completeItem.js +76 -76
  131. package/dist/esm/create-hub-request-options.d.ts +29 -29
  132. package/dist/esm/create-hub-request-options.js +59 -59
  133. package/dist/esm/deleteHelpers/deleteEmptyGroups.d.ts +24 -24
  134. package/dist/esm/deleteHelpers/deleteEmptyGroups.js +37 -37
  135. package/dist/esm/deleteHelpers/deleteGroupIfEmpty.d.ts +27 -27
  136. package/dist/esm/deleteHelpers/deleteGroupIfEmpty.js +91 -91
  137. package/dist/esm/deleteHelpers/deleteSolutionContents.d.ts +38 -38
  138. package/dist/esm/deleteHelpers/deleteSolutionContents.js +124 -124
  139. package/dist/esm/deleteHelpers/deleteSolutionFolder.d.ts +29 -29
  140. package/dist/esm/deleteHelpers/deleteSolutionFolder.js +73 -73
  141. package/dist/esm/deleteHelpers/deleteSolutionItem.d.ts +30 -30
  142. package/dist/esm/deleteHelpers/deleteSolutionItem.js +48 -48
  143. package/dist/esm/deleteHelpers/index.d.ts +22 -22
  144. package/dist/esm/deleteHelpers/index.js +22 -22
  145. package/dist/esm/deleteHelpers/reconstructBuildOrderIds.d.ts +27 -27
  146. package/dist/esm/deleteHelpers/reconstructBuildOrderIds.js +28 -28
  147. package/dist/esm/deleteHelpers/removeItems.d.ts +34 -34
  148. package/dist/esm/deleteHelpers/removeItems.js +106 -106
  149. package/dist/esm/deleteHelpers/reportProgress.d.ts +27 -27
  150. package/dist/esm/deleteHelpers/reportProgress.js +41 -41
  151. package/dist/esm/deleteSolution.d.ts +55 -55
  152. package/dist/esm/deleteSolution.js +100 -100
  153. package/dist/esm/dependencies.d.ts +26 -26
  154. package/dist/esm/dependencies.js +166 -166
  155. package/dist/esm/featureServiceHelpers.d.ts +791 -791
  156. package/dist/esm/featureServiceHelpers.js +2336 -2336
  157. package/dist/esm/generalHelpers.d.ts +392 -385
  158. package/dist/esm/generalHelpers.js +810 -808
  159. package/dist/esm/generalHelpers.js.map +1 -1
  160. package/dist/esm/get-subscription-info.d.ts +27 -27
  161. package/dist/esm/get-subscription-info.js +34 -34
  162. package/dist/esm/getDeletableSolutionInfo.d.ts +29 -29
  163. package/dist/esm/getDeletableSolutionInfo.js +47 -47
  164. package/dist/esm/getItemTypeAbbrev.d.ts +19 -19
  165. package/dist/esm/getItemTypeAbbrev.js +180 -180
  166. package/dist/esm/getSolutionSummary.d.ts +27 -27
  167. package/dist/esm/getSolutionSummary.js +95 -95
  168. package/dist/esm/index.d.ts +43 -44
  169. package/dist/esm/index.js +43 -44
  170. package/dist/esm/index.js.map +1 -1
  171. package/dist/esm/interfaces.d.ts +1334 -1334
  172. package/dist/esm/interfaces.js +70 -70
  173. package/dist/esm/libConnectors.d.ts +73 -73
  174. package/dist/esm/libConnectors.js +104 -104
  175. package/dist/esm/migrations/apply-schema.d.ts +24 -24
  176. package/dist/esm/migrations/apply-schema.js +31 -31
  177. package/dist/esm/migrations/is-legacy-solution.d.ts +24 -24
  178. package/dist/esm/migrations/is-legacy-solution.js +35 -35
  179. package/dist/esm/migrations/upgrade-three-dot-one.d.ts +27 -27
  180. package/dist/esm/migrations/upgrade-three-dot-one.js +44 -44
  181. package/dist/esm/migrations/upgrade-three-dot-zero.d.ts +27 -27
  182. package/dist/esm/migrations/upgrade-three-dot-zero.js +38 -38
  183. package/dist/esm/migrations/upgrade-two-dot-five.d.ts +24 -24
  184. package/dist/esm/migrations/upgrade-two-dot-five.js +68 -68
  185. package/dist/esm/migrations/upgrade-two-dot-four.d.ts +24 -24
  186. package/dist/esm/migrations/upgrade-two-dot-four.js +67 -67
  187. package/dist/esm/migrations/upgrade-two-dot-one.d.ts +7 -7
  188. package/dist/esm/migrations/upgrade-two-dot-one.js +34 -34
  189. package/dist/esm/migrations/upgrade-two-dot-seven.d.ts +23 -23
  190. package/dist/esm/migrations/upgrade-two-dot-seven.js +53 -53
  191. package/dist/esm/migrations/upgrade-two-dot-six.d.ts +27 -27
  192. package/dist/esm/migrations/upgrade-two-dot-six.js +56 -56
  193. package/dist/esm/migrations/upgrade-two-dot-three.d.ts +23 -23
  194. package/dist/esm/migrations/upgrade-two-dot-three.js +50 -50
  195. package/dist/esm/migrations/upgrade-two-dot-two.d.ts +23 -23
  196. package/dist/esm/migrations/upgrade-two-dot-two.js +53 -53
  197. package/dist/esm/migrations/upgrade-two-dot-zero.d.ts +44 -44
  198. package/dist/esm/migrations/upgrade-two-dot-zero.js +87 -87
  199. package/dist/esm/migrator.d.ts +25 -25
  200. package/dist/esm/migrator.js +72 -72
  201. package/dist/esm/resourceHelpers.d.ts +191 -191
  202. package/dist/esm/resourceHelpers.js +364 -371
  203. package/dist/esm/resourceHelpers.js.map +1 -1
  204. package/dist/esm/resources/add-resource-from-blob.d.ts +26 -26
  205. package/dist/esm/resources/add-resource-from-blob.js +47 -47
  206. package/dist/esm/resources/addMetadataFromBlob.d.ts +25 -25
  207. package/dist/esm/resources/addMetadataFromBlob.js +38 -38
  208. package/dist/esm/resources/convert-item-resource-to-storage-resource.d.ts +32 -32
  209. package/dist/esm/resources/convert-item-resource-to-storage-resource.js +65 -65
  210. package/dist/esm/resources/convert-storage-resource-to-item-resource.d.ts +29 -29
  211. package/dist/esm/resources/convert-storage-resource-to-item-resource.js +65 -65
  212. package/dist/esm/resources/copyAssociatedFiles.d.ts +67 -67
  213. package/dist/esm/resources/copyAssociatedFiles.js +293 -293
  214. package/dist/esm/resources/copyDataIntoItem.d.ts +33 -33
  215. package/dist/esm/resources/copyDataIntoItem.js +56 -57
  216. package/dist/esm/resources/copyDataIntoItem.js.map +1 -1
  217. package/dist/esm/resources/copyMetadataIntoItem.d.ts +26 -26
  218. package/dist/esm/resources/copyMetadataIntoItem.js +41 -41
  219. package/dist/esm/resources/copyResourceIntoZip.d.ts +33 -33
  220. package/dist/esm/resources/copyResourceIntoZip.js +72 -72
  221. package/dist/esm/resources/copyZipIntoItem.d.ts +25 -25
  222. package/dist/esm/resources/copyZipIntoItem.js +49 -49
  223. package/dist/esm/resources/createCopyResults.d.ts +25 -25
  224. package/dist/esm/resources/createCopyResults.js +31 -31
  225. package/dist/esm/resources/get-blob.d.ts +26 -26
  226. package/dist/esm/resources/get-blob.js +22 -22
  227. package/dist/esm/resources/getItemResourcesFilesFromPaths.d.ts +24 -24
  228. package/dist/esm/resources/getItemResourcesFilesFromPaths.js +44 -44
  229. package/dist/esm/resources/getItemResourcesPaths.d.ts +26 -26
  230. package/dist/esm/resources/getItemResourcesPaths.js +68 -68
  231. package/dist/esm/resources/index.d.ts +29 -29
  232. package/dist/esm/resources/index.js +29 -29
  233. package/dist/esm/resources/solution-resource.d.ts +35 -35
  234. package/dist/esm/resources/solution-resource.js +27 -27
  235. package/dist/esm/resources/transform-resource-paths-to-solution-resources.d.ts +56 -56
  236. package/dist/esm/resources/transform-resource-paths-to-solution-resources.js +137 -137
  237. package/dist/esm/restHelpers.d.ts +586 -585
  238. package/dist/esm/restHelpers.js +1827 -1823
  239. package/dist/esm/restHelpers.js.map +1 -1
  240. package/dist/esm/restHelpersGet.d.ts +288 -288
  241. package/dist/esm/restHelpersGet.js +763 -763
  242. package/dist/esm/sharing/index.d.ts +16 -16
  243. package/dist/esm/sharing/index.js +16 -16
  244. package/dist/esm/sharing/share-item-to-groups.d.ts +26 -26
  245. package/dist/esm/sharing/share-item-to-groups.js +39 -39
  246. package/dist/esm/templatization.d.ts +139 -139
  247. package/dist/esm/templatization.js +293 -293
  248. package/dist/esm/trackingHelpers.d.ts +116 -116
  249. package/dist/esm/trackingHelpers.js +204 -204
  250. package/dist/esm/velocityHelpers.d.ts +57 -57
  251. package/dist/esm/velocityHelpers.js +128 -128
  252. package/dist/esm/workforceHelpers.d.ts +115 -115
  253. package/dist/esm/workforceHelpers.js +717 -717
  254. package/dist/esm/workforceHelpers.js.map +1 -1
  255. package/package.json +2 -2
  256. package/dist/cjs/polyfills.d.ts +0 -40
  257. package/dist/cjs/polyfills.js +0 -98
  258. package/dist/cjs/polyfills.js.map +0 -1
  259. package/dist/esm/polyfills.d.ts +0 -40
  260. package/dist/esm/polyfills.js +0 -93
  261. package/dist/esm/polyfills.js.map +0 -1
@@ -1,2421 +1,2421 @@
1
- "use strict";
2
- /** @license
3
- * Copyright 2019 Esri
4
- *
5
- * Licensed under the Apache License, Version 2.0 (the "License");
6
- * you may not use this file except in compliance with the License.
7
- * You may obtain a copy of the License at
8
- *
9
- * http://www.apache.org/licenses/LICENSE-2.0
10
- *
11
- * Unless required by applicable law or agreed to in writing, software
12
- * distributed under the License is distributed on an "AS IS" BASIS,
13
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- * See the License for the specific language governing permissions and
15
- * limitations under the License.
16
- */
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports._processAdminObject = exports._templatizeAdminLayerInfo = exports._templatizeLayerFieldReferences = exports._validateEditFieldsInfo = exports._validateTypesTemplates = exports._validateTemplatesFields = exports._validateIndexes = exports._validateDisplayField = exports._validateFields = exports._templatizeLayer = exports._templatizeProperty = exports._templatize = exports.updatePopupInfo = exports.postProcessFields = exports.updateLayerFieldReferences = exports.validateSpatialReferenceAndExtent = exports.setDefaultSpatialReference = exports._updateTemplateDictionaryFields = exports._updateGeomFieldName = exports._updateSourceLayerFields = exports._updateItemFields = exports._getDynamicFieldNames = exports._getFieldNames = exports._updateForPortal = exports.removeLayerOptimization = exports._isSelfReferential = exports._updateAddOptions = exports._updateOrder = exports.addFeatureServiceDefinition = exports.addFeatureServiceLayersAndTables = exports.getExistingLayersAndTables = exports.getLayersAndTables = exports.deTemplatizeFieldInfos = exports.processContingentValues = exports.updateTemplateForInvalidDesignations = exports.updateSettingsFieldInfos = exports.setNamesAndTitles = exports.getLayerSettings = exports._updateTypeKeywords = exports.updateTemplate = exports.cacheLayerInfo = exports._cachePopupInfo = exports.cachePopupInfos = exports._cacheFieldInfo = exports.cacheContingentValues = exports.cacheFieldInfos = exports.deleteViewProps = exports.templatize = exports.rest_addFeatures = exports.rest_queryFeatures = void 0;
19
- exports._getLayerChunkSize = exports._getNameMapping = exports._templatizeDefinitionQuery = exports._templatizeTimeInfo = exports._templatizeKeys = exports._templatizeTypeTemplates = exports._templatizeTemplates = exports._templatizeLabelingInfo = exports._templatizeArcadeExpressions = exports._templatizeAuthoringInfo = exports._templatizeTemporalRenderer = exports._templatizeGenRenderer = exports._templatizeRenderer = exports._templatizeDrawingInfo = exports._templatizeSimpleName = exports._templatizeDefinitionExpression = exports._templatizeDefinitionEditor = exports._templatizeMediaInfos = exports._templatizePopupElements = exports._templatizeExpressionInfos = exports._templatizeFieldName = exports._templatizePopupInfoFieldInfos = exports._templatizeName = exports._templatizePopupInfo = exports._templatizeRelationshipFields = exports._templatizeTopFilter = exports._templatizeAdminSourceLayerFields = exports._getDependantItemId = exports._templatizeAdminLayerInfoFields = exports._templatizeSourceServiceName = void 0;
20
- /**
21
- * Provides general helper functions.
22
- *
23
- * @module featureServiceHelpers
24
- */
25
- // ------------------------------------------------------------------------------------------------------------------ //
26
- var arcgis_rest_feature_layer_1 = require("@esri/arcgis-rest-feature-layer");
27
- Object.defineProperty(exports, "rest_queryFeatures", { enumerable: true, get: function () { return arcgis_rest_feature_layer_1.queryFeatures; } });
28
- Object.defineProperty(exports, "rest_addFeatures", { enumerable: true, get: function () { return arcgis_rest_feature_layer_1.addFeatures; } });
29
- //#region Imports -------------------------------------------------------------------------------------------------------//
30
- const interfaces_1 = require("./interfaces");
31
- const generalHelpers_1 = require("./generalHelpers");
32
- const templatization_1 = require("./templatization");
33
- const restHelpers_1 = require("./restHelpers");
34
- const trackingHelpers_1 = require("./trackingHelpers");
35
- //#endregion ------------------------------------------------------------------------------------------------------------//
36
- //#region Public functions ----------------------------------------------------------------------------------------------//
37
- /**
38
- * Templatize the ID, url, field references ect
39
- *
40
- * @param itemTemplate Template for feature service item
41
- * @param dependencies Array of IDependency for name mapping
42
- * @param templatizeFieldReferences Templatize all field references within a layer
43
- * @param templateDictionary Hash mapping property names to replacement values
44
- * @returns A promise that will resolve when template has been updated
45
- * @private
46
- */
47
- function templatize(itemTemplate, dependencies, templatizeFieldReferences, templateDictionary) {
48
- templateDictionary = templateDictionary || {};
49
- // Common templatizations
50
- const id = itemTemplate.item.id;
51
- const fsUrl = itemTemplate.item.url;
52
- itemTemplate.item = {
53
- ...itemTemplate.item,
54
- id: (0, templatization_1.templatizeTerm)(id, id, ".itemId"),
55
- url: _templatize(id, "url"),
56
- typeKeywords: (0, templatization_1.templatizeIds)(itemTemplate.item.typeKeywords)
57
- };
58
- // special handeling if we are dealing with a tracker view
59
- (0, trackingHelpers_1.templatizeTracker)(itemTemplate);
60
- // added for issue #928
61
- (0, generalHelpers_1.deleteProp)(itemTemplate, "properties.service.size");
62
- const jsonLayers = itemTemplate.properties.layers || [];
63
- const jsonTables = itemTemplate.properties.tables || [];
64
- const jsonItems = jsonLayers.concat(jsonTables);
65
- const data = itemTemplate.data || {};
66
- const layers = data.layers || [];
67
- const tables = data.tables || [];
68
- const _items = layers.concat(tables);
69
- // Set up symbols for the URL of the feature service and its layers and tables
70
- templateDictionary[fsUrl] = itemTemplate.item.url; // map FS URL to its templatized form
71
- jsonItems.concat(_items).forEach(layer => {
72
- templateDictionary[fsUrl + "/" + layer.id] = _templatize(id, "layer" + layer.id + ".url");
73
- });
74
- // templatize the service references serviceItemId
75
- itemTemplate.properties.service.serviceItemId = (0, templatization_1.templatizeTerm)(itemTemplate.properties.service.serviceItemId, itemTemplate.properties.service.serviceItemId, ".itemId");
76
- const initialExtent = (0, generalHelpers_1.getProp)(itemTemplate, "properties.service.initialExtent");
77
- /* istanbul ignore else */
78
- if (initialExtent) {
79
- itemTemplate.properties.service.initialExtent = (0, templatization_1.templatizeTerm)(id, id, ".solutionExtent");
80
- }
81
- const fullExtent = (0, generalHelpers_1.getProp)(itemTemplate, "properties.service.fullExtent");
82
- /* istanbul ignore else */
83
- if (fullExtent) {
84
- itemTemplate.properties.service.fullExtent = (0, templatization_1.templatizeTerm)(id, id, ".solutionExtent");
85
- }
86
- // this default extent will be used in cases where it does not make sense to apply the orgs
87
- // extent to a service with a local spatial reference
88
- itemTemplate.properties.defaultExtent = initialExtent || fullExtent;
89
- // in some cases a service does not have a spatial reference defined
90
- // added for issue #699
91
- if (!(0, generalHelpers_1.getProp)(itemTemplate, "properties.service.spatialReference") &&
92
- (0, generalHelpers_1.getProp)(itemTemplate, "properties.defaultExtent.spatialReference")) {
93
- (0, generalHelpers_1.setCreateProp)(itemTemplate, "properties.service.spatialReference", itemTemplate.properties.defaultExtent.spatialReference);
94
- }
95
- // if any layer hasZ enabled then we need to set
96
- // enableZDefaults and zDefault to deploy to enterprise
97
- let hasZ = false;
98
- jsonItems.forEach((jsonItem) => {
99
- // get the source service json for the given data item
100
- const matchingItems = _items.filter(item => {
101
- return jsonItem.id === item.id;
102
- });
103
- // templatize the source service json
104
- const _item = matchingItems.length === 1 ? matchingItems[0] : undefined;
105
- _templatizeLayer(_item, jsonItem, itemTemplate, dependencies, templatizeFieldReferences, templateDictionary);
106
- hasZ = jsonItem.hasZ || (_item && _item.hasZ) ? true : hasZ;
107
- });
108
- if (hasZ) {
109
- itemTemplate.properties.service.enableZDefaults = true;
110
- itemTemplate.properties.service.zDefault = 0;
111
- }
112
- return itemTemplate;
113
- }
114
- exports.templatize = templatize;
115
- /**
116
- * Delete key properties that are system managed
117
- *
118
- * @param layer The data layer instance with field name references within
119
- */
120
- function deleteViewProps(layer) {
121
- const props = ["definitionQuery"];
122
- props.forEach(prop => {
123
- (0, generalHelpers_1.deleteProp)(layer, prop);
124
- });
125
- }
126
- exports.deleteViewProps = deleteViewProps;
127
- /**
128
- * Cache properties that contain field references
129
- *
130
- * removeProp added for issue #644
131
- * setting all props on add for online now
132
- * investigating if we can also just allow them to be set during add for portal
133
- *
134
- * @param layer The data layer instance with field name references within
135
- * @param fieldInfos the object that stores the cached field infos
136
- * @returns An updated instance of the fieldInfos
137
- */
138
- function cacheFieldInfos(layer, fieldInfos) {
139
- // cache the source fields as they are in the original source
140
- if (layer && layer.fields) {
141
- fieldInfos[layer.id] = {
142
- sourceFields: JSON.parse(JSON.stringify(layer.fields)),
143
- type: layer.type,
144
- id: layer.id
145
- };
146
- }
147
- // cache each of these properties as they each can contain field references
148
- // and will have associated updateDefinition calls when deploying to portal
149
- // as well as online for relationships...as relationships added with addToDef will cause failure
150
- const props = {
151
- editFieldsInfo: false,
152
- types: false,
153
- templates: false,
154
- relationships: true,
155
- drawingInfo: false,
156
- timeInfo: false,
157
- viewDefinitionQuery: false
158
- };
159
- Object.keys(props).forEach(k => {
160
- _cacheFieldInfo(layer, k, fieldInfos, props[k]);
161
- });
162
- return fieldInfos;
163
- }
164
- exports.cacheFieldInfos = cacheFieldInfos;
165
- /**
166
- * Cache the stored contingent values so we can add them in subsequent addToDef calls
167
- *
168
- * @param id The layer id for the associated values to be stored with
169
- * @param fieldInfos The object that stores the cached field infos
170
- * @param itemTemplate The current itemTemplate being processed
171
- * @returns An updated instance of the fieldInfos
172
- */
173
- function cacheContingentValues(id, fieldInfos, itemTemplate) {
174
- const contingentValues = (0, generalHelpers_1.getProp)(itemTemplate, 'properties.contingentValues');
175
- if (contingentValues && contingentValues[id]) {
176
- fieldInfos[id]['contingentValues'] = contingentValues[id];
177
- }
178
- return fieldInfos;
179
- }
180
- exports.cacheContingentValues = cacheContingentValues;
181
- /**
182
- * Helper function to cache a single property into the fieldInfos object
183
- * This property will be removed from the layer instance.
184
- *
185
- * @param layer the data layer being cloned
186
- * @param prop the property name used to cache
187
- * @param fieldInfos the object that will store the cached property
188
- * @private
189
- */
190
- function _cacheFieldInfo(layer, prop, fieldInfos, removeProp) {
191
- /* istanbul ignore else */
192
- if (layer &&
193
- layer.hasOwnProperty(prop) &&
194
- fieldInfos &&
195
- fieldInfos.hasOwnProperty(layer.id)) {
196
- fieldInfos[layer.id][prop] = layer[prop];
197
- // editFieldsInfo does not come through unless its with the layer
198
- // when it's being added
199
- /* istanbul ignore else */
200
- if (removeProp) {
201
- layer[prop] = null;
202
- }
203
- }
204
- }
205
- exports._cacheFieldInfo = _cacheFieldInfo;
206
- /**
207
- * Cache popup info that can contain field references
208
- *
209
- * @param data The items data property
210
- * @returns An updated instance of the popupInfos
211
- */
212
- function cachePopupInfos(data) {
213
- // store any popupInfo so we can update after any potential name changes
214
- const popupInfos = {
215
- layers: {},
216
- tables: {}
217
- };
218
- if (data && data.layers && data.layers.length > 0) {
219
- _cachePopupInfo(popupInfos, "layers", data.layers);
220
- }
221
- if (data && data.tables && data.tables.length > 0) {
222
- _cachePopupInfo(popupInfos, "tables", data.tables);
223
- }
224
- return popupInfos;
225
- }
226
- exports.cachePopupInfos = cachePopupInfos;
227
- /**
228
- * Helper function to cache a single popupInfo
229
- * This property will be reset on the layer
230
- *
231
- * @param popupInfos object to store the cahced popupInfo
232
- * @param type is it a layer or table
233
- * @param _items list or either layers or tables
234
- * @private
235
- */
236
- function _cachePopupInfo(popupInfos, type, _items) {
237
- _items.forEach((item) => {
238
- if (item && item.hasOwnProperty("popupInfo")) {
239
- popupInfos[type][item.id] = item.popupInfo;
240
- item.popupInfo = {};
241
- }
242
- });
243
- }
244
- exports._cachePopupInfo = _cachePopupInfo;
245
- /**
246
- * Store basic layer information for potential replacement if we are unable to access a given service
247
- * added for issue #859
248
- *
249
- * @param layerId the id for the layer
250
- * @param itemId the id for the item
251
- * @param url the url for the layer
252
- * @param templateDictionary Hash of key details used for variable replacement
253
- * @returns templatized itemTemplate
254
- */
255
- function cacheLayerInfo(layerId, itemId, url, templateDictionary) {
256
- if (layerId) {
257
- const layerIdVar = `layer${layerId}`;
258
- // need to structure these differently so they are not used for standard replacement calls
259
- // this now adds additional vars that are not needing replacement unless we fail to fetch the service
260
- const newVars = (0, generalHelpers_1.getProp)(templateDictionary, `${interfaces_1.UNREACHABLE}.${itemId}`) || {
261
- itemId
262
- };
263
- newVars[layerIdVar] = (0, generalHelpers_1.getProp)(newVars, layerIdVar) || {
264
- layerId,
265
- itemId
266
- };
267
- if (url !== "") {
268
- newVars[layerIdVar]["url"] = url;
269
- }
270
- const unreachableVars = {};
271
- unreachableVars[itemId] = newVars;
272
- templateDictionary[interfaces_1.UNREACHABLE] = {
273
- ...templateDictionary[interfaces_1.UNREACHABLE],
274
- ...unreachableVars
275
- };
276
- }
277
- }
278
- exports.cacheLayerInfo = cacheLayerInfo;
279
- /**
280
- * Creates an item in a specified folder (except for Group item type).
281
- *
282
- * @param itemTemplate Item to be created; n.b.: this item is modified
283
- * @param templateDictionary Hash mapping property names to replacement values
284
- * @param createResponse Response from create service
285
- * @returns An updated instance of the template
286
- * @private
287
- */
288
- function updateTemplate(itemTemplate, templateDictionary, createResponse) {
289
- // Update the item with any typeKeywords that were added on create
290
- _updateTypeKeywords(itemTemplate, createResponse);
291
- // Add the new item to the template dictionary
292
- templateDictionary[itemTemplate.itemId] = Object.assign(templateDictionary[itemTemplate.itemId] || {}, {
293
- itemId: createResponse.serviceItemId,
294
- url: (0, generalHelpers_1.checkUrlPathTermination)(createResponse.serviceurl),
295
- name: createResponse.name
296
- });
297
- // Update the item template now that the new service has been created
298
- itemTemplate.itemId = createResponse.serviceItemId;
299
- return (0, templatization_1.replaceInTemplate)(itemTemplate, templateDictionary);
300
- }
301
- exports.updateTemplate = updateTemplate;
302
- /**
303
- * Updates the items typeKeywords to include any typeKeywords that
304
- * were added by the create service request
305
- *
306
- * @param itemTemplate Item to be created; n.b.: this item is modified
307
- * @param createResponse Response from create service
308
- * @returns An updated instance of the template
309
- * @private
310
- */
311
- function _updateTypeKeywords(itemTemplate, createResponse) {
312
- // https://github.com/Esri/solution.js/issues/589
313
- const iKwords = (0, generalHelpers_1.getProp)(itemTemplate, "item.typeKeywords");
314
- const cKwords = (0, generalHelpers_1.getProp)(createResponse, "typeKeywords");
315
- if (iKwords && cKwords) {
316
- (0, generalHelpers_1.setProp)(itemTemplate, "item.typeKeywords", iKwords.concat(cKwords.filter(k => iKwords.indexOf(k) < 0)));
317
- }
318
- return itemTemplate;
319
- }
320
- exports._updateTypeKeywords = _updateTypeKeywords;
321
- /**
322
- * Create the name mapping object that will allow for all templatized field
323
- * references to be de-templatized.
324
- * This also removes the stored sourceFields and newFields arrays from fieldInfos.
325
- *
326
- * @example
327
- * \{ layer0: \{ fields: \{ lowerCaseSourceFieldName: newFieldNameAfterDeployment \} \} \}
328
- *
329
- * @param layerInfos The object that stores the cached layer properties and name mapping
330
- * @returns The settings object that will be used to de-templatize the field references.
331
- */
332
- function getLayerSettings(layerInfos, url, itemId, enterpriseIDMapping) {
333
- const settings = {};
334
- const ids = Object.keys(layerInfos);
335
- ids.forEach((id) => {
336
- const _layerId = (0, generalHelpers_1.getProp)(layerInfos[id], "item.id");
337
- const isNum = parseInt(_layerId, 10) > -1;
338
- const layerId = isNum && enterpriseIDMapping
339
- ? enterpriseIDMapping[_layerId]
340
- : isNum
341
- ? _layerId
342
- : id;
343
- settings[`layer${isNum ? _layerId : id}`] = {
344
- fields: _getNameMapping(layerInfos, id),
345
- url: (0, generalHelpers_1.checkUrlPathTermination)(url) + layerId,
346
- layerId,
347
- itemId
348
- };
349
- (0, generalHelpers_1.deleteProp)(layerInfos[id], "newFields");
350
- (0, generalHelpers_1.deleteProp)(layerInfos[id], "sourceFields");
351
- });
352
- return settings;
353
- }
354
- exports.getLayerSettings = getLayerSettings;
355
- /**
356
- * Set the names and titles for all feature services.
357
- *
358
- * This function will ensure that we have unique feature service names.
359
- * The feature service name will have the solution item id appended.
360
- *
361
- * @param templates A collection of AGO item templates.
362
- * @param solutionItemId The item id for the deployed solution item.
363
- * @returns An updated collection of AGO templates with unique feature service names.
364
- */
365
- function setNamesAndTitles(templates, solutionItemId) {
366
- const names = [];
367
- return templates.map(t => {
368
- /* istanbul ignore else */
369
- if (t.item.type === "Feature Service") {
370
- // Retain the existing title but swap with name if it's missing
371
- t.item.title = t.item.title || t.item.name;
372
- /* istanbul ignore else */
373
- if (!(0, trackingHelpers_1.isTrackingViewTemplate)(t)) {
374
- // Need to set the service name: name + "_" + newItemId
375
- let baseName = t.item.name || t.item.title;
376
- // If the name already contains a GUID remove it
377
- baseName = baseName.replace(/_[0-9A-F]{32}/gi, "");
378
- // The name length limit is 98
379
- // Limit the baseName to 50 characters before the _<guid>
380
- const name = baseName.substring(0, 50) + "_" + solutionItemId;
381
- // If the name + GUID already exists then append "_occurrenceCount"
382
- t.item.name =
383
- names.indexOf(name) === -1
384
- ? name
385
- : `${name}_${names.filter(n => n === name).length}`;
386
- names.push(name);
387
- }
388
- }
389
- return t;
390
- });
391
- }
392
- exports.setNamesAndTitles = setNamesAndTitles;
393
- /**
394
- * This is used when deploying views.
395
- * We need to update fields referenced in adminLayerInfo for relationships prior to deploying the view.
396
- * This moves the fieldInfos for the views source layers from the item settings for the source layer
397
- * to the item settings for the view.
398
- *
399
- * @param itemTemplate The current itemTemplate being processed.
400
- * @param settings The settings object used to de-templatize the various templates within the item.
401
- */
402
- function updateSettingsFieldInfos(itemTemplate, settings) {
403
- const dependencies = itemTemplate.dependencies;
404
- const id = itemTemplate.itemId;
405
- const settingsKeys = Object.keys(settings);
406
- settingsKeys.forEach((k) => {
407
- if (id === settings[k].itemId) {
408
- dependencies.forEach((d) => {
409
- settingsKeys.forEach((_k) => {
410
- /* istanbul ignore else */
411
- if (d === _k) {
412
- // combine for multi-source views
413
- const fieldInfos = {};
414
- fieldInfos[d] = (0, generalHelpers_1.getProp)(settings[_k], "fieldInfos");
415
- settings[k]["sourceServiceFields"] = settings[k]["sourceServiceFields"]
416
- ? { ...settings[k]["sourceServiceFields"], ...fieldInfos }
417
- : fieldInfos;
418
- const layerKeys = Object.keys(settings[_k]);
419
- layerKeys.forEach(layerKey => {
420
- /* istanbul ignore else */
421
- if (layerKey.startsWith("layer")) {
422
- settings[k][layerKey] = settings[_k][layerKey];
423
- }
424
- });
425
- }
426
- });
427
- });
428
- }
429
- });
430
- }
431
- exports.updateSettingsFieldInfos = updateSettingsFieldInfos;
432
- /**
433
- * Add flag to indicate item should be ignored.
434
- * Construct template dictionary to detemplatize any references to this item by other items.
435
- *
436
- * @param template Template for feature service item
437
- * @param authentication Credentials for the request
438
- * @returns A promise that will resolve when template has been updated
439
- * @private
440
- */
441
- function updateTemplateForInvalidDesignations(template, authentication) {
442
- return new Promise((resolve, reject) => {
443
- template.properties.hasInvalidDesignations = true;
444
- if (template.item.url) {
445
- // get the admin URL
446
- const url = template.item.url;
447
- (0, restHelpers_1.rest_request)(url + "?f=json", {
448
- authentication: authentication
449
- }).then(serviceData => {
450
- const layerInfos = {};
451
- const layersAndTables = (serviceData.layers || []).concat(serviceData.tables || []);
452
- layersAndTables.forEach((l) => {
453
- /* istanbul ignore else */
454
- if (l && l.hasOwnProperty("id")) {
455
- layerInfos[l.id] = l;
456
- }
457
- });
458
- template.data[template.itemId] = Object.assign({
459
- itemId: template.itemId
460
- }, getLayerSettings(layerInfos, url, template.itemId));
461
- resolve(template);
462
- }, e => reject((0, generalHelpers_1.fail)(e)));
463
- }
464
- else {
465
- resolve(template);
466
- }
467
- });
468
- }
469
- exports.updateTemplateForInvalidDesignations = updateTemplateForInvalidDesignations;
470
- /**
471
- * Get the contingent values for each layer in the service.
472
- * Remove key props that cannot be included with the addToDef call on deploy.
473
- * Store the values alongside other key feature service properties in the template
474
- *
475
- * @param properties the current feature services properties
476
- * @param adminUrl the current feature service url
477
- * @param authentication Credentials for the request to AGOL
478
- * @returns A promise that will resolve when the contingent values have been fetched.
479
- * This function will update the provided properties argument when contingent values are found.
480
- */
481
- function processContingentValues(properties, adminUrl, authentication) {
482
- return new Promise((resolve, reject) => {
483
- if ((0, generalHelpers_1.getProp)(properties, 'service.isView')) {
484
- // views will inherit from the source service
485
- resolve();
486
- }
487
- else {
488
- const layersAndTables = (properties.layers || []).concat(properties.tables || []);
489
- const layerIds = [];
490
- const contingentValuePromises = layersAndTables.reduce((prev, cur) => {
491
- /* istanbul ignore else */
492
- if (cur.hasContingentValuesDefinition) {
493
- prev.push((0, restHelpers_1.rest_request)(`${adminUrl}/${cur['id']}/contingentValues?f=json`, { authentication }));
494
- layerIds.push(cur['id']);
495
- }
496
- return prev;
497
- }, []);
498
- if (contingentValuePromises.length > 0) {
499
- Promise.all(contingentValuePromises).then((results) => {
500
- const contingentValues = {};
501
- results.forEach((r, i) => {
502
- (0, generalHelpers_1.deleteProp)(r, 'typeCodes');
503
- /* istanbul ignore else */
504
- if ((0, generalHelpers_1.getProp)(r, 'stringDicts') && (0, generalHelpers_1.getProp)(r, 'contingentValuesDefinition')) {
505
- r.contingentValuesDefinition['stringDicts'] = r.stringDicts;
506
- (0, generalHelpers_1.deleteProp)(r, 'stringDicts');
507
- }
508
- (0, generalHelpers_1.deleteProps)((0, generalHelpers_1.getProp)(r, 'contingentValuesDefinition'), ['layerID', 'layerName', 'geometryType', 'hasSubType']);
509
- contingentValues[layerIds[i]] = r;
510
- });
511
- properties.contingentValues = contingentValues;
512
- resolve();
513
- }, reject);
514
- }
515
- else {
516
- resolve();
517
- }
518
- }
519
- });
520
- }
521
- exports.processContingentValues = processContingentValues;
522
- /**
523
- * Replace the field name reference templates with the new field names after deployment.
524
- *
525
- * @param fieldInfos The object that stores the cached layer properties and name mapping
526
- * @param popupInfos The object from the popupInfo property for the layer
527
- * @param adminLayerInfos The object from the adminLayerInfo property for the layer
528
- * @param settings The settings object that has all of the mappings for de-templatizing.
529
- * @returns An object that contains updated instances of popupInfos, fieldInfos, and adminLayerInfos
530
- */
531
- function deTemplatizeFieldInfos(fieldInfos, popupInfos, adminLayerInfos, settings) {
532
- const fieldInfoKeys = Object.keys(fieldInfos);
533
- fieldInfoKeys.forEach(id => {
534
- if (fieldInfos[id].hasOwnProperty("templates")) {
535
- fieldInfos[id].templates = JSON.parse((0, templatization_1.replaceInTemplate)(JSON.stringify(fieldInfos[id].templates), settings));
536
- }
537
- if (fieldInfos[id].hasOwnProperty("adminLayerInfo")) {
538
- adminLayerInfos[id].viewLayerDefinition.table.relatedTables =
539
- fieldInfos[id].adminLayerInfo;
540
- (0, generalHelpers_1.deleteProp)(fieldInfos[id], "adminLayerInfo");
541
- }
542
- if (fieldInfos[id].hasOwnProperty("types")) {
543
- fieldInfos[id].types = JSON.parse((0, templatization_1.replaceInTemplate)(JSON.stringify(fieldInfos[id].types), settings));
544
- }
545
- });
546
- return {
547
- popupInfos: (0, templatization_1.replaceInTemplate)(popupInfos, settings),
548
- fieldInfos: (0, templatization_1.replaceInTemplate)(fieldInfos, settings),
549
- adminLayerInfos: (0, templatization_1.replaceInTemplate)(adminLayerInfos, settings)
550
- };
551
- }
552
- exports.deTemplatizeFieldInfos = deTemplatizeFieldInfos;
553
- /**
554
- * This is used when deploying views.
555
- * We need to update fields referenced in adminLayerInfo for relationships prior to deploying the view.
556
- * This moves the fieldInfos for the views source layers from the item settings for the source layer
557
- * to the item settings for the view.
558
- *
559
- * @param itemTemplate The current itemTemplate being processed.
560
- * @returns array of layers and tables
561
- */
562
- function getLayersAndTables(itemTemplate) {
563
- const properties = itemTemplate.properties;
564
- const layersAndTables = [];
565
- (properties.layers || []).forEach(function (layer) {
566
- layersAndTables.push({
567
- item: layer,
568
- type: "layer"
569
- });
570
- });
571
- (properties.tables || []).forEach(function (table) {
572
- layersAndTables.push({
573
- item: table,
574
- type: "table"
575
- });
576
- });
577
- return layersAndTables;
578
- }
579
- exports.getLayersAndTables = getLayersAndTables;
580
- /**
581
- * Fetch each layer and table from service so we can determine what fields they have.
582
- * This is leveraged when we are using existing services so we can determine if we need to
583
- * remove any fields from views that depend on these layers and tables.
584
- *
585
- * @param url Feature service endpoint
586
- * @param ids layer and table ids
587
- * @param authentication Credentials for the request
588
- * @returns A promise that will resolve an array of promises with either a failure or the data
589
- * @private
590
- */
591
- function getExistingLayersAndTables(url, ids, authentication) {
592
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
593
- return new Promise(resolve => {
594
- const defs = ids.map(id => {
595
- return (0, restHelpers_1.rest_request)((0, generalHelpers_1.checkUrlPathTermination)(url) + id, {
596
- authentication
597
- });
598
- });
599
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
600
- Promise.all(defs.map(p => p.catch(e => e))).then(resolve);
601
- });
602
- }
603
- exports.getExistingLayersAndTables = getExistingLayersAndTables;
604
- /**
605
- * Adds the layers and tables of a feature service to it and restores their relationships.
606
- *
607
- * @param itemTemplate Feature service
608
- * @param templateDictionary Hash mapping Solution source id to id of its clone (and name & URL for feature
609
- * service)
610
- * @param popupInfos the cached popup info from the layers
611
- * @param authentication Credentials for the request
612
- * @returns A promise that will resolve when all layers and tables have been added
613
- * @private
614
- */
615
- function addFeatureServiceLayersAndTables(itemTemplate, templateDictionary, popupInfos, authentication) {
616
- return new Promise((resolve, reject) => {
617
- if ((0, trackingHelpers_1.isTrackingViewTemplate)(itemTemplate)) {
618
- resolve(null);
619
- }
620
- else {
621
- // Create a hash of various properties that contain field references
622
- const fieldInfos = {};
623
- const adminLayerInfos = {};
624
- // Add the service's layers and tables to it
625
- const layersAndTables = getLayersAndTables(itemTemplate);
626
- if (layersAndTables.length > 0) {
627
- addFeatureServiceDefinition(itemTemplate.item.url || "", layersAndTables, templateDictionary, authentication, itemTemplate.key, adminLayerInfos, fieldInfos, itemTemplate).then(() => {
628
- // Detemplatize field references and update the layer properties
629
- // Only failure path is handled by addFeatureServiceDefinition
630
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
631
- updateLayerFieldReferences(itemTemplate, fieldInfos, popupInfos, adminLayerInfos, templateDictionary).then(r => {
632
- // Update relationships and layer definitions
633
- const updates = (0, restHelpers_1.getLayerUpdates)({
634
- message: "updated layer definition",
635
- objects: r.layerInfos.fieldInfos,
636
- itemTemplate: r.itemTemplate,
637
- authentication
638
- }, templateDictionary.isPortal);
639
- // Process the updates sequentially
640
- updates
641
- .reduce((prev, update) => {
642
- return prev.then(() => {
643
- return (0, restHelpers_1.getRequest)(update);
644
- });
645
- }, Promise.resolve(null))
646
- .then(() => resolve(null), (e) => reject((0, generalHelpers_1.fail)(e)) // getRequest
647
- );
648
- });
649
- }, e => reject((0, generalHelpers_1.fail)(e)) // addFeatureServiceDefinition
650
- );
651
- }
652
- else {
653
- resolve(null);
654
- }
655
- }
656
- });
657
- }
658
- exports.addFeatureServiceLayersAndTables = addFeatureServiceLayersAndTables;
659
- /**
660
- * Updates a feature service with a list of layers and/or tables.
661
- *
662
- * @param serviceUrl URL of feature service
663
- * @param listToAdd List of layers and/or tables to add
664
- * @param templateDictionary Hash mapping Solution source id to id of its clone (and name & URL for feature
665
- * service)
666
- * @param authentication Credentials for the request
667
- * @param key
668
- * @param adminLayerInfos Hash map of a layers adminLayerInfo
669
- * @param fieldInfos Hash map of properties that contain field references
670
- * @param itemTemplate
671
- * @returns A promise that will resolve when the feature service has been updated
672
- * @private
673
- */
674
- function addFeatureServiceDefinition(serviceUrl, listToAdd, templateDictionary, authentication, key, adminLayerInfos, fieldInfos, itemTemplate) {
675
- return new Promise((resolve, reject) => {
676
- if ((0, trackingHelpers_1.isTrackingViewTemplate)(itemTemplate)) {
677
- resolve(null);
678
- }
679
- else {
680
- let options = {
681
- layers: [],
682
- tables: [],
683
- authentication
684
- };
685
- // if the service has veiws keep track of the fields so we can use them to
686
- // compare with the view fields
687
- /* istanbul ignore else */
688
- if ((0, generalHelpers_1.getProp)(itemTemplate, "properties.service.hasViews")) {
689
- _updateTemplateDictionaryFields(itemTemplate, templateDictionary);
690
- }
691
- const isSelfReferential = _isSelfReferential(listToAdd);
692
- listToAdd = _updateOrder(listToAdd, isSelfReferential);
693
- const chunkSize = _getLayerChunkSize();
694
- const layerChunks = [];
695
- listToAdd.forEach((toAdd, i) => {
696
- let item = toAdd.item;
697
- const originalId = item.id;
698
- fieldInfos = cacheFieldInfos(item, fieldInfos);
699
- // cache the values to be added in seperate addToDef calls
700
- fieldInfos = cacheContingentValues(item.id, fieldInfos, itemTemplate);
701
- /* istanbul ignore else */
702
- if (item.isView) {
703
- deleteViewProps(item);
704
- }
705
- // when the item is a view we need to grab the supporting fieldInfos
706
- /* istanbul ignore else */
707
- if (itemTemplate.properties.service.isView) {
708
- _updateGeomFieldName(item.adminLayerInfo, templateDictionary);
709
- adminLayerInfos[originalId] = item.adminLayerInfo;
710
- // need to update adminLayerInfo before adding to the service def
711
- // bring over the fieldInfos from the source layer
712
- updateSettingsFieldInfos(itemTemplate, templateDictionary);
713
- // update adminLayerInfo before add to definition with view source fieldInfo settings
714
- item.adminLayerInfo = (0, templatization_1.replaceInTemplate)(item.adminLayerInfo, templateDictionary);
715
- /* istanbul ignore else */
716
- if (fieldInfos && fieldInfos.hasOwnProperty(item.id)) {
717
- Object.keys(templateDictionary).some(k => {
718
- if (templateDictionary[k].itemId === itemTemplate.itemId) {
719
- fieldInfos[item.id]["sourceServiceFields"] =
720
- templateDictionary[k].sourceServiceFields;
721
- return true;
722
- }
723
- else {
724
- return false;
725
- }
726
- });
727
- }
728
- }
729
- /* istanbul ignore else */
730
- if (templateDictionary.isPortal) {
731
- item = _updateForPortal(item, itemTemplate, templateDictionary);
732
- }
733
- removeLayerOptimization(item);
734
- // this can still chunk layers
735
- options = _updateAddOptions(itemTemplate, options, layerChunks, isSelfReferential, authentication);
736
- if (item.type === "Feature Layer") {
737
- options.layers.push(item);
738
- }
739
- else {
740
- options.tables.push(item);
741
- }
742
- // In general we are switching to not use chunking. Rather if we exceed the defined chunk size
743
- // we will use an async request.
744
- // Currently the only case that should chunk the requests is when we have a multisource view
745
- // handled in _updateAddOptions above
746
- /* istanbul ignore else */
747
- if (i + 1 === listToAdd.length) {
748
- layerChunks.push(Object.assign({}, options));
749
- options = {
750
- layers: [],
751
- tables: [],
752
- authentication
753
- };
754
- }
755
- });
756
- // will use async by default rather than chunk the layer requests when we have more layers
757
- // than the defined chunk size
758
- const useAsync = listToAdd.length > chunkSize;
759
- layerChunks
760
- .reduce((prev, curr) => prev.then(() => (0, restHelpers_1.addToServiceDefinition)(serviceUrl, curr, false, useAsync)), Promise.resolve(null))
761
- .then(() => resolve(null), (e) => reject((0, generalHelpers_1.fail)(e)));
762
- }
763
- });
764
- }
765
- exports.addFeatureServiceDefinition = addFeatureServiceDefinition;
766
- /**
767
- * When a view is a multi service view sort based on the id
768
- * https://github.com/Esri/solution.js/issues/1048
769
- *
770
- * @param layersAndTables The list of layers and tables for the current template
771
- * @param isSelfReferential Indicates if any layers or tables have relationships with other layers or tables in the same service
772
- *
773
- * @returns Sorted list of layers and tables when using a multi-service view
774
- * @private
775
- */
776
- function _updateOrder(layersAndTables, isSelfReferential) {
777
- return isSelfReferential ? layersAndTables.sort((a, b) => a.item.id - b.item.id) : layersAndTables;
778
- }
779
- exports._updateOrder = _updateOrder;
780
- /**
781
- * When a view is a multi service view add each layer separately
782
- * https://github.com/Esri/solution.js/issues/871
783
- *
784
- * @param itemTemplate The current itemTemplate being processed
785
- * @param options Add to service definition options
786
- * @param layerChunks Groups of layers or tables to add to the service
787
- * @param isSelfReferential Indicates if any layers or tables have relationships with other layers or tables in the same service
788
- * @param authentication Credentials for the request
789
- *
790
- * @returns Add to service definition options
791
- * @private
792
- */
793
- function _updateAddOptions(itemTemplate, options, layerChunks, isSelfReferential, authentication) {
794
- const isMsView = (0, generalHelpers_1.getProp)(itemTemplate, "properties.service.isMultiServicesView") || false;
795
- /* istanbul ignore else */
796
- if (isMsView || isSelfReferential) {
797
- // if we already have some layers or tables add them first
798
- /* istanbul ignore else */
799
- if (options.layers.length > 0 || options.tables.length > 0) {
800
- layerChunks.push(Object.assign({}, options));
801
- options = {
802
- layers: [],
803
- tables: [],
804
- authentication
805
- };
806
- }
807
- }
808
- return options;
809
- }
810
- exports._updateAddOptions = _updateAddOptions;
811
- /**
812
- * Determine if any layer or table within the service references
813
- * other layers or tables within the same service
814
- *
815
- * @param layersAndTables the list of layers and tables from the service
816
- *
817
- * @returns true when valid internal references are found
818
- * @private
819
- */
820
- function _isSelfReferential(layersAndTables) {
821
- const names = layersAndTables.map(l => l.item.name);
822
- const srcTables = {};
823
- return layersAndTables.some(l => {
824
- const table = l.item.adminLayerInfo?.viewLayerDefinition?.table;
825
- if (table) {
826
- const name = table.sourceServiceName;
827
- const id = table.sourceLayerId;
828
- if (name && id > -1) {
829
- if (Object.keys(srcTables).indexOf(name) > -1) {
830
- if (srcTables[name].indexOf(id) > -1) {
831
- return true;
832
- }
833
- else {
834
- srcTables[name].push(id);
835
- }
836
- }
837
- else {
838
- srcTables[name] = [id];
839
- }
840
- }
841
- return (table.relatedTables || []).some(r => names.indexOf(r.name) > -1);
842
- }
843
- });
844
- }
845
- exports._isSelfReferential = _isSelfReferential;
846
- /**
847
- * Remove "multiScaleGeometryInfo" for issue #526 to prevent invalid enablement of layer optimization
848
- *
849
- * @param layer the layer to evaluate
850
- * @private
851
- */
852
- function removeLayerOptimization(layer) {
853
- /* istanbul ignore else */
854
- if (layer.multiScaleGeometryInfo) {
855
- (0, generalHelpers_1.deleteProp)(layer, "multiScaleGeometryInfo");
856
- }
857
- }
858
- exports.removeLayerOptimization = removeLayerOptimization;
859
- /**
860
- * Handle portal specific updates to the item
861
- *
862
- * @param item the item to update
863
- * @param itemTemplate the item template
864
- * @param templateDictionary Hash mapping Solution source id to id of its clone
865
- *
866
- * @returns the updated item
867
- * @private
868
- */
869
- function _updateForPortal(item, itemTemplate, templateDictionary) {
870
- // When deploying to portal we need to adjust the uniquie ID field up front
871
- /* istanbul ignore else */
872
- if (item.uniqueIdField && item.uniqueIdField.name) {
873
- item.uniqueIdField.name = String(item.uniqueIdField.name).toLocaleLowerCase();
874
- }
875
- // Portal will fail if the geometryField is null
876
- if (item.type === "Table" && item.adminLayerInfo) {
877
- (0, generalHelpers_1.deleteProp)(item.adminLayerInfo, "geometryField");
878
- }
879
- // Portal will fail if the sourceFields in the viewLayerDef contain fields that are not in the source service
880
- /* istanbul ignore else */
881
- if (item.isView) {
882
- const viewLayerDefTable = (0, generalHelpers_1.getProp)(item, "adminLayerInfo.viewLayerDefinition.table");
883
- let fieldNames = [];
884
- if (viewLayerDefTable) {
885
- const tableFieldNames = _getFieldNames(viewLayerDefTable, itemTemplate, templateDictionary);
886
- fieldNames = fieldNames.concat(tableFieldNames);
887
- const dynamicFieldNames = _getDynamicFieldNames(viewLayerDefTable);
888
- fieldNames = fieldNames.concat(dynamicFieldNames);
889
- (0, generalHelpers_1.setProp)(item, "adminLayerInfo.viewLayerDefinition.table", _updateSourceLayerFields(viewLayerDefTable, fieldNames));
890
- // Handle related also
891
- /* istanbul ignore else */
892
- if (Array.isArray(viewLayerDefTable.relatedTables)) {
893
- viewLayerDefTable.relatedTables.map((relatedTable) => {
894
- const relatedTableFieldNames = _getFieldNames(relatedTable, itemTemplate, templateDictionary);
895
- fieldNames = fieldNames.concat(relatedTableFieldNames);
896
- const dynamicRelatedFieldNames = _getDynamicFieldNames(relatedTable);
897
- fieldNames = fieldNames.concat(dynamicRelatedFieldNames);
898
- return _updateSourceLayerFields(relatedTable, [...relatedTableFieldNames, ...dynamicRelatedFieldNames]);
899
- });
900
- }
901
- }
902
- else {
903
- Object.keys(templateDictionary).some(k => {
904
- /* istanbul ignore else */
905
- if (templateDictionary[k].itemId === item.serviceItemId) {
906
- const layerInfo = templateDictionary[k][`layer${item.id}`];
907
- /* istanbul ignore else */
908
- if (layerInfo && layerInfo.fields) {
909
- if (Array.isArray(layerInfo.fields)) {
910
- fieldNames = layerInfo.fields.map((f) => f.name);
911
- }
912
- else {
913
- fieldNames = Object.keys(layerInfo.fields);
914
- }
915
- }
916
- return true;
917
- }
918
- });
919
- }
920
- item = _updateItemFields(item, fieldNames);
921
- }
922
- // not allowed to set sourceSchemaChangesAllowed or isView for portal
923
- // these are set when you create the service
924
- (0, generalHelpers_1.deleteProp)(item, "isView");
925
- return item;
926
- }
927
- exports._updateForPortal = _updateForPortal;
928
- /**
929
- * Get a list of the source layer field names
930
- *
931
- * @param table the table instance to compare
932
- * @param itemTemplate the item template
933
- * @param templateDictionary Hash mapping Solution source id to id of its clone
934
- *
935
- * @returns an array of the source layers fields
936
- * @private
937
- */
938
- function _getFieldNames(table, itemTemplate, templateDictionary) {
939
- let sourceLayerFields = [];
940
- const viewSourceLayerId = table.sourceLayerId;
941
- /* istanbul ignore else */
942
- if (typeof viewSourceLayerId === "number") {
943
- // need to make sure these actually exist in the source..
944
- itemTemplate.dependencies.forEach(d => {
945
- const layerInfo = templateDictionary[d][`layer${viewSourceLayerId}`];
946
- /* istanbul ignore else */
947
- if (layerInfo &&
948
- layerInfo.fields &&
949
- templateDictionary[d].name === table.sourceServiceName) {
950
- if (Array.isArray(layerInfo.fields)) {
951
- sourceLayerFields = sourceLayerFields.concat(layerInfo.fields.map((f) => f.name));
952
- }
953
- else {
954
- sourceLayerFields = sourceLayerFields.concat(Object.keys(layerInfo.fields));
955
- }
956
- }
957
- });
958
- return sourceLayerFields;
959
- }
960
- }
961
- exports._getFieldNames = _getFieldNames;
962
- /**
963
- * Get a list of any dynamically calculated fields
964
- * These fields are still valid but will not exist in the source service
965
- *
966
- * @param table the table instance to compare
967
- *
968
- * @returns an array of field names
969
- * @private
970
- */
971
- function _getDynamicFieldNames(table) {
972
- const fieldNames = table.sourceLayerFields.reduce((prev, cur) => {
973
- if (cur.statisticType) {
974
- prev.push(cur.name);
975
- }
976
- return prev;
977
- }, []);
978
- return [...new Set(fieldNames)];
979
- }
980
- exports._getDynamicFieldNames = _getDynamicFieldNames;
981
- /**
982
- * Remove fields references from fields and indexes that do not exist in the source service
983
- *
984
- * @param item Layer or table
985
- * @param templateDictionary Hash mapping Solution source id to id of its clone
986
- *
987
- * @returns updated layer or table
988
- * @private
989
- */
990
- function _updateItemFields(item, fieldNames) {
991
- /* istanbul ignore else */
992
- if (fieldNames.length > 0) {
993
- /* istanbul ignore else */
994
- if (item.fields) {
995
- item.fields = item.fields.filter((f) => fieldNames.indexOf(f.name) > -1);
996
- }
997
- /* istanbul ignore else */
998
- if (item.indexes) {
999
- item.indexes = item.indexes.filter((f) => fieldNames.indexOf(f.fields) > -1);
1000
- }
1001
- }
1002
- return item;
1003
- }
1004
- exports._updateItemFields = _updateItemFields;
1005
- /**
1006
- * Filter the sourceLayerFields for the table
1007
- *
1008
- * @param table the table instance to evaluate
1009
- * @param sourceLayerFields array of fields from the source service
1010
- * @returns Updated instance of the table
1011
- * @private
1012
- */
1013
- function _updateSourceLayerFields(table, sourceLayerFields) {
1014
- /* istanbul ignore else */
1015
- if (Array.isArray(table.sourceLayerFields) &&
1016
- table.sourceLayerFields.length > 0) {
1017
- // need to make sure these actually exist in the source..
1018
- /* istanbul ignore else */
1019
- if (sourceLayerFields.length > 0) {
1020
- (0, generalHelpers_1.setProp)(table, "sourceLayerFields", table.sourceLayerFields.filter((f) => sourceLayerFields.indexOf(f.source.toLowerCase()) > -1));
1021
- }
1022
- }
1023
- return table;
1024
- }
1025
- exports._updateSourceLayerFields = _updateSourceLayerFields;
1026
- /**
1027
- * When the itemm is a view with a geometry field update the value to
1028
- * use the table name from the view layer def
1029
- *
1030
- * @param item the item details from the current template
1031
- * @param templateDictionary Hash mapping property names to replacement values
1032
- * @private
1033
- */
1034
- function _updateGeomFieldName(adminLayerInfo, templateDictionary) {
1035
- // issue #471
1036
- const tableName = (0, generalHelpers_1.getProp)(adminLayerInfo, "viewLayerDefinition.table.name");
1037
- const fieldName = (0, generalHelpers_1.getProp)(adminLayerInfo, "geometryField.name");
1038
- /* istanbul ignore else */
1039
- if (fieldName && tableName) {
1040
- const geomName = templateDictionary.isPortal
1041
- ? `${tableName}.shape`
1042
- : `${tableName}.Shape`;
1043
- (0, generalHelpers_1.setProp)(adminLayerInfo, "geometryField.name", geomName);
1044
- }
1045
- else if (!fieldName && (0, generalHelpers_1.getProp)(adminLayerInfo, "geometryField")) {
1046
- // null geom field will cause failure to deploy in portal
1047
- // this is also checked and removed on deploy for older solutions
1048
- (0, generalHelpers_1.deleteProp)(adminLayerInfo, "geometryField");
1049
- }
1050
- }
1051
- exports._updateGeomFieldName = _updateGeomFieldName;
1052
- /**
1053
- * Add the fields to the templateDictionary when a service has views
1054
- * these are used to compare with fields from the view when domains are involved
1055
- * when a view field has a domain that differs from that of the source service
1056
- * the definition needs to be modified in an update call rather than when it is first added.
1057
- * This should only happen when the domain differs.
1058
- *
1059
- * @param itemTemplate
1060
- * @param templateDictionary Hash mapping Solution source id to id of its clone (and name & URL for feature service)
1061
- * @private
1062
- */
1063
- function _updateTemplateDictionaryFields(itemTemplate, templateDictionary, compareItemId = true) {
1064
- const layers = itemTemplate.properties.layers;
1065
- const tables = itemTemplate.properties.tables;
1066
- const layersAndTables = layers.concat(tables);
1067
- const fieldInfos = {};
1068
- layersAndTables.forEach(layerOrTable => {
1069
- fieldInfos[layerOrTable.id] = layerOrTable.fields;
1070
- });
1071
- Object.keys(templateDictionary).some(k => {
1072
- if (compareItemId
1073
- ? templateDictionary[k].itemId === itemTemplate.itemId
1074
- : k === itemTemplate.itemId) {
1075
- templateDictionary[k].fieldInfos = fieldInfos;
1076
- return true;
1077
- }
1078
- else {
1079
- return false;
1080
- }
1081
- });
1082
- }
1083
- exports._updateTemplateDictionaryFields = _updateTemplateDictionaryFields;
1084
- /**
1085
- * Set the defaultSpatialReference variable with the services spatial reference.
1086
- * If this item is a Feature Service that has child views then we will use this value
1087
- * if one or more of the child views spatial reference differs from that of its parent.
1088
- *
1089
- * @param templateDictionary Hash mapping Solution source id to id of its clone (and name & URL for feature service)
1090
- * @param itemId The source id for the item
1091
- * @param spatialReference \{ wkid: 102100 \} for example
1092
- * @private
1093
- */
1094
- function setDefaultSpatialReference(templateDictionary, itemId, spatialReference) {
1095
- /* istanbul ignore else */
1096
- if (spatialReference) {
1097
- (0, generalHelpers_1.setCreateProp)(templateDictionary, `${itemId}.defaultSpatialReference`, spatialReference);
1098
- }
1099
- }
1100
- exports.setDefaultSpatialReference = setDefaultSpatialReference;
1101
- /**
1102
- * Compare the spatial reference of the current item against its dependencies.
1103
- * The spatial reference of a view cannot differ from its source service.
1104
- * If the view has a different spatial reference from its source use the source spatial reference.
1105
- *
1106
- * @param serviceInfo Basic service information
1107
- * @param itemTemplate The current template to process
1108
- * @param templateDictionary Hash mapping Solution source id to id of its clone (and name & URL for feature service)
1109
- * @private
1110
- */
1111
- function validateSpatialReferenceAndExtent(serviceInfo, itemTemplate, templateDictionary) {
1112
- /* istanbul ignore else */
1113
- if ((0, generalHelpers_1.getProp)(serviceInfo, "service.isView")) {
1114
- let sourceSR;
1115
- let sourceExt;
1116
- itemTemplate.dependencies.some(id => {
1117
- const source = templateDictionary[id];
1118
- const sr = (0, generalHelpers_1.getProp)(source, "defaultSpatialReference");
1119
- /* istanbul ignore else */
1120
- if (!sourceSR && sr) {
1121
- sourceSR = sr;
1122
- }
1123
- const ext = (0, generalHelpers_1.getProp)(source, "defaultExtent");
1124
- /* istanbul ignore else */
1125
- if (!sourceExt && ext) {
1126
- sourceExt = ext;
1127
- }
1128
- return sourceSR && sourceExt;
1129
- });
1130
- const sourceWkid = (0, generalHelpers_1.getProp)(sourceSR, "wkid");
1131
- const viewWkid = (0, generalHelpers_1.getProp)(serviceInfo, "service.spatialReference.wkid");
1132
- /* istanbul ignore else */
1133
- if (sourceWkid && viewWkid && sourceWkid !== viewWkid) {
1134
- (0, generalHelpers_1.setCreateProp)(serviceInfo, "service.spatialReference", sourceSR);
1135
- }
1136
- const viewExt = (0, generalHelpers_1.getProp)(serviceInfo, "service.fullExtent");
1137
- /* istanbul ignore else */
1138
- if (sourceExt &&
1139
- viewExt &&
1140
- JSON.stringify(sourceExt) !== JSON.stringify(viewExt)) {
1141
- (0, generalHelpers_1.setCreateProp)(serviceInfo, "defaultExtent", sourceExt);
1142
- }
1143
- }
1144
- }
1145
- exports.validateSpatialReferenceAndExtent = validateSpatialReferenceAndExtent;
1146
- /**
1147
- * Updates a feature service with a list of layers and/or tables.
1148
- *
1149
- * @param itemTemplate
1150
- * @param fieldInfos Hash map of properties that contain field references
1151
- * @param popupInfos Hash map of a layers popupInfo
1152
- * @param adminLayerInfos Hash map of a layers adminLayerInfo
1153
- * @param templateDictionary Hash mapping Solution source id to id of its clone (and name & URL for feature service)
1154
- * @param authentication Credentials for the request
1155
- * @returns A promise that will resolve when the feature service has been updated
1156
- * @private
1157
- */
1158
- function updateLayerFieldReferences(itemTemplate, fieldInfos, popupInfos, adminLayerInfos, templateDictionary) {
1159
- return new Promise((resolveFn, rejectFn) => {
1160
- // Will need to do some post processing for fields
1161
- // to handle any potential field name changes when deploying to portal
1162
- postProcessFields(itemTemplate, fieldInfos, popupInfos, adminLayerInfos, templateDictionary).then((layerInfos) => {
1163
- // Update the items text with detemplatized popupInfo
1164
- updatePopupInfo(itemTemplate, layerInfos.popupInfos);
1165
- resolveFn({
1166
- itemTemplate,
1167
- layerInfos
1168
- });
1169
- }, e => rejectFn((0, generalHelpers_1.fail)(e)));
1170
- });
1171
- }
1172
- exports.updateLayerFieldReferences = updateLayerFieldReferences;
1173
- /**
1174
- * Update the names of fields for each layer or table after it has been
1175
- * added to the definition
1176
- *
1177
- * @param itemTemplate Item to be created
1178
- * @param layerInfos Hash map of properties that contain field references and various layer info
1179
- * @param popupInfos Hash map of a layers popupInfo
1180
- * @param adminLayerInfos Hash map of a layers adminLayerInfo
1181
- * @param templateDictionary
1182
- * @param authentication Credentials for the request
1183
- * @returns An object with detemplatized field references
1184
- * @private
1185
- */
1186
- function postProcessFields(itemTemplate, layerInfos, popupInfos, adminLayerInfos, templateDictionary) {
1187
- return new Promise((resolveFn, rejectFn) => {
1188
- if (!itemTemplate.item.url) {
1189
- rejectFn((0, generalHelpers_1.fail)("Feature layer " + itemTemplate.itemId + " does not have a URL"));
1190
- }
1191
- else {
1192
- const id = itemTemplate.itemId;
1193
- const settingsKeys = Object.keys(templateDictionary);
1194
- let templateInfo;
1195
- settingsKeys.some(k => {
1196
- if (templateDictionary[k].itemId === id) {
1197
- templateInfo = templateDictionary[k];
1198
- return true;
1199
- }
1200
- else {
1201
- return false;
1202
- }
1203
- });
1204
- // concat any layers and tables to process
1205
- const layers = itemTemplate.properties.layers;
1206
- const tables = itemTemplate.properties.tables;
1207
- const layersAndTables = layers.concat(tables);
1208
- // Set the newFields property for the layerInfos...this will contain all fields
1209
- // as they are after being added to the definition.
1210
- // This allows us to handle any potential field name changes after deploy to portal
1211
- layersAndTables.forEach((item) => {
1212
- // when deploying to portal "isView" is only set for create service and will fail when
1213
- // present on addToDef so this property is removed from item and we should check the templates service info
1214
- const isView = item.isView || itemTemplate.properties.service.isView;
1215
- /* istanbul ignore else */
1216
- if (layerInfos && layerInfos.hasOwnProperty(item.id)) {
1217
- const layerInfo = layerInfos[item.id];
1218
- layerInfo["isView"] = item.isView;
1219
- layerInfo["newFields"] = item.fields;
1220
- layerInfo["sourceSchemaChangesAllowed"] =
1221
- item.sourceSchemaChangesAllowed;
1222
- /* istanbul ignore else */
1223
- if (item.editFieldsInfo) {
1224
- // more than case change when deployed to protal so keep track of the new names
1225
- layerInfo["newEditFieldsInfo"] = JSON.parse(JSON.stringify(item.editFieldsInfo));
1226
- }
1227
- /* istanbul ignore else */
1228
- if (isView && templateInfo && templateDictionary.isPortal) {
1229
- // when the item is a view bring over the source service fields so we can compare the domains
1230
- layerInfo["sourceServiceFields"] = templateInfo.sourceServiceFields;
1231
- }
1232
- }
1233
- });
1234
- // Add the layerInfos to the settings object to be used while detemplatizing
1235
- settingsKeys.forEach((k) => {
1236
- if (id === templateDictionary[k].itemId) {
1237
- templateDictionary[k] = Object.assign(templateDictionary[k], getLayerSettings(layerInfos, templateDictionary[k].url, id));
1238
- }
1239
- });
1240
- // update the layerInfos object with current field names
1241
- resolveFn(deTemplatizeFieldInfos(layerInfos, popupInfos, adminLayerInfos, templateDictionary));
1242
- }
1243
- });
1244
- }
1245
- exports.postProcessFields = postProcessFields;
1246
- /**
1247
- * Add popup info back to the layer item
1248
- *
1249
- * @param itemTemplate
1250
- * @param popupInfos popup info to be added back to the layer
1251
- * @private
1252
- */
1253
- function updatePopupInfo(itemTemplate, popupInfos) {
1254
- ["layers", "tables"].forEach(type => {
1255
- const _items = (0, generalHelpers_1.getProp)(itemTemplate, "data." + type);
1256
- /* istanbul ignore else */
1257
- if (_items && Array.isArray(_items)) {
1258
- _items.forEach((item) => {
1259
- item.popupInfo = (0, generalHelpers_1.getProp)(popupInfos, type + "." + item.id) || {};
1260
- });
1261
- }
1262
- });
1263
- }
1264
- exports.updatePopupInfo = updatePopupInfo;
1265
- //#endregion
1266
- //#region Private helper functions --------------------------------------------------//
1267
- /**
1268
- * Helper function to templatize value and make sure its converted to lowercase
1269
- *
1270
- * @param basePath path used to de-templatize while deploying
1271
- * @param value to be converted to lower case for lookup while deploying
1272
- * @private
1273
- */
1274
- function _templatize(basePath, value, suffix) {
1275
- if (value.startsWith("{{")) {
1276
- return value;
1277
- }
1278
- else {
1279
- return String((0, templatization_1.templatizeTerm)(basePath, basePath, "." + String(value).toLowerCase() + (suffix ? "." + suffix : "")));
1280
- }
1281
- }
1282
- exports._templatize = _templatize;
1283
- /**
1284
- * templatize an objects property
1285
- *
1286
- * @param object the object with the property to templatize
1287
- * @param property the property of the object to templatize
1288
- * @param basePath path used to de-templatize while deploying
1289
- * @private
1290
- */
1291
- function _templatizeProperty(object, property, basePath, suffix) {
1292
- if (object && object.hasOwnProperty(property) && object[property]) {
1293
- object[property] = _templatize(basePath, object[property], suffix);
1294
- }
1295
- }
1296
- exports._templatizeProperty = _templatizeProperty;
1297
- /**
1298
- * Templatize field references, serviceItemId, and adminLayerInfo for a layer
1299
- *
1300
- * @param dataItem from the items data property
1301
- * @param adminItem from the services admin api
1302
- * @param itemTemplate Template for feature service item
1303
- * @param dependencies Array of IDependency for name mapping
1304
- * @param templatizeFieldReferences Templatize all field references within a layer
1305
- * @returns A promise that will resolve when template has been updated
1306
- * @private
1307
- */
1308
- function _templatizeLayer(dataItem, adminItem, itemTemplate, dependencies, templatizeFieldReferences, templateDictionary) {
1309
- // check for and repair common field issues
1310
- _validateFields(adminItem);
1311
- // Templatize all properties that contain field references
1312
- /* istanbul ignore else */
1313
- if (templatizeFieldReferences) {
1314
- _templatizeLayerFieldReferences(dataItem, itemTemplate.itemId, adminItem, dependencies);
1315
- }
1316
- const updates = [adminItem];
1317
- if (dataItem) {
1318
- updates.push(dataItem);
1319
- }
1320
- updates.forEach(update => {
1321
- if (update.hasOwnProperty("name")) {
1322
- // templatize the name but leave the current name as the optional default
1323
- update.name = (0, templatization_1.templatizeTerm)(update["serviceItemId"] + ".layer" + update.id, update["serviceItemId"] + ".layer" + update.id, ".name||" + update.name);
1324
- }
1325
- if (update.hasOwnProperty("extent")) {
1326
- update.extent = (0, templatization_1.templatizeTerm)(update["serviceItemId"], update["serviceItemId"], ".solutionExtent");
1327
- }
1328
- if (update.hasOwnProperty("serviceItemId")) {
1329
- update["serviceItemId"] = (0, templatization_1.templatizeTerm)(update["serviceItemId"], update["serviceItemId"], ".itemId");
1330
- }
1331
- if (update.hasOwnProperty("adminLayerInfo")) {
1332
- update.adminLayerInfo = _templatizeAdminLayerInfo(update, dependencies, templateDictionary);
1333
- }
1334
- });
1335
- }
1336
- exports._templatizeLayer = _templatizeLayer;
1337
- /**
1338
- * Repair common issues that can occur with feature service field references.
1339
- * This function will mutate the input item if any of the common issues have occured.
1340
- *
1341
- * @param adminItem layer or table from the service
1342
- */
1343
- function _validateFields(adminItem) {
1344
- const fieldNames = (adminItem.fields || []).map((f) => f.name);
1345
- // Update primary display field if field isn't in the layer.
1346
- _validateDisplayField(adminItem, fieldNames);
1347
- // Remove indexes on fields that don't exist in the layer.
1348
- // Remove duplicate indexes on the same field.
1349
- _validateIndexes(adminItem, fieldNames);
1350
- // Remove field references in templates when field doesn't exist in the layer.
1351
- _validateTemplatesFields(adminItem, fieldNames);
1352
- _validateTypesTemplates(adminItem, fieldNames);
1353
- // Repair editFieldsInfo if field referenced doesn't exist in the layer
1354
- _validateEditFieldsInfo(adminItem, fieldNames);
1355
- }
1356
- exports._validateFields = _validateFields;
1357
- /**
1358
- * Update primary display field if casing doesn't match.
1359
- * Update primary display field to the first non OID or GlobalId if the field isn't in the layer.
1360
- *
1361
- * @param adminItem layer or table from the service
1362
- * @param fieldNames string list of fields names
1363
- * @private
1364
- */
1365
- function _validateDisplayField(adminItem, fieldNames) {
1366
- const displayField = adminItem.displayField || "";
1367
- let i = -1;
1368
- if (fieldNames.some(name => {
1369
- i += 1;
1370
- return name === displayField || name === displayField.toLowerCase();
1371
- })) {
1372
- adminItem.displayField = fieldNames[i];
1373
- }
1374
- else {
1375
- // use the first non-OID non-globalId field we find
1376
- const skipFields = [];
1377
- const oidField = (0, generalHelpers_1.getProp)(adminItem, "uniqueIdField.name");
1378
- /* istanbul ignore else */
1379
- if (oidField) {
1380
- skipFields.push(oidField);
1381
- }
1382
- const globalIdField = (0, generalHelpers_1.getProp)(adminItem, "globalIdField");
1383
- /* istanbul ignore else */
1384
- if (globalIdField) {
1385
- skipFields.push(globalIdField);
1386
- }
1387
- fieldNames.some(name => {
1388
- if (skipFields.indexOf(name) === -1) {
1389
- adminItem.displayField = name;
1390
- return true;
1391
- }
1392
- else {
1393
- return false;
1394
- }
1395
- });
1396
- }
1397
- }
1398
- exports._validateDisplayField = _validateDisplayField;
1399
- /**
1400
- * Remove indexes on fields that don't exist in the layer.
1401
- * Remove duplicate indexes on the same field.
1402
- *
1403
- * @param adminItem layer or table from the service
1404
- * @param fieldNames string list of fields names
1405
- * @private
1406
- */
1407
- function _validateIndexes(adminItem, fieldNames) {
1408
- const indexes = adminItem.indexes;
1409
- /* istanbul ignore else */
1410
- if (indexes) {
1411
- const indexedFields = [];
1412
- adminItem.indexes = indexes.reduce((filtered, index) => {
1413
- const indexFields = index.fields.split(",");
1414
- const verifiedFields = [];
1415
- indexFields.forEach(indexField => {
1416
- /* istanbul ignore else */
1417
- if (indexedFields.indexOf(indexField) === -1) {
1418
- indexedFields.push(indexField);
1419
- // this is the first index with this field and it should be added if the field exists
1420
- /* istanbul ignore else */
1421
- if (fieldNames.indexOf(indexField) > -1) {
1422
- verifiedFields.push(indexField);
1423
- }
1424
- }
1425
- // else the field has more than one index associated and should not be returned
1426
- });
1427
- /* istanbul ignore else */
1428
- if (verifiedFields.length > 0) {
1429
- index.fields = verifiedFields.join(",");
1430
- filtered.push(index);
1431
- }
1432
- return filtered;
1433
- }, []);
1434
- }
1435
- }
1436
- exports._validateIndexes = _validateIndexes;
1437
- /**
1438
- * Remove field references from templates that no longer exist.
1439
- *
1440
- * @param adminItem layer or table from the service
1441
- * @param fieldNames string list of fields names
1442
- * @private
1443
- */
1444
- function _validateTemplatesFields(adminItem, fieldNames) {
1445
- const templates = adminItem.templates;
1446
- /* istanbul ignore else */
1447
- if (templates) {
1448
- adminItem.templates = templates.map(template => {
1449
- const attributes = (0, generalHelpers_1.getProp)(template, "prototype.attributes");
1450
- /* istanbul ignore else */
1451
- if (attributes) {
1452
- Object.keys(attributes).forEach(k => {
1453
- /* istanbul ignore else */
1454
- if (fieldNames.indexOf(k) === -1) {
1455
- delete attributes[k];
1456
- }
1457
- });
1458
- (0, generalHelpers_1.setProp)(template, "prototype.attributes", attributes);
1459
- }
1460
- return template;
1461
- });
1462
- }
1463
- }
1464
- exports._validateTemplatesFields = _validateTemplatesFields;
1465
- /**
1466
- * Remove field references from templates that no longer exist.
1467
- *
1468
- * @param adminItem layer or table from the service
1469
- * @param fieldNames string list of fields names
1470
- * @private
1471
- */
1472
- function _validateTypesTemplates(adminItem, fieldNames) {
1473
- const types = adminItem.types;
1474
- /* istanbul ignore else */
1475
- if (types) {
1476
- adminItem.types = types.map(t => {
1477
- _validateTemplatesFields(t, fieldNames);
1478
- return t;
1479
- });
1480
- }
1481
- }
1482
- exports._validateTypesTemplates = _validateTypesTemplates;
1483
- /**
1484
- * Check if edit feilds exist but with lower case
1485
- *
1486
- * @param adminItem layer or table from the service
1487
- * @param fieldNames string list of fields names
1488
- * @private
1489
- */
1490
- function _validateEditFieldsInfo(adminItem, fieldNames) {
1491
- const editFieldsInfo = adminItem.editFieldsInfo;
1492
- /* istanbul ignore else */
1493
- if (editFieldsInfo) {
1494
- const editFieldsInfoKeys = Object.keys(editFieldsInfo);
1495
- editFieldsInfoKeys.forEach(k => {
1496
- const editFieldName = editFieldsInfo[k];
1497
- /* istanbul ignore else */
1498
- if (editFieldName) {
1499
- fieldNames.some(name => {
1500
- if (name === editFieldName) {
1501
- return true;
1502
- }
1503
- else if (name === editFieldName.toLowerCase()) {
1504
- editFieldsInfo[k] = name;
1505
- return true;
1506
- }
1507
- else {
1508
- return false;
1509
- }
1510
- });
1511
- }
1512
- });
1513
- }
1514
- }
1515
- exports._validateEditFieldsInfo = _validateEditFieldsInfo;
1516
- /**
1517
- *
1518
- * Templatize all field references within a layer
1519
- * This is necessary to support potential field name changes when deploying to portal
1520
- * Portal will force all field names to be lower case
1521
- *
1522
- * @param dataItem The data layer instance with field name references within
1523
- * @param itemID The id for the item that contains this layer.
1524
- * @param layer JSON return from the layer being templatized.
1525
- * @param dependencies
1526
- * @returns An updated instance of the layer
1527
- * @private
1528
- */
1529
- function _templatizeLayerFieldReferences(dataItem, itemID, layer, dependencies) {
1530
- // This is the value that will be used as the template for adlib replacement
1531
- const path = itemID + ".layer" + layer.id + ".fields";
1532
- // Get the field names for various tests
1533
- const fieldNames = layer.fields.map((f) => f.name);
1534
- // Update the layer from the items data property
1535
- if (dataItem) {
1536
- _templatizeAdminLayerInfoFields(dataItem, dependencies);
1537
- _templatizePopupInfo(dataItem, layer, path, itemID, fieldNames);
1538
- }
1539
- // Update the layer
1540
- _templatizeAdminLayerInfoFields(layer, dependencies);
1541
- _templatizeRelationshipFields(layer, itemID);
1542
- _templatizeDefinitionEditor(layer, path, fieldNames);
1543
- _templatizeDefinitionExpression(layer, path, fieldNames);
1544
- _templatizeDrawingInfo(layer, path, fieldNames);
1545
- _templatizeTemplates(layer, path);
1546
- _templatizeTypeTemplates(layer, path);
1547
- _templatizeTimeInfo(layer, path);
1548
- _templatizeDefinitionQuery(layer, path, fieldNames);
1549
- }
1550
- exports._templatizeLayerFieldReferences = _templatizeLayerFieldReferences;
1551
- /**
1552
- * Templatize a layers adminLayerInfo by removing properties that will case issues with clone.
1553
- * Also templatizes the source service name when we are dealing with a view.
1554
- *
1555
- * @param layer The layer to be modified
1556
- * @param dependencies Array of service dependencies
1557
- * @returns A new copy of the modified adminLayerInfo for the given layer
1558
- * @private
1559
- */
1560
- function _templatizeAdminLayerInfo(layer, dependencies, templateDictionary) {
1561
- // Create new instance of adminLayerInfo to update for clone
1562
- const adminLayerInfo = Object.assign({}, layer.adminLayerInfo);
1563
- _updateGeomFieldName(adminLayerInfo, templateDictionary);
1564
- (0, generalHelpers_1.deleteProp)(adminLayerInfo, "xssTrustedFields");
1565
- (0, generalHelpers_1.deleteProp)(adminLayerInfo, "tableName");
1566
- // Remove unnecessary properties and templatize key properties from viewLayerDefinition
1567
- /* istanbul ignore else */
1568
- if (adminLayerInfo.viewLayerDefinition) {
1569
- const viewDef = Object.assign({}, adminLayerInfo.viewLayerDefinition);
1570
- _processAdminObject(viewDef, dependencies);
1571
- // Remove unnecessary properties and templatize key properties from viewLayerDefinition.table
1572
- /* istanbul ignore else */
1573
- if (viewDef.table) {
1574
- _processAdminObject(viewDef.table, dependencies);
1575
- /* istanbul ignore else */
1576
- if (viewDef.table.relatedTables) {
1577
- viewDef.table.relatedTables.forEach((table) => {
1578
- _processAdminObject(table, dependencies);
1579
- });
1580
- }
1581
- }
1582
- adminLayerInfo.viewLayerDefinition = viewDef;
1583
- }
1584
- return adminLayerInfo;
1585
- }
1586
- exports._templatizeAdminLayerInfo = _templatizeAdminLayerInfo;
1587
- /**
1588
- * Remove sourceId and templatize the sourceServiceName
1589
- *
1590
- * @param object The layer to be modified
1591
- * @param dependencies Array of service dependencies
1592
- * @private
1593
- */
1594
- function _processAdminObject(object, dependencies) {
1595
- (0, generalHelpers_1.deleteProp)(object, "sourceId");
1596
- if (object.hasOwnProperty("sourceServiceName")) {
1597
- object.sourceServiceName = _templatizeSourceServiceName(object.sourceServiceName, dependencies);
1598
- }
1599
- }
1600
- exports._processAdminObject = _processAdminObject;
1601
- /**
1602
- * Templatize the name based on the given dependencies
1603
- *
1604
- * @param lookupName The current name from the source service
1605
- * @param dependencies Array of IDependency for name mapping
1606
- * @returns The templatized name || undefined when no matching dependency is found
1607
- * @private
1608
- */
1609
- function _templatizeSourceServiceName(lookupName, dependencies) {
1610
- const deps = dependencies.filter(dependency => dependency.name === lookupName);
1611
- return deps.length === 1 ? _templatize(deps[0].id, "name") : undefined;
1612
- }
1613
- exports._templatizeSourceServiceName = _templatizeSourceServiceName;
1614
- /**
1615
- * templatize the fields referenced in adminLayerInfo
1616
- *
1617
- * @param layer the layer object with the adminLayerInfo property to templatize
1618
- * @param basePath path used to de-templatize while deploying
1619
- * @param itemID the id for the item that contains this layer
1620
- * @private
1621
- */
1622
- function _templatizeAdminLayerInfoFields(layer, dependencies) {
1623
- // templatize the source layer fields
1624
- const table = (0, generalHelpers_1.getProp)(layer, "adminLayerInfo.viewLayerDefinition.table");
1625
- if (table) {
1626
- let id = _getDependantItemId(table.sourceServiceName, dependencies);
1627
- const path = id + ".layer" + table.sourceLayerId + ".fields";
1628
- _templatizeAdminSourceLayerFields(table.sourceLayerFields || [], path);
1629
- // templatize the releated table fields
1630
- const relatedTables = (0, generalHelpers_1.getProp)(layer, "adminLayerInfo.viewLayerDefinition.table.relatedTables") || [];
1631
- if (relatedTables.length > 0) {
1632
- relatedTables.forEach((t) => {
1633
- id = _getDependantItemId(t.sourceServiceName, dependencies);
1634
- const relatedPath = id + ".layer" + t.sourceLayerId + ".fields";
1635
- _templatizeTopFilter(t.topFilter || {}, relatedPath);
1636
- _templatizeAdminSourceLayerFields(t.sourceLayerFields || [], relatedPath);
1637
- const parentKeyFields = t.parentKeyFields || [];
1638
- t.parentKeyFields = parentKeyFields.map((f) => {
1639
- return _templatize(path, f, "name");
1640
- });
1641
- const keyFields = t.keyFields || [];
1642
- t.keyFields = keyFields.map((f) => {
1643
- return _templatize(relatedPath, f, "name");
1644
- });
1645
- });
1646
- }
1647
- }
1648
- }
1649
- exports._templatizeAdminLayerInfoFields = _templatizeAdminLayerInfoFields;
1650
- /**
1651
- * find id based on dependency name
1652
- *
1653
- * @param lookupName name of dependency we want to find the id of
1654
- * @param dependencies array of item dependencies
1655
- * @private
1656
- */
1657
- function _getDependantItemId(lookupName, dependencies) {
1658
- const deps = dependencies.filter(dependency => dependency.name === lookupName);
1659
- return deps.length === 1 ? deps[0].id : "";
1660
- }
1661
- exports._getDependantItemId = _getDependantItemId;
1662
- /**
1663
- * templatize the sourceLayerFields referenced in adminLayerInfo
1664
- *
1665
- * @param fields array of sourceLayerFields to templatize
1666
- * @param basePath path used to de-templatize while deploying
1667
- * @private
1668
- */
1669
- function _templatizeAdminSourceLayerFields(fields, basePath) {
1670
- fields.forEach(f => _templatizeProperty(f, "source", basePath, "name"));
1671
- }
1672
- exports._templatizeAdminSourceLayerFields = _templatizeAdminSourceLayerFields;
1673
- /**
1674
- * templatize the topFilter property from adminLayerInfo related tables
1675
- *
1676
- * @param topFilter the topFilter object to templatize
1677
- * @param basePath path used to de-templatize while deploying
1678
- * @private
1679
- */
1680
- function _templatizeTopFilter(topFilter, basePath) {
1681
- /* istanbul ignore else */
1682
- if (topFilter) {
1683
- // templatize the orderByFields prop
1684
- const orderByFields = topFilter["orderByFields"] || "";
1685
- /* istanbul ignore else */
1686
- if (orderByFields !== "") {
1687
- const orderByField = orderByFields.split(" ")[0];
1688
- topFilter.orderByFields = topFilter.orderByFields.replace(orderByField, _templatize(basePath, orderByField, "name"));
1689
- }
1690
- const groupByFields = topFilter["groupByFields"] || "";
1691
- /* istanbul ignore else */
1692
- if (groupByFields !== "") {
1693
- const _groupByFields = groupByFields.split(",");
1694
- /* istanbul ignore else */
1695
- if (_groupByFields.length > 0) {
1696
- const mappedFields = _groupByFields.map((f) => {
1697
- return _templatize(basePath, f, "name");
1698
- });
1699
- topFilter.groupByFields = mappedFields.join(",");
1700
- }
1701
- }
1702
- }
1703
- }
1704
- exports._templatizeTopFilter = _templatizeTopFilter;
1705
- /**
1706
- * templatize the relationships key fields using the related table id in the basePath
1707
- *
1708
- * @param layer the layer that has the relationships to templatize
1709
- * @param itemID the id of the item that contains the related table
1710
- * @private
1711
- */
1712
- function _templatizeRelationshipFields(layer, itemID) {
1713
- if (layer && layer.relationships) {
1714
- const relationships = layer.relationships;
1715
- relationships.forEach(r => {
1716
- /* istanbul ignore else */
1717
- if (r.keyField) {
1718
- const basePath = itemID + ".layer" + layer.id + ".fields";
1719
- _templatizeProperty(r, "keyField", basePath, "name");
1720
- }
1721
- });
1722
- }
1723
- }
1724
- exports._templatizeRelationshipFields = _templatizeRelationshipFields;
1725
- /**
1726
- * templatize the popupInfo
1727
- *
1728
- * @param layerDefinition the layerDefinition that has the popupInfo to templatize
1729
- * @param layer the JSON for the layer being templatized
1730
- * @param basePath path used to de-templatize while deploying
1731
- * @param itemID the id for the item that contains this layer
1732
- * @param fieldNames array of fieldNames
1733
- * @private
1734
- */
1735
- function _templatizePopupInfo(layerDefinition, layer, basePath, itemID, fieldNames) {
1736
- // the data layer does not have the fields...will need to get those
1737
- // from the associated layer json
1738
- if (fieldNames && layerDefinition.popupInfo) {
1739
- const popupInfo = layerDefinition.popupInfo;
1740
- _templatizeName(popupInfo, "title", fieldNames, basePath);
1741
- _templatizeName(popupInfo, "description", fieldNames, basePath);
1742
- const fieldInfos = popupInfo.fieldInfos || [];
1743
- _templatizePopupInfoFieldInfos(fieldInfos, layer, itemID, basePath);
1744
- const expressionInfos = popupInfo.expressionInfos || [];
1745
- _templatizeExpressionInfos(expressionInfos, fieldNames, basePath);
1746
- const popupElements = popupInfo.popupElements || [];
1747
- _templatizePopupElements(popupElements, basePath, layer, itemID, fieldNames);
1748
- const mediaInfos = popupInfo.mediaInfos || [];
1749
- _templatizeMediaInfos(mediaInfos, fieldNames, basePath, layer, itemID);
1750
- }
1751
- }
1752
- exports._templatizePopupInfo = _templatizePopupInfo;
1753
- /**
1754
- * templatize field name when referenced like this: \{\{fieldName\}\}
1755
- * checks each field name from the layer
1756
- *
1757
- * @param object with the property to test for a field name
1758
- * @param property that could have a field name referenced
1759
- * @param fieldNames array for field names for the layer
1760
- * @param basePath path used to de-templatize while deploying
1761
- * @private
1762
- */
1763
- function _templatizeName(object, property, fieldNames, basePath) {
1764
- if (object.hasOwnProperty(property)) {
1765
- fieldNames.forEach(name => {
1766
- // Only test and replace instance of the name so any enclosing characters
1767
- // will be retained
1768
- const regEx = new RegExp("(\\b" + name + "\\b(?![}]{2}))", "gm");
1769
- if (regEx.test(object[property])) {
1770
- object[property] = object[property].replace(regEx, _templatize(basePath, name, "name"));
1771
- }
1772
- });
1773
- }
1774
- }
1775
- exports._templatizeName = _templatizeName;
1776
- /**
1777
- * templatize field name when referenced like this: \{\{fieldName\}\}
1778
- * checks each field name from the layer
1779
- *
1780
- * @param fieldInfos object that contains the popups fieldInfos
1781
- * @param layer json of layer being cloned
1782
- * @param itemID id of the item that contains the current layer
1783
- * @param basePath path used to de-templatize while deploying
1784
- * @private
1785
- */
1786
- function _templatizePopupInfoFieldInfos(fieldInfos, layer, itemID, basePath) {
1787
- fieldInfos.forEach((f) => {
1788
- f.fieldName = _templatizeFieldName(f.fieldName, layer, itemID, basePath);
1789
- });
1790
- }
1791
- exports._templatizePopupInfoFieldInfos = _templatizePopupInfoFieldInfos;
1792
- /**
1793
- * templatize field name when referenced like this: \{\{fieldName\}\}
1794
- * checks each field name from the layer
1795
- *
1796
- * @param name the field name to templatize
1797
- * @param layer json of layer being cloned
1798
- * @param itemID id of the item that contains the current layer
1799
- * @param basePath path used to de-templatize while deploying
1800
- * @private
1801
- */
1802
- function _templatizeFieldName(name, layer, itemID, basePath) {
1803
- if (name.indexOf("relationships/") > -1) {
1804
- const rels = name.split("/");
1805
- const relationshipId = rels[1];
1806
- const adminRelatedTables = (0, generalHelpers_1.getProp)(layer, "adminLayerInfo.viewLayerDefinition.table.relatedTables");
1807
- const relatedTables = layer.relationships || adminRelatedTables;
1808
- /* istanbul ignore else */
1809
- if (relatedTables && relatedTables.length > parseInt(relationshipId, 10)) {
1810
- const relatedTable = relatedTables[relationshipId];
1811
- // the layers relationships stores the property as relatedTableId
1812
- // the layers adminLayerInfo relatedTables stores the property as sourceLayerId
1813
- const prop = (0, generalHelpers_1.getProp)(relatedTable, "relatedTableId")
1814
- ? "relatedTableId"
1815
- : "sourceLayerId";
1816
- const _basePath = itemID + ".layer" + relatedTable[prop] + ".fields";
1817
- rels[2] = _templatize(_basePath, rels[2], "name");
1818
- name = rels.join("/");
1819
- }
1820
- }
1821
- else {
1822
- // do not need to templatize expression references as the expression
1823
- // itself will be templatized
1824
- if (name.indexOf("expression/") === -1) {
1825
- name = _templatize(basePath, name, "name");
1826
- }
1827
- }
1828
- return name;
1829
- }
1830
- exports._templatizeFieldName = _templatizeFieldName;
1831
- /**
1832
- * templatize field name when referenced in expressionInfos
1833
- *
1834
- * @param expressionInfos the popups expressionInfos to check
1835
- * @param fieldNames array of the layers field names
1836
- * @param basePath path used to de-templatize while deploying
1837
- * @private
1838
- */
1839
- function _templatizeExpressionInfos(expressionInfos, fieldNames, basePath) {
1840
- return expressionInfos.map((i) => {
1841
- fieldNames.forEach(name => {
1842
- i.expression = _templatizeArcadeExpressions(i.expression, name, basePath);
1843
- });
1844
- return i;
1845
- });
1846
- }
1847
- exports._templatizeExpressionInfos = _templatizeExpressionInfos;
1848
- /**
1849
- * templatize field name when referenced in popupElelments
1850
- *
1851
- * @param popupElelments the popups popupElelments to check
1852
- * @param basePath path used to de-templatize while deploying
1853
- * @param layer json of layer being cloned
1854
- * @param itemID id of the item that contains the current layer
1855
- * @param fieldNames array of field names
1856
- * @private
1857
- */
1858
- function _templatizePopupElements(popupElelments, basePath, layer, itemID, fieldNames) {
1859
- popupElelments.forEach((pe) => {
1860
- if (pe.hasOwnProperty("fieldInfos")) {
1861
- _templatizePopupInfoFieldInfos(pe.fieldInfos, layer, itemID, basePath);
1862
- }
1863
- if (pe.hasOwnProperty("mediaInfos")) {
1864
- _templatizeMediaInfos(pe.mediaInfos, fieldNames, basePath, layer, itemID);
1865
- }
1866
- });
1867
- }
1868
- exports._templatizePopupElements = _templatizePopupElements;
1869
- /**
1870
- * templatize field name when referenced in mediaInfos
1871
- *
1872
- * @param mediaInfos the popups mediaInfos to check
1873
- * @param fieldNames array of the layers field names
1874
- * @param basePath path used to de-templatize while deploying
1875
- * @param layer json of layer being cloned
1876
- * @param itemID id of the item that contains the current layer
1877
- * @private
1878
- */
1879
- function _templatizeMediaInfos(mediaInfos, fieldNames, basePath, layer, itemId) {
1880
- // templatize various properties of mediaInfos
1881
- const props = ["title", "caption"];
1882
- props.forEach(p => _templatizeName(mediaInfos, p, fieldNames, basePath));
1883
- mediaInfos.forEach((mi) => {
1884
- /* istanbul ignore else */
1885
- if (mi.hasOwnProperty("value")) {
1886
- const v = mi.value;
1887
- const vfields = v.fields || [];
1888
- v.fields = vfields.map(f => _templatizeFieldName(f, layer, itemId, basePath));
1889
- if (v.hasOwnProperty("normalizeField")) {
1890
- _templatizeProperty(v, "normalizeField", basePath, "name");
1891
- }
1892
- /* istanbul ignore else */
1893
- if (v.hasOwnProperty("tooltipField")) {
1894
- v.tooltipField = _templatizeFieldName(v.tooltipField, layer, itemId, basePath);
1895
- }
1896
- }
1897
- });
1898
- }
1899
- exports._templatizeMediaInfos = _templatizeMediaInfos;
1900
- /**
1901
- * templatize field names when referenced in definitionEditor
1902
- *
1903
- * @param layer the layer with the definition editor
1904
- * @param basePath path used to de-templatize while deploying
1905
- * @param fieldNames json of layer being cloned
1906
- * @private
1907
- */
1908
- function _templatizeDefinitionEditor(layer, basePath, fieldNames) {
1909
- if (layer) {
1910
- const defEditor = layer.definitionEditor || {};
1911
- /* istanbul ignore else */
1912
- if (defEditor) {
1913
- const inputs = defEditor.inputs;
1914
- if (inputs) {
1915
- inputs.forEach(i => {
1916
- /* istanbul ignore else */
1917
- if (i.parameters) {
1918
- i.parameters.forEach((p) => {
1919
- _templatizeProperty(p, "fieldName", basePath, "name");
1920
- });
1921
- }
1922
- });
1923
- }
1924
- if (defEditor.hasOwnProperty("parameterizedExpression")) {
1925
- defEditor.parameterizedExpression = _templatizeSimpleName(defEditor.parameterizedExpression || "", basePath, fieldNames, "name");
1926
- }
1927
- }
1928
- }
1929
- }
1930
- exports._templatizeDefinitionEditor = _templatizeDefinitionEditor;
1931
- /**
1932
- * templatize field names when referenced in definitionExpression
1933
- *
1934
- * @param layer the layer with the definition editor
1935
- * @param basePath path used to de-templatize while deploying
1936
- * @param fieldNames array of field names
1937
- * @private
1938
- */
1939
- function _templatizeDefinitionExpression(layer, basePath, fieldNames) {
1940
- if (layer && layer.hasOwnProperty("definitionExpression")) {
1941
- layer.definitionExpression = _templatizeSimpleName(layer.definitionExpression || "", basePath, fieldNames, "name");
1942
- }
1943
- }
1944
- exports._templatizeDefinitionExpression = _templatizeDefinitionExpression;
1945
- /**
1946
- * Case sensitive test for field names that appear anywhere within a string
1947
- *
1948
- * @param expression the expression to test for field name references
1949
- * @param basePath path used to de-templatize while deploying
1950
- * @param fieldNames array of the layers field names
1951
- * @private
1952
- */
1953
- function _templatizeSimpleName(expression, basePath, fieldNames, suffix) {
1954
- fieldNames.forEach(name => {
1955
- // look for the name but not if its followed by }}
1956
- const regEx = new RegExp("\\b" + name + "\\b(?![}]{2})", "gm");
1957
- if (expression && regEx.test(expression)) {
1958
- expression = expression.replace(regEx, _templatize(basePath, name, suffix));
1959
- }
1960
- });
1961
- return expression;
1962
- }
1963
- exports._templatizeSimpleName = _templatizeSimpleName;
1964
- /**
1965
- * Templatize field references within a layers drawingInfo
1966
- *
1967
- * @param layer the data layer
1968
- * @param basePath path used to de-templatize while deploying
1969
- * @param fieldNames array of the layers field names
1970
- * @private
1971
- */
1972
- function _templatizeDrawingInfo(layer, basePath, fieldNames) {
1973
- if (layer) {
1974
- const drawingInfo = layer.drawingInfo;
1975
- if (drawingInfo) {
1976
- // templatize the renderer fields
1977
- const renderer = drawingInfo.renderer || {};
1978
- _templatizeRenderer(renderer, basePath, fieldNames);
1979
- // templatize the labelingInfo
1980
- const labelingInfo = drawingInfo.labelingInfo || [];
1981
- _templatizeLabelingInfo(labelingInfo, basePath, fieldNames);
1982
- }
1983
- }
1984
- }
1985
- exports._templatizeDrawingInfo = _templatizeDrawingInfo;
1986
- /**
1987
- * Templatize field references within a layers drawingInfo
1988
- *
1989
- * @param renderer the layers renderer
1990
- * @param basePath path used to de-templatize while deploying
1991
- * @param fieldNames array of the layers field names
1992
- * @private
1993
- */
1994
- function _templatizeRenderer(renderer, basePath, fieldNames) {
1995
- switch (renderer.type) {
1996
- case "classBreaks":
1997
- case "uniqueValue":
1998
- case "predominance":
1999
- case "simple":
2000
- case "heatmap":
2001
- _templatizeGenRenderer(renderer, basePath, fieldNames);
2002
- break;
2003
- case "temporal":
2004
- _templatizeTemporalRenderer(renderer, basePath, fieldNames);
2005
- break;
2006
- default:
2007
- break;
2008
- }
2009
- }
2010
- exports._templatizeRenderer = _templatizeRenderer;
2011
- /**
2012
- * Templatize field references within a layers renderer
2013
- *
2014
- * @param renderer the renderer object to check for field references
2015
- * @param basePath path used to de-templatize while deploying
2016
- * @param fieldNames array of field names that will be used to search expressions
2017
- * @private
2018
- */
2019
- function _templatizeGenRenderer(renderer, basePath, fieldNames) {
2020
- /* istanbul ignore else */
2021
- if (renderer) {
2022
- // update authoringInfo
2023
- const authoringInfo = renderer.authoringInfo;
2024
- if (authoringInfo) {
2025
- _templatizeAuthoringInfo(authoringInfo, basePath, fieldNames);
2026
- }
2027
- const props = ["field", "normalizationField"];
2028
- props.forEach(p => _templatizeProperty(renderer, p, basePath, "name"));
2029
- const fieldNameProps = ["field1", "field2", "field3"];
2030
- fieldNameProps.forEach(fnP => _templatizeProperty(renderer, fnP, basePath, "name"));
2031
- // When an attribute name is specified, it's enclosed in square brackets
2032
- const rExp = renderer.rotationExpression;
2033
- if (rExp) {
2034
- fieldNames.forEach(name => {
2035
- const regEx = new RegExp("(\\[" + name + "\\])", "gm");
2036
- if (regEx.test(rExp)) {
2037
- renderer.rotationExpression = rExp.replace(regEx, "[" + _templatize(basePath, name, "name") + "]");
2038
- }
2039
- });
2040
- }
2041
- // update valueExpression
2042
- if (renderer.valueExpression) {
2043
- fieldNames.forEach(name => {
2044
- renderer.valueExpression = _templatizeArcadeExpressions(renderer.valueExpression, name, basePath);
2045
- });
2046
- }
2047
- // update visualVariables
2048
- const visualVariables = renderer.visualVariables;
2049
- if (visualVariables) {
2050
- visualVariables.forEach(v => {
2051
- props.forEach(p => _templatizeProperty(v, p, basePath, "name"));
2052
- if (v.valueExpression) {
2053
- fieldNames.forEach(name => {
2054
- v.valueExpression = _templatizeArcadeExpressions(v.valueExpression, name, basePath);
2055
- });
2056
- }
2057
- });
2058
- }
2059
- }
2060
- }
2061
- exports._templatizeGenRenderer = _templatizeGenRenderer;
2062
- /**
2063
- * Templatize field references within a layers renderer
2064
- *
2065
- * @param renderer the renderer object to check for field references
2066
- * @param basePath path used to de-templatize while deploying
2067
- * @param fieldNames array of field names that will be used to search expressions
2068
- * @private
2069
- */
2070
- function _templatizeTemporalRenderer(renderer, basePath, fieldNames) {
2071
- const renderers = [
2072
- renderer.latestObservationRenderer,
2073
- renderer.observationRenderer,
2074
- renderer.trackRenderer
2075
- ];
2076
- renderers.forEach(r => {
2077
- _templatizeRenderer(r, basePath, fieldNames);
2078
- });
2079
- }
2080
- exports._templatizeTemporalRenderer = _templatizeTemporalRenderer;
2081
- /**
2082
- * Templatize renderers authoringInfo
2083
- *
2084
- * @param authoringInfo object containing metadata about the authoring process
2085
- * @param basePath path used to de-templatize while deploying
2086
- * @param fieldNames the name of fields from the layer
2087
- * @private
2088
- */
2089
- function _templatizeAuthoringInfo(authoringInfo, basePath, fieldNames) {
2090
- /* istanbul ignore else */
2091
- if (authoringInfo) {
2092
- const props = ["field", "normalizationField"];
2093
- const field1 = authoringInfo.field1;
2094
- props.forEach(p => _templatizeProperty(field1, p, basePath, "name"));
2095
- const field2 = authoringInfo.field2;
2096
- props.forEach(p => _templatizeProperty(field2, p, basePath, "name"));
2097
- const fields = authoringInfo.fields;
2098
- if (fields) {
2099
- authoringInfo.fields = fields.map(f => _templatize(basePath, f, "name"));
2100
- }
2101
- const vProps = ["endTime", "field", "startTime"];
2102
- const vVars = authoringInfo.visualVariables;
2103
- if (vVars) {
2104
- vProps.forEach(p => {
2105
- // endTime and startTime may or may not be a field name
2106
- if (fieldNames.indexOf(vVars[p]) > -1) {
2107
- _templatizeProperty(vVars, p, basePath, "name");
2108
- }
2109
- });
2110
- }
2111
- }
2112
- }
2113
- exports._templatizeAuthoringInfo = _templatizeAuthoringInfo;
2114
- /**
2115
- * Templatize field references within an arcade expression
2116
- *
2117
- * @param text the text that contains the expression
2118
- * @param fieldName name of the field to test for
2119
- * @param basePath path used to de-templatize while deploying
2120
- * @private
2121
- */
2122
- function _templatizeArcadeExpressions(text, fieldName, basePath) {
2123
- const t = _templatize(basePath, fieldName, "name");
2124
- if (text) {
2125
- // test for $feature. notation
2126
- // captures VOTED_DEM_2012 from $feature.VOTED_DEM_2012
2127
- let exp = "(?:\\$feature\\.)(" + fieldName + ")\\b";
2128
- let regEx = new RegExp(exp, "gm");
2129
- text = regEx.test(text) ? text.replace(regEx, "$feature." + t) : text;
2130
- // test for $feature[] notation
2131
- // captures VOTED_DEM_2012 from $feature["VOTED_DEM_2012"]
2132
- // captures VOTED_DEM_2012 from $feature['VOTED_DEM_2012']
2133
- // captures VOTED_DEM_2012 from $feature[VOTED_DEM_2012]
2134
- exp = "(?:[$]feature)(\\[\\\"?\\'?)" + fieldName + "(\\\"?\\'?\\])";
2135
- regEx = new RegExp(exp, "gm");
2136
- let result = regEx.exec(text);
2137
- if (result) {
2138
- text = text.replace(regEx, "$feature" + result[1] + t + result[2]);
2139
- }
2140
- // test for $feature[] with join case
2141
- // captures VOTED_DEM_2016 from $feature["COUNTY_ID.VOTED_DEM_2016"]
2142
- exp =
2143
- "(?:[$]feature)(\\[\\\"?\\'?)(\\w+)[.]" + fieldName + "(\\\"?\\'?\\])";
2144
- regEx = new RegExp(exp, "gm");
2145
- result = regEx.exec(text);
2146
- if (result && result.length > 3) {
2147
- // TODO result[2] is the table name...this needs to be templatized as well
2148
- text = text.replace(regEx, "$feature" + result[1] + result[2] + "." + t + result[3]);
2149
- }
2150
- // test for "fieldName"
2151
- // captures fieldName from "var names = ["fieldName", "fieldName2"]..."
2152
- // captures fieldName from "var names = ['fieldName', 'fieldName2']..."
2153
- exp = "(\\\"|\\')+" + fieldName + "(\\\"|\\')+";
2154
- regEx = new RegExp(exp, "gm");
2155
- result = regEx.exec(text);
2156
- if (result) {
2157
- text = text.replace(regEx, result[1] + t + result[2]);
2158
- }
2159
- }
2160
- return text;
2161
- }
2162
- exports._templatizeArcadeExpressions = _templatizeArcadeExpressions;
2163
- /**
2164
- * templatize field names when referenced in the layers labelingInfo
2165
- *
2166
- * @param labelingInfo the object that contains the labelingInfo
2167
- * @param basePath path used to de-templatize while deploying
2168
- * @param fieldNames array of the layers field names
2169
- * @private
2170
- */
2171
- function _templatizeLabelingInfo(labelingInfo, basePath, fieldNames) {
2172
- labelingInfo.forEach((li) => {
2173
- /* istanbul ignore else */
2174
- if (li.hasOwnProperty("fieldInfos")) {
2175
- const fieldInfos = li.fieldInfos || [];
2176
- fieldInfos.forEach(fi => _templatizeProperty(fi, "fieldName", basePath, "name"));
2177
- }
2178
- const labelExp = li.labelExpression || "";
2179
- const labelExpInfo = li.labelExpressionInfo || {};
2180
- fieldNames.forEach(n => {
2181
- const t = _templatize(basePath, n, "name");
2182
- // check for [fieldName] or ["fieldName"]
2183
- const regExBracket = new RegExp('(\\[\\"*)+(' + n + ')(\\"*\\])+', "gm");
2184
- let result = regExBracket.exec(labelExp);
2185
- if (result) {
2186
- li.labelExpression = labelExp.replace(regExBracket, result[1] + t + result[3]);
2187
- }
2188
- /* istanbul ignore else */
2189
- if (labelExpInfo.value) {
2190
- let v = labelExpInfo.value;
2191
- // check for {fieldName}
2192
- const regExCurly = new RegExp("(\\{" + n + "\\})", "gm");
2193
- v = regExCurly.test(v) ? v.replace(regExCurly, "{" + t + "}") : v;
2194
- // check for [fieldName] or ["fieldName"]
2195
- result = regExBracket.exec(v);
2196
- v = result ? v.replace(regExBracket, result[1] + t + result[3]) : v;
2197
- li.labelExpressionInfo.value = v;
2198
- }
2199
- /* istanbul ignore else */
2200
- if (labelExpInfo.expression) {
2201
- li.labelExpressionInfo.expression = _templatizeArcadeExpressions(labelExpInfo.expression, n, basePath);
2202
- }
2203
- });
2204
- });
2205
- }
2206
- exports._templatizeLabelingInfo = _templatizeLabelingInfo;
2207
- /**
2208
- * templatize the layers editing templates
2209
- *
2210
- * @param layer the data layer being cloned
2211
- * @param basePath path used to de-templatize while deploying
2212
- * @private
2213
- */
2214
- function _templatizeTemplates(layer, basePath) {
2215
- const templates = layer.templates || [];
2216
- templates.forEach(t => {
2217
- const attributes = (0, generalHelpers_1.getProp)(t, "prototype.attributes");
2218
- const _attributes = _templatizeKeys(attributes, basePath, "name");
2219
- /* istanbul ignore else */
2220
- if (_attributes) {
2221
- t.prototype.attributes = _attributes;
2222
- }
2223
- });
2224
- }
2225
- exports._templatizeTemplates = _templatizeTemplates;
2226
- /**
2227
- * templatize the layer types and templates
2228
- *
2229
- * @param layer the data layer being cloned
2230
- * @param basePath path used to de-templatize while deploying
2231
- * @private
2232
- */
2233
- function _templatizeTypeTemplates(layer, basePath) {
2234
- const types = layer.types;
2235
- if (types && Array.isArray(types) && types.length > 0) {
2236
- types.forEach((type) => {
2237
- const domains = _templatizeKeys(type.domains, basePath, "name");
2238
- /* istanbul ignore else */
2239
- if (domains) {
2240
- type.domains = domains;
2241
- }
2242
- const templates = type.templates;
2243
- /* istanbul ignore else */
2244
- if (templates && templates.length > 0) {
2245
- templates.forEach((t) => {
2246
- const attributes = (0, generalHelpers_1.getProp)(t, "prototype.attributes");
2247
- const _attributes = _templatizeKeys(attributes, basePath, "name");
2248
- /* istanbul ignore else */
2249
- if (_attributes) {
2250
- t.prototype.attributes = _attributes;
2251
- }
2252
- });
2253
- }
2254
- });
2255
- }
2256
- }
2257
- exports._templatizeTypeTemplates = _templatizeTypeTemplates;
2258
- /**
2259
- * templatize object keys
2260
- *
2261
- * @param obj the object to templatize
2262
- * @param basePath path used to de-templatize while deploying
2263
- * @param suffix expected suffix for template variable
2264
- * @private
2265
- */
2266
- function _templatizeKeys(obj, basePath, suffix) {
2267
- let _obj;
2268
- /* istanbul ignore else */
2269
- if (obj) {
2270
- _obj = {};
2271
- const objKeys = Object.keys(obj);
2272
- /* istanbul ignore else */
2273
- if (objKeys && objKeys.length > 0) {
2274
- objKeys.forEach(k => {
2275
- _obj[_templatize(basePath, k, suffix)] = obj[k];
2276
- });
2277
- }
2278
- }
2279
- return _obj;
2280
- }
2281
- exports._templatizeKeys = _templatizeKeys;
2282
- /**
2283
- * templatize fields referenced in the layers time info
2284
- *
2285
- * @param layer the data layer being cloned
2286
- * @param basePath path used to de-templatize while deploying
2287
- * @private
2288
- */
2289
- function _templatizeTimeInfo(layer, basePath) {
2290
- if (layer.timeInfo) {
2291
- const timeInfo = layer.timeInfo;
2292
- const timeProps = [
2293
- "endTimeField",
2294
- "startTimeField",
2295
- "trackIdField"
2296
- ];
2297
- timeProps.forEach(t => {
2298
- if (timeInfo[t] !== "") {
2299
- _templatizeProperty(timeInfo, t, basePath, "name");
2300
- }
2301
- else {
2302
- timeInfo[t] = null;
2303
- }
2304
- });
2305
- }
2306
- }
2307
- exports._templatizeTimeInfo = _templatizeTimeInfo;
2308
- /**
2309
- * templatize the layers definition query
2310
- *
2311
- * @param layer the data layer being cloned
2312
- * @param basePath path used to de-templatize while deploying
2313
- * @param fieldNames array of the layers field names
2314
- * @private
2315
- */
2316
- function _templatizeDefinitionQuery(layer, basePath, fieldNames) {
2317
- // templatize view definition query
2318
- if (layer && layer.hasOwnProperty("viewDefinitionQuery")) {
2319
- layer.viewDefinitionQuery = _templatizeSimpleName(layer.viewDefinitionQuery || "", basePath, fieldNames, "name");
2320
- }
2321
- if (layer && layer.hasOwnProperty("definitionQuery")) {
2322
- layer.definitionQuery = _templatizeSimpleName(layer.definitionQuery || "", basePath, fieldNames, "name");
2323
- }
2324
- }
2325
- exports._templatizeDefinitionQuery = _templatizeDefinitionQuery;
2326
- /**
2327
- * Helper function to create the name mapping used to
2328
- * de-templatize the field reference
2329
- *
2330
- * @param fieldInfos the object that stores the cached information
2331
- * @param id the id for the current layer being processed
2332
- * @private
2333
- */
2334
- function _getNameMapping(fieldInfos, id) {
2335
- // create name mapping
2336
- const fInfo = fieldInfos[id];
2337
- const nameMapping = {};
2338
- const newFields = fInfo.newFields;
2339
- const newFieldNames = newFields
2340
- ? newFields.map((f) => f.name)
2341
- : [];
2342
- const sourceFields = fInfo.sourceFields || [];
2343
- sourceFields.forEach((field) => {
2344
- const lName = String(field.name).toLowerCase();
2345
- newFields.forEach((f) => {
2346
- // Names can change more than case
2347
- if (newFieldNames.indexOf(field.name) === -1 &&
2348
- newFieldNames.indexOf(lName) === -1) {
2349
- // If both new (f) and source (field) aliases are defined and are equal, map the source name to the new name
2350
- if (f.alias && f.alias === field.alias) {
2351
- nameMapping[lName] = {
2352
- name: f.name,
2353
- alias: f.alias,
2354
- type: f.type ? f.type : ""
2355
- };
2356
- }
2357
- }
2358
- if (String(f.name).toLowerCase() === lName) {
2359
- nameMapping[lName] = {
2360
- name: f.name,
2361
- alias: f.alias ? f.alias : "",
2362
- type: f.type ? f.type : ""
2363
- };
2364
- }
2365
- });
2366
- });
2367
- // update for editFieldsInfo
2368
- if (fInfo.editFieldsInfo && fInfo.newEditFieldsInfo) {
2369
- const efi = JSON.parse(JSON.stringify(fInfo.editFieldsInfo));
2370
- const newEfi = JSON.parse(JSON.stringify(fInfo.newEditFieldsInfo));
2371
- const nameMappingKeys = Object.keys(nameMapping);
2372
- Object.keys(efi).forEach(k => {
2373
- const lowerEfi = String(efi[k]).toLowerCase();
2374
- if ((nameMappingKeys.indexOf(lowerEfi) === -1 ||
2375
- nameMapping[lowerEfi].name !== newEfi[k]) &&
2376
- newFieldNames.indexOf(lowerEfi) > -1) {
2377
- // Only add delete fields if source schema changes allowed
2378
- /* istanbul ignore else */
2379
- if (fInfo.sourceSchemaChangesAllowed && !fInfo.isView) {
2380
- /* istanbul ignore else */
2381
- if (!fInfo.hasOwnProperty("deleteFields")) {
2382
- fInfo.deleteFields = [];
2383
- }
2384
- // This issue only occurs on portal so we
2385
- // need to delete the lcase version of the field
2386
- fInfo.deleteFields.push(lowerEfi);
2387
- }
2388
- // editFieldsInfo only has the name and not the alias and type
2389
- let sourceEfiField;
2390
- fInfo.sourceFields.some((sf) => {
2391
- if (sf.name === efi[k]) {
2392
- sourceEfiField = sf;
2393
- }
2394
- return sf.name === efi[k];
2395
- });
2396
- nameMapping[lowerEfi] = {
2397
- name: newEfi[k],
2398
- alias: sourceEfiField && sourceEfiField.alias ? sourceEfiField.alias : "",
2399
- type: sourceEfiField && sourceEfiField.type ? sourceEfiField.type : ""
2400
- };
2401
- }
2402
- });
2403
- (0, generalHelpers_1.deleteProp)(fInfo, "sourceSchemaChangesAllowed");
2404
- (0, generalHelpers_1.deleteProp)(fInfo, "editFieldsInfo");
2405
- (0, generalHelpers_1.deleteProp)(fInfo, "newEditFieldsInfo");
2406
- (0, generalHelpers_1.deleteProp)(fInfo, "isView");
2407
- }
2408
- return nameMapping;
2409
- }
2410
- exports._getNameMapping = _getNameMapping;
2411
- /**
2412
- * Helper function to ensure same chunk size value is used in multiple locations
2413
- *
2414
- * @returns a number that represents how many layers should be included per addToDef call
2415
- * @private
2416
- */
2417
- function _getLayerChunkSize() {
2418
- return 20;
2419
- }
2420
- exports._getLayerChunkSize = _getLayerChunkSize;
1
+ "use strict";
2
+ /** @license
3
+ * Copyright 2019 Esri
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports._processAdminObject = exports._templatizeAdminLayerInfo = exports._templatizeLayerFieldReferences = exports._validateEditFieldsInfo = exports._validateTypesTemplates = exports._validateTemplatesFields = exports._validateIndexes = exports._validateDisplayField = exports._validateFields = exports._templatizeLayer = exports._templatizeProperty = exports._templatize = exports.updatePopupInfo = exports.postProcessFields = exports.updateLayerFieldReferences = exports.validateSpatialReferenceAndExtent = exports.setDefaultSpatialReference = exports._updateTemplateDictionaryFields = exports._updateGeomFieldName = exports._updateSourceLayerFields = exports._updateItemFields = exports._getDynamicFieldNames = exports._getFieldNames = exports._updateForPortal = exports.removeLayerOptimization = exports._isSelfReferential = exports._updateAddOptions = exports._updateOrder = exports.addFeatureServiceDefinition = exports.addFeatureServiceLayersAndTables = exports.getExistingLayersAndTables = exports.getLayersAndTables = exports.deTemplatizeFieldInfos = exports.processContingentValues = exports.updateTemplateForInvalidDesignations = exports.updateSettingsFieldInfos = exports.setNamesAndTitles = exports.getLayerSettings = exports._updateTypeKeywords = exports.updateTemplate = exports.cacheLayerInfo = exports._cachePopupInfo = exports.cachePopupInfos = exports._cacheFieldInfo = exports.cacheContingentValues = exports.cacheFieldInfos = exports.deleteViewProps = exports.templatize = exports.rest_addFeatures = exports.rest_queryFeatures = void 0;
19
+ exports._getLayerChunkSize = exports._getNameMapping = exports._templatizeDefinitionQuery = exports._templatizeTimeInfo = exports._templatizeKeys = exports._templatizeTypeTemplates = exports._templatizeTemplates = exports._templatizeLabelingInfo = exports._templatizeArcadeExpressions = exports._templatizeAuthoringInfo = exports._templatizeTemporalRenderer = exports._templatizeGenRenderer = exports._templatizeRenderer = exports._templatizeDrawingInfo = exports._templatizeSimpleName = exports._templatizeDefinitionExpression = exports._templatizeDefinitionEditor = exports._templatizeMediaInfos = exports._templatizePopupElements = exports._templatizeExpressionInfos = exports._templatizeFieldName = exports._templatizePopupInfoFieldInfos = exports._templatizeName = exports._templatizePopupInfo = exports._templatizeRelationshipFields = exports._templatizeTopFilter = exports._templatizeAdminSourceLayerFields = exports._getDependantItemId = exports._templatizeAdminLayerInfoFields = exports._templatizeSourceServiceName = void 0;
20
+ /**
21
+ * Provides general helper functions.
22
+ *
23
+ * @module featureServiceHelpers
24
+ */
25
+ // ------------------------------------------------------------------------------------------------------------------ //
26
+ var arcgis_rest_feature_layer_1 = require("@esri/arcgis-rest-feature-layer");
27
+ Object.defineProperty(exports, "rest_queryFeatures", { enumerable: true, get: function () { return arcgis_rest_feature_layer_1.queryFeatures; } });
28
+ Object.defineProperty(exports, "rest_addFeatures", { enumerable: true, get: function () { return arcgis_rest_feature_layer_1.addFeatures; } });
29
+ //#region Imports -------------------------------------------------------------------------------------------------------//
30
+ const interfaces_1 = require("./interfaces");
31
+ const generalHelpers_1 = require("./generalHelpers");
32
+ const templatization_1 = require("./templatization");
33
+ const restHelpers_1 = require("./restHelpers");
34
+ const trackingHelpers_1 = require("./trackingHelpers");
35
+ //#endregion ------------------------------------------------------------------------------------------------------------//
36
+ //#region Public functions ----------------------------------------------------------------------------------------------//
37
+ /**
38
+ * Templatize the ID, url, field references ect
39
+ *
40
+ * @param itemTemplate Template for feature service item
41
+ * @param dependencies Array of IDependency for name mapping
42
+ * @param templatizeFieldReferences Templatize all field references within a layer
43
+ * @param templateDictionary Hash mapping property names to replacement values
44
+ * @returns A promise that will resolve when template has been updated
45
+ * @private
46
+ */
47
+ function templatize(itemTemplate, dependencies, templatizeFieldReferences, templateDictionary) {
48
+ templateDictionary = templateDictionary || {};
49
+ // Common templatizations
50
+ const id = itemTemplate.item.id;
51
+ const fsUrl = itemTemplate.item.url;
52
+ itemTemplate.item = {
53
+ ...itemTemplate.item,
54
+ id: (0, templatization_1.templatizeTerm)(id, id, ".itemId"),
55
+ url: _templatize(id, "url"),
56
+ typeKeywords: (0, templatization_1.templatizeIds)(itemTemplate.item.typeKeywords)
57
+ };
58
+ // special handeling if we are dealing with a tracker view
59
+ (0, trackingHelpers_1.templatizeTracker)(itemTemplate);
60
+ // added for issue #928
61
+ (0, generalHelpers_1.deleteProp)(itemTemplate, "properties.service.size");
62
+ const jsonLayers = itemTemplate.properties.layers || [];
63
+ const jsonTables = itemTemplate.properties.tables || [];
64
+ const jsonItems = jsonLayers.concat(jsonTables);
65
+ const data = itemTemplate.data || {};
66
+ const layers = data.layers || [];
67
+ const tables = data.tables || [];
68
+ const _items = layers.concat(tables);
69
+ // Set up symbols for the URL of the feature service and its layers and tables
70
+ templateDictionary[fsUrl] = itemTemplate.item.url; // map FS URL to its templatized form
71
+ jsonItems.concat(_items).forEach(layer => {
72
+ templateDictionary[fsUrl + "/" + layer.id] = _templatize(id, "layer" + layer.id + ".url");
73
+ });
74
+ // templatize the service references serviceItemId
75
+ itemTemplate.properties.service.serviceItemId = (0, templatization_1.templatizeTerm)(itemTemplate.properties.service.serviceItemId, itemTemplate.properties.service.serviceItemId, ".itemId");
76
+ const initialExtent = (0, generalHelpers_1.getProp)(itemTemplate, "properties.service.initialExtent");
77
+ /* istanbul ignore else */
78
+ if (initialExtent) {
79
+ itemTemplate.properties.service.initialExtent = (0, templatization_1.templatizeTerm)(id, id, ".solutionExtent");
80
+ }
81
+ const fullExtent = (0, generalHelpers_1.getProp)(itemTemplate, "properties.service.fullExtent");
82
+ /* istanbul ignore else */
83
+ if (fullExtent) {
84
+ itemTemplate.properties.service.fullExtent = (0, templatization_1.templatizeTerm)(id, id, ".solutionExtent");
85
+ }
86
+ // this default extent will be used in cases where it does not make sense to apply the orgs
87
+ // extent to a service with a local spatial reference
88
+ itemTemplate.properties.defaultExtent = initialExtent || fullExtent;
89
+ // in some cases a service does not have a spatial reference defined
90
+ // added for issue #699
91
+ if (!(0, generalHelpers_1.getProp)(itemTemplate, "properties.service.spatialReference") &&
92
+ (0, generalHelpers_1.getProp)(itemTemplate, "properties.defaultExtent.spatialReference")) {
93
+ (0, generalHelpers_1.setCreateProp)(itemTemplate, "properties.service.spatialReference", itemTemplate.properties.defaultExtent.spatialReference);
94
+ }
95
+ // if any layer hasZ enabled then we need to set
96
+ // enableZDefaults and zDefault to deploy to enterprise
97
+ let hasZ = false;
98
+ jsonItems.forEach((jsonItem) => {
99
+ // get the source service json for the given data item
100
+ const matchingItems = _items.filter(item => {
101
+ return jsonItem.id === item.id;
102
+ });
103
+ // templatize the source service json
104
+ const _item = matchingItems.length === 1 ? matchingItems[0] : undefined;
105
+ _templatizeLayer(_item, jsonItem, itemTemplate, dependencies, templatizeFieldReferences, templateDictionary);
106
+ hasZ = jsonItem.hasZ || (_item && _item.hasZ) ? true : hasZ;
107
+ });
108
+ if (hasZ) {
109
+ itemTemplate.properties.service.enableZDefaults = true;
110
+ itemTemplate.properties.service.zDefault = 0;
111
+ }
112
+ return itemTemplate;
113
+ }
114
+ exports.templatize = templatize;
115
+ /**
116
+ * Delete key properties that are system managed
117
+ *
118
+ * @param layer The data layer instance with field name references within
119
+ */
120
+ function deleteViewProps(layer) {
121
+ const props = ["definitionQuery"];
122
+ props.forEach(prop => {
123
+ (0, generalHelpers_1.deleteProp)(layer, prop);
124
+ });
125
+ }
126
+ exports.deleteViewProps = deleteViewProps;
127
+ /**
128
+ * Cache properties that contain field references
129
+ *
130
+ * removeProp added for issue #644
131
+ * setting all props on add for online now
132
+ * investigating if we can also just allow them to be set during add for portal
133
+ *
134
+ * @param layer The data layer instance with field name references within
135
+ * @param fieldInfos the object that stores the cached field infos
136
+ * @returns An updated instance of the fieldInfos
137
+ */
138
+ function cacheFieldInfos(layer, fieldInfos) {
139
+ // cache the source fields as they are in the original source
140
+ if (layer && layer.fields) {
141
+ fieldInfos[layer.id] = {
142
+ sourceFields: JSON.parse(JSON.stringify(layer.fields)),
143
+ type: layer.type,
144
+ id: layer.id
145
+ };
146
+ }
147
+ // cache each of these properties as they each can contain field references
148
+ // and will have associated updateDefinition calls when deploying to portal
149
+ // as well as online for relationships...as relationships added with addToDef will cause failure
150
+ const props = {
151
+ editFieldsInfo: false,
152
+ types: false,
153
+ templates: false,
154
+ relationships: true,
155
+ drawingInfo: false,
156
+ timeInfo: false,
157
+ viewDefinitionQuery: false
158
+ };
159
+ Object.keys(props).forEach(k => {
160
+ _cacheFieldInfo(layer, k, fieldInfos, props[k]);
161
+ });
162
+ return fieldInfos;
163
+ }
164
+ exports.cacheFieldInfos = cacheFieldInfos;
165
+ /**
166
+ * Cache the stored contingent values so we can add them in subsequent addToDef calls
167
+ *
168
+ * @param id The layer id for the associated values to be stored with
169
+ * @param fieldInfos The object that stores the cached field infos
170
+ * @param itemTemplate The current itemTemplate being processed
171
+ * @returns An updated instance of the fieldInfos
172
+ */
173
+ function cacheContingentValues(id, fieldInfos, itemTemplate) {
174
+ const contingentValues = (0, generalHelpers_1.getProp)(itemTemplate, 'properties.contingentValues');
175
+ if (contingentValues && contingentValues[id]) {
176
+ fieldInfos[id]['contingentValues'] = contingentValues[id];
177
+ }
178
+ return fieldInfos;
179
+ }
180
+ exports.cacheContingentValues = cacheContingentValues;
181
+ /**
182
+ * Helper function to cache a single property into the fieldInfos object
183
+ * This property will be removed from the layer instance.
184
+ *
185
+ * @param layer the data layer being cloned
186
+ * @param prop the property name used to cache
187
+ * @param fieldInfos the object that will store the cached property
188
+ * @private
189
+ */
190
+ function _cacheFieldInfo(layer, prop, fieldInfos, removeProp) {
191
+ /* istanbul ignore else */
192
+ if (layer &&
193
+ layer.hasOwnProperty(prop) &&
194
+ fieldInfos &&
195
+ fieldInfos.hasOwnProperty(layer.id)) {
196
+ fieldInfos[layer.id][prop] = layer[prop];
197
+ // editFieldsInfo does not come through unless its with the layer
198
+ // when it's being added
199
+ /* istanbul ignore else */
200
+ if (removeProp) {
201
+ layer[prop] = null;
202
+ }
203
+ }
204
+ }
205
+ exports._cacheFieldInfo = _cacheFieldInfo;
206
+ /**
207
+ * Cache popup info that can contain field references
208
+ *
209
+ * @param data The items data property
210
+ * @returns An updated instance of the popupInfos
211
+ */
212
+ function cachePopupInfos(data) {
213
+ // store any popupInfo so we can update after any potential name changes
214
+ const popupInfos = {
215
+ layers: {},
216
+ tables: {}
217
+ };
218
+ if (data && data.layers && data.layers.length > 0) {
219
+ _cachePopupInfo(popupInfos, "layers", data.layers);
220
+ }
221
+ if (data && data.tables && data.tables.length > 0) {
222
+ _cachePopupInfo(popupInfos, "tables", data.tables);
223
+ }
224
+ return popupInfos;
225
+ }
226
+ exports.cachePopupInfos = cachePopupInfos;
227
+ /**
228
+ * Helper function to cache a single popupInfo
229
+ * This property will be reset on the layer
230
+ *
231
+ * @param popupInfos object to store the cahced popupInfo
232
+ * @param type is it a layer or table
233
+ * @param _items list or either layers or tables
234
+ * @private
235
+ */
236
+ function _cachePopupInfo(popupInfos, type, _items) {
237
+ _items.forEach((item) => {
238
+ if (item && item.hasOwnProperty("popupInfo")) {
239
+ popupInfos[type][item.id] = item.popupInfo;
240
+ item.popupInfo = {};
241
+ }
242
+ });
243
+ }
244
+ exports._cachePopupInfo = _cachePopupInfo;
245
+ /**
246
+ * Store basic layer information for potential replacement if we are unable to access a given service
247
+ * added for issue #859
248
+ *
249
+ * @param layerId the id for the layer
250
+ * @param itemId the id for the item
251
+ * @param url the url for the layer
252
+ * @param templateDictionary Hash of key details used for variable replacement
253
+ * @returns templatized itemTemplate
254
+ */
255
+ function cacheLayerInfo(layerId, itemId, url, templateDictionary) {
256
+ if (layerId) {
257
+ const layerIdVar = `layer${layerId}`;
258
+ // need to structure these differently so they are not used for standard replacement calls
259
+ // this now adds additional vars that are not needing replacement unless we fail to fetch the service
260
+ const newVars = (0, generalHelpers_1.getProp)(templateDictionary, `${interfaces_1.UNREACHABLE}.${itemId}`) || {
261
+ itemId
262
+ };
263
+ newVars[layerIdVar] = (0, generalHelpers_1.getProp)(newVars, layerIdVar) || {
264
+ layerId,
265
+ itemId
266
+ };
267
+ if (url !== "") {
268
+ newVars[layerIdVar]["url"] = url;
269
+ }
270
+ const unreachableVars = {};
271
+ unreachableVars[itemId] = newVars;
272
+ templateDictionary[interfaces_1.UNREACHABLE] = {
273
+ ...templateDictionary[interfaces_1.UNREACHABLE],
274
+ ...unreachableVars
275
+ };
276
+ }
277
+ }
278
+ exports.cacheLayerInfo = cacheLayerInfo;
279
+ /**
280
+ * Creates an item in a specified folder (except for Group item type).
281
+ *
282
+ * @param itemTemplate Item to be created; n.b.: this item is modified
283
+ * @param templateDictionary Hash mapping property names to replacement values
284
+ * @param createResponse Response from create service
285
+ * @returns An updated instance of the template
286
+ * @private
287
+ */
288
+ function updateTemplate(itemTemplate, templateDictionary, createResponse) {
289
+ // Update the item with any typeKeywords that were added on create
290
+ _updateTypeKeywords(itemTemplate, createResponse);
291
+ // Add the new item to the template dictionary
292
+ templateDictionary[itemTemplate.itemId] = Object.assign(templateDictionary[itemTemplate.itemId] || {}, {
293
+ itemId: createResponse.serviceItemId,
294
+ url: (0, generalHelpers_1.checkUrlPathTermination)(createResponse.serviceurl),
295
+ name: createResponse.name
296
+ });
297
+ // Update the item template now that the new service has been created
298
+ itemTemplate.itemId = createResponse.serviceItemId;
299
+ return (0, templatization_1.replaceInTemplate)(itemTemplate, templateDictionary);
300
+ }
301
+ exports.updateTemplate = updateTemplate;
302
+ /**
303
+ * Updates the items typeKeywords to include any typeKeywords that
304
+ * were added by the create service request
305
+ *
306
+ * @param itemTemplate Item to be created; n.b.: this item is modified
307
+ * @param createResponse Response from create service
308
+ * @returns An updated instance of the template
309
+ * @private
310
+ */
311
+ function _updateTypeKeywords(itemTemplate, createResponse) {
312
+ // https://github.com/Esri/solution.js/issues/589
313
+ const iKwords = (0, generalHelpers_1.getProp)(itemTemplate, "item.typeKeywords");
314
+ const cKwords = (0, generalHelpers_1.getProp)(createResponse, "typeKeywords");
315
+ if (iKwords && cKwords) {
316
+ (0, generalHelpers_1.setProp)(itemTemplate, "item.typeKeywords", iKwords.concat(cKwords.filter(k => iKwords.indexOf(k) < 0)));
317
+ }
318
+ return itemTemplate;
319
+ }
320
+ exports._updateTypeKeywords = _updateTypeKeywords;
321
+ /**
322
+ * Create the name mapping object that will allow for all templatized field
323
+ * references to be de-templatized.
324
+ * This also removes the stored sourceFields and newFields arrays from fieldInfos.
325
+ *
326
+ * @example
327
+ * \{ layer0: \{ fields: \{ lowerCaseSourceFieldName: newFieldNameAfterDeployment \} \} \}
328
+ *
329
+ * @param layerInfos The object that stores the cached layer properties and name mapping
330
+ * @returns The settings object that will be used to de-templatize the field references.
331
+ */
332
+ function getLayerSettings(layerInfos, url, itemId, enterpriseIDMapping) {
333
+ const settings = {};
334
+ const ids = Object.keys(layerInfos);
335
+ ids.forEach((id) => {
336
+ const _layerId = (0, generalHelpers_1.getProp)(layerInfos[id], "item.id");
337
+ const isNum = parseInt(_layerId, 10) > -1;
338
+ const layerId = isNum && enterpriseIDMapping
339
+ ? enterpriseIDMapping[_layerId]
340
+ : isNum
341
+ ? _layerId
342
+ : id;
343
+ settings[`layer${isNum ? _layerId : id}`] = {
344
+ fields: _getNameMapping(layerInfos, id),
345
+ url: (0, generalHelpers_1.checkUrlPathTermination)(url) + layerId,
346
+ layerId,
347
+ itemId
348
+ };
349
+ (0, generalHelpers_1.deleteProp)(layerInfos[id], "newFields");
350
+ (0, generalHelpers_1.deleteProp)(layerInfos[id], "sourceFields");
351
+ });
352
+ return settings;
353
+ }
354
+ exports.getLayerSettings = getLayerSettings;
355
+ /**
356
+ * Set the names and titles for all feature services.
357
+ *
358
+ * This function will ensure that we have unique feature service names.
359
+ * The feature service name will have the solution item id appended.
360
+ *
361
+ * @param templates A collection of AGO item templates.
362
+ * @param solutionItemId The item id for the deployed solution item.
363
+ * @returns An updated collection of AGO templates with unique feature service names.
364
+ */
365
+ function setNamesAndTitles(templates, solutionItemId) {
366
+ const names = [];
367
+ return templates.map(t => {
368
+ /* istanbul ignore else */
369
+ if (t.item.type === "Feature Service") {
370
+ // Retain the existing title but swap with name if it's missing
371
+ t.item.title = t.item.title || t.item.name;
372
+ /* istanbul ignore else */
373
+ if (!(0, trackingHelpers_1.isTrackingViewTemplate)(t)) {
374
+ // Need to set the service name: name + "_" + newItemId
375
+ let baseName = t.item.name || t.item.title;
376
+ // If the name already contains a GUID remove it
377
+ baseName = baseName.replace(/_[0-9A-F]{32}/gi, "");
378
+ // The name length limit is 98
379
+ // Limit the baseName to 50 characters before the _<guid>
380
+ const name = baseName.substring(0, 50) + "_" + solutionItemId;
381
+ // If the name + GUID already exists then append "_occurrenceCount"
382
+ t.item.name =
383
+ names.indexOf(name) === -1
384
+ ? name
385
+ : `${name}_${names.filter(n => n === name).length}`;
386
+ names.push(name);
387
+ }
388
+ }
389
+ return t;
390
+ });
391
+ }
392
+ exports.setNamesAndTitles = setNamesAndTitles;
393
+ /**
394
+ * This is used when deploying views.
395
+ * We need to update fields referenced in adminLayerInfo for relationships prior to deploying the view.
396
+ * This moves the fieldInfos for the views source layers from the item settings for the source layer
397
+ * to the item settings for the view.
398
+ *
399
+ * @param itemTemplate The current itemTemplate being processed.
400
+ * @param settings The settings object used to de-templatize the various templates within the item.
401
+ */
402
+ function updateSettingsFieldInfos(itemTemplate, settings) {
403
+ const dependencies = itemTemplate.dependencies;
404
+ const id = itemTemplate.itemId;
405
+ const settingsKeys = Object.keys(settings);
406
+ settingsKeys.forEach((k) => {
407
+ if (id === settings[k].itemId) {
408
+ dependencies.forEach((d) => {
409
+ settingsKeys.forEach((_k) => {
410
+ /* istanbul ignore else */
411
+ if (d === _k) {
412
+ // combine for multi-source views
413
+ const fieldInfos = {};
414
+ fieldInfos[d] = (0, generalHelpers_1.getProp)(settings[_k], "fieldInfos");
415
+ settings[k]["sourceServiceFields"] = settings[k]["sourceServiceFields"]
416
+ ? { ...settings[k]["sourceServiceFields"], ...fieldInfos }
417
+ : fieldInfos;
418
+ const layerKeys = Object.keys(settings[_k]);
419
+ layerKeys.forEach(layerKey => {
420
+ /* istanbul ignore else */
421
+ if (layerKey.startsWith("layer")) {
422
+ settings[k][layerKey] = settings[_k][layerKey];
423
+ }
424
+ });
425
+ }
426
+ });
427
+ });
428
+ }
429
+ });
430
+ }
431
+ exports.updateSettingsFieldInfos = updateSettingsFieldInfos;
432
+ /**
433
+ * Add flag to indicate item should be ignored.
434
+ * Construct template dictionary to detemplatize any references to this item by other items.
435
+ *
436
+ * @param template Template for feature service item
437
+ * @param authentication Credentials for the request
438
+ * @returns A promise that will resolve when template has been updated
439
+ * @private
440
+ */
441
+ function updateTemplateForInvalidDesignations(template, authentication) {
442
+ return new Promise((resolve, reject) => {
443
+ template.properties.hasInvalidDesignations = true;
444
+ if (template.item.url) {
445
+ // get the admin URL
446
+ const url = template.item.url;
447
+ (0, restHelpers_1.rest_request)(url + "?f=json", {
448
+ authentication: authentication
449
+ }).then(serviceData => {
450
+ const layerInfos = {};
451
+ const layersAndTables = (serviceData.layers || []).concat(serviceData.tables || []);
452
+ layersAndTables.forEach((l) => {
453
+ /* istanbul ignore else */
454
+ if (l && l.hasOwnProperty("id")) {
455
+ layerInfos[l.id] = l;
456
+ }
457
+ });
458
+ template.data[template.itemId] = Object.assign({
459
+ itemId: template.itemId
460
+ }, getLayerSettings(layerInfos, url, template.itemId));
461
+ resolve(template);
462
+ }, e => reject((0, generalHelpers_1.fail)(e)));
463
+ }
464
+ else {
465
+ resolve(template);
466
+ }
467
+ });
468
+ }
469
+ exports.updateTemplateForInvalidDesignations = updateTemplateForInvalidDesignations;
470
+ /**
471
+ * Get the contingent values for each layer in the service.
472
+ * Remove key props that cannot be included with the addToDef call on deploy.
473
+ * Store the values alongside other key feature service properties in the template
474
+ *
475
+ * @param properties the current feature services properties
476
+ * @param adminUrl the current feature service url
477
+ * @param authentication Credentials for the request to AGOL
478
+ * @returns A promise that will resolve when the contingent values have been fetched.
479
+ * This function will update the provided properties argument when contingent values are found.
480
+ */
481
+ function processContingentValues(properties, adminUrl, authentication) {
482
+ return new Promise((resolve, reject) => {
483
+ if ((0, generalHelpers_1.getProp)(properties, 'service.isView')) {
484
+ // views will inherit from the source service
485
+ resolve();
486
+ }
487
+ else {
488
+ const layersAndTables = (properties.layers || []).concat(properties.tables || []);
489
+ const layerIds = [];
490
+ const contingentValuePromises = layersAndTables.reduce((prev, cur) => {
491
+ /* istanbul ignore else */
492
+ if (cur.hasContingentValuesDefinition) {
493
+ prev.push((0, restHelpers_1.rest_request)(`${adminUrl}/${cur['id']}/contingentValues?f=json`, { authentication }));
494
+ layerIds.push(cur['id']);
495
+ }
496
+ return prev;
497
+ }, []);
498
+ if (contingentValuePromises.length > 0) {
499
+ Promise.all(contingentValuePromises).then((results) => {
500
+ const contingentValues = {};
501
+ results.forEach((r, i) => {
502
+ (0, generalHelpers_1.deleteProp)(r, 'typeCodes');
503
+ /* istanbul ignore else */
504
+ if ((0, generalHelpers_1.getProp)(r, 'stringDicts') && (0, generalHelpers_1.getProp)(r, 'contingentValuesDefinition')) {
505
+ r.contingentValuesDefinition['stringDicts'] = r.stringDicts;
506
+ (0, generalHelpers_1.deleteProp)(r, 'stringDicts');
507
+ }
508
+ (0, generalHelpers_1.deleteProps)((0, generalHelpers_1.getProp)(r, 'contingentValuesDefinition'), ['layerID', 'layerName', 'geometryType', 'hasSubType']);
509
+ contingentValues[layerIds[i]] = r;
510
+ });
511
+ properties.contingentValues = contingentValues;
512
+ resolve();
513
+ }, reject);
514
+ }
515
+ else {
516
+ resolve();
517
+ }
518
+ }
519
+ });
520
+ }
521
+ exports.processContingentValues = processContingentValues;
522
+ /**
523
+ * Replace the field name reference templates with the new field names after deployment.
524
+ *
525
+ * @param fieldInfos The object that stores the cached layer properties and name mapping
526
+ * @param popupInfos The object from the popupInfo property for the layer
527
+ * @param adminLayerInfos The object from the adminLayerInfo property for the layer
528
+ * @param settings The settings object that has all of the mappings for de-templatizing.
529
+ * @returns An object that contains updated instances of popupInfos, fieldInfos, and adminLayerInfos
530
+ */
531
+ function deTemplatizeFieldInfos(fieldInfos, popupInfos, adminLayerInfos, settings) {
532
+ const fieldInfoKeys = Object.keys(fieldInfos);
533
+ fieldInfoKeys.forEach(id => {
534
+ if (fieldInfos[id].hasOwnProperty("templates")) {
535
+ fieldInfos[id].templates = JSON.parse((0, templatization_1.replaceInTemplate)(JSON.stringify(fieldInfos[id].templates), settings));
536
+ }
537
+ if (fieldInfos[id].hasOwnProperty("adminLayerInfo")) {
538
+ adminLayerInfos[id].viewLayerDefinition.table.relatedTables =
539
+ fieldInfos[id].adminLayerInfo;
540
+ (0, generalHelpers_1.deleteProp)(fieldInfos[id], "adminLayerInfo");
541
+ }
542
+ if (fieldInfos[id].hasOwnProperty("types")) {
543
+ fieldInfos[id].types = JSON.parse((0, templatization_1.replaceInTemplate)(JSON.stringify(fieldInfos[id].types), settings));
544
+ }
545
+ });
546
+ return {
547
+ popupInfos: (0, templatization_1.replaceInTemplate)(popupInfos, settings),
548
+ fieldInfos: (0, templatization_1.replaceInTemplate)(fieldInfos, settings),
549
+ adminLayerInfos: (0, templatization_1.replaceInTemplate)(adminLayerInfos, settings)
550
+ };
551
+ }
552
+ exports.deTemplatizeFieldInfos = deTemplatizeFieldInfos;
553
+ /**
554
+ * This is used when deploying views.
555
+ * We need to update fields referenced in adminLayerInfo for relationships prior to deploying the view.
556
+ * This moves the fieldInfos for the views source layers from the item settings for the source layer
557
+ * to the item settings for the view.
558
+ *
559
+ * @param itemTemplate The current itemTemplate being processed.
560
+ * @returns array of layers and tables
561
+ */
562
+ function getLayersAndTables(itemTemplate) {
563
+ const properties = itemTemplate.properties;
564
+ const layersAndTables = [];
565
+ (properties.layers || []).forEach(function (layer) {
566
+ layersAndTables.push({
567
+ item: layer,
568
+ type: "layer"
569
+ });
570
+ });
571
+ (properties.tables || []).forEach(function (table) {
572
+ layersAndTables.push({
573
+ item: table,
574
+ type: "table"
575
+ });
576
+ });
577
+ return layersAndTables;
578
+ }
579
+ exports.getLayersAndTables = getLayersAndTables;
580
+ /**
581
+ * Fetch each layer and table from service so we can determine what fields they have.
582
+ * This is leveraged when we are using existing services so we can determine if we need to
583
+ * remove any fields from views that depend on these layers and tables.
584
+ *
585
+ * @param url Feature service endpoint
586
+ * @param ids layer and table ids
587
+ * @param authentication Credentials for the request
588
+ * @returns A promise that will resolve an array of promises with either a failure or the data
589
+ * @private
590
+ */
591
+ function getExistingLayersAndTables(url, ids, authentication) {
592
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
593
+ return new Promise(resolve => {
594
+ const defs = ids.map(id => {
595
+ return (0, restHelpers_1.rest_request)((0, generalHelpers_1.checkUrlPathTermination)(url) + id, {
596
+ authentication
597
+ });
598
+ });
599
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
600
+ Promise.all(defs.map(p => p.catch(e => e))).then(resolve);
601
+ });
602
+ }
603
+ exports.getExistingLayersAndTables = getExistingLayersAndTables;
604
+ /**
605
+ * Adds the layers and tables of a feature service to it and restores their relationships.
606
+ *
607
+ * @param itemTemplate Feature service
608
+ * @param templateDictionary Hash mapping Solution source id to id of its clone (and name & URL for feature
609
+ * service)
610
+ * @param popupInfos the cached popup info from the layers
611
+ * @param authentication Credentials for the request
612
+ * @returns A promise that will resolve when all layers and tables have been added
613
+ * @private
614
+ */
615
+ function addFeatureServiceLayersAndTables(itemTemplate, templateDictionary, popupInfos, authentication) {
616
+ return new Promise((resolve, reject) => {
617
+ if ((0, trackingHelpers_1.isTrackingViewTemplate)(itemTemplate)) {
618
+ resolve(null);
619
+ }
620
+ else {
621
+ // Create a hash of various properties that contain field references
622
+ const fieldInfos = {};
623
+ const adminLayerInfos = {};
624
+ // Add the service's layers and tables to it
625
+ const layersAndTables = getLayersAndTables(itemTemplate);
626
+ if (layersAndTables.length > 0) {
627
+ addFeatureServiceDefinition(itemTemplate.item.url || "", layersAndTables, templateDictionary, authentication, itemTemplate.key, adminLayerInfos, fieldInfos, itemTemplate).then(() => {
628
+ // Detemplatize field references and update the layer properties
629
+ // Only failure path is handled by addFeatureServiceDefinition
630
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
631
+ updateLayerFieldReferences(itemTemplate, fieldInfos, popupInfos, adminLayerInfos, templateDictionary).then(r => {
632
+ // Update relationships and layer definitions
633
+ const updates = (0, restHelpers_1.getLayerUpdates)({
634
+ message: "updated layer definition",
635
+ objects: r.layerInfos.fieldInfos,
636
+ itemTemplate: r.itemTemplate,
637
+ authentication
638
+ }, templateDictionary.isPortal);
639
+ // Process the updates sequentially
640
+ updates
641
+ .reduce((prev, update) => {
642
+ return prev.then(() => {
643
+ return (0, restHelpers_1.getRequest)(update);
644
+ });
645
+ }, Promise.resolve(null))
646
+ .then(() => resolve(null), (e) => reject((0, generalHelpers_1.fail)(e)) // getRequest
647
+ );
648
+ });
649
+ }, e => reject((0, generalHelpers_1.fail)(e)) // addFeatureServiceDefinition
650
+ );
651
+ }
652
+ else {
653
+ resolve(null);
654
+ }
655
+ }
656
+ });
657
+ }
658
+ exports.addFeatureServiceLayersAndTables = addFeatureServiceLayersAndTables;
659
+ /**
660
+ * Updates a feature service with a list of layers and/or tables.
661
+ *
662
+ * @param serviceUrl URL of feature service
663
+ * @param listToAdd List of layers and/or tables to add
664
+ * @param templateDictionary Hash mapping Solution source id to id of its clone (and name & URL for feature
665
+ * service)
666
+ * @param authentication Credentials for the request
667
+ * @param key
668
+ * @param adminLayerInfos Hash map of a layers adminLayerInfo
669
+ * @param fieldInfos Hash map of properties that contain field references
670
+ * @param itemTemplate
671
+ * @returns A promise that will resolve when the feature service has been updated
672
+ * @private
673
+ */
674
+ function addFeatureServiceDefinition(serviceUrl, listToAdd, templateDictionary, authentication, key, adminLayerInfos, fieldInfos, itemTemplate) {
675
+ return new Promise((resolve, reject) => {
676
+ if ((0, trackingHelpers_1.isTrackingViewTemplate)(itemTemplate)) {
677
+ resolve(null);
678
+ }
679
+ else {
680
+ let options = {
681
+ layers: [],
682
+ tables: [],
683
+ authentication
684
+ };
685
+ // if the service has veiws keep track of the fields so we can use them to
686
+ // compare with the view fields
687
+ /* istanbul ignore else */
688
+ if ((0, generalHelpers_1.getProp)(itemTemplate, "properties.service.hasViews")) {
689
+ _updateTemplateDictionaryFields(itemTemplate, templateDictionary);
690
+ }
691
+ const isSelfReferential = _isSelfReferential(listToAdd);
692
+ listToAdd = _updateOrder(listToAdd, isSelfReferential);
693
+ const chunkSize = _getLayerChunkSize();
694
+ const layerChunks = [];
695
+ listToAdd.forEach((toAdd, i) => {
696
+ let item = toAdd.item;
697
+ const originalId = item.id;
698
+ fieldInfos = cacheFieldInfos(item, fieldInfos);
699
+ // cache the values to be added in seperate addToDef calls
700
+ fieldInfos = cacheContingentValues(item.id, fieldInfos, itemTemplate);
701
+ /* istanbul ignore else */
702
+ if (item.isView) {
703
+ deleteViewProps(item);
704
+ }
705
+ // when the item is a view we need to grab the supporting fieldInfos
706
+ /* istanbul ignore else */
707
+ if (itemTemplate.properties.service.isView) {
708
+ _updateGeomFieldName(item.adminLayerInfo, templateDictionary);
709
+ adminLayerInfos[originalId] = item.adminLayerInfo;
710
+ // need to update adminLayerInfo before adding to the service def
711
+ // bring over the fieldInfos from the source layer
712
+ updateSettingsFieldInfos(itemTemplate, templateDictionary);
713
+ // update adminLayerInfo before add to definition with view source fieldInfo settings
714
+ item.adminLayerInfo = (0, templatization_1.replaceInTemplate)(item.adminLayerInfo, templateDictionary);
715
+ /* istanbul ignore else */
716
+ if (fieldInfos && fieldInfos.hasOwnProperty(item.id)) {
717
+ Object.keys(templateDictionary).some(k => {
718
+ if (templateDictionary[k].itemId === itemTemplate.itemId) {
719
+ fieldInfos[item.id]["sourceServiceFields"] =
720
+ templateDictionary[k].sourceServiceFields;
721
+ return true;
722
+ }
723
+ else {
724
+ return false;
725
+ }
726
+ });
727
+ }
728
+ }
729
+ /* istanbul ignore else */
730
+ if (templateDictionary.isPortal) {
731
+ item = _updateForPortal(item, itemTemplate, templateDictionary);
732
+ }
733
+ removeLayerOptimization(item);
734
+ // this can still chunk layers
735
+ options = _updateAddOptions(itemTemplate, options, layerChunks, isSelfReferential, authentication);
736
+ if (item.type === "Feature Layer") {
737
+ options.layers.push(item);
738
+ }
739
+ else {
740
+ options.tables.push(item);
741
+ }
742
+ // In general we are switching to not use chunking. Rather if we exceed the defined chunk size
743
+ // we will use an async request.
744
+ // Currently the only case that should chunk the requests is when we have a multisource view
745
+ // handled in _updateAddOptions above
746
+ /* istanbul ignore else */
747
+ if (i + 1 === listToAdd.length) {
748
+ layerChunks.push(Object.assign({}, options));
749
+ options = {
750
+ layers: [],
751
+ tables: [],
752
+ authentication
753
+ };
754
+ }
755
+ });
756
+ // will use async by default rather than chunk the layer requests when we have more layers
757
+ // than the defined chunk size
758
+ const useAsync = listToAdd.length > chunkSize;
759
+ layerChunks
760
+ .reduce((prev, curr) => prev.then(() => (0, restHelpers_1.addToServiceDefinition)(serviceUrl, curr, false, useAsync)), Promise.resolve(null))
761
+ .then(() => resolve(null), (e) => reject((0, generalHelpers_1.fail)(e)));
762
+ }
763
+ });
764
+ }
765
+ exports.addFeatureServiceDefinition = addFeatureServiceDefinition;
766
+ /**
767
+ * When a view is a multi service view sort based on the id
768
+ * https://github.com/Esri/solution.js/issues/1048
769
+ *
770
+ * @param layersAndTables The list of layers and tables for the current template
771
+ * @param isSelfReferential Indicates if any layers or tables have relationships with other layers or tables in the same service
772
+ *
773
+ * @returns Sorted list of layers and tables when using a multi-service view
774
+ * @private
775
+ */
776
+ function _updateOrder(layersAndTables, isSelfReferential) {
777
+ return isSelfReferential ? layersAndTables.sort((a, b) => a.item.id - b.item.id) : layersAndTables;
778
+ }
779
+ exports._updateOrder = _updateOrder;
780
+ /**
781
+ * When a view is a multi service view add each layer separately
782
+ * https://github.com/Esri/solution.js/issues/871
783
+ *
784
+ * @param itemTemplate The current itemTemplate being processed
785
+ * @param options Add to service definition options
786
+ * @param layerChunks Groups of layers or tables to add to the service
787
+ * @param isSelfReferential Indicates if any layers or tables have relationships with other layers or tables in the same service
788
+ * @param authentication Credentials for the request
789
+ *
790
+ * @returns Add to service definition options
791
+ * @private
792
+ */
793
+ function _updateAddOptions(itemTemplate, options, layerChunks, isSelfReferential, authentication) {
794
+ const isMsView = (0, generalHelpers_1.getProp)(itemTemplate, "properties.service.isMultiServicesView") || false;
795
+ /* istanbul ignore else */
796
+ if (isMsView || isSelfReferential) {
797
+ // if we already have some layers or tables add them first
798
+ /* istanbul ignore else */
799
+ if (options.layers.length > 0 || options.tables.length > 0) {
800
+ layerChunks.push(Object.assign({}, options));
801
+ options = {
802
+ layers: [],
803
+ tables: [],
804
+ authentication
805
+ };
806
+ }
807
+ }
808
+ return options;
809
+ }
810
+ exports._updateAddOptions = _updateAddOptions;
811
+ /**
812
+ * Determine if any layer or table within the service references
813
+ * other layers or tables within the same service
814
+ *
815
+ * @param layersAndTables the list of layers and tables from the service
816
+ *
817
+ * @returns true when valid internal references are found
818
+ * @private
819
+ */
820
+ function _isSelfReferential(layersAndTables) {
821
+ const names = layersAndTables.map(l => l.item.name);
822
+ const srcTables = {};
823
+ return layersAndTables.some(l => {
824
+ const table = l.item.adminLayerInfo?.viewLayerDefinition?.table;
825
+ if (table) {
826
+ const name = table.sourceServiceName;
827
+ const id = table.sourceLayerId;
828
+ if (name && id > -1) {
829
+ if (Object.keys(srcTables).indexOf(name) > -1) {
830
+ if (srcTables[name].indexOf(id) > -1) {
831
+ return true;
832
+ }
833
+ else {
834
+ srcTables[name].push(id);
835
+ }
836
+ }
837
+ else {
838
+ srcTables[name] = [id];
839
+ }
840
+ }
841
+ return (table.relatedTables || []).some(r => names.indexOf(r.name) > -1);
842
+ }
843
+ });
844
+ }
845
+ exports._isSelfReferential = _isSelfReferential;
846
+ /**
847
+ * Remove "multiScaleGeometryInfo" for issue #526 to prevent invalid enablement of layer optimization
848
+ *
849
+ * @param layer the layer to evaluate
850
+ * @private
851
+ */
852
+ function removeLayerOptimization(layer) {
853
+ /* istanbul ignore else */
854
+ if (layer.multiScaleGeometryInfo) {
855
+ (0, generalHelpers_1.deleteProp)(layer, "multiScaleGeometryInfo");
856
+ }
857
+ }
858
+ exports.removeLayerOptimization = removeLayerOptimization;
859
+ /**
860
+ * Handle portal specific updates to the item
861
+ *
862
+ * @param item the item to update
863
+ * @param itemTemplate the item template
864
+ * @param templateDictionary Hash mapping Solution source id to id of its clone
865
+ *
866
+ * @returns the updated item
867
+ * @private
868
+ */
869
+ function _updateForPortal(item, itemTemplate, templateDictionary) {
870
+ // When deploying to portal we need to adjust the uniquie ID field up front
871
+ /* istanbul ignore else */
872
+ if (item.uniqueIdField && item.uniqueIdField.name) {
873
+ item.uniqueIdField.name = String(item.uniqueIdField.name).toLocaleLowerCase();
874
+ }
875
+ // Portal will fail if the geometryField is null
876
+ if (item.type === "Table" && item.adminLayerInfo) {
877
+ (0, generalHelpers_1.deleteProp)(item.adminLayerInfo, "geometryField");
878
+ }
879
+ // Portal will fail if the sourceFields in the viewLayerDef contain fields that are not in the source service
880
+ /* istanbul ignore else */
881
+ if (item.isView) {
882
+ const viewLayerDefTable = (0, generalHelpers_1.getProp)(item, "adminLayerInfo.viewLayerDefinition.table");
883
+ let fieldNames = [];
884
+ if (viewLayerDefTable) {
885
+ const tableFieldNames = _getFieldNames(viewLayerDefTable, itemTemplate, templateDictionary);
886
+ fieldNames = fieldNames.concat(tableFieldNames);
887
+ const dynamicFieldNames = _getDynamicFieldNames(viewLayerDefTable);
888
+ fieldNames = fieldNames.concat(dynamicFieldNames);
889
+ (0, generalHelpers_1.setProp)(item, "adminLayerInfo.viewLayerDefinition.table", _updateSourceLayerFields(viewLayerDefTable, fieldNames));
890
+ // Handle related also
891
+ /* istanbul ignore else */
892
+ if (Array.isArray(viewLayerDefTable.relatedTables)) {
893
+ viewLayerDefTable.relatedTables.map((relatedTable) => {
894
+ const relatedTableFieldNames = _getFieldNames(relatedTable, itemTemplate, templateDictionary);
895
+ fieldNames = fieldNames.concat(relatedTableFieldNames);
896
+ const dynamicRelatedFieldNames = _getDynamicFieldNames(relatedTable);
897
+ fieldNames = fieldNames.concat(dynamicRelatedFieldNames);
898
+ return _updateSourceLayerFields(relatedTable, [...relatedTableFieldNames, ...dynamicRelatedFieldNames]);
899
+ });
900
+ }
901
+ }
902
+ else {
903
+ Object.keys(templateDictionary).some(k => {
904
+ /* istanbul ignore else */
905
+ if (templateDictionary[k].itemId === item.serviceItemId) {
906
+ const layerInfo = templateDictionary[k][`layer${item.id}`];
907
+ /* istanbul ignore else */
908
+ if (layerInfo && layerInfo.fields) {
909
+ if (Array.isArray(layerInfo.fields)) {
910
+ fieldNames = layerInfo.fields.map((f) => f.name);
911
+ }
912
+ else {
913
+ fieldNames = Object.keys(layerInfo.fields);
914
+ }
915
+ }
916
+ return true;
917
+ }
918
+ });
919
+ }
920
+ item = _updateItemFields(item, fieldNames);
921
+ }
922
+ // not allowed to set sourceSchemaChangesAllowed or isView for portal
923
+ // these are set when you create the service
924
+ (0, generalHelpers_1.deleteProp)(item, "isView");
925
+ return item;
926
+ }
927
+ exports._updateForPortal = _updateForPortal;
928
+ /**
929
+ * Get a list of the source layer field names
930
+ *
931
+ * @param table the table instance to compare
932
+ * @param itemTemplate the item template
933
+ * @param templateDictionary Hash mapping Solution source id to id of its clone
934
+ *
935
+ * @returns an array of the source layers fields
936
+ * @private
937
+ */
938
+ function _getFieldNames(table, itemTemplate, templateDictionary) {
939
+ let sourceLayerFields = [];
940
+ const viewSourceLayerId = table.sourceLayerId;
941
+ /* istanbul ignore else */
942
+ if (typeof viewSourceLayerId === "number") {
943
+ // need to make sure these actually exist in the source..
944
+ itemTemplate.dependencies.forEach(d => {
945
+ const layerInfo = templateDictionary[d][`layer${viewSourceLayerId}`];
946
+ /* istanbul ignore else */
947
+ if (layerInfo &&
948
+ layerInfo.fields &&
949
+ templateDictionary[d].name === table.sourceServiceName) {
950
+ if (Array.isArray(layerInfo.fields)) {
951
+ sourceLayerFields = sourceLayerFields.concat(layerInfo.fields.map((f) => f.name));
952
+ }
953
+ else {
954
+ sourceLayerFields = sourceLayerFields.concat(Object.keys(layerInfo.fields));
955
+ }
956
+ }
957
+ });
958
+ return sourceLayerFields;
959
+ }
960
+ }
961
+ exports._getFieldNames = _getFieldNames;
962
+ /**
963
+ * Get a list of any dynamically calculated fields
964
+ * These fields are still valid but will not exist in the source service
965
+ *
966
+ * @param table the table instance to compare
967
+ *
968
+ * @returns an array of field names
969
+ * @private
970
+ */
971
+ function _getDynamicFieldNames(table) {
972
+ const fieldNames = table.sourceLayerFields.reduce((prev, cur) => {
973
+ if (cur.statisticType) {
974
+ prev.push(cur.name);
975
+ }
976
+ return prev;
977
+ }, []);
978
+ return [...new Set(fieldNames)];
979
+ }
980
+ exports._getDynamicFieldNames = _getDynamicFieldNames;
981
+ /**
982
+ * Remove fields references from fields and indexes that do not exist in the source service
983
+ *
984
+ * @param item Layer or table
985
+ * @param templateDictionary Hash mapping Solution source id to id of its clone
986
+ *
987
+ * @returns updated layer or table
988
+ * @private
989
+ */
990
+ function _updateItemFields(item, fieldNames) {
991
+ /* istanbul ignore else */
992
+ if (fieldNames.length > 0) {
993
+ /* istanbul ignore else */
994
+ if (item.fields) {
995
+ item.fields = item.fields.filter((f) => fieldNames.indexOf(f.name) > -1);
996
+ }
997
+ /* istanbul ignore else */
998
+ if (item.indexes) {
999
+ item.indexes = item.indexes.filter((f) => fieldNames.indexOf(f.fields) > -1);
1000
+ }
1001
+ }
1002
+ return item;
1003
+ }
1004
+ exports._updateItemFields = _updateItemFields;
1005
+ /**
1006
+ * Filter the sourceLayerFields for the table
1007
+ *
1008
+ * @param table the table instance to evaluate
1009
+ * @param sourceLayerFields array of fields from the source service
1010
+ * @returns Updated instance of the table
1011
+ * @private
1012
+ */
1013
+ function _updateSourceLayerFields(table, sourceLayerFields) {
1014
+ /* istanbul ignore else */
1015
+ if (Array.isArray(table.sourceLayerFields) &&
1016
+ table.sourceLayerFields.length > 0) {
1017
+ // need to make sure these actually exist in the source..
1018
+ /* istanbul ignore else */
1019
+ if (sourceLayerFields.length > 0) {
1020
+ (0, generalHelpers_1.setProp)(table, "sourceLayerFields", table.sourceLayerFields.filter((f) => sourceLayerFields.indexOf(f.source.toLowerCase()) > -1));
1021
+ }
1022
+ }
1023
+ return table;
1024
+ }
1025
+ exports._updateSourceLayerFields = _updateSourceLayerFields;
1026
+ /**
1027
+ * When the itemm is a view with a geometry field update the value to
1028
+ * use the table name from the view layer def
1029
+ *
1030
+ * @param item the item details from the current template
1031
+ * @param templateDictionary Hash mapping property names to replacement values
1032
+ * @private
1033
+ */
1034
+ function _updateGeomFieldName(adminLayerInfo, templateDictionary) {
1035
+ // issue #471
1036
+ const tableName = (0, generalHelpers_1.getProp)(adminLayerInfo, "viewLayerDefinition.table.name");
1037
+ const fieldName = (0, generalHelpers_1.getProp)(adminLayerInfo, "geometryField.name");
1038
+ /* istanbul ignore else */
1039
+ if (fieldName && tableName) {
1040
+ const geomName = templateDictionary.isPortal
1041
+ ? `${tableName}.shape`
1042
+ : `${tableName}.Shape`;
1043
+ (0, generalHelpers_1.setProp)(adminLayerInfo, "geometryField.name", geomName);
1044
+ }
1045
+ else if (!fieldName && (0, generalHelpers_1.getProp)(adminLayerInfo, "geometryField")) {
1046
+ // null geom field will cause failure to deploy in portal
1047
+ // this is also checked and removed on deploy for older solutions
1048
+ (0, generalHelpers_1.deleteProp)(adminLayerInfo, "geometryField");
1049
+ }
1050
+ }
1051
+ exports._updateGeomFieldName = _updateGeomFieldName;
1052
+ /**
1053
+ * Add the fields to the templateDictionary when a service has views
1054
+ * these are used to compare with fields from the view when domains are involved
1055
+ * when a view field has a domain that differs from that of the source service
1056
+ * the definition needs to be modified in an update call rather than when it is first added.
1057
+ * This should only happen when the domain differs.
1058
+ *
1059
+ * @param itemTemplate
1060
+ * @param templateDictionary Hash mapping Solution source id to id of its clone (and name & URL for feature service)
1061
+ * @private
1062
+ */
1063
+ function _updateTemplateDictionaryFields(itemTemplate, templateDictionary, compareItemId = true) {
1064
+ const layers = itemTemplate.properties.layers;
1065
+ const tables = itemTemplate.properties.tables;
1066
+ const layersAndTables = layers.concat(tables);
1067
+ const fieldInfos = {};
1068
+ layersAndTables.forEach(layerOrTable => {
1069
+ fieldInfos[layerOrTable.id] = layerOrTable.fields;
1070
+ });
1071
+ Object.keys(templateDictionary).some(k => {
1072
+ if (compareItemId
1073
+ ? templateDictionary[k].itemId === itemTemplate.itemId
1074
+ : k === itemTemplate.itemId) {
1075
+ templateDictionary[k].fieldInfos = fieldInfos;
1076
+ return true;
1077
+ }
1078
+ else {
1079
+ return false;
1080
+ }
1081
+ });
1082
+ }
1083
+ exports._updateTemplateDictionaryFields = _updateTemplateDictionaryFields;
1084
+ /**
1085
+ * Set the defaultSpatialReference variable with the services spatial reference.
1086
+ * If this item is a Feature Service that has child views then we will use this value
1087
+ * if one or more of the child views spatial reference differs from that of its parent.
1088
+ *
1089
+ * @param templateDictionary Hash mapping Solution source id to id of its clone (and name & URL for feature service)
1090
+ * @param itemId The source id for the item
1091
+ * @param spatialReference \{ wkid: 102100 \} for example
1092
+ * @private
1093
+ */
1094
+ function setDefaultSpatialReference(templateDictionary, itemId, spatialReference) {
1095
+ /* istanbul ignore else */
1096
+ if (spatialReference) {
1097
+ (0, generalHelpers_1.setCreateProp)(templateDictionary, `${itemId}.defaultSpatialReference`, spatialReference);
1098
+ }
1099
+ }
1100
+ exports.setDefaultSpatialReference = setDefaultSpatialReference;
1101
+ /**
1102
+ * Compare the spatial reference of the current item against its dependencies.
1103
+ * The spatial reference of a view cannot differ from its source service.
1104
+ * If the view has a different spatial reference from its source use the source spatial reference.
1105
+ *
1106
+ * @param serviceInfo Basic service information
1107
+ * @param itemTemplate The current template to process
1108
+ * @param templateDictionary Hash mapping Solution source id to id of its clone (and name & URL for feature service)
1109
+ * @private
1110
+ */
1111
+ function validateSpatialReferenceAndExtent(serviceInfo, itemTemplate, templateDictionary) {
1112
+ /* istanbul ignore else */
1113
+ if ((0, generalHelpers_1.getProp)(serviceInfo, "service.isView")) {
1114
+ let sourceSR;
1115
+ let sourceExt;
1116
+ itemTemplate.dependencies.some(id => {
1117
+ const source = templateDictionary[id];
1118
+ const sr = (0, generalHelpers_1.getProp)(source, "defaultSpatialReference");
1119
+ /* istanbul ignore else */
1120
+ if (!sourceSR && sr) {
1121
+ sourceSR = sr;
1122
+ }
1123
+ const ext = (0, generalHelpers_1.getProp)(source, "defaultExtent");
1124
+ /* istanbul ignore else */
1125
+ if (!sourceExt && ext) {
1126
+ sourceExt = ext;
1127
+ }
1128
+ return sourceSR && sourceExt;
1129
+ });
1130
+ const sourceWkid = (0, generalHelpers_1.getProp)(sourceSR, "wkid");
1131
+ const viewWkid = (0, generalHelpers_1.getProp)(serviceInfo, "service.spatialReference.wkid");
1132
+ /* istanbul ignore else */
1133
+ if (sourceWkid && viewWkid && sourceWkid !== viewWkid) {
1134
+ (0, generalHelpers_1.setCreateProp)(serviceInfo, "service.spatialReference", sourceSR);
1135
+ }
1136
+ const viewExt = (0, generalHelpers_1.getProp)(serviceInfo, "service.fullExtent");
1137
+ /* istanbul ignore else */
1138
+ if (sourceExt &&
1139
+ viewExt &&
1140
+ JSON.stringify(sourceExt) !== JSON.stringify(viewExt)) {
1141
+ (0, generalHelpers_1.setCreateProp)(serviceInfo, "defaultExtent", sourceExt);
1142
+ }
1143
+ }
1144
+ }
1145
+ exports.validateSpatialReferenceAndExtent = validateSpatialReferenceAndExtent;
1146
+ /**
1147
+ * Updates a feature service with a list of layers and/or tables.
1148
+ *
1149
+ * @param itemTemplate
1150
+ * @param fieldInfos Hash map of properties that contain field references
1151
+ * @param popupInfos Hash map of a layers popupInfo
1152
+ * @param adminLayerInfos Hash map of a layers adminLayerInfo
1153
+ * @param templateDictionary Hash mapping Solution source id to id of its clone (and name & URL for feature service)
1154
+ * @param authentication Credentials for the request
1155
+ * @returns A promise that will resolve when the feature service has been updated
1156
+ * @private
1157
+ */
1158
+ function updateLayerFieldReferences(itemTemplate, fieldInfos, popupInfos, adminLayerInfos, templateDictionary) {
1159
+ return new Promise((resolveFn, rejectFn) => {
1160
+ // Will need to do some post processing for fields
1161
+ // to handle any potential field name changes when deploying to portal
1162
+ postProcessFields(itemTemplate, fieldInfos, popupInfos, adminLayerInfos, templateDictionary).then((layerInfos) => {
1163
+ // Update the items text with detemplatized popupInfo
1164
+ updatePopupInfo(itemTemplate, layerInfos.popupInfos);
1165
+ resolveFn({
1166
+ itemTemplate,
1167
+ layerInfos
1168
+ });
1169
+ }, e => rejectFn((0, generalHelpers_1.fail)(e)));
1170
+ });
1171
+ }
1172
+ exports.updateLayerFieldReferences = updateLayerFieldReferences;
1173
+ /**
1174
+ * Update the names of fields for each layer or table after it has been
1175
+ * added to the definition
1176
+ *
1177
+ * @param itemTemplate Item to be created
1178
+ * @param layerInfos Hash map of properties that contain field references and various layer info
1179
+ * @param popupInfos Hash map of a layers popupInfo
1180
+ * @param adminLayerInfos Hash map of a layers adminLayerInfo
1181
+ * @param templateDictionary
1182
+ * @param authentication Credentials for the request
1183
+ * @returns An object with detemplatized field references
1184
+ * @private
1185
+ */
1186
+ function postProcessFields(itemTemplate, layerInfos, popupInfos, adminLayerInfos, templateDictionary) {
1187
+ return new Promise((resolveFn, rejectFn) => {
1188
+ if (!itemTemplate.item.url) {
1189
+ rejectFn((0, generalHelpers_1.fail)("Feature layer " + itemTemplate.itemId + " does not have a URL"));
1190
+ }
1191
+ else {
1192
+ const id = itemTemplate.itemId;
1193
+ const settingsKeys = Object.keys(templateDictionary);
1194
+ let templateInfo;
1195
+ settingsKeys.some(k => {
1196
+ if (templateDictionary[k].itemId === id) {
1197
+ templateInfo = templateDictionary[k];
1198
+ return true;
1199
+ }
1200
+ else {
1201
+ return false;
1202
+ }
1203
+ });
1204
+ // concat any layers and tables to process
1205
+ const layers = itemTemplate.properties.layers;
1206
+ const tables = itemTemplate.properties.tables;
1207
+ const layersAndTables = layers.concat(tables);
1208
+ // Set the newFields property for the layerInfos...this will contain all fields
1209
+ // as they are after being added to the definition.
1210
+ // This allows us to handle any potential field name changes after deploy to portal
1211
+ layersAndTables.forEach((item) => {
1212
+ // when deploying to portal "isView" is only set for create service and will fail when
1213
+ // present on addToDef so this property is removed from item and we should check the templates service info
1214
+ const isView = item.isView || itemTemplate.properties.service.isView;
1215
+ /* istanbul ignore else */
1216
+ if (layerInfos && layerInfos.hasOwnProperty(item.id)) {
1217
+ const layerInfo = layerInfos[item.id];
1218
+ layerInfo["isView"] = item.isView;
1219
+ layerInfo["newFields"] = item.fields;
1220
+ layerInfo["sourceSchemaChangesAllowed"] =
1221
+ item.sourceSchemaChangesAllowed;
1222
+ /* istanbul ignore else */
1223
+ if (item.editFieldsInfo) {
1224
+ // more than case change when deployed to protal so keep track of the new names
1225
+ layerInfo["newEditFieldsInfo"] = JSON.parse(JSON.stringify(item.editFieldsInfo));
1226
+ }
1227
+ /* istanbul ignore else */
1228
+ if (isView && templateInfo && templateDictionary.isPortal) {
1229
+ // when the item is a view bring over the source service fields so we can compare the domains
1230
+ layerInfo["sourceServiceFields"] = templateInfo.sourceServiceFields;
1231
+ }
1232
+ }
1233
+ });
1234
+ // Add the layerInfos to the settings object to be used while detemplatizing
1235
+ settingsKeys.forEach((k) => {
1236
+ if (id === templateDictionary[k].itemId) {
1237
+ templateDictionary[k] = Object.assign(templateDictionary[k], getLayerSettings(layerInfos, templateDictionary[k].url, id));
1238
+ }
1239
+ });
1240
+ // update the layerInfos object with current field names
1241
+ resolveFn(deTemplatizeFieldInfos(layerInfos, popupInfos, adminLayerInfos, templateDictionary));
1242
+ }
1243
+ });
1244
+ }
1245
+ exports.postProcessFields = postProcessFields;
1246
+ /**
1247
+ * Add popup info back to the layer item
1248
+ *
1249
+ * @param itemTemplate
1250
+ * @param popupInfos popup info to be added back to the layer
1251
+ * @private
1252
+ */
1253
+ function updatePopupInfo(itemTemplate, popupInfos) {
1254
+ ["layers", "tables"].forEach(type => {
1255
+ const _items = (0, generalHelpers_1.getProp)(itemTemplate, "data." + type);
1256
+ /* istanbul ignore else */
1257
+ if (_items && Array.isArray(_items)) {
1258
+ _items.forEach((item) => {
1259
+ item.popupInfo = (0, generalHelpers_1.getProp)(popupInfos, type + "." + item.id) || {};
1260
+ });
1261
+ }
1262
+ });
1263
+ }
1264
+ exports.updatePopupInfo = updatePopupInfo;
1265
+ //#endregion
1266
+ //#region Private helper functions --------------------------------------------------//
1267
+ /**
1268
+ * Helper function to templatize value and make sure its converted to lowercase
1269
+ *
1270
+ * @param basePath path used to de-templatize while deploying
1271
+ * @param value to be converted to lower case for lookup while deploying
1272
+ * @private
1273
+ */
1274
+ function _templatize(basePath, value, suffix) {
1275
+ if (value.startsWith("{{")) {
1276
+ return value;
1277
+ }
1278
+ else {
1279
+ return String((0, templatization_1.templatizeTerm)(basePath, basePath, "." + String(value).toLowerCase() + (suffix ? "." + suffix : "")));
1280
+ }
1281
+ }
1282
+ exports._templatize = _templatize;
1283
+ /**
1284
+ * templatize an objects property
1285
+ *
1286
+ * @param object the object with the property to templatize
1287
+ * @param property the property of the object to templatize
1288
+ * @param basePath path used to de-templatize while deploying
1289
+ * @private
1290
+ */
1291
+ function _templatizeProperty(object, property, basePath, suffix) {
1292
+ if (object && object.hasOwnProperty(property) && object[property]) {
1293
+ object[property] = _templatize(basePath, object[property], suffix);
1294
+ }
1295
+ }
1296
+ exports._templatizeProperty = _templatizeProperty;
1297
+ /**
1298
+ * Templatize field references, serviceItemId, and adminLayerInfo for a layer
1299
+ *
1300
+ * @param dataItem from the items data property
1301
+ * @param adminItem from the services admin api
1302
+ * @param itemTemplate Template for feature service item
1303
+ * @param dependencies Array of IDependency for name mapping
1304
+ * @param templatizeFieldReferences Templatize all field references within a layer
1305
+ * @returns A promise that will resolve when template has been updated
1306
+ * @private
1307
+ */
1308
+ function _templatizeLayer(dataItem, adminItem, itemTemplate, dependencies, templatizeFieldReferences, templateDictionary) {
1309
+ // check for and repair common field issues
1310
+ _validateFields(adminItem);
1311
+ // Templatize all properties that contain field references
1312
+ /* istanbul ignore else */
1313
+ if (templatizeFieldReferences) {
1314
+ _templatizeLayerFieldReferences(dataItem, itemTemplate.itemId, adminItem, dependencies);
1315
+ }
1316
+ const updates = [adminItem];
1317
+ if (dataItem) {
1318
+ updates.push(dataItem);
1319
+ }
1320
+ updates.forEach(update => {
1321
+ if (update.hasOwnProperty("name")) {
1322
+ // templatize the name but leave the current name as the optional default
1323
+ update.name = (0, templatization_1.templatizeTerm)(update["serviceItemId"] + ".layer" + update.id, update["serviceItemId"] + ".layer" + update.id, ".name||" + update.name);
1324
+ }
1325
+ if (update.hasOwnProperty("extent")) {
1326
+ update.extent = (0, templatization_1.templatizeTerm)(update["serviceItemId"], update["serviceItemId"], ".solutionExtent");
1327
+ }
1328
+ if (update.hasOwnProperty("serviceItemId")) {
1329
+ update["serviceItemId"] = (0, templatization_1.templatizeTerm)(update["serviceItemId"], update["serviceItemId"], ".itemId");
1330
+ }
1331
+ if (update.hasOwnProperty("adminLayerInfo")) {
1332
+ update.adminLayerInfo = _templatizeAdminLayerInfo(update, dependencies, templateDictionary);
1333
+ }
1334
+ });
1335
+ }
1336
+ exports._templatizeLayer = _templatizeLayer;
1337
+ /**
1338
+ * Repair common issues that can occur with feature service field references.
1339
+ * This function will mutate the input item if any of the common issues have occured.
1340
+ *
1341
+ * @param adminItem layer or table from the service
1342
+ */
1343
+ function _validateFields(adminItem) {
1344
+ const fieldNames = (adminItem.fields || []).map((f) => f.name);
1345
+ // Update primary display field if field isn't in the layer.
1346
+ _validateDisplayField(adminItem, fieldNames);
1347
+ // Remove indexes on fields that don't exist in the layer.
1348
+ // Remove duplicate indexes on the same field.
1349
+ _validateIndexes(adminItem, fieldNames);
1350
+ // Remove field references in templates when field doesn't exist in the layer.
1351
+ _validateTemplatesFields(adminItem, fieldNames);
1352
+ _validateTypesTemplates(adminItem, fieldNames);
1353
+ // Repair editFieldsInfo if field referenced doesn't exist in the layer
1354
+ _validateEditFieldsInfo(adminItem, fieldNames);
1355
+ }
1356
+ exports._validateFields = _validateFields;
1357
+ /**
1358
+ * Update primary display field if casing doesn't match.
1359
+ * Update primary display field to the first non OID or GlobalId if the field isn't in the layer.
1360
+ *
1361
+ * @param adminItem layer or table from the service
1362
+ * @param fieldNames string list of fields names
1363
+ * @private
1364
+ */
1365
+ function _validateDisplayField(adminItem, fieldNames) {
1366
+ const displayField = adminItem.displayField || "";
1367
+ let i = -1;
1368
+ if (fieldNames.some(name => {
1369
+ i += 1;
1370
+ return name === displayField || name === displayField.toLowerCase();
1371
+ })) {
1372
+ adminItem.displayField = fieldNames[i];
1373
+ }
1374
+ else {
1375
+ // use the first non-OID non-globalId field we find
1376
+ const skipFields = [];
1377
+ const oidField = (0, generalHelpers_1.getProp)(adminItem, "uniqueIdField.name");
1378
+ /* istanbul ignore else */
1379
+ if (oidField) {
1380
+ skipFields.push(oidField);
1381
+ }
1382
+ const globalIdField = (0, generalHelpers_1.getProp)(adminItem, "globalIdField");
1383
+ /* istanbul ignore else */
1384
+ if (globalIdField) {
1385
+ skipFields.push(globalIdField);
1386
+ }
1387
+ fieldNames.some(name => {
1388
+ if (skipFields.indexOf(name) === -1) {
1389
+ adminItem.displayField = name;
1390
+ return true;
1391
+ }
1392
+ else {
1393
+ return false;
1394
+ }
1395
+ });
1396
+ }
1397
+ }
1398
+ exports._validateDisplayField = _validateDisplayField;
1399
+ /**
1400
+ * Remove indexes on fields that don't exist in the layer.
1401
+ * Remove duplicate indexes on the same field.
1402
+ *
1403
+ * @param adminItem layer or table from the service
1404
+ * @param fieldNames string list of fields names
1405
+ * @private
1406
+ */
1407
+ function _validateIndexes(adminItem, fieldNames) {
1408
+ const indexes = adminItem.indexes;
1409
+ /* istanbul ignore else */
1410
+ if (indexes) {
1411
+ const indexedFields = [];
1412
+ adminItem.indexes = indexes.reduce((filtered, index) => {
1413
+ const indexFields = index.fields.split(",");
1414
+ const verifiedFields = [];
1415
+ indexFields.forEach(indexField => {
1416
+ /* istanbul ignore else */
1417
+ if (indexedFields.indexOf(indexField) === -1) {
1418
+ indexedFields.push(indexField);
1419
+ // this is the first index with this field and it should be added if the field exists
1420
+ /* istanbul ignore else */
1421
+ if (fieldNames.indexOf(indexField) > -1) {
1422
+ verifiedFields.push(indexField);
1423
+ }
1424
+ }
1425
+ // else the field has more than one index associated and should not be returned
1426
+ });
1427
+ /* istanbul ignore else */
1428
+ if (verifiedFields.length > 0) {
1429
+ index.fields = verifiedFields.join(",");
1430
+ filtered.push(index);
1431
+ }
1432
+ return filtered;
1433
+ }, []);
1434
+ }
1435
+ }
1436
+ exports._validateIndexes = _validateIndexes;
1437
+ /**
1438
+ * Remove field references from templates that no longer exist.
1439
+ *
1440
+ * @param adminItem layer or table from the service
1441
+ * @param fieldNames string list of fields names
1442
+ * @private
1443
+ */
1444
+ function _validateTemplatesFields(adminItem, fieldNames) {
1445
+ const templates = adminItem.templates;
1446
+ /* istanbul ignore else */
1447
+ if (templates) {
1448
+ adminItem.templates = templates.map(template => {
1449
+ const attributes = (0, generalHelpers_1.getProp)(template, "prototype.attributes");
1450
+ /* istanbul ignore else */
1451
+ if (attributes) {
1452
+ Object.keys(attributes).forEach(k => {
1453
+ /* istanbul ignore else */
1454
+ if (fieldNames.indexOf(k) === -1) {
1455
+ delete attributes[k];
1456
+ }
1457
+ });
1458
+ (0, generalHelpers_1.setProp)(template, "prototype.attributes", attributes);
1459
+ }
1460
+ return template;
1461
+ });
1462
+ }
1463
+ }
1464
+ exports._validateTemplatesFields = _validateTemplatesFields;
1465
+ /**
1466
+ * Remove field references from templates that no longer exist.
1467
+ *
1468
+ * @param adminItem layer or table from the service
1469
+ * @param fieldNames string list of fields names
1470
+ * @private
1471
+ */
1472
+ function _validateTypesTemplates(adminItem, fieldNames) {
1473
+ const types = adminItem.types;
1474
+ /* istanbul ignore else */
1475
+ if (types) {
1476
+ adminItem.types = types.map(t => {
1477
+ _validateTemplatesFields(t, fieldNames);
1478
+ return t;
1479
+ });
1480
+ }
1481
+ }
1482
+ exports._validateTypesTemplates = _validateTypesTemplates;
1483
+ /**
1484
+ * Check if edit feilds exist but with lower case
1485
+ *
1486
+ * @param adminItem layer or table from the service
1487
+ * @param fieldNames string list of fields names
1488
+ * @private
1489
+ */
1490
+ function _validateEditFieldsInfo(adminItem, fieldNames) {
1491
+ const editFieldsInfo = adminItem.editFieldsInfo;
1492
+ /* istanbul ignore else */
1493
+ if (editFieldsInfo) {
1494
+ const editFieldsInfoKeys = Object.keys(editFieldsInfo);
1495
+ editFieldsInfoKeys.forEach(k => {
1496
+ const editFieldName = editFieldsInfo[k];
1497
+ /* istanbul ignore else */
1498
+ if (editFieldName) {
1499
+ fieldNames.some(name => {
1500
+ if (name === editFieldName) {
1501
+ return true;
1502
+ }
1503
+ else if (name === editFieldName.toLowerCase()) {
1504
+ editFieldsInfo[k] = name;
1505
+ return true;
1506
+ }
1507
+ else {
1508
+ return false;
1509
+ }
1510
+ });
1511
+ }
1512
+ });
1513
+ }
1514
+ }
1515
+ exports._validateEditFieldsInfo = _validateEditFieldsInfo;
1516
+ /**
1517
+ *
1518
+ * Templatize all field references within a layer
1519
+ * This is necessary to support potential field name changes when deploying to portal
1520
+ * Portal will force all field names to be lower case
1521
+ *
1522
+ * @param dataItem The data layer instance with field name references within
1523
+ * @param itemID The id for the item that contains this layer.
1524
+ * @param layer JSON return from the layer being templatized.
1525
+ * @param dependencies
1526
+ * @returns An updated instance of the layer
1527
+ * @private
1528
+ */
1529
+ function _templatizeLayerFieldReferences(dataItem, itemID, layer, dependencies) {
1530
+ // This is the value that will be used as the template for adlib replacement
1531
+ const path = itemID + ".layer" + layer.id + ".fields";
1532
+ // Get the field names for various tests
1533
+ const fieldNames = layer.fields.map((f) => f.name);
1534
+ // Update the layer from the items data property
1535
+ if (dataItem) {
1536
+ _templatizeAdminLayerInfoFields(dataItem, dependencies);
1537
+ _templatizePopupInfo(dataItem, layer, path, itemID, fieldNames);
1538
+ }
1539
+ // Update the layer
1540
+ _templatizeAdminLayerInfoFields(layer, dependencies);
1541
+ _templatizeRelationshipFields(layer, itemID);
1542
+ _templatizeDefinitionEditor(layer, path, fieldNames);
1543
+ _templatizeDefinitionExpression(layer, path, fieldNames);
1544
+ _templatizeDrawingInfo(layer, path, fieldNames);
1545
+ _templatizeTemplates(layer, path);
1546
+ _templatizeTypeTemplates(layer, path);
1547
+ _templatizeTimeInfo(layer, path);
1548
+ _templatizeDefinitionQuery(layer, path, fieldNames);
1549
+ }
1550
+ exports._templatizeLayerFieldReferences = _templatizeLayerFieldReferences;
1551
+ /**
1552
+ * Templatize a layers adminLayerInfo by removing properties that will case issues with clone.
1553
+ * Also templatizes the source service name when we are dealing with a view.
1554
+ *
1555
+ * @param layer The layer to be modified
1556
+ * @param dependencies Array of service dependencies
1557
+ * @returns A new copy of the modified adminLayerInfo for the given layer
1558
+ * @private
1559
+ */
1560
+ function _templatizeAdminLayerInfo(layer, dependencies, templateDictionary) {
1561
+ // Create new instance of adminLayerInfo to update for clone
1562
+ const adminLayerInfo = Object.assign({}, layer.adminLayerInfo);
1563
+ _updateGeomFieldName(adminLayerInfo, templateDictionary);
1564
+ (0, generalHelpers_1.deleteProp)(adminLayerInfo, "xssTrustedFields");
1565
+ (0, generalHelpers_1.deleteProp)(adminLayerInfo, "tableName");
1566
+ // Remove unnecessary properties and templatize key properties from viewLayerDefinition
1567
+ /* istanbul ignore else */
1568
+ if (adminLayerInfo.viewLayerDefinition) {
1569
+ const viewDef = Object.assign({}, adminLayerInfo.viewLayerDefinition);
1570
+ _processAdminObject(viewDef, dependencies);
1571
+ // Remove unnecessary properties and templatize key properties from viewLayerDefinition.table
1572
+ /* istanbul ignore else */
1573
+ if (viewDef.table) {
1574
+ _processAdminObject(viewDef.table, dependencies);
1575
+ /* istanbul ignore else */
1576
+ if (viewDef.table.relatedTables) {
1577
+ viewDef.table.relatedTables.forEach((table) => {
1578
+ _processAdminObject(table, dependencies);
1579
+ });
1580
+ }
1581
+ }
1582
+ adminLayerInfo.viewLayerDefinition = viewDef;
1583
+ }
1584
+ return adminLayerInfo;
1585
+ }
1586
+ exports._templatizeAdminLayerInfo = _templatizeAdminLayerInfo;
1587
+ /**
1588
+ * Remove sourceId and templatize the sourceServiceName
1589
+ *
1590
+ * @param object The layer to be modified
1591
+ * @param dependencies Array of service dependencies
1592
+ * @private
1593
+ */
1594
+ function _processAdminObject(object, dependencies) {
1595
+ (0, generalHelpers_1.deleteProp)(object, "sourceId");
1596
+ if (object.hasOwnProperty("sourceServiceName")) {
1597
+ object.sourceServiceName = _templatizeSourceServiceName(object.sourceServiceName, dependencies);
1598
+ }
1599
+ }
1600
+ exports._processAdminObject = _processAdminObject;
1601
+ /**
1602
+ * Templatize the name based on the given dependencies
1603
+ *
1604
+ * @param lookupName The current name from the source service
1605
+ * @param dependencies Array of IDependency for name mapping
1606
+ * @returns The templatized name || undefined when no matching dependency is found
1607
+ * @private
1608
+ */
1609
+ function _templatizeSourceServiceName(lookupName, dependencies) {
1610
+ const deps = dependencies.filter(dependency => dependency.name === lookupName);
1611
+ return deps.length === 1 ? _templatize(deps[0].id, "name") : undefined;
1612
+ }
1613
+ exports._templatizeSourceServiceName = _templatizeSourceServiceName;
1614
+ /**
1615
+ * templatize the fields referenced in adminLayerInfo
1616
+ *
1617
+ * @param layer the layer object with the adminLayerInfo property to templatize
1618
+ * @param basePath path used to de-templatize while deploying
1619
+ * @param itemID the id for the item that contains this layer
1620
+ * @private
1621
+ */
1622
+ function _templatizeAdminLayerInfoFields(layer, dependencies) {
1623
+ // templatize the source layer fields
1624
+ const table = (0, generalHelpers_1.getProp)(layer, "adminLayerInfo.viewLayerDefinition.table");
1625
+ if (table) {
1626
+ let id = _getDependantItemId(table.sourceServiceName, dependencies);
1627
+ const path = id + ".layer" + table.sourceLayerId + ".fields";
1628
+ _templatizeAdminSourceLayerFields(table.sourceLayerFields || [], path);
1629
+ // templatize the releated table fields
1630
+ const relatedTables = (0, generalHelpers_1.getProp)(layer, "adminLayerInfo.viewLayerDefinition.table.relatedTables") || [];
1631
+ if (relatedTables.length > 0) {
1632
+ relatedTables.forEach((t) => {
1633
+ id = _getDependantItemId(t.sourceServiceName, dependencies);
1634
+ const relatedPath = id + ".layer" + t.sourceLayerId + ".fields";
1635
+ _templatizeTopFilter(t.topFilter || {}, relatedPath);
1636
+ _templatizeAdminSourceLayerFields(t.sourceLayerFields || [], relatedPath);
1637
+ const parentKeyFields = t.parentKeyFields || [];
1638
+ t.parentKeyFields = parentKeyFields.map((f) => {
1639
+ return _templatize(path, f, "name");
1640
+ });
1641
+ const keyFields = t.keyFields || [];
1642
+ t.keyFields = keyFields.map((f) => {
1643
+ return _templatize(relatedPath, f, "name");
1644
+ });
1645
+ });
1646
+ }
1647
+ }
1648
+ }
1649
+ exports._templatizeAdminLayerInfoFields = _templatizeAdminLayerInfoFields;
1650
+ /**
1651
+ * find id based on dependency name
1652
+ *
1653
+ * @param lookupName name of dependency we want to find the id of
1654
+ * @param dependencies array of item dependencies
1655
+ * @private
1656
+ */
1657
+ function _getDependantItemId(lookupName, dependencies) {
1658
+ const deps = dependencies.filter(dependency => dependency.name === lookupName);
1659
+ return deps.length === 1 ? deps[0].id : "";
1660
+ }
1661
+ exports._getDependantItemId = _getDependantItemId;
1662
+ /**
1663
+ * templatize the sourceLayerFields referenced in adminLayerInfo
1664
+ *
1665
+ * @param fields array of sourceLayerFields to templatize
1666
+ * @param basePath path used to de-templatize while deploying
1667
+ * @private
1668
+ */
1669
+ function _templatizeAdminSourceLayerFields(fields, basePath) {
1670
+ fields.forEach(f => _templatizeProperty(f, "source", basePath, "name"));
1671
+ }
1672
+ exports._templatizeAdminSourceLayerFields = _templatizeAdminSourceLayerFields;
1673
+ /**
1674
+ * templatize the topFilter property from adminLayerInfo related tables
1675
+ *
1676
+ * @param topFilter the topFilter object to templatize
1677
+ * @param basePath path used to de-templatize while deploying
1678
+ * @private
1679
+ */
1680
+ function _templatizeTopFilter(topFilter, basePath) {
1681
+ /* istanbul ignore else */
1682
+ if (topFilter) {
1683
+ // templatize the orderByFields prop
1684
+ const orderByFields = topFilter["orderByFields"] || "";
1685
+ /* istanbul ignore else */
1686
+ if (orderByFields !== "") {
1687
+ const orderByField = orderByFields.split(" ")[0];
1688
+ topFilter.orderByFields = topFilter.orderByFields.replace(orderByField, _templatize(basePath, orderByField, "name"));
1689
+ }
1690
+ const groupByFields = topFilter["groupByFields"] || "";
1691
+ /* istanbul ignore else */
1692
+ if (groupByFields !== "") {
1693
+ const _groupByFields = groupByFields.split(",");
1694
+ /* istanbul ignore else */
1695
+ if (_groupByFields.length > 0) {
1696
+ const mappedFields = _groupByFields.map((f) => {
1697
+ return _templatize(basePath, f, "name");
1698
+ });
1699
+ topFilter.groupByFields = mappedFields.join(",");
1700
+ }
1701
+ }
1702
+ }
1703
+ }
1704
+ exports._templatizeTopFilter = _templatizeTopFilter;
1705
+ /**
1706
+ * templatize the relationships key fields using the related table id in the basePath
1707
+ *
1708
+ * @param layer the layer that has the relationships to templatize
1709
+ * @param itemID the id of the item that contains the related table
1710
+ * @private
1711
+ */
1712
+ function _templatizeRelationshipFields(layer, itemID) {
1713
+ if (layer && layer.relationships) {
1714
+ const relationships = layer.relationships;
1715
+ relationships.forEach(r => {
1716
+ /* istanbul ignore else */
1717
+ if (r.keyField) {
1718
+ const basePath = itemID + ".layer" + layer.id + ".fields";
1719
+ _templatizeProperty(r, "keyField", basePath, "name");
1720
+ }
1721
+ });
1722
+ }
1723
+ }
1724
+ exports._templatizeRelationshipFields = _templatizeRelationshipFields;
1725
+ /**
1726
+ * templatize the popupInfo
1727
+ *
1728
+ * @param layerDefinition the layerDefinition that has the popupInfo to templatize
1729
+ * @param layer the JSON for the layer being templatized
1730
+ * @param basePath path used to de-templatize while deploying
1731
+ * @param itemID the id for the item that contains this layer
1732
+ * @param fieldNames array of fieldNames
1733
+ * @private
1734
+ */
1735
+ function _templatizePopupInfo(layerDefinition, layer, basePath, itemID, fieldNames) {
1736
+ // the data layer does not have the fields...will need to get those
1737
+ // from the associated layer json
1738
+ if (fieldNames && layerDefinition.popupInfo) {
1739
+ const popupInfo = layerDefinition.popupInfo;
1740
+ _templatizeName(popupInfo, "title", fieldNames, basePath);
1741
+ _templatizeName(popupInfo, "description", fieldNames, basePath);
1742
+ const fieldInfos = popupInfo.fieldInfos || [];
1743
+ _templatizePopupInfoFieldInfos(fieldInfos, layer, itemID, basePath);
1744
+ const expressionInfos = popupInfo.expressionInfos || [];
1745
+ _templatizeExpressionInfos(expressionInfos, fieldNames, basePath);
1746
+ const popupElements = popupInfo.popupElements || [];
1747
+ _templatizePopupElements(popupElements, basePath, layer, itemID, fieldNames);
1748
+ const mediaInfos = popupInfo.mediaInfos || [];
1749
+ _templatizeMediaInfos(mediaInfos, fieldNames, basePath, layer, itemID);
1750
+ }
1751
+ }
1752
+ exports._templatizePopupInfo = _templatizePopupInfo;
1753
+ /**
1754
+ * templatize field name when referenced like this: \{\{fieldName\}\}
1755
+ * checks each field name from the layer
1756
+ *
1757
+ * @param object with the property to test for a field name
1758
+ * @param property that could have a field name referenced
1759
+ * @param fieldNames array for field names for the layer
1760
+ * @param basePath path used to de-templatize while deploying
1761
+ * @private
1762
+ */
1763
+ function _templatizeName(object, property, fieldNames, basePath) {
1764
+ if (object.hasOwnProperty(property)) {
1765
+ fieldNames.forEach(name => {
1766
+ // Only test and replace instance of the name so any enclosing characters
1767
+ // will be retained
1768
+ const regEx = new RegExp("(\\b" + name + "\\b(?![}]{2}))", "gm");
1769
+ if (regEx.test(object[property])) {
1770
+ object[property] = object[property].replace(regEx, _templatize(basePath, name, "name"));
1771
+ }
1772
+ });
1773
+ }
1774
+ }
1775
+ exports._templatizeName = _templatizeName;
1776
+ /**
1777
+ * templatize field name when referenced like this: \{\{fieldName\}\}
1778
+ * checks each field name from the layer
1779
+ *
1780
+ * @param fieldInfos object that contains the popups fieldInfos
1781
+ * @param layer json of layer being cloned
1782
+ * @param itemID id of the item that contains the current layer
1783
+ * @param basePath path used to de-templatize while deploying
1784
+ * @private
1785
+ */
1786
+ function _templatizePopupInfoFieldInfos(fieldInfos, layer, itemID, basePath) {
1787
+ fieldInfos.forEach((f) => {
1788
+ f.fieldName = _templatizeFieldName(f.fieldName, layer, itemID, basePath);
1789
+ });
1790
+ }
1791
+ exports._templatizePopupInfoFieldInfos = _templatizePopupInfoFieldInfos;
1792
+ /**
1793
+ * templatize field name when referenced like this: \{\{fieldName\}\}
1794
+ * checks each field name from the layer
1795
+ *
1796
+ * @param name the field name to templatize
1797
+ * @param layer json of layer being cloned
1798
+ * @param itemID id of the item that contains the current layer
1799
+ * @param basePath path used to de-templatize while deploying
1800
+ * @private
1801
+ */
1802
+ function _templatizeFieldName(name, layer, itemID, basePath) {
1803
+ if (name.indexOf("relationships/") > -1) {
1804
+ const rels = name.split("/");
1805
+ const relationshipId = rels[1];
1806
+ const adminRelatedTables = (0, generalHelpers_1.getProp)(layer, "adminLayerInfo.viewLayerDefinition.table.relatedTables");
1807
+ const relatedTables = layer.relationships || adminRelatedTables;
1808
+ /* istanbul ignore else */
1809
+ if (relatedTables && relatedTables.length > parseInt(relationshipId, 10)) {
1810
+ const relatedTable = relatedTables[relationshipId];
1811
+ // the layers relationships stores the property as relatedTableId
1812
+ // the layers adminLayerInfo relatedTables stores the property as sourceLayerId
1813
+ const prop = (0, generalHelpers_1.getProp)(relatedTable, "relatedTableId")
1814
+ ? "relatedTableId"
1815
+ : "sourceLayerId";
1816
+ const _basePath = itemID + ".layer" + relatedTable[prop] + ".fields";
1817
+ rels[2] = _templatize(_basePath, rels[2], "name");
1818
+ name = rels.join("/");
1819
+ }
1820
+ }
1821
+ else {
1822
+ // do not need to templatize expression references as the expression
1823
+ // itself will be templatized
1824
+ if (name.indexOf("expression/") === -1) {
1825
+ name = _templatize(basePath, name, "name");
1826
+ }
1827
+ }
1828
+ return name;
1829
+ }
1830
+ exports._templatizeFieldName = _templatizeFieldName;
1831
+ /**
1832
+ * templatize field name when referenced in expressionInfos
1833
+ *
1834
+ * @param expressionInfos the popups expressionInfos to check
1835
+ * @param fieldNames array of the layers field names
1836
+ * @param basePath path used to de-templatize while deploying
1837
+ * @private
1838
+ */
1839
+ function _templatizeExpressionInfos(expressionInfos, fieldNames, basePath) {
1840
+ return expressionInfos.map((i) => {
1841
+ fieldNames.forEach(name => {
1842
+ i.expression = _templatizeArcadeExpressions(i.expression, name, basePath);
1843
+ });
1844
+ return i;
1845
+ });
1846
+ }
1847
+ exports._templatizeExpressionInfos = _templatizeExpressionInfos;
1848
+ /**
1849
+ * templatize field name when referenced in popupElelments
1850
+ *
1851
+ * @param popupElelments the popups popupElelments to check
1852
+ * @param basePath path used to de-templatize while deploying
1853
+ * @param layer json of layer being cloned
1854
+ * @param itemID id of the item that contains the current layer
1855
+ * @param fieldNames array of field names
1856
+ * @private
1857
+ */
1858
+ function _templatizePopupElements(popupElelments, basePath, layer, itemID, fieldNames) {
1859
+ popupElelments.forEach((pe) => {
1860
+ if (pe.hasOwnProperty("fieldInfos")) {
1861
+ _templatizePopupInfoFieldInfos(pe.fieldInfos, layer, itemID, basePath);
1862
+ }
1863
+ if (pe.hasOwnProperty("mediaInfos")) {
1864
+ _templatizeMediaInfos(pe.mediaInfos, fieldNames, basePath, layer, itemID);
1865
+ }
1866
+ });
1867
+ }
1868
+ exports._templatizePopupElements = _templatizePopupElements;
1869
+ /**
1870
+ * templatize field name when referenced in mediaInfos
1871
+ *
1872
+ * @param mediaInfos the popups mediaInfos to check
1873
+ * @param fieldNames array of the layers field names
1874
+ * @param basePath path used to de-templatize while deploying
1875
+ * @param layer json of layer being cloned
1876
+ * @param itemID id of the item that contains the current layer
1877
+ * @private
1878
+ */
1879
+ function _templatizeMediaInfos(mediaInfos, fieldNames, basePath, layer, itemId) {
1880
+ // templatize various properties of mediaInfos
1881
+ const props = ["title", "caption"];
1882
+ props.forEach(p => _templatizeName(mediaInfos, p, fieldNames, basePath));
1883
+ mediaInfos.forEach((mi) => {
1884
+ /* istanbul ignore else */
1885
+ if (mi.hasOwnProperty("value")) {
1886
+ const v = mi.value;
1887
+ const vfields = v.fields || [];
1888
+ v.fields = vfields.map(f => _templatizeFieldName(f, layer, itemId, basePath));
1889
+ if (v.hasOwnProperty("normalizeField")) {
1890
+ _templatizeProperty(v, "normalizeField", basePath, "name");
1891
+ }
1892
+ /* istanbul ignore else */
1893
+ if (v.hasOwnProperty("tooltipField")) {
1894
+ v.tooltipField = _templatizeFieldName(v.tooltipField, layer, itemId, basePath);
1895
+ }
1896
+ }
1897
+ });
1898
+ }
1899
+ exports._templatizeMediaInfos = _templatizeMediaInfos;
1900
+ /**
1901
+ * templatize field names when referenced in definitionEditor
1902
+ *
1903
+ * @param layer the layer with the definition editor
1904
+ * @param basePath path used to de-templatize while deploying
1905
+ * @param fieldNames json of layer being cloned
1906
+ * @private
1907
+ */
1908
+ function _templatizeDefinitionEditor(layer, basePath, fieldNames) {
1909
+ if (layer) {
1910
+ const defEditor = layer.definitionEditor || {};
1911
+ /* istanbul ignore else */
1912
+ if (defEditor) {
1913
+ const inputs = defEditor.inputs;
1914
+ if (inputs) {
1915
+ inputs.forEach(i => {
1916
+ /* istanbul ignore else */
1917
+ if (i.parameters) {
1918
+ i.parameters.forEach((p) => {
1919
+ _templatizeProperty(p, "fieldName", basePath, "name");
1920
+ });
1921
+ }
1922
+ });
1923
+ }
1924
+ if (defEditor.hasOwnProperty("parameterizedExpression")) {
1925
+ defEditor.parameterizedExpression = _templatizeSimpleName(defEditor.parameterizedExpression || "", basePath, fieldNames, "name");
1926
+ }
1927
+ }
1928
+ }
1929
+ }
1930
+ exports._templatizeDefinitionEditor = _templatizeDefinitionEditor;
1931
+ /**
1932
+ * templatize field names when referenced in definitionExpression
1933
+ *
1934
+ * @param layer the layer with the definition editor
1935
+ * @param basePath path used to de-templatize while deploying
1936
+ * @param fieldNames array of field names
1937
+ * @private
1938
+ */
1939
+ function _templatizeDefinitionExpression(layer, basePath, fieldNames) {
1940
+ if (layer && layer.hasOwnProperty("definitionExpression")) {
1941
+ layer.definitionExpression = _templatizeSimpleName(layer.definitionExpression || "", basePath, fieldNames, "name");
1942
+ }
1943
+ }
1944
+ exports._templatizeDefinitionExpression = _templatizeDefinitionExpression;
1945
+ /**
1946
+ * Case sensitive test for field names that appear anywhere within a string
1947
+ *
1948
+ * @param expression the expression to test for field name references
1949
+ * @param basePath path used to de-templatize while deploying
1950
+ * @param fieldNames array of the layers field names
1951
+ * @private
1952
+ */
1953
+ function _templatizeSimpleName(expression, basePath, fieldNames, suffix) {
1954
+ fieldNames.forEach(name => {
1955
+ // look for the name but not if its followed by }}
1956
+ const regEx = new RegExp("\\b" + name + "\\b(?![}]{2})", "gm");
1957
+ if (expression && regEx.test(expression)) {
1958
+ expression = expression.replace(regEx, _templatize(basePath, name, suffix));
1959
+ }
1960
+ });
1961
+ return expression;
1962
+ }
1963
+ exports._templatizeSimpleName = _templatizeSimpleName;
1964
+ /**
1965
+ * Templatize field references within a layers drawingInfo
1966
+ *
1967
+ * @param layer the data layer
1968
+ * @param basePath path used to de-templatize while deploying
1969
+ * @param fieldNames array of the layers field names
1970
+ * @private
1971
+ */
1972
+ function _templatizeDrawingInfo(layer, basePath, fieldNames) {
1973
+ if (layer) {
1974
+ const drawingInfo = layer.drawingInfo;
1975
+ if (drawingInfo) {
1976
+ // templatize the renderer fields
1977
+ const renderer = drawingInfo.renderer || {};
1978
+ _templatizeRenderer(renderer, basePath, fieldNames);
1979
+ // templatize the labelingInfo
1980
+ const labelingInfo = drawingInfo.labelingInfo || [];
1981
+ _templatizeLabelingInfo(labelingInfo, basePath, fieldNames);
1982
+ }
1983
+ }
1984
+ }
1985
+ exports._templatizeDrawingInfo = _templatizeDrawingInfo;
1986
+ /**
1987
+ * Templatize field references within a layers drawingInfo
1988
+ *
1989
+ * @param renderer the layers renderer
1990
+ * @param basePath path used to de-templatize while deploying
1991
+ * @param fieldNames array of the layers field names
1992
+ * @private
1993
+ */
1994
+ function _templatizeRenderer(renderer, basePath, fieldNames) {
1995
+ switch (renderer.type) {
1996
+ case "classBreaks":
1997
+ case "uniqueValue":
1998
+ case "predominance":
1999
+ case "simple":
2000
+ case "heatmap":
2001
+ _templatizeGenRenderer(renderer, basePath, fieldNames);
2002
+ break;
2003
+ case "temporal":
2004
+ _templatizeTemporalRenderer(renderer, basePath, fieldNames);
2005
+ break;
2006
+ default:
2007
+ break;
2008
+ }
2009
+ }
2010
+ exports._templatizeRenderer = _templatizeRenderer;
2011
+ /**
2012
+ * Templatize field references within a layers renderer
2013
+ *
2014
+ * @param renderer the renderer object to check for field references
2015
+ * @param basePath path used to de-templatize while deploying
2016
+ * @param fieldNames array of field names that will be used to search expressions
2017
+ * @private
2018
+ */
2019
+ function _templatizeGenRenderer(renderer, basePath, fieldNames) {
2020
+ /* istanbul ignore else */
2021
+ if (renderer) {
2022
+ // update authoringInfo
2023
+ const authoringInfo = renderer.authoringInfo;
2024
+ if (authoringInfo) {
2025
+ _templatizeAuthoringInfo(authoringInfo, basePath, fieldNames);
2026
+ }
2027
+ const props = ["field", "normalizationField"];
2028
+ props.forEach(p => _templatizeProperty(renderer, p, basePath, "name"));
2029
+ const fieldNameProps = ["field1", "field2", "field3"];
2030
+ fieldNameProps.forEach(fnP => _templatizeProperty(renderer, fnP, basePath, "name"));
2031
+ // When an attribute name is specified, it's enclosed in square brackets
2032
+ const rExp = renderer.rotationExpression;
2033
+ if (rExp) {
2034
+ fieldNames.forEach(name => {
2035
+ const regEx = new RegExp("(\\[" + name + "\\])", "gm");
2036
+ if (regEx.test(rExp)) {
2037
+ renderer.rotationExpression = rExp.replace(regEx, "[" + _templatize(basePath, name, "name") + "]");
2038
+ }
2039
+ });
2040
+ }
2041
+ // update valueExpression
2042
+ if (renderer.valueExpression) {
2043
+ fieldNames.forEach(name => {
2044
+ renderer.valueExpression = _templatizeArcadeExpressions(renderer.valueExpression, name, basePath);
2045
+ });
2046
+ }
2047
+ // update visualVariables
2048
+ const visualVariables = renderer.visualVariables;
2049
+ if (visualVariables) {
2050
+ visualVariables.forEach(v => {
2051
+ props.forEach(p => _templatizeProperty(v, p, basePath, "name"));
2052
+ if (v.valueExpression) {
2053
+ fieldNames.forEach(name => {
2054
+ v.valueExpression = _templatizeArcadeExpressions(v.valueExpression, name, basePath);
2055
+ });
2056
+ }
2057
+ });
2058
+ }
2059
+ }
2060
+ }
2061
+ exports._templatizeGenRenderer = _templatizeGenRenderer;
2062
+ /**
2063
+ * Templatize field references within a layers renderer
2064
+ *
2065
+ * @param renderer the renderer object to check for field references
2066
+ * @param basePath path used to de-templatize while deploying
2067
+ * @param fieldNames array of field names that will be used to search expressions
2068
+ * @private
2069
+ */
2070
+ function _templatizeTemporalRenderer(renderer, basePath, fieldNames) {
2071
+ const renderers = [
2072
+ renderer.latestObservationRenderer,
2073
+ renderer.observationRenderer,
2074
+ renderer.trackRenderer
2075
+ ];
2076
+ renderers.forEach(r => {
2077
+ _templatizeRenderer(r, basePath, fieldNames);
2078
+ });
2079
+ }
2080
+ exports._templatizeTemporalRenderer = _templatizeTemporalRenderer;
2081
+ /**
2082
+ * Templatize renderers authoringInfo
2083
+ *
2084
+ * @param authoringInfo object containing metadata about the authoring process
2085
+ * @param basePath path used to de-templatize while deploying
2086
+ * @param fieldNames the name of fields from the layer
2087
+ * @private
2088
+ */
2089
+ function _templatizeAuthoringInfo(authoringInfo, basePath, fieldNames) {
2090
+ /* istanbul ignore else */
2091
+ if (authoringInfo) {
2092
+ const props = ["field", "normalizationField"];
2093
+ const field1 = authoringInfo.field1;
2094
+ props.forEach(p => _templatizeProperty(field1, p, basePath, "name"));
2095
+ const field2 = authoringInfo.field2;
2096
+ props.forEach(p => _templatizeProperty(field2, p, basePath, "name"));
2097
+ const fields = authoringInfo.fields;
2098
+ if (fields) {
2099
+ authoringInfo.fields = fields.map(f => _templatize(basePath, f, "name"));
2100
+ }
2101
+ const vProps = ["endTime", "field", "startTime"];
2102
+ const vVars = authoringInfo.visualVariables;
2103
+ if (vVars) {
2104
+ vProps.forEach(p => {
2105
+ // endTime and startTime may or may not be a field name
2106
+ if (fieldNames.indexOf(vVars[p]) > -1) {
2107
+ _templatizeProperty(vVars, p, basePath, "name");
2108
+ }
2109
+ });
2110
+ }
2111
+ }
2112
+ }
2113
+ exports._templatizeAuthoringInfo = _templatizeAuthoringInfo;
2114
+ /**
2115
+ * Templatize field references within an arcade expression
2116
+ *
2117
+ * @param text the text that contains the expression
2118
+ * @param fieldName name of the field to test for
2119
+ * @param basePath path used to de-templatize while deploying
2120
+ * @private
2121
+ */
2122
+ function _templatizeArcadeExpressions(text, fieldName, basePath) {
2123
+ const t = _templatize(basePath, fieldName, "name");
2124
+ if (text) {
2125
+ // test for $feature. notation
2126
+ // captures VOTED_DEM_2012 from $feature.VOTED_DEM_2012
2127
+ let exp = "(?:\\$feature\\.)(" + fieldName + ")\\b";
2128
+ let regEx = new RegExp(exp, "gm");
2129
+ text = regEx.test(text) ? text.replace(regEx, "$feature." + t) : text;
2130
+ // test for $feature[] notation
2131
+ // captures VOTED_DEM_2012 from $feature["VOTED_DEM_2012"]
2132
+ // captures VOTED_DEM_2012 from $feature['VOTED_DEM_2012']
2133
+ // captures VOTED_DEM_2012 from $feature[VOTED_DEM_2012]
2134
+ exp = "(?:[$]feature)(\\[\\\"?\\'?)" + fieldName + "(\\\"?\\'?\\])";
2135
+ regEx = new RegExp(exp, "gm");
2136
+ let result = regEx.exec(text);
2137
+ if (result) {
2138
+ text = text.replace(regEx, "$feature" + result[1] + t + result[2]);
2139
+ }
2140
+ // test for $feature[] with join case
2141
+ // captures VOTED_DEM_2016 from $feature["COUNTY_ID.VOTED_DEM_2016"]
2142
+ exp =
2143
+ "(?:[$]feature)(\\[\\\"?\\'?)(\\w+)[.]" + fieldName + "(\\\"?\\'?\\])";
2144
+ regEx = new RegExp(exp, "gm");
2145
+ result = regEx.exec(text);
2146
+ if (result && result.length > 3) {
2147
+ // TODO result[2] is the table name...this needs to be templatized as well
2148
+ text = text.replace(regEx, "$feature" + result[1] + result[2] + "." + t + result[3]);
2149
+ }
2150
+ // test for "fieldName"
2151
+ // captures fieldName from "var names = ["fieldName", "fieldName2"]..."
2152
+ // captures fieldName from "var names = ['fieldName', 'fieldName2']..."
2153
+ exp = "(\\\"|\\')+" + fieldName + "(\\\"|\\')+";
2154
+ regEx = new RegExp(exp, "gm");
2155
+ result = regEx.exec(text);
2156
+ if (result) {
2157
+ text = text.replace(regEx, result[1] + t + result[2]);
2158
+ }
2159
+ }
2160
+ return text;
2161
+ }
2162
+ exports._templatizeArcadeExpressions = _templatizeArcadeExpressions;
2163
+ /**
2164
+ * templatize field names when referenced in the layers labelingInfo
2165
+ *
2166
+ * @param labelingInfo the object that contains the labelingInfo
2167
+ * @param basePath path used to de-templatize while deploying
2168
+ * @param fieldNames array of the layers field names
2169
+ * @private
2170
+ */
2171
+ function _templatizeLabelingInfo(labelingInfo, basePath, fieldNames) {
2172
+ labelingInfo.forEach((li) => {
2173
+ /* istanbul ignore else */
2174
+ if (li.hasOwnProperty("fieldInfos")) {
2175
+ const fieldInfos = li.fieldInfos || [];
2176
+ fieldInfos.forEach(fi => _templatizeProperty(fi, "fieldName", basePath, "name"));
2177
+ }
2178
+ const labelExp = li.labelExpression || "";
2179
+ const labelExpInfo = li.labelExpressionInfo || {};
2180
+ fieldNames.forEach(n => {
2181
+ const t = _templatize(basePath, n, "name");
2182
+ // check for [fieldName] or ["fieldName"]
2183
+ const regExBracket = new RegExp('(\\[\\"*)+(' + n + ')(\\"*\\])+', "gm");
2184
+ let result = regExBracket.exec(labelExp);
2185
+ if (result) {
2186
+ li.labelExpression = labelExp.replace(regExBracket, result[1] + t + result[3]);
2187
+ }
2188
+ /* istanbul ignore else */
2189
+ if (labelExpInfo.value) {
2190
+ let v = labelExpInfo.value;
2191
+ // check for {fieldName}
2192
+ const regExCurly = new RegExp("(\\{" + n + "\\})", "gm");
2193
+ v = regExCurly.test(v) ? v.replace(regExCurly, "{" + t + "}") : v;
2194
+ // check for [fieldName] or ["fieldName"]
2195
+ result = regExBracket.exec(v);
2196
+ v = result ? v.replace(regExBracket, result[1] + t + result[3]) : v;
2197
+ li.labelExpressionInfo.value = v;
2198
+ }
2199
+ /* istanbul ignore else */
2200
+ if (labelExpInfo.expression) {
2201
+ li.labelExpressionInfo.expression = _templatizeArcadeExpressions(labelExpInfo.expression, n, basePath);
2202
+ }
2203
+ });
2204
+ });
2205
+ }
2206
+ exports._templatizeLabelingInfo = _templatizeLabelingInfo;
2207
+ /**
2208
+ * templatize the layers editing templates
2209
+ *
2210
+ * @param layer the data layer being cloned
2211
+ * @param basePath path used to de-templatize while deploying
2212
+ * @private
2213
+ */
2214
+ function _templatizeTemplates(layer, basePath) {
2215
+ const templates = layer.templates || [];
2216
+ templates.forEach(t => {
2217
+ const attributes = (0, generalHelpers_1.getProp)(t, "prototype.attributes");
2218
+ const _attributes = _templatizeKeys(attributes, basePath, "name");
2219
+ /* istanbul ignore else */
2220
+ if (_attributes) {
2221
+ t.prototype.attributes = _attributes;
2222
+ }
2223
+ });
2224
+ }
2225
+ exports._templatizeTemplates = _templatizeTemplates;
2226
+ /**
2227
+ * templatize the layer types and templates
2228
+ *
2229
+ * @param layer the data layer being cloned
2230
+ * @param basePath path used to de-templatize while deploying
2231
+ * @private
2232
+ */
2233
+ function _templatizeTypeTemplates(layer, basePath) {
2234
+ const types = layer.types;
2235
+ if (types && Array.isArray(types) && types.length > 0) {
2236
+ types.forEach((type) => {
2237
+ const domains = _templatizeKeys(type.domains, basePath, "name");
2238
+ /* istanbul ignore else */
2239
+ if (domains) {
2240
+ type.domains = domains;
2241
+ }
2242
+ const templates = type.templates;
2243
+ /* istanbul ignore else */
2244
+ if (templates && templates.length > 0) {
2245
+ templates.forEach((t) => {
2246
+ const attributes = (0, generalHelpers_1.getProp)(t, "prototype.attributes");
2247
+ const _attributes = _templatizeKeys(attributes, basePath, "name");
2248
+ /* istanbul ignore else */
2249
+ if (_attributes) {
2250
+ t.prototype.attributes = _attributes;
2251
+ }
2252
+ });
2253
+ }
2254
+ });
2255
+ }
2256
+ }
2257
+ exports._templatizeTypeTemplates = _templatizeTypeTemplates;
2258
+ /**
2259
+ * templatize object keys
2260
+ *
2261
+ * @param obj the object to templatize
2262
+ * @param basePath path used to de-templatize while deploying
2263
+ * @param suffix expected suffix for template variable
2264
+ * @private
2265
+ */
2266
+ function _templatizeKeys(obj, basePath, suffix) {
2267
+ let _obj;
2268
+ /* istanbul ignore else */
2269
+ if (obj) {
2270
+ _obj = {};
2271
+ const objKeys = Object.keys(obj);
2272
+ /* istanbul ignore else */
2273
+ if (objKeys && objKeys.length > 0) {
2274
+ objKeys.forEach(k => {
2275
+ _obj[_templatize(basePath, k, suffix)] = obj[k];
2276
+ });
2277
+ }
2278
+ }
2279
+ return _obj;
2280
+ }
2281
+ exports._templatizeKeys = _templatizeKeys;
2282
+ /**
2283
+ * templatize fields referenced in the layers time info
2284
+ *
2285
+ * @param layer the data layer being cloned
2286
+ * @param basePath path used to de-templatize while deploying
2287
+ * @private
2288
+ */
2289
+ function _templatizeTimeInfo(layer, basePath) {
2290
+ if (layer.timeInfo) {
2291
+ const timeInfo = layer.timeInfo;
2292
+ const timeProps = [
2293
+ "endTimeField",
2294
+ "startTimeField",
2295
+ "trackIdField"
2296
+ ];
2297
+ timeProps.forEach(t => {
2298
+ if (timeInfo[t] !== "") {
2299
+ _templatizeProperty(timeInfo, t, basePath, "name");
2300
+ }
2301
+ else {
2302
+ timeInfo[t] = null;
2303
+ }
2304
+ });
2305
+ }
2306
+ }
2307
+ exports._templatizeTimeInfo = _templatizeTimeInfo;
2308
+ /**
2309
+ * templatize the layers definition query
2310
+ *
2311
+ * @param layer the data layer being cloned
2312
+ * @param basePath path used to de-templatize while deploying
2313
+ * @param fieldNames array of the layers field names
2314
+ * @private
2315
+ */
2316
+ function _templatizeDefinitionQuery(layer, basePath, fieldNames) {
2317
+ // templatize view definition query
2318
+ if (layer && layer.hasOwnProperty("viewDefinitionQuery")) {
2319
+ layer.viewDefinitionQuery = _templatizeSimpleName(layer.viewDefinitionQuery || "", basePath, fieldNames, "name");
2320
+ }
2321
+ if (layer && layer.hasOwnProperty("definitionQuery")) {
2322
+ layer.definitionQuery = _templatizeSimpleName(layer.definitionQuery || "", basePath, fieldNames, "name");
2323
+ }
2324
+ }
2325
+ exports._templatizeDefinitionQuery = _templatizeDefinitionQuery;
2326
+ /**
2327
+ * Helper function to create the name mapping used to
2328
+ * de-templatize the field reference
2329
+ *
2330
+ * @param fieldInfos the object that stores the cached information
2331
+ * @param id the id for the current layer being processed
2332
+ * @private
2333
+ */
2334
+ function _getNameMapping(fieldInfos, id) {
2335
+ // create name mapping
2336
+ const fInfo = fieldInfos[id];
2337
+ const nameMapping = {};
2338
+ const newFields = fInfo.newFields;
2339
+ const newFieldNames = newFields
2340
+ ? newFields.map((f) => f.name)
2341
+ : [];
2342
+ const sourceFields = fInfo.sourceFields || [];
2343
+ sourceFields.forEach((field) => {
2344
+ const lName = String(field.name).toLowerCase();
2345
+ newFields.forEach((f) => {
2346
+ // Names can change more than case
2347
+ if (newFieldNames.indexOf(field.name) === -1 &&
2348
+ newFieldNames.indexOf(lName) === -1) {
2349
+ // If both new (f) and source (field) aliases are defined and are equal, map the source name to the new name
2350
+ if (f.alias && f.alias === field.alias) {
2351
+ nameMapping[lName] = {
2352
+ name: f.name,
2353
+ alias: f.alias,
2354
+ type: f.type ? f.type : ""
2355
+ };
2356
+ }
2357
+ }
2358
+ if (String(f.name).toLowerCase() === lName) {
2359
+ nameMapping[lName] = {
2360
+ name: f.name,
2361
+ alias: f.alias ? f.alias : "",
2362
+ type: f.type ? f.type : ""
2363
+ };
2364
+ }
2365
+ });
2366
+ });
2367
+ // update for editFieldsInfo
2368
+ if (fInfo.editFieldsInfo && fInfo.newEditFieldsInfo) {
2369
+ const efi = JSON.parse(JSON.stringify(fInfo.editFieldsInfo));
2370
+ const newEfi = JSON.parse(JSON.stringify(fInfo.newEditFieldsInfo));
2371
+ const nameMappingKeys = Object.keys(nameMapping);
2372
+ Object.keys(efi).forEach(k => {
2373
+ const lowerEfi = String(efi[k]).toLowerCase();
2374
+ if ((nameMappingKeys.indexOf(lowerEfi) === -1 ||
2375
+ nameMapping[lowerEfi].name !== newEfi[k]) &&
2376
+ newFieldNames.indexOf(lowerEfi) > -1) {
2377
+ // Only add delete fields if source schema changes allowed
2378
+ /* istanbul ignore else */
2379
+ if (fInfo.sourceSchemaChangesAllowed && !fInfo.isView) {
2380
+ /* istanbul ignore else */
2381
+ if (!fInfo.hasOwnProperty("deleteFields")) {
2382
+ fInfo.deleteFields = [];
2383
+ }
2384
+ // This issue only occurs on portal so we
2385
+ // need to delete the lcase version of the field
2386
+ fInfo.deleteFields.push(lowerEfi);
2387
+ }
2388
+ // editFieldsInfo only has the name and not the alias and type
2389
+ let sourceEfiField;
2390
+ fInfo.sourceFields.some((sf) => {
2391
+ if (sf.name === efi[k]) {
2392
+ sourceEfiField = sf;
2393
+ }
2394
+ return sf.name === efi[k];
2395
+ });
2396
+ nameMapping[lowerEfi] = {
2397
+ name: newEfi[k],
2398
+ alias: sourceEfiField && sourceEfiField.alias ? sourceEfiField.alias : "",
2399
+ type: sourceEfiField && sourceEfiField.type ? sourceEfiField.type : ""
2400
+ };
2401
+ }
2402
+ });
2403
+ (0, generalHelpers_1.deleteProp)(fInfo, "sourceSchemaChangesAllowed");
2404
+ (0, generalHelpers_1.deleteProp)(fInfo, "editFieldsInfo");
2405
+ (0, generalHelpers_1.deleteProp)(fInfo, "newEditFieldsInfo");
2406
+ (0, generalHelpers_1.deleteProp)(fInfo, "isView");
2407
+ }
2408
+ return nameMapping;
2409
+ }
2410
+ exports._getNameMapping = _getNameMapping;
2411
+ /**
2412
+ * Helper function to ensure same chunk size value is used in multiple locations
2413
+ *
2414
+ * @returns a number that represents how many layers should be included per addToDef call
2415
+ * @private
2416
+ */
2417
+ function _getLayerChunkSize() {
2418
+ return 20;
2419
+ }
2420
+ exports._getLayerChunkSize = _getLayerChunkSize;
2421
2421
  //# sourceMappingURL=featureServiceHelpers.js.map