_base.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*******************************************************************************
  2. * IBM Confidential
  3. *
  4. * OCO Source Materials
  5. *
  6. * A and PM: PD
  7. *
  8. * (c) Copyright IBM Corp. 2014, 2015
  9. *
  10. * The source code for this program is not published or otherwise divested of
  11. * its trade secrets, irrespective of what has been deposited with the U.S.
  12. * Copyright Office.
  13. ******************************************************************************/
  14. define([
  15. "dojo/_base/declare",
  16. "dijit/_WidgetBase",
  17. "dijit/_TemplatedMixin",
  18. "dijit/_WidgetsInTemplateMixin",
  19. "dojo/text!pd/ui/templates/_base.html",
  20. "dojo/dom",
  21. "pd/widgets/ColumnGrid",
  22. "pd/widgets/PreviewGrid",
  23. "pd/data/PdSpecStore",
  24. "dojo/data/ObjectStore",
  25. "dojo/string",
  26. "pd/widgets/ComboBox",
  27. "pd/data/PdDataStore",
  28. "pd/widgets/SaveDialog",
  29. "pd/widgets/MessageDialog",
  30. "dojo/dom-construct",
  31. "bux/application/controllers/ForestModelsSubController",
  32. "bux/data/RestStore",
  33. "bux/data/AtomHelper",
  34. "pd/widgets/Uploader",
  35. "pd/widgets/ProgressBar",
  36. "dojo/domReady!"
  37. ], function(declare, _WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin,
  38. template, dom, ColumnGrid, PreviewGrid, PdSpecStore, ObjectStore,
  39. string, ComboBox, PdDataStore, SaveDialog, MessageDialog, domConstruct, ForestModelsSubController) {
  40. var self = this;
  41. new ForestModelsSubController();
  42. return declare("pd/ui/_base", [_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], {
  43. //default to "normal", can be "edit", "refresh"
  44. mode: pd.statics.MODE_NORMAL,
  45. //left side meta data grid
  46. columnGrid: null,
  47. //data preview grid
  48. previewGrid: null,
  49. //column name comboBox
  50. comboBox: null,
  51. //data set name
  52. dataSetName: "",
  53. //save as package name
  54. saveAsPackageName: "",
  55. templateString : template,
  56. getDataSetName: function() {
  57. //to be overridden
  58. },
  59. getDataSetStoreID: function() {
  60. //to be overridden
  61. },
  62. renderMainContent: function(pdSpecValue) {
  63. //to be overridden
  64. },
  65. _getFileFullName: function() {
  66. return pd_uploader.get("value")[0].name;
  67. },
  68. _getFileName: function() {
  69. var fileName = this._getFileFullName();
  70. var lastPeriod = fileName.lastIndexOf(".");
  71. if (lastPeriod > 0) {
  72. fileName = fileName.substring(0, lastPeriod);
  73. }
  74. return fileName;
  75. },
  76. _getFileType: function() {
  77. // Summary:
  78. // According to the current External Data implementation, the supported file types are CSV,
  79. // Excel2003 and XML, and the file type is determined by the file extension. For xls
  80. // and xlsx, it's always Excel2003
  81. var type = "";
  82. var extension = pd_uploader.getFileType(this._getFileFullName())
  83. switch (extension) {
  84. case "XLS":
  85. case "XLSX":
  86. type = "Excel2003";
  87. break;
  88. case "CSV":
  89. type = "CSV";
  90. break;
  91. default:
  92. // for now, rely on server side to throw an exception.
  93. }
  94. return type;
  95. },
  96. setStatus: function(status) {
  97. switch(status){
  98. case pd.statics.STATUS_INIT:
  99. self.isProcessing = false;
  100. pd_progressBar.init(pd_uploader.uploadType);
  101. pd_uploader.uploadUrl = this.getTargetUrl();
  102. break;
  103. case pd.statics.STATUS_UPLOAD:
  104. self.isProcessing = false;
  105. pd_progressBar.onStageZero();
  106. pd_uploader.set("disabled", true);
  107. break;
  108. case pd.statics.STATUS_PROCESS:
  109. self.isProcessing = true;
  110. pd_progressBar.onStageTwo(PDMSG.IPT.IDS_IPT_PROGRESS_STAGE_LOAD);
  111. pd_uploader.set("disabled", true);
  112. break;
  113. case pd.statics.STATUS_FINISH:
  114. self.isProcessing = false;
  115. pd_progressBar.onStageThree();
  116. pd_uploader.set("disabled", false);
  117. break;
  118. default:
  119. }
  120. },
  121. _uploaderOnchangeHandler: function() {
  122. this.dataSetName = this.getDataSetName();
  123. this.pd_uploaderForm.inSpec.value = '<input><dataSet type="'
  124. + this._getFileType()
  125. + '"><name>' + this._getFileName() + '</name>'
  126. + '<sourcePath>'
  127. + this._getFileFullName()
  128. + '</sourcePath>'
  129. + '</dataSet>'
  130. + '</input>';
  131. this.setStatus(pd.statics.STATUS_UPLOAD);
  132. },
  133. _uploaderOnerrorHandler: function(err) {
  134. pd_uploader.reset();
  135. this.setStatus(pd.statics.STATUS_FINISH);
  136. var pdError = {};
  137. if (typeof err == "string"){
  138. pdError.message = err;
  139. } else {
  140. pdError = err;
  141. }
  142. //try to analysis unexpected response and improve the error messages.
  143. var response = pdError.response;
  144. if (response) {
  145. if (dojo.isSafari) {
  146. //safari response is plain text.
  147. pdError.detail = response;
  148. } else if (dojo.isIE){
  149. var doc = response.documentElement;
  150. if (doc && doc.document) {
  151. if (!pdError.message){
  152. pdError.message = doc.document.title;
  153. }
  154. pdError.detail = doc.innerHTML;
  155. }
  156. } else {
  157. if (!pdError.message){
  158. pdError.message = response.title;
  159. }
  160. if (response.parseError){
  161. pdError.detail = null;
  162. } else {
  163. pdError.detail = response.body?response.body.innerHTML : response.xml;
  164. }
  165. }
  166. }
  167. if (pdError.name == pd.CAM_PASSPORT_ERROR){
  168. pd.showLogonDialog();
  169. return;
  170. } else {
  171. //RTC 12700: The error message displayed when the importing phase of large files times out could be improved
  172. if (pdError.name == "504" || pdError.message.indexOf("504") > -1) {
  173. pd.messageBox(pd.statics.MB_WARNING, pdError.message,
  174. PDMSG.ERR.IDS_ERR_BACKGROUND_RUNNING_MESSAGE, pdError.detail);
  175. return;
  176. }
  177. //RTC 15489, handle IE permission denied in case of IIS and PassThrough case.
  178. if (pdError.message.indexOf("Permission denied") > -1) {
  179. pd.messageBox(pd.statics.MB_ERROR, pdError.message,
  180. PDMSG.ERR.IDS_ERR_EXCEED_REQUEST_CONTENT_LENGTH, null);
  181. return;
  182. }
  183. //in IIS case
  184. if (pdError.name == "404" || pdError.message.indexOf("404") > -1) {
  185. pd.messageBox(pd.statics.MB_ERROR, pdError.message,
  186. PDMSG.ERR.IDS_ERR_EXCEED_REQUEST_CONTENT_LENGTH, pdError.detail);
  187. return;
  188. }
  189. //in IHS, Apache cases
  190. if (pdError.name == "413" || pdError.message.indexOf("413") > -1) {
  191. pd.messageBox(pd.statics.MB_ERROR, pdError.message,
  192. PDMSG.ERR.IDS_ERR_EXCEED_REQUEST_CONTENT_LENGTH, null);
  193. return;
  194. }
  195. //Display generic error if none of above
  196. pd.messageBox(pd.statics.MB_ERROR, PDMSG.ERR.IDS_ERR_FILE_UPLOAD, pdError.message, pdError.detail);
  197. return;
  198. }
  199. },
  200. _uploaderOnprogressHandler: function(evt) {
  201. if (evt.type == "progress"){
  202. if (this.mode == pd.statics.MODE_EDIT){
  203. self.isProcessing = true;
  204. pd_progressBar.onStageTwo(PDMSG.IPT.IDS_IPT_PROGRESS_STAGE_LOAD);
  205. } else {
  206. var percent = Math.round(evt.decimal * 100);
  207. var currentLoad = evt.bytesLoaded - pd_progressBar.previousBytesLoaded;
  208. if (currentLoad > pd_progressBar.maxBytesPerLoad){
  209. pd_progressBar.maxBytesPerLoad = currentLoad;
  210. }
  211. if ((evt.bytesTotal - evt.bytesLoaded) < pd_progressBar.maxBytesPerLoad) {
  212. self.isProcessing = true;
  213. pd_progressBar.onStageOne(100);
  214. pd_progressBar.onStageTwo();
  215. } else {
  216. pd_progressBar.onStageOne(percent);
  217. }
  218. pd_progressBar.previousBytesLoaded = evt.bytesLoaded;
  219. }
  220. } else {
  221. self.isProcessing = false;
  222. }
  223. },
  224. _uploaderOncompleteHandler: function(response, isGovernorCheck) {
  225. try {
  226. if (!isGovernorCheck) {
  227. this.setStatus(pd.statics.STATUS_FINISH);
  228. }
  229. pd.parseResponse(response);
  230. var pdSpecDoc = null;
  231. if (dojo.isSafari) {
  232. var responseNode = domConstruct.toDom(response);
  233. if (responseNode.querySelector) {
  234. pdSpecDoc = responseNode.querySelector("#pdSpec");
  235. }
  236. } else {
  237. pdSpecDoc = response.getElementById("pdSpec");
  238. }
  239. if (pdSpecDoc) {
  240. if (!isGovernorCheck) {
  241. var pdSpecValue = pdSpecDoc.textContent || pdSpecDoc.innerText;
  242. this.renderMainContent(pdSpecValue);
  243. }
  244. } else {
  245. //something wrong, let error handler take care of it
  246. throw({
  247. response: response
  248. });
  249. }
  250. return true;
  251. } catch (err) {
  252. this._uploaderOnerrorHandler(err);
  253. return false;
  254. }
  255. },
  256. _renderMainContent: function(pdSpecValue) {
  257. this.destroy();
  258. //create preview grid
  259. var pdDataStore = new PdDataStore({xmlInput: pdSpecValue});
  260. var dataStore = ObjectStore({objectStore: pdDataStore});
  261. pd.placeDiv("pd_preview_grid_div", pd_previewContainer.domNode);
  262. this.previewGrid = new PreviewGrid({
  263. store: dataStore,
  264. "aria-label":PDMSG.ACC.IDS_ACC_GROUP_LABEL_PREVIEW_PANE
  265. },"pd_preview_grid_div");
  266. this.previewGrid.startup();
  267. //create column view grid
  268. var pdSpecStore = new PdSpecStore({xmlInput: pdSpecValue});
  269. var specStore = ObjectStore({objectStore: pdSpecStore});
  270. pd.placeDiv("pd_column_grid_div", pd_columnGridContainer.domNode);
  271. this.columnGrid = new ColumnGrid({
  272. store: specStore,
  273. previewGrid: this.previewGrid,
  274. "aria-label":PDMSG.ACC.IDS_ACC_GROUP_LABEL_METADATA_TABLE
  275. },"pd_column_grid_div");
  276. this.columnGrid.startup();
  277. //create comboBox
  278. pd.placeDiv("pd_column_combobox_div", pd_columnComboboxContainer.domNode);
  279. this.comboBox = new ComboBox({
  280. store: pdSpecStore,
  281. searchAttr: "name",
  282. columnGrid: this.columnGrid,
  283. "aria-label":PDMSG.ACC.IDS_ACC_GROUP_LABEL_COLUMN_NAME_SEARCH_BOX
  284. },"pd_column_combobox_div");
  285. this.comboBox.startup();
  286. },
  287. _publishBtnOnclickHandler: function(){
  288. if (this.columnGrid.selection.getSelected().length == 0){
  289. pd.messageBox(pd.statics.MB_ERROR, PDMSG.ERR.IDS_ERR_NO_COLUMN_INCLUDED);
  290. return false;
  291. }
  292. if (!this.columnGrid.hasFactSelected()) {
  293. var dlg = new MessageDialog.Confirm(PDMSG.IPT.IDS_IPT_APPLICATION_TITLE,
  294. PDMSG.IPT.IDS_IPT_NO_FACT_CONFIRM_MESSAGE,
  295. PDMSG.IPT.IDS_IPT_NO_FACT_CONFIRM_DETAILS,
  296. dojo.hitch(this, this._showPackageExplorer));
  297. dlg.startup();
  298. dlg.show();
  299. } else {
  300. this._showPackageExplorer();
  301. }
  302. },
  303. _closeBtnOnclickHandler: function() {
  304. pd_uploader.set("disabled", true);
  305. if (this.mode == pd.statics.MODE_NORMAL && self.isProcessing) {
  306. pd_progressBar.onCancel();
  307. this._removeEventListeners();
  308. pd_onClose(self.isProcessing,g_pd_backURL);
  309. } else {
  310. this._removeEventListeners();
  311. location.href = g_pd_backURL;
  312. }
  313. },
  314. _showPackageExplorer: function() {
  315. var oDlg = new SaveDialog({
  316. title: PDMSG.IPT.IDS_IPT_FILE_DIALOG_TITLE,
  317. filter: 'content-folder',
  318. sMainActionButtonLabel: PDMSG.IPT.IDS_IPT_PUBLISH_BUTTON,
  319. "class": "bux-fileDialog",
  320. saveAsHandler: dojo.hitch(this, this._saveAsHandler),
  321. sLocationLabel: BUXMSG.FDG.IDS_FDG_SAVE_AS_LOCATION,
  322. pdDefaultName: this.dataSetName
  323. });
  324. oDlg.startup();
  325. oDlg.show();
  326. },
  327. _saveAsHandler: function(saveAsName, oLink, _callBackFunction, oContainerItem){
  328. this.folderSearchPath = oContainerItem.i.cm$searchPath;
  329. D_ManagePersonalData.m_sTargetPath = this.folderSearchPath;
  330. D_ManagePersonalData.m_sTargetName = this.saveAsPackageName = saveAsName;
  331. D_ManagePersonalData.m_docInputXML = this.columnGrid.pdGetXml();
  332. D_ManagePersonalData.pd_packageOverwriteConfirm = dojo.hitch(this, this.packageOverwriteConfirm);
  333. D_ManagePersonalData.f_checkPackageNameOnOK();
  334. },
  335. packageOverwriteConfirm: function(){
  336. var dlg = new MessageDialog.Confirm(PDMSG.IPT.IDS_IPT_APPLICATION_TITLE,
  337. string.substitute(PDMSG.IPT.IDS_IPT_OVERWRITE_PACKAGE_CONFIRM_MESSAGE,
  338. {0: this.saveAsPackageName}), null,
  339. dojo.hitch(D_ManagePersonalData, D_ManagePersonalData.f_publishPackage));
  340. dlg.startup();
  341. dlg.show();
  342. return dlg;
  343. },
  344. postCreate : function() {
  345. this.pdsContentLabelNode.innerHTML = PDMSG.IPT.IDS_IPT_FILE_NAME_LABEL+"&nbsp;";
  346. this.pd_uploaderForm = dom.byId("pd_uploaderForm");
  347. this.setStatus(pd.statics.STATUS_INIT);
  348. this._eventListeners[this._eventListeners.length] = this.connect(pd_uploader, "onChange", this._uploaderOnchangeHandler);
  349. this._eventListeners[this._eventListeners.length] = this.connect(pd_uploader, "onProgress", dojo.hitch(this, this._uploaderOnprogressHandler));
  350. this._eventListeners[this._eventListeners.length] = this.connect(pd_uploader, "onComplete", this._uploaderOncompleteHandler);
  351. this._eventListeners[this._eventListeners.length] = this.connect(pd_uploader, "onError", this._uploaderOnerrorHandler);
  352. },
  353. startup: function() {
  354. this.inherited(arguments);
  355. this._eventListeners[this._eventListeners.length] = this.connect(pd_publishBtn, "onClick", this._publishBtnOnclickHandler);
  356. this._eventListeners[this._eventListeners.length] = this.connect(pd_cancelBtn, "onClick", dojo.hitch(this, this._closeBtnOnclickHandler));
  357. this._eventListeners[this._eventListeners.length] = this.connect(pd_closeBtn, "onclick", dojo.hitch(this, this._closeBtnOnclickHandler));
  358. pd.connect.subscribe("/pd/widgets/grid/cells/applyChange", dojo.hitch(this, function(){
  359. this.columnGrid.edit.apply();
  360. }));
  361. pd.setSupplementId(pd_publishBtn.domNode, "IMPORT_DIALOG_PUBLISH_BUTTON");
  362. pd.setSupplementId(pd_cancelBtn.domNode, "IMPORT_DIALOG_CANCEL_BUTTON");
  363. },
  364. destroy: function() {
  365. if (this.columnGrid){
  366. this.columnGrid.destroy();
  367. }
  368. if (this.comboBox){
  369. this.comboBox.destroy();
  370. }
  371. if (this.previewGrid){
  372. this.previewGrid.destroy();
  373. }
  374. },
  375. _removeEventListeners: function() {
  376. for (var i=0; i < this._eventListeners.length; i++) {
  377. dojo.disconnect(this._eventListeners[i]);
  378. }
  379. },
  380. isProcessing: false,
  381. _eventListeners: []
  382. });
  383. }
  384. );