1 14 package org.compiere.process; 15 16 import java.sql.*; 17 import java.math.*; 18 19 import org.compiere.util.*; 20 21 27 public class ImportReportLine extends SvrProcess 28 { 29 32 public ImportReportLine() 33 { 34 super(); 35 Log.trace(Log.l1_User, "ImportReportLine"); 36 } 38 39 private int m_AD_Client_ID = 0; 40 41 private int m_PA_ReportLineSet_ID = 0; 42 43 private boolean m_deleteOldImported = false; 44 45 46 private int m_AD_Org_ID = 0; 47 48 private Timestamp m_DateValue = null; 49 50 53 protected void prepare() 54 { 55 ProcessInfoParameter[] para = getParameter(); 56 for (int i = 0; i < para.length; i++) 57 { 58 String name = para[i].getParameterName(); 59 if (para[i].getParameter() == null) 60 ; 61 else if (name.equals("AD_Client_ID")) 62 m_AD_Client_ID = ((BigDecimal)para[i].getParameter()).intValue(); 63 else if (name.equals("PA_ReportLineSet_ID")) 64 m_PA_ReportLineSet_ID = ((BigDecimal)para[i].getParameter()).intValue(); 65 else if (name.equals("DeleteOldImported")) 66 m_deleteOldImported = "Y".equals(para[i].getParameter()); 67 else 68 Log.error("ImportReportLine.prepare - Unknown Parameter: " + name); 69 } 70 if (m_DateValue == null) 71 m_DateValue = new Timestamp (System.currentTimeMillis()); 72 } 74 75 80 protected String doIt() throws java.lang.Exception 81 { 82 StringBuffer sql = null; 83 int no = 0; 84 String clientCheck = " AND AD_Client_ID=" + m_AD_Client_ID; 85 86 88 if (m_deleteOldImported) 90 { 91 sql = new StringBuffer ("DELETE I_ReportLine " 92 + "WHERE I_IsImported='Y'").append(clientCheck); 93 no = DB.executeUpdate(sql.toString()); 94 Log.trace(Log.l5_DData, "ImportReportLine.doIt", "Delete Old Impored =" + no); 95 } 96 97 sql = new StringBuffer ("UPDATE I_ReportLine " 99 + "SET AD_Client_ID = COALESCE (AD_Client_ID, ").append(m_AD_Client_ID).append(")," 100 + " AD_Org_ID = COALESCE (AD_Org_ID, 0)," 101 + " IsActive = COALESCE (IsActive, 'Y')," 102 + " Created = COALESCE (Created, SysDate)," 103 + " CreatedBy = COALESCE (CreatedBy, 0)," 104 + " Updated = COALESCE (Updated, SysDate)," 105 + " UpdatedBy = COALESCE (UpdatedBy, 0)," 106 + " I_ErrorMsg = NULL," 107 + " I_IsImported = 'N' " 108 + "WHERE I_IsImported<>'Y' OR I_IsImported IS NULL"); 109 no = DB.executeUpdate(sql.toString()); 110 Log.trace(Log.l5_DData, "ImportReportLine.doIt", "Reset=" + no); 111 112 if (m_PA_ReportLineSet_ID != 0) 114 { 115 sql = new StringBuffer ("UPDATE I_ReportLine i " 116 + "SET ReportLineSetName=(SELECT Name FROM PA_ReportLineSet r" 117 + " WHERE PA_ReportLineSet_ID=").append(m_PA_ReportLineSet_ID).append(" AND i.AD_Client_ID=r.AD_Client_ID) " 118 + "WHERE ReportLineSetName IS NULL AND PA_ReportLineSet_ID IS NULL" 119 + " AND I_IsImported<>'Y'").append(clientCheck); 120 no = DB.executeUpdate(sql.toString()); 121 Log.trace(Log.l5_DData, "ImportReportLine.doIt", "Set ReportLineSetName Default=" + no); 122 } 123 sql = new StringBuffer ("UPDATE I_ReportLine i " 125 + "SET PA_ReportLineSet_ID=(SELECT PA_ReportLineSet_ID FROM PA_ReportLineSet r" 126 + " WHERE i.ReportLineSetName=r.Name AND i.AD_Client_ID=r.AD_Client_ID) " 127 + "WHERE PA_ReportLineSet_ID IS NULL" 128 + " AND I_IsImported<>'Y'").append(clientCheck); 129 no = DB.executeUpdate(sql.toString()); 130 Log.trace(Log.l5_DData, "ImportReportLine.doIt", "Set PA_ReportLineSet_ID=" + no); 131 sql = new StringBuffer ("UPDATE I_ReportLine " 133 + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid ReportLineSet, ' " 134 + "WHERE PA_ReportLineSet_ID IS NULL" 135 + " AND I_IsImported<>'Y'").append(clientCheck); 136 no = DB.executeUpdate(sql.toString()); 137 Log.trace(Log.l3_Util, "ImportReportLine.doIt", "Invalid ReportLineSet=" + no); 138 139 sql = new StringBuffer ("UPDATE I_ReportLine " 141 + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'Ignored=NoLineName, ' " 142 + "WHERE PA_ReportLine_ID IS NULL AND Name IS NULL" 143 + " AND I_IsImported<>'Y'").append(clientCheck); 144 no = DB.executeUpdate(sql.toString()); 145 Log.trace(Log.l3_Util, "ImportReportLine.doIt", "Invalid ReportLineSet=" + no); 146 147 sql = new StringBuffer ("UPDATE I_ReportLine i " 149 + "SET C_ElementValue_ID=(SELECT C_ElementValue_ID FROM C_ElementValue e" 150 + " WHERE i.ElementValue=e.Value AND i.AD_Client_ID=e.AD_Client_ID) " 151 + "WHERE C_ElementValue_ID IS NULL AND ElementValue IS NOT NULL" 152 + " AND I_IsImported<>'Y'").append(clientCheck); 153 no = DB.executeUpdate(sql.toString()); 154 Log.trace(Log.l5_DData, "ImportReportLine.doIt", "Set C_ElementValue_ID=" + no); 155 sql = new StringBuffer ("UPDATE I_ReportLine " 157 + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid ElementValue, ' " 158 + "WHERE C_ElementValue_ID IS NULL AND CalculationType<>'C'" 159 + " AND I_IsImported<>'Y'").append(clientCheck); 160 no = DB.executeUpdate(sql.toString()); 161 Log.trace(Log.l3_Util, "ImportReportLine.doIt", "Invalid AccountType=" + no); 162 163 sql = new StringBuffer ("UPDATE I_ReportLine " 165 + "SET SeqNo=I_ReportLine_ID " 166 + "WHERE SeqNo IS NULL" 167 + " AND I_IsImported='N'").append(clientCheck); 168 no = DB.executeUpdate(sql.toString()); 169 Log.trace(Log.l5_DData, "ImportReportLine.doIt", "Set SeqNo Default=" + no); 170 171 sql = new StringBuffer ("UPDATE I_ReportLine i " 173 + "SET (Description, SeqNo, IsSummary, IsPrinted, LineType, CalculationType, AmountType, PostingType)=" 174 + " (SELECT Description, SeqNo, IsSummary, IsPrinted, LineType, CalculationType, AmountType, PostingType" 175 + " FROM I_ReportLine ii WHERE i.Name=ii.Name AND i.PA_ReportLineSet_ID=ii.PA_ReportLineSet_ID" 176 + " AND ii.I_ReportLine_ID=(SELECT MIN(I_ReportLine_ID) FROM I_ReportLine iii" 177 + " WHERE i.Name=iii.Name AND i.PA_ReportLineSet_ID=iii.PA_ReportLineSet_ID)) " 178 + "WHERE EXISTS (SELECT *" 179 + " FROM I_ReportLine ii WHERE i.Name=ii.Name AND i.PA_ReportLineSet_ID=ii.PA_ReportLineSet_ID" 180 + " AND ii.I_ReportLine_ID=(SELECT MIN(I_ReportLine_ID) FROM I_ReportLine iii" 181 + " WHERE i.Name=iii.Name AND i.PA_ReportLineSet_ID=iii.PA_ReportLineSet_ID))" 182 + " AND I_IsImported='N'").append(clientCheck); no = DB.executeUpdate(sql.toString()); 184 Log.trace(Log.l5_DData, "ImportReportLine.doIt", "Sync from first Row of Line=" + no); 185 186 sql = new StringBuffer ("UPDATE I_ReportLine " 188 + "SET IsSummary='N' " 189 + "WHERE IsSummary IS NULL OR IsSummary NOT IN ('Y','N')" 190 + " AND I_IsImported<>'Y'").append(clientCheck); 191 no = DB.executeUpdate(sql.toString()); 192 Log.trace(Log.l5_DData, "ImportReportLine.doIt", "Set IsSummary Default=" + no); 193 194 sql = new StringBuffer ("UPDATE I_ReportLine " 196 + "SET IsPrinted='Y' " 197 + "WHERE IsPrinted IS NULL OR IsPrinted NOT IN ('Y','N')" 198 + " AND I_IsImported<>'Y'").append(clientCheck); 199 no = DB.executeUpdate(sql.toString()); 200 Log.trace(Log.l5_DData, "ImportReportLine.doIt", "Set IsPrinted Default=" + no); 201 202 sql = new StringBuffer ("UPDATE I_ReportLine " 204 + "SET LineType='S' " 205 + "WHERE LineType IS NULL OR LineType NOT IN ('S','C')" 206 + " AND I_IsImported<>'Y'").append(clientCheck); 207 no = DB.executeUpdate(sql.toString()); 208 Log.trace(Log.l5_DData, "ImportReportLine.doIt", "Set LineType Default=" + no); 209 210 sql = new StringBuffer ("UPDATE I_ReportLine " 212 + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid CalculationType, ' " 213 + "WHERE CalculationType IS NOT NULL AND CalculationType NOT IN ('A','P','R','S')" 214 + " AND I_IsImported<>'Y'").append(clientCheck); 215 no = DB.executeUpdate(sql.toString()); 216 Log.trace(Log.l3_Util, "ImportReportLine.doIt", "Invalid CalculationType=" + no); 217 218 sql = new StringBuffer ("UPDATE I_ReportLine " 220 + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid CalculationType, ' " 221 + "WHERE AmountType IS NOT NULL AND UPPER(AmountType) NOT IN ('BP','CP','DP','QP', 'BY','CY','DY','QY', 'BT','CT','DT','QT')" 222 + " AND I_IsImported<>'Y'").append(clientCheck); 223 no = DB.executeUpdate(sql.toString()); 224 Log.trace(Log.l3_Util, "ImportReportLine.doIt", "Invalid AmountType=" + no); 225 226 sql = new StringBuffer ("UPDATE I_ReportLine " 228 + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid CalculationType, ' " 229 + "WHERE PostingType IS NOT NULL AND PostingType NOT IN ('A','B','E','S')" 230 + " AND I_IsImported<>'Y'").append(clientCheck); 231 no = DB.executeUpdate(sql.toString()); 232 Log.trace(Log.l3_Util, "ImportReportLine.doIt", "Invalid PostingType=" + no); 233 234 sql = new StringBuffer ("UPDATE I_ReportLine i " 236 + "SET PA_ReportLine_ID=(SELECT PA_ReportLine_ID FROM PA_ReportLine r" 237 + " WHERE i.Name=r.Name AND i.PA_ReportLineSet_ID=r.PA_ReportLineSet_ID AND ROWNUM=1) " 238 + "WHERE PA_ReportLine_ID IS NULL AND PA_ReportLineSet_ID IS NOT NULL" 239 + " AND I_IsImported='N'").append(clientCheck); 240 no = DB.executeUpdate(sql.toString()); 241 Log.trace(Log.l5_DData, "ImportReportLine.doIt", "Set PA_ReportLine_ID=" + no); 242 243 int noInsertLine = 0; 245 int noUpdateLine = 0; 246 Connection conn = DB.createConnection(false, Connection.TRANSACTION_READ_COMMITTED); 247 248 sql = new StringBuffer ("SELECT DISTINCT PA_ReportLineSet_ID, Name " 250 + "FROM I_ReportLine " 251 + "WHERE I_IsImported='N' AND PA_ReportLine_ID IS NULL" 252 + " AND I_IsImported='N'").append(clientCheck); 253 try 254 { 255 PreparedStatement pstmt_insertLine = conn.prepareStatement 257 ("INSERT INTO PA_ReportLine " 258 + "(PA_ReportLine_ID,PA_ReportLineSet_ID," 259 + "AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy," 260 + "Name,SeqNo,IsPrinted,IsSummary,LineType)" 261 + "SELECT ?,PA_ReportLineSet_ID," 262 + "AD_Client_ID,AD_Org_ID,'Y',SysDate,CreatedBy,SysDate,UpdatedBy," 263 + "Name,SeqNo,IsPrinted,IsSummary,LineType " 264 + "FROM I_ReportLine " 265 + "WHERE PA_ReportLineSet_ID=? AND Name=? AND ROWNUM=1" + clientCheck); 267 268 PreparedStatement pstmt = DB.prepareStatement(sql.toString()); 269 ResultSet rs = pstmt.executeQuery(); 270 while (rs.next()) 271 { 272 int PA_ReportLineSet_ID = rs.getInt(1); 273 String Name = rs.getString(2); 274 int PA_ReportLine_ID = DB.getKeyNextNo(m_AD_Client_ID, "PA_ReportLine"); 276 pstmt_insertLine.setInt(1, PA_ReportLine_ID); 277 pstmt_insertLine.setInt(2, PA_ReportLineSet_ID); 278 pstmt_insertLine.setString(3, Name); 279 try 280 { 281 no = pstmt_insertLine.executeUpdate(); 282 Log.trace(10, "Insert ReportLine = " + no + ", PA_ReportLine_ID=" + PA_ReportLine_ID); 283 noInsertLine++; 284 } 285 catch (SQLException ex) 286 { 287 Log.trace(10, "Insert ReportLine - " + ex.toString()); 288 continue; 289 } 290 } 291 rs.close(); 292 pstmt.close(); 293 pstmt_insertLine.close(); 295 } 296 catch (SQLException e) 297 { 298 Log.error("ImportReportLine - Create ReportLine", e); 299 } 300 301 sql = new StringBuffer ("UPDATE I_ReportLine i " 303 + "SET PA_ReportLine_ID=(SELECT PA_ReportLine_ID FROM PA_ReportLine r" 304 + " WHERE i.Name=r.Name AND i.PA_ReportLineSet_ID=r.PA_ReportLineSet_ID AND ROWNUM=1) " 305 + "WHERE PA_ReportLine_ID IS NULL AND PA_ReportLineSet_ID IS NOT NULL" 306 + " AND I_IsImported='N'").append(clientCheck); 307 no = DB.executeUpdate(sql.toString()); 308 Log.trace(Log.l5_DData, "ImportReportLine.doIt", "Set PA_ReportLine_ID=" + no); 309 310 sql = new StringBuffer ("UPDATE PA_ReportLine r " 312 + "SET (Description,SeqNo,IsSummary,IsPrinted,LineType,CalculationType,AmountType,PostingType,Updated,UpdatedBy)=" 313 + " (SELECT Description,SeqNo,IsSummary,IsPrinted,LineType,CalculationType,AmountType,PostingType,SysDate,UpdatedBy" 314 + " FROM I_ReportLine i WHERE r.Name=i.Name AND r.PA_ReportLineSet_ID=i.PA_ReportLineSet_ID" 315 + " AND i.I_ReportLine_ID=(SELECT MIN(I_ReportLine_ID) FROM I_ReportLine iii" 316 + " WHERE i.Name=iii.Name AND i.PA_ReportLineSet_ID=iii.PA_ReportLineSet_ID)) " 317 + "WHERE EXISTS (SELECT *" 318 + " FROM I_ReportLine i WHERE r.Name=i.Name AND r.PA_ReportLineSet_ID=i.PA_ReportLineSet_ID" 319 + " AND i.I_ReportLine_ID=(SELECT MIN(I_ReportLine_ID) FROM I_ReportLine iii" 320 + " WHERE i.Name=iii.Name AND i.PA_ReportLineSet_ID=iii.PA_ReportLineSet_ID AND i.I_IsImported='N'))") 321 .append(clientCheck); 322 noUpdateLine = DB.executeUpdate(sql.toString()); 323 Log.trace(Log.l3_Util, "ImportReportLine.doIt", "Update PA_ReportLine=" + noUpdateLine); 324 325 326 int noInsertSource = 0; 328 int noUpdateSource = 0; 329 330 sql = new StringBuffer ("SELECT I_ReportLine_ID, PA_ReportSource_ID " 332 + "FROM I_ReportLine " 333 + "WHERE PA_ReportLine_ID IS NOT NULL" 334 + " AND I_IsImported='N'").append(clientCheck); 335 try 336 { 337 PreparedStatement pstmt_insertSource = conn.prepareStatement 339 ("INSERT INTO PA_ReportSource " 340 + "(PA_ReportSource_ID," 341 + "AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy," 342 + "PA_ReportLine_ID,ElementType,C_ElementValue_ID) " 343 + "SELECT ?," 344 + "AD_Client_ID,AD_Org_ID,'Y',SysDate,CreatedBy,SysDate,UpdatedBy," 345 + "PA_ReportLine_ID,'AC',C_ElementValue_ID " 346 + "FROM I_ReportLine " 347 + "WHERE I_ReportLine_ID=?" 348 + " AND I_IsImported='N'" 349 + clientCheck); 350 351 PreparedStatement pstmt_updateSource = conn.prepareStatement 353 ("UPDATE PA_ReportSource " 354 + "SET (ElementType,C_ElementValue_ID,Updated,UpdatedBy)=" 355 + " (SELECT 'AC',C_ElementValue_ID,SysDate,UpdatedBy" 356 + " FROM I_ReportLine" 357 + " WHERE I_ReportLine_ID=?) " 358 + "WHERE PA_ReportSource_ID=?" 359 + clientCheck); 360 361 PreparedStatement pstmt_setImported = conn.prepareStatement 363 ("UPDATE I_ReportLine SET I_IsImported='Y'," 364 + " PA_ReportSource_ID=?, " 365 + " Updated=SysDate, Processed='Y' WHERE I_ReportLine_ID=?"); 366 367 PreparedStatement pstmt = DB.prepareStatement(sql.toString()); 368 ResultSet rs = pstmt.executeQuery(); 369 while (rs.next()) 370 { 371 int I_ReportLine_ID = rs.getInt(1); 372 int PA_ReportSource_ID = rs.getInt(2); 373 if (PA_ReportSource_ID == 0) { 376 PA_ReportSource_ID = DB.getKeyNextNo(m_AD_Client_ID, "PA_ReportSource"); 377 pstmt_insertSource.setInt(1, PA_ReportSource_ID); 378 pstmt_insertSource.setInt(2, I_ReportLine_ID); 379 try 380 { 381 no = pstmt_insertSource.executeUpdate(); 382 Log.trace(10, "Insert ReportSource = " + no + ", I_ReportLine_ID=" + I_ReportLine_ID + ", PA_ReportSource_ID=" + PA_ReportSource_ID); 383 noInsertSource++; 384 } 385 catch (SQLException ex) 386 { 387 Log.trace(10, "Insert ReportSource - " + ex.toString()); 388 sql = new StringBuffer ("UPDATE I_ReportLine i " 389 + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||").append(DB.TO_STRING("Insert ElementSource: " + ex.toString())) 390 .append("WHERE I_ReportLine_ID=").append(I_ReportLine_ID); 391 DB.executeUpdate(sql.toString()); 392 continue; 393 } 394 } 395 else { 397 pstmt_updateSource.setInt(1, I_ReportLine_ID); 398 pstmt_updateSource.setInt(2, PA_ReportSource_ID); 399 try 400 { 401 no = pstmt_updateSource.executeUpdate(); 402 Log.trace(10, "Update ReportSource = " + no + ", I_ReportLine_ID=" + I_ReportLine_ID + ", PA_ReportSource_ID=" + PA_ReportSource_ID); 403 noUpdateSource++; 404 } 405 catch (SQLException ex) 406 { 407 Log.trace(10, "Update ReportSource - " + ex.toString()); 408 sql = new StringBuffer ("UPDATE I_ReportLine i " 409 + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||").append(DB.TO_STRING("Update ElementSource: " + ex.toString())) 410 .append("WHERE I_ReportLine_ID=").append(I_ReportLine_ID); 411 DB.executeUpdate(sql.toString()); 412 continue; 413 } 414 } 416 pstmt_setImported.setInt(1, PA_ReportSource_ID); 418 pstmt_setImported.setInt(2, I_ReportLine_ID); 419 no = pstmt_setImported.executeUpdate(); 420 if (no != 1) 421 Log.error("ImportReportLine.doIt - Set Imported=" + no); 422 } 423 rs.close(); 424 pstmt.close(); 425 pstmt_insertSource.close(); 427 pstmt_updateSource.close(); 428 pstmt_setImported.close(); 429 conn.close(); 431 conn = null; 432 } 433 catch (SQLException e) 434 { 435 try 436 { 437 if (conn != null) 438 conn.close(); 439 conn = null; 440 } 441 catch (SQLException ex) 442 { 443 } 444 throw new Exception ("ImportReportLine.doIt", e); 445 } 446 finally 447 { 448 if (conn != null) 449 conn.close(); 450 conn = null; 451 } 452 453 sql = new StringBuffer ("UPDATE I_ReportLine " 455 + "SET I_IsImported='N', Updated=SysDate " 456 + "WHERE I_IsImported<>'Y'").append(clientCheck); 457 no = DB.executeUpdate(sql.toString()); 458 addLog (0, null, new BigDecimal (no), "@Errors@"); 459 addLog (0, null, new BigDecimal (noInsertLine), "@PA_ReportLine_ID@: @Inserted@"); 460 addLog (0, null, new BigDecimal (noUpdateLine), "@PA_ReportLine_ID@: @Updated@"); 461 addLog (0, null, new BigDecimal (noInsertSource), "@PA_ReportSource_ID@: @Inserted@"); 462 addLog (0, null, new BigDecimal (noUpdateSource), "@PA_ReportSource_ID@: @Updated@"); 463 464 return ""; 465 } 467 } | Popular Tags |