public void AddTransitStateToNotification(TransitState transitState) { Guard.ArgumentNotNull(() => transitState, transitState); if (TransitStatesCollection.Any(ts => ts.OrdinalPosition == transitState.OrdinalPosition)) { throw new InvalidOperationException( string.Format( "Attempted to add a Transit State at position {0} for TransportRoute {1}. The TransportRoute already has a Transit State at this position.", transitState.OrdinalPosition, Id)); } if (TransitStatesCollection.Count > 0) { var validPositions = GetAvailableTransitStatePositions(); if (!validPositions.Contains(transitState.OrdinalPosition)) { throw new InvalidOperationException( string.Format( "Attempted to add a Transit State at position {0} for TransportRoute {1}. This is not a valid position for a new Transit State.", transitState.OrdinalPosition, Id)); } } TransitStatesCollection.Add(transitState); }
public void UpdateTransitStateForNotification(Guid targetTransitStateId, Country country, CompetentAuthority competentAuthority, EntryOrExitPoint entryPoint, EntryOrExitPoint exitPoint, int?ordinalPosition) { var targetTransitState = TransitStatesCollection.Single(ts => ts.Id == targetTransitStateId); if (ordinalPosition.HasValue && ordinalPosition < 1) { ordinalPosition = null; } var allTransitStatesExceptTarget = TransitStatesCollection.Where(ts => ts.Id != targetTransitStateId).ToArray(); if (ordinalPosition.HasValue && allTransitStatesExceptTarget.Any(ts => ts.OrdinalPosition == ordinalPosition)) { throw new InvalidOperationException( string.Format( "Attempted to edit a Transit State {0} to position {1} for TransportRoute {2}. The TransportRoute already has another Transit State at this position.", targetTransitStateId, ordinalPosition.Value, Id)); } targetTransitState.UpdateTransitState(country, competentAuthority, entryPoint, exitPoint, ordinalPosition); }
public void UpdateTransitStateEntryOrExitPoint(Guid targetTransitStateId, EntryOrExitPoint entryPoint, EntryOrExitPoint exitPoint) { var targetTransitState = TransitStatesCollection.Single(ts => ts.Id == targetTransitStateId); targetTransitState.UpdateTransitState(targetTransitState.Country, targetTransitState.CompetentAuthority, entryPoint, exitPoint, targetTransitState.OrdinalPosition); }
public void RemoveTransitState(Guid id) { var transitState = TransitStatesCollection.Single(ts => ts.Id == id); var removedPosition = transitState.OrdinalPosition; TransitStatesCollection.Remove(transitState); // Down-shift all transit states above the removed item. foreach (var state in TransitStatesCollection.Where(ts => ts.OrdinalPosition > removedPosition)) { state.UpdateOrdinalPosition(state.OrdinalPosition - 1); } }
public int[] GetAvailableTransitStatePositions() { if (TransitStatesCollection.Count == 0) { return(new[] { 1 }); } var currentOrdinalPositionsSet = TransitStatesCollection.Select(ts => ts.OrdinalPosition) .OrderBy(i => i).ToArray(); // We construct a list of positions available for this item by checking the positions already occupied on the notification. // For the series 1, 2 the valid position is 3. // For the series 1, 3 the valid positions are 2, 4 var validPositions = Enumerable.Range(1, currentOrdinalPositionsSet[currentOrdinalPositionsSet.Length - 1] + 1) .Where(i => !currentOrdinalPositionsSet.Contains(i)) .ToArray(); return(validPositions); }