Skip to content

Commit d97a721

Browse files
Merge pull request #393 from PSMRI/feature/hindhi_translation_changes
feature for hindi translation for CG
2 parents 01110c1 + 1f8111c commit d97a721

5 files changed

Lines changed: 101 additions & 24 deletions

File tree

src/main/java/com/iemr/common/data/dynamic_from/FormField.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public class FormField {
6262
@Column(name = "created_at")
6363
private LocalDateTime createdAt = LocalDateTime.now();
6464

65+
@Column(name = "option_key")
66+
private String optionKey;
67+
6568

6669

6770
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.iemr.common.data.dynamic_from;
2+
3+
import jakarta.persistence.*;
4+
import lombok.Data;
5+
6+
@Entity
7+
@Table(name = "form_field_options")
8+
@Data
9+
public class FormFieldOption {
10+
11+
@Id
12+
@GeneratedValue(strategy = GenerationType.IDENTITY)
13+
private Integer id;
14+
15+
@Column(name = "option_key")
16+
private String optionKey;
17+
18+
@Column(name = "value")
19+
private String value;
20+
21+
@Column(name = "label_en")
22+
private String labelEn;
23+
24+
@Column(name = "label_hi")
25+
private String labelHi;
26+
27+
@Column(name = "label_as")
28+
private String labelAs;
29+
30+
@Column(name = "sort_order")
31+
private Integer sortOrder;
32+
33+
// getters/setters
34+
}

src/main/java/com/iemr/common/dto/dynamicForm/FieldResponseDTO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class FieldResponseDTO {
2020
private Integer sequence;
2121
private Boolean isEditable;
2222
private Integer stateCode;
23-
private List<String> options;
23+
public List<Map<String, Object>> options;
2424
private Map<String, Object> validation;
2525
private Map<String, Object> conditional;
2626
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.iemr.common.repository.dynamic_form;
2+
import com.iemr.common.data.dynamic_from.FormFieldOption;
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.stereotype.Repository;
5+
6+
import java.util.List;
7+
8+
@Repository
9+
public interface FormFieldOptionRepository
10+
extends JpaRepository<FormFieldOption, Integer> {
11+
12+
List<FormFieldOption> findByOptionKeyOrderBySortOrderAsc(String optionKey);
13+
14+
}

src/main/java/com/iemr/common/service/dynamicForm/FormMasterServiceImpl.java

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package com.iemr.common.service.dynamicForm;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
34
import com.fasterxml.jackson.databind.JsonNode;
45
import com.fasterxml.jackson.databind.ObjectMapper;
56
import com.iemr.common.data.dynamic_from.FormDefinition;
67
import com.iemr.common.data.dynamic_from.FormField;
8+
import com.iemr.common.data.dynamic_from.FormFieldOption;
79
import com.iemr.common.data.dynamic_from.FormModule;
810
import com.iemr.common.data.translation.Translation;
911
import com.iemr.common.data.users.UserServiceRole;
1012
import com.iemr.common.dto.dynamicForm.*;
1113
import com.iemr.common.repository.dynamic_form.FieldRepository;
14+
import com.iemr.common.repository.dynamic_form.FormFieldOptionRepository;
1215
import com.iemr.common.repository.dynamic_form.FormRepository;
1316
import com.iemr.common.repository.dynamic_form.ModuleRepository;
1417
import 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

Comments
 (0)