Advertisement
psi_mmobile

Untitled

Jun 21st, 2021
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.38 KB | None | 0 0
  1.     public void genericLoad() {
  2.         try {
  3.             // Connect to DB (JDBC 8)
  4.     //            String connString = "jdbc:oracle:thin:@144.91.103.48:1521:FLEET";
  5.             String connString = "java:comp/env/jdbc/BuildixxDS";
  6.             OracleDataSource ods = new OracleDataSource();
  7.             ods.setURL(connString);
  8.             ods.setUser("OF_OWNER");
  9.             ods.setPassword("ofo");
  10.             Connection conn = ods.getConnection();
  11.  
  12.             // Get all the generic load with GENERIC_LOAD_STATUS_ID = 1 (join table with PHYSICAL_FILE table to get the file_path to the CSV)
  13.             String genericLoadRecords =
  14.                 "select GenericLoad.GENERIC_LOAD_ID,GenericLoad.GENERIC_LOAD_TYPE_ID,GenericLoad.GENERIC_LOAD_STATUS_ID,PhysicalFile.FILE_PATH, GenericLoadType.NAME from GENERIC_LOAD GenericLoad,PHYSICAL_FILE PhysicalFile, GENERIC_LOAD_TYPE GenericLoadType where GenericLoad.PHYSICAL_FILE_ID=PhysicalFile.PHYSICAL_FILE_ID and GenericLoad.GENERIC_LOAD_TYPE_ID = GenericLoadType.GENERIC_LOAD_TYPE_ID and GenericLoad.GENERIC_LOAD_STATUS_ID=1";
  15.             Statement stmt = conn.createStatement();
  16.             ResultSet rs = stmt.executeQuery(genericLoadRecords);
  17.            
  18.             // For all generic loads (depending on the GENERIC_LOAD_TYPE_ID (for now there is only ‘1’ = WBS_TASK))
  19.             BufferedReader br = null;
  20.             while (rs.next()) {
  21.                 File genericLoadFile = new File(rs.getString("FILE_PATH"));
  22.                 if (genericLoadFile.isFile() && genericLoadFile.exists() && genericLoadFile.length() > 0) { // If file not empty
  23.                    
  24.                     String loadTable = rs.getString("NAME") + "_LOAD"; // GENERALIZE LOAD TABLE NAME
  25.                    
  26.                     String loadDataTable = rs.getString("NAME") + "_LOAD_DATA"; // GENERALIZE LOAD DATA TABLE NAME
  27.                    
  28.                     String selectNewLoadId = "select seq_" + loadTable + "_id.nextval from dual";
  29.                    
  30.                     Statement selectNewLoadIdStmt = conn.createStatement();
  31.                     ResultSet newIdRs = selectNewLoadIdStmt.executeQuery(selectNewLoadId);
  32.                     Integer newId = newIdRs.getInt(0);
  33.                    
  34.                     String createLoadRecordSQL = "insert into " + loadTable + " values ("+ newId + ",trunc(sysdate),'GENERIC LOAD PROCESS',null,null," + rs.getInt("GENERIC_LOAD_ID") + ",null)";
  35.                    
  36.                     Statement createLoadRecordStmt = conn.createStatement();
  37.                     createLoadRecordStmt.executeUpdate(createLoadRecordSQL);
  38.                    
  39.                     String sqlErrorStatement = "insert into user_message (user_message_id,user_message_type_id,generic_load_id,user_message) values (seq_user_message_id.nextval,3," + rs.getInt("GENERIC_LOAD_ID") + ",'"; //For each error : Insert a USER_MESSAGE linked to the generic load with USER_MESSAGE_TYPE_ID = 3  The message should be “Error at line … : “
  40.                    
  41.                     String insertLoadStatement = "insert into " + loadDataTable + " values (seq_" + loadDataTable +"_id.nexval," + newId + ",?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
  42.                    
  43.                     br = new BufferedReader(new InputStreamReader(new FileInputStream(genericLoadFile), "UTF-8"));
  44.                     br.mark(1);
  45.                     if (br.read() != 0xFEFF) // remove first UTF-8 begin char
  46.                         br.reset();
  47.                     String row = null;
  48.                     Integer firstRowLength = null;
  49.                     Integer rowLength = null;
  50.                     Integer indexes = null;
  51.                     int errors = 0;
  52.                     Statement errorStatement = null;
  53.                     List<List<String>> results = new ArrayList<List<String>>();
  54.                     int counter = 0;
  55.  
  56.                     while ((row = br.readLine()) != null) {
  57.                         counter++;
  58.                         String[] data = row.split(";(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
  59.                         rowLength = row.length();
  60.                         if (firstRowLength == null) {
  61.                             firstRowLength = row.length();
  62.                             indexes = data.length;
  63.                         } else {
  64.                             if (firstRowLength < rowLength) { // “Too many columns”
  65.                                 errors++;
  66.                                 errorStatement = conn.createStatement();
  67.                                 errorStatement.executeUpdate(sqlErrorStatement + "Error at line: " + counter + " Too many columns!')");
  68.                                
  69.                             }
  70.                             if (firstRowLength > rowLength) { // “Column(s) are missing”
  71.                                 errors++;
  72.                                 errorStatement = conn.createStatement();
  73.                                 errorStatement.executeUpdate(sqlErrorStatement + "Error at line: " + counter + " Column(s) are missing!')");
  74.                             }
  75.                             if (firstRowLength == rowLength) {
  76.                                 PreparedStatement insertLoadStmt = conn.prepareStatement(insertLoadStatement);
  77.                                 // MIGHT HAVE A SHIFT BY 2 IF XXXX_TASK_LOAD_ID and XXXXX_TASK_LOAD_DATA_ID are in the file
  78.                                 insertLoadStmt.setString(3, data[2]); // OBJECT_CODE
  79.                                 insertLoadStmt.setString(4, data[3]); // ACTIVITY_CODE
  80.                                 insertLoadStmt.setString(5, data[4]); // ACTIVITY_NAME
  81.                                 insertLoadStmt.setString(6, data[5]); // COMPANY_NAME
  82.                                 insertLoadStmt.setString(7, data[6]); // WBS_NAME
  83.                                 insertLoadStmt.setString(8, data[7]); // REF_NUMBER
  84.                                 insertLoadStmt.setDate(9, Date.valueOf(data[8])); // FROM_DATE
  85.                                 insertLoadStmt.setDate(10, Date.valueOf(data[9])); // TO_DATE
  86.                                 insertLoadStmt.setString(11, data[10]); // IS_WORK_TIME_REPORTED
  87.                                 insertLoadStmt.setString(12, data[11]); // ASSEMBLY_NAME
  88.                                 insertLoadStmt.setDouble(13, Double.parseDouble(data[12])); // ASSEMBLY_HR
  89.                                 insertLoadStmt.setDouble(14, Double.parseDouble(data[13])); // ASSEMBLY_QUANTITY
  90.                                 insertLoadStmt.setString(15, data[14]); // EXISTING_RECORD
  91.                                 insertLoadStmt.setInt(16, Integer.valueOf(data[15])); // WBS_TASK_STATUS_ID
  92.                                 insertLoadStmt.setString(17, data[16]); // VO_WS_TOKEN
  93.                                 insertLoadStmt.setString(18, data[17]); // DESCRIPTION
  94.                                 insertLoadStmt.setString(19, data[18]); // WBS_TASK_STRING
  95.                                 insertLoadStmt.setString(20, data[19]); // SHORT_WBS_TASK_STRING
  96.                                 insertLoadStmt.setString(21, data[20]); // ERP_ID
  97.                                 insertLoadStmt.setString(22, data[21]); // OBJECT_CODE_STRING
  98.                                 insertLoadStmt.setString(23, data[22]); // WBS_REF_NUMBER
  99.                                 insertLoadStmt.setString(24, data[23]); // WBS_TASK_STERING_DESCR
  100.                                 insertLoadStmt.executeUpdate(insertLoadStatement);
  101.                             }
  102.                         }
  103.                     }
  104.                    
  105.                     if (errors == 0) { // If there was no error, status of XXXX_LOAD should be ‘S’. The GENERIC_LOAD_STATUS_ID should be changed to 2.
  106.                         String genericLoadNoErrorsStatement = "update generic_load set generic_load_status_id=2 where generic_load_id=" + rs.getInt("GENERIC_LOAD_ID");
  107.                         Statement genericLoadNoErrorsStmt = conn.createStatement();
  108.                         genericLoadNoErrorsStmt.executeUpdate(genericLoadNoErrorsStatement);
  109.                        
  110.                         String checkDataIntegrityProcedureSQL = "begin of_owner.p_wbs_task_load.check_data_integrity(?); end;";
  111.                         PreparedStatement checkDataIntegrityProcedure = conn.prepareStatement(checkDataIntegrityProcedureSQL);
  112.                         checkDataIntegrityProcedure.setInt(1, newId);
  113.                     } else {// If there was any error, status of XXXX_LOAD should be ‘E’. The GENERIC_LOAD_STATUS_ID should be changed to 7.
  114.                         String genericLoadWithErrorsStatement = "update generic_load set generic_load_status_id=7 where generic_load_id=" + rs.getInt("GENERIC_LOAD_ID");
  115.                         Statement genericLoadWithErrorsStmt = conn.createStatement();
  116.                         genericLoadWithErrorsStmt.executeUpdate(genericLoadWithErrorsStatement);
  117.                     }
  118.                 }
  119.                 conn.commit();
  120.             }
  121.         } catch (FileNotFoundException fnfe) {
  122.             log.debug("FILE NOT FOUND : " + fnfe.getMessage());
  123.         } catch (UnsupportedEncodingException uee) {
  124.             log.debug("ERROR READING FILE : " + uee.getMessage());
  125.         } catch (IOException ioe) {
  126.             log.debug("IO ERROR : " + ioe.getMessage());
  127.         } catch (SQLException sqle) {
  128.             log.debug("ERROR WITH SQL : " + sqle.getMessage());
  129.         }
  130.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement