Advertisement
psi_mmobile

Untitled

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