| 1 | package mk.ukim.finki.eglas.services.Impl;
|
|---|
| 2 |
|
|---|
| 3 | import mk.ukim.finki.eglas.model.Address;
|
|---|
| 4 | import mk.ukim.finki.eglas.model.Citizen;
|
|---|
| 5 | import mk.ukim.finki.eglas.model.ElectoralUnit;
|
|---|
| 6 | import mk.ukim.finki.eglas.model.PollingStation;
|
|---|
| 7 | import mk.ukim.finki.eglas.repository.AddressRepository;
|
|---|
| 8 | import mk.ukim.finki.eglas.repository.CitizenRepository;
|
|---|
| 9 | import mk.ukim.finki.eglas.repository.PollingStationRepository;
|
|---|
| 10 | import mk.ukim.finki.eglas.services.AddressService;
|
|---|
| 11 | import mk.ukim.finki.eglas.services.CitizenService;
|
|---|
| 12 | import mk.ukim.finki.eglas.services.ElectoralUnitService;
|
|---|
| 13 | import mk.ukim.finki.eglas.services.MunicipalityService;
|
|---|
| 14 | import org.hibernate.annotations.DialectOverride;
|
|---|
| 15 | import org.springframework.beans.factory.annotation.Autowired;
|
|---|
| 16 | import org.springframework.stereotype.Service;
|
|---|
| 17 |
|
|---|
| 18 | import java.security.Principal;
|
|---|
| 19 | import java.util.List;
|
|---|
| 20 | @Service
|
|---|
| 21 | public class AddressServiceImpl implements AddressService {
|
|---|
| 22 |
|
|---|
| 23 | private final AddressRepository addressRepository;
|
|---|
| 24 | private final MunicipalityService municipalityService;
|
|---|
| 25 | private final ElectoralUnitService electoralUnitService;
|
|---|
| 26 | private final PollingStationRepository pollingStationRepository;
|
|---|
| 27 | private final CitizenRepository citizenRepository;
|
|---|
| 28 | // private final CitizenService citizenService;
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 | @Autowired
|
|---|
| 32 | AddressServiceImpl(AddressRepository addressRepository, MunicipalityService municipalityService, ElectoralUnitService electoralUnitService, PollingStationRepository pollingStationRepository, CitizenRepository citizenRepository){
|
|---|
| 33 | this.addressRepository = addressRepository;
|
|---|
| 34 | this.municipalityService = municipalityService;
|
|---|
| 35 | this.electoralUnitService = electoralUnitService;
|
|---|
| 36 | this.pollingStationRepository = pollingStationRepository;
|
|---|
| 37 | this.citizenRepository = citizenRepository;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | @Override
|
|---|
| 41 | public List<Address> findAllAddresses() {
|
|---|
| 42 | return addressRepository.findAll();
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | @Override
|
|---|
| 46 | public Address findAddressById(Long id) {
|
|---|
| 47 | return addressRepository.findById(id).orElseThrow(() -> new RuntimeException("Address not found"));
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 | @Override
|
|---|
| 52 | public Address updateAddress(Long id, String street, Integer houseNumber, Integer entranceNumber, Integer apartmentNumber, Long municipality, Long pollingStation) {
|
|---|
| 53 | Address address = new Address();
|
|---|
| 54 | if (id != null){
|
|---|
| 55 | address = findAddressById(id);
|
|---|
| 56 | }
|
|---|
| 57 | address.setStreet(street);
|
|---|
| 58 | address.setHouseNumber(houseNumber);
|
|---|
| 59 | address.setMunicipality(municipalityService.findById(municipality));
|
|---|
| 60 | address.setEntranceNumber(entranceNumber);
|
|---|
| 61 | address.setApartmentNumber(apartmentNumber);
|
|---|
| 62 | try
|
|---|
| 63 | {
|
|---|
| 64 | address.setPollingStation(findPollingStationById(pollingStation));
|
|---|
| 65 | }
|
|---|
| 66 | catch (Exception ex)
|
|---|
| 67 | {
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | return addressRepository.save(address);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | @Override
|
|---|
| 74 | public void deleteAddress(Long id) {
|
|---|
| 75 | addressRepository.delete(findAddressById(id));
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | @Override
|
|---|
| 79 | public List<PollingStation> findAllPollingStations() {
|
|---|
| 80 | return pollingStationRepository.findAll();
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | @Override
|
|---|
| 84 | public List<PollingStation> findAllPollingStationsByMunicipalityId(Long municipalityId) {
|
|---|
| 85 | return pollingStationRepository.findAllByAddress_Municipality_Id(municipalityId);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | @Override
|
|---|
| 89 | public PollingStation findPollingStationById(Long id) {
|
|---|
| 90 | return pollingStationRepository.findById(id).orElseThrow(() -> new RuntimeException("Polling Station not found"));
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 | @Override
|
|---|
| 96 | public PollingStation updatePollingStation(Long id, String name, String street, Integer houseNumber, Integer entranceNumber, Integer apartmentNumber, Long municipality, Long electoralUnitId) {
|
|---|
| 97 | Address address = findByStreetLikeAndHouseNumberAndEntranceNumberAndApartmentNumberAAndMunicipality(street, houseNumber, entranceNumber, apartmentNumber, municipality);
|
|---|
| 98 | PollingStation poll = new PollingStation();
|
|---|
| 99 | if (id!= null){
|
|---|
| 100 | poll = findPollingStationById(id);
|
|---|
| 101 | }
|
|---|
| 102 | poll.setAddress(address);
|
|---|
| 103 | poll.setElectoralUnit(electoralUnitService.findById(electoralUnitId));
|
|---|
| 104 | poll.setName(name);
|
|---|
| 105 | return pollingStationRepository.save(poll);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 | @Override
|
|---|
| 110 | public PollingStation deletePollingStation(Long id) {
|
|---|
| 111 | PollingStation pollingStation = findPollingStationById(id);
|
|---|
| 112 | pollingStationRepository.delete(pollingStation);
|
|---|
| 113 | return pollingStation;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | @Override
|
|---|
| 117 | public List<String> findStreetNames() {
|
|---|
| 118 | return addressRepository.findAllDistinctStreet();
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | @Override
|
|---|
| 122 | public Address findByStreetLikeAndHouseNumberAndEntranceNumberAndApartmentNumberAAndMunicipality(String street, Integer houseNumber, Integer entranceNumber, Integer apartmentNumber, Long municipality) {
|
|---|
| 123 | return addressRepository.findByStreetLikeAndHouseNumberAndEntranceNumberAndApartmentNumberAndMunicipality_Id(street, houseNumber, entranceNumber, apartmentNumber, municipality)
|
|---|
| 124 | .orElseGet(() -> updateAddress(null, street, houseNumber, entranceNumber, apartmentNumber, municipality, null));
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | @Override
|
|---|
| 128 | public PollingStation findPollingStationByCitizenId(Long citizenId){
|
|---|
| 129 | Citizen c = citizenRepository.findById(citizenId).get();
|
|---|
| 130 | return c.getAddress().getPollingStation();
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | }
|
|---|