1919import reactor .core .publisher .Mono ;
2020
2121import java .util .List ;
22+ import java .util .Objects ;
23+ import java .util .Set ;
2224import java .util .concurrent .atomic .AtomicInteger ;
2325import java .util .stream .Collectors ;
26+ import java .util .stream .Stream ;
2427
2528import static java .util .Collections .emptyList ;
2629import static java .util .Objects .isNull ;
2730import static java .util .Objects .nonNull ;
31+ import static no .nav .dolly .errorhandling .ErrorStatusDecoder .getInfoVenter ;
2832
2933@ Slf4j
3034@ Service
@@ -33,6 +37,7 @@ public class InstdataClient implements ClientRegister {
3337
3438 private static final String INST2_STATUS = "INST2_STATUS#%s" ;
3539 private static final String KDI_STATUS = "KDI_STATUS#%s" ;
40+ private static final String INSTDATA = "Institusjonsopphold" ;
3641
3742 private final MapperFacade mapperFacade ;
3843 private final InstdataConsumer instdataConsumer ;
@@ -43,25 +48,29 @@ public class InstdataClient implements ClientRegister {
4348 @ Override
4449 public Mono <BestillingProgress > gjenopprett (RsDollyUtvidetBestilling bestilling , DollyPerson dollyPerson , BestillingProgress progress , boolean isOpprettEndre ) {
4550
46- if (bestilling .getInstdata ().isEmpty () && isNull (bestilling .getInstdataKdi ())) {
51+ if (( bestilling .getInstdata ().isEmpty () && isNull (bestilling .getInstdataKdi ())) || bestilling . getEnvironments (). isEmpty () ) {
4752
4853 return Mono .empty ();
4954 }
5055
51- return Flux .merge (doInst2Bestilling (bestilling , dollyPerson , isOpprettEndre )
56+ return oppdaterStatus (progress , prepInitStatus (bestilling ))
57+ .then (Flux .merge (doInst2Bestilling (bestilling , dollyPerson , isOpprettEndre )
5258 .map (INST2_STATUS ::formatted ),
5359 doInstKdiBestilling (bestilling , dollyPerson , progress .getBestillingId (), isOpprettEndre )
5460 .map (KDI_STATUS ::formatted ))
5561 .collect (Collectors .joining ("|" ))
5662 .filter (resultat -> !resultat .isBlank ())
57- .flatMap (resultat -> oppdaterStatus (progress , resultat ));
63+ .flatMap (resultat -> oppdaterStatus (progress , resultat ))) ;
5864 }
5965
60- private Mono <String > doInstKdiBestilling (RsDollyUtvidetBestilling bestilling , DollyPerson dollyPerson , Long bestillingId , boolean isOpprettEndre ) {
66+ private Mono <String > doInstKdiBestilling (RsDollyUtvidetBestilling bestilling , DollyPerson dollyPerson ,
67+ Long bestillingId , boolean isOpprettEndre ) {
6168
6269 return Mono .just (bestilling )
63- .filter (bestilling1 -> nonNull (bestilling1 .getInstdataKdi ()))
64- .flatMap (bestilling1 -> instKdiHendelseService .getOppdaterBestilling (bestilling1 , bestillingId , isOpprettEndre ))
70+ .filter (_ -> nonNull (bestilling .getInstdataKdi ()) &&
71+ !bestilling .getEnvironments ().isEmpty ())
72+ .flatMap (_ -> instKdiHendelseService .getOppdaterBestilling (bestilling ,
73+ bestillingId , isOpprettEndre ))
6574 .flatMap (instKdiData -> instdataConsumer .getMiljoer ()
6675 .flatMapMany (miljoer -> Flux .fromIterable (miljoer .getKdiEnvironments ())
6776 .filter (miljoe -> bestilling .getEnvironments ().contains (miljoe ))
@@ -78,12 +87,13 @@ private Mono<String> doInstKdiBestilling(RsDollyUtvidetBestilling bestilling, Do
7887
7988 private Flux <String > doInst2Bestilling (RsDollyUtvidetBestilling bestilling , DollyPerson dollyPerson , boolean isOpprettEndre ) {
8089
81- var context = MappingContextUtils .getMappingContext ();
82- context .setProperty ("ident" , dollyPerson .getIdent ());
83-
84- return Mono .just (bestilling .getInstdata ())
85- .filter (rsInstdata -> !rsInstdata .isEmpty ())
86- .flatMapMany (rsInstdata -> Mono .just (mapperFacade .mapAsList (rsInstdata , Instdata .class , context )))
90+ return Mono .just (bestilling )
91+ .filter (_ -> !bestilling .getInstdata ().isEmpty () && !bestilling .getEnvironments ().isEmpty ())
92+ .flatMapMany (_ -> {
93+ var context = MappingContextUtils .getMappingContext ();
94+ context .setProperty ("ident" , dollyPerson .getIdent ());
95+ return Mono .just (mapperFacade .mapAsList (bestilling .getInstdata (), Instdata .class , context ));
96+ })
8797 .flatMap (instdata -> instdataConsumer .getMiljoer ()
8898 .flatMapMany (miljoer -> Flux .fromIterable (miljoer .getInstitusjonsoppholdEnvironments ())
8999 .filter (miljoe -> bestilling .getEnvironments ().contains (miljoe ))
@@ -102,13 +112,20 @@ private Mono<BestillingProgress> oppdaterStatus(BestillingProgress progress, Str
102112 public void release (List <String > identer ) {
103113
104114 instdataConsumer .deleteInstdata (identer )
105- .subscribe (_ -> log .info ("Slettet identer fra Instdata (inst 2)" ));
115+ .subscribe (
116+ _ -> log .info ("Slettet identer fra Instdata (inst 2)" ),
117+ throwable -> log .error ("Feil ved sletting av identer fra Instdata (inst 2)" , throwable ));
106118 instdataConsumer .deleteInstKdiData (identer )
107- .subscribe (_ -> log .info ("Slettet identer fra Institusjonsopphold fengsel (KDI)" ));
119+ .subscribe (
120+ _ -> log .info ("Slettet identer fra Institusjonsopphold fengsel (KDI)" ),
121+ throwable -> log .error ("Feil ved sletting av identer fra Institusjonsopphold fengsel (KDI)" , throwable ));
108122 }
109123
110124 private Mono <List <Instdata >> filterInstdata (List <Instdata > instdataRequest , String miljoe ) {
111125
126+ if (instdataRequest .isEmpty ()) {
127+ return Mono .just (emptyList ());
128+ }
112129 return instdataConsumer .getInstdata (instdataRequest .getFirst ().getNorskident (), miljoe )
113130 .map (eksisterende -> {
114131 log .info ("Instdata hentet data fra {}: {}" , miljoe , eksisterende .getInstitusjonsopphold ());
@@ -121,6 +138,17 @@ private Mono<List<Instdata>> filterInstdata(List<Instdata> instdataRequest, Stri
121138 });
122139 }
123140
141+ private String prepInitStatus (RsDollyUtvidetBestilling bestilling ) {
142+
143+ var miljoerString = bestilling .getEnvironments ().stream ()
144+ .map (miljo -> String .format ("%s:%s" , miljo , getInfoVenter (INSTDATA )))
145+ .collect (Collectors .joining ("," ));
146+ return Stream .of (bestilling .getInstdata ().isEmpty () ? null : INST2_STATUS .formatted (miljoerString ),
147+ bestilling .getInstdataKdi () == null ? null : KDI_STATUS .formatted (miljoerString ))
148+ .filter (Objects ::nonNull )
149+ .collect (Collectors .joining ("|" ));
150+ }
151+
124152 private String getStatus (InstdataResponse response ) {
125153
126154 return response .getStatus ().is2xxSuccessful () ? "OK" :
@@ -148,11 +176,10 @@ private Mono<String> postInstdata(boolean isNewOpphold, List<Instdata> instdata,
148176 private Mono <String > postInstdataKdi (InstdataKdiDTO instdata , String ident ) {
149177
150178 return instdataConsumer .postInstdataKdi (instdata , ident )
151- .map (response -> String . format ( "%s:%s" .formatted (
179+ .map (response -> "%s:%s" .formatted (
152180 instdata .getEnvironment (),
153181 response .getStatus ().is2xxSuccessful () ? "OK" :
154182 ErrorStatusDecoder .encodeStatus (errorStatusDecoder .getErrorText (response .getStatus (),
155- response .getFeilmelding ())))));
156-
183+ response .getFeilmelding ()))));
157184 }
158185}
0 commit comments