123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- /**
- * Licensed Materials - Property of IBM
- * IBM Cognos Products: rs
- * (C) Copyright IBM Corp. 2020
- * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
- *
- */
- define([
- 'baglass/core-client/js/core-client/ui/core/Class',
- 'bi/authoring/utils/pat/rsLaunchParameters',
- 'q'
- ], function(Class, rsLaunchParameters, Q) {
- 'use strict';
- /**
- * Utility class that does the real work of creating a dataset
- */
- function C_DatasetCreator( glassContext )
- {
- this._glassContext = glassContext;
- this._deferred = Q.defer();
- };
- C_DatasetCreator._serializer = new XMLSerializer();
- /**
- * Perform XML parsing of a string
- */
- C_DatasetCreator.prototype.parseString = function( v_sXML )
- {
- var doc = null;
-
- try
- {
- doc = ( new DOMParser() ).parseFromString( v_sXML, "text/xml" );
- }
- catch ( e )
- {
- // IE throws an error
- return null;
- }
- if (doc.documentElement.querySelector("parsererror") || doc.documentElement.nodeName == "parsererror") {
- // Chrome injects parsererror in the document where the error occurred, others set the root as parsererror with a namespace
- return null;
- }
- return doc;
- };
-
- C_DatasetCreator.prototype.addDataItem = function( v_nSelection, v_oItem )
- {
- var v_nDataItem = v_nSelection.appendChild(this.v_nDoc.createElementNS(this.m_sNamespace, "dataItem"));
- v_nDataItem.setAttribute("name", v_oItem.name);
- if (v_oItem.aggregate) {
- v_nDataItem.setAttribute("aggregate", v_oItem.aggregate);
- }
- if (v_oItem.rollupAggregate) {
- v_nDataItem.setAttribute("rollupAggregate", v_oItem.rollupAggregate);
- }
- var expression = v_nDataItem.appendChild(this.v_nDoc.createElementNS(this.m_sNamespace, "expression"));
- expression.appendChild(this.v_nDoc.createTextNode(v_oItem.expression));
- };
- C_DatasetCreator.prototype.addStyle = function( v_nParent, v_sRefStyle )
- {
- var v_nStyle = v_nParent.appendChild(this.v_nDoc.createElementNS(this.m_sNamespace, "style"));
- var v_nDefaultStyles = v_nStyle.appendChild(this.v_nDoc.createElementNS(this.m_sNamespace, "defaultStyles"));
- var v_nDefaultStyle = v_nDefaultStyles.appendChild(this.v_nDoc.createElementNS(this.m_sNamespace, "defaultStyle"));
- v_nDefaultStyle.setAttribute("refStyle", v_sRefStyle);
- };
- C_DatasetCreator.prototype.addDataSource = function( v_nParent, v_sDataItemName, v_sName )
- {
- var v_nContents = v_nParent.appendChild(this.v_nDoc.createElementNS(this.m_sNamespace, "contents"));
- var v_nTextItem = v_nContents.appendChild(this.v_nDoc.createElementNS(this.m_sNamespace, "textItem"));
- var v_nDataSource = v_nTextItem.appendChild(this.v_nDoc.createElementNS(this.m_sNamespace, "dataSource"));
- var v_nDataItem = v_nDataSource.appendChild(this.v_nDoc.createElementNS(this.m_sNamespace, v_sDataItemName));
- v_nDataItem.setAttribute("refDataItem", v_sName);
- };
- C_DatasetCreator.prototype.addTitle = function( v_nListColumn, v_sName )
- {
- var v_nListColumnTitle = v_nListColumn.appendChild( this.v_nDoc.createElementNS(this.m_sNamespace, "listColumnTitle"));
- this.addStyle(v_nListColumnTitle, "lt");
- this.addDataSource(v_nListColumnTitle, "dataItemLabel", v_sName);
- };
- C_DatasetCreator.prototype.addBody = function( v_nListColumn, v_oItem )
- {
- var v_nListColumnBody = v_nListColumn.appendChild( this.v_nDoc.createElementNS(this.m_sNamespace, "listColumnBody"));
- this.addStyle(v_nListColumnBody, v_oItem.measure ? "lm" : "lc");
- this.addDataSource(v_nListColumnBody, "dataItemValue", v_oItem.name);
- };
- C_DatasetCreator.prototype.addColumn = function( v_nListColumns, v_oItem )
- {
- var v_nListColumn = v_nListColumns.appendChild( this.v_nDoc.createElementNS(this.m_sNamespace, "listColumn"));
- this.addTitle(v_nListColumn, v_oItem.name);
- this.addBody(v_nListColumn, v_oItem);
- };
- C_DatasetCreator.prototype.getSpecVersion = function()
- {
- return this._glassContext.services.ajax.ajax({
- type : 'GET',
- url : '../schemas/rspec/version.json'
- }).then(function(v_oResponse){
- return v_oResponse.version;
- });
- };
- C_DatasetCreator.prototype.getCMInfo = function( v_sID )
- {
- return this._glassContext.services.ajax.ajax({
- type : 'GET',
- url : "v1/objects/" + v_sID + "?fields=searchPath,type"
- }).then( function(v_oResponse) {
- return v_oResponse.data[0];
- });
- };
-
- C_DatasetCreator.prototype.raiseError = function( v_sMsg )
- {
- console.log("rsDatasetService.createDataset: " + v_sMsg);
- this._deferred.reject( new Error(v_sMsg) );
- };
-
- C_DatasetCreator.prototype.setUpdateAction = function( v_sUpdateAction )
- {
- switch (v_sUpdateAction) {
- case 'fail':
- case 'replace':
- case 'update':
- case null:
- case undefined:
- this.m_sUpdateAction = v_sUpdateAction;
- break;
- default:
- delete this.m_sUpdateAction;
- }
- };
- C_DatasetCreator.prototype.setNames = function( v_sName, v_sQueryName )
- {
- this.m_sName = v_sName;
- this.m_sQueryName = v_sQueryName || v_sName || 'Query1';
- };
-
- C_DatasetCreator.prototype.loadItems = function( v_aItems )
- {
- // Create a map of existing names
- var v_oSpecifiedNames = {};
- v_aItems.forEach( function(v_oItem) {
- if (v_oItem.name) {
- if (v_oSpecifiedNames[v_oItem.name]) {
- // Duplicate name
- this.raiseError( "Duplicate item name " + v_oItem.name);
- return this._deferred.promise;
- }
- v_oSpecifiedNames[v_oItem.name] = true;
- }
- }.bind(this));
- // Assign unique name if none specified
- var i = 0;
- v_aItems.forEach( function(v_oItem) {
- if (!v_oItem.name) {
- var v_sName = 'Data Item' + (++i);
- while (v_oSpecifiedNames[v_sName]) {
- v_sName = 'Data Item' + (++i);
- }
- v_oItem.name = v_sName;
- }
- });
- this.m_aItems = v_aItems;
- return null;
- };
- C_DatasetCreator.prototype.generateSpecification = function(v_oMetadata, v_bExclude, v_bSummarize)
- {
- // Remove top level comments (template copyright)
- for (var v_nNode = this.v_nDoc.firstChild; v_nNode != null; ) {
- var v_nNext = v_nNode.nextSibling;
- if (v_nNode.nodeType == Node.COMMENT_NODE) {
- this.v_nDoc.removeChild(v_nNode);
- }
- v_nNode = v_nNext;
- }
- var v_nReport = this.v_nDoc.documentElement;
- if (this.m_sName) {
- var v_nReportName = v_nReport.appendChild(this.v_nDoc.createElementNS(this.m_sNamespace, "reportName"));
- v_nReportName.appendChild(this.v_nDoc.createTextNode(this.m_sName));
- }
-
- // set refQuery in list
- var v_nList = v_nReport.querySelector("list");
- v_nList.setAttribute("refQuery", this.m_sQueryName);
- v_nList.setAttribute("name", "List1");
-
- // rename query
- var v_nQuery = v_nReport.querySelector("query");
- v_nQuery.setAttribute("name", this.m_sQueryName);
-
- // set modelPath
- var v_nModelPath = v_nReport.querySelector("modelPath");
- var v_sSearchPath = v_oMetadata.searchPath;
- switch (v_oMetadata.type) {
- case "module":
- v_nModelPath.setAttribute("type", "module");
- break;
- case "package":
- v_sSearchPath += "/model[last()]";
- break;
- case "model":
- break;
- default:
- this.raiseError( 'Metadata ID references invalid object of type ' + v_oMetadata.type );
- return;
- }
- v_nModelPath.appendChild(this.v_nDoc.createTextNode(v_oMetadata.searchPath));
- // set autoSummary=false on query/selection
- var v_nSelection = v_nQuery.querySelector("selection");
- if (!v_bSummarize) {
- v_nSelection.setAttribute("autoSummary", "false");
- }
- // iterate over items
- var v_nListColumns = v_nList.appendChild( this.v_nDoc.createElementNS(this.m_sNamespace, "listColumns"));
- this.m_aItems.forEach( function(v_oItem) {
- this.addColumn(v_nListColumns, v_oItem);
- this.addDataItem(v_nSelection, v_oItem);
- }.bind(this));
- // set listSupress
- if (v_bExclude) {
- v_nList.appendChild( this.v_nDoc.createElementNS(this.m_sNamespace, "listSuppress"));
- }
- };
- C_DatasetCreator.prototype.create = function( v_sParentId, v_sMetadataId, v_bExclude, v_bSummarize, v_aParameters )
- {
- this.getSpecVersion().then(function(v_sVersion){
- this.m_sNamespace = 'http://developer.cognos.com/schemas/report/' + v_sVersion + '/';
- this.getCMInfo(v_sMetadataId).then( function(v_oMetadata) {
- // Retrieve dataset list template and parse
- var v_sTemplate = rsLaunchParameters.GetTemplate( 'DatasetList' );
- // Inject namespace attribute into template
- v_sTemplate = v_sTemplate.replace( /<report /, '<report xmlns="' + this.m_sNamespace + '" ');
- this.v_nDoc = this.parseString( v_sTemplate );
- if (!this.v_nDoc) {
- this.raiseError( "Failed to parse template" );
- return;
- }
- this.generateSpecification(v_oMetadata, v_bExclude, v_bSummarize);
-
- var v_sSpecification = C_DatasetCreator._serializer.serializeToString( this.v_nDoc );
-
- // POST to report service to create the dataset object
- var v_oDataset = {
- type: 'dataSet2',
- defaultName: this.m_sName || undefined,
- specification: v_sSpecification,
- parameters: v_aParameters || undefined,
- runInAdvancedViewer: true
- };
- var v_aUrl = ['v1/reports/', v_sParentId];
- if (this.m_sUpdateAction) {
- v_aUrl.push('?updateAction=');
- v_aUrl.push(this.m_sUpdateAction);
- }
- var v_oRequest = {
- type : 'POST',
- url : v_aUrl.join(''),
- contentType: "application/json",
- data : JSON.stringify(v_oDataset)
- };
- this._glassContext.services.ajax.ajax(v_oRequest).then(function(v_oResponse){
- this._deferred.resolve(v_oResponse);
- }.bind(this))
- .fail(function(x, http, y){
- this.raiseError( 'Server request to create dataset failed. ' + http.status + " " + http.statusText );
- }.bind(this));
- }.bind(this))
- .fail(function(v_oErr) {
- this.raiseError( "Invalid metadata ID" );
- }.bind(this));
- }.bind(this));
- return this._deferred.promise;
- };
- var rsDatasetService = Class.extend({
- init: function(attributes) {
- this._glassContext = attributes.glassContext;
- rsDatasetService.inherited('init', this, arguments);
- },
- /**
- * Create a dataset object.
- * @param v_sName (optional) The name of the dataset. If unspecified, a unique name will be generated by the system.
- * @param v_sQueryName (optional) The name of the query in the dataset. If unspecified, use v_sName or "Query1".
- * @param v_sParentId The ID of the object under which the dataset will be created
- * @param v_sMetadataId The ID of the metadata source (module/package/model)
- * @param v_bExclude Whether or not to exclude
- * @param v_bSummarize Whether or not auto summarization should be used
- * @param v_aParameters The parameter values to be stored with the dataset
- * @param v_aItems Array of data item names and corresponding expressions as in
- * [
- * {
- * name: "ProductLine", - (optional) a unique name of the form "Data Item#" will be generated if none provided
- * expression: "[Inventory (query)].[Products].[Product line]" - a V5 query expression
- * aggregate: (optional) aggregation mode from V5 spec
- * rollupAggregate: (optional) aggregation mode from V5 spec
- * measure: (optional) true/false- used to determine styling of list
- * }
- * ...
- * ]
- * @param v_sUpdateAction How object name collisions are resolved. Values other than the following are ignored: fail, replace, update
- * The default is fail.
- *
- * @return A promise that will resolve with the new datatastore object e.g.
- * {
- * searchPath: "/content/folder[@name='Data Sets']/dataSet2[@name='myDataset']"
- * _meta: {links: {…}}
- * id: "i96D7B925A62746988FD5ACD4814B266C"
- * type: "dataSet2"
- * defaultName: "myDataset"
- * }
- *
- * The aggregation values from the V5 specification are currently:
- * none, automatic, summarize, total, minimum, maximum, average, count, calculated,
- * countDistinct, standardDeviation, variance, median, notApplicable
- *
- * Parameter values are specified as an array of parameter objects using the JSON representation of CA parameter values
- * as per the soap-bridge or content-service.
- * [
- * {
- * name: "parameterName",
- * value: [...]
- * }
- * ...
- * ]
- * where the value objects for simple values are
- * {
- * type: "simpleParmValueItem",
- * use: "use value",
- * display: "display value" (optional)
- * inclusive: true
- * }
- */
- createDataset: function( v_sName, v_sQueryName, v_sParentId, v_sMetadataId, v_bExclude, v_bSummarize, v_aParameters, v_aItems, v_sUpdateAction ) {
- var creator = new C_DatasetCreator( this._glassContext );
- creator.setNames( v_sName, v_sQueryName );
- creator.setUpdateAction( v_sUpdateAction );
- var v_oError = creator.loadItems(v_aItems);
- return v_oError || creator.create( v_sParentId, v_sMetadataId, v_bExclude, v_bSummarize, v_aParameters );
- }
- });
- return rsDatasetService;
- });
|