tko_pb

IdolMartGenerateProductSearchKey5September

Sep 5th, 2018
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.09 KB | None | 0 0
  1. package org.wirabumi.gen.oez.callout;
  2.  
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.text.DecimalFormat;
  6.  
  7. import org.hibernate.criterion.Restrictions;
  8. import org.openbravo.base.exception.OBException;
  9. import org.openbravo.dal.core.OBContext;
  10. import org.openbravo.dal.service.OBCriteria;
  11. import org.openbravo.dal.service.OBDal;
  12. import org.openbravo.erpCommon.ad_callouts.SimpleCallout.CalloutInfo;
  13. import org.openbravo.erpCommon.utility.Utility;
  14. import org.openbravo.model.ad.system.ClientInformation;
  15. import org.openbravo.model.ad.utility.Tree;
  16. import org.openbravo.model.ad.utility.TreeNode;
  17. import org.openbravo.model.common.plm.Product;
  18. import org.openbravo.model.common.plm.ProductCategory;
  19.  
  20. public class IdolMartGenerateProductSearchKey implements GenerateProductSearchKey{
  21.  
  22.     public String getProductSearchKey(CalloutInfo info) {
  23.         //get key from sub category
  24.         String strProductCategoryId = info.getStringParameter("inpmProductCategoryId", null);
  25.  
  26.         // get preference
  27.         String myPreference = Utility.getPreference(info.vars, "IdolMartPreference", null);
  28.  
  29.         ClientInformation ClientInfo = OBContext.getOBContext().getCurrentClient().getClientInformationList().get(0);
  30.         Tree adTree = ClientInfo.getPrimaryTreeProductCategory();
  31.         String adTreeId = adTree.getId();
  32.  
  33.         //get Product ID
  34.         String strProductId = info.getStringParameter("inpmProductId",null);
  35.         Product productMain = OBDal.getInstance().get(Product.class, strProductId);
  36.  
  37.         //get product category ID
  38.         ProductCategory pcSubId = OBDal.getInstance().get(ProductCategory.class, strProductCategoryId);
  39.         String subKey = pcSubId.getSearchKey();
  40.         String[] subKeyParts = subKey.split("-");
  41.         String SK = subKeyParts[0].trim();
  42.  
  43.         //get key from main category
  44.         OBCriteria<TreeNode> treeNodeCriteria = OBDal.getInstance().createCriteria(TreeNode.class);
  45.         treeNodeCriteria.add(Restrictions.eq(TreeNode.PROPERTY_TREE, adTree));
  46.         treeNodeCriteria.add(Restrictions.eq(TreeNode.PROPERTY_NODE, strProductCategoryId));
  47.  
  48.         TreeNode treeNode = treeNodeCriteria.list().get(0);
  49.         String pcMainId = treeNode.getReportSet();
  50.         if (pcMainId.equalsIgnoreCase("0")) {
  51.             pcMainId = subKey;
  52.         }
  53.         ProductCategory pcMain = OBDal.getInstance().get(ProductCategory.class, pcMainId);
  54.         String mainKey = pcMain.getSearchKey();
  55.         String[] mainKeyParts = mainKey.split("-");
  56.         String MK = mainKeyParts[0].trim();    
  57.  
  58.         String MaxSquenceSql = " select max(em_oez_productsequence) as maxSquence" +
  59.                 " from m_product " +
  60.                 " where m_product_category_id=?";
  61.         java.sql.Connection connMaxSquence = OBDal.getInstance().getConnection();
  62.         String searchKey = null;
  63.         try {
  64.             PreparedStatement psMaxSquence = connMaxSquence.prepareStatement(MaxSquenceSql);
  65.             psMaxSquence.setString(1, strProductCategoryId);
  66.             ResultSet rsMaxSquence = psMaxSquence.executeQuery();
  67.             int maxSquenceProduct = 0;
  68.             while (rsMaxSquence.next()) {
  69.                 maxSquenceProduct = rsMaxSquence.getInt("maxSquence");             
  70.             }
  71.             boolean maxSquenceIsNull = maxSquenceProduct == 0;
  72.  
  73.             if (maxSquenceIsNull) {
  74.                 //put to field search key window product
  75.                 String strJumlahProduct = generateSkuSquenceIsNull(pcMainId, adTreeId, strProductCategoryId, pcMain, productMain, info);
  76.                 searchKey = myPreference+MK+SK+strJumlahProduct;
  77.             }
  78.             else {
  79.                 //add from last squence
  80.                 int addMaxSquence = maxSquenceProduct + 1;
  81.        
  82.                 //save squence to product
  83.                 Long addMaxSquenceLong = Long.valueOf(addMaxSquence);
  84.                 info.addResult("inpemOezProductsequence", addMaxSquenceLong);
  85.                
  86.                 DecimalFormat myFormatter = new DecimalFormat("00000000");
  87.                 String productSequenceString = myFormatter.format(addMaxSquenceLong);
  88.                 searchKey = myPreference+MK+SK+productSequenceString;
  89.             }
  90.         }
  91.         catch (Exception e) {
  92.             e.printStackTrace();
  93.             // TODO: handle exception
  94.         }
  95.         return searchKey;
  96.     }
  97.  
  98.  
  99.     public String generateSkuSquenceIsNull (String pcMainId, String adTreeId, String strProductCategoryId, ProductCategory pcMain, Product productMain, CalloutInfo info) {
  100.         //get sequence number
  101.         String sql = "select count(*) as jumlahproduct" +
  102.                 " from ad_treenode a" +
  103.                 " inner join m_product b on b.m_product_category_id=a.node_id" +
  104.                 " where a.parent_id=? " +
  105.                 " and a.ad_tree_id=? "  +
  106.                 " and b.m_product_category_id=? ";
  107.         java.sql.Connection conn = OBDal.getInstance().getConnection();
  108.         try {
  109.             PreparedStatement ps = conn.prepareStatement(sql);
  110.             ps.setString(1, pcMainId);
  111.             ps.setString(2, adTreeId);
  112.             ps.setString(3, strProductCategoryId);
  113.             ResultSet rs = ps.executeQuery();
  114.             int jumlahProduct = 0;
  115.             while (rs.next()) {
  116.                 jumlahProduct = rs.getInt("jumlahProduct");
  117.             }
  118.             jumlahProduct++;
  119.  
  120.             DecimalFormat myFormatter = new DecimalFormat("00000000");
  121.             String strJumlahProduct = myFormatter.format(jumlahProduct);
  122.            
  123.             //set value product sequence on tabel
  124.             Long JumlahProductLong = Long.valueOf(jumlahProduct);
  125.             info.addResult("inpemOezProductsequence", JumlahProductLong);
  126.             return strJumlahProduct;
  127.         }
  128.         catch (Exception e) {
  129.             e.printStackTrace();
  130.             throw new OBException(e);
  131.         }
  132.  
  133.     }
  134. }
Add Comment
Please, Sign In to add comment