Rahmadnet

working with DAL part 2

Oct 3rd, 2019
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.05 KB | None | 0 0
  1. package org.rahmadnet.learning.query;
  2.  
  3. import java.math.BigDecimal;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. import org.hibernate.Session;
  8. import org.hibernate.query.Query;
  9. import org.junit.Test;
  10. import org.openbravo.dal.service.OBDal;
  11. import org.openbravo.dal.service.OBQuery;
  12. import org.openbravo.model.common.order.OrderLine;
  13. import org.openbravo.test.base.OBBaseTest;
  14.  
  15. public class Query_Test extends OBBaseTest
  16. {  
  17.     @Test
  18.     public void testQueryTwo()
  19.     {
  20.         // this sets the user to the Openbravo Admin
  21.         setUserContext("100");
  22.          
  23.         // create the where clause
  24.         final StringBuilder whereClause = new StringBuilder();
  25.         // set the alias, the OrderLine will be added by the DAL
  26.         whereClause.append(" as ol ");
  27.        
  28.         // Other filtering options:
  29.         // AND C_ORDER.AD_CLIENT_ID IN ('1') <-- these are done automatically by the dal
  30.         // AND C_ORDER.AD_ORG_ID IN ('1') <-- these are done automatically by the dal
  31.      
  32.         // AND C_ORDER.ISSOTRX='N'
  33.         whereClause.append(" where ol.salesOrder.salesTransaction=false ");
  34.        
  35.        
  36.         // AND C_BPARTNER.C_BPARTNER_ID = ?
  37.         // note the value of the parameter is set below
  38.         whereClause.append(" and ol.salesOrder.businessPartner.id=:bpId ");
  39.  
  40.         // AND C_ORDER.DOCSTATUS = 'CO'
  41.         whereClause.append(" and ol.salesOrder.documentStatus='CO' ");
  42.  
  43.         // ORDER BY C_BPARTNER_ID, ID
  44.         whereClause.append(" order by ol.salesOrder.businessPartner.id, ol.id");
  45.        
  46.        
  47.         // final Session session = OBDal.getInstance().getSession();
  48.         // session.createQuery(hql.toString());
  49.         final OBQuery<OrderLine> qry = OBDal.getInstance().createQuery(OrderLine.class,
  50.             whereClause.toString());
  51.      
  52.         // set the business partner parameter
  53.         final Map<String, Object> parameters = new HashMap<>(1);
  54.         parameters.put("bpId", "2E7F35F3ACE94769BAD7A99BF71691B1");
  55.         qry.setNamedParameters(parameters);
  56.  
  57.         for (OrderLine ol : qry.list()) {
  58.              
  59.             // C_ORDERLINE.C_ORDERLINE_ID AS ID, C_ORDER.C_ORDER_ID AS C_ORDER_ID
  60.             System.err.println(ol.getId());
  61.             System.err.println(ol.getSalesOrder().getId());
  62.        
  63.             // C_ORDER.DOCUMENTNO AS DOCUMENTNO, C_ORDER.DATEORDERED AS DATEORDERED,
  64.             System.err.println(ol.getSalesOrder().getDocumentNo());
  65.             System.err.println(ol.getSalesOrder().getOrderDate());
  66.        
  67.             // C_BPARTNER.C_BPARTNER_ID AS C_BPARTNER_ID, C_BPARTNER.NAME AS PARTNER_NAME,
  68.             System.err.println(ol.getSalesOrder().getBusinessPartner().getId());
  69.             System.err.println(ol.getSalesOrder().getBusinessPartner().getName());
  70.        
  71.             // AD_COLUMN_IDENTIFIER(TO_CHAR('M_Product'), TO_CHAR(C_ORDERLINE.M_PRODUCT_ID),
  72.             // TO_CHAR(?)) AS PRODUCT_NAME,
  73.             System.err.println(ol.getProduct().getIdentifier());
  74.             System.err.println(ol.getProduct().getId());
  75.             System.err.println(ol.getProduct().getName());
  76.        
  77.             // M_ATTRIBUTESETINSTANCE.DESCRIPTION AS DESCRIPTION,
  78.             if (ol.getAttributeSetValue() != null) {
  79.               System.err.println(ol.getAttributeSetValue().getDescription());
  80.             }
  81.        
  82.             // C_ORDERLINE.QTYORDERED AS TOTAL_QTY,
  83.             System.err.println(ol.getOrderedQuantity());
  84.             // C_ORDERLINE.QTYORDERED-SUM(COALESCE(M_MATCHPO.QTY,0)) AS QTYORDERED, '-1' AS ISACTIVE
  85.             // todo this we have to repeat the sum query, we use direct hql for this
  86.             final String hql = "select sum(quantity) from ProcurementPOInvoiceMatch "
  87.                 + "where goodsShipmentLine is not null and salesOrderLine=:orderLine";
  88.             final Session session = OBDal.getInstance().getSession();
  89.             final Query query = session.createQuery(hql);
  90.             query.setParameter("orderLine", ol);
  91.             final BigDecimal sum = (BigDecimal) query.uniqueResult();
  92.             System.err.println(sum);
  93.       }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment