| 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.common.ScriptException; |
|---|
| 10 | |
|---|
| 11 | /** |
|---|
| 12 | * (Not used) |
|---|
| 13 | * @author Matthew Cechini |
|---|
| 14 | * @version |
|---|
| 15 | */ |
|---|
| 16 | public class CADUserDB { |
|---|
| 17 | |
|---|
| 18 | public static enum PERMISSION_LEVEL { |
|---|
| 19 | TMC ("TMC"), |
|---|
| 20 | CHP ("CHP"), |
|---|
| 21 | UNKNOWN (""); |
|---|
| 22 | |
|---|
| 23 | private String level; |
|---|
| 24 | |
|---|
| 25 | private PERMISSION_LEVEL(String l) { |
|---|
| 26 | level = l; |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | public static PERMISSION_LEVEL fromString(String l) { |
|---|
| 30 | for(PERMISSION_LEVEL pLevel : values()) { |
|---|
| 31 | if(pLevel.level.equals(l)) |
|---|
| 32 | return pLevel; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | return UNKNOWN; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | }; |
|---|
| 39 | |
|---|
| 40 | private static enum DVD_TAGS { |
|---|
| 41 | |
|---|
| 42 | /** Top level tag. */ |
|---|
| 43 | CAD_USERS ("CAD_USERS"), |
|---|
| 44 | /** CAD User info. */ |
|---|
| 45 | USER ("USER"), |
|---|
| 46 | /** User ID. */ |
|---|
| 47 | ID ("ID"), |
|---|
| 48 | /** DVD player address. */ |
|---|
| 49 | PERMISSION ("PERMISSION_LEVEL"); |
|---|
| 50 | |
|---|
| 51 | public String tag; |
|---|
| 52 | |
|---|
| 53 | private DVD_TAGS(String t) { |
|---|
| 54 | tag = t; |
|---|
| 55 | } |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | /** Error Logger. */ |
|---|
| 59 | //private Logger userLogger = Logger.getLogger("tmcsim.cadsimulator.db"); |
|---|
| 60 | |
|---|
| 61 | private TreeMap<String, PERMISSION_LEVEL> userPermissionsMap; |
|---|
| 62 | |
|---|
| 63 | public CADUserDB() { |
|---|
| 64 | userPermissionsMap = new TreeMap<String, PERMISSION_LEVEL>(); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | public PERMISSION_LEVEL getUserPermissionLevel(String userID) { |
|---|
| 68 | //exists?? |
|---|
| 69 | |
|---|
| 70 | return userPermissionsMap.get(userID); |
|---|
| 71 | } |
|---|
| 72 | /** |
|---|
| 73 | * Load the DVD Player map from an xml document, adhering to the |
|---|
| 74 | * following schema: <br> |
|---|
| 75 | * <DVD_PLAYERS> <br> |
|---|
| 76 | * <DVD_PLAYER type="" host="" port=""> |
|---|
| 77 | * <CCTV id="" dir=""/> |
|---|
| 78 | * <INCIDENT log_num="" title="" duration=""/> |
|---|
| 79 | * <RANGE min_speed="" max_speed="" title="" duration=""/> |
|---|
| 80 | ( </DVD_PLAYER> |
|---|
| 81 | *</DVD_PLAYERS> <br> |
|---|
| 82 | * |
|---|
| 83 | * @param newDoc The XML document containing the DVD player |
|---|
| 84 | * registration information. |
|---|
| 85 | * @throws ScriptException if there is an error in parsing the node. |
|---|
| 86 | */ |
|---|
| 87 | public void loadFromXML(Document newDoc) throws ScriptException { |
|---|
| 88 | |
|---|
| 89 | //temporary variables |
|---|
| 90 | Element rootElement = newDoc.getDocumentElement(); |
|---|
| 91 | Element userElement = null; |
|---|
| 92 | Element idElement = null; |
|---|
| 93 | Element permissionElement = null; |
|---|
| 94 | |
|---|
| 95 | String userID = null; |
|---|
| 96 | PERMISSION_LEVEL pLevel = null; |
|---|
| 97 | |
|---|
| 98 | NodeList users = rootElement.getElementsByTagName(DVD_TAGS.USER.tag); |
|---|
| 99 | |
|---|
| 100 | for(int i = 0; i < users.getLength(); i++) { |
|---|
| 101 | |
|---|
| 102 | userElement = (Element)users.item(i); |
|---|
| 103 | |
|---|
| 104 | idElement = (Element)userElement.getFirstChild(); |
|---|
| 105 | userID = idElement.getTextContent(); |
|---|
| 106 | |
|---|
| 107 | permissionElement = (Element)idElement.getNextSibling(); |
|---|
| 108 | pLevel = PERMISSION_LEVEL.fromString(permissionElement.getTextContent()); |
|---|
| 109 | |
|---|
| 110 | if(pLevel != PERMISSION_LEVEL.UNKNOWN) |
|---|
| 111 | userPermissionsMap.put(userID, pLevel); |
|---|
| 112 | |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | } |
|---|
| 116 | } |
|---|