2020import de .symeda .sormas .api .EntityDto ;
2121import de .symeda .sormas .api .caze .CaseDataDto ;
2222import de .symeda .sormas .api .i18n .I18nProperties ;
23- import de .symeda .sormas .api .patch .partial_retrieval .DisplayableFieldInfo ;
24- import de .symeda .sormas .api .patch .partial_retrieval .DisplayablePartialRetrievalResponse ;
25- import de .symeda .sormas .api .patch .partial_retrieval .FieldInfo ;
26- import de .symeda .sormas .api .patch .partial_retrieval .PartialRetrievalFailureCause ;
27- import de .symeda .sormas .api .patch .partial_retrieval .PartialRetrievalRequest ;
28- import de .symeda .sormas .api .patch .partial_retrieval .PartialRetrievalResponse ;
29- import de .symeda .sormas .api .patch .partial_retrieval .PartialRetriever ;
23+ import de .symeda .sormas .api .patch .partial_retrieval .*;
3024import de .symeda .sormas .api .utils .Tuple ;
3125import de .symeda .sormas .api .utils .fieldvisibility .FieldVisibilityCheckers ;
3226import de .symeda .sormas .backend .common .ConfigFacadeEjb ;
3327import de .symeda .sormas .backend .feature .FeatureConfigurationFacadeEjb ;
34- import de .symeda .sormas .backend .patch .BusinessDtoFacade ;
35- import de .symeda .sormas .backend .patch .PatchFieldHelper ;
36- import de .symeda .sormas .backend .patch .PathFailureCause ;
37- import de .symeda .sormas .backend .patch .PropertyAccessFailure ;
38- import de .symeda .sormas .backend .patch .PropertyAccessor ;
28+ import de .symeda .sormas .backend .patch .*;
3929import de .symeda .sormas .backend .patch .alias .PathAliasHelper ;
4030
4131@ ApplicationScoped
@@ -74,73 +64,83 @@ public PartialRetrievalResponse retrievePartial(PartialRetrievalRequest request)
7464 List <Tuple <String , Tuple <FieldInfo , PartialRetrievalFailureCause >>> results =
7565 patchFieldHelper .extractFieldTuples (request .getFieldsToRetrieve (), businessDtoFacade .fetchablePrefixes ()).stream ().map (tuple -> {
7666
77- String originalFieldName = tuple .getFirst ();
78- PathFailureCause pathFailureCause = tuple .getSecond ();
67+ try {
68+ return buildTupleImpl (tuple , caseData , beanCache );
69+ } catch (RuntimeException e ) {
70+ logger .warn ("Failure during retrieval for [{}]" , tuple , e );
71+ return Tuple .of (tuple .getFirst (), new Tuple <>((FieldInfo ) null , PartialRetrievalFailureCause .TECHNICAL ));
72+ }
7973
80- Tuple < String , PathFailureCause > unAliasedTuple = patchFieldHelper . resolveAlias ( originalFieldName );
74+ }). collect ( Collectors . toList () );
8175
82- PartialRetrievalFailureCause failureCause = Optional .ofNullable (pathFailureCause )
83- .map (PathFailureCause ::getRelatedRetrieveFailureCause )
84- .or (() -> Optional .ofNullable (unAliasedTuple .getSecond ()).map (PathFailureCause ::getRelatedRetrieveFailureCause ))
85- .orElse (null );
76+ Map <String , FieldInfo > successes = results .stream ()
77+ .filter (tuple -> tuple .getSecond ().getSecond () == null )
78+ .collect (Collectors .toMap (Tuple ::getFirst , tuple -> tuple .getSecond ().getFirst ()));
8679
87- if ( failureCause != null ) {
88- return Tuple . of ( originalFieldName , new Tuple <>(( FieldInfo ) null , failureCause ));
89- }
80+ Map < String , PartialRetrievalFailureCause > failures = results . stream ()
81+ . filter ( tuple -> tuple . getSecond (). getSecond () != null )
82+ . collect ( Collectors . toMap ( Tuple :: getFirst , tuple -> tuple . getSecond (). getSecond ()));
9083
91- String pathWithoutAlias = unAliasedTuple . getFirst ( );
92- String physicalPathName = pathWithoutAlias . substring ( pathWithoutAlias . indexOf ( '.' ) + 1 );
84+ return new PartialRetrievalResponse (). setFailuresDictionary ( failures ). setFieldInfoDictionary ( successes );
85+ }
9386
94- String aliasPath = pathAliasHelper .toAliasPath (pathWithoutAlias );
95- Optional <EntityDto > adequateBeanOpt = getAdequateBean (pathWithoutAlias , caseData , beanCache );
87+ private Tuple <String , Tuple <FieldInfo , PartialRetrievalFailureCause >> buildTupleImpl (
88+ Tuple <String , PathFailureCause > tuple ,
89+ CaseDataDto caseData ,
90+ Map <String , Optional <EntityDto >> beanCache ) {
91+ String originalFieldName = tuple .getFirst ();
92+ PathFailureCause pathFailureCause = tuple .getSecond ();
9693
97- if (adequateBeanOpt .isEmpty ()) {
98- return Tuple .of (originalFieldName , new Tuple <>((FieldInfo ) null , PartialRetrievalFailureCause .ENTITY_COULD_NOT_BE_FOUND ));
99- }
94+ Tuple <String , PathFailureCause > unAliasedTuple = patchFieldHelper .resolveAlias (originalFieldName );
10095
101- EntityDto adequateBean = adequateBeanOpt .orElseThrow ();
102- Optional <FieldInfo > specificFieldInfo = specificFieldValueRetrieverRegistry .getFieldInfo (aliasPath , adequateBean );
96+ PartialRetrievalFailureCause failureCause = Optional .ofNullable (pathFailureCause )
97+ .map (PathFailureCause ::getRelatedRetrieveFailureCause )
98+ .or (() -> Optional .ofNullable (unAliasedTuple .getSecond ()).map (PathFailureCause ::getRelatedRetrieveFailureCause ))
99+ .orElse (null );
103100
104- if (specificFieldInfo . isPresent () ) {
105- return Tuple .of (originalFieldName , new Tuple <>(specificFieldInfo . get (), ( PartialRetrievalFailureCause ) null ));
106- }
101+ if (failureCause != null ) {
102+ return Tuple .of (originalFieldName , new Tuple <>(( FieldInfo ) null , failureCause ));
103+ }
107104
108- Tuple < Tuple < Class <?>, Object >, PropertyAccessFailure > propertyType = PropertyAccessor
109- . getPropertyTypeAndValue ( adequateBean , physicalPathName , getFieldVisibilityCheckers ( caseData . getDisease ()) );
105+ String pathWithoutAlias = unAliasedTuple . getFirst ();
106+ String physicalPathName = pathWithoutAlias . substring ( pathWithoutAlias . indexOf ( '.' ) + 1 );
110107
111- PropertyAccessFailure propertyAccessFailure = propertyType .getSecond ();
112- if (propertyAccessFailure != null ) {
113- return Tuple .of (originalFieldName , new Tuple <>((FieldInfo ) null , propertyAccessFailure .getRelatedRetrieveFailureCause ()));
114- }
108+ String aliasPath = pathAliasHelper .toAliasPath (pathWithoutAlias );
109+ Optional <EntityDto > adequateBeanOpt = getAdequateBean (pathWithoutAlias , caseData , beanCache );
115110
116- Tuple <Class <?>, Object > fieldInfo = propertyType .getFirst ();
111+ if (adequateBeanOpt .isEmpty ()) {
112+ return Tuple .of (originalFieldName , new Tuple <>((FieldInfo ) null , PartialRetrievalFailureCause .ENTITY_COULD_NOT_BE_FOUND ));
113+ }
117114
118- // Some fields are translated only by there "physical-path" from root level
119- // example: Person.firstName has translation key "firstName"
120- // example: CaseData.disease has translation key "firstName"
121- String translatedFieldName = Optional .ofNullable (I18nProperties .getCaption (aliasPath , null ))
122- .or (() -> Optional .ofNullable (I18nProperties .getCaption (physicalPathName , null )))
123- .orElseGet (() -> I18nProperties .getDescription (aliasPath , aliasPath ));
115+ EntityDto adequateBean = adequateBeanOpt .orElseThrow ();
116+ Optional <FieldInfo > specificFieldInfo = specificFieldValueRetrieverRegistry .getFieldInfo (aliasPath , adequateBean );
124117
125- return Tuple .of (
126- originalFieldName ,
127- new Tuple <>(
128- new FieldInfo ().setFieldType (fieldInfo .getFirst ())
129- .setFieldValue (fieldInfo .getSecond ())
130- .setTranslatedFieldName (translatedFieldName ),
131- (PartialRetrievalFailureCause ) null ));
118+ if (specificFieldInfo .isPresent ()) {
119+ return Tuple .of (originalFieldName , new Tuple <>(specificFieldInfo .get (), (PartialRetrievalFailureCause ) null ));
120+ }
132121
133- }).collect (Collectors .toList ());
122+ Tuple <Tuple <Class <?>, Object >, PropertyAccessFailure > propertyType =
123+ PropertyAccessor .getPropertyTypeAndValue (adequateBean , physicalPathName , getFieldVisibilityCheckers (caseData .getDisease ()));
134124
135- Map <String , FieldInfo > successes = results .stream ()
136- .filter (tuple -> tuple .getSecond ().getSecond () == null )
137- .collect (Collectors .toMap (Tuple ::getFirst , tuple -> tuple .getSecond ().getFirst ()));
125+ PropertyAccessFailure propertyAccessFailure = propertyType .getSecond ();
126+ if (propertyAccessFailure != null ) {
127+ return Tuple .of (originalFieldName , new Tuple <>((FieldInfo ) null , propertyAccessFailure .getRelatedRetrieveFailureCause ()));
128+ }
138129
139- Map <String , PartialRetrievalFailureCause > failures = results .stream ()
140- .filter (tuple -> tuple .getSecond ().getSecond () != null )
141- .collect (Collectors .toMap (Tuple ::getFirst , tuple -> tuple .getSecond ().getSecond ()));
130+ Tuple <Class <?>, Object > fieldInfo = propertyType .getFirst ();
142131
143- return new PartialRetrievalResponse ().setFailuresDictionary (failures ).setFieldInfoDictionary (successes );
132+ // Some fields are translated only by there "physical-path" from root level
133+ // example: Person.firstName has translation key "firstName"
134+ // example: CaseData.disease has translation key "firstName"
135+ String translatedFieldName = Optional .ofNullable (I18nProperties .getCaption (aliasPath , null ))
136+ .or (() -> Optional .ofNullable (I18nProperties .getCaption (physicalPathName , null )))
137+ .orElseGet (() -> I18nProperties .getDescription (aliasPath , aliasPath ));
138+
139+ return Tuple .of (
140+ originalFieldName ,
141+ new Tuple <>(
142+ new FieldInfo ().setFieldType (fieldInfo .getFirst ()).setFieldValue (fieldInfo .getSecond ()).setTranslatedFieldName (translatedFieldName ),
143+ (PartialRetrievalFailureCause ) null ));
144144 }
145145
146146 @ Override
@@ -161,13 +161,13 @@ public DisplayablePartialRetrievalResponse retrievePartialForDisplay(PartialRetr
161161 }
162162
163163 private Optional <EntityDto > getAdequateBean (
164- @ NotNull String aliasPath ,
164+ @ NotNull String path ,
165165 @ NotNull CaseDataDto caseData ,
166166 @ NotNull Map <String , Optional <EntityDto >> beanCache ) {
167167
168- int i = aliasPath .indexOf ("." );
168+ int i = path .indexOf ("." );
169169
170- String prefix = StringUtils .substring (aliasPath , 0 , i );
170+ String prefix = StringUtils .substring (path , 0 , i );
171171
172172 if (CaseDataDto .I18N_PREFIX .equals (prefix )) {
173173 return Optional .of (caseData );
@@ -182,7 +182,7 @@ private Optional<EntityDto> getAdequateBean(
182182 }
183183
184184 if (entitiesSize != 1 ) {
185- logger .warn ("Only first element is supported for now: [{}], was: [{}]" , aliasPath , entitiesSize );
185+ logger .warn ("Only first element is supported for now: [{}], was: [{}]" , path , entitiesSize );
186186 }
187187
188188 return Optional .ofNullable (entityDtos ).map (actualEntities -> actualEntities .get (0 ));
0 commit comments