| 1 | /* author: jdalbey date: 4/10/2019 */ |
|---|
| 2 | |
|---|
| 3 | // Parse the entire Incident XML Script file |
|---|
| 4 | // Extract the Incidents and Events and create lists of each |
|---|
| 5 | function parseXml(response) |
|---|
| 6 | { |
|---|
| 7 | // Create a parser and grab the high level tag we're interested in |
|---|
| 8 | var parser = new DOMParser(); |
|---|
| 9 | var xmlDoc = parser.parseFromString(response,"text/xml"); |
|---|
| 10 | var eventTags = xmlDoc.getElementsByTagName("SCRIPT_EVENT"); |
|---|
| 11 | console.log("parsing incident xml file containing "+eventTags.length+" event tags."); |
|---|
| 12 | // Process each SCRIPT_EVENT tag |
|---|
| 13 | for (var i = 0; i < eventTags.length; i++) |
|---|
| 14 | { |
|---|
| 15 | var currEvt = eventTags[i]; |
|---|
| 16 | // local variable declarations |
|---|
| 17 | var timeFields; var evtTime; var incidentNum; var incidentTitle; var cadProp; var telProp; |
|---|
| 18 | var proparray = new Array(); |
|---|
| 19 | var evalarray = new Array(); |
|---|
| 20 | // Process all the children of one event |
|---|
| 21 | for (var child = 1; child < currEvt.childNodes.length; child++) |
|---|
| 22 | { |
|---|
| 23 | // Ignore undefined nodes |
|---|
| 24 | if (currEvt.childNodes[child].localName != undefined) |
|---|
| 25 | { |
|---|
| 26 | var tagName = currEvt.childNodes[child].localName.toUpperCase(); |
|---|
| 27 | // Determine the tag type and dispatch it for further processing |
|---|
| 28 | switch(tagName) |
|---|
| 29 | { |
|---|
| 30 | case "TIME_INDEX": |
|---|
| 31 | timeFields = currEvt.childNodes[child].textContent.split(":"); |
|---|
| 32 | evtTime = new Time(Number(timeFields[0]), Number(timeFields[1]), Number(timeFields[2])); |
|---|
| 33 | break; |
|---|
| 34 | case "INCIDENT": |
|---|
| 35 | /* This tag identifies a new incident. As long as it appears before any |
|---|
| 36 | other tags, we can use it to create a new incident. (The alternative is to |
|---|
| 37 | have a separate function that parses xml to extract incidents before |
|---|
| 38 | we load events.) Creating an event (below) requires that the incident has already |
|---|
| 39 | been created. */ |
|---|
| 40 | incidentNum = Number(currEvt.childNodes[child].attributes["LogNum"].value); |
|---|
| 41 | // If this incident number doesn't exist |
|---|
| 42 | if (incidents.get(incidentNum) == undefined) |
|---|
| 43 | { |
|---|
| 44 | // prepare title field |
|---|
| 45 | incidentTitle = currEvt.childNodes[child].textContent; |
|---|
| 46 | // select a color from the pallette |
|---|
| 47 | var palletteSize = incidents.colorpallette.length; |
|---|
| 48 | var currColor = incidents.colorpallette[incidents.size()% palletteSize]; |
|---|
| 49 | //console.log(incidents.size() + " " + incidentNum + " " + currColor); |
|---|
| 50 | // Construct the incident |
|---|
| 51 | var theIncident = new Incident(evtTime, incidentNum, incidentTitle, "", currColor); |
|---|
| 52 | // Add the incident to the list of incidents |
|---|
| 53 | incidents.add(theIncident); |
|---|
| 54 | } |
|---|
| 55 | break; |
|---|
| 56 | case "GENERAL_INFO": |
|---|
| 57 | // Add the summary description to the incident |
|---|
| 58 | var desc = currEvt.childNodes[child].getElementsByTagName("TEXT")[0].textContent; |
|---|
| 59 | incidentToUpdate = incidents.get(incidentNum); |
|---|
| 60 | incidentToUpdate.setSummary(desc); |
|---|
| 61 | // Create an entry showing the Incident start description. Fixes ticket #164 |
|---|
| 62 | var result = new Array(); |
|---|
| 63 | result.push("Description:"); |
|---|
| 64 | result.push(desc); |
|---|
| 65 | proparray.push(new Property("Incident Start",result)); |
|---|
| 66 | break; |
|---|
| 67 | case "CAD_DATA": |
|---|
| 68 | var caddata = parseCAD(currEvt.childNodes[child]); |
|---|
| 69 | if (caddata.length > 0) |
|---|
| 70 | { |
|---|
| 71 | cadProp = new Property("CHP CAD", caddata ); |
|---|
| 72 | proparray.push(cadProp); |
|---|
| 73 | } |
|---|
| 74 | break; |
|---|
| 75 | case "TELEPHONE": |
|---|
| 76 | telProp = new Property("TELEPHONE CONVERSATION", |
|---|
| 77 | parseTelephone(currEvt.childNodes[child]) ); |
|---|
| 78 | proparray.push(telProp); |
|---|
| 79 | break; |
|---|
| 80 | case "CHP_RADIO": |
|---|
| 81 | var chpradio = parseCHPradio(currEvt.childNodes[child]); |
|---|
| 82 | if (chpradio.length > 0) |
|---|
| 83 | { |
|---|
| 84 | cadProp = new Property("CHP RADIO", chpradio ); |
|---|
| 85 | proparray.push(cadProp); |
|---|
| 86 | } |
|---|
| 87 | break; |
|---|
| 88 | |
|---|
| 89 | case "TMT_RADIO": |
|---|
| 90 | case "MAINTENANCE_RADIO": |
|---|
| 91 | var result = new Array(); |
|---|
| 92 | result.push("Details:"); |
|---|
| 93 | result.push(currEvt.childNodes[child].textContent.trim()); |
|---|
| 94 | var radProp = new Property(tagName, result ); |
|---|
| 95 | proparray.push(radProp); |
|---|
| 96 | break; |
|---|
| 97 | |
|---|
| 98 | case "FACILITATOR_EVALUATION": |
|---|
| 99 | case "CAD_EVALUATION": |
|---|
| 100 | case "ATMS_EVALUATION": |
|---|
| 101 | case "ACTIVITY_LOG_EVALUATION": |
|---|
| 102 | case "RADIO_EVALUATION": |
|---|
| 103 | // remove the suffix |
|---|
| 104 | var evalType = tagName.replace("_EVALUATION",""); |
|---|
| 105 | // Build the evaluation item |
|---|
| 106 | var evalItem = new Evaluation(evalType, |
|---|
| 107 | parseEvaluation(currEvt.childNodes[child]) ); |
|---|
| 108 | evalarray.push(evalItem); |
|---|
| 109 | break; |
|---|
| 110 | |
|---|
| 111 | case "CMS_EVALUATION": |
|---|
| 112 | var cmsEval = new Evaluation("CMS", parseCMSEvaluation(currEvt.childNodes[child])); |
|---|
| 113 | evalarray.push(cmsEval); |
|---|
| 114 | break; |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | }//end one event |
|---|
| 118 | // console.log(evtTime.format(), incidentNum, proparray.length, evalarray.length); |
|---|
| 119 | // Ignore Media Log incident and empty nodes |
|---|
| 120 | if (incidentNum != undefined && incidentNum != 100) |
|---|
| 121 | { |
|---|
| 122 | // Create new event with fields obtained from xml file |
|---|
| 123 | events.add(new Event(evtTime, incidents.get(incidentNum), |
|---|
| 124 | new Properties(proparray), |
|---|
| 125 | new Evaluations(evalarray)) ); |
|---|
| 126 | } |
|---|
| 127 | }// end all events |
|---|
| 128 | console.log("Done parsing xml, " + events.length + " events and " +incidents.length + " incidents saved."); |
|---|
| 129 | |
|---|
| 130 | // NOW THAT WE HAVE THE EVENT LIST WE CAN PERFORM SETUP |
|---|
| 131 | setupNotebook(); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | function parseCAD(element) |
|---|
| 135 | { |
|---|
| 136 | var result = new Array(); |
|---|
| 137 | var details = element.getElementsByTagName("DETAIL"); |
|---|
| 138 | if (details.length > 0) |
|---|
| 139 | { |
|---|
| 140 | for (detail in details) |
|---|
| 141 | { |
|---|
| 142 | if (details[detail].textContent != undefined) |
|---|
| 143 | { |
|---|
| 144 | result.push("Detail:"); |
|---|
| 145 | result.push(details[detail].textContent); |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | return result; |
|---|
| 150 | } |
|---|
| 151 | function parseTelephone(element) |
|---|
| 152 | { |
|---|
| 153 | var result = new Array(); |
|---|
| 154 | for (var child = 1; child < element.childNodes.length; child++) |
|---|
| 155 | { |
|---|
| 156 | if (element.childNodes[child].localName != undefined) |
|---|
| 157 | { |
|---|
| 158 | // IF line is INSTRUCTOR THEN display only the Role field |
|---|
| 159 | if (element.childNodes[child].localName == "INSTRUCTOR") |
|---|
| 160 | { |
|---|
| 161 | result.push(element.childNodes[child].attributes["Role"].value); |
|---|
| 162 | } |
|---|
| 163 | else // Display the name (e.g., STUDENT) |
|---|
| 164 | { |
|---|
| 165 | result.push(element.childNodes[child].localName); |
|---|
| 166 | } |
|---|
| 167 | result.push(element.childNodes[child].textContent); |
|---|
| 168 | } |
|---|
| 169 | } |
|---|
| 170 | return result; |
|---|
| 171 | } |
|---|
| 172 | function parseCHPradio(element) |
|---|
| 173 | { |
|---|
| 174 | var result = new Array(); |
|---|
| 175 | var dialog = element.getElementsByTagName("LINE"); |
|---|
| 176 | if (dialog.length > 0) |
|---|
| 177 | { |
|---|
| 178 | for (line in dialog) |
|---|
| 179 | { |
|---|
| 180 | if (dialog[line].textContent != undefined) |
|---|
| 181 | { |
|---|
| 182 | result.push(dialog[line].attributes["Role"].value); |
|---|
| 183 | result.push(dialog[line].textContent); |
|---|
| 184 | } |
|---|
| 185 | } |
|---|
| 186 | } |
|---|
| 187 | return result; |
|---|
| 188 | } |
|---|
| 189 | function parseEvaluation(element) |
|---|
| 190 | { |
|---|
| 191 | var result = new Array(); |
|---|
| 192 | var details = element.getElementsByTagName("EXPECTED_ACTION"); |
|---|
| 193 | if (details.length > 0) |
|---|
| 194 | { |
|---|
| 195 | for (detail in details) |
|---|
| 196 | { |
|---|
| 197 | if (details[detail].textContent != undefined) |
|---|
| 198 | { |
|---|
| 199 | result.push("Expected Action:"); |
|---|
| 200 | result.push(details[detail].textContent.trim()); |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | } |
|---|
| 204 | return result; |
|---|
| 205 | } |
|---|
| 206 | function parseCMSEvaluation(element) |
|---|
| 207 | { |
|---|
| 208 | var result = new Array(); |
|---|
| 209 | var locations = element.getElementsByTagName("LOCATION"); |
|---|
| 210 | if (locations.length > 0) |
|---|
| 211 | { |
|---|
| 212 | result.push("Sign Location:"); |
|---|
| 213 | result.push(locations[0].textContent); |
|---|
| 214 | } |
|---|
| 215 | var details = element.getElementsByTagName("CMS_LINE"); |
|---|
| 216 | if (details.length > 0) |
|---|
| 217 | { |
|---|
| 218 | for (detail in details) |
|---|
| 219 | { |
|---|
| 220 | if (details[detail].textContent != undefined) |
|---|
| 221 | { |
|---|
| 222 | result.push("Sample Message:"); |
|---|
| 223 | result.push(details[detail].textContent.trim()); |
|---|
| 224 | } |
|---|
| 225 | } |
|---|
| 226 | } |
|---|
| 227 | return result; |
|---|
| 228 | } |
|---|
| 229 | // MAIN ENTRY POINT for this application |
|---|
| 230 | function init() |
|---|
| 231 | { |
|---|
| 232 | try { |
|---|
| 233 | // the script must be located where accessible by the web server |
|---|
| 234 | var scriptFilename = "../dynamicdata/incident_script.xml"; |
|---|
| 235 | console.log("LoadEvents.js main Attempting to load ", scriptFilename); |
|---|
| 236 | // Now load the Incident Script and go parse it |
|---|
| 237 | // NB: This is an async function, so all other notebook setup must be in the callback. |
|---|
| 238 | loadJSON(scriptFilename, parseXml) |
|---|
| 239 | |
|---|
| 240 | } catch(e) { |
|---|
| 241 | console.log("Error attempting to parse incident script "+response) |
|---|
| 242 | } |
|---|
| 243 | } |
|---|
| 244 | |
|---|