# Validate Supplier Details When Admin Submits

When an **Admin user** submits an order request, the system must validate that **supplier stock is sufficient** to fulfill the requested order.

For each **Order Request Item**, the **total In Stock value from all suppliers** must be **greater than or equal to the Unit of the order item**.\
If supplier stock is insufficient, the system must **block submission** and display an error message.

This section explains how to implement this validation using **DAO‑based database queries** and integrate it with the **Admin submit logic**.

***

### Requirement

For every Order Request Item in a request:

```
SUM(In Stock of all suppliers) ≥ Unit of order item
```

If this condition is not met for at least one order item, the system must show:

```
Please verify supplier stock.
```

and prevent submission.

***

### Objective

By completing this section, you will learn how to:

* Extend existing DAO interfaces for additional validation
* Query database data across multiple tables
* Validate supplier stock before Admin submits
* Integrate DAO validation with App Designer update actions

***

### Step 1: Extend DAO Interface

Open the existing DAO interface:

```
com.manual.doc.order.dao.ManualDocOrderDAO
```

Add the following method declaration:

```
public int countInCompleteInStock(String requestID) throws Exception;
```

This method will return the number of order items that have **insufficient supplier stock**.

***

### Step 2: Implement DAO Method

Open the DAO implementation class:

```
com.manual.doc.order.dao.ManualDocOrderDAOImpl
```

Add the following method implementation.

#### DAO Validation Logic

```
@Override
public int countInCompleteInStock(String requestID) throws Exception {

    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    int result = 0;

    try {
        conn = ManualJDBCServiceLocator.getInstance()
                .getConnection(ManualJDBCServiceLocator.ONEWEB_DB);

        StringBuilder sql = new StringBuilder();

        sql.append("SELECT COUNT(1) AS more_than_stock ");
        sql.append("FROM ( ");
        sql.append("  SELECT oi.order_item_id, oi.unit, ");
        sql.append("         SUM(ois.in_stock) AS sum_stock ");
        sql.append("  FROM wf_order_req_item oi ");
        sql.append("  INNER JOIN wf_order_req_item_supplier ois ");
        sql.append("    ON oi.order_item_id = ois.order_item_id ");
        sql.append("  WHERE oi.request_id = ? ");
        sql.append("  GROUP BY oi.order_item_id, oi.unit ");
        sql.append(") tmp ");
        sql.append("WHERE tmp.unit - tmp.sum_stock > 0 ");

        ps = conn.prepareStatement(sql.toString());
        ps.setString(1, requestID);

        rs = ps.executeQuery();

        if (rs.next()) {
            result = rs.getInt("more_than_stock");
        }

        return result;

    } finally {
        closeConnection(conn, ps, rs);
    }
}
```

This query identifies order items where the requested unit exceeds available supplier stock.

***

### Step 3: Create Admin Validation Class

Create a new Java class in **MasterWeb**.

#### Class Name

```
com.manual.doc.order.java.OrderRequestAdminUpdateMode
```

#### Purpose

* Validate supplier stock when Admin submits
* Block submit when stock is insufficient

#### Java Validation Logic

```
package com.manual.doc.order.java;

import java.util.HashMap;
import java.util.Vector;

import com.manual.service.ManualDAOFactory;
import com.master.form.EntityFormHandler;
import com.master.util.EAFManualUtil;
import com.master.util.ProcessAction;
import com.master.util.ProcessHelper;

public class OrderRequestAdminUpdateMode
        extends ProcessHelper
        implements ProcessAction {

    @Override
    public boolean validateResult() {

        String nextEntity = request.getParameter("goEntity");

        if (nextEntity == null || "".equals(nextEntity)) {

            String entityID =
                (String) getRequest().getSession()
                        .getAttribute("entityID");

            EntityFormHandler entityForm =
                (EntityFormHandler) getRequest()
                        .getSession()
                        .getAttribute(entityID + "_session");

            Vector errorVect = entityForm.getFormErrors();
            String moduleID = entityForm.getMainModuleID();

            HashMap mainRequestData =
                EAFManualUtil.getDataHashMapFromSession(
                        moduleID, request);

            String requestID =
                (String) mainRequestData.get("REQUEST_ID");

            try {
                int inCompleteStock =
                    ManualDAOFactory.getManualDocOrderDAO()
                        .countInCompleteInStock(requestID);

                if (inCompleteStock > 0) {
                    errorVect.add("Please verify supplier stock");
                    return false;
                }
            } catch (Exception e) {
                errorVect.add(e.getMessage());
                return false;
            }
        }
        return super.validateResult();
    }
}
```

***

### Step 4: Deploy Java Classes

1. From the **ONEWEB workspace**, export `EafMasterEar.ear`
2. Deploy the EAR file to the server

All updated classes from Steps **1–3** will now be available.

***

### Step 5: Test the Validation

1. Log in to **FrontWeb** as an **Admin user**
2. Go to **To Do List**
3. Claim a job
4. Click **Save** or **Submit**

#### Expected Result

* If at least one order item has:

  ```
  SUM(In Stock) < Unit
  ```
* The system blocks submission
* Error message is displayed:

  ```
  Please verify supplier stock.
  ```

***

### Summary

In this section, you have:

* Added DAO‑level validation for supplier stock availability
* Queried and aggregated supplier data directly from the database
* Integrated validation logic into the Admin submit action
* Ensured that purchase decisions cannot proceed with insufficient stock

This validation completes the **Admin‑level data integrity checks** in the **Order Management App (Web)** and ensures that orders move forward only when suppliers can fulfill the requested quantities.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.onewebstack.com/oneweb-platform-th/tutorials-examples/order-management-app-web/app-designer-customize/validate-supplier-details-when-admin-submits.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
