public void AddLineStation(DO.LineStation lineStation) { var lineStationList = XmlTools.LoadListFromXMLSerializer <DO.LineStation>(lineStationsPath); var ls = lineStationList.FirstOrDefault(s => s.Station == lineStation.Station && s.LineId == lineStation.LineId); if (ls != null && !ls.IsDeleted) { throw new DO.BadLineStationException(lineStation.Station, lineStation.LineId, "Duplicate line station"); } var line = GetLine(lineStation.LineId); foreach (var s in lineStationList) { if (s.LineId == line.Id) { if (lineStation.PrevStation == s.Station) { lineStation.LineStationIndex = s.LineStationIndex + 1; continue; } if (lineStation.LineStationIndex != -1) { s.LineStationIndex += 1; UpdateLineStation(s); } } } lineStationList.Add(lineStation); XmlTools.SaveListToXMLSerializer(lineStationList, lineStationsPath); }
public DO.LineStation LineStationBoDoAdapter(BO.LineStation LineStationBO) { DO.LineStation LineStationDO = new DO.LineStation(); LineStationBO.CopyPropertiesTo(LineStationDO); LineStationDO.StationCode = LineStationBO.Station.Code; return(LineStationDO); }
/// <summary> /// Add new line station /// </summary> /// <param name="lineStation"></param> public void AddLineStation(DO.LineStation lineStation) { var ls = DS.DataSource.LineStationsList.FirstOrDefault(s => s.Station == lineStation.Station && s.LineId == lineStation.LineId); if (ls != null) { if (!lineStation.IsDeleted) { throw new DO.BadLineStationException(lineStation.Station, lineStation.LineId, "Duplicate line station"); } } #region Check if there is needed data on the database var station = DS.DataSource.StationsList.Find(s => s.Code == lineStation.Station); if (station == null) { throw new DO.BadLineStationException(lineStation.Station, lineStation.LineId, $"Station '{lineStation.Station}' does not exist"); } if (station.IsDeleted) { throw new DO.BadLineStationException(lineStation.Station, lineStation.LineId, $"Station '{lineStation.Station}' does not exist"); } var line = DS.DataSource.LinesList.Find(l => l.Id == lineStation.LineId); if (line == null) { throw new DO.BadLineStationException(lineStation.Station, lineStation.LineId, $"Line '{lineStation.LineId}' does not exist"); } if (line.IsDeleted) { throw new DO.BadLineStationException(lineStation.Station, lineStation.LineId, $"Line '{lineStation.LineId}' does not exist"); } #endregion // Updates the station number on the line of all line stations of this line foreach (var s in GetAllLineStationBy(s => s.LineId == line.Id)) { if (lineStation.PrevStation == s.Station) { lineStation.LineStationIndex = s.LineStationIndex + 1; continue; } if (lineStation.LineStationIndex != -1) { s.LineStationIndex += 1; UpdateLineStation(s); } } DS.DataSource.LineStationsList.Add(lineStation.Clone()); }
public BO.LineStation GetLineStation(int lineId, int stationId) { DO.LineStation ls = dl.GetLineStation(lineId, stationId); if (ls != null) { return(LineStationDoBoAdapter(dl.GetLineStation(lineId, stationId))); } else { return(null); } }
/// <summary> /// In deleting a line we want to delete all the stations /// As opposed to a specific deletion of a station /// Therefore we will not set conditions and we will not change other /// stations on the line because everyone will delete /// </summary> /// <param name="lineStation"></param> public void RemoveLineStationOnRemoveline(DO.LineStation lineStation) { var ls = DS.DataSource.LineStationsList.Find(s => s.Station == lineStation.Station && s.LineId == lineStation.LineId); if (ls != null && !ls.IsDeleted) { DS.DataSource.LineStationsList.Remove(ls); ls.IsDeleted = true; DS.DataSource.LineStationsList.Add(ls); } }
public void RemoveLineStationOnRemoveline(DO.LineStation lineStation) { var lineStationList = XmlTools.LoadListFromXMLSerializer <DO.LineStation>(lineStationsPath); var ls = lineStationList.Find(s => s.Station == lineStation.Station && s.LineId == lineStation.LineId); if (ls != null && !ls.IsDeleted) { lineStationList.Remove(ls); ls.IsDeleted = true; lineStationList.Add(ls); XmlTools.SaveListToXMLSerializer(lineStationList, lineStationsPath); } }
// Update prev and next stations on remove void updatePrevAndNextStationOnRemove(DO.LineStation lineStation) { if (lineStation.NextStation == -1 && lineStation.PrevStation == 0) { return; } foreach (var ls in GetAllLineStationBy(s => s.LineId == lineStation.LineId && s.LineStationIndex > lineStation.LineStationIndex)) { ls.LineStationIndex -= 1; UpdateLineStation(ls); } if (lineStation.PrevStation != 0 && lineStation.NextStation != -1) { var nextAdja = GetAdjacentStations(lineStation.Station, lineStation.NextStation); var prevAdja = GetAdjacentStations(lineStation.PrevStation, lineStation.Station); AddAdjacentStations(new DO.AdjacentStations { Distance = nextAdja.Distance + nextAdja.Distance, IsDeleted = false, Station1 = prevAdja.Station1, Station2 = nextAdja.Station2, TimeInHours = prevAdja.TimeInHours + nextAdja.TimeInHours, TimeInMinutes = prevAdja.TimeInMinutes + nextAdja.TimeInMinutes }); var prev = GetLineStation(lineStation.PrevStation, lineStation.LineId); var next = GetLineStation(lineStation.NextStation, lineStation.LineId); prev.NextStation = lineStation.NextStation; next.PrevStation = lineStation.PrevStation; UpdateLineStation(prev); UpdateLineStation(next); return; } if (lineStation.PrevStation == 0) { var next = GetLineStation(lineStation.NextStation, lineStation.LineId); next.PrevStation = 0; UpdateLineStation(next); return; } if (lineStation.NextStation == -1) { var prev = GetLineStation(lineStation.PrevStation, lineStation.LineId); prev.NextStation = -1; UpdateLineStation(prev); return; } }
void updatePrevAndNextStationOnRemove(DO.LineStation lineStation, List <DO.LineStation> lineStationList) { if (lineStation.NextStation == -1 && lineStation.PrevStation == 0) { return; } foreach (var ls in lineStationList.FindAll(s => s.LineId == lineStation.LineId && s.LineStationIndex > lineStation.LineStationIndex && !s.IsDeleted).OrderBy(s => s.LineStationIndex)) { ls.LineStationIndex -= 1; } if (lineStation.PrevStation != 0 && lineStation.NextStation != -1) { var nextAdja = GetAdjacentStations(lineStation.Station, lineStation.NextStation); var prevAdja = GetAdjacentStations(lineStation.PrevStation, lineStation.Station); AddAdjacentStations(new DO.AdjacentStations { Distance = nextAdja.Distance + nextAdja.Distance, IsDeleted = false, Station1 = lineStation.PrevStation, Station2 = lineStation.NextStation, TimeInHours = prevAdja.TimeInHours + nextAdja.TimeInHours, TimeInMinutes = prevAdja.TimeInMinutes + nextAdja.TimeInMinutes }); var prev = lineStationList.Find(s => s.Station == lineStation.PrevStation && s.LineId == lineStation.LineId); var next = lineStationList.Find(s => s.Station == lineStation.NextStation && s.LineId == lineStation.LineId); prev.NextStation = lineStation.NextStation; next.PrevStation = lineStation.PrevStation; return; } if (lineStation.PrevStation == 0) { var next = lineStationList.Find(s => s.Station == lineStation.NextStation && s.LineId == lineStation.LineId); next.PrevStation = 0; return; } if (lineStation.NextStation == -1) { var prev = lineStationList.Find(s => s.Station == lineStation.PrevStation && s.LineId == lineStation.LineId); prev.NextStation = -1; return; } }
/// <summary> /// Upadte a line station /// </summary> /// <param name="lineStation"></param> public void UpdateLineStation(DO.LineStation lineStation) { var ls = DS.DataSource.LineStationsList.Find(s => s.Station == lineStation.Station && s.LineId == lineStation.LineId); if (ls != null && !ls.IsDeleted) { DS.DataSource.LineStationsList.Remove(ls); DS.DataSource.LineStationsList.Add(lineStation); } else { throw new DO.BadLineStationException(lineStation.Station, lineStation.LineId, "Station line not found"); } }
public void UpdateLineStation(DO.LineStation lineStation) { var lineStationList = XmlTools.LoadListFromXMLSerializer <DO.LineStation>(lineStationsPath); var ls = lineStationList.Find(s => s.Station == lineStation.Station && s.LineId == lineStation.LineId); if (ls != null && !ls.IsDeleted) { lineStationList.Remove(ls); lineStationList.Add(lineStation); XmlTools.SaveListToXMLSerializer(lineStationList, lineStationsPath); } else { throw new DO.BadLineStationException(lineStation.Station, lineStation.LineId, "Station line not found"); } }
static int[] initializLineStations(int lineId, int numOfLS = 10) { List <DO.LineStation> stationTemp = new List <DO.LineStation>(); var first = new DO.LineStation() { LineId = lineId, Station = RandomValues.getStation(stationTemp), LineStationIndex = 0, PrevStation = 0, IsDeleted = false }; var area = StationsList.Find(s => s.Code == first.Station).Area; stationTemp.Add(first); for (int i = 1; i < numOfLS; i++) { stationTemp.Add(new DO.LineStation() { LineId = lineId, Station = RandomValues.getStation(stationTemp, area), LineStationIndex = i, PrevStation = stationTemp.ElementAt(i - 1).Station, IsDeleted = false }); } foreach (var ls in stationTemp) { if (ls.LineStationIndex != stationTemp.Count - 1) { ls.NextStation = stationTemp.ElementAt(ls.LineStationIndex + 1).Station; } else { ls.NextStation = -1; // last station mark in -1 } } LineStationsList.AddRange(stationTemp); return(new int[] { stationTemp.ElementAt(0).Station, stationTemp.ElementAt(stationTemp.Count - 1).Station }); }
/// <summary> /// Remove line station /// </summary> /// <param name="lineStation"></param> public void RemoveLineStation(DO.LineStation lineStation) { var ls = DS.DataSource.LineStationsList.Find(s => s.Station == lineStation.Station && s.LineId == lineStation.LineId); if (ls != null) { if (!ls.IsDeleted) { updatePrevAndNextStationOnRemove(ls); DS.DataSource.LineStationsList.Remove(ls); ls.IsDeleted = true; DS.DataSource.LineStationsList.Add(ls); } } else { throw new DO.BadLineStationException(lineStation.Station, lineStation.LineId, "Station line not found"); } }
private BO.LineStation LineStationDoToBo(DO.LineStation doLineStation, BO.LineStation prevStation = null) { if (prevStation != null) { try { var doAdjStations = dl.GetAdjacentStations(prevStation.Station.Code, doLineStation.StationCode); prevStation.Distance = doAdjStations.Distance; prevStation.DrivingTime = doAdjStations.AverageTime; } catch (DO.BadLineStationKeyException) { } } return(new BO.LineStation { Station = GetStation(doLineStation.StationCode), DrivingTime = TimeSpan.Zero, Distance = 0, ID = doLineStation.ID, LineId = doLineStation.LineId, IndexInLine = doLineStation.IndexInLine }); }
public void CreateLineStation(long lineId, long numberInLine, long code) { string exception = ""; bool foundException = false; try { valid.LineIdExist(lineId); } catch (Exception ex) { exception += ex.Message; foundException = true; } try { valid.NumberInLineExist(lineId, numberInLine); } catch (Exception ex) { exception += ex.Message; foundException = true; } try { valid.StopCodeExist(code); } catch (Exception ex) { exception += ex.Message; foundException = true; } if (foundException) { throw new Exception(exception); } try { var checkIfAlreadyExists = RequestLineStation(line => line.LineId == lineId && line.NumberInLine == numberInLine && line.Code == code); if (checkIfAlreadyExists == null) { checkIfAlreadyExists.Valid = true; return; } } catch { } var allLineStatoins = GetAllLineStations(line => line.LineId == lineId); foreach (LineStation lineStation in allLineStatoins) { if (lineStation.NumberInLine >= numberInLine) { UpdateLineStationNumberInLine(lineStation.NumberInLine + 1, lineStation.LineId, lineStation.Code); } } LineStation lineStationBO = new LineStation(code, numberInLine, lineId); DO.LineStation lineStationDO = lineStationBO.GetPropertiesFrom <DO.LineStation, BO.LineStation>(); lineStationDO.Valid = true; dal.CreateLineStation(lineStationDO); }
DO.LineStation LineStationBoDoAdapter(BO.LineStation lineStationBo) { DO.LineStation lineStationDo = new DO.LineStation(); lineStationBo.CopyPropertiesTo(lineStationDo); return(lineStationDo); }
BO.LineStation LineStationDoBoAdapter(DO.LineStation lineStationDo) { BO.LineStation lineStationBo = new BO.LineStation(); lineStationDo.CopyPropertiesTo(lineStationBo); return(lineStationBo); }