12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- /*
- *+------------------------------------------------------------------------+
- *| Licensed Materials - Property of IBM
- *| IBM Cognos Products: Content Explorer
- *| (C) Copyright IBM Corp. 2015, 2018
- *|
- *| US Government Users Restricted Rights - Use, duplication or disclosure
- *| restricted by GSA ADP Schedule Contract with IBM Corp.
- *+------------------------------------------------------------------------+
- */
- import { GlassContextHelper, ContentStoreObject, ContentManagement } from '@businessanalytics/content-nav';
- class SaveUtils {
- constructor() {
- }
- canSave = (updatedInfo, l10n, glassContext) => {
- const keys = Object.keys(updatedInfo);
- if (keys.length === 0){
- return false;
- }
- if (updatedInfo.defaultName && (updatedInfo.defaultName.indexOf('\'') > -1) && (updatedInfo.defaultName.indexOf('"') > -1)){
- GlassContextHelper.displayToast(glassContext, l10n.get('errorMessageApostropheError'), {
- 'type': 'error'
- });
- return false;
- }
- return true;
- }
- prepareRequestObj = (updatedInfo, objInfo) => {
- let type = null;
- let url = null;
- if (ContentStoreObject.getMetadataModelPackage(objInfo) && objInfo.refReportSelfLink) {
- type = 'report';
- url = objInfo.refReportSelfLink;
- } else {
- type = ContentStoreObject.getType(objInfo);
- url = ContentStoreObject.getSelfLink(objInfo);
- }
- updatedInfo.type = type;
- const options = {
- 'headers': {
- 'Accept': 'application/json',
- 'Content-Type': 'application/json'
- },
- 'type': 'PUT',
- 'url': url,
- 'data': JSON.stringify(updatedInfo)
- };
- return options;
- }
- sendUpdateRequest = (request, glassContext, l10n) => {
- return glassContext.getCoreSvc('.Ajax').ajax(request)
- .then((response) => {
- return Promise.resolve(response);
- })
- .catch((err) => {
- const isDuplicate = ContentManagement.isNameConflict(err);
- const isUnknownUpdateError = !isDuplicate && ContentManagement.isUnknownUpdateError(err);
- if (isDuplicate || isUnknownUpdateError) {
- const message = isDuplicate ?
- l10n.get('renameCollisionError', {
- 'name': request.data && JSON.parse(request.data).defaultName || ''
- }) : l10n.get('unknownUpdateError');
- GlassContextHelper.displayToast(glassContext, message, {
- 'type': 'error'
- });
- } else {
- GlassContextHelper.showAjaxServiceErrorMessage(glassContext, err.jqXHR);
- }
- return Promise.resolve(err);
- });
- }
- updateItem = (updatedInfo, l10n, glassContext, objInfo) => {
- if (this.canSave(updatedInfo, l10n, glassContext)) {
- const requestObj = this.prepareRequestObj(updatedInfo, objInfo);
- return this.sendUpdateRequest(requestObj, glassContext, l10n);
- } else {
- return Promise.resolve(false);
- }
- }
- }
- export default new SaveUtils();
|