bali-nusa-plugin/andromeida.midsuit.project/src/andromedia/midsuit/process/MID_UploadPEBWindow.java

165 lines
5.7 KiB
Java

package andromedia.midsuit.process;
import java.io.File;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import org.adempiere.exceptions.AdempiereException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.compiere.model.MBPartnerLocation;
import org.compiere.model.MOrder;
import org.compiere.model.MOrderLine;
import org.compiere.process.ProcessInfoParameter;
import org.compiere.process.SvrProcess;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;
import andromedia.midsuit.model.MID_MOrder;
import andromedia.midsuit.model.X_MID_UploadPEB;
import andromedia.midsuit.model.X_MID_UploadPEBLine;
import net.ucanaccess.jdbc.UcanaccessDriver;
public class MID_UploadPEBWindow extends SvrProcess{
String p_File = "";
int p_C_DocType_ID =0;
X_MID_UploadPEB peb = null;
@Override
protected void prepare() {
// TODO Auto-generated method stub
ProcessInfoParameter[] para = getParameter();
for (int i = 0; i < para.length; i++)
{
if(para[i].getParameterName().equals("FileName"))
p_File = para[i].getParameterAsString();
else if(para[i].getParameterName().equals("C_DocType_ID"))
p_C_DocType_ID = para[i].getParameterAsInt();
}
peb = new X_MID_UploadPEB(getCtx(), getRecord_ID(), get_TrxName());
}
@Override
protected String doIt() throws Exception {
File TPB = new File(p_File);
int formatIndex = TPB.getName().lastIndexOf(".");
int fileNameIndex = TPB.getName().lastIndexOf("_");
if(TPB.getName().substring(formatIndex+1).equalsIgnoreCase("xlsx") ||
TPB.getName().substring(formatIndex+1).equalsIgnoreCase("xls")){
readExcel(TPB, peb);
}else{
readAccess(TPB, peb);
}
peb.setFileName(TPB.getName().substring(fileNameIndex+1));
peb.saveEx();
return "";
}
private void readExcel(File TPB, X_MID_UploadPEB peb) throws Exception{
Workbook workbook = WorkbookFactory.create(TPB);
Sheet header = workbook.getSheet("Header");
Iterator<Row> rows = header.rowIterator();
int rowNum = 0;
Iterator<Row> checkRows = header.rowIterator();
String documentValidation = "";
while(checkRows.hasNext()) {
Row checkRow = checkRows.next();
String NoAju = checkRow.getCell(0).getStringCellValue();
int no = DB.getSQLValue(get_TrxName(), "SELECT COUNT(*) FROM MID_UploadPEBLine WHERE NoAju=?", new Object[] { NoAju });
if(no>0)
documentValidation= documentValidation+NoAju+" ";
}
if(documentValidation.length()>1)
throw new AdempiereException(documentValidation+" already created !!!");
while(rows.hasNext()){
Row row = rows.next();
if(rowNum==0) {
rowNum++;
continue;
}
X_MID_UploadPEBLine line = new X_MID_UploadPEBLine(getCtx(), 0, get_TrxName());
line.setMID_UploadPEB_ID(peb.get_ID());
line.setAD_Org_ID(peb.getAD_Org_ID());
line.setRegisterNo(row.getCell(103).getStringCellValue());
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy");
Date parsedDate = dateFormat.parse(row.getCell(117).getStringCellValue());
Timestamp registerDate = new Timestamp(parsedDate.getTime());
line.setRegisterDate(registerDate);
line.setNoAju(row.getCell(0).getStringCellValue());
String AJU_DocType_Value = row.getCell(5).getStringCellValue();
int MID_AJUDocType_ID = DB.getSQLValue(get_TrxName(), "SELECT MID_AJUDocumentType_ID FROM MID_AjuDocumentType WHERE Value =? AND AD_Client_ID =?", new Object[] { AJU_DocType_Value, getAD_Client_ID() });
if(MID_AJUDocType_ID <=0)
throw new AdempiereException("AJU Document For "+AJU_DocType_Value+" !!!");
line.setMID_AJUDocumentType_ID(MID_AJUDocType_ID);
line.saveEx();
}
}
private void readAccess(File TPB, X_MID_UploadPEB peb) throws Exception{
String msg = "";
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
String databaseURL = "jdbc:ucanaccess://" + TPB.getPath() + ";jackcessOpener=andromedia.midsuit.process.CryptCodecOpener";
try (Connection conn = DriverManager.getConnection(databaseURL)) {
String sql = "SELECT * FROM tblPebHdr";
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery(sql);
String documentValidation = "";
while(rs.next()) {
String NoAju = rs.getString("CAR");
int no = DB.getSQLValue(get_TrxName(), "SELECT COUNT(*) FROM MID_UploadPEBLine WHERE NoAju=?", new Object[] { NoAju });
if(no>0)
documentValidation= documentValidation+NoAju+" ";
}
if(documentValidation.length()>1)
throw new AdempiereException(documentValidation+" already created !!!");
statement.close();
rs.close();
statement=null;
rs = null;
statement = conn.createStatement();
rs = statement.executeQuery(sql);
while (rs.next()) {
if (msg != "") throw new AdempiereException(msg);
X_MID_UploadPEBLine line = new X_MID_UploadPEBLine(getCtx(), 0, get_TrxName());
line.setMID_UploadPEB_ID(peb.get_ID());
line.setAD_Org_ID(peb.getAD_Org_ID());
int MID_AJUDocumentType_ID = DB.getSQLValue(get_TrxName(), "SELECT MID_AJUDocumentType_ID FROM MID_AJUDocumentType WHERE Value =? AND AD_Client_ID =?", new Object[] { "30", getAD_Client_ID() });
line.setNoAju(rs.getString("CAR"));
line.setRegisterNo(rs.getString("NODAFT"));
line.setRegisterDate(rs.getTimestamp("TGDAFT"));
line.setMID_AJUDocumentType_ID(MID_AJUDocumentType_ID);
line.saveEx();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}