Advertisement
tko_pb

IdolMartGenerateProductSearchKey4September

Sep 3rd, 2018
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.87 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.  
  31.         ProductCategory pcSubId = OBDal.getInstance().get(ProductCategory.class, strProductCategoryId);
  32.         String subKey = pcSubId.getSearchKey();
  33.         String[] subKeyParts = subKey.split("-");
  34.         String SK = subKeyParts[0].trim();
  35.  
  36.         //get key from main category
  37.         OBCriteria<TreeNode> treeNodeCriteria = OBDal.getInstance().createCriteria(TreeNode.class);
  38.         treeNodeCriteria.add(Restrictions.eq(TreeNode.PROPERTY_TREE, adTree));
  39.         treeNodeCriteria.add(Restrictions.eq(TreeNode.PROPERTY_NODE, strProductCategoryId));
  40.  
  41.         TreeNode treeNode = treeNodeCriteria.list().get(0);
  42.         String pcMainId = treeNode.getReportSet();
  43.         if (pcMainId.equalsIgnoreCase("0")) {
  44.             pcMainId = subKey;
  45.         }
  46.         ProductCategory pcMain = OBDal.getInstance().get(ProductCategory.class, pcMainId);
  47.         String mainKey = pcMain.getSearchKey();
  48.         String[] mainKeyParts = mainKey.split("-");
  49.         String MK = mainKeyParts[0].trim();    
  50.                
  51.         if (pcMain.getOezProductsequence() == null) {
  52.             //get sequence number
  53.             String sql = "select count(*) as jumlahproduct" +
  54.                     " from ad_treenode a" +
  55.                     " inner join m_product b on b.m_product_category_id=a.node_id" +
  56.                     " where a.parent_id=? " +
  57.                     " and a.ad_tree_id=? "  +
  58.                     " and b.m_product_category_id=? ";
  59.             java.sql.Connection conn = OBDal.getInstance().getConnection();
  60.             try {
  61.                 PreparedStatement ps = conn.prepareStatement(sql);
  62.                 ps.setString(1, pcMainId);
  63.                 ps.setString(2, adTree.getId());
  64.                 ps.setString(3, strProductCategoryId);
  65.                 ResultSet rs = ps.executeQuery();
  66.                 int jumlahProduct = 0;
  67.                 while (rs.next()) {
  68.                     jumlahProduct = rs.getInt("jumlahProduct");
  69.                 }
  70.                 jumlahProduct++;
  71.  
  72.                 DecimalFormat myFormatter = new DecimalFormat("00000000");
  73.                 String strJumlahProduct = myFormatter.format(jumlahProduct);
  74.                
  75.                 //set value product sequence on tabel
  76.                 Long JumlahProductLong = Long.valueOf(jumlahProduct);
  77.                 pcMain.setOezProductsequence(JumlahProductLong);
  78.                 OBDal.getInstance().save(pcMain);
  79.                        
  80.                 //put to field search key window product
  81.                 String searchKey = myPreference+MK+SK+strJumlahProduct;
  82.                 return searchKey;
  83.             }
  84.             catch (Exception e) {
  85.                 e.printStackTrace();
  86.                 throw new OBException(e);
  87.             }
  88.         }
  89.         else
  90.         {
  91.             long productSequence = pcMain.getOezProductsequence();
  92.             long addProductSequence = productSequence + 1;
  93.             pcMain.setOezProductsequence(addProductSequence);
  94.             OBDal.getInstance().save(pcMain);
  95.            
  96.             DecimalFormat myFormatter = new DecimalFormat("00000000");
  97.             String productSequenceString = myFormatter.format(addProductSequence);
  98.             String searchKey = myPreference+MK+SK+productSequenceString;
  99.             return searchKey;
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement