Changeset 465 in tmcsimulator for trunk/src/tmcsim


Ignore:
Timestamp:
07/26/2019 05:12:05 AM (7 years ago)
Author:
jdalbey
Message:

Coordinator.java: Update writeToCAD to sort comments by timestamp instead of incident number. Also add a unit test for this. Fixes #175.

Location:
trunk/src/tmcsim
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/tmcsim/application.properties

    r463 r465  
    1 #Tue, 23 Jul 2019 14:36:26 -0700 
     1#Fri, 26 Jul 2019 06:34:33 -0700 
    22 
    3 Application.revision=462 
     3Application.revision=464 
    44 
    55Application.buildnumber=170 
  • trunk/src/tmcsim/cadsimulator/Coordinator.java

    r448 r465  
    88import java.text.DateFormat; 
    99import java.text.SimpleDateFormat; 
     10import java.util.ArrayList; 
     11import java.util.Collections; 
    1012import java.util.Date; 
    1113import java.util.LinkedList; 
     14import java.util.List; 
    1215import java.util.Observer; 
    1316import java.util.TreeMap; 
     
    744747            Logger.getLogger(Coordinator.class.getName()).log(Level.SEVERE, null, ex); 
    745748        } 
    746         // Initialize an output buffer 
    747         StringBuffer output = new StringBuffer(); 
     749        // Write output list to a file 
     750        try  
     751        { 
     752            PrintWriter writer = new PrintWriter(new FileWriter(kCADcommentLog)); 
     753            // process the list of comments obtained from helper method 
     754            for (String item: getSortedComments(incList)) 
     755            { 
     756                writer.print(item.substring(9)); 
     757            } 
     758            writer.close(); 
     759        } catch (Exception exc)  
     760        { 
     761            exc.printStackTrace(); 
     762        }  
     763    } 
     764     
     765    /** Extract a list of comments from the current incidents, ordered 
     766     * by timestamp.  Fixes defect #175. 
     767     * Package private to allow unit testing. 
     768     *  
     769     * @param incidentList the incidents from which comments are desired 
     770     * @return list of comments, ready to be output. 
     771     */ 
     772    List<String> getSortedComments(Vector<Incident> incidentList) 
     773    { 
     774        // Initialize a list of for outputing CAD comments 
     775        ArrayList<String> commentList = new ArrayList<String>(); 
    748776        // Process all the incidents 
    749         for (Incident incident: incList) 
     777        for (Incident incident: incidentList) 
    750778        { 
    751779            // Retrieve the table of comments/notes the users created 
     
    755783            { 
    756784                // Combine the fields into one output entry 
     785                String timestamp = (String)notesTable.getValueAt(row,1); 
    757786                String initials = (String) notesTable.getValueAt(row,2); // user initials 
    758787                // If there are user intials, include this item. 
    759788                // Zero length initials mean it's a scripted item, ignore it. 
     789                StringBuffer output = new StringBuffer(); 
    760790                if (initials.length() > 0) 
    761                 {//CAD Log Entry, Incident #187, Sharon: REQUEST EXTRA ANCHOVIES 
    762                     output.append("CAD log, "); 
     791                {//17:03:19 CAD Log Entry, Incident #187, Sharon: REQUEST EXTRA ANCHOVIES 
     792                    output.append(timestamp); 
     793                    output.append(" CAD log, "); 
    763794                    output.append("Incident #" + incident.logNum + ", "); 
    764795                    output.append(initials + ": "); 
    765796                    output.append(notesTable.getValueAt(row,4) + "\n"); // notes 
     797                    commentList.add(output.toString()); // Add this comment to the list 
    766798                } 
    767799            } 
    768800        } 
    769         // Write output buffer to a file 
    770         try  
    771         { 
    772             PrintWriter writer = new PrintWriter(new FileWriter(kCADcommentLog)); 
    773             writer.print(output); 
    774             writer.close(); 
    775         } catch (Exception exc)  
    776         { 
    777             exc.printStackTrace(); 
    778         }  
    779     } 
    780      
     801        // Order the comments by timestamp instead of incident number. 
     802        Collections.sort(commentList, new java.util.Comparator<String>()  
     803        { 
     804            // provide a comparator that can compare number strings 
     805            public int compare(String o1, String o2)  
     806            { 
     807                return extractInt(o1) - extractInt(o2); 
     808            } 
     809            // extract an integer from a String     
     810            int extractInt(String s)  
     811            {   // remove all non-digits 
     812                String num = s.replaceAll("\\D", ""); 
     813                // return 0 if no digits found, else return the number 
     814                return num.isEmpty() ? 0 : Integer.parseInt(num); 
     815            } 
     816        }); 
     817        return commentList; 
     818    } 
    781819    /** Write the currentSimTime to a file.  
    782820       This will be read asynchronously by web clients, e.g., 
Note: See TracChangeset for help on using the changeset viewer.