package devguide.examples.appmodules;

import devguide.examples.entities.OrdersEOImpl;

import devguide.examples.entities.PersonsEOImpl;
import devguide.examples.entities.ProductsBaseEOImpl;

import oracle.jbo.ApplicationModule;
import oracle.jbo.JboException;
import oracle.jbo.Key;
import oracle.jbo.client.Configuration;
import oracle.jbo.domain.DBSequence;
import oracle.jbo.domain.Number;
import oracle.jbo.server.ApplicationModuleImpl;
import oracle.jbo.server.EntityDefImpl;

// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
/**
 * This custom application module class illustrates the same
 * example methods as StoreFrontServiceImpl.java, except that here
 * we're using the strongly-typed custom Entity Java classes
 * OrderEOImpl, PersonsEOImpl, and ProductsBaseEOImpl instead of working
 * with all the entity objects using the base EntityImpl class.
 */

public class StoreFrontService2Impl extends ApplicationModuleImpl {
    /**This is the default constructor (do not remove).
     */
     public StoreFrontService2Impl() {
     }
     /*
      * Helper method to return an Order by Id
      */
     private OrdersEOImpl retrieveOrderById(long orderId) {
       EntityDefImpl orderDef = OrdersEOImpl.getDefinitionObject();
       Key orderKey = OrdersEOImpl.createPrimaryKey(new DBSequence(orderId));
       return (OrdersEOImpl)orderDef.findByPrimaryKey(getDBTransaction(),orderKey);
     }

     /*
      * Find an Order by Id
      */
     public String findOrderTotal(long orderId) {
       OrdersEOImpl order = retrieveOrderById(orderId);
       if (order != null) {
         return order.getOrderTotal().toString();
       }
       return null;
     }
     
     /*
      * Create a new Product and Return its new id
      */
     public long createProduct(String name, String status, String shipCode) {
       EntityDefImpl productDef = ProductsBaseEOImpl.getDefinitionObject();
       ProductsBaseEOImpl newProduct = (ProductsBaseEOImpl)productDef.createInstance2(getDBTransaction(),null);
         newProduct.setProductName(name);
         newProduct.setProductStatus(status);
         newProduct.setShippingClassCode(shipCode);
         newProduct.setSupplierId(new Number(100));
         newProduct.setListPrice(new Number(499));
         newProduct.setMinPrice(new Number(479));
         newProduct.setCreatedBy("Test Client");
         newProduct.setLastUpdatedBy("Test Client");
         newProduct.setCategoryId(new Number(5));
       try {
         getDBTransaction().commit();
       }
       catch (JboException ex) {
         getDBTransaction().rollback();
         throw ex;
       }
       DBSequence newIdAssigned = newProduct.getProductId();
       return newIdAssigned.getSequenceNumber().longValue();
     }
     /*
      * Update the status of an existing order
      */  
     public void updateRequestStatus(long orderId, String newStatus) {
       OrdersEOImpl order = retrieveOrderById(orderId);
       if (order != null) {
         order.setOrderStatusCode(newStatus);
         try {
           getDBTransaction().commit();
         }
         catch (JboException ex) {
           getDBTransaction().rollback();
           throw ex;
         }
       }
     }
     
     /*
      * Access an associated Customer entity from the Order entity
      */  
     public String findOrderCustomer(long orderId) {
       OrdersEOImpl order = retrieveOrderById(orderId);
       if (order != null) {
         PersonsEOImpl cust  = (PersonsEOImpl)order.getPersonsEO();
         if (cust != null) {
           return cust.getFirstName() + " " + cust.getLastName();
         }
         else {
           return "Unassigned";
         }
       }
       else {
         return null;
       }
     }  
    
      /*
       * Testing method
       */
         public static void main(String[] args) {
           String        amDef = "devguide.model.StoreFrontService";
           String        config = "StoreFrontServiceLocal";
           ApplicationModule am = Configuration.createRootApplicationModule(amDef,config);
           /* 
            * NOTE: This cast to use the SRServiceImpl class is OK since this
            * ----  code is inside a business tier *Impl.java file and not in a
            *       client class that is accessing the business tier from "outside".
            */
           StoreFrontServiceImpl service = (StoreFrontServiceImpl)am;
           String total = service.findOrderTotal(1011);
           System.out.println("Status of Order # 1011 = " + total);
           String customerName = service.findOrderCustomer(1011);
           System.out.println("Customer for Order # 1011 = " + customerName);
           try {
               service.updateOrderStatus(1011,"CANCEL");
           }
           catch (JboException ex) {
             System.out.println("ERROR: "+ex.getMessage());
           }
           long id = 0;
           try {
               id = service.createProduct(null, "NEW", "CLASS1");
           }
           catch (JboException ex) {
             System.out.println("ERROR: "+ex.getMessage());
           }
           id = service.createProduct("Canon PowerShot G9", "NEW", "CLASS1");
           System.out.println("New product created successfully with id = "+id);
           Configuration.releaseRootApplicationModule(am,true);
         }
     }
