11package com .iemr .common .service .dynamicForm ;
22
3+ import com .fasterxml .jackson .core .JsonProcessingException ;
34import com .fasterxml .jackson .databind .JsonNode ;
45import com .fasterxml .jackson .databind .ObjectMapper ;
56import com .iemr .common .data .dynamic_from .FormDefinition ;
67import com .iemr .common .data .dynamic_from .FormField ;
8+ import com .iemr .common .data .dynamic_from .FormFieldOption ;
79import com .iemr .common .data .dynamic_from .FormModule ;
810import com .iemr .common .data .translation .Translation ;
911import com .iemr .common .data .users .UserServiceRole ;
1012import com .iemr .common .dto .dynamicForm .*;
1113import com .iemr .common .repository .dynamic_form .FieldRepository ;
14+ import com .iemr .common .repository .dynamic_form .FormFieldOptionRepository ;
1215import com .iemr .common .repository .dynamic_form .FormRepository ;
1316import com .iemr .common .repository .dynamic_form .ModuleRepository ;
1417import com .iemr .common .repository .translation .TranslationRepo ;
@@ -43,6 +46,9 @@ public class FormMasterServiceImpl implements FormMasterService {
4346 @ Autowired
4447 private JwtUtil jwtUtil ;
4548
49+ @ Autowired
50+ private FormFieldOptionRepository formFieldOptionRepo ;
51+
4652 @ Override
4753 public FormModule createModule (ModuleDTO dto ) {
4854 FormModule module = new FormModule ();
@@ -135,24 +141,40 @@ public FormResponseDTO getStructuredFormByFormId(String formId, String lang, Str
135141 Integer finalStateId = stateId ;
136142 List <FieldResponseDTO > fieldDtos = fields .stream ().filter (formField -> (formField .getStateCode ().equals (0 ) || formField .getStateCode ().equals (finalStateId )))
137143 .map (field -> {
138- String labelKey = field .getFieldId (); // field label already contains label_key
144+ String labelKey = field .getFieldId ();
145+
146+ Translation label = translationRepo .findByLabelKeyAndIsActive (labelKey , true )
147+ .orElse (null );
139148
140- Translation t = translationRepo .findByLabelKeyAndIsActive (labelKey , true )
149+ Translation placeHolder = translationRepo .findByLabelKeyAndIsActive ("placeholder_" + labelKey , true )
141150 .orElse (null );
142151
143- String translatedLabel = field .getLabel (); // fallback
152+ String translatedLabel = field .getLabel ();
153+ String translatedPlaceHolder = field .getPlaceholder ();
144154
145- if (t != null ) {
155+ if (label != null ) {
146156 if ("hi" .equalsIgnoreCase (lang )) {
147- translatedLabel = t .getHindiTranslation ();
157+ translatedLabel = label .getHindiTranslation ();
148158 } else if ("as" .equalsIgnoreCase (lang )) {
149- translatedLabel = t .getAssameseTranslation ();
159+ translatedLabel = label .getAssameseTranslation ();
150160 } else if ("en" .equalsIgnoreCase (lang )) {
151- translatedLabel = t .getEnglish ();
161+ translatedLabel = label .getEnglish ();
152162
153163 }
154164 }
155165
166+ if (placeHolder != null ) {
167+ if ("hi" .equalsIgnoreCase (lang )) {
168+ translatedPlaceHolder = placeHolder .getHindiTranslation ();
169+ } else if ("as" .equalsIgnoreCase (lang )) {
170+ translatedPlaceHolder = placeHolder .getAssameseTranslation ();
171+ } else if ("en" .equalsIgnoreCase (lang )) {
172+ translatedPlaceHolder = placeHolder .getEnglish ();
173+
174+ }
175+ }
176+
177+
156178 FieldResponseDTO dto = new FieldResponseDTO ();
157179 dto .setId (field .getId ());
158180 dto .setIsEditable (field .getIsEditable ());
@@ -165,27 +187,32 @@ public FormResponseDTO getStructuredFormByFormId(String formId, String lang, Str
165187 dto .setType (field .getType ());
166188 dto .setIsRequired (field .getIsRequired ());
167189 dto .setDefaultValue (field .getDefaultValue ());
168- dto .setPlaceholder (field . getPlaceholder () );
190+ dto .setPlaceholder (translatedPlaceHolder );
169191 dto .setSequence (field .getSequence ());
170192
193+
171194 try {
172- // Handle options
173- if (field .getOptions () != null && !field .getOptions ().isBlank ()) {
174- JsonNode node = objectMapper .readTree (field .getOptions ());
175- List <String > options = null ;
176- if (node .isArray ()) {
177- options = objectMapper .convertValue (node , new TypeReference <>() {
178- });
179- } else if (node .has ("options" )) {
180- options = objectMapper .convertValue (node .get ("options" ), new TypeReference <>() {
181- });
182- }
183- dto .setOptions (options == null || options .isEmpty () ? null : options );
195+ if (field .getOptionKey () != null && !field .getOptionKey ().isBlank ()) {
196+ List <FormFieldOption > dbOptions = formFieldOptionRepo
197+ .findByOptionKeyOrderBySortOrderAsc (field .getOptionKey ());
198+
199+ List <Map <String , Object >> translatedOptions = dbOptions .stream ()
200+ .map (opt -> {
201+ Map <String , Object > map = new LinkedHashMap <>();
202+ map .put ("id" , opt .getId ());
203+ map .put ("value" , opt .getValue ());
204+ if ("hi" .equalsIgnoreCase (lang )) map .put ("label" , opt .getLabelHi ());
205+ else if ("as" .equalsIgnoreCase (lang )) map .put ("label" , opt .getLabelAs ());
206+ else map .put ("label" , opt .getLabelEn ());
207+ return map ;
208+ })
209+ .collect (Collectors .toList ());
210+
211+ dto .setOptions (translatedOptions .isEmpty () ? null : translatedOptions );
212+
184213 } else {
185214 dto .setOptions (null );
186215 }
187-
188- // Handle validation
189216 if (field .getValidation () != null && !field .getValidation ().isBlank ()) {
190217 Map <String , Object > validation = objectMapper .readValue (field .getValidation (), new TypeReference <>() {
191218 });
@@ -194,7 +221,6 @@ public FormResponseDTO getStructuredFormByFormId(String formId, String lang, Str
194221 dto .setValidation (null );
195222 }
196223
197- // Handle conditional
198224 if (field .getConditional () != null && !field .getConditional ().isBlank ()) {
199225 Map <String , Object > conditional = objectMapper .readValue (field .getConditional (), new TypeReference <>() {
200226 });
0 commit comments