Rahmadnet

call stored procedure from DAL

Sep 14th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. package org.rahmadnet.learning.spdal;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5.  
  6. import org.junit.Test;
  7. import org.openbravo.base.provider.OBProvider;
  8. import org.openbravo.dal.core.OBContext;
  9. import org.openbravo.dal.service.OBDal;
  10. import org.openbravo.model.ad.process.Parameter;
  11. import org.openbravo.model.ad.process.ProcessInstance;
  12. import org.openbravo.model.ad.ui.Process;
  13. import org.openbravo.test.base.OBBaseTest;
  14.  
  15. public class StoredProcedureTest extends OBBaseTest
  16. {
  17.     @Test
  18.     public void testStoredProcedure()
  19.     {
  20.         setUserContext("0");
  21.        
  22.         // get the process, we know that 800000 is the generate shiptments from invoice sp
  23.         final Process process = OBDal.getInstance().get(Process.class, "199");
  24.        
  25.         // Create the pInstance
  26.         final ProcessInstance pInstance = OBProvider.getInstance().get(ProcessInstance.class);
  27.         // sets its process
  28.         pInstance.setProcess(process);
  29.         // must be set to true
  30.         pInstance.setActive(true);
  31.         pInstance.setRecordID("0");
  32.        
  33.         // get the user from the context
  34.         pInstance.setUserContact(OBContext.getOBContext().getUser());
  35.        
  36.         // now create a parameter and set its values
  37.         final Parameter parameter = OBProvider.getInstance().get(Parameter.class);
  38.         parameter.setSequenceNumber("1");
  39.         parameter.setParameterName("Selection");
  40.         parameter.setString("Y");
  41.        
  42.         // set both sides of te bidirection association
  43.         pInstance.getADParameterList().add(parameter);
  44.         parameter.setProcessInstance(pInstance);
  45.        
  46.         // persist to the db
  47.         OBDal.getInstance().save(pInstance);
  48.        
  49.         // flush, this gives pInstance an ID
  50.         OBDal.getInstance().flush();
  51.        
  52.         System.err.println(pInstance.getId());
  53.        
  54.         // call the SP
  55.         try {
  56.             // first get a connection
  57.             final Connection connection = OBDal.getInstance().getConnection();
  58.             // connection.createStatement().execute("CALL M_InOut_Create0(?)");
  59.             final PreparedStatement ps = connection.prepareStatement("SELECT * FROM M_InOut_Create0(?)");
  60.             ps.setString(1, pInstance.getId());
  61.             ps.execute();
  62.         } catch (Exception e) {
  63.             // TODO: handle exception
  64.             throw new IllegalStateException(e);
  65.         }
  66.        
  67.         System.err.println(pInstance.getResult());
  68.         System.err.println(pInstance.getErrorMsg());
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment