Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public class FormField {
@Column(name = "created_at")
private LocalDateTime createdAt = LocalDateTime.now();

@Column(name = "option_key")
private String optionKey;



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.iemr.common.data.dynamic_from;

import jakarta.persistence.*;
import lombok.Data;

@Entity
@Table(name = "form_field_options")
@Data
public class FormFieldOption {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@Column(name = "option_key")
private String optionKey;

@Column(name = "value")
private String value;

@Column(name = "label_en")
private String labelEn;

@Column(name = "label_hi")
private String labelHi;

@Column(name = "label_as")
private String labelAs;

@Column(name = "sort_order")
private Integer sortOrder;

// getters/setters
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
private Integer sequence;
private Boolean isEditable;
private Integer stateCode;
private List<String> options;
public List<Map<String, Object>> options;

Check warning on line 23 in src/main/java/com/iemr/common/dto/dynamicForm/FieldResponseDTO.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make options a static final constant or non-public and provide accessors if needed.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Common-API&issues=AZ12PdA3t-zbsGOGFFm6&open=AZ12PdA3t-zbsGOGFFm6&pullRequest=393
private Map<String, Object> validation;
private Map<String, Object> conditional;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.iemr.common.repository.dynamic_form;
import com.iemr.common.data.dynamic_from.FormFieldOption;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface FormFieldOptionRepository
extends JpaRepository<FormFieldOption, Integer> {

List<FormFieldOption> findByOptionKeyOrderBySortOrderAsc(String optionKey);

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.iemr.common.service.dynamicForm;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iemr.common.data.dynamic_from.FormDefinition;
import com.iemr.common.data.dynamic_from.FormField;
import com.iemr.common.data.dynamic_from.FormFieldOption;
import com.iemr.common.data.dynamic_from.FormModule;
import com.iemr.common.data.translation.Translation;
import com.iemr.common.data.users.UserServiceRole;
import com.iemr.common.dto.dynamicForm.*;
import com.iemr.common.repository.dynamic_form.FieldRepository;
import com.iemr.common.repository.dynamic_form.FormFieldOptionRepository;
import com.iemr.common.repository.dynamic_form.FormRepository;
import com.iemr.common.repository.dynamic_form.ModuleRepository;
import com.iemr.common.repository.translation.TranslationRepo;
Expand Down Expand Up @@ -43,6 +46,9 @@
@Autowired
private JwtUtil jwtUtil;

@Autowired

Check warning on line 49 in src/main/java/com/iemr/common/service/dynamicForm/FormMasterServiceImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this field injection and use constructor injection instead.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Common-API&issues=AZ12Pc-yt-zbsGOGFFm4&open=AZ12Pc-yt-zbsGOGFFm4&pullRequest=393
private FormFieldOptionRepository formFieldOptionRepo ;

@Override
public FormModule createModule(ModuleDTO dto) {
FormModule module = new FormModule();
Expand Down Expand Up @@ -135,24 +141,40 @@
Integer finalStateId = stateId;
List<FieldResponseDTO> fieldDtos = fields.stream().filter(formField -> (formField.getStateCode().equals(0) || formField.getStateCode().equals(finalStateId)))
.map(field -> {
String labelKey = field.getFieldId(); // field label already contains label_key
String labelKey = field.getFieldId();

Translation label = translationRepo.findByLabelKeyAndIsActive(labelKey, true)
.orElse(null);

Translation t = translationRepo.findByLabelKeyAndIsActive(labelKey, true)
Translation placeHolder = translationRepo.findByLabelKeyAndIsActive("placeholder_"+labelKey, true)
.orElse(null);

String translatedLabel = field.getLabel(); // fallback
String translatedLabel = field.getLabel();
String translatedPlaceHolder = field.getPlaceholder();

if (t != null) {
if (label != null) {
if ("hi".equalsIgnoreCase(lang)) {
translatedLabel = t.getHindiTranslation();
translatedLabel = label.getHindiTranslation();
} else if ("as".equalsIgnoreCase(lang)) {
translatedLabel = t.getAssameseTranslation();
translatedLabel = label.getAssameseTranslation();
} else if ("en".equalsIgnoreCase(lang)) {
translatedLabel = t.getEnglish();
translatedLabel = label.getEnglish();

}
}

if (placeHolder != null) {
if ("hi".equalsIgnoreCase(lang)) {
translatedPlaceHolder= placeHolder.getHindiTranslation();
} else if ("as".equalsIgnoreCase(lang)) {
translatedPlaceHolder = placeHolder.getAssameseTranslation();
} else if ("en".equalsIgnoreCase(lang)) {
translatedPlaceHolder = placeHolder.getEnglish();

}
}


FieldResponseDTO dto = new FieldResponseDTO();
dto.setId(field.getId());
dto.setIsEditable(field.getIsEditable());
Expand All @@ -165,27 +187,32 @@
dto.setType(field.getType());
dto.setIsRequired(field.getIsRequired());
dto.setDefaultValue(field.getDefaultValue());
dto.setPlaceholder(field.getPlaceholder());
dto.setPlaceholder(translatedPlaceHolder);
dto.setSequence(field.getSequence());


try {
// Handle options
if (field.getOptions() != null && !field.getOptions().isBlank()) {
JsonNode node = objectMapper.readTree(field.getOptions());
List<String> options = null;
if (node.isArray()) {
options = objectMapper.convertValue(node, new TypeReference<>() {
});
} else if (node.has("options")) {
options = objectMapper.convertValue(node.get("options"), new TypeReference<>() {
});
}
dto.setOptions(options == null || options.isEmpty() ? null : options);
if (field.getOptionKey() != null && !field.getOptionKey().isBlank()) {
List<FormFieldOption> dbOptions = formFieldOptionRepo
.findByOptionKeyOrderBySortOrderAsc(field.getOptionKey());

List<Map<String, Object>> translatedOptions = dbOptions.stream()
.map(opt -> {
Map<String, Object> map = new LinkedHashMap<>();
map.put("id", opt.getId());
map.put("value", opt.getValue());
if ("hi".equalsIgnoreCase(lang)) map.put("label", opt.getLabelHi());

Check failure on line 204 in src/main/java/com/iemr/common/service/dynamicForm/FormMasterServiceImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "label" 3 times.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Common-API&issues=AZ12Pc-yt-zbsGOGFFm3&open=AZ12Pc-yt-zbsGOGFFm3&pullRequest=393
else if ("as".equalsIgnoreCase(lang)) map.put("label", opt.getLabelAs());
else map.put("label", opt.getLabelEn());
return map;
})
.collect(Collectors.toList());

Check warning on line 209 in src/main/java/com/iemr/common/service/dynamicForm/FormMasterServiceImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this usage of 'Stream.collect(Collectors.toList())' with 'Stream.toList()' and ensure that the list is unmodified.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Common-API&issues=AZ12Pc-yt-zbsGOGFFm5&open=AZ12Pc-yt-zbsGOGFFm5&pullRequest=393

dto.setOptions(translatedOptions.isEmpty() ? null : translatedOptions);

} else {
dto.setOptions(null);
}

// Handle validation
if (field.getValidation() != null && !field.getValidation().isBlank()) {
Map<String, Object> validation = objectMapper.readValue(field.getValidation(), new TypeReference<>() {
});
Expand All @@ -194,7 +221,6 @@
dto.setValidation(null);
}

// Handle conditional
if (field.getConditional() != null && !field.getConditional().isBlank()) {
Map<String, Object> conditional = objectMapper.readValue(field.getConditional(), new TypeReference<>() {
});
Expand Down
Loading