API Name: sn_chg_soc.SoCStyleRuleSNC

var SoCStyleRuleSNC = Class.create();
SoCStyleRuleSNC.prototype = Object.extendsObject(SoC, {

TABLES: {
chg_soc_style_rule: true,
chg_soc_definition_style_rule: true,
chg_soc_def_child_style_rule: true
},

initialize: function(_gr, _gs) {
SoC.prototype.initialize.call(this, _gr, _gs);
},

/**
* Returns the name of the style which will be generated by this style rule
*/

getStyleName: function() {
return "soc_" + this._gr.getUniqueValue();
},

/**
* Refresh the CSS for the style rule
*/

refreshStyle: function() {
var selector = "\ndiv." + this.getStyleName();
this._gr.style = selector + " .gantt_task_content {\n" +
" background-color: " + current.event_color + ";\n" +
"}\n" +
selector + " .gantt_cell {\n" +
" font-weight: " + current.label_weight + ";\n" +
" color: " + current.label_color + ";\n" +
"}";
},

_isValidStyleRule: function(chgSocDef, sysId, tableName) {
var result = {
isValid: false,
styleRecord: null,
errorMessage: ""
};
if (!chgSocDef || !sysId || !tableName || !this.TABLES[tableName]) {
result.errorMessage = "Invalid Style Rule";
return result;
}
var styleGr = new GlideRecordSecure(tableName);
if (sysId !== "-1" && !styleGr.get(sysId)) {
result.errorMessage = "Style rule: " + sysId + " not found in " + tableName;
return result;
}
result.isValid = true;
result.styleRecord = styleGr;
return result;
},

_getNewData: function(chgSocDef, records) {
var chgSocDefGr = new GlideRecord("chg_soc_definition");
chgSocDefGr.get(chgSocDef);

var socModel = new SoCModelBuilder();
socModel.buildDefinitionModel(chgSocDefGr);

for (var id in records)
if (records.hasOwnProperty(id)) {
var recordGr = new GlideRecord(records[id].table_name);
if (!recordGr.get(records[id].sys_id))
continue;
records[id].style_rule = socModel._getStyleReference(records[id].chg_soc_def, recordGr);
}

return {
records: records,
style_sheet: socModel.getCSS()
};
},

serviceUpdateStyleRule: function(chgSocDef, style, records) {
var sysId = style.sys_id ? style.sys_id.value : "";
var tableName = style.sys_class_name.value ? style.sys_class_name.value : "";

var validatedStyleRule = this._isValidStyleRule(chgSocDef, sysId, tableName);
if (!validatedStyleRule.isValid)
return new sn_ws_err.BadRequestError(validatedStyleRule.errorMessage);

var name = style.name ? style.name.display_value : "";
var eventColor = style.event_color ? style.event_color.value : "";
var condition = style.condition ? style.condition.value : "";

var styleGr = validatedStyleRule.styleRecord;
styleGr.setValue("chg_soc_definition", chgSocDef);
if (name)
styleGr.setValue("name", name);
if (eventColor)
styleGr.setValue("event_color", eventColor);
if (condition)
styleGr.setValue("condition", condition);

var recordSysId = sysId === "-1" ? styleGr.insert() : styleGr.update();

// If we are not creating a new record or not changing the condition
if (!records)
return recordSysId;

var result = this._getNewData(chgSocDef, records);
result.style = new SoCDefinitionStyleRule(styleGr).toJS();
return result;
},

serviceDeleteStyleRule: function(chgSocDef, style, records) {
var sysId = style.sys_id ? style.sys_id.value : "";
var tableName = style.sys_class_name.value ? style.sys_class_name.value : "";

var validatedStyleRule = this._isValidStyleRule(chgSocDef, sysId, tableName);
if (!validatedStyleRule.isValid)
return new sn_ws_err.BadRequestError(validatedStyleRule.errorMessage);

var gr = validatedStyleRule.styleRecord;
if (!gr.deleteRecord())
return new sn_ws_err.BadRequestError("Unable to delete style rule: " + sysId + " from " + tableName);

return this._getNewData(chgSocDef, records);
},

type: 'SoCStyleRuleSNC'
});

SoCStyleRuleSNC.findByTableName = function(tableName) {
if (!tableName)
return null;

var styleRuleGr = new GlideRecord(SoC.STYLE_RULE);
styleRuleGr.addActiveQuery();
styleRuleGr.addQuery(SoC.TABLE_NAME, tableName);
styleRuleGr.addQuery(SoC.CONDITION, "ISNOTEMPTY", "");
styleRuleGr.orderBy(SoC.ORDER);
styleRuleGr.query();
return styleRuleGr;
};