/// <summary> /// Initializes a new instance of the <see cref="ReserveRailForm"/> class. /// </summary> /// <param name="tramDepotManagementSystem">Contains an instance of the TramDepotManagementSystem class</param> /// <param name="rail">Contains an instance of the Rail class</param> public ReserveRailForm(TramDepotManagementSystem tramDepotManagementSystem, Rail rail) { this.InitializeComponent(); this.tramDepotManagementSystem = tramDepotManagementSystem; this.rail = rail; this.rtbxRail.Text = rail.Number.ToString(); }
/// <summary> /// Initializes a new instance of the <see cref="BlockRailForm"/> class. /// </summary> /// <param name="tramDepotManagementSystem">Contains an instance from the TramDepotManagementSystem class.</param> /// <param name="rail">Contains an instance from the Rail class.</param> public BlockRailForm(TramDepotManagementSystem tramDepotManagementSystem, Rail rail) { this.InitializeComponent(); this.tramDepotManagementSystem = tramDepotManagementSystem; this.rail = rail; this.rtbxRail.Text = rail.Number.ToString(); // If all sectors are blocked if (this.rail.Sectors.Count(s => s.IsBlocked) == this.rail.Sectors.Count) { this.BackColor = Color.Red; } }
/// <summary> /// Loads the Rail data in the form. /// </summary> /// <param name="closeConnection">closes the connection after the method is done.</param> private void LoadRailData(bool closeConnection = true) { this.Rails.Clear(); List<Line> lines = new List<Line>(); DataTable data = DatabaseManager.GetTramDepotData(LoggedInAccount.AccountLoggedIn.Depot, closeConnection); foreach (DataRow row in data.Rows) { int railID = Convert.ToInt32(row["SPID"]); Rail rail = this.Rails.Find(r => r.ID == railID); if (rail == null) { int railNumber = Convert.ToInt32(row["SPNUMMER"]); RailType railType; if (!Enum.TryParse(row["SPTYPE"].ToString(), out railType)) { throw new ArgumentException("Rail type could not be parsed as a type"); } // Line int lineID; Line line = null; if (row["LID"] != System.DBNull.Value) { lineID = Convert.ToInt32(row["LID"]); line = lines.Find(l => l.ID == lineID); if (line == null) { string lineNumber = row["LNUMMER"].ToString(); System.Drawing.Color lineColor = System.Drawing.ColorTranslator.FromHtml(row["LKLEUR"].ToString()); line = new Line(lineID, lineNumber, lineColor); } } rail = new Rail(railID, railNumber, railType, line); } // Sector int sectorID = Convert.ToInt32(row["SEID"].ToString()); int sectorNumber = Convert.ToInt32(row["SENUMMER"].ToString()); int sectorBlockedNumber = Convert.ToInt32(row["SEGEBLOKKEERD"].ToString()); bool isBlocked = sectorBlockedNumber == 1; int tramID; Tram tram = null; Sector sector = new Sector(sectorID, sectorNumber, isBlocked, rail, tram); if (row["WID"] != System.DBNull.Value) { tramID = Convert.ToInt32(row["WID"].ToString()); tram = this.Trams.Find(t => t.ID == tramID); sector.Tram = tram; tram.Sector = sector; } rail.AddSector(sector); this.Rails.Add(rail); } }
/// <summary> /// Toggles whether the given rail is blocked or not. /// If one of the sectors that is to be blocked contains a tram, the rail will not be blocked. /// </summary> /// <param name="rail">The rail that is to be (de)blocked.</param> /// <returns>A boolean indicating if one of the sectors to block contains a tram.</returns> public bool ToggleRailIsBlocked(Rail rail) { bool newIsBlocked; if (rail.Sectors.Count(s => s.IsBlocked) == rail.Sectors.Count) { newIsBlocked = false; } else { newIsBlocked = true; } if ((newIsBlocked && rail.Sectors.Find(s => s.Tram != null) == null) || !newIsBlocked) { DatabaseManager.ChangeRailIsBlocked(rail, newIsBlocked); foreach (Sector sector in rail.Sectors) { sector.IsBlocked = newIsBlocked; } return true; } else { return false; } }
/// <summary> /// Gets the first available sector of a rail. If no sector was found, return null. /// </summary> /// <param name="rail">The rail at which a sector must be found.</param> /// <returns>The first available sector of the rail, if no sector was available, return null.</returns> public Sector GetFirstAvailableSectorOfRail(Rail rail) { List<Sector> occupiedSectors = rail.Sectors.Where(s => s.Tram != null || s.IsBlocked).ToList(); Sector lastOccupiedSector = occupiedSectors[occupiedSectors.Count - 1]; int lastOccupiedSectorIndexInRail = rail.Sectors.IndexOf(lastOccupiedSector); if (lastOccupiedSectorIndexInRail < rail.Sectors.Count - 1) { return rail.Sectors[lastOccupiedSectorIndexInRail + 1]; } else { return null; } }
/// <summary> /// Draws the given rail. /// </summary> /// <param name="rail">the rail to draw.</param> /// <param name="startX">The x starting point.</param> /// <param name="y">The y starting point.</param> private void DrawSingleSectorRail(Rail rail, int startX, int y) { int x = startX; RailNumberTextBox rntb = new RailNumberTextBox(rail, new Point(x, y)); rntb.ContextMenu = this.CreateRailContextMenu(); this.gbOverview.Controls.Add(rntb); x += rntb.Size.Width; SectorTextBox stb = new SectorTextBox(rail.Sectors[0], new Point(x, y)); stb.ContextMenu = this.CreateSectorContextMenu(); this.gbOverview.Controls.Add(stb); }
/// <summary> /// Draws the given rail in the group box. /// </summary> /// <param name="rail">The rail to draw.</param> /// <param name="x">The x starting point.</param> /// <param name="startY">The y starting point.</param> private void DrawMultipleSectorRail(Rail rail, int x, int startY) { int y = startY; RailNumberTextBox rntb = new RailNumberTextBox(rail, new Point(x, y)); rntb.ContextMenu = this.CreateRailContextMenu(); this.gbOverview.Controls.Add(rntb); y += rntb.Size.Height; LineNumberTextBox lntb = new LineNumberTextBox(rail.Line, new Point(x, y)); this.gbOverview.Controls.Add(lntb); y += lntb.Size.Height; rail.Sectors.Sort(); foreach (Sector sector in rail.Sectors) { SectorTextBox stb = new SectorTextBox(sector, new Point(x, y)); stb.ContextMenu = this.CreateSectorContextMenu(); this.gbOverview.Controls.Add(stb); y += stb.Size.Height; } }
/// <summary> /// Initializes a new instance of the <see cref="Reservation"/> class. /// Used to reserve at least one sector on the given rail for the given tram. /// </summary> /// <param name="tram">Tram the reservation is for.</param> /// <param name="rail">The rail reserved for the tram.</param> public Reservation(Tram tram, Rail rail) { this.Tram = tram; this.Rail = rail; }
/// <summary> /// Fetches all sectors that are part of the given rail, and returns them. /// </summary> /// <param name="rail">The rail to fetch the sectors for</param> /// <param name="closeConnection">Whether the database connection should be closed after the call. It is recommended not to give a value for this parameter.</param> /// <returns>A list of sectors that are part of the given rail</returns> public static List<Sector> GetSectors(Rail rail, bool closeConnection = true) { try { OracleCommand command = CreateOracleCommand("SELECT ID, nummer, WAGEN_ID, geblokkeerd FROM SECTOR WHERE SPOOR_ID = :RailID"); command.Parameters.Add(":RailID", rail.ID); OracleDataReader reader = ExecuteQuery(command); List<Sector> sectors = new List<Sector>(); while (reader.Read()) { try { int sectorID = Convert.ToInt32(reader["ID"].ToString()); int number = Convert.ToInt32(reader["nummer"].ToString()); int tramID = -1; int.TryParse(reader["WAGEN_ID"].ToString(), out tramID); int blockedInt = Convert.ToInt32(reader["geblokkeerd"].ToString()); bool blocked = blockedInt == 1; Sector sector = new Sector(sectorID, number, blocked, rail, null); Tram tram = GetTram(sector, false); if (tram != null) { sector.Tram = tram; tram.Sector = sector; } sectors.Add(sector); } catch (Exception exc) { Debug.WriteLine(exc.Message); continue; } } return sectors; } finally { if (closeConnection) { connection.Close(); } } }
/// <summary> /// Returns a list with all reservations for trams on lines, for the given tram depot. /// Reservations are used to keep at least on the sector on the rail of the reservation free for the given tram the reservation is made for. /// </summary> /// <param name="tramDepot">The tram depot to fetch all reservations for.</param> /// <param name="closeConnection">If true, closes the database connection at the end of this method.</param> /// <returns>A list with all reservations for trams on lines, for the given tram depot.</returns> public static List<Reservation> GetReservations(TramDepot tramDepot, bool closeConnection = true) { try { OracleCommand command = CreateOracleCommand("SELECT SPOOR_ID, WAGEN_ID FROM RESERVERING WHERE SPOOR_ID IN ( SELECT ID FROM SPOOR WHERE REMISE_ID = :TramDepotID )"); command.Parameters.Add(":TramDepotID", tramDepot.ID); OracleDataReader reader = ExecuteQuery(command); List<Reservation> reservations = new List<Reservation>(); while (reader.Read()) { try { Tram t = new Tram(Convert.ToInt32(reader["WAGEN_ID"].ToString())); Rail r = new Rail(Convert.ToInt32(reader["SPOOR_ID"].ToString())); reservations.Add(new Reservation(t, r)); } catch (Exception exc) { Debug.WriteLine(exc.Message); continue; } } return reservations; } finally { if (closeConnection) { connection.Close(); } } }
/// <summary> /// Changes the isBlocked status for every sector of the given rail to the new value. /// </summary> /// <param name="rail">The rail for which sectors the isBlocked statuses have to be changed.</param> /// <param name="newIsBlocked">Whether the sectors have to be blocked or unblocked.</param> /// <param name="closeConnection">If true, closes the database connection at the end of this method.</param> /// <returns>Amount of updated entries. Should match the amount of sectors on the rail.</returns> public static int ChangeRailIsBlocked(Rail rail, bool newIsBlocked, bool closeConnection = true) { try { int updates = 0; foreach (Sector sector in rail.Sectors) { try { bool success = ChangeSectorIsBlocked(sector, newIsBlocked, false); if (success) { updates++; } } catch (Exception exc) { Debug.WriteLine(exc.Message); continue; } } return updates; } finally { if (closeConnection) { connection.Close(); } } }
/// <summary> /// Returns the rail from the current row of data in the given reader. Given reader must contain column data for $'ID', 'nummer' and 'type'$. /// </summary> /// <param name="reader">The OracleDataReader containing the information to parse (reader.Read() called prior to calling this method).</param> /// <returns>Rail without the sector of the rail.</returns> private static Rail ParseRail(OracleDataReader reader) { int railId = Convert.ToInt32(reader["ID"].ToString()); int number = Convert.ToInt32(reader["nummer"].ToString()); string type = reader["type"].ToString(); RailType railType; if (!Enum.TryParse(type, out railType)) { throw new ArgumentException("Rail type could not be parsed as a RailType"); } Rail rail = new Rail(railId, number, railType, null); Line line = GetLine(rail, false); rail.Line = line; return rail; }
/// <summary> /// Returns the line bound to this rail, or null if no line is bound to the given rail. /// </summary> /// <param name="rail">The rail to fetch the line of.</param> /// <param name="closeConnection">Whether the database connection should be closed at the end of this method.</param> /// <returns>The line bound to the given rail, or null if no line is bound.</returns> private static Line GetLine(Rail rail, bool closeConnection = true) { try { OracleCommand command = CreateOracleCommand("SELECT ID, nummer, kleur FROM LIJN WHERE ID IN ( SELECT LIJN_ID FROM LIJN_SPOOR WHERE SPOOR_ID = :RailId )"); command.Parameters.Add(":RailID", rail.ID); OracleDataReader reader = ExecuteQuery(command); if (!reader.HasRows) { return null; } int id = Convert.ToInt32(reader["ID"].ToString()); string number = reader["nummer"].ToString(); string colorString = reader["kleur"].ToString(); System.Drawing.Color color = System.Drawing.ColorTranslator.FromHtml(colorString); return new Line(id, number, color); } finally { if (closeConnection) { connection.Close(); } } }