/* *+------------------------------------------------------------------------+ *| Licensed Materials - Property of IBM *| IBM Cognos Products: Viewer *| (C) Copyright IBM Corp. 2001, 2013 *| *| US Government Users Restricted Rights - Use, duplication or *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp. *| *+------------------------------------------------------------------------+ */ dojo.provide("viewer.AnnotationHelper"); dojo.declare("viewer.AnnotationHelper", null, { m_loadAnnotationsDfd : null, //This records if any annotation is returned on the first getAnnotation call. if none is returned, we want to skip getAnnotation call. //default value is true. responseHandler of getAnnotations update this flag accordingly. //If a user select 'reset' action, this flag reset to 'true' so that we issue getAnnotation call. m_bCallGetAnnotations : true, m_bAnnotationsEnabled : null, constructor : function(iWidget) { this.m_viewerIWidget = iWidget; this.m_bCallGetAnnotations = true; this.m_bAnnotationsEnabled = null; }, getAnnotationStore : function() { return this.m_viewerIWidget.getAnnotationStore(); }, setCallGetAnnotations : function(flag) { this.m_bCallGetAnnotations = flag; }, isEnabled : function() { if(this.m_bAnnotationsEnabled === null) { this.m_bAnnotationsEnabled = this.m_viewerIWidget.getViewerObject().bBuxAnnotationsAvailable; } return this.m_bAnnotationsEnabled; }, refreshAnnotationData : function() { if(this.m_loadAnnotationsDfd) { return null; } this.m_loadAnnotationsDfd = new dojo.Deferred(); this.fetchAnnotationData(); return this.m_loadAnnotationsDfd; }, /** * Sends a request to the server for the latest annotation data */ fetchAnnotationData : function() { var viewerIWidget = this.m_viewerIWidget; if (!this.m_bCallGetAnnotations || !this.isEnabled()) { //stop proceede if the flag is false return; } dojo.when(viewerIWidget.getWidgetStoreID(), dojo.hitch(this, function(widgetStoreID) { if(!widgetStoreID) { return; } this.m_viewerIWidget.getLoadManager().runWhenHasViewer( dojo.hitch(this, this._fetchAnnotationData, widgetStoreID) ); }) ); }, _fetchAnnotationData: function(widgetStoreID) { var cognosViewer = this.m_viewerIWidget.getViewerObject(); var selCon = cognosViewer.getSelectionController(); var metadata = selCon.getCCDManager().m_md; if(!metadata) { metadata = {}; } metadata = dojo.toJson(metadata); var contextData = selCon.getCCDManager().m_cd; if(!contextData) { contextData = {}; } contextData = dojo.toJson(contextData); var callbacks = {"complete":{}, "fault": {}}; callbacks.complete["object"] = this; callbacks.complete["method"] = this.fetchAnnotationDataResponse; callbacks.fault["object"] = this; callbacks.fault["method"] = this.fetchAnnotationDataResponseFault; var asynchRequest = new AsynchJSONDispatcherEntry(cognosViewer); asynchRequest.setCallbacks(callbacks); asynchRequest.addFormField("widgetStoreID", widgetStoreID); asynchRequest.addFormField("ui.action", "getAnnotations"); asynchRequest.addFormField("cv.metatdata", metadata); asynchRequest.addFormField("cv.context", contextData); // Need to bypass the queue for this request since it's a background request and // we don't want other requests (actions) to get dropped from the queue asynchRequest.sendRequest(); }, fetchAnnotationDataResponseFault: function(oAsynchResponse) { this.m_bCallGetAnnotations = false; var oFaultDialog = new FaultDialog(this.m_viewerIWidget.getViewerObject()); var oSoapFault = oAsynchResponse.getSoapFault(); var sErrorCode = null; var nException = XMLHelper_FindChildByTagName(oSoapFault, "exception", true); if(nException) { var nErrorCode = XMLHelper_FindChildByTagName(nException, "errorCode", false); if(nErrorCode) { sErrorCode = XMLHelper_GetText(nErrorCode); } } if(sErrorCode === "0") { var sMessage = null; var nMessage = XMLHelper_FindChildByTagName(nException, "message", false); if(nMessage) { var nMessageString = XMLHelper_FindChildByTagName(nMessage, "messageString", true); if(nMessageString) { sMessage = XMLHelper_GetText(nMessageString, false); } } if(sMessage) { var rErrorCode = /ANS-GEN-(\d{2,4})/; var aMatches = rErrorCode.exec(sMessage); if(aMatches && aMatches.length && aMatches.length > 1) { var sAnsErrorCode = aMatches[1]; if(sAnsErrorCode === "0075") { //Parent deleted - i.e. widget has been deleted. oFaultDialog.setErrorMessage(RV_RES.IDS_ANNOTATION_WIDGET_DELETED_ERROR); } } } } //Delete all annotations (they're no longer valid). this._cleanupPreviousAnnotations(); this.getAnnotationStore().clear(); oFaultDialog.show(oSoapFault); if(this.m_loadAnnotationsDfd) { this.m_loadAnnotationsDfd.errback(); this.m_loadAnnotationsDfd = null; } }, fetchAnnotationDataResponse : function(asynchJSONResponse) { var editContentAction = window["CVEditContentActionInstance"]; var jsonResult = asynchJSONResponse.getResult(); //Prevent subsequent queries for annotations only if the report //has no annotations at all (i.e. jsonResult is null, not empty). this.m_bCallGetAnnotations = this.m_bCallGetAnnotations && jsonResult !== null; if (jsonResult===null) { jsonResult = []; } var viewerObject = this.m_viewerIWidget.getViewerObject(); viewerObject.envParams["cv.annotationData"] = jsonResult; this._cleanupPreviousAnnotations(); if (jsonResult.length > 0) { if (this.getAnnotationStore()) { var annotationArray = this._generateMatchedAnnotationsFromViewerObject(); this.getAnnotationStore().load(annotationArray); this.displayCommentIndicators(); } } if(this.m_loadAnnotationsDfd) { this.m_loadAnnotationsDfd.callback(); this.m_loadAnnotationsDfd = null; } }, /* * destroy all commentViewer widget for previous annotations */ _cleanupPreviousAnnotations : function () { var store = this.getAnnotationStore(); if (store) { var annotations = store.getAll(); //clean up all annotations for (var contextId in annotations) { var commentId = this.m_viewerIWidget.getWidgetId() + "_" + contextId + "_comment"; var commentViewer = dijit.byId(commentId); if (typeof commentViewer != "undefined" && commentViewer != null) { commentViewer.destroy(); delete commentViewer; } store.clear(contextId); } } }, /* * Returns array of annotations. Ctx of an annotation is updated with the explicit ctx value of the report cell the annotation is on. * In order to update ctx value, it does the following. * * - remove any OR (|) operator in ctxValue * - find a matching report cell and take ctx value of it * */ _generateMatchedAnnotationsFromViewerObject : function () { var viewerObject = this.m_viewerIWidget.getViewerObject(); var annotationData = []; if (viewerObject && viewerObject.envParams && viewerObject.envParams["cv.annotationData"]) { annotationData = eval(viewerObject.envParams["cv.annotationData"]); } var annotationListWithUniqueCTX = this._generateAnnotationListWithoutORoperator(annotationData); return this._updateAnnotationCtxWithMatchedCellCtx(annotationListWithUniqueCTX, viewerObject); }, /* * ctx of an annotation may have 'OR' operator. * for example, 9:3|4:* * It means that annotated cell's ctx could be either 9:3:* or 9:4:* * * This function removes 'OR' operator and make two annotations with one of each possible ctx value. * */ _generateAnnotationListWithoutORoperator : function(annotationData) { var annotationListWithUniqueCTX = []; for (var i=0, len=annotationData.length; i