diff --git a/src/main/java/net/gepafin/tendermanagement/util/Utils.java b/src/main/java/net/gepafin/tendermanagement/util/Utils.java index 76f4fac6..efba6f31 100644 --- a/src/main/java/net/gepafin/tendermanagement/util/Utils.java +++ b/src/main/java/net/gepafin/tendermanagement/util/Utils.java @@ -15,9 +15,14 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; import com.fasterxml.jackson.annotation.JsonAutoDetect; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.node.ObjectNode; +import jakarta.persistence.ManyToMany; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; +import jakarta.persistence.OneToOne; import jakarta.servlet.http.HttpServletRequest; import net.gepafin.tendermanagement.constants.GepafinConstant; import org.apache.commons.collections4.MapUtils; @@ -517,19 +522,30 @@ public class Utils { public static String convertEntityToJsonForLogging(Object entity) { - if(entity == null){ + if (entity == null) { return null; } - try { - return mapper.writeValueAsString(entity); - } catch (JsonProcessingException e) { - e.printStackTrace(); - return null; - } - } - @JsonIgnoreProperties({ "childEntities", "relatedData", "otherRelations" }) - private abstract static class IgnoreChildEntities { + try { + mapper.registerModule(new JavaTimeModule()); + mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); + + JsonNode entityNode = mapper.valueToTree(entity); + ObjectNode objectNode = (ObjectNode) entityNode; + + Field[] fields = entity.getClass().getDeclaredFields(); + for (Field field : fields) { + if (field.getType().isAnnotationPresent(ManyToOne.class) || field.getType().isAnnotationPresent(OneToMany.class) || field.getType() + .isAnnotationPresent(OneToOne.class) || field.getType().isAnnotationPresent(ManyToMany.class)) { + objectNode.remove(field.getName()); + } + } + return mapper.writeValueAsString(objectNode); + } catch (JsonProcessingException e) { + log.error("Failed to parse entity in json {}", e.getMessage()); + return null; + } } public static T getClonedEntityForData(T entity) { @@ -542,8 +558,8 @@ public class Utils { @SuppressWarnings("unchecked") T clonedEntity = (T) mapper.readValue(json, entity.getClass()); return clonedEntity; } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException("Failed to clone entity", e); + log.error("Failed to clone entity {}", e.getMessage()); + return null; } } } diff --git a/src/main/java/net/gepafin/tendermanagement/web/rest/api/impl/CallApiController.java b/src/main/java/net/gepafin/tendermanagement/web/rest/api/impl/CallApiController.java index 5bd63df2..28088304 100644 --- a/src/main/java/net/gepafin/tendermanagement/web/rest/api/impl/CallApiController.java +++ b/src/main/java/net/gepafin/tendermanagement/web/rest/api/impl/CallApiController.java @@ -46,9 +46,7 @@ public class CallApiController implements CallApi { public ResponseEntity> createCallStep1(HttpServletRequest request, CreateCallRequestStep1 createCallRequest) { /** This code is responsible for creating user action logs for the "Create Call" operation. **/ - loggingUtil.logUserAction(UserActionRequest.builder().request(request).actionType(UserActionLogsEnum.INSERT) - .actionContext(UserActionContextEnum.CREATE_CALL).actionContext(UserActionContextEnum.CREATE_CALL) - .build()); + loggingUtil.logUserAction(UserActionRequest.builder().request(request).actionType(UserActionLogsEnum.INSERT).actionContext(UserActionContextEnum.CREATE_CALL).build()); CallResponse createCallResponseBean = callService.createCallStep1(request, createCallRequest); return ResponseEntity.status(HttpStatus.CREATED)