Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.wirabumi.gen.oez.callout;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.text.DecimalFormat;
- import org.hibernate.criterion.Restrictions;
- import org.openbravo.base.exception.OBException;
- import org.openbravo.dal.core.OBContext;
- import org.openbravo.dal.service.OBCriteria;
- import org.openbravo.dal.service.OBDal;
- import org.openbravo.erpCommon.ad_callouts.SimpleCallout.CalloutInfo;
- import org.openbravo.erpCommon.utility.Utility;
- import org.openbravo.model.ad.system.ClientInformation;
- import org.openbravo.model.ad.utility.Tree;
- import org.openbravo.model.ad.utility.TreeNode;
- import org.openbravo.model.common.plm.ProductCategory;
- public class IdolMartGenerateProductSearchKey implements GenerateProductSearchKey{
- public String getProductSearchKey(CalloutInfo info) {
- //get key from sub category
- String strProductCategoryId = info.getStringParameter("inpmProductCategoryId", null);
- // get preference
- String myPreference = Utility.getPreference(info.vars, "IdolMartPreference", null);
- // get adTreeId
- ClientInformation clientInfo = OBContext.getOBContext().getCurrentClient().getClientInformationList().get(0);
- Tree adTree = clientInfo.getPrimaryTreeProductCategory();
- String adTreeId = adTree.getId();
- // getsubKey (SK)
- ProductCategory pcSubKeyId = OBDal.getInstance().get(ProductCategory.class, strProductCategoryId);
- String subKey = pcSubKeyId.getSearchKey(); //getSubKey
- String sK = getKeyNumber(pcSubKeyId);
- //get treenode
- TreeNode treeNode = GetTreeNodeForProduct(adTree, strProductCategoryId).list().get(0);
- String pcMainId = treeNode.getReportSet();
- if (pcMainId.equalsIgnoreCase("0")) {
- pcMainId = subKey;
- }
- // get MainKey (MK)
- ProductCategory pcMain = OBDal.getInstance().get(ProductCategory.class, pcMainId);
- String mK = getKeyNumber(pcMain); //GetSplitString()
- // return to searchKey
- String searchKey = GetGenerateSearchKey(pcMainId, adTreeId, strProductCategoryId, pcMain, info, myPreference, mK, sK);
- return searchKey;
- }
- // get getKeyNumber
- private String getKeyNumber (ProductCategory productCategoryId) {
- String Key = productCategoryId.getSearchKey();
- String[] hasilSplit = Key.split("-");
- String finalSplit = hasilSplit[0].trim();
- return finalSplit;
- }
- // getMaxSquence
- private int GetMaxSquence (String strProductCategoryId) {
- String maxSquenceSql = " select max(em_oez_productsequence) as maxSquence" +
- " from m_product " +
- " where m_product_category_id=?";
- java.sql.Connection connMaxSquence = OBDal.getInstance().getConnection();
- int maxSquenceProduct = 0;
- try {
- PreparedStatement psMaxSquence = connMaxSquence.prepareStatement(maxSquenceSql);
- psMaxSquence.setString(1, strProductCategoryId);
- ResultSet rsMaxSquence = psMaxSquence.executeQuery();
- while (rsMaxSquence.next()) {
- maxSquenceProduct = rsMaxSquence.getInt("maxSquence");
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- // TODO: handle exception
- }
- return maxSquenceProduct;
- }
- // jika squence product kosong maka bentuk squence baru
- private String GenerateSkuSquenceIsNull (String pcMainId, String adTreeId, String strProductCategoryId, ProductCategory pcMain, CalloutInfo info) {
- int jumlahProduct = GetJumlahProduct(pcMainId, adTreeId, strProductCategoryId);
- DecimalFormat myFormatter = new DecimalFormat("00000000");
- String strJumlahProduct = myFormatter.format(jumlahProduct);
- //set value product sequence on tabel
- Long jumlahProductLong = Long.valueOf(jumlahProduct);
- info.addResult("inpemOezProductsequence", jumlahProductLong);
- return strJumlahProduct;
- }
- // get jumlah prouduct
- private int GetJumlahProduct(String pcMainId, String adTreeId, String strProductCategoryId) {
- //get sequence number
- String sql = "select count(*) as jumlahproduct" +
- " from ad_treenode a" +
- " inner join m_product b on b.m_product_category_id=a.node_id" +
- " where a.parent_id=? " +
- " and a.ad_tree_id=? " +
- " and b.m_product_category_id=? ";
- java.sql.Connection conn = OBDal.getInstance().getConnection();
- try {
- PreparedStatement ps = conn.prepareStatement(sql);
- ps.setString(1, pcMainId);
- ps.setString(2, adTreeId);
- ps.setString(3, strProductCategoryId);
- ResultSet rs = ps.executeQuery();
- int jumlahProduct = 0;
- while (rs.next()) {
- jumlahProduct = rs.getInt("jumlahProduct");
- }
- jumlahProduct++;
- return jumlahProduct;
- }
- catch (Exception e) {
- e.printStackTrace();
- throw new OBException(e);
- }
- }
- //GetTreeNodeForProduct
- private OBCriteria<TreeNode> GetTreeNodeForProduct (Tree adTree, String strProductCategoryId){
- OBCriteria<TreeNode> treeNodeCriteria = OBDal.getInstance().createCriteria(TreeNode.class);
- treeNodeCriteria.add(Restrictions.eq(TreeNode.PROPERTY_TREE, adTree));
- treeNodeCriteria.add(Restrictions.eq(TreeNode.PROPERTY_NODE, strProductCategoryId));
- return treeNodeCriteria;
- }
- // getGenerateSearchKey
- private String GetGenerateSearchKey (String pcMainId, String adTreeId, String strProductCategoryId, ProductCategory pcMain, CalloutInfo info, String myPreference, String MK, String SK) {
- int maxSquenceProduct = GetMaxSquence(strProductCategoryId);
- String searchKey = null;
- boolean maxSquenceIsNull = maxSquenceProduct == 0;
- if (maxSquenceIsNull) {
- //put to field search key window product
- String strJumlahProduct = GenerateSkuSquenceIsNull(pcMainId, adTreeId, strProductCategoryId, pcMain, info);
- searchKey = myPreference+MK+SK+strJumlahProduct;
- }
- else {
- //add from last squence
- int addMaxSquence = maxSquenceProduct + 1;
- //save squence to product
- Long addMaxSquenceLong = Long.valueOf(addMaxSquence);
- info.addResult("inpemOezProductsequence", addMaxSquenceLong);
- DecimalFormat myFormatter = new DecimalFormat("00000000");
- String productSequenceString = myFormatter.format(addMaxSquenceLong);
- searchKey = myPreference+MK+SK+productSequenceString;
- }
- return searchKey;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement