-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fetch HH from matview #1025
base: master
Are you sure you want to change the base?
Fetch HH from matview #1025
Changes from all commits
19482c0
29e951b
0ad9a94
94fcd8c
d348299
18fecab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,39 @@ | ||||||||
package org.egov.referralmanagement.repository; | ||||||||
|
||||||||
import org.egov.common.ds.Tuple; | ||||||||
import org.egov.common.models.household.Household; | ||||||||
import org.egov.referralmanagement.repository.rowmapper.HouseholdRowMapper; | ||||||||
import org.springframework.beans.factory.annotation.Autowired; | ||||||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; | ||||||||
import org.springframework.stereotype.Repository; | ||||||||
|
||||||||
import java.util.HashMap; | ||||||||
import java.util.List; | ||||||||
import java.util.Map; | ||||||||
|
||||||||
@Repository | ||||||||
public class HouseholdRepository { | ||||||||
|
||||||||
@Autowired | ||||||||
private NamedParameterJdbcTemplate namedParameterJdbcTemplate; | ||||||||
|
||||||||
@Autowired | ||||||||
private HouseholdRowMapper householdRowMapper; | ||||||||
|
||||||||
public Tuple<Long, List<Household>> findByView (String localityCode, Integer limit, Integer offset, String tenantId) { | ||||||||
|
||||||||
|
||||||||
String query = "select * from household_address_mv where localitycode=:localitycode and rank between :start and :end "; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The SQL queries do not filter by Also applies to: 35-35 🛠️ Refactor suggestion Avoid using Using Modify the query to include specific columns: - String query = "select * from household_address_mv where localitycode=:localitycode ORDER BY rank LIMIT :limit OFFSET :offset";
+ String query = "select column1, column2, column3 from household_address_mv where localitycode=:localitycode ORDER BY rank LIMIT :limit OFFSET :offset"; Replace
|
||||||||
|
||||||||
Comment on lines
+26
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Potential issues with pagination using Using Apply this diff to modify the pagination approach: - String query = "select * from household_address_mv where localitycode=:localitycode and rank between :start and :end ";
+ String query = "select * from household_address_mv where localitycode=:localitycode ORDER BY rank LIMIT :limit OFFSET :offset"; And adjust the parameters accordingly: - paramsMap.put("start", offset);
- paramsMap.put("end", offset+limit);
+ paramsMap.put("limit", limit);
+ paramsMap.put("offset", offset); |
||||||||
Map<String, Object> paramsMap = new HashMap<>(); | ||||||||
paramsMap.put("start", offset); | ||||||||
paramsMap.put("end", offset+limit); | ||||||||
paramsMap.put("localitycode", localityCode); | ||||||||
|
||||||||
Map<String, Object> paramsMapCount = new HashMap<>(); | ||||||||
paramsMapCount.put("localitycode", localityCode); | ||||||||
Integer maxRank = namedParameterJdbcTemplate.queryForObject("select max(rank) from household_address_mv where localitycode=:localitycode", paramsMapCount, Integer.class); | ||||||||
Comment on lines
+28
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Combine parameter maps to reduce redundancy Currently, two parameter maps are used ( Here's how you can adjust the code: Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("localitycode", localityCode);
- paramsMap.put("start", offset);
- paramsMap.put("end", offset+limit);
+ paramsMap.put("limit", limit);
+ paramsMap.put("offset", offset);
- Map<String, Object> paramsMapCount = new HashMap<>();
- paramsMapCount.put("localitycode", localityCode);
- Long totalCount = namedParameterJdbcTemplate.queryForObject("select count(*) from household_address_mv where localitycode=:localitycode", paramsMapCount, Long.class);
+ Long totalCount = namedParameterJdbcTemplate.queryForObject("select count(*) from household_address_mv where localitycode=:localitycode", paramsMap, Long.class); Now,
|
||||||||
Long totalCount = maxRank == null ? 0L : Long.valueOf(maxRank); | ||||||||
Comment on lines
+35
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using Relying on Apply this diff to correct the total count calculation: - Integer maxRank = namedParameterJdbcTemplate.queryForObject("select max(rank) from household_address_mv where localitycode=:localitycode", paramsMapCount, Integer.class);
- Long totalCount = maxRank == null ? 0L : Long.valueOf(maxRank);
+ Long totalCount = namedParameterJdbcTemplate.queryForObject("select count(*) from household_address_mv where localitycode=:localitycode", paramsMapCount, Long.class); 📝 Committable suggestion
Suggested change
|
||||||||
return new Tuple<>(totalCount, this.namedParameterJdbcTemplate.query(query, paramsMap, householdRowMapper)); | ||||||||
} | ||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,76 @@ | ||||||||||||||
package org.egov.referralmanagement.repository.rowmapper; | ||||||||||||||
|
||||||||||||||
import com.fasterxml.jackson.core.JsonProcessingException; | ||||||||||||||
import com.fasterxml.jackson.databind.ObjectMapper; | ||||||||||||||
import digit.models.coremodels.AuditDetails; | ||||||||||||||
import org.egov.common.models.core.AdditionalFields; | ||||||||||||||
import org.egov.common.models.household.Address; | ||||||||||||||
import org.egov.common.models.household.AddressType; | ||||||||||||||
import org.egov.common.models.core.Boundary; | ||||||||||||||
import org.egov.common.models.household.Household; | ||||||||||||||
import org.springframework.jdbc.core.RowMapper; | ||||||||||||||
import org.springframework.stereotype.Component; | ||||||||||||||
|
||||||||||||||
import java.sql.ResultSet; | ||||||||||||||
import java.sql.SQLException; | ||||||||||||||
|
||||||||||||||
@Component | ||||||||||||||
public class HouseholdRowMapper implements RowMapper<Household> { | ||||||||||||||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Prefer Dependency Injection for ObjectMapper It is recommended to inject the Apply this change to use dependency injection: - private final ObjectMapper objectMapper = new ObjectMapper();
+ @Autowired
+ private ObjectMapper objectMapper; Ensure that the class is configured for component scanning and that an 📝 Committable suggestion
Suggested change
|
||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public Household mapRow(ResultSet resultSet, int i) throws SQLException { | ||||||||||||||
try { | ||||||||||||||
AuditDetails auditDetails = AuditDetails.builder() | ||||||||||||||
.createdBy(resultSet.getString("createdBy")) | ||||||||||||||
.createdTime(resultSet.getLong("createdTime")) | ||||||||||||||
.lastModifiedBy(resultSet.getString("lastModifiedBy")) | ||||||||||||||
.lastModifiedTime(resultSet.getLong("lastModifiedTime")) | ||||||||||||||
.build(); | ||||||||||||||
AuditDetails clientAuditDetails = AuditDetails.builder() | ||||||||||||||
.createdTime(resultSet.getLong("clientCreatedTime")) | ||||||||||||||
.createdBy(resultSet.getString("clientCreatedBy")) | ||||||||||||||
.lastModifiedTime(resultSet.getLong("clientLastModifiedTime")) | ||||||||||||||
.lastModifiedBy(resultSet.getString("clientLastModifiedBy")) | ||||||||||||||
.build(); | ||||||||||||||
Household household = Household.builder() | ||||||||||||||
.id(resultSet.getString("id")) | ||||||||||||||
.rowVersion(resultSet.getInt("rowVersion")) | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle Potential Null Values for Integer Fields Using Apply this diff to handle null values appropriately: - .rowVersion(resultSet.getInt("rowVersion"))
- .memberCount(resultSet.getInt("numberOfMembers"))
+ .rowVersion(resultSet.getObject("rowVersion") != null ? resultSet.getInt("rowVersion") : null)
+ .memberCount(resultSet.getObject("numberOfMembers") != null ? resultSet.getInt("numberOfMembers") : null) Also, update the data types in the - private int rowVersion;
- private int memberCount;
+ private Integer rowVersion;
+ private Integer memberCount; Also applies to: 41-41 |
||||||||||||||
.isDeleted(resultSet.getBoolean("isDeleted")) | ||||||||||||||
.tenantId(resultSet.getString("tenantId")) | ||||||||||||||
.memberCount(resultSet.getInt("numberOfMembers")) | ||||||||||||||
.clientReferenceId(resultSet.getString("clientReferenceId")) | ||||||||||||||
.auditDetails(auditDetails) | ||||||||||||||
.clientAuditDetails(clientAuditDetails) | ||||||||||||||
.additionalFields(resultSet.getString("additionalDetails") == null ? null : objectMapper.readValue(resultSet | ||||||||||||||
.getString("additionalDetails"), AdditionalFields.class)) | ||||||||||||||
Comment on lines
+45
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick (assertive) Handle Null Values for AdditionalFields If Consider simplifying the code: - .additionalFields(resultSet.getString("additionalDetails") == null ? null : objectMapper.readValue(resultSet
- .getString("additionalDetails"), AdditionalFields.class))
+ .additionalFields(resultSet.getString("additionalDetails") != null ?
+ objectMapper.readValue(resultSet.getString("additionalDetails"), AdditionalFields.class) : null) 📝 Committable suggestion
Suggested change
|
||||||||||||||
.address(Address.builder() | ||||||||||||||
.id(resultSet.getString("aid")) | ||||||||||||||
.clientReferenceId(resultSet.getString("aclientreferenceid")) | ||||||||||||||
.tenantId(resultSet.getString("atenantid")) | ||||||||||||||
.doorNo(resultSet.getString("doorNo")) | ||||||||||||||
.latitude(resultSet.getDouble("latitude")) | ||||||||||||||
.longitude(resultSet.getDouble("longitude")) | ||||||||||||||
.locationAccuracy(resultSet.getDouble("locationAccuracy")) | ||||||||||||||
Comment on lines
+52
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Properly Handle Null Values for Double Fields Using Apply this diff to handle null values correctly: - .latitude(resultSet.getDouble("latitude"))
- .longitude(resultSet.getDouble("longitude"))
- .locationAccuracy(resultSet.getDouble("locationAccuracy"))
+ .latitude(resultSet.getObject("latitude") != null ? resultSet.getDouble("latitude") : null)
+ .longitude(resultSet.getObject("longitude") != null ? resultSet.getDouble("longitude") : null)
+ .locationAccuracy(resultSet.getObject("locationAccuracy") != null ? resultSet.getDouble("locationAccuracy") : null) Also, update the corresponding fields in the - private double latitude;
- private double longitude;
- private double locationAccuracy;
+ private Double latitude;
+ private Double longitude;
+ private Double locationAccuracy; 📝 Committable suggestion
Suggested change
|
||||||||||||||
.type(AddressType.fromValue(resultSet.getString("type"))) | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check for Null Before Using AddressType.fromValue If Apply this diff to handle potential null values: - .type(AddressType.fromValue(resultSet.getString("type")))
+ .type(resultSet.getString("type") != null ? AddressType.fromValue(resultSet.getString("type")) : null) 📝 Committable suggestion
Suggested change
|
||||||||||||||
.addressLine1(resultSet.getString("addressLine1")) | ||||||||||||||
.addressLine2(resultSet.getString("addressLine2")) | ||||||||||||||
.landmark(resultSet.getString("landmark")) | ||||||||||||||
.city(resultSet.getString("city")) | ||||||||||||||
.pincode(resultSet.getString("pinCode")) | ||||||||||||||
.buildingName(resultSet.getString("buildingName")) | ||||||||||||||
.street(resultSet.getString("street")) | ||||||||||||||
.locality(resultSet.getString("localityCode") != null ? | ||||||||||||||
Boundary.builder().code(resultSet.getString("localityCode")).build() : null) | ||||||||||||||
.build()) | ||||||||||||||
.build(); | ||||||||||||||
if (household.getAddress().getId() == null) { | ||||||||||||||
household.setAddress(null); | ||||||||||||||
} | ||||||||||||||
Comment on lines
+67
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prevent Potential NullPointerException When Checking Address ID If Apply this diff to ensure safety: - if (household.getAddress().getId() == null) {
+ if (household.getAddress() == null || household.getAddress().getId() == null) { Alternatively, if 📝 Committable suggestion
Suggested change
|
||||||||||||||
return household; | ||||||||||||||
} catch (JsonProcessingException e) { | ||||||||||||||
throw new SQLException(e); | ||||||||||||||
} | ||||||||||||||
Comment on lines
+71
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick (assertive) Log Exceptions Before Rethrowing Currently, Consider logging the exception: } catch (JsonProcessingException e) {
+ // Log the exception
+ logger.error("Error processing JSON for additional fields", e);
throw new SQLException(e);
} Ensure that a logger is initialized in the class: private static final Logger logger = LoggerFactory.getLogger(HouseholdRowMapper.class); |
||||||||||||||
|
||||||||||||||
} | ||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused parameter
tenantId
in methodfindByView
The parameter
tenantId
is declared but not used within the method. In a multitenant system, it's important to includetenantId
in your query to ensure data isolation. Consider removing it if unnecessary or incorporating it into the SQL queries.