SaveAgentView.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| IBM Cognos Products: Content Explorer
  5. *| (C) Copyright IBM Corp. 2022
  6. *|
  7. *| US Government Users Restricted Rights - Use, duplication or disclosure
  8. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  9. *+------------------------------------------------------------------------+
  10. */
  11. define([
  12. 'bi/content_apps/nls/StringResource',
  13. 'bacontentnav/lib/@waca/core-client/js/core-client/utils/Deferred'
  14. ], function(StringResource, Deferred) {
  15. 'use strict';
  16. return {
  17. saveAgentView: function(cmProperties, context) {
  18. var saveDeferred = new Deferred();
  19. window.parent.require(['bi/commons/ui/content/dialog/SaveAsDialog'], function(SaveAsDialog) {
  20. var dialog = new SaveAsDialog({
  21. glassContext: context.glassContext,
  22. defaultFileName: StringResource.get('agentViewOf', { agentName: cmProperties.defaultName }),
  23. ancestors: cmProperties.ancestors,
  24. service: {
  25. context: context,
  26. save: function(service, selection, name, overwrite) {
  27. dialog.hide();
  28. var data = {
  29. 'type': 'agentDefinitionView',
  30. 'defaultName': name,
  31. 'executionPrompt': false,
  32. 'base': [{
  33. 'searchPath': 'storeID("' + cmProperties.id + '")',
  34. 'type': 'baseClass'
  35. }]
  36. };
  37. var deferred = new Deferred();
  38. var url = selection.url + '?updateAction=' + (overwrite ? 'replace' : 'fail');
  39. service.context.glassContext.services.ajax.post(url, {
  40. contentType: 'application/json',
  41. processData: false,
  42. dataType: 'text',
  43. data: JSON.stringify(data)
  44. })
  45. .fail(function(d, request) {
  46. var response = request.responseJSON;
  47. if (!response && request.responseText){
  48. response = JSON.parse(request.responseText);
  49. }
  50. if (response.errorCode === 'cmDuplicateName') {
  51. deferred.reject({ isDuplicate: true });
  52. } else {
  53. deferred.reject();
  54. saveDeferred.reject();
  55. }
  56. })
  57. .done(function(v_oResult) {
  58. service.context.glassContext.services.ajax.ajax({
  59. 'url': 'v1/objects/' + cmProperties.id + '/items',
  60. 'dataType': 'json',
  61. 'type': 'GET'
  62. }).then(function(reportObj) {
  63. // Find current report in AgentDefinition (only one report obj can be there)
  64. var reportId = reportObj.data.filter(function(item){
  65. return item.type === 'report';
  66. })[0].id;
  67. service.context.glassContext.services.ajax.ajax({
  68. 'url': selection.url,
  69. 'dataType': 'json',
  70. 'type': 'GET'
  71. }).then(function(objs){
  72. var agentViewId = objs.data.filter(function(d) {
  73. return d.defaultName === name;
  74. })[0].id;
  75. //create reportView with agentViewId and reportId that stored in agentdefinition
  76. var rvdata = {
  77. 'type': 'reportView',
  78. 'defaultName': new Date(),
  79. 'executionPrompt': false,
  80. 'searchPath': 'storeID("' + agentViewId + '")',
  81. 'base': [{
  82. 'searchPath': 'storeID("' + reportId + '")',
  83. 'type': 'baseClass'
  84. }]
  85. };
  86. service.context.glassContext.services.ajax.post(url, {
  87. contentType: 'application/json',
  88. processData: false,
  89. dataType: 'text',
  90. data: JSON.stringify(rvdata)
  91. }).done(function(){
  92. context.target.plugin.activeObject.oListControl.contentView.refresh();
  93. });
  94. });
  95. });
  96. deferred.resolve(v_oResult);
  97. saveDeferred.resolve(v_oResult);
  98. });
  99. return deferred.promise;
  100. }
  101. }
  102. });
  103. dialog.open();
  104. });
  105. return saveDeferred.promise;
  106. }
  107. };
  108. });