Name
sn_irm_shared_cmn.GRCDocumentUtilBase
Description
No description available
Script
var GRCDocumentUtilBase = Class.create();
GRCDocumentUtilBase.prototype = {
initialize: function() {
this.DOC_FORMAT = "docx";
this.HTML_FORMAT = "html";
this.PDF_FORMAT = "pdf";
this.DOCUMENT_VERSIONS = 'sn_irm_shared_cmn_document_version';
},
createDocumentVersion: function(document) {
var docVersion = new GlideRecord(this.DOCUMENT_VERSIONS);
docVersion.setValue('table_name', document.table_name);
docVersion.setValue('record_id', document.record_id);
docVersion.setValue('owner', document.owner);
docVersion.setValue('valid_from', document.valid_from);
docVersion.setValue('valid_to', document.valid_to);
docVersion.setValue('reason_for_change', document.reason_for_change);
docVersion.setValue('name', document.name);
docVersion.reviewers = document.reviewers;
docVersion.approvers = document.approvers;
docVersion.contributors = document.contributors;
return docVersion.insert();
},
updateDocumentVersion: function(docVersion, fields) {
for (var field in fields) {
docVersion[field] = fields[field];
}
return docVersion.update();
},
deleteDocumentVersions: function(tableName, recordId) {
var docVersion = new GlideRecord('sn_irm_shared_cmn_document_version');
docVersion.addQuery('table_name', tableName);
docVersion.addQuery('record_id', recordId);
docVersion.query();
if (docVersion.hasNext())
docVersion.deleteMultiple();
},
isRedliningSetUpDone: function() {
if ((GlidePluginManager.isActive('sn_onedrive_spoke') || GlidePluginManager.isActive('com.sn_onedrive_spoke'))) {
var driveType = gs.getProperty('sn_grc.drive_type');
if (driveType && driveType != 'none')
return true;
}
return false;
},
convertHtmlToDoc: function(requestParams) {
var response = this._validateRequestParams(requestParams, 'convertHtmlToDocOrPdf');
if (response.status && response.status != 'Error') {
var document = this._getDocumentRecord(requestParams.table, requestParams.record);
if (document) {
return new sn_docviewer.DocumentConversionOnDemand().convertHTML(requestParams.record.getValue(field), this.DOC_FORMAT, document.getTableName(), document.getUniqueValue(), requestParams.fileName);
}
}
return response;
},
convertDocToHTMLOrPDF: function(requestParams) {
var response = this._validateRequestParams(requestParams, 'convertDocToHtmlOrPdf');
if (response.status && response.status != 'Error') {
var document = this._getDocumentRecord(requestParams.table, requestParams.record);
if (document) {
var attachment = new GlideRecord('sys_attachment');
if (attachment.get(document.attachment)) {
return new sn_docviewer.DocumentConversionOnDemand().convertDocument(attachment.getUniqueValue(), requestParams.type, attachment.getValue('file_name').split('.').slice(0, -1).join('.'));
}
}
}
return response;
},
convertHtmlToPDF: function(requestParams) {
var response = this._validateRequestParams(requestParams, 'convertHtmlToDocOrPdf');
if (response.status && response.status != 'Error') {
return new sn_docviewer.DocumentConversionOnDemand().convertHTML(requestParams.record.getValue(field), this.PDF_FORMAT, requestParams.table, requestParams.record.getUniqueValue(), requestParams.fileName);
}
return response;
},
copyAttachmentContent: function(requestParams) {
var response = this._validateRequestParams(requestParams, 'copyContent');
if (response.status && response.status != 'Error') {
var attachment = new GlideRecord('sys_attachment');
if (attachment.get(requestParams.attachmentId)) {
var gsaTextFile = new GlideSysAttachment();
var htmlContent = gsaTextFile.getContent(attachment);
var updateSuccessFull = this._updateTargetRecord(requestParams.table, requestParams.record, requestParams.field, htmlContent);
if (!updateSuccessFull) {
response.status = 'Error';
response.message = gs.getMessage('Updating the record failed');
return response;
}
response.status = 'completed';
}
}
return response;
},
copyFormattedHTMLContent: function(requestParams) {
var response = this._validateRequestParams(requestParams, 'copyContent');
if (response.status && response.status != 'Error') {
var htmlUtil = new global.GRCHtmlConversionUtil();
var htmlContent = htmlUtil.getHtmlContent(requestParams.attachmentId);
if (htmlContent) {
var updateSuccessFull = this._updateTargetRecord(requestParams.table, requestParams.record, requestParams.field, htmlContent);
if (!updateSuccessFull) {
response.status = 'Error';
response.message = gs.getMessage('Updating the record failed');
return response;
}
response.status = 'completed';
} else {
response.status = 'Error';
response.message = gs.getMessage('Formatting html content failed');
}
}
return response;
},
getDocumentRecordWithRecordId: function(table, recordId) {
return this._getDocumentRecordWithRecordId(table, recordId);
},
convertAndCopyContent: function(requestParams, isFormatted) {
return this._convertAndCopyContent(requestParams, isFormatted);
},
_convertAndCopyContent: function(requestParams, isFormatted) {
var result;
var resultDocConvert = this.convertDocToHTMLOrPDF(requestParams);
//Copy content to given record's specified field
if (resultDocConvert.status != "Error") {
requestParams.attachmentId = resultDocConvert.converted_attachment_id;
if (isFormatted) {
result = this.copyFormattedHTMLContent(requestParams);
} else {
result = this.copyAttachmentContent(requestParams);
}
}
return result;
},
_validateRequestParams: function(requestParams, action) {
var response = {};
if (!requestParams.table) {
response.status = 'Error';
response.message = gs.getMessage('Table is mandatory to convert document');
return response;
}
if (!requestParams.record) {
response.status = 'Error';
response.message = gs.getMessage('Record is mandatory to convert document');
return response;
}
if (action == 'convertHtmlToDocOrPdf') {
if (!requestParams.field) {
response.status = 'Error';
response.message = gs.getMessage('Field is mandatory to convert document');
return response;
}
if (!requestParams.fileName) {
response.status = 'Error';
response.message = gs.getMessage('File name is mandatory to convert document');
return response;
}
}
if (action == 'copyContent') {
if (!requestParams.attachmentId) {
response.status = 'Error';
response.message = gs.getMessage('Attaxchment is mandatory to copy content');
return response;
}
if (!requestParams.field) {
response.status = 'Error';
response.message = gs.getMessage('Field is mandatory to copy content');
return response;
}
}
response.status = 'completed';
return response;
},
_updateTargetRecord: function(table, record, field, content) {
var targetRecord = new GlideRecord(table);
if (targetRecord.get(record.getUniqueValue())) {
targetRecord.setValue(field, content);
return targetRecord.update();
}
return false;
},
_getDocumentRecord: function(table, record) {
var documentRecord = new GlideRecord(this.DOCUMENT_VERSIONS);
documentRecord.addQuery('table_name', table);
documentRecord.addQuery('record_id', record.getUniqueValue());
documentRecord.orderByDesc('sys_updated_on');
documentRecord.setLimit(1);
documentRecord.query();
if (documentRecord.next())
return documentRecord;
return null;
},
_getDocumentRecordWithRecordId: function(table, recordId) {
var documentRecord = new GlideRecord(this.DOCUMENT_VERSIONS);
documentRecord.addQuery('table_name', table);
documentRecord.addQuery('record_id', recordId);
documentRecord.orderByDesc('sys_updated_on');
documentRecord.setLimit(1);
documentRecord.query();
if (documentRecord.next())
return documentRecord;
return null;
},
getNumberOfAttachmentsForDoc: function(recordId) {
return this._getNumberOfAttachmentsForDoc(recordId);
},
getRecentDocumentVersionRecordId: function(table, recordSysId) {
return this._getRecentDocumentVersionRecordId(table, recordSysId);
},
formatFolderPath: function(folderPath) {
return this._formatFolderPath(folderPath);
},
getDateTimeDifferenceInWords: function(firstDateTime, secondDateTime) {
return this._getDateTimeDifferenceInWords(firstDateTime, secondDateTime);
},
_getNumberOfAttachmentsForDoc: function(recordId) {
var result = {};
result.count = 0;
result.isAllowedFormat = false;
result.fileLength = 0;
var grAttachment = new GlideAggregate('sys_attachment');
grAttachment.addQuery('table_name', 'sn_irm_shared_cmn_document_version');
grAttachment.addQuery('table_sys_id', recordId);
grAttachment.addAggregate('COUNT', 'file_name');
grAttachment.groupBy('file_name');
grAttachment.query();
var countOfAttachments = 0;
var fetchedFileName;
while (grAttachment.next()) {
countOfAttachments += parseInt(grAttachment.getAggregate('COUNT', 'file_name'));
if (countOfAttachments >= 2) {
break;
}
fetchedFileName = grAttachment.file_name + '';
}
result.count = countOfAttachments;
if (fetchedFileName) {
result.fileLength = fetchedFileName.length;
result.isAllowedFormat = (fetchedFileName.match(/.docx/g) || []).length == 1;
}
return result;
},
_getRecentDocumentVersionRecordId: function(table, recordSysId) {
var docRec = new GlideRecord('sn_irm_shared_cmn_document_version');
docRec.addQuery('table_name', table);
docRec.addQuery('record_id', recordSysId);
docRec.orderByDesc('sys_updated_on');
docRec.setLimit(1);
docRec.query();
if (docRec.next()) {
return docRec.getUniqueValue();
}
return null;
},
_formatFolderPath: function(folderPath) {
if (folderPath.length > 51) {
var folderPathArr = folderPath.split('/');
var folderPathVal = "";
if (folderPathArr.length > 3) {
for (var i = 1; i < folderPathArr.length; i++) {
if (folderPathArr[i].length > 20) {
folderPathVal = folderPathVal + '/' + folderPathArr[i].slice(0, 20) + '...';
} else {
folderPathVal = folderPathVal + '/' + folderPathArr[i];
}
if (i == 1) {
folderPathVal += '/...';
i += folderPathArr.length - 3;
}
}
folderPath = folderPathVal;
} else if (folderPathArr.length == 3) {
for (var j = 1; j < folderPathArr.length; j++) {
if (folderPathArr[j].length > 20) {
folderPathVal = folderPathVal + '/' + folderPathArr[j].slice(0, 20) + '...';
} else {
folderPathVal = folderPathVal + '/' + folderPathArr[j];
}
}
folderPath = folderPathVal;
} else if (folderPathArr.length == 2) {
folderPath = folderPath.slice(0, 48) + '...';
}
}
return folderPath;
},
_getDateTimeDifferenceInWords: function(firstDateTime, secondDateTime) {
var lastUpdatedDuration = "";
var dateDiff;
var dur = GlideDateTime.subtract(secondDateTime, firstDateTime);
var days = dur.getRoundedDayPart();
var duration = dur.getDurationValue();
if (days > 0) {
lastUpdatedDuration = (days == 1) ? (days + " day ago.") : (days + " days ago.");
} else {
var durationArray = duration.split(':');
if (durationArray[0] == "00" && durationArray[1] == "00") {
lastUpdatedDuration = parseInt(durationArray[2]) == 1 ? parseInt(durationArray[2]) + " second ago." : parseInt(durationArray[2]) + " seconds ago.";
} else if (durationArray[0] == "00" && durationArray[1] != "00") {
lastUpdatedDuration = parseInt(durationArray[1]) == 1 ? parseInt(durationArray[1]) + " minute ago." : parseInt(durationArray[1]) + " minutes ago.";
} else if (durationArray[0] != "00") {
lastUpdatedDuration = parseInt(durationArray[0]) == 1 ? parseInt(durationArray[0]) + " hour ago." : parseInt(durationArray[0]) + " hours ago.";
}
}
return lastUpdatedDuration;
},
type: 'GRCDocumentUtilBase'
};
Sys ID
2d307e185bc40110a72732fb0a81c708