Advertisement
tko_pb

IdolMartGenerateProductSearchKey 28 AGUSTUS

Aug 27th, 2018
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.45 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.ProductCategory;
  18.  
  19. public class IdolMartGenerateProductSearchKey implements GenerateProductSearchKey{
  20.  
  21.     public String getProductSearchKey(CalloutInfo info) {
  22.         //get key from sub category
  23.         String strProductCategoryId = info.getStringParameter("inpmProductCategoryId", null);
  24.  
  25.         // get preference
  26.         String myPreference = Utility.getPreference(info.vars, "IdolMartPreference", null);
  27.  
  28.         ClientInformation ClientInfo = OBContext.getOBContext().getCurrentClient().getClientInformationList().get(0);
  29.         Tree adTree = ClientInfo.getPrimaryTreeProductCategory();
  30.         String adTreeId = adTree.getId();
  31.        
  32.         ProductCategory pcSubId = OBDal.getInstance().get(ProductCategory.class, strProductCategoryId);
  33.         String subKey = pcSubId.getSearchKey();
  34.         String[] subKeyParts = subKey.split("-");
  35.         String SK = subKeyParts[0].trim();
  36.  
  37.         //get key from main category
  38.         OBCriteria<TreeNode> treeNodeCriteria = OBDal.getInstance().createCriteria(TreeNode.class);
  39.         treeNodeCriteria.add(Restrictions.eq(TreeNode.PROPERTY_TREE, adTree));
  40.         treeNodeCriteria.add(Restrictions.eq(TreeNode.PROPERTY_NODE, strProductCategoryId));
  41.  
  42.         TreeNode treeNode = treeNodeCriteria.list().get(0);
  43.         String pcMainId = treeNode.getReportSet();
  44.         if (pcMainId.equalsIgnoreCase("0")) {
  45.             pcMainId = subKey;
  46.         }
  47.         ProductCategory pcMain = OBDal.getInstance().get(ProductCategory.class, pcMainId);
  48.         String mainKey = pcMain.getSearchKey();
  49.         String[] mainKeyParts = mainKey.split("-");
  50.         String MK = mainKeyParts[0].trim();    
  51.  
  52.         String MaxSquenceSql = " select max(em_oez_productsequence) as maxSquence" +
  53.                             " from m_product " +
  54.                             " where m_product_category_id=?";
  55.         java.sql.Connection connMaxSquence = OBDal.getInstance().getConnection();
  56.         try {
  57.             PreparedStatement psMaxSquence = connMaxSquence.prepareStatement(MaxSquenceSql);
  58.             psMaxSquence.setString(1, strProductCategoryId);
  59.             ResultSet rsMaxSquence = psMaxSquence.executeQuery();
  60.             int maxSquenceProduct = 0;
  61.             while (rsMaxSquence.next()) {
  62.                 maxSquenceProduct = rsMaxSquence.getInt("maxSquence");             
  63.             }
  64.             boolean maxSquenceIsNull = maxSquenceProduct == 0;
  65.            
  66.             if (maxSquenceIsNull) {
  67.                 //put to field search key window product
  68.                 String strJumlahProduct = generateSkuSquenceIsNull(pcMainId, adTreeId, strProductCategoryId, pcMain);
  69.                 String searchKey = myPreference+MK+SK+strJumlahProduct;
  70.                 return searchKey;
  71.             }
  72.             else {
  73.                 return mainKey;
  74.             }
  75.         }
  76.         catch (Exception e) {
  77.             e.printStackTrace();
  78.             // TODO: handle exception
  79.         }
  80.     }
  81.  
  82.    
  83.     public String generateSkuSquenceIsNull (String pcMainId, String adTreeId, String strProductCategoryId, ProductCategory pcMain) {
  84.         //get sequence number
  85.                 String sql = "select count(*) as jumlahproduct" +
  86.                         " from ad_treenode a" +
  87.                         " inner join m_product b on b.m_product_category_id=a.node_id" +
  88.                         " where a.parent_id=? " +
  89.                         " and a.ad_tree_id=? "  +
  90.                         " and b.m_product_category_id=? ";
  91.                 java.sql.Connection conn = OBDal.getInstance().getConnection();
  92.                 try {
  93.                     PreparedStatement ps = conn.prepareStatement(sql);
  94.                     ps.setString(1, pcMainId);
  95.                     ps.setString(2, adTreeId);
  96.                     ps.setString(3, strProductCategoryId);
  97.                     ResultSet rs = ps.executeQuery();
  98.                     int jumlahProduct = 0;
  99.                     while (rs.next()) {
  100.                         jumlahProduct = rs.getInt("jumlahProduct");
  101.                     }
  102.                     jumlahProduct++;
  103.  
  104.                     DecimalFormat myFormatter = new DecimalFormat("00000000");
  105.                     String strJumlahProduct = myFormatter.format(jumlahProduct);
  106.  
  107.                     //set value product sequence on tabel
  108.                     Long JumlahProductLong = Long.valueOf(jumlahProduct);
  109.  
  110.                     OBDal.getInstance().save(pcMain);
  111.                     OBDal.getInstance().getConnection().commit();
  112.                     return strJumlahProduct;
  113.                 }
  114.                 catch (Exception e) {
  115.                     e.printStackTrace();
  116.                     throw new OBException(e);
  117.                 }
  118.  
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement