Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void genericLoad() {
- try {
- // Connect to DB (JDBC 8)
- // String connString = "jdbc:oracle:thin:@144.91.103.48:1521:FLEET";
- String connString = "java:comp/env/jdbc/BuildixxDS";
- OracleDataSource ods = new OracleDataSource();
- ods.setURL(connString);
- ods.setUser("OF_OWNER");
- ods.setPassword("ofo");
- Connection conn = ods.getConnection();
- // 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)
- String genericLoadRecords =
- "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";
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery(genericLoadRecords);
- // For all generic loads (depending on the GENERIC_LOAD_TYPE_ID (for now there is only ‘1’ = WBS_TASK))
- BufferedReader br = null;
- while (rs.next()) {
- File genericLoadFile = new File(rs.getString("FILE_PATH"));
- if (genericLoadFile.isFile() && genericLoadFile.exists() && genericLoadFile.length() > 0) { // If file not empty
- String loadTable = rs.getString("NAME") + "_LOAD"; // GENERALIZE LOAD TABLE NAME
- String loadDataTable = rs.getString("NAME") + "_LOAD_DATA"; // GENERALIZE LOAD DATA TABLE NAME
- String selectNewLoadId = "select seq_" + loadTable + "_id.nextval from dual";
- Statement selectNewLoadIdStmt = conn.createStatement();
- ResultSet newIdRs = selectNewLoadIdStmt.executeQuery(selectNewLoadId);
- Integer newId = newIdRs.getInt(0);
- String createLoadRecordSQL = "insert into " + loadTable + " values ("+ newId + ",trunc(sysdate),'GENERIC LOAD PROCESS',null,null," + rs.getInt("GENERIC_LOAD_ID") + ",null)";
- Statement createLoadRecordStmt = conn.createStatement();
- createLoadRecordStmt.executeUpdate(createLoadRecordSQL);
- 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 … : “
- String insertLoadStatement = "insert into " + loadDataTable + " values (seq_" + loadDataTable +"_id.nexval," + newId + ",?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
- br = new BufferedReader(new InputStreamReader(new FileInputStream(genericLoadFile), "UTF-8"));
- br.mark(1);
- if (br.read() != 0xFEFF) // remove first UTF-8 begin char
- br.reset();
- String row = null;
- Integer firstRowLength = null;
- Integer rowLength = null;
- Integer indexes = null;
- int errors = 0;
- Statement errorStatement = null;
- List<List<String>> results = new ArrayList<List<String>>();
- int counter = 0;
- while ((row = br.readLine()) != null) {
- counter++;
- String[] data = row.split(";(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
- rowLength = row.length();
- if (firstRowLength == null) {
- firstRowLength = row.length();
- indexes = data.length;
- } else {
- if (firstRowLength < rowLength) { // “Too many columns”
- errors++;
- errorStatement = conn.createStatement();
- errorStatement.executeUpdate(sqlErrorStatement + "Error at line: " + counter + " Too many columns!')");
- }
- if (firstRowLength > rowLength) { // “Column(s) are missing”
- errors++;
- errorStatement = conn.createStatement();
- errorStatement.executeUpdate(sqlErrorStatement + "Error at line: " + counter + " Column(s) are missing!')");
- }
- if (firstRowLength == rowLength) {
- PreparedStatement insertLoadStmt = conn.prepareStatement(insertLoadStatement);
- // MIGHT HAVE A SHIFT BY 2 IF XXXX_TASK_LOAD_ID and XXXXX_TASK_LOAD_DATA_ID are in the file
- insertLoadStmt.setString(3, data[2]); // OBJECT_CODE
- insertLoadStmt.setString(4, data[3]); // ACTIVITY_CODE
- insertLoadStmt.setString(5, data[4]); // ACTIVITY_NAME
- insertLoadStmt.setString(6, data[5]); // COMPANY_NAME
- insertLoadStmt.setString(7, data[6]); // WBS_NAME
- insertLoadStmt.setString(8, data[7]); // REF_NUMBER
- insertLoadStmt.setDate(9, Date.valueOf(data[8])); // FROM_DATE
- insertLoadStmt.setDate(10, Date.valueOf(data[9])); // TO_DATE
- insertLoadStmt.setString(11, data[10]); // IS_WORK_TIME_REPORTED
- insertLoadStmt.setString(12, data[11]); // ASSEMBLY_NAME
- insertLoadStmt.setDouble(13, Double.parseDouble(data[12])); // ASSEMBLY_HR
- insertLoadStmt.setDouble(14, Double.parseDouble(data[13])); // ASSEMBLY_QUANTITY
- insertLoadStmt.setString(15, data[14]); // EXISTING_RECORD
- insertLoadStmt.setInt(16, Integer.valueOf(data[15])); // WBS_TASK_STATUS_ID
- insertLoadStmt.setString(17, data[16]); // VO_WS_TOKEN
- insertLoadStmt.setString(18, data[17]); // DESCRIPTION
- insertLoadStmt.setString(19, data[18]); // WBS_TASK_STRING
- insertLoadStmt.setString(20, data[19]); // SHORT_WBS_TASK_STRING
- insertLoadStmt.setString(21, data[20]); // ERP_ID
- insertLoadStmt.setString(22, data[21]); // OBJECT_CODE_STRING
- insertLoadStmt.setString(23, data[22]); // WBS_REF_NUMBER
- insertLoadStmt.setString(24, data[23]); // WBS_TASK_STERING_DESCR
- insertLoadStmt.executeUpdate(insertLoadStatement);
- }
- }
- }
- 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.
- String genericLoadNoErrorsStatement = "update generic_load set generic_load_status_id=2 where generic_load_id=" + rs.getInt("GENERIC_LOAD_ID");
- Statement genericLoadNoErrorsStmt = conn.createStatement();
- genericLoadNoErrorsStmt.executeUpdate(genericLoadNoErrorsStatement);
- String checkDataIntegrityProcedureSQL = "begin of_owner.p_wbs_task_load.check_data_integrity(?); end;";
- PreparedStatement checkDataIntegrityProcedure = conn.prepareStatement(checkDataIntegrityProcedureSQL);
- checkDataIntegrityProcedure.setInt(1, newId);
- } else {// If there was any error, status of XXXX_LOAD should be ‘E’. The GENERIC_LOAD_STATUS_ID should be changed to 7.
- String genericLoadWithErrorsStatement = "update generic_load set generic_load_status_id=7 where generic_load_id=" + rs.getInt("GENERIC_LOAD_ID");
- Statement genericLoadWithErrorsStmt = conn.createStatement();
- genericLoadWithErrorsStmt.executeUpdate(genericLoadWithErrorsStatement);
- }
- }
- conn.commit();
- }
- } catch (FileNotFoundException fnfe) {
- log.debug("FILE NOT FOUND : " + fnfe.getMessage());
- } catch (UnsupportedEncodingException uee) {
- log.debug("ERROR READING FILE : " + uee.getMessage());
- } catch (IOException ioe) {
- log.debug("IO ERROR : " + ioe.getMessage());
- } catch (SQLException sqle) {
- log.debug("ERROR WITH SQL : " + sqle.getMessage());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement