Untitled
unknown
plain_text
a year ago
3.9 kB
10
Indexable
package de.evobus.on.portal.visData.persistence;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import de.evobus.on.core.vehicle.persistence.Vehicle;
/**
* Service for access {@link VisData}.
*/
@Service
@AllArgsConstructor
@Slf4j
public class InternalVehicleInformationService {
private VisDataRepository visDataRepository;
/**
* Persist the given vis data.
*
* @param visData the new visData to persist.
* @return the persisted entity.
*/
public VisData save(VisData visData) {
return visDataRepository.save(visData);
}
/**
* Finds vis data by the given fin.
*
* @param fin the fin of the vehicle.
* @return Optional of vis data, if it could be foundd
*/
public Optional<VisData> getVisData(String fin) {
return visDataRepository.getByFin(fin);
}
/**
* Saves the engine in the VIS data table, if it is not null or an empty string.
*
* @param vehicle The vehicle for which the engine is set.
* @param engine The engine from VIS for the given vehicle.
*/
public void saveEngine(final Vehicle vehicle, final String engine) {
if (StringUtils.isNotBlank(engine)) {
final VisData visData = getCreateVisData(vehicle);
visData.setModelMarkedOnEngine(engine);
visDataRepository.save(visData);
}
}
/**
* Find vis data by the a given vehicle.
*
* @param vehicle vehicle
* @return vis data of the vehicle
*/
public Optional<VisData> getVisData(final Vehicle vehicle) {
return visDataRepository.getByVehicleVin(vehicle.getVin());
}
/**
* Returns the existing VIS data associated with the vehicle or creates a new (non-persisted) VIS data entry
* for the vehicle.
*
* @param vehicle The vehicle whose existing or new VIS data should be retrieved.
* @return The existing VIS data associated with the vehicle or a new VIS data instance.
*/
private VisData getCreateVisData(final Vehicle vehicle) {
final Optional<VisData> existingVisData = visDataRepository.getByVehicleVin(vehicle.getVin());
final VisData visData;
if (existingVisData.isPresent()) {
visData = existingVisData.get();
} else {
visData = new VisData();
visData.setVehicle(vehicle);
}
return visData;
}
/**
* Deletes all VIS data associated with the given vehicle.
*
* Note that the vehicle may be marked as deleted.
*
* @param vehicle The vehicle whose VIS data should be deleted.
*/
public void deleteByVehicle(final Vehicle vehicle) {
visDataRepository.deleteByVehicle(vehicle.getId());
}
/**
* Returns the FIN for a given VIN, if already known by the portal.
*
* @param vin the vehicle's VIN.
* @return {@code Optional} containing the FIN if known by the portal, otherwise an empty {@code Optional} is
* returned.
*/
public Optional<String> getFin(String vin) {
return visDataRepository.getByVehicleVin(vin).map(VisData::getFin);
}
/**
* @return {@code Optional} containing the FIN if known by the portal, otherwise an empty {@code Optional} is
* returned.
*/
public String getAftersalesModel(final String vin){
final var fin = getFin(vin);
if (fin.isEmpty() || !org.springframework.util.StringUtils.hasText(fin.get()) || fin.get().length() < 10) {
final var invalidFin = fin.orElse("'unknown'");
LOGGER.warn("FIN {} is invalid for {}. No extended information possible.", invalidFin, vin);
return null;
}
return fin.get().substring(3, 10);
}
}
Editor is loading...
Leave a Comment