avivijay19 commented on code in PR #5657: URL: https://github.com/apache/fineract/pull/5657#discussion_r2971893626
########## fineract-provider/src/main/java/org/apache/fineract/portfolio/address/service/ClientAddressWriteServiceImpl.java: ########## @@ -0,0 +1,225 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.portfolio.address.service; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import java.time.LocalDate; +import lombok.RequiredArgsConstructor; +import org.apache.fineract.infrastructure.codes.domain.CodeValue; +import org.apache.fineract.infrastructure.codes.domain.CodeValueRepository; +import org.apache.fineract.infrastructure.core.api.JsonCommand; +import org.apache.fineract.infrastructure.core.service.DateUtils; +import org.apache.fineract.portfolio.address.data.ClientAddressCreateResponse; +import org.apache.fineract.portfolio.address.data.ClientAddressUpdateResponse; +import org.apache.fineract.portfolio.address.domain.Address; +import org.apache.fineract.portfolio.address.domain.AddressRepository; +import org.apache.fineract.portfolio.address.exception.AddressNotFoundException; +import org.apache.fineract.portfolio.address.serialization.AddressCommandFromApiJsonDeserializer; +import org.apache.fineract.portfolio.client.data.ClientAddressRequest; +import org.apache.fineract.portfolio.client.domain.Client; +import org.apache.fineract.portfolio.client.domain.ClientAddress; +import org.apache.fineract.portfolio.client.domain.ClientAddressRepository; +import org.apache.fineract.portfolio.client.domain.ClientAddressRepositoryWrapper; +import org.apache.fineract.portfolio.client.domain.ClientRepositoryWrapper; + +@RequiredArgsConstructor +public class ClientAddressWriteServiceImpl implements ClientAddressWriteService { + + private final CodeValueRepository codeValueRepository; + private final ClientAddressRepository clientAddressRepository; + private final ClientRepositoryWrapper clientRepositoryWrapper; + private final AddressRepository addressRepository; + private final ClientAddressRepositoryWrapper clientAddressRepositoryWrapper; + private final AddressCommandFromApiJsonDeserializer fromApiJsonDeserializer; + + @Override + public ClientAddressCreateResponse createClientAddress(ClientAddressRequest request) { + final CodeValue addressTypeIdCodeValue = codeValueRepository.getReferenceById(request.getAddressTypeId()); + final Client client = clientRepositoryWrapper.findOneWithNotFoundDetection(request.getClientId()); + + final Address address = createAddressFromRequest(request); + addressRepository.save(address); + + boolean isActive = request.getIsActive() != null && request.getIsActive(); + final ClientAddress clientAddress = ClientAddress.fromJson(isActive, client, address, addressTypeIdCodeValue); + clientAddressRepository.saveAndFlush(clientAddress); + + return ClientAddressCreateResponse.builder().resourceId(clientAddress.getId()).build(); + } + + @Override + public ClientAddressUpdateResponse updateClientAddress(ClientAddressRequest request) { + final long addressId = request.getAddressId(); + + final ClientAddress clientAddressObj = clientAddressRepositoryWrapper.findOneByClientIdAndAddressId(request.getClientId(), + addressId); + + if (clientAddressObj == null) { + throw new AddressNotFoundException(request.getClientId()); + } + + final Address addobj = addressRepository.getReferenceById(addressId); + + boolean isAddressUpdate = false; + + if (request.getAddressLine1() != null && !request.getAddressLine1().isEmpty()) { + isAddressUpdate = true; + addobj.setAddressLine1(request.getAddressLine1()); + } + + if (request.getAddressLine2() != null && !request.getAddressLine2().isEmpty()) { + isAddressUpdate = true; + addobj.setAddressLine2(request.getAddressLine2()); + } + + if (request.getAddressLine3() != null && !request.getAddressLine3().isEmpty()) { + isAddressUpdate = true; + addobj.setAddressLine3(request.getAddressLine3()); + } + + if (request.getTownVillage() != null && !request.getTownVillage().isEmpty()) { + isAddressUpdate = true; + addobj.setTownVillage(request.getTownVillage()); + } + + if (request.getCity() != null && !request.getCity().isEmpty()) { + isAddressUpdate = true; + addobj.setCity(request.getCity()); + } + + if (request.getCountyDistrict() != null && !request.getCountyDistrict().isEmpty()) { + isAddressUpdate = true; + addobj.setCountyDistrict(request.getCountyDistrict()); + } + + if (request.getStateProvinceId() != null && request.getStateProvinceId() != 0) { + isAddressUpdate = true; + CodeValue stateIdobj = codeValueRepository.getReferenceById(request.getStateProvinceId()); + addobj.setStateProvince(stateIdobj); + } + + if (request.getCountryId() != null && request.getCountryId() != 0) { + isAddressUpdate = true; + CodeValue countryIdObj = codeValueRepository.getReferenceById(request.getCountryId()); + addobj.setCountry(countryIdObj); + } + + if (request.getPostalCode() != null && !request.getPostalCode().isEmpty()) { + isAddressUpdate = true; + addobj.setPostalCode(request.getPostalCode()); + } + + if (request.getLatitude() != null) { + isAddressUpdate = true; + addobj.setLatitude(request.getLatitude()); + } + + if (request.getLongitude() != null) { + isAddressUpdate = true; + addobj.setLongitude(request.getLongitude()); + } + + if (isAddressUpdate) { + addobj.setUpdatedOn(LocalDate.now(DateUtils.getDateTimeZoneOfTenant())); + addressRepository.save(addobj); + } + + if (request.getIsActive() != null) { + clientAddressObj.setIs_active(request.getIsActive()); + } + + return ClientAddressUpdateResponse.builder().resourceId(clientAddressObj.getId()).build(); + } + + @Override + public void addNewClientAddress(Client client, JsonCommand command) { + final JsonArray addressArray = command.arrayOfParameterNamed("address"); + + if (addressArray != null) { + for (int i = 0; i < addressArray.size(); i++) { + final JsonObject jsonObject = addressArray.get(i).getAsJsonObject(); + + fromApiJsonDeserializer.validateForCreate(jsonObject.toString(), true); + + final long addressTypeId = jsonObject.get("addressTypeId").getAsLong(); + final CodeValue addressTypeIdCodeValue = codeValueRepository.getReferenceById(addressTypeId); + + final Address address = createAddress(jsonObject); + addressRepository.save(address); + + boolean clientAddressIsActive = false; + if (jsonObject.get("isActive") != null) { + clientAddressIsActive = jsonObject.get("isActive").getAsBoolean(); + } + ClientAddress clientAddress = ClientAddress.fromJson(clientAddressIsActive, client, address, addressTypeIdCodeValue); + clientAddressRepository.saveAndFlush(clientAddress); + } + } + } + + private Address createAddressFromRequest(ClientAddressRequest request) { + CodeValue stateIdCodeValue = null; + if (request.getStateProvinceId() != null) { + stateIdCodeValue = codeValueRepository.getReferenceById(request.getStateProvinceId()); + } + + CodeValue countryIdCodeValue = null; + if (request.getCountryId() != null) { + countryIdCodeValue = codeValueRepository.getReferenceById(request.getCountryId()); + } + + final Address address = new Address(); + address.setAddressLine1(request.getAddressLine1()); + address.setAddressLine2(request.getAddressLine2()); + address.setAddressLine3(request.getAddressLine3()); + address.setTownVillage(request.getTownVillage()); + address.setCity(request.getCity()); + address.setCountyDistrict(request.getCountyDistrict()); + address.setStateProvince(stateIdCodeValue); + address.setCountry(countryIdCodeValue); + address.setPostalCode(request.getPostalCode()); + address.setLatitude(request.getLatitude()); + address.setLongitude(request.getLongitude()); + address.setCreatedBy(request.getCreatedBy()); + address.setUpdatedBy(request.getUpdatedBy()); + address.setCreatedOn(LocalDate.now(DateUtils.getDateTimeZoneOfTenant())); + address.setUpdatedOn(LocalDate.now(DateUtils.getDateTimeZoneOfTenant())); + return address; + } + + private Address createAddress(JsonObject jsonObject) { Review Comment: resolved -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
