CCognosViewerSaveReport.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| IBM Cognos Products: Viewer
  5. *| (C) Copyright IBM Corp. 2001, 2011
  6. *|
  7. *| US Government Users Restricted Rights - Use, duplication or
  8. *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. *|
  10. *+------------------------------------------------------------------------+
  11. */
  12. function CCognosViewerSaveReport( cognosViewer, payload )
  13. {
  14. this.m_cognosViewer = cognosViewer;
  15. this.m_params = null;
  16. //chrome always send use the storeid of the dashboard to save report in so we know if it is save-as or save operation
  17. this.dashboardToSaveIn = payload.cm$storeID;
  18. this.m_doSaveAsOnFault = false;
  19. }
  20. /**
  21. * Can save if the user has write permission, creating a new dashboard or we're doing a saveAs (also creating a 'new' dashboard)
  22. * @param {Object} permission
  23. */
  24. CCognosViewerSaveReport.prototype.canSave = function( permission )
  25. {
  26. return ( this.doSaveAs() || permission && permission.indexOf( "write" ) !== -1 ) ;
  27. };
  28. CCognosViewerSaveReport.prototype.isSavedOutput = function()
  29. {
  30. //do not save if report is a saved output
  31. var sAction = this.m_cognosViewer.envParams["ui.action"];
  32. return ( typeof sAction !== "undefined" && sAction === "view");
  33. };
  34. /**
  35. *
  36. */
  37. CCognosViewerSaveReport.prototype.doSaveAs = function()
  38. {
  39. //savedReportName is only set when report had been saved in the dashboard
  40. var result = ( this.m_doSaveAsOnFault || !this.m_cognosViewer.envParams["savedReportName"] || !this.isSameDashboard() );
  41. return result;
  42. };
  43. CCognosViewerSaveReport.prototype.isSameDashboard = function()
  44. {
  45. var result = ( this.m_cognosViewer.envParams["ui.object"].indexOf( this.dashboardToSaveIn ) !== -1 );
  46. return result;
  47. };
  48. CCognosViewerSaveReport.prototype.getUIAction = function()
  49. {
  50. return ( this.doSaveAs() ? "saveInDashboard" : "updateSavedReport");
  51. };
  52. CCognosViewerSaveReport.prototype.populateRequestParams = function(asynchRequest) {
  53. asynchRequest.addFormField('ui.action', this.getUIAction());
  54. asynchRequest.addFormField('cv.ignoreState', 'true');
  55. asynchRequest.addFormField("dashboard-id", this.dashboardToSaveIn);
  56. asynchRequest.addNonEmptyStringFormField("executionParameters", this.m_cognosViewer.m_sParameters);
  57. for(var param in this.m_cognosViewer.envParams)
  58. {
  59. if( param.indexOf("frag-") == 0 || param == "cv.actionState" ||
  60. param == "ui.primaryAction" || param == "dashboard" ||
  61. param == "ui.action" || param == "cv.responseFormat" ||
  62. param == "b_action") {
  63. continue;
  64. }
  65. asynchRequest.addFormField(param, this.m_cognosViewer.envParams[param]);
  66. }
  67. };
  68. CCognosViewerSaveReport.prototype.getCognosViewer = function()
  69. {
  70. return this.m_cognosViewer;
  71. };
  72. CCognosViewerSaveReport.prototype.getViewerWidget = function()
  73. {
  74. return this.getCognosViewer().getViewerWidget();
  75. };
  76. CCognosViewerSaveReport.prototype.dispatchRequest = function()
  77. {
  78. var cognosViewer = this.m_cognosViewer;
  79. var viewerWidget = this.getViewerWidget();
  80. var callbacks = {
  81. "complete":{"object":viewerWidget,"method":viewerWidget.handleWidgetSaveDone},
  82. "fault":{"object":this,"method":this.onFault}
  83. };
  84. var asynchRequest = new AsynchJSONDispatcherEntry(cognosViewer);
  85. asynchRequest.setCallbacks(callbacks);
  86. this.populateRequestParams(asynchRequest);
  87. cognosViewer.dispatchRequest(asynchRequest);
  88. };
  89. CCognosViewerSaveReport.prototype.onFault = function(asynchJSONResponse, arg1){
  90. var cognosViewer = this.m_cognosViewer;
  91. var viewerWidget = this.getViewerWidget();
  92. var soapFaultEnvelope = asynchJSONResponse.getSoapFault();
  93. var soapFaultNode = XMLHelper_FindChildByTagName(soapFaultEnvelope, "Fault", true);
  94. if( this.ifIsEmptySelectionFault( soapFaultNode ) )
  95. {
  96. this.handleEmptySelectionFault();
  97. return;
  98. }
  99. // set retry to False - can't retry a save
  100. var retryNode = soapFaultEnvelope.createElement("allowRetry");
  101. retryNode.appendChild(soapFaultEnvelope.createTextNode("false"));
  102. soapFaultNode.appendChild(retryNode);
  103. var sSoapFault = XMLBuilderSerializeNode(soapFaultNode);
  104. cognosViewer.setSoapFault(sSoapFault);
  105. viewerWidget.handleFault();
  106. var saveDonePayload = {'status':false};
  107. viewerWidget.iContext.iEvents.fireEvent( "com.ibm.bux.widget.save.done", null, saveDonePayload );
  108. };
  109. /**
  110. * Returns true if the fault is caused by an attempt to update a non-existing report
  111. */
  112. CCognosViewerSaveReport.prototype.ifIsEmptySelectionFault = function( soapFault ){
  113. if(soapFault) {
  114. var errorCodeElement = XMLHelper_FindChildByTagName(soapFault, 'errorCode', true);
  115. if( errorCodeElement )
  116. {
  117. var errorCode = XMLHelper_GetText(errorCodeElement, false);
  118. return ( errorCode === 'cmEmptySelection' );
  119. }
  120. }
  121. return false;
  122. };
  123. /**
  124. * Sends a save-as request
  125. */
  126. CCognosViewerSaveReport.prototype.handleEmptySelectionFault = function(){
  127. delete (this.m_cognosViewer.envParams["savedReportName"]);
  128. this.m_doSaveAsOnFault = true;
  129. this.dispatchRequest();
  130. };