|
| 1 | +angular.module("sn.chg_soc.data", []) |
| 2 | + .service("dataService", ["$http", "$q", "$window", "i18n", "urlService", "ganttChart", "durationFormatter", "SOC", "$filter", function($http, $q, $window, i18n, urlService, ganttChart, durationFormatter, SOC, $filter) { |
| 3 | + var dataService = this; |
| 4 | + |
| 5 | + dataService.more = false; |
| 6 | + dataService.count = 0; |
| 7 | + dataService.child_table = {}; |
| 8 | + dataService.definition = {}; |
| 9 | + dataService.style = { |
| 10 | + chg_soc_style_rule: {}, |
| 11 | + chg_soc_definition_style_rule: {}, |
| 12 | + chg_soc_def_child_style_rule: {}, |
| 13 | + style_sheet: "" |
| 14 | + }; |
| 15 | + dataService.tasks = { |
| 16 | + data: [], |
| 17 | + links: [] |
| 18 | + }; |
| 19 | + dataService.allRecords = {}; |
| 20 | + |
| 21 | + function isValidDate(date) { |
| 22 | + if (Object.prototype.toString.call(date) === "[object Date]" && !isNaN(date.getTime())) |
| 23 | + return true; |
| 24 | + return false; |
| 25 | + } |
| 26 | + |
| 27 | + function buildFields(record, selectedFieldsList, tableMeta) { |
| 28 | + var result = []; |
| 29 | + if (!selectedFieldsList) |
| 30 | + return result; |
| 31 | + var selectedFields = selectedFieldsList.split(","); |
| 32 | + selectedFields.forEach(function(fieldName) { |
| 33 | + if (fieldName && tableMeta[fieldName]) |
| 34 | + result.push({ |
| 35 | + column_name: fieldName, |
| 36 | + label: tableMeta[fieldName].label, |
| 37 | + display_value: record[fieldName].display_value, |
| 38 | + value: record[fieldName].value, |
| 39 | + }); |
| 40 | + }); |
| 41 | + return result; |
| 42 | + } |
| 43 | + |
| 44 | + function buildRecord(record, chgSocDef, tableMeta, styleRule, scheduleWindow) { |
| 45 | + var ganttUtil = ganttChart.getGantt(urlService.socId); |
| 46 | + var startDate = ganttUtil.date.parseDate(record[chgSocDef.start_date_field.value].display_value_internal, "xml_date"); |
| 47 | + var endDate = ganttUtil.date.parseDate(record[chgSocDef.end_date_field.value].display_value_internal, "xml_date"); |
| 48 | + // Check start/end dates are valid before adding the task to gantt chart |
| 49 | + if (!isValidDate(startDate) || !isValidDate(endDate)) |
| 50 | + return; |
| 51 | + |
| 52 | + var recordEvent = { |
| 53 | + id: record.sys_id ? record.sys_id.value : "", |
| 54 | + text: record.number ? record.number.display_value : "", |
| 55 | + number: record.number ? record.number.display_value : "", |
| 56 | + chg_soc_def: chgSocDef.sys_id.value, |
| 57 | + config_item: record.cmdb_ci ? record.cmdb_ci.display_value : "", |
| 58 | + start_date: startDate, |
| 59 | + end_date: endDate, |
| 60 | + dur_display: durationFormatter.buildDurationDisplay(startDate, endDate), |
| 61 | + order: 0, |
| 62 | + progress: 0, |
| 63 | + table: record.sys_class_name ? record.sys_class_name.value : chgSocDef.table_name.value, |
| 64 | + left_fields: buildFields(record, chgSocDef.popover_left_col_fields.value, tableMeta), |
| 65 | + right_fields: buildFields(record, chgSocDef.popover_right_col_fields.value, tableMeta), |
| 66 | + record: record, |
| 67 | + blackout_spans: [], |
| 68 | + maint_spans: [], |
| 69 | + sys_id: record.sys_id ? record.sys_id.value : "", |
| 70 | + short_description: record.short_description ? record.short_description.display_value : "", |
| 71 | + __visible: true |
| 72 | + }; |
| 73 | + |
| 74 | + if (styleRule && styleRule.sys_id) |
| 75 | + recordEvent.style_class = SOC.STYLE_PREFIX + styleRule.sys_id; |
| 76 | + |
| 77 | + if (scheduleWindow) { |
| 78 | + if (chgSocDef.show_maintenance.value) |
| 79 | + angular.forEach(scheduleWindow.maintenance, function (schedule) { |
| 80 | + Array.prototype.push.apply(recordEvent.maint_spans, schedule.spans); |
| 81 | + }); |
| 82 | + if (chgSocDef.show_blackout.value) |
| 83 | + angular.forEach(scheduleWindow.blackout, function (schedule) { |
| 84 | + Array.prototype.push.apply(recordEvent.blackout_spans, schedule.spans); |
| 85 | + }); |
| 86 | + } else { |
| 87 | + recordEvent.id = chgSocDef.sys_id.value + "_" + recordEvent.id; |
| 88 | + recordEvent.parent = record[chgSocDef.reference_field.value].value; |
| 89 | + } |
| 90 | + |
| 91 | + dataService.allRecords[recordEvent.id] = { |
| 92 | + style_rule: styleRule, |
| 93 | + sys_id: record.sys_id ? record.sys_id.value : "", |
| 94 | + table_name: record.sys_class_name ? record.sys_class_name.value : chgSocDef.table_name.value, |
| 95 | + chg_soc_def: chgSocDef.sys_id.value |
| 96 | + }; |
| 97 | + |
| 98 | + return recordEvent; |
| 99 | + } |
| 100 | + |
| 101 | + function buildItem(result, item) { |
| 102 | + // Build change_request record |
| 103 | + var record = buildRecord(result[item.table_name][item.sys_id], result.chg_soc_definition, result[item.table_name].__table_meta, item.style, item.schedule_window); |
| 104 | + if (!record) |
| 105 | + return; |
| 106 | + |
| 107 | + dataService.tasks.data.push(record); |
| 108 | + |
| 109 | + // Build related tasks |
| 110 | + if (item.related) |
| 111 | + for (var childSocDefId in item.related) { |
| 112 | + var childRecords = item.related[childSocDefId]; |
| 113 | + for (var i = 0; i < childRecords.length; i++) { |
| 114 | + var childRecord = buildRecord(result[childRecords[i].table_name][childRecords[i].sys_id], result.chg_soc_definition.__child[childSocDefId], result[childRecords[i].table_name].__table_meta, childRecords[i].style); |
| 115 | + if (childRecord) |
| 116 | + dataService.tasks.data.push(childRecord); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + dataService.buildData = function(result) { |
| 122 | + if (!result) |
| 123 | + return; |
| 124 | + |
| 125 | + dataService.more = result.__more; |
| 126 | + dataService.count = result.__change_count; |
| 127 | + |
| 128 | + // Start with the definition object |
| 129 | + if (result.chg_soc_definition) |
| 130 | + dataService.definition = result.chg_soc_definition; |
| 131 | + |
| 132 | + // Ordered change requests with style and related records |
| 133 | + if (result.__struct) |
| 134 | + for (var i = 0; i < result.__struct.length; i++) |
| 135 | + buildItem(result, result.__struct[i]); |
| 136 | + |
| 137 | + // Find all child tables |
| 138 | + for (var table in result) |
| 139 | + if (result[table].__has_children) |
| 140 | + dataService.child_table[table] = result[table].__table_meta; |
| 141 | + |
| 142 | + // Set style rules and style sheet to the model |
| 143 | + dataService.style.chg_soc_style_rule = result.chg_soc_style_rule; |
| 144 | + dataService.style.chg_soc_definition_style_rule = result.chg_soc_definition_style_rule; |
| 145 | + dataService.style.chg_soc_def_child_style_rule = result.chg_soc_def_child_style_rule; |
| 146 | + dataService.style.style_sheet = result._css; |
| 147 | + }; |
| 148 | + |
| 149 | + dataService.addData = function(result) { |
| 150 | + dataService.more = result.__more; |
| 151 | + dataService.count = result.__change_count; |
| 152 | + |
| 153 | + if (result.__struct) |
| 154 | + for (var i = 0; i < result.__struct.length; i++) |
| 155 | + buildItem(result, result.__struct[i]); |
| 156 | + |
| 157 | + for (var table in result) |
| 158 | + if (result[table].__has_children) |
| 159 | + dataService.child_table[table] = result[table].__table_meta; |
| 160 | + }; |
| 161 | + |
| 162 | + dataService.initPage = function(chgSocDefId, condition) { |
| 163 | + var deferred = $q.defer(); |
| 164 | + var url = SOC.GET_CHANGE_SCHEDULE + chgSocDefId; |
| 165 | + var config = {}; |
| 166 | + config.params = { |
| 167 | + sysparm_ck: $window.g_ck |
| 168 | + }; |
| 169 | + if (condition) |
| 170 | + config.params.condition = condition; |
| 171 | + $http.get(url, config).then(function(response){ |
| 172 | + deferred.resolve(dataService.buildData(response.data.result)); |
| 173 | + }, function(response) { |
| 174 | + deferred.reject(response); |
| 175 | + }); |
| 176 | + return deferred.promise; |
| 177 | + }; |
| 178 | + |
| 179 | + dataService.getChanges = function() { |
| 180 | + var deferred = $q.defer(); |
| 181 | + var url = SOC.GET_CHANGE_SCHEDULE + dataService.definition.sys_id.value; |
| 182 | + var config = {}; |
| 183 | + config.params = { |
| 184 | + sysparm_ck: $window.g_ck, |
| 185 | + count: dataService.count |
| 186 | + }; |
| 187 | + if (dataService.definition.condition.dryRun) |
| 188 | + config.params.condition = dataService.definition.condition.value; |
| 189 | + |
| 190 | + $http.get(url, config).then(function(response){ |
| 191 | + deferred.resolve(dataService.addData(response.data.result)); |
| 192 | + }, function(response) { |
| 193 | + deferred.reject(response); |
| 194 | + }); |
| 195 | + return deferred.promise; |
| 196 | + }; |
| 197 | + |
| 198 | + dataService.getChildren = function(parentId) { |
| 199 | + var res = $filter("filter")(dataService.tasks.data, function(task) { |
| 200 | + return task.parent === parentId; |
| 201 | + }); |
| 202 | + return res; |
| 203 | + }; |
| 204 | + |
| 205 | + dataService.destroyData = function() { |
| 206 | + dataService.more = false; |
| 207 | + dataService.count = 0; |
| 208 | + dataService.child_table = {}; |
| 209 | + dataService.definition = {}; |
| 210 | + dataService.style = { |
| 211 | + chg_soc_style_rule: {}, |
| 212 | + chg_soc_definition_style_rule: {}, |
| 213 | + chg_soc_def_child_style_rule: {}, |
| 214 | + style_sheet: "" |
| 215 | + }; |
| 216 | + dataService.tasks = { |
| 217 | + data: [], |
| 218 | + links: [] |
| 219 | + }; |
| 220 | + dataService.allRecords = {}; |
| 221 | + }; |
| 222 | + |
| 223 | + dataService.parseQuery = function(condition) { |
| 224 | + condition = condition + ""; |
| 225 | + var deferred = $q.defer(); |
| 226 | + var url = SOC.GET_PARSE_QUERY + condition; |
| 227 | + var config = {}; |
| 228 | + config.params = {}; |
| 229 | + config.params.sysparm_ck = $window.g_ck; |
| 230 | + |
| 231 | + $http.get(url, config).then(function(response) { |
| 232 | + deferred.resolve(response.data.result); |
| 233 | + }, function(response) { |
| 234 | + deferred.reject(response); |
| 235 | + }); |
| 236 | + |
| 237 | + return deferred.promise; |
| 238 | + }; |
| 239 | + |
| 240 | + function checkSecurityObject() { |
| 241 | + return dataService.definition && dataService.definition.__security; |
| 242 | + } |
| 243 | + |
| 244 | + dataService.canCreate = function() { |
| 245 | + if (checkSecurityObject() && dataService.definition.__security.canCreate) |
| 246 | + return dataService.definition.__security.canCreate; |
| 247 | + return false; |
| 248 | + }; |
| 249 | + |
| 250 | + dataService.canRead = function() { |
| 251 | + if (checkSecurityObject() && dataService.definition.__security.canRead) |
| 252 | + return dataService.definition.__security.canRead; |
| 253 | + return false; |
| 254 | + }; |
| 255 | + |
| 256 | + dataService.canWrite = function() { |
| 257 | + if (checkSecurityObject() && dataService.definition.__security.canWrite) |
| 258 | + return dataService.definition.__security.canWrite; |
| 259 | + return false; |
| 260 | + }; |
| 261 | + |
| 262 | + dataService.canDelete = function() { |
| 263 | + if (checkSecurityObject() && dataService.definition.__security.canDelete) |
| 264 | + return dataService.definition.__security.canDelete; |
| 265 | + return false; |
| 266 | + }; |
| 267 | + |
| 268 | + dataService.trackEvent = function(source) { |
| 269 | + if ($window.GlideWebAnalytics && $window.GlideWebAnalytics.trackEvent) |
| 270 | + $window.GlideWebAnalytics.trackEvent('com.snc.change_management.soc', 'Change Schedules', source, 0, 0); |
| 271 | + }; |
| 272 | + }]); |
0 commit comments