source: tmcsimulator/trunk/src/tmcsim/cadsimulator/CADScreenManager.java @ 2

Revision 2, 39.0 KB checked in by jdalbey, 10 years ago (diff)

Initial Import of project files

Line 
1
2package tmcsim.cadsimulator;
3
4
5import java.rmi.RemoteException;
6import java.util.Date;
7import java.util.LinkedList;
8import java.util.Observable;
9import java.util.Observer;
10import java.util.StringTokenizer;
11import java.util.Timer;
12import java.util.TimerTask;
13import java.util.TreeMap;
14import java.util.Vector;
15
16import org.w3c.dom.Element;
17import org.w3c.dom.Node;
18import org.w3c.dom.NodeList;
19
20import tmcsim.cadmodels.BlankScreenModel;
21import tmcsim.cadmodels.CADRoutedMessage;
22import tmcsim.cadmodels.CADScreenModel;
23import tmcsim.cadmodels.IncidentBoardModel;
24import tmcsim.cadmodels.IncidentBoardModel_obj;
25import tmcsim.cadmodels.IncidentInquiryDetails;
26import tmcsim.cadmodels.IncidentInquiryModel;
27import tmcsim.cadmodels.IncidentInquiryModel_obj;
28import tmcsim.cadmodels.IncidentSummaryModel;
29import tmcsim.cadmodels.IncidentSummaryModel_obj;
30import tmcsim.cadmodels.RoutedMessageModel;
31import tmcsim.client.cadclientgui.data.Incident;
32import tmcsim.client.cadclientgui.data.IncidentEvent;
33import tmcsim.common.ObserverMessage;
34import tmcsim.common.CADEnums.CADScreenNum;
35import tmcsim.common.CADEnums.CADScreenType;
36import tmcsim.common.CADEnums.CAD_ERROR;
37import tmcsim.common.CADEnums.CAD_KEYS;
38import tmcsim.common.CADProtocol.CAD_FIELD_CODES;
39import tmcsim.common.CADProtocol.DATA_TAGS;
40
41
42/**
43 * CADScreenManager is used to contain the current information for all four
44 * CAD screens that are available on a CAD Client.  The CADScreenManager
45 * uniquely identifies a CAD Client with a unique CAD Position number and User
46 * ID.  The CADScreenManager object keeps track of the number of routed
47 * messages received and the screen update map.<br>
48 * <br>
49 * The CADScreenManager object observers the Coordinator for any updates to
50 * the simulation data. Whenever there is an event in the simulation, the
51 * CADScreenManager object determines if the update is relevant for this CAD
52 * position and the currently displayed CAD screens.<br>
53 * <br>
54 * CADScreenManager handles all CAD commands, either through the receiveCommand()
55 * method or series of screen request methods.
56 *
57 * A timer is instantiated to update the remote CAD client with the current
58 * time at each minute.
59 *
60 * @author Matthew Cechini
61 * @version $Revision: 1.5 $ $Date: 2006/06/06 20:46:41 $
62 */
63public class CADScreenManager extends Observable implements Observer {
64
65   
66    /**
67     * The CADScreenTimer is a timer task that calls the updateTime()
68     * method everytime it expires.  This timer is used to keep the
69     * CAD time current on the CAD screen.
70     */
71    private class CADScreenTimer extends TimerTask {
72        public void run() {
73            updateTime();
74        }
75    }       
76       
77    /** Reference to the Coordinator object. */
78    private Coordinator theCoordinator;
79   
80    /** The current value of the CAD terminal's position. */
81    private int CADPosition;   
82
83    /** Unique CAD User ID for this set of CAD Screens. */
84    private String CADUserID;
85   
86    /** The current CAD Screen number. */   
87    private CADScreenNum currentCADScreenNum; 
88       
89    /**
90     * Map of the CAD Screens.  The map's values are the CADScreenNum enumeration values,
91     * which reference the current CADScreenModel object values.
92     */
93    private TreeMap<CADScreenNum, CADScreenModel> CADScreensMap;
94   
95    /**
96     * Map of CAD Screen updates.  The map's values are a boolean signifying whether there
97     * is an update available for each CAD screen, keyed by the CADScreenNum objects.
98     */
99    private TreeMap<CADScreenNum, Boolean> CADScreenUpdates;
100       
101    /** Map of CADRoutedMessages that have been received to this CAD terminal, and whether
102     *  the message has been read. */
103    private TreeMap<CADRoutedMessage, Boolean> messageMap = null;
104   
105    /**
106     * Constructor.  Initialize CADScreen windows.  All screens are initialized
107     * to a blank screen.  The CADScreenUpdates map is also initialized with each
108     * cad screen number being set to having a false update value.
109     * The CADScreenTimer is created and scheduled to call the updateTime()
110     * method every minute to increment the current CAD time.
111     */ 
112    public CADScreenManager(Coordinator coor) {
113       
114        theCoordinator = coor;
115       
116        CADPosition = 0;
117        CADUserID   = "A00000";
118        currentCADScreenNum = CADScreenNum.ONE;
119   
120        CADScreensMap    = new TreeMap<CADScreenNum, CADScreenModel>();
121        CADScreenUpdates = new TreeMap<CADScreenNum, Boolean>();
122
123        CADScreenNum screenNum = CADScreenNum.ONE;
124
125        CADScreensMap.put(screenNum, new BlankScreenModel(screenNum));
126        CADScreenUpdates.put(screenNum, false);
127        screenNum = screenNum.next();
128       
129        CADScreensMap.put(screenNum, new BlankScreenModel(screenNum)); 
130        CADScreenUpdates.put(screenNum, false);
131        screenNum = screenNum.next();
132       
133        CADScreensMap.put(screenNum, new BlankScreenModel(screenNum)); 
134        CADScreenUpdates.put(screenNum, false);
135        screenNum = screenNum.next();
136       
137        CADScreensMap.put(screenNum, new BlankScreenModel(screenNum)); 
138        CADScreenUpdates.put(screenNum, false);
139        screenNum = screenNum.next();       
140
141        CADScreenModel.theCADTime = CADSimulator.getCADTime();
142        CADScreenModel.theCADDate = CADSimulator.getCADDate(); 
143           
144        messageMap = new TreeMap<CADRoutedMessage, Boolean>();     
145
146        Date d = new Date();
147        long delay = (60 - ((d.getTime() / 1000) % 60)) * 1000;
148
149        Timer timer             = new Timer(); 
150        CADScreenTimer cadTimer = new CADScreenTimer();   
151        timer.scheduleAtFixedRate(cadTimer, new Date(d.getTime() + delay), (long)1000 * 60);       
152       
153    }
154
155    /**
156     * Called by the timer at the beginning of every minute.  When this method
157     * is called, notify observers with the new time.
158     */ 
159    protected void updateTime() {
160        CADScreenModel.theCADTime = CADSimulator.getCADTime();
161       
162        setChanged();
163        notifyObservers(new ObserverMessage(ObserverMessage.messageType.TIME_UPDATE, 
164                CADSimulator.getCADTime()));
165    }   
166 
167    /**
168     * Returns the current CAD model after updating it with any new screen
169     * updates and messages.
170     *
171     * @return The current CAD model object.
172     */
173    public CADScreenModel getCurrentCADModel() {
174
175        updateCADModel();
176       
177        return currentCADModel();   
178    }
179   
180    /**
181     * Set the CAD position for this terminal. 
182     * @param position The new CAD position.
183     */ 
184    public void setCADPosition(int position) { 
185        CADPosition = position; 
186    }
187   
188    /**
189     * Set the CAD user id for this terminal.
190     * @param userID The new CAD user id.
191     */
192    public void setCADUserID(String userID) {
193        CADUserID = userID;
194    }
195   
196    /**
197     * Updates the current CAD model with the number of routed messages
198     * that have been received by this terminal, and the current
199     * update map.
200     */
201    private void updateCADModel() {
202        currentCADModel().numberRoutedMessages = messageMap.size();
203        currentCADModel().unreadMessages = messageMap.values().contains(new Boolean(false));
204        currentCADModel().screenUpdateMap.putAll(CADScreenUpdates);
205    }
206   
207    /**
208     * A helper method to reduce code.  The CADScreenModel object for the
209     * current CAD screen is returned from the CADScreensMap.
210     * @return The current CADScreenModel object.
211     */
212    private CADScreenModel currentCADModel() {
213       
214        return CADScreensMap.get(currentCADScreenNum);
215    }
216   
217    /**
218     * Returns the log info for this terminal in the following format: XXXYYYYY.
219     * XXX is the cad position, left zero padded. YYYYYY is the CAD user id.   
220     * @return Log info string.
221     */
222    private String getLogInfo() {
223        StringBuffer logInfoBuf = new StringBuffer();
224       
225        while(Integer.toString(CADPosition).length() + logInfoBuf.length() < 3) 
226            logInfoBuf.append("0");
227       
228        logInfoBuf.append(CADPosition);
229        logInfoBuf.append(CADUserID);
230       
231        return logInfoBuf.toString();
232    }
233   
234    /**
235     * This method receives ObserverMessage updates.  The message types that
236     * are responded to are:
237     *
238     * INCIDENT_SUMMARY - Update all CADScreenManager that are showing the
239     *                    SA_INCIDENT_SUMMARY screen.  Add the parameter
240     *                    object to the screen's model object, update the
241     *                    CADScreenUpdates map, update the current model,
242     *                    and notify observers that there is a screen update.
243     *
244     * INCIDENT_INQUIRY - Update all CADScreenManager that are showing the
245     *                    II_INCIDENT_INQUIRY screen with the same log number
246     *                    as the parameter model object.  Add the parameter
247     *                    object to the screen's model object, update the
248     *                    CADScreenUpdates map, update the current model,
249     *                    and notify observers that there is a screen update.
250     *
251     * INCIDENT_BOARD - Update all CADScreenManager that are showing the
252     *                    IB_INCIDENT_BOARD screen.  Add the parameter
253     *                    object to the screen's model object, update the
254     *                    CADScreenUpdates map, update the current model,
255     *                    and notify observers that there is a screen update.
256     *
257     * ROUTED_MESSAGE - Only respond to this update if the routed message
258     *                  has been routed to this CAD terminal posision.
259     *                  Update all CADScreenManager that are showing the
260     *                  TO_ROUTED_MESSAGE screen.  Add the parameter
261     *                  object to the screen's model object, update the current model,
262     *                  and notify observers that there is a new routed message.
263     *
264     * RESET_SIMULATION - Reset the CADScreensMap to contain the BlankScreenModel
265     *                    for all CADScreenManager, reset all screen updates to false,
266     *                    and notify observers to refresh the view.  Update the
267     *                    current CAD model and notify observers of the screen
268     *                    update.  Finally, reset the current screen to screen ONE,
269     *                    and notify observers to refresh.
270     */
271    public void update(Observable o, Object arg) {
272       
273        ObserverMessage oMessage = (ObserverMessage)arg;
274        boolean updatedModel = false;
275       
276        switch(oMessage.type) {
277            case INCIDENT_SUMMARY:
278               
279                for(CADScreenModel model : CADScreensMap.values()) {
280                    if(model.getType() == CADScreenType.SA_INCIDENT_SUMMARY) {                         
281
282                        model.addModelObject(oMessage.value);
283                        updatedModel = true;
284                       
285                        CADScreenUpdates.put(model.getScreenNum(), true);
286                    }
287                }
288       
289                if(updatedModel) {     
290                    updateCADModel();
291                   
292                    setChanged();
293                    notifyObservers(new ObserverMessage(ObserverMessage.messageType.SCREEN_UPDATE, 
294                            CADScreenModel.updateMapToString(currentCADModel().screenUpdateMap))); 
295                }
296           
297                break;
298                                               
299            case INCIDENT_INQUIRY:
300
301                for(CADScreenModel model : CADScreensMap.values()) {
302                    if(model.getType() == CADScreenType.II_INCIDENT_INQUIRY) {                         
303
304                        if(((IncidentInquiryModel)model).logNumMatches(((IncidentInquiryModel_obj)oMessage.value))) {
305                            model.addModelObject(oMessage.value);
306                            updatedModel = true;
307
308                            CADScreenUpdates.put(model.getScreenNum(), true);
309                        }
310                    }
311                }
312               
313                if(updatedModel) {
314                    updateCADModel();
315                   
316                    setChanged();
317                    notifyObservers(new ObserverMessage(ObserverMessage.messageType.SCREEN_UPDATE, 
318                            CADScreenModel.updateMapToString(currentCADModel().screenUpdateMap))); 
319                   
320                   
321                }
322                break;
323               
324            case INCIDENT_BOARD:
325           
326                for(CADScreenModel model : CADScreensMap.values()) {
327                    if(model.getType() == CADScreenType.IB_INCIDENT_BOARD) {                       
328
329                        model.addModelObject(oMessage.value);           
330                        updatedModel = true;
331                    }
332                }
333           
334                if(updatedModel) {
335                    updateCADModel();
336                   
337                    setChanged();
338                    notifyObservers(new ObserverMessage(ObserverMessage.messageType.SCREEN_UPDATE, 
339                            CADScreenModel.updateMapToString(currentCADModel().screenUpdateMap))); 
340                }
341               
342                break;             
343
344            case ROUTED_MESSAGE:
345           
346                if(((CADRoutedMessage)oMessage.value).toPosition == CADPosition) { 
347                    messageMap.put((CADRoutedMessage)oMessage.value, false); 
348                   
349                    for(CADScreenNum screen : CADScreensMap.keySet()) {
350                        if(CADScreensMap.get(screen).getType() == CADScreenType.TO_ROUTED_MESSAGE) {
351                            ((RoutedMessageModel)CADScreensMap.get(screen)).addModelObject(oMessage.value);                                     
352                        }
353                    }
354                   
355                    updateCADModel();
356
357                    setChanged();
358                    notifyObservers(new ObserverMessage(ObserverMessage.messageType.ROUTED_MESSAGE, null));
359
360                }
361               
362                break;
363
364            case RESET_SIMULATION:
365               
366                for(CADScreenNum num : CADScreensMap.keySet()) {
367                    CADScreensMap.put(num, new BlankScreenModel(num)); 
368
369                    currentCADScreenNum = num;
370                    CADScreenUpdates.put(num, false);
371                   
372                    setChanged();
373                    notifyObservers(new ObserverMessage(ObserverMessage.messageType.REFRESH_VIEW, null));
374                }
375
376                updateCADModel();               
377                setChanged();
378                notifyObservers(new ObserverMessage(ObserverMessage.messageType.SCREEN_UPDATE, 
379                        CADScreenModel.updateMapToString(currentCADModel().screenUpdateMap))); 
380
381                //reset back to screen one
382                currentCADScreenNum = CADScreenNum.ONE;
383                setChanged();
384                notifyObservers(new ObserverMessage(ObserverMessage.messageType.REFRESH_VIEW, null));
385                break;
386        }
387    }
388   
389
390    /**
391     * Set the command line in the current CAD model.
392     *
393     * @param cmdLine New command line text.
394     */
395    public void receiveCommandLine(String cmdLine) {
396        getCurrentCADModel().commandLine = cmdLine;
397    }
398 
399   /**
400    * Receive a command from the CAD Client.  This method determines which
401    * command was pushed, and then takes the correct action, determining
402    * if the current CAD Screen needs update.  Each command that is pressed
403    * is referenced by the keycode for the associated key.  These key codes
404    * are defined in the CADProtocol class.  The following table shows the
405    * CAD Command and action.
406    *
407    *<table cellpadding="2" cellspacing="2" border="1"
408    * style="text-align: left; width: 250px;">
409    *  <tbody>
410    *    <tr>
411    *      <th>CAD Protocol Command<br></th>
412    *      <th>Action Taken<br></th>
413    *    </tr>
414    *    <tr>
415    *      <td>CYCLE</td>
416    *      <td>Set the currentCADScreenNum to the next screen number, set the screen update
417    *          value to false, update the current model, and notify observers to refresh the view.
418    *      </td>
419    *    </tr>
420    *    <tr>
421    *      <td>REFRESH</td>
422    *      <td>Set the screen update value to false, update the current model,
423    *          and notify observers to refresh the view.</td>
424    *    </tr>
425    *    <tr>
426    *      <td>NEXT_QUEUE</td>
427    *      <td>If this terminal has not received any messages, do nothing.  If
428    *          messages have been received, and the current CADScreen is
429    *          showing the TO_ROUTED_MESSAGE screen, then call the model's
430    *          nextQueue() method and notify observers to refresh their view. 
431    *          If messages have been received, but the current screen is not
432    *          showing a routed message, then set the current cad screen model
433    *          to the RoutedMessageModel with the received list of messages and
434    *          notify observers.  Update the message map to show that the
435    *          message has been viewd.  Update the unreadMessages flag to
436    *          designate whether unread messages still exist for this CAD
437    *          position.  Notify observers with the updated routed message info.</td>
438    *    </tr>
439    *    <tr>
440    *      <td>DELETE_QUEUE<br></td>
441    *      <td>If this terminal has not received any messages, do nothing. 
442    *          If the current CADScreen is showing a routed message, get the
443    *          current message.  Remove this message from the current message map.
444    *          Also remove the message from all RoutedMessageModels that are
445    *          being shown in a CAD Screen by calling the deleteQueue() method.
446    *          Notify observers to refresh their view.
447    *      </td>
448    *    </tr>
449    *    <tr>
450    *      <td>PREV_QUEUE<br></td>
451    *      <td>If this terminal has not received any messages, do nothing. 
452    *          If messages have been received, and the current CADScreen is
453    *          showing the TO_ROUTED_MESSAGE screen, then call the model's
454    *          prevQueue() method and notify observers to refresh their view. 
455    *          If messages have been received, but the current screen is not
456    *          showing a routed message, then set the current cad screen model
457    *          to the RoutedMessageModel with the received list of messages and
458    *          notify observers.  Update the message map to show that the
459    *          message has been viewd.  Update the unreadMessages flag to
460    *          designate whether unread messages still exist for this CAD
461    *          position.  Notify observers with the updated routed message info.
462    *      </td>
463    *    </tr>
464    *    <tr>
465    *      <td>SCREEN_CLEAR<br></td>
466    *      <td>Set the current model to the BlankScreenModel and notify observers
467    *          to refresh the view.</td>
468    *   </tr>
469    *  </tbody>
470    *</table>
471    *
472    *
473    * @param receivedCommand Integer value of the key that was pressed by client.
474    * @return true if the current CAD Screen needs updating.
475    */
476   public void receiveCommand(CAD_KEYS key) {
477     
478      switch (key) {
479       
480        case CYCLE: 
481           
482            currentCADScreenNum = currentCADScreenNum.next();   
483
484            CADScreenUpdates.put(currentCADScreenNum, false);
485                       
486            updateCADModel();
487           
488            setChanged();
489            notifyObservers(new ObserverMessage(ObserverMessage.messageType.REFRESH_VIEW, null));
490            break;
491           
492        case REFRESH:           
493
494            CADScreenUpdates.put(currentCADScreenNum, false);
495           
496            updateCADModel();
497           
498            setChanged();
499            notifyObservers(new ObserverMessage(ObserverMessage.messageType.REFRESH_VIEW, null));
500            break;
501
502        case  NEXT_QUEUE:
503
504            if(messageMap.size() > 0) {
505                if(currentCADModel().getType() == CADScreenType.TO_ROUTED_MESSAGE) {
506                    ((RoutedMessageModel)currentCADModel()).nextQueue();
507                }
508                else {
509                    LinkedList<CADRoutedMessage> messages = new LinkedList<CADRoutedMessage>();
510                    messages.addAll(messageMap.keySet());
511                   
512                    CADScreensMap.put(currentCADScreenNum, new RoutedMessageModel(
513                            currentCADScreenNum, messages));                   
514                }
515               
516                setChanged();
517                notifyObservers(new ObserverMessage(ObserverMessage.messageType.REFRESH_VIEW, null));
518
519               
520                messageMap.put(((RoutedMessageModel)currentCADModel()).getCurrentMessage(), true);
521                currentCADModel().unreadMessages = messageMap.values().contains(new Boolean(false));
522
523                setChanged();
524                notifyObservers(new ObserverMessage(ObserverMessage.messageType.ROUTED_MESSAGE, null));
525            }
526
527            break;
528
529        case  DELETE_QUEUE: 
530
531            if(messageMap.size() > 0) {
532
533                if(currentCADModel().getType() == CADScreenType.TO_ROUTED_MESSAGE) {
534                   
535                    CADRoutedMessage delMsg = ((RoutedMessageModel)currentCADModel()).getCurrentMessage();
536                   
537                    messageMap.remove(delMsg);
538                   
539                    for(CADScreenNum screen : CADScreensMap.keySet()) {
540                        if(CADScreensMap.get(screen).getType() == CADScreenType.TO_ROUTED_MESSAGE) {
541                            ((RoutedMessageModel)CADScreensMap.get(screen)).deleteQueue(delMsg);
542                        }
543                    }                       
544
545                    setChanged();
546                    notifyObservers(new ObserverMessage(ObserverMessage.messageType.REFRESH_VIEW, null)); 
547                }
548            }
549            break;
550
551 
552        case  PREV_QUEUE:                         
553            if(messageMap.size() > 0) {
554                if(currentCADModel().getType() == CADScreenType.TO_ROUTED_MESSAGE) {
555                    ((RoutedMessageModel)currentCADModel()).prevQueue();
556                }
557                else {
558                    LinkedList<CADRoutedMessage> messages = new LinkedList<CADRoutedMessage>();
559                    messages.addAll(messageMap.keySet());
560                   
561                    CADScreensMap.put(currentCADScreenNum, new RoutedMessageModel(
562                            currentCADScreenNum, messages));       
563
564                }
565               
566                setChanged();
567                notifyObservers(new ObserverMessage(ObserverMessage.messageType.REFRESH_VIEW, null));
568
569
570                messageMap.put(((RoutedMessageModel)currentCADModel()).getCurrentMessage(), true);
571                currentCADModel().unreadMessages = messageMap.values().contains(new Boolean(false));
572               
573                setChanged();
574                notifyObservers(new ObserverMessage(ObserverMessage.messageType.ROUTED_MESSAGE, null));
575            }
576            break;
577
578        case  SCREEN_CLEAR: 
579            CADScreensMap.put(currentCADScreenNum, new BlankScreenModel(currentCADScreenNum));   
580           
581            setChanged();
582            notifyObservers(new ObserverMessage(ObserverMessage.messageType.REFRESH_VIEW, null));
583            break;
584        }
585
586    }   
587   
588    /**
589     * Handles a request for the current screen to show the IB_INCIDENT_BOARD screen. 
590     * Create a new IncidentBoardModel object and set its data with the Incident Board
591     * model data from the Coordinator.  Put the new model object into the CADScreensMap
592     * and notify observers to refresh the view.
593     *
594     * There are no additional tokens that are read from the parameter node
595     * when this command is received.
596     *
597     * @param root (Not used)
598     */
599    public void incidentBoardRequest(Element root) {
600
601        IncidentBoardModel tempIB = new IncidentBoardModel(currentCADScreenNum);                       
602                   
603        //update with new information                   
604        for(IncidentBoardModel_obj ibmo : theCoordinator.getIncidentBoardModelObjects()) {
605            tempIB.addModelObject(ibmo);
606        }
607           
608        CADScreensMap.put(currentCADScreenNum, tempIB); 
609        CADScreenUpdates.put(currentCADScreenNum, false);
610           
611        setChanged();
612        notifyObservers(new ObserverMessage(ObserverMessage.messageType.REFRESH_VIEW, null));
613
614    }
615   
616    /**
617     * Handles a request to update an existing incident.  The parameter XML node is parsed
618     * for the incident update data.  The following is an example of the received XML element.
619     *
620     * <UPDATE_INCIDENT LOG_NUM="">
621     *    <DETAILS/>
622     * </UPDATE_INCIDENT>
623     *
624     * If the LOG_NUM attribute contains a value, then the user is attempting to update
625     * an incident that may or may not be currently viewed.  If this value does not
626     * match any current incidents, notify the observers with an INVALID_LOG error message.<br>
627     *
628     * If the LOG_NUM attribute is empty, then the user is attempting to modify the incident
629     * on the current screen.  If the current screen is not an II_INCIDENT_INQUIRY, then
630     * notify the observers with A NO_LOG_NUMBER error message.
631     *
632     * If the UPDATE_INCIDENT node contains <DETAILS> elements, parse the text content for
633     * each of these detail elements, creating IncidentInquiryDetails objects and adding
634     * them to a IncidentInquiryModel_obj, which is then sent to the Coordinator to update
635     * the incident.
636     *
637     * If the ROOT node does not contain any <DETAILS> elements, then notify observers
638     * with an UNAUTH_CMD error message.
639     *
640     * @param root XML document element containing incident update request data.
641     */ 
642    public void incidentUpdateRequest(Element root) {
643
644        /* UI.  or UI.###
645         * If it's a UI. check if we're in an II screen, if not error
646         * If UI.### add it to the log... can we check against what current log we're looking at???
647         * if so, we need to update it....
648         */             
649               
650        //ascertain if the next token is a number, if so, we're updating a specific log, else
651        //updating the current log(assuming we're looking at one)
652       
653        IncidentInquiryModel_obj newIIobj = new IncidentInquiryModel_obj(CADPosition);
654           
655        String  parsedLogNumber = null;
656        Integer logNumber       = null;
657
658        NodeList detailList  = null;       
659       
660        parsedLogNumber = root.getAttribute(DATA_TAGS.LOG_NUM.tag);     
661       
662        if(parsedLogNumber.length() > 0) { //UI.### Format
663       
664            logNumber = Integer.parseInt(parsedLogNumber);
665           
666            //if doesn't exist, send INVALID_LOG error
667            if(!theCoordinator.incidentExists(logNumber)) {
668                setChanged();
669                notifyObservers(new ObserverMessage(ObserverMessage.messageType.CAD_INFO_MESSAGE,
670                                                    CAD_ERROR.INVALID_LOG.message));   
671                                                   
672                return;     
673                                                   
674            }
675
676        }
677        else {  //UI. format
678           
679            //not looking at II, error
680            if(currentCADModel().getType() != CADScreenType.II_INCIDENT_INQUIRY) {                 
681                setChanged();
682                notifyObservers(new ObserverMessage(ObserverMessage.messageType.CAD_INFO_MESSAGE,
683                                                        CAD_ERROR.NO_LOG_NUM.message));
684                return;
685            }
686            //looking at II, continue to update
687            else { 
688                logNumber = ((IncidentInquiryModel)currentCADModel()).getLogNumber();                               
689            }                       
690        }
691       
692        //TODO more than just details...
693       
694        detailList = root.getElementsByTagName(CAD_FIELD_CODES.DETAILS.fullName);       
695        if(detailList.getLength() > 0) {
696            for(int i = 0; i < detailList.getLength(); i++) {
697                Node detailNode = detailList.item(i);           
698                newIIobj.getDetails().add(new IncidentInquiryDetails(
699                        getLogInfo(), detailNode.getTextContent(), 
700                        Boolean.parseBoolean(detailNode.getAttributes().getNamedItem(
701                                DATA_TAGS.SENSITIVE.tag).getNodeValue())));
702            }
703           
704            newIIobj.setLogNumber(logNumber);           
705           
706            theCoordinator.commandLineUpdate(newIIobj);
707        }   
708        else {
709            setChanged();
710            notifyObservers(new ObserverMessage(ObserverMessage.messageType.CAD_INFO_MESSAGE,
711                                                CAD_ERROR.UNAUTH_CMD.message)); 
712            return;
713        }
714       
715    }
716   
717    /**
718     * Handles a request for the current screen to show the II_INCIDENT_INQUIRY screen. 
719     * The parameter XML node is parsed for the incident inquiry data.  The following
720     * is an example of the received XML element.  <br>
721     *
722     * <INCIDENT_INQUIRY>###</INCIDENT_INQUIRY> <br>
723     *
724     * Parse the text content of the INCIDENT_INQUIRY node for the incident number that
725     * is being inquired for.  If this incident does not exist, notify observers with an
726     * INVALID_LOG error message.  If the incident does exist, create a new
727     * IncidentInquiryModel object and set its data with the Incident Inquiry
728     * model data from the Coordinator.  Put the new model object into the CADScreensMap
729     * and notify observers to refresh the view.
730     *
731     * @param root XML document element containing incident inquiry request data.
732     */ 
733    public void incidentInquiryRequest(Element root) {
734
735        Integer incidentNum = Integer.parseInt(root.getTextContent());
736                       
737        if(theCoordinator.incidentExists(incidentNum)) {
738               
739            IncidentInquiryModel tempII = new IncidentInquiryModel(currentCADScreenNum, incidentNum);
740           
741            for(IncidentInquiryModel_obj iimo : 
742                theCoordinator.getIncidentInquiryModelObjects(incidentNum))  {                         
743                tempII.addModelObject(iimo);
744            }
745                           
746            CADScreensMap.put(currentCADScreenNum, tempII);   
747            CADScreenUpdates.put(currentCADScreenNum, false);
748               
749            setChanged();
750            notifyObservers(new ObserverMessage(ObserverMessage.messageType.REFRESH_VIEW, null));             
751
752        }
753        else {
754            setChanged();
755            notifyObservers(new ObserverMessage(ObserverMessage.messageType.CAD_INFO_MESSAGE,
756                                                CAD_ERROR.INVALID_LOG.message));
757        }
758    }
759   
760    /**
761     * Handles a request for the current screen to show the SA_SUMMARY_BOARD screen.
762     * Create a new IncidentSummaryModel object and set its data with the Incident Summary
763     * model data from the Coordinator.  Put the new model object into the CADScreensMap
764     * and notify observers to refresh the view.
765     *
766     * @param root (not used)
767     */ 
768    public void incidentSummaryRequest(Element root) {
769       
770        IncidentSummaryModel tempSA = new IncidentSummaryModel(currentCADScreenNum);
771               
772        for(IncidentSummaryModel_obj ismo : 
773            theCoordinator.getIncidentSummaryModelObjects()) 
774                tempSA.addModelObject(ismo);   
775               
776        CADScreensMap.put(currentCADScreenNum, tempSA); 
777        CADScreenUpdates.put(currentCADScreenNum, false);
778       
779        setChanged();
780        notifyObservers(new ObserverMessage(ObserverMessage.messageType.REFRESH_VIEW, null));                   
781
782    }
783   
784    /**
785     * (EI) - Enter Incident<br>
786     *
787     * TODO  work on this.
788     */
789    public void enterIncidentRequest(Element root) {       
790
791        try {
792            Incident newIncident   = null;
793            IncidentEvent newEvent = null;;
794           
795            Long currentSimTime     = 0L;
796            Integer logNumber       = new Integer(0);
797           
798            NodeList detailList  = null;                   
799   
800            newEvent = new IncidentEvent(0);       
801   
802            detailList = root.getElementsByTagName(CAD_FIELD_CODES.DETAILS.fullName);       
803            if(detailList.getLength() > 0) {
804                for(int i = 0; i < detailList.getLength(); i++) {
805                    Node detailNode = detailList.item(i);           
806                    newEvent.eventInfo.getDetails().add(new IncidentInquiryDetails(getLogInfo(), 
807                            detailNode.getTextContent(),
808                            Boolean.parseBoolean(detailNode.getAttributes().getNamedItem(
809                                    DATA_TAGS.SENSITIVE.tag).getNodeValue())));
810                }               
811            }   
812            else {
813                setChanged();
814                notifyObservers(new ObserverMessage(ObserverMessage.messageType.CAD_INFO_MESSAGE,
815                                                    CAD_ERROR.UNAUTH_CMD.message)); 
816                return;
817            }           
818           
819            for(Incident incident : theCoordinator.getIncidentList()) {
820                if(incident.getLogNumber() > logNumber)
821                    logNumber = incident.getLogNumber(); 
822            }
823            logNumber++;
824
825            currentSimTime = theCoordinator.getCurrentSimulationTime();
826   
827            newIncident = new Incident(logNumber, "", currentSimTime);
828            newIncident.addEvent(newEvent);
829           
830            //theCoordinator.addIncident(newIncident);     
831           
832        }
833        catch (RemoteException re) { /*we're not remote*/ }
834
835                   
836        setChanged();
837        notifyObservers(new ObserverMessage(ObserverMessage.messageType.REFRESH_VIEW, null));
838
839    }
840
841    /**
842     * Handles a request to send a routed message.  The parameter XML node is
843     * parsed for the routed message data. The following is an example of
844     * the received XML element.
845     *
846     * <ROUTED_MESSAGE>
847     *    <DESTINATION/>
848     *    <MESSAGE/>
849     * </ROUTED_MESSAGE>
850     *
851     * Parse the DESTINATION element for the CAD Position to receive the message.
852     * If a DESTINATION element does not exist, then notify observers with an UNAUTH_CMD
853     * error message.
854     *
855     * Parse the MESSAGE element for the message text.  If the MESSAGE element
856     * does not exist, then notify observers with an UNAUTH_CMD error message.
857     *
858     * The parsed destination may be a comma-delimited string of CAD positions.
859     * Tokenize through the parsed string for all destinations and route a message
860     * to each of them with a new RoutedMessage object sent to the Coordinator.
861     *
862     * Notify observers to refresh their current view.  If the current screen is an
863     * II_INCIDENT_INQUIRY, then a detail is to be added to the log.  Create
864     * an IncidentInquiryModel_obj and send it to the Coordinator as a command
865     * line update.
866     *
867     * @param root XML document element containing incident update request data.
868     */ 
869    public void routedMessageRequest(Element root) { 
870
871       
872        String parsed_dest = "";
873        String message     = "";
874       
875        StringTokenizer destTok = null;
876        Vector<Integer> destinations = new Vector<Integer>();
877        boolean messageSent = false;
878       
879               
880        NodeList destList = root.getElementsByTagName(DATA_TAGS.DESTINATION.tag);
881       
882        if(destList.getLength() > 0) {
883            Node destNode = destList.item(0);               
884            parsed_dest   = destNode.getTextContent();
885        }   
886        else {
887            setChanged();
888            notifyObservers(new ObserverMessage(ObserverMessage.messageType.CAD_INFO_MESSAGE,
889                                                CAD_ERROR.UNAUTH_CMD.message));
890            return;
891        }           
892       
893
894        NodeList messageList = root.getElementsByTagName(DATA_TAGS.MESSAGE.tag);
895       
896        if(messageList.getLength() > 0) {
897            Node messageNode = messageList.item(0);             
898            message = messageNode.getTextContent();         
899        }   
900        else {
901            setChanged();
902            notifyObservers(new ObserverMessage(ObserverMessage.messageType.CAD_INFO_MESSAGE,
903                                                CAD_ERROR.UNAUTH_CMD.message));
904            return;
905        }                   
906       
907        destTok = new StringTokenizer(parsed_dest, ",");
908       
909        while(destTok.hasMoreTokens()) {
910           
911            try {
912                destinations.add(Integer.parseInt(destTok.nextToken()));
913            }
914            catch (NumberFormatException nfe) {
915                setChanged();
916                notifyObservers(new ObserverMessage(ObserverMessage.messageType.CAD_INFO_MESSAGE,
917                            CAD_ERROR.UNAUTH_CMD.message)); 
918                return;
919            }
920        }
921       
922        for(int dest : destinations) {
923           
924            if(CADPosition != dest) {
925
926                CADRoutedMessage newMessage = new CADRoutedMessage(CADPosition,
927                           dest, 
928                           message, 
929                           false);
930               
931                theCoordinator.routeMessage(newMessage);       
932               
933                messageSent = true;
934
935            }
936        }
937        setChanged();
938        notifyObservers(new ObserverMessage(ObserverMessage.messageType.REFRESH_VIEW, null));
939       
940
941        setChanged();
942        notifyObservers(new ObserverMessage(ObserverMessage.messageType.CAD_INFO_MESSAGE,
943                "0146: Routed message to " + parsed_dest + "."));
944       
945       
946        //If we sent a message and are looking at a CAD log, add a DETAIL as well
947        if(messageSent && getCurrentCADModel().getType() == CADScreenType.II_INCIDENT_INQUIRY) {
948            IncidentInquiryModel_obj newIIobj = new IncidentInquiryModel_obj(CADPosition);
949                       
950            newIIobj.getDetails().add(new IncidentInquiryDetails(getLogInfo(), message, true));             
951            newIIobj.setLogNumber(((IncidentInquiryModel)getCurrentCADModel()).getLogNumber());
952               
953            theCoordinator.commandLineUpdate(newIIobj);         
954        }
955
956    }
957   
958    /**
959     * Handles a request to log off the terminal.  All screens are reset with
960     * a BlankScreenModel.  The current screen number is set to ONE.  All
961     * screen updates and messages are cleared.  The model is then updated
962     * and Observers are notified ot refresh the view.
963     *
964     * @param root (not used)
965     */
966    public void terminalOffRequest() {
967
968        for(CADScreenNum num : CADScreensMap.keySet()) {
969            CADScreensMap.put(num, new BlankScreenModel(num));
970        }
971           
972        currentCADScreenNum = CADScreenNum.ONE;
973       
974        CADScreenUpdates.clear();
975       
976        messageMap.clear();
977       
978        updateCADModel();
979       
980        setChanged();
981        notifyObservers(new ObserverMessage(ObserverMessage.messageType.REFRESH_VIEW, null));
982       
983    }
984   
985}
Note: See TracBrowser for help on using the repository browser.