| 1 | package tmcsim.cadsimulator.db; |
|---|
| 2 | |
|---|
| 3 | import java.util.TreeMap; |
|---|
| 4 | |
|---|
| 5 | import org.w3c.dom.Document; |
|---|
| 6 | import org.w3c.dom.Element; |
|---|
| 7 | import org.w3c.dom.NodeList; |
|---|
| 8 | |
|---|
| 9 | import tmcsim.cadmodels.CMSInfo; |
|---|
| 10 | |
|---|
| 11 | /** |
|---|
| 12 | * CMSDiversionDB is a singleton object that acts as a database containing |
|---|
| 13 | * the CMS Diversion information. The initial data is read in from an XML |
|---|
| 14 | * file. This object is accessed to get current CMS diversion information |
|---|
| 15 | * and to be updated with new diversions as they are set by the user. |
|---|
| 16 | * Diversions may be reset to their original values (0) through the |
|---|
| 17 | * resetDiversions() method. |
|---|
| 18 | * |
|---|
| 19 | * @author Matthew Cechini (mcechini@calpoly.edu) |
|---|
| 20 | * @version $Date: 2006/06/14 00:12:38 $ $Revision: 1.4 $ |
|---|
| 21 | */ |
|---|
| 22 | public class CMSDiversionDB { |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * Enumeration containing XML tag names used to parse the XML |
|---|
| 26 | * document containing information about CMS diversion control. |
|---|
| 27 | * @author Matthew Cechini |
|---|
| 28 | */ |
|---|
| 29 | private static enum CMS_DB_TAGS { |
|---|
| 30 | /** This is the top level tag. */ |
|---|
| 31 | CMS ("CMS"), |
|---|
| 32 | /** Individual diversion data. */ |
|---|
| 33 | DIVERSION ("DIVERSION"), |
|---|
| 34 | /** CMS ID. */ |
|---|
| 35 | ID ("ID"), |
|---|
| 36 | /** CMS Postmile. */ |
|---|
| 37 | POSTMILE ("POSTMILE"), |
|---|
| 38 | /** Diversion initial route. */ |
|---|
| 39 | INIT_ROUTE ("INIT_ROUTE"), |
|---|
| 40 | /** Diversion original path. */ |
|---|
| 41 | ORIG_PATH ("ORIG_PATH"), |
|---|
| 42 | /** Diversion new path. */ |
|---|
| 43 | NEW_PATH ("NEW_PATH"), |
|---|
| 44 | /** Diversion path. */ |
|---|
| 45 | DIV_PATH ("DIV_PATH"), |
|---|
| 46 | /** Max diversion percentage. */ |
|---|
| 47 | MAX_DIVERSION ("MAX_DIVERSION"); |
|---|
| 48 | |
|---|
| 49 | /** XML Tag name. */ |
|---|
| 50 | public String tag; |
|---|
| 51 | |
|---|
| 52 | private CMS_DB_TAGS(String t) { |
|---|
| 53 | tag = t; |
|---|
| 54 | } |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | /** The CMSDiversionDB Singleton. */ |
|---|
| 58 | private static CMSDiversionDB theDB; |
|---|
| 59 | |
|---|
| 60 | /** |
|---|
| 61 | * The map of all CMSInfo Objects (value), referenced by a |
|---|
| 62 | * CMS ID String(key). |
|---|
| 63 | */ |
|---|
| 64 | private TreeMap<String, CMSInfo> diversionMap = null; |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | /** Constructor. Initialize the diversion map. */ |
|---|
| 68 | private CMSDiversionDB() { |
|---|
| 69 | diversionMap = new TreeMap<String, CMSInfo>(); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | /** |
|---|
| 73 | * Get the current instance of the CMS Diversion DB. Construct the |
|---|
| 74 | * singleton if it has not yete been constructed. |
|---|
| 75 | */ |
|---|
| 76 | public synchronized static CMSDiversionDB getInstance() { |
|---|
| 77 | |
|---|
| 78 | if(theDB == null) |
|---|
| 79 | theDB = new CMSDiversionDB(); |
|---|
| 80 | |
|---|
| 81 | return theDB; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | /** |
|---|
| 85 | * Load the CMS Diversion map from an xml document, adhering to the |
|---|
| 86 | * following schema: <br/> |
|---|
| 87 | * <CMS><br/> |
|---|
| 88 | * <ID/><br/> |
|---|
| 89 | * <POSTMILE/><br/> |
|---|
| 90 | * <INIT_ROUTE/><br/> |
|---|
| 91 | * <DIVERSION><br/> |
|---|
| 92 | * <ORIG_PATH/><br/> |
|---|
| 93 | * <NEW_PATH/><br/> |
|---|
| 94 | * <DIV_PATH/><br/> |
|---|
| 95 | * <MAX_DIVERSION/><br/> |
|---|
| 96 | * </DIVERSION><br/> |
|---|
| 97 | * |
|---|
| 98 | * <DIVERSION/>...<br/> |
|---|
| 99 | * </CMS> |
|---|
| 100 | */ |
|---|
| 101 | public void loadFromXML(Document newDoc) { |
|---|
| 102 | |
|---|
| 103 | Element rootElement = newDoc.getDocumentElement(); |
|---|
| 104 | Element cmsElement = null; |
|---|
| 105 | Element diversionElement = null; |
|---|
| 106 | NodeList diversions = null; |
|---|
| 107 | |
|---|
| 108 | CMSInfo cmsInfo = null; |
|---|
| 109 | String cmsID = null; |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | NodeList cms_signs = rootElement.getElementsByTagName(CMS_DB_TAGS.CMS.tag); |
|---|
| 113 | |
|---|
| 114 | for(int i = 0; i < cms_signs.getLength(); i++) { |
|---|
| 115 | |
|---|
| 116 | cmsElement = (Element)cms_signs.item(i); |
|---|
| 117 | |
|---|
| 118 | cmsID = cmsElement.getElementsByTagName(CMS_DB_TAGS.ID.tag).item(0) |
|---|
| 119 | .getTextContent(); |
|---|
| 120 | |
|---|
| 121 | cmsInfo = diversionMap.get(cmsID); |
|---|
| 122 | |
|---|
| 123 | if (cmsInfo == null) { |
|---|
| 124 | cmsInfo = new CMSInfo(cmsID, new Float(cmsElement |
|---|
| 125 | .getElementsByTagName(CMS_DB_TAGS.POSTMILE.tag).item(0) |
|---|
| 126 | .getTextContent()), cmsElement.getElementsByTagName( |
|---|
| 127 | CMS_DB_TAGS.INIT_ROUTE.tag).item(0).getTextContent()); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | //parse diversions for CMS |
|---|
| 131 | diversions = cmsElement.getElementsByTagName(CMS_DB_TAGS.DIVERSION.tag); |
|---|
| 132 | |
|---|
| 133 | for(int j = 0; j < diversions.getLength(); j++) { |
|---|
| 134 | |
|---|
| 135 | diversionElement = (Element)diversions.item(j); |
|---|
| 136 | |
|---|
| 137 | cmsInfo.addNewDiversion( |
|---|
| 138 | diversionElement.getElementsByTagName(CMS_DB_TAGS.ORIG_PATH.tag).item(0).getTextContent(), |
|---|
| 139 | diversionElement.getElementsByTagName(CMS_DB_TAGS.NEW_PATH.tag).item(0).getTextContent(), |
|---|
| 140 | diversionElement.getElementsByTagName(CMS_DB_TAGS.DIV_PATH.tag).item(0).getTextContent(), |
|---|
| 141 | new Integer(diversionElement.getElementsByTagName(CMS_DB_TAGS.MAX_DIVERSION.tag).item(0).getTextContent())); |
|---|
| 142 | |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | diversionMap.put(cmsInfo.cmsID, cmsInfo); |
|---|
| 146 | |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | /** |
|---|
| 151 | * Get the current diversion information for the Diversion ID |
|---|
| 152 | * |
|---|
| 153 | * @param ID CMS ID key to get the corresponding CMSInfo object. |
|---|
| 154 | * @returns A CMSInfo object containing diversion info. Returns null if the |
|---|
| 155 | * parameter ID does not exist. |
|---|
| 156 | */ |
|---|
| 157 | public CMSInfo getDiversion(String ID) { |
|---|
| 158 | return diversionMap.get(ID); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | /** |
|---|
| 162 | * Update the current diversions with the new CMSInfo object |
|---|
| 163 | * by putting the parameter CMSInfo object into the local map. |
|---|
| 164 | * |
|---|
| 165 | * @param theDiversion CMSInfo object containing updated diversions. |
|---|
| 166 | */ |
|---|
| 167 | public void updateDiversions(CMSInfo theDiversion) { |
|---|
| 168 | diversionMap.put(theDiversion.cmsID, theDiversion); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | /** |
|---|
| 172 | * Reset all CMSInfo objects. |
|---|
| 173 | */ |
|---|
| 174 | public void resetDiversions() { |
|---|
| 175 | for(CMSInfo cms : diversionMap.values()) { |
|---|
| 176 | cms.reset(); |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | /** |
|---|
| 181 | * Get the CMS Diversion map. |
|---|
| 182 | * |
|---|
| 183 | * @returns The CMSInfo map. |
|---|
| 184 | */ |
|---|
| 185 | public TreeMap<String, CMSInfo> getAllDiversions() { |
|---|
| 186 | return diversionMap; |
|---|
| 187 | } |
|---|
| 188 | } |
|---|