Functional Programming
Optional.ofNullable(supplementaryOfferings)
.ifPresentOrElse((supplementaryOfferingsList) -> {
Optional<SupplementaryOfferingDto> collect = supplementaryOfferingsList.stream()
.filter(s -> s.getOfferingName()
.contains(primaryOffering.getOfferingName().toUpperCase()))
.findFirst();
if (collect.isPresent() && ACTIVE.equals(collect.get().getStatus())) {
setActivationStatus(entity, ACTIVE.getName());
}
}, () -> {
if (ACTIVE.name().equals(primaryOffering.getStatus())) {
setActivationStatus(entity, ACTIVE.getName());
}
});
Here's the breakdown:
Optional.ofNullable(supplementaryOfferings)
: Creates anOptional
object with the value ofsupplementaryOfferings
. IfsupplementaryOfferings
isnull
, an emptyOptional
is created..ifPresentOrElse((supplementaryOfferingsList) -> {...}, () -> {...})
: This method is used to perform one of two actions based on whether theOptional
contains a non-null value (supplementaryOfferings
) or is empty.(supplementaryOfferingsList) -> {...}
: IfsupplementaryOfferings
is notnull
, this lambda expression is executed. It operates on the assumption thatsupplementaryOfferings
is a list. Inside this block:- It creates a stream from the list of
SupplementaryOfferingDto
. - Filters the stream based on whether the
offeringName
contains the uppercase version ofprimaryOffering.getOfferingName()
. - Uses
findFirst()
to get the first matching element. - If the element is present and its status is
ACTIVE
, it sets the activation status ofentity
usingsetActivationStatus
.
- It creates a stream from the list of
() -> {...}
: IfsupplementaryOfferings
isnull
or an empty list, this lambda expression is executed. Inside this block:- Checks if the status of
primaryOffering
isACTIVE
. - If true, it sets the activation status of
entity
usingsetActivationStatus
.
- Checks if the status of
In summary, the code checks for the presence of supplementaryOfferings
and performs different actions based on whether it is present and contains certain conditions or if it is absent and the status of primaryOffering
is ACTIVE
. The use of Optional
helps handle potential null situations more elegantly.
2. Instead of :
if (Objects.nonNull(supplementOfferByMsisdn.getData())) {
Optional<SupplementaryOfferOutput> first = supplementOfferByMsisdn.getData()
.stream()
.filter(so -> Objects.equals(so.getNgbssId(), numberTariffProjection.getExtId()))
.findFirst();
if (first.isPresent() && ACTIVE.getName().equals(first.get().getStatus())) {
setActivationStatus(entity, ACTIVE.getName());
}
}
write this:
Optional.ofNullable(supplementOfferByMsisdn.getData())
.map(data -> data.stream()
.filter(so -> Objects.equals(so.getNgbssId(), numberTariffProjection.getExtId()))
.findFirst())
.filter(found -> found.isPresent() && ACTIVE.getName().equals(found.get().getStatus()))
.ifPresent(result -> setActivationStatus(entity, ACTIVE.getName()));
--------------------------------------------------------------------------------------------------------------------------------
//package az.azerconnect.sales.salesprocess.service.impl;
//
//import az.azerconnect.sales.salesprocess.clients.GenericResponse;
//import az.azerconnect.sales.salesprocess.clients.ngbss.azerfon.NgbssAzerfonClient;
//import az.azerconnect.sales.salesprocess.clients.ngbss.azerfon.model.response.PrimaryOfferingAzf;
//import az.azerconnect.sales.salesprocess.clients.ngbss.azerfon.model.response.SubscriberAzfPrimaryOfferResponse;
//import az.azerconnect.sales.salesprocess.constants.types.TariffTypes;
//import az.azerconnect.sales.salesprocess.dto.response.SupplementaryOfferingDto;
//import az.azerconnect.sales.salesprocess.entity.TariffEntity;
//import az.azerconnect.sales.salesprocess.projection.NumberTariffProjection;
//import az.azerconnect.sales.salesprocess.repository.TariffRepository;
//import az.azerconnect.sales.salesprocess.service.AzfTariffActivationService;
//import lombok.RequiredArgsConstructor;
//import org.springframework.stereotype.Service;
//
//import java.util.List;
//import java.util.Optional;
//
//import static az.azerconnect.sales.salesprocess.constants.OfferingStatus.ACTIVE;
//
//@Service
//@RequiredArgsConstructor
//public class AzfTariffActivationServiceImpl implements AzfTariffActivationService {
//
// private final NgbssAzerfonClient ngbssAzerfonClient;
// private final TariffRepository tariffRepository;
//
// @Override
// public void activateTariffStatus(String phoneNumber, NumberTariffProjection numberTariffProjection) {
// GenericResponse<SubscriberAzfPrimaryOfferResponse> offerResponse =
// ngbssAzerfonClient.checkSubscriberByMsisdnV2(null, phoneNumber);
//
// TariffEntity tariffEntity = tariffRepository.getById(numberTariffProjection.getTariffId());
//
// if (offerResponse.getData() != null) {
// if (TariffTypes.PO.equals(numberTariffProjection.getType())) {
// PrimaryOfferingAzf primaryOffering = offerResponse.getData().getPrimaryOffering();
// if (primaryOffering != null
// && primaryOffering.getOfferingId() != null
// && primaryOffering.getOfferingId().equals(numberTariffProjection.getExtId())
// && ACTIVE.getName().equals(primaryOffering.getStatus())
// ) {
// List<SupplementaryOfferingDto> supplementaryOfferingList = offerResponse.getData().getSupplementaryOfferingList();
// if (supplementaryOfferingList != null) {
// Optional<SupplementaryOfferingDto> collect =
// supplementaryOfferingList.stream()
// .filter(s -> s.getOfferingName().contains(primaryOffering.getOfferingName().toUpperCase()))
// .findFirst();
// if (collect.isPresent()) {
// if (ACTIVE.equals(collect.get().getStatus())) {
// tariffEntity.setStatus(ACTIVE.getName());
// }
// } else {
// tariffEntity.setStatus(ACTIVE.getName());
// }
// } else {
// tariffEntity.setStatus(ACTIVE.getName());
// }
// }
// tariffRepository.save(tariffEntity);
// } else {
// List<SupplementaryOfferingDto> supplementaryOfferingList = offerResponse.getData().getSupplementaryOfferingList();
//
// if (supplementaryOfferingList != null && supplementaryOfferingList.stream()
// .anyMatch(x -> x.getOfferingId().equals(numberTariffProjection.getExtId()) &&
// x.getStatus().equals(ACTIVE))) {
// tariffEntity.setStatus(ACTIVE.getName());
// }
// tariffRepository.save(tariffEntity);
// }
// }
// }
//}
--------
package az.azerconnect.sales.salesprocess.service.impl;
import az.azerconnect.sales.salesprocess.clients.GenericResponse;
import az.azerconnect.sales.salesprocess.clients.ngbss.azerfon.NgbssAzerfonClient;
import az.azerconnect.sales.salesprocess.clients.ngbss.azerfon.model.response.PrimaryOfferingAzf;
import az.azerconnect.sales.salesprocess.clients.ngbss.azerfon.model.response.SubscriberAzfPrimaryOfferResponse;
import az.azerconnect.sales.salesprocess.constants.types.TariffTypes;
import az.azerconnect.sales.salesprocess.dto.response.SupplementaryOfferingDto;
import az.azerconnect.sales.salesprocess.entity.ChangeOfferEntity;
import az.azerconnect.sales.salesprocess.entity.TariffEntity;
import az.azerconnect.sales.salesprocess.projection.NumberTariffProjection;
import az.azerconnect.sales.salesprocess.repository.ChangeOfferRepository;
import az.azerconnect.sales.salesprocess.repository.TariffRepository;
import az.azerconnect.sales.salesprocess.service.AzfTariffActivationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import static az.azerconnect.sales.salesprocess.constants.OfferingStatus.ACTIVE;
import static az.azerconnect.sales.salesprocess.constants.ScheduleConstants.NEW_NUMBER_SALE;
@Service
@RequiredArgsConstructor
public class AzfTariffActivationServiceImpl2 implements AzfTariffActivationService {
private final NgbssAzerfonClient ngbssAzerfonClient;
private final TariffRepository tariffRepository;
private final ChangeOfferRepository changeOfferRepository;
@Override
public void activateTariffStatus(String phoneNumber, NumberTariffProjection numberTariffProjection) {
GenericResponse<SubscriberAzfPrimaryOfferResponse> offerResponse =
ngbssAzerfonClient.checkSubscriberByMsisdnV2(null, phoneNumber);
if (NEW_NUMBER_SALE.equals(numberTariffProjection.getOperationType())) {
processTariffActivation(tariffRepository.getById(numberTariffProjection.getTariffId()),
offerResponse, numberTariffProjection);
} else {
processTariffActivation(changeOfferRepository.findById(Long.valueOf(numberTariffProjection
.getTariffId())).get(), offerResponse, numberTariffProjection);
}
}
private void processTariffActivation(Object entity,
GenericResponse<SubscriberAzfPrimaryOfferResponse> offerResponse,
NumberTariffProjection numberTariffProjection) {
if (Objects.isNull(offerResponse.getData())) {
return;
}
if (TariffTypes.PO.equals(numberTariffProjection.getType())) {
processPrimaryOfferingActivation(entity, offerResponse.getData().getPrimaryOffering(),
offerResponse.getData().getSupplementaryOfferingList(), numberTariffProjection);
} else {
processSupplementaryOfferingActivation(entity, offerResponse.getData().getSupplementaryOfferingList(),
numberTariffProjection);
}
if (entity instanceof TariffEntity) {
tariffRepository.save((TariffEntity) entity);
} else if (entity instanceof ChangeOfferEntity) {
changeOfferRepository.save((ChangeOfferEntity) entity);
}
}
private void processPrimaryOfferingActivation(Object entity, PrimaryOfferingAzf primaryOffering,
List<SupplementaryOfferingDto> supplementaryOfferings,
NumberTariffProjection numberTariffProjection) {
if (primaryOffering != null && primaryOffering.getOfferingId() != null
&& primaryOffering.getOfferingId().equals(numberTariffProjection.getExtId())
&& ACTIVE.getName().equals(primaryOffering.getStatus())) {
Optional.ofNullable(supplementaryOfferings)
.ifPresentOrElse((supplementaryOfferingsList) -> {
Optional<SupplementaryOfferingDto> collect = supplementaryOfferingsList.stream()
.filter(s -> s.getOfferingName()
.contains(primaryOffering.getOfferingName().toUpperCase()))
.findFirst();
if (collect.isPresent() && ACTIVE.equals(collect.get().getStatus())) {
setActivationStatus(entity, ACTIVE.getName());
}
}, () -> {
if (ACTIVE.name().equals(primaryOffering.getStatus())) {
setActivationStatus(entity, ACTIVE.getName());
}
});
}
}
private void processSupplementaryOfferingActivation(Object entity,
List<SupplementaryOfferingDto> supplementaryOfferings,
NumberTariffProjection numberTariffProjection) {
if (supplementaryOfferings != null && supplementaryOfferings.stream()
.anyMatch(so -> so.getOfferingId().equals(numberTariffProjection.getExtId()) &&
so.getStatus().equals(ACTIVE))) {
setActivationStatus(entity, ACTIVE.getName());
}
}
private void setActivationStatus(Object entity, String status) {
if (entity instanceof TariffEntity) {
((TariffEntity) entity).setStatus(status);
} else if (entity instanceof ChangeOfferEntity) {
((ChangeOfferEntity) entity).setActivateStatus(status);
}
}
}
--------------------------------------------------------------------------------------------------------------------------------
//package az.azerconnect.sales.salesprocess.service.impl;
//
//import az.azerconnect.sales.salesprocess.clients.GenericResponse;
//import az.azerconnect.sales.salesprocess.clients.ngbss.bakcell.NgbssBakcellClient;
//import az.azerconnect.sales.salesprocess.clients.ngbss.bakcell.model.SubscriberInfoResponse;
//import az.azerconnect.sales.salesprocess.clients.ngbss.bakcell.model.SupplementaryOfferOutput;
//import az.azerconnect.sales.salesprocess.constants.types.TariffTypes;
//import az.azerconnect.sales.salesprocess.entity.TariffEntity;
//import az.azerconnect.sales.salesprocess.projection.NumberTariffProjection;
//import az.azerconnect.sales.salesprocess.repository.TariffRepository;
//import az.azerconnect.sales.salesprocess.service.BkcTariffActivationService;
//import lombok.RequiredArgsConstructor;
//import org.springframework.stereotype.Service;
//
//import java.util.List;
//import java.util.Optional;
//
//import static az.azerconnect.sales.salesprocess.constants.OfferingStatus.ACTIVE;
//
//@Service
//@RequiredArgsConstructor
//public class BkcTariffActivationServiceImpl implements BkcTariffActivationService {
//
// private final NgbssBakcellClient bakcellClient;
// private final TariffRepository tariffRepository;
//
// @Override
// public void activateTariffStatus(String phoneNumber, NumberTariffProjection numberTariffProjection) {
// TariffEntity tariffEntity = tariffRepository.getById(numberTariffProjection.getTariffId());
//
// if (TariffTypes.PO.equals(numberTariffProjection.getType())) {
// GenericResponse<SubscriberInfoResponse> subscriberInfoResponseGenericResponse = bakcellClient.checkSubscriberByMSISDN(null, phoneNumber);
// SubscriberInfoResponse data = subscriberInfoResponseGenericResponse.getData();
// if (data != null && data.getPrimaryOffering() != null
// && data.getPrimaryOffering().getOfferingId() != null
// && data.getPrimaryOffering().getOfferingId().equals(numberTariffProjection.getExtId())
// && ACTIVE.getName().equals(data.getPrimaryOffering().getStatus())
// ) {
// tariffEntity.setStatus(ACTIVE.getName());
// tariffRepository.save(tariffEntity);
// }
// } else {
// GenericResponse<List<SupplementaryOfferOutput>> supplementOfferByMsisdn = bakcellClient.getSupplementOfferByMsisdn(phoneNumber);
// if (supplementOfferByMsisdn.getData() != null) {
// Optional<SupplementaryOfferOutput> first = supplementOfferByMsisdn.getData()
// .stream()
// .filter(x -> x.getNgbssId().equals(numberTariffProjection.getExtId()))
// .findFirst();
// if (first.isPresent() && ACTIVE.getName().equals(first.get().getStatus())) {
// tariffEntity.setStatus(ACTIVE.getName());
// tariffRepository.save(tariffEntity);
// }
// }
// }
// }
//}
----------
package az.azerconnect.sales.salesprocess.service.impl;
import az.azerconnect.sales.salesprocess.clients.GenericResponse;
import az.azerconnect.sales.salesprocess.clients.ngbss.bakcell.NgbssBakcellClient;
import az.azerconnect.sales.salesprocess.clients.ngbss.bakcell.model.PrimaryOfferingBkc;
import az.azerconnect.sales.salesprocess.clients.ngbss.bakcell.model.SubscriberInfoResponse;
import az.azerconnect.sales.salesprocess.clients.ngbss.bakcell.model.SupplementaryOfferOutput;
import az.azerconnect.sales.salesprocess.constants.types.TariffTypes;
import az.azerconnect.sales.salesprocess.entity.ChangeOfferEntity;
import az.azerconnect.sales.salesprocess.entity.TariffEntity;
import az.azerconnect.sales.salesprocess.projection.NumberTariffProjection;
import az.azerconnect.sales.salesprocess.repository.ChangeOfferRepository;
import az.azerconnect.sales.salesprocess.repository.TariffRepository;
import az.azerconnect.sales.salesprocess.service.BkcTariffActivationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import static az.azerconnect.sales.salesprocess.constants.OfferingStatus.ACTIVE;
import static az.azerconnect.sales.salesprocess.constants.ScheduleConstants.NEW_NUMBER_SALE;
@Service
@RequiredArgsConstructor
public class BkcTariffActivationServiceImpl2 implements BkcTariffActivationService {
private final NgbssBakcellClient bakcellClient;
private final TariffRepository tariffRepository;
private final ChangeOfferRepository changeOfferRepository;
@Override
public void activateTariffStatus(String phoneNumber, NumberTariffProjection numberTariffProjection) {
GenericResponse<SubscriberInfoResponse> offerResponse =
bakcellClient.checkSubscriberByMSISDN(null, phoneNumber);
if (NEW_NUMBER_SALE.equals(numberTariffProjection.getOperationType())) {
proceedPrimaryOffering(tariffRepository.getById(numberTariffProjection.getTariffId()),
offerResponse, numberTariffProjection, phoneNumber);
} else {
proceedPrimaryOffering(changeOfferRepository.findById(
Long.valueOf(numberTariffProjection.getTariffId()))
.get(), offerResponse, numberTariffProjection, phoneNumber);
}
}
private void proceedPrimaryOffering(Object entity, GenericResponse<SubscriberInfoResponse> offerResponse,
NumberTariffProjection numberTariffProjection, String phoneNumber) {
GenericResponse<List<SupplementaryOfferOutput>> supplementOfferByMsisdn =
bakcellClient.getSupplementOfferByMsisdn(phoneNumber);
if (Objects.isNull(offerResponse.getData())) {
return;
}
if (TariffTypes.PO.equals(numberTariffProjection.getType())) {
processPrimaryOfferingActivation(entity, offerResponse.getData().getPrimaryOffering(),
numberTariffProjection);
} else {
processSupplementaryOfferingActivation(entity, supplementOfferByMsisdn, numberTariffProjection);
}
if (entity instanceof TariffEntity) {
tariffRepository.save((TariffEntity) entity);
} else if (entity instanceof ChangeOfferEntity) {
changeOfferRepository.save((ChangeOfferEntity) entity);
}
}
private void processPrimaryOfferingActivation(Object entity, PrimaryOfferingBkc primaryOffering,
NumberTariffProjection numberTariffProjection) {
if (Objects.nonNull(primaryOffering)
&& Objects.equals(primaryOffering.getOfferingId(), numberTariffProjection.getExtId())
&& ACTIVE.getName().equals(primaryOffering.getStatus())) {
setActivationStatus(entity, ACTIVE.getName());
}
}
private void processSupplementaryOfferingActivation(
Object entity,
GenericResponse<List<SupplementaryOfferOutput>> supplementOfferByMsisdn,
NumberTariffProjection numberTariffProjection) {
Optional.ofNullable(supplementOfferByMsisdn.getData())
.map(data -> data.stream()
.filter(so -> Objects.equals(so.getNgbssId(), numberTariffProjection.getExtId()))
.findFirst())
.filter(found -> found.isPresent() && ACTIVE.getName().equals(found.get().getStatus()))
.ifPresent(result -> setActivationStatus(entity, ACTIVE.getName()));
}
private void setActivationStatus(Object entity, String status) {
if (entity instanceof TariffEntity) {
((TariffEntity) entity).setStatus(status);
} else if (entity instanceof ChangeOfferEntity) {
((ChangeOfferEntity) entity).setActivateStatus(status);
}
}
}
Комментарии
Отправить комментарий