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 @@ -574,30 +574,6 @@ public class RMNCHBeneficiaryDetailsRmnch {
@Column(name = "gpsUnavailableReason")
private String gpsUnavailableReason;

@Expose
@Column(name = "gpsLatitude")
private Double gpsLatitude;

@Expose
@Column(name = "gpsLongitude")
private Double gpsLongitude;

@Expose
@Column(name = "digipin")
private String digipin;

@Expose
@Column(name = "gpsTimestamp")
private Timestamp gpsTimestamp;

@Expose
@Column(name = "isGpsUnavailable", nullable = false, columnDefinition = "TINYINT(1) DEFAULT 0")
private Boolean isGpsUnavailable = false;

@Expose
@Column(name = "gpsUnavailableReason")
private String gpsUnavailableReason;

// Anthropometry fields sent by Stop TB mobile app via beneficiaryDetails payload.
// i_beneficiarydetails_rmnch has no these columns — stored in i_beneficiarydetails.otherFields instead.
@Expose
Expand Down
43 changes: 42 additions & 1 deletion src/main/java/com/iemr/common/identity/mapper/InputMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
*/
package com.iemr.common.identity.mapper;

import java.io.IOException;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -33,6 +40,10 @@
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.LongSerializationPolicy;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import com.iemr.common.identity.exception.IEMRException;

public class InputMapper
Expand All @@ -42,11 +53,41 @@
private static GsonBuilder builder;
private static InputMapper instance = null;

private static final DateTimeFormatter ISO_WITH_Z =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
private static final DateTimeFormatter CLIENT_DATE_FORMAT =
DateTimeFormatter.ofPattern("MMM dd, yyyy, h:mm:ss a", Locale.ENGLISH);

private static final TypeAdapter<Timestamp> TIMESTAMP_ADAPTER = new TypeAdapter<Timestamp>() {
@Override
public void write(JsonWriter out, Timestamp value) throws IOException {
if (value == null) out.nullValue();
else out.value(value.getTime());
}

@Override
public Timestamp read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; }
if (in.peek() == JsonToken.NUMBER) return new Timestamp(in.nextLong());
String s = in.nextString();
// epoch millis as string
try { return new Timestamp(Long.parseLong(s)); } catch (NumberFormatException ignored) {}

Check warning on line 74 in src/main/java/com/iemr/common/identity/mapper/InputMapper.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this block of code, fill it in, or add a comment explaining why it is empty.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Identity-API&issues=AZ7d-sYz_cCzvGEMsQBK&open=AZ7d-sYz_cCzvGEMsQBK&pullRequest=168
// ISO 8601 with Z e.g. "2021-06-18T00:00:00.000Z"
try { return Timestamp.from(Instant.parse(s)); } catch (Exception ignored) {}

Check warning on line 76 in src/main/java/com/iemr/common/identity/mapper/InputMapper.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this block of code, fill it in, or add a comment explaining why it is empty.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Identity-API&issues=AZ7d-sYz_cCzvGEMsQBL&open=AZ7d-sYz_cCzvGEMsQBL&pullRequest=168
// Mobile client format e.g. "Jun 18, 2021, 5:30:00 AM"
try { return Timestamp.valueOf(LocalDateTime.parse(s, CLIENT_DATE_FORMAT)); } catch (Exception ignored) {}

Check warning on line 78 in src/main/java/com/iemr/common/identity/mapper/InputMapper.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this block of code, fill it in, or add a comment explaining why it is empty.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Identity-API&issues=AZ7d-sYz_cCzvGEMsQBM&open=AZ7d-sYz_cCzvGEMsQBM&pullRequest=168
// Fallback: "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" literal Z
try { return Timestamp.valueOf(LocalDateTime.parse(s, ISO_WITH_Z)); } catch (Exception ignored) {}

Check warning on line 80 in src/main/java/com/iemr/common/identity/mapper/InputMapper.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this block of code, fill it in, or add a comment explaining why it is empty.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Identity-API&issues=AZ7d-sYz_cCzvGEMsQBN&open=AZ7d-sYz_cCzvGEMsQBN&pullRequest=168
return null;
}
};

private InputMapper()
{
builder = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
// .excludeFieldsWithoutExposeAnnotation()
.serializeNulls().setLongSerializationPolicy(LongSerializationPolicy.STRING);
.serializeNulls().setLongSerializationPolicy(LongSerializationPolicy.STRING)
.registerTypeAdapter(Timestamp.class, TIMESTAMP_ADAPTER);
}

public static InputMapper getInstance()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,24 @@
*/
package com.iemr.common.identity.utils.mapper;

import java.io.IOException;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.gson.ExclusionStrategy;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import com.iemr.common.identity.utils.exception.IEMRException;

/**
Expand All @@ -44,10 +55,34 @@
ExclusionStrategy strategy;
Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());

public InputMapper() {

Check failure on line 58 in src/main/java/com/iemr/common/identity/utils/mapper/InputMapper.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 20 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Identity-API&issues=AZ7d-sXy_cCzvGEMsQBG&open=AZ7d-sXy_cCzvGEMsQBG&pullRequest=168
if (builder == null) {
builder = new GsonBuilder();
builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
builder.registerTypeAdapter(Timestamp.class, new TypeAdapter<Timestamp>() {
private final DateTimeFormatter isoWithZ = DateTimeFormatter.ISO_INSTANT;
private final DateTimeFormatter isoNoTz = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");

@Override
public void write(JsonWriter out, Timestamp value) throws IOException {
if (value == null) out.nullValue();
else out.value(value.getTime());
}

@Override
public Timestamp read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; }
if (in.peek() == JsonToken.NUMBER) return new Timestamp(in.nextLong());
String s = in.nextString();
// epoch millis as string
try { return new Timestamp(Long.parseLong(s)); } catch (NumberFormatException ignored) {}

Check warning on line 78 in src/main/java/com/iemr/common/identity/utils/mapper/InputMapper.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this block of code, fill it in, or add a comment explaining why it is empty.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Identity-API&issues=AZ7d-sXy_cCzvGEMsQBH&open=AZ7d-sXy_cCzvGEMsQBH&pullRequest=168
// ISO 8601 with timezone (e.g. "2026-05-28T03:24:35.000Z")
try { return Timestamp.from(Instant.parse(s)); } catch (Exception ignored) {}

Check warning on line 80 in src/main/java/com/iemr/common/identity/utils/mapper/InputMapper.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this block of code, fill it in, or add a comment explaining why it is empty.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Identity-API&issues=AZ7d-sXy_cCzvGEMsQBI&open=AZ7d-sXy_cCzvGEMsQBI&pullRequest=168
// ISO without timezone (e.g. "2026-05-28T03:24:35.000")
try { return Timestamp.from(LocalDateTime.parse(s, isoNoTz).toInstant(ZoneOffset.UTC)); } catch (Exception ignored) {}

Check warning on line 82 in src/main/java/com/iemr/common/identity/utils/mapper/InputMapper.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this block of code, fill it in, or add a comment explaining why it is empty.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Identity-API&issues=AZ7d-sXy_cCzvGEMsQBJ&open=AZ7d-sXy_cCzvGEMsQBJ&pullRequest=168
return null;
}
});
}
}

Expand Down
Loading