Skip to content

created dto for before stock taking report - #17726#18216

Open
RaveeshaPeiris wants to merge 1 commit intodevelopmentfrom
#17726-DTO-for-before-stock-taking-report
Open

created dto for before stock taking report - #17726#18216
RaveeshaPeiris wants to merge 1 commit intodevelopmentfrom
#17726-DTO-for-before-stock-taking-report

Conversation

@RaveeshaPeiris
Copy link
Collaborator

@RaveeshaPeiris RaveeshaPeiris commented Jan 27, 2026

Summary by CodeRabbit

  • New Features

    • Enhanced before stock-taking report with improved filtering capabilities for stock quantity, department, and category.
  • Performance

    • Optimized report data retrieval through internal refactoring for better efficiency.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 27, 2026

Walkthrough

This pull request introduces a DTO-based approach for stock-taking reports in the pharmacy module. A new BeforeStockTakingDTO class is created, a corresponding retrieval method is added to StockFacade, and PharmacyController is refactored to use DTO projections via JPQL queries instead of loading full Stock entities, with conversion logic for backward compatibility.

Changes

Cohort / File(s) Summary
DTO & Facade Layer
src/main/java/com/divudi/core/data/dto/BeforeStockTakingDTO.java, src/main/java/com/divudi/core/facade/StockFacade.java
New BeforeStockTakingDTO class with 13 fields (id, itemCode, itemName, stockLocator, batchCode, expiryDate, systemStock, purchaseRate, retailRate, categoryName, categoryId, departmentId, departmentName) and full constructor/getters/setters. New public method findBeforeStockTakingReport(String sql, Map<String, Object> parameters) in StockFacade returns List of DTOs via JPA Query with parameter binding.
Controller Integration
src/main/java/com/divudi/bean/pharmacy/PharmacyController.java
Refactored before stock-taking report flow: imports BeforeStockTakingDTO and StockFacade; builds DTO-projection JPQL with stock quantity and conditional department/category filtering; retrieves DTOs via StockFacade.findBeforeStockTakingReport(); converts DTOs to Stock objects via new helper method convertDtosToStocks(); minor log message updates.

Sequence Diagram(s)

sequenceDiagram
    participant UI as Client/UI
    participant PC as PharmacyController
    participant SF as StockFacade
    participant DB as Database (JPA)
    
    UI->>PC: Request before stock-taking report<br/>(with filters: dept, category, qty)
    PC->>PC: Build DTO-projection JPQL query<br/>with filters and parameters
    PC->>SF: findBeforeStockTakingReport(sql, parameters)
    SF->>DB: Execute JPA Query<br/>(bind parameters)
    DB-->>SF: List<BeforeStockTakingDTO>
    SF-->>PC: DTO list returned
    PC->>PC: Convert DTOs to Stock objects<br/>(convertDtosToStocks)
    PC->>UI: Return Stock list for UI display
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'created dto for before stock taking report - #17726' is fully related to the main changes in the pull request, which introduce a new BeforeStockTakingDTO class and update PharmacyController to use DTO-based data retrieval for the before stock-taking report.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ast-grep (0.40.5)
src/main/java/com/divudi/bean/pharmacy/PharmacyController.java

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@src/main/java/com/divudi/core/facade/StockFacade.java`:
- Around line 37-45: Rename the method parameter `sql` to `jpql` for clarity and
update usages in findBeforeStockTakingReport; also add null-safety for
`parameters` by treating a null map as empty before iterating (e.g., check if
parameters is null and skip the loop or initialize an empty map), and continue
to set parameters on the Query created via em.createQuery(jpql) before returning
query.getResultList(); ensure you update the method signature and all internal
references (findBeforeStockTakingReport, em.createQuery, parameters)
accordingly.
🧹 Nitpick comments (2)
src/main/java/com/divudi/core/data/dto/BeforeStockTakingDTO.java (1)

28-55: Consider adding a no-arg constructor for serialization compatibility.

While the full-argument constructor works well for JPQL constructor expressions, some serialization frameworks (e.g., Jackson, certain JAX-RS providers) require a no-arg constructor. If this DTO may be serialized/deserialized in the future, adding one would improve compatibility.

♻️ Suggested improvement
+    public BeforeStockTakingDTO() {
+    }
+
     public BeforeStockTakingDTO(
             Long id,
             String itemCode,
src/main/java/com/divudi/core/facade/StockFacade.java (1)

27-31: Empty if-block appears to be dead code.

The if (em == null) {} block does nothing. If this was intended to throw an exception or initialize something, the implementation is missing. If it's not needed, consider removing it.

♻️ Suggested fix
     `@Override`
     protected EntityManager getEntityManager() {
-        if (em == null) {
-        }
         return em;
     }

Comment on lines +37 to +45
public List<BeforeStockTakingDTO> findBeforeStockTakingReport(String sql, Map<String, Object> parameters) {
Query query = em.createQuery(sql);

for (Map.Entry<String, Object> entry : parameters.entrySet()) {
query.setParameter(entry.getKey(), entry.getValue());
}

return query.getResultList();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Add null safety for parameters and consider renaming sql to jpql.

Two observations:

  1. NullPointerException risk: If parameters is null, the for-each loop will throw NPE.
  2. Misleading parameter name: em.createQuery() expects JPQL, not SQL. Naming the parameter jpql would be clearer.
🔧 Suggested fix
-    public List<BeforeStockTakingDTO> findBeforeStockTakingReport(String sql, Map<String, Object> parameters) {
-        Query query = em.createQuery(sql);
-
-        for (Map.Entry<String, Object> entry : parameters.entrySet()) {
-            query.setParameter(entry.getKey(), entry.getValue());
+    public List<BeforeStockTakingDTO> findBeforeStockTakingReport(String jpql, Map<String, Object> parameters) {
+        Query query = em.createQuery(jpql);
+
+        if (parameters != null) {
+            for (Map.Entry<String, Object> entry : parameters.entrySet()) {
+                query.setParameter(entry.getKey(), entry.getValue());
+            }
         }

         return query.getResultList();
     }
🤖 Prompt for AI Agents
In `@src/main/java/com/divudi/core/facade/StockFacade.java` around lines 37 - 45,
Rename the method parameter `sql` to `jpql` for clarity and update usages in
findBeforeStockTakingReport; also add null-safety for `parameters` by treating a
null map as empty before iterating (e.g., check if parameters is null and skip
the loop or initialize an empty map), and continue to set parameters on the
Query created via em.createQuery(jpql) before returning query.getResultList();
ensure you update the method signature and all internal references
(findBeforeStockTakingReport, em.createQuery, parameters) accordingly.

Copy link
Member

@buddhika75 buddhika75 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Review: Before Stock Taking Report DTO Implementation

Overall Assessment

The approach of using DTO projection for the stock taking report is good for performance. However, there are several issues that need to be addressed before this can be merged.


🔴 Critical Issues

1. Variable Shadowing Bug (PharmacyController.java, line ~1734)

Department dept = new Department();  // Shadows class field 'dept'

This shadows the class field dept which is used as a filter parameter. Use a different variable name like department or deptEntity.

2. Excessive Formatting Changes

The PR includes ~1500+ lines of formatting changes (4-space to 8-space indentation changes) that are unrelated to the feature. This:

  • Makes the PR nearly impossible to review properly
  • Pollutes git history/blame
  • Could introduce unintended side effects

Please create a separate commit or PR for formatting changes, or revert them from this PR.


🟡 Medium Issues

3. Null Safety Missing (convertDtosToStocks method)

private List<Stock> convertDtosToStocks(List<BeforeStockTakingDTO> dtos) {
    List<Stock> stocks = new ArrayList<>();
    for (BeforeStockTakingDTO dto : dtos) {  // No null check for dtos

Add null check: if (dtos == null) return stocks;

4. Potential Entity/Lazy Loading Issues

Creating detached entity objects (Item, Category, Department) and setting them on Stock may cause issues if the UI tries to access properties that weren't loaded in the DTO. The entities created are just shells with IDs and names - if any other property is accessed, it will be null instead of lazy-loading.

Consider either:

  • Using the DTOs directly in the frontend
  • Or ensuring all needed properties are included in the DTO

🟢 Minor Suggestions

5. StockFacade Method Could Be More Generic

The findBeforeStockTakingReport method is very specific. Consider renaming to something more generic or using the existing findByJpql pattern with proper typing.

6. DTO Could Benefit from a No-Args Constructor

For serialization compatibility, consider adding a no-args constructor to BeforeStockTakingDTO.


Summary

  • ✅ Good: Using DTO projection for performance
  • ✅ Good: Proper JPQL query with joins
  • ❌ Fix: Variable shadowing bug
  • ❌ Fix: Remove or separate formatting changes
  • ⚠️ Consider: Null safety and lazy loading concerns

Please address the critical issues and resubmit. TIA!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants