@@ -36,6 +36,12 @@ public class RideService {
3636 @ Autowired
3737 private RequestTransitService requestTransitService ;
3838
39+ @ Autowired
40+ private ChangePickupService changePickupService ;
41+
42+ @ Autowired
43+ private ChangeDestinationService changeDestinationService ;
44+
3945 @ Autowired
4046 private DriverRepository driverRepository ;
4147
@@ -103,6 +109,24 @@ public TransitDTO createTransit(Long clientId, AddressDTO fromDto, AddressDTO to
103109 return loadTransit (requestForTransit .getId ());
104110 }
105111
112+ @ Transactional
113+ public void changeTransitAddressFrom (UUID requestUUID , Address newAddress ) {
114+ if (driverAssignmentFacade .isDriverAssigned (requestUUID )) {
115+ throw new IllegalStateException ("Driver already assigned, requestUUID = " + requestUUID );
116+ }
117+ newAddress = addressRepository .save (newAddress );
118+ TransitDetailsDTO transitDetails = transitDetailsFacade .find (requestUUID );
119+ Address oldAddress = transitDetails .from .toAddressEntity ();
120+ Distance newDistance = changePickupService .changeTransitAddressFrom (requestUUID , newAddress , oldAddress );
121+ transitDetailsFacade .pickupChangedTo (requestUUID , newAddress , newDistance );
122+ driverAssignmentFacade .notifyProposedDriversAboutChangedDestination (requestUUID );
123+ }
124+
125+ @ Transactional
126+ public void changeTransitAddressFrom (UUID requestUUID , AddressDTO newAddress ) {
127+ changeTransitAddressFrom (requestUUID , newAddress .toAddressEntity ());
128+ }
129+
106130 private Client findClient (Long clientId ) {
107131 Client client = clientRepository .getOne (clientId );
108132 if (client == null ) {
@@ -116,80 +140,22 @@ private Address addressFromDto(AddressDTO addressDTO) {
116140 return addressRepository .save (address );
117141 }
118142
119- @ Transactional
120- public void changeTransitAddressFrom (UUID requestUUID , Address newAddress ) {
121- newAddress = addressRepository .save (newAddress );
122- TransitDemand transitDemand = transitDemandRepository .findByTransitRequestUUID (requestUUID );
123- if (transitDemand == null ) {
124- throw new IllegalArgumentException ("Transit does not exist, id = " + requestUUID );
125- }
126- if (driverAssignmentFacade .isDriverAssigned (requestUUID )) {
127- throw new IllegalStateException ("Driver already assigned, requestUUID = " + requestUUID );
128- }
129- TransitDetailsDTO transitDetails = transitDetailsFacade .find (requestUUID );
130- // FIXME later: add some exceptions handling
131- double [] geoFromNew = geocodingService .geocodeAddress (newAddress );
132- double [] geoFromOld = geocodingService .geocodeAddress (transitDetails .from .toAddressEntity ());
133-
134- // https://www.geeksforgeeks.org/program-distance-two-points-earth/
135- // The math module contains a function
136- // named toRadians which converts from
137- // degrees to radians.
138- double lon1 = Math .toRadians (geoFromNew [1 ]);
139- double lon2 = Math .toRadians (geoFromOld [1 ]);
140- double lat1 = Math .toRadians (geoFromNew [0 ]);
141- double lat2 = Math .toRadians (geoFromOld [0 ]);
142-
143- // Haversine formula
144- double dlon = lon2 - lon1 ;
145- double dlat = lat2 - lat1 ;
146- double a = Math .pow (Math .sin (dlat / 2 ), 2 )
147- + Math .cos (lat1 ) * Math .cos (lat2 )
148- * Math .pow (Math .sin (dlon / 2 ), 2 );
149-
150- double c = 2 * Math .asin (Math .sqrt (a ));
151-
152- // Radius of earth in kilometers. Use 3956 for miles
153- double r = 6371 ;
154-
155- // calculate the result
156- double distanceInKMeters = c * r ;
157-
158- Distance newDistance = Distance .ofKm ((float ) distanceCalculator .calculateByMap (geoFromNew [0 ], geoFromNew [1 ], geoFromOld [0 ], geoFromOld [1 ]));
159- transitDemand .changePickup (distanceInKMeters );
160- transitDetailsFacade .pickupChangedTo (requestUUID , newAddress , newDistance );
161- driverAssignmentFacade .notifyProposedDriversAboutChangedDestination (requestUUID );
162- }
163-
164143 @ Transactional
165144 public void changeTransitAddressTo (UUID requestUUID , AddressDTO newAddress ) {
166145 changeTransitAddressTo (requestUUID , newAddress .toAddressEntity ());
167146 }
168147
169- @ Transactional
170- public void changeTransitAddressFrom (UUID requestUUID , AddressDTO newAddress ) {
171- changeTransitAddressFrom (requestUUID , newAddress .toAddressEntity ());
172- }
173-
174148 @ Transactional
175149 public void changeTransitAddressTo (UUID requestUUID , Address newAddress ) {
176- addressRepository .save (newAddress );
177- RequestForTransit requestForTransit = requestForTransitRepository .findByRequestUUID (requestUUID );
150+ newAddress = addressRepository .save (newAddress );
178151 TransitDetailsDTO transitDetails = transitDetailsFacade .find (requestUUID );
179- if (requestForTransit == null ) {
152+ if (transitDetails == null ) {
180153 throw new IllegalArgumentException ("Transit does not exist, id = " + requestUUID );
181154 }
182-
183- // FIXME later: add some exceptions handling
184- double [] geoFrom = geocodingService .geocodeAddress (transitDetails .from .toAddressEntity ());
185- double [] geoTo = geocodingService .geocodeAddress (newAddress );
186- Distance newDistance = Distance .ofKm ((float ) distanceCalculator .calculateByMap (geoFrom [0 ], geoFrom [1 ], geoTo [0 ], geoTo [1 ]));
187- Transit transit = transitRepository .findByTransitRequestUUID (requestUUID );
188- if (transit != null ) {
189- transit .changeDestination (newDistance );
190- }
155+ Address oldAddress = transitDetails .from .toAddressEntity ();
156+ Distance distance = changeDestinationService .changeTransitAddressTo (requestUUID , newAddress , oldAddress );
191157 driverAssignmentFacade .notifyAssignedDriverAboutChangedDestination (requestUUID );
192- transitDetailsFacade .destinationChanged (requestUUID , newAddress );
158+ transitDetailsFacade .destinationChanged (requestUUID , newAddress , distance );
193159 }
194160
195161 @ Transactional
0 commit comments