SaveUtils.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| IBM Cognos Products: Content Explorer
  5. *| (C) Copyright IBM Corp. 2015, 2018
  6. *|
  7. *| US Government Users Restricted Rights - Use, duplication or disclosure
  8. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  9. *+------------------------------------------------------------------------+
  10. */
  11. import { GlassContextHelper, ContentStoreObject, ContentManagement } from '@businessanalytics/content-nav';
  12. class SaveUtils {
  13. constructor() {
  14. }
  15. canSave = (updatedInfo, l10n, glassContext) => {
  16. const keys = Object.keys(updatedInfo);
  17. if (keys.length === 0){
  18. return false;
  19. }
  20. if (updatedInfo.defaultName && (updatedInfo.defaultName.indexOf('\'') > -1) && (updatedInfo.defaultName.indexOf('"') > -1)){
  21. GlassContextHelper.displayToast(glassContext, l10n.get('errorMessageApostropheError'), {
  22. 'type': 'error'
  23. });
  24. return false;
  25. }
  26. return true;
  27. }
  28. prepareRequestObj = (updatedInfo, objInfo) => {
  29. let type = null;
  30. let url = null;
  31. if (ContentStoreObject.getMetadataModelPackage(objInfo) && objInfo.refReportSelfLink) {
  32. type = 'report';
  33. url = objInfo.refReportSelfLink;
  34. } else {
  35. type = ContentStoreObject.getType(objInfo);
  36. url = ContentStoreObject.getSelfLink(objInfo);
  37. }
  38. updatedInfo.type = type;
  39. const options = {
  40. 'headers': {
  41. 'Accept': 'application/json',
  42. 'Content-Type': 'application/json'
  43. },
  44. 'type': 'PUT',
  45. 'url': url,
  46. 'data': JSON.stringify(updatedInfo)
  47. };
  48. return options;
  49. }
  50. sendUpdateRequest = (request, glassContext, l10n) => {
  51. return glassContext.getCoreSvc('.Ajax').ajax(request)
  52. .then((response) => {
  53. return Promise.resolve(response);
  54. })
  55. .catch((err) => {
  56. const isDuplicate = ContentManagement.isNameConflict(err);
  57. const isUnknownUpdateError = !isDuplicate && ContentManagement.isUnknownUpdateError(err);
  58. if (isDuplicate || isUnknownUpdateError) {
  59. const message = isDuplicate ?
  60. l10n.get('renameCollisionError', {
  61. 'name': request.data && JSON.parse(request.data).defaultName || ''
  62. }) : l10n.get('unknownUpdateError');
  63. GlassContextHelper.displayToast(glassContext, message, {
  64. 'type': 'error'
  65. });
  66. } else {
  67. GlassContextHelper.showAjaxServiceErrorMessage(glassContext, err.jqXHR);
  68. }
  69. return Promise.resolve(err);
  70. });
  71. }
  72. updateItem = (updatedInfo, l10n, glassContext, objInfo) => {
  73. if (this.canSave(updatedInfo, l10n, glassContext)) {
  74. const requestObj = this.prepareRequestObj(updatedInfo, objInfo);
  75. return this.sendUpdateRequest(requestObj, glassContext, l10n);
  76. } else {
  77. return Promise.resolve(false);
  78. }
  79. }
  80. }
  81. export default new SaveUtils();