ViewerIWidgetSave.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| IBM Cognos Products: Viewer
  5. *| (C) Copyright IBM Corp. 2001, 2013
  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. SAVE_REPORT_TYPE = {
  13. reportView : 'application/x-ibmcognos_v5reportview+xml',
  14. report : 'application/x-ibmcognos_v5report+xml'
  15. };
  16. function ViewerIWidgetSave( viewerWidget, payload )
  17. {
  18. this.m_ViewerWidget = viewerWidget;
  19. this.m_payload = payload;
  20. this._setIsSavedDashboard();
  21. }
  22. ViewerIWidgetSave.prototype.setDoCWCopy = function(value) {
  23. this._doCWCopy = value;
  24. };
  25. ViewerIWidgetSave.prototype._getSavedReport = function(){
  26. var sSavedReport = this._getWidgetAttributeValue('savedReportPath') ;
  27. if( !sSavedReport )
  28. {
  29. //for backwards compatibility
  30. sSavedReport = this._getWidgetAttributeValue( 'savedReportName');
  31. }
  32. return sSavedReport;
  33. };
  34. /**
  35. * Check if current dashboard is already saved
  36. */
  37. ViewerIWidgetSave.prototype._setIsSavedDashboard = function()
  38. {
  39. var sSavedReport = this._getSavedReport();
  40. this._bIsSavedDashboard = ( sSavedReport !== null && sSavedReport !== undefined && sSavedReport.length !== 0) ;
  41. };
  42. ViewerIWidgetSave.prototype._isSavedDashboard = function(){
  43. return this._bIsSavedDashboard;
  44. };
  45. /**
  46. * Can save if the user has write permission, creating a new dashboard or we're doing a saveAs (also creating a 'new' dashboard)
  47. * @param {Object} permission
  48. */
  49. ViewerIWidgetSave.prototype.canSave = function( permission )
  50. {
  51. //user is allowed to save in dashboard if they are doing a save new or save-as without write permission
  52. return ( this._doSaveNewOrSaveAs() || permission && permission.indexOf( "write" ) !== -1 || this.m_ViewerWidget.isDropped() ) ;
  53. };
  54. ViewerIWidgetSave.prototype.isSavedOutput = function()
  55. {
  56. //do not save if report is a saved output
  57. var sAction = this.m_cognosViewer.envParams["ui.action"];
  58. return ( typeof sAction !== "undefined" && sAction === "view");
  59. };
  60. ViewerIWidgetSave.prototype._doSaveNewOrSaveAs = function(){
  61. var result = ( this.m_payload.operation === 'save' && !this._isSavedDashboard() ) || ( this.m_payload.operation === 'saveAs');
  62. return result;
  63. };
  64. ViewerIWidgetSave.prototype._getWidgetAttributeValue = function( attribName ){
  65. return this._getViewerWidget().getAttributeValue( attribName );
  66. };
  67. ViewerIWidgetSave.prototype._getEnvParam = function( paramName ){
  68. return this._getViewerWidget().getEnvParam( paramName );
  69. };
  70. ViewerIWidgetSave.prototype._getViewerWidget = function(){
  71. return this.m_ViewerWidget;
  72. };
  73. ViewerIWidgetSave.prototype._isLimitedInteractiveMode = function(){
  74. return this._getViewerWidget().isLimitedInteractiveMode();
  75. };
  76. ViewerIWidgetSave.prototype._getDefaultReportName = function(){
  77. return this._getEnvParam( 'ui.name');
  78. };
  79. ViewerIWidgetSave.prototype._getReportSpec = function(){
  80. return this._getEnvParam( 'ui.spec');
  81. };
  82. ViewerIWidgetSave.prototype._getCurrentReportIsReportView = function(){
  83. return ( this._getEnvParam( 'ui.objectClass') === 'reportView' );
  84. };
  85. ViewerIWidgetSave.prototype.doGetSavePropertiesFromServer = function(){
  86. this.delayedLoadingContext = this._getViewerWidget().getLoadManager().getDelayedLoadingContext();
  87. if (this._getEnvParam( 'delayedLoadingExecutionParams')) {
  88. return true;
  89. }
  90. return ( this.delayedLoadingContext && this.delayedLoadingContext.getPromptValues() !== null );
  91. };
  92. /**
  93. * This function gets the information that is needed from the server in order to do the save.
  94. * Currently, it sends the execution parameters to the server to get it in the proper syntax
  95. * to be saved in CM.
  96. */
  97. ViewerIWidgetSave.prototype.getSavePropertiesFromServer = function(){
  98. var oCV = this._getViewerWidget().getViewerObject();
  99. var serverRequest = new JSONDispatcherEntry( oCV );
  100. var widget = this._getViewerWidget();
  101. //set the callback function
  102. serverRequest.setCallbacks({
  103. customArguments: [ this.m_payload ],
  104. complete: {"object": widget, "method": widget.handleGetSavePropertiesFromServerResponse }
  105. });
  106. this._addRequestOptions(serverRequest);
  107. serverRequest.sendRequest();
  108. };
  109. ViewerIWidgetSave.prototype._addRequestOptions = function(serverRequest)
  110. {
  111. serverRequest.addFormField("ui.action", "noOp" );
  112. serverRequest.addFormField("bux", "true");
  113. serverRequest.addFormField("cv.responseFormat", "IWidgetSavePropertiesJSON");
  114. // delayedLoadingExecutionParams contains the saved prompt values that was obtained on the initial load of viewer widget but
  115. // before the report is ran (which means no ui.conversation). This needs to be sent to the server so the saved prompt values can be merged with
  116. // any new prompt values that is stored in the delayedLoadingContext before being saved. Otherwise, only global prompt values are save and
  117. // caused the report to prompt next time, the tab is made visible.
  118. if( this._getEnvParam( 'delayedLoadingExecutionParams') ){
  119. serverRequest.addFormField("delayedLoadingExecutionParams", this._getEnvParam('delayedLoadingExecutionParams') );
  120. }
  121. else
  122. {
  123. // if there is no delayedLoadingExecutionParams, it means that the report had ran and there is conversation and it
  124. // should be sent to the server so that the prompt values contained in it will be merged with the ones in delayedLoadingContext.
  125. serverRequest.addFormField("ui.conversation", this._getViewerWidget().getViewerObject().getConversation() );
  126. }
  127. var promptValues = this.delayedLoadingContext.getPromptValues();
  128. for( var promptValue in promptValues ){
  129. serverRequest.addFormField( promptValue, promptValues[promptValue] )
  130. }
  131. };
  132. ViewerIWidgetSave.prototype._getExecutionParameters = function(){
  133. return this._getViewerWidget().getViewerObject().getExecutionParameters();
  134. };
  135. ViewerIWidgetSave.prototype._setExecutionParameters = function( body )
  136. {
  137. var sParameters = this._getExecutionParameters();
  138. var doc = XMLBuilderLoadXMLFromString( sParameters );
  139. if( !doc.documentElement )
  140. {
  141. return;
  142. }
  143. var root = XMLBuilderCreateXMLDocument ( 'root');
  144. var eParameters = root.createElement( 'parameters' );
  145. XMLBuilderSetAttributeNodeNS(eParameters, "xmlns:SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/");
  146. XMLBuilderSetAttributeNodeNS(eParameters, "xsi:type", "bus:parameterValueArrayProp", "http://www.w3.org/2001/XMLSchema-instance" );
  147. XMLBuilderSetAttributeNodeNS(eParameters, "xmlns:bus", "http://developer.cognos.com/schemas/bibus/3/" );
  148. XMLBuilderSetAttributeNodeNS(eParameters, "xmlns:xs", "http://www.w3.org/2001/XMLSchema" );
  149. root.documentElement.appendChild(eParameters);
  150. var allItems = XMLHelper_FindChildrenByTagName(doc.documentElement, 'item', false);
  151. var eValue = root.createElement("value");
  152. XMLBuilderSetAttributeNodeNS(eValue, "xsi:type", "SOAP-ENC:Array", "http://www.w3.org/2001/XMLSchema-instance");
  153. eParameters.appendChild( eValue );
  154. var iNumberOfParms = allItems.length;
  155. for (var i = 0; i < allItems.length; i++) {
  156. var eName = XMLHelper_FindChildByTagName(allItems[i], 'name', false);
  157. if( eName && eName.childNodes[0].nodeValue.indexOf( 'credential:') !== -1 ){
  158. iNumberOfParms--;
  159. continue;
  160. }
  161. eValue.appendChild( allItems[i] );
  162. }
  163. XMLBuilderSetAttributeNodeNS(eValue, "SOAP-ENC:arrayType", "bus:parameterValue[" + iNumberOfParms + "]", "http://schemas.xmlsoap.org/soap/encoding/");
  164. body.parameters = XMLBuilderSerializeNode( eParameters );
  165. };
  166. /**
  167. * Sets the source report to be copied or base report for the report view
  168. */
  169. ViewerIWidgetSave.prototype._setSourceObject = function(resource, bUseCurrentReport) {
  170. var sOriginalReportValue = (bUseCurrentReport === true)
  171. ? this._getEnvParam('ui.object')
  172. : this._getEnvParam('originalReport');
  173. if (sOriginalReportValue) {
  174. resource.sourceObject = sOriginalReportValue;
  175. }
  176. };
  177. ViewerIWidgetSave.prototype._setReportTypeToReportView = function(resource) {
  178. resource.type = SAVE_REPORT_TYPE.reportView;
  179. };
  180. ViewerIWidgetSave.prototype._setReportTypeToReport = function(resource) {
  181. resource.type = SAVE_REPORT_TYPE.report;
  182. };
  183. ViewerIWidgetSave.prototype._setReportSpec = function( body ) {
  184. body.specification = this._getReportSpec();
  185. };
  186. ViewerIWidgetSave.prototype._setResourceForSave = function(resource){
  187. if( !this._getCurrentReportIsReportView() && !this._isLimitedInteractiveMode() ){
  188. this._setReportSpec( resource.body );
  189. this._setReportTypeToReport(resource);
  190. }
  191. return resource;
  192. };
  193. ViewerIWidgetSave.prototype._setResourceForCopy = function(resource){
  194. this._setReportSpec( resource.body );
  195. this._setReportTypeToReport(resource);
  196. return resource;
  197. };
  198. ViewerIWidgetSave.prototype._setResourceForSaveNew = function(resource){
  199. var bUseCurrentReport = false;
  200. if (this._getEnvParam('originalReport') == null) {
  201. //this is a special case for copy/pasted ci published widget
  202. // when originalReport is null we use ui.object env. variable to set SourceObject
  203. bUseCurrentReport = true;
  204. }
  205. this._setSourceObject( resource, bUseCurrentReport );
  206. if( this._isLimitedInteractiveMode() )
  207. {
  208. this._setReportTypeToReportView( resource );
  209. }else
  210. {
  211. this._setReportTypeToReport( resource );
  212. this._setReportSpec(resource.body );
  213. }
  214. return resource;
  215. };
  216. ViewerIWidgetSave.prototype._setResourceForSaveAs = function( resource ){
  217. if( this._getCurrentReportIsReportView() )
  218. {
  219. this._setReportTypeToReportView( resource );
  220. this._setSourceObject( resource );
  221. }
  222. else if( this._isLimitedInteractiveMode() )
  223. {
  224. this._setReportTypeToReportView( resource );
  225. this._setSourceObject( resource, true /*bUseCurrentReport*/ );
  226. }
  227. else
  228. {
  229. this._setReportTypeToReport( resource );
  230. this._setSourceObject( resource, true /*bUseCurrentReport*/ );
  231. this._setReportSpec( resource.body );
  232. }
  233. return resource;
  234. };
  235. ViewerIWidgetSave.prototype._getResource = function(){
  236. var resource = {};
  237. if (this._doCWCopy === true) {
  238. resource.copyOnCreate = true;
  239. }
  240. resource.body = {};
  241. var bDoSave = (this.m_payload.operation === 'save');
  242. var bIsCopy = (this.m_payload.operation === 'copy');
  243. if ( bIsCopy )
  244. {
  245. this._setResourceForCopy( resource );
  246. }
  247. else if( bDoSave)
  248. {
  249. this._setResourceForSave( resource );
  250. }
  251. else
  252. {
  253. if( this._isSavedDashboard() )
  254. {
  255. this._setResourceForSaveAs(resource);
  256. }
  257. else
  258. {
  259. this._setResourceForSaveNew(resource);
  260. }
  261. }
  262. this._setExecutionParameters( resource.body );
  263. if (!bIsCopy){
  264. // list of itemSets for chrome to update after a save
  265. resource.itemSetUpdate = { name: 'savedReportPath',
  266. type: 'searchPath' };
  267. }
  268. return resource;
  269. };
  270. ViewerIWidgetSave.prototype._getWidgetId = function(){
  271. return this._getViewerWidget().getWidgetId();
  272. };
  273. /**
  274. * payload = {
  275. * widgetId : <widget id>,
  276. * resource : [
  277. * {
  278. * type : <report or report view>
  279. * sourceObject : <source report to be cloned or saved as report view>
  280. * body : { <list of cm properties to be updated> }
  281. * itemSetUpdate : { <list of itemsets to be updated by BUX after a successful save }
  282. * }
  283. * ]
  284. * }
  285. *
  286. * body = {
  287. * specification : <optional - report spec if saving report>,
  288. * parameters : <execution parameters>
  289. * }
  290. *
  291. * itemSetUpdate = {
  292. * name : 'savedReportPath',
  293. * type : 'searchPath'
  294. * }
  295. */
  296. ViewerIWidgetSave.prototype.getPayload = function(){
  297. var payload = {};
  298. payload.resource = new Array();
  299. payload.widgetId = this._getWidgetId();
  300. payload.resource.push( this._getResource() );
  301. return payload;
  302. };