Advertisement
tko_pb

IdolMartGenerateProductSearchKey.java

Oct 1st, 2018
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.03 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.         // get adTreeId
  29.         ClientInformation clientInfo = OBContext.getOBContext().getCurrentClient().getClientInformationList().get(0);
  30.         Tree adTree = clientInfo.getPrimaryTreeProductCategory();
  31.         String adTreeId = adTree.getId();
  32.  
  33.         // getsubKey (SK)
  34.         ProductCategory pcSubKeyId = OBDal.getInstance().get(ProductCategory.class, strProductCategoryId);
  35.         String subKey = pcSubKeyId.getSearchKey(); //getSubKey
  36.         String sK = getKeyNumber(pcSubKeyId);
  37.  
  38.         //get treenode
  39.         TreeNode treeNode = GetTreeNodeForProduct(adTree, strProductCategoryId).list().get(0);
  40.         String pcMainId = treeNode.getReportSet();
  41.         if (pcMainId.equalsIgnoreCase("0")) {
  42.             pcMainId = subKey;
  43.         }
  44.  
  45.         // get MainKey (MK)
  46.         ProductCategory pcMain = OBDal.getInstance().get(ProductCategory.class, pcMainId);
  47.         String mK = getKeyNumber(pcMain); //GetSplitString()
  48.  
  49.         // return to searchKey
  50.         String searchKey = GetGenerateSearchKey(pcMainId, adTreeId, strProductCategoryId, pcMain, info, myPreference, mK, sK);
  51.         return searchKey;
  52.     }
  53.  
  54.     // get getKeyNumber
  55.     private String getKeyNumber (ProductCategory productCategoryId) {
  56.         String Key = productCategoryId.getSearchKey();
  57.         String[] hasilSplit = Key.split("-");
  58.         String finalSplit = hasilSplit[0].trim();
  59.         return finalSplit;
  60.     }
  61.  
  62.     // getMaxSquence
  63.     private int GetMaxSquence (String strProductCategoryId) {
  64.         String maxSquenceSql = " select max(em_oez_productsequence) as maxSquence" +
  65.                 " from m_product " +
  66.                 " where m_product_category_id=?";
  67.         java.sql.Connection connMaxSquence = OBDal.getInstance().getConnection();
  68.         int maxSquenceProduct = 0;
  69.         try {
  70.             PreparedStatement psMaxSquence = connMaxSquence.prepareStatement(maxSquenceSql);
  71.             psMaxSquence.setString(1, strProductCategoryId);
  72.             ResultSet rsMaxSquence = psMaxSquence.executeQuery();
  73.  
  74.             while (rsMaxSquence.next()) {
  75.                 maxSquenceProduct = rsMaxSquence.getInt("maxSquence");             
  76.             }
  77.         }
  78.         catch (Exception e) {
  79.             e.printStackTrace();
  80.             // TODO: handle exception
  81.         }
  82.         return maxSquenceProduct;
  83.     }
  84.  
  85.     // jika squence product kosong maka bentuk squence baru
  86.     private String GenerateSkuSquenceIsNull (String pcMainId, String adTreeId, String strProductCategoryId, ProductCategory pcMain, CalloutInfo info) {
  87.  
  88.         int jumlahProduct = GetJumlahProduct(pcMainId, adTreeId, strProductCategoryId);
  89.         DecimalFormat myFormatter = new DecimalFormat("00000000");
  90.         String strJumlahProduct = myFormatter.format(jumlahProduct);
  91.  
  92.         //set value product sequence on tabel
  93.         Long jumlahProductLong = Long.valueOf(jumlahProduct);
  94.         info.addResult("inpemOezProductsequence", jumlahProductLong);
  95.         return strJumlahProduct;
  96.     }
  97.  
  98.     // get jumlah prouduct
  99.     private int GetJumlahProduct(String pcMainId, String adTreeId, String strProductCategoryId) {
  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.             return jumlahProduct;
  121.         }
  122.         catch (Exception e) {
  123.             e.printStackTrace();
  124.             throw new OBException(e);
  125.         }
  126.  
  127.     }
  128.  
  129.     //GetTreeNodeForProduct
  130.     private  OBCriteria<TreeNode> GetTreeNodeForProduct (Tree adTree, String strProductCategoryId){
  131.         OBCriteria<TreeNode> treeNodeCriteria = OBDal.getInstance().createCriteria(TreeNode.class);
  132.         treeNodeCriteria.add(Restrictions.eq(TreeNode.PROPERTY_TREE, adTree));
  133.         treeNodeCriteria.add(Restrictions.eq(TreeNode.PROPERTY_NODE, strProductCategoryId));
  134.         return treeNodeCriteria;
  135.     }
  136.  
  137.     // getGenerateSearchKey
  138.     private String GetGenerateSearchKey (String pcMainId, String adTreeId, String strProductCategoryId, ProductCategory pcMain, CalloutInfo info, String myPreference, String MK, String SK) {
  139.         int maxSquenceProduct = GetMaxSquence(strProductCategoryId);
  140.         String searchKey = null;
  141.         boolean maxSquenceIsNull = maxSquenceProduct == 0;
  142.  
  143.         if (maxSquenceIsNull) {
  144.             //put to field search key window product
  145.             String strJumlahProduct = GenerateSkuSquenceIsNull(pcMainId, adTreeId, strProductCategoryId, pcMain, info);
  146.             searchKey = myPreference+MK+SK+strJumlahProduct;
  147.         }
  148.         else {
  149.             //add from last squence
  150.             int addMaxSquence = maxSquenceProduct + 1;
  151.  
  152.             //save squence to product
  153.             Long addMaxSquenceLong = Long.valueOf(addMaxSquence);
  154.             info.addResult("inpemOezProductsequence", addMaxSquenceLong);
  155.  
  156.             DecimalFormat myFormatter = new DecimalFormat("00000000");
  157.             String productSequenceString = myFormatter.format(addMaxSquenceLong);
  158.             searchKey = myPreference+MK+SK+productSequenceString;
  159.         }
  160.         return searchKey;
  161.     }
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement