Merge 3101959123e6

This commit is contained in:
Heng Sin Low 2013-03-12 16:21:48 +08:00
commit 3d95f7f716
2 changed files with 171 additions and 131 deletions

View File

@ -842,9 +842,6 @@ public class Login
String retValue = ""; String retValue = "";
int AD_Client_ID = Env.getContextAsInt(m_ctx, "#AD_Client_ID"); int AD_Client_ID = Env.getContextAsInt(m_ctx, "#AD_Client_ID");
int AD_Org_ID = org.getKey(); int AD_Org_ID = org.getKey();
@SuppressWarnings("unused")
int AD_User_ID = Env.getContextAsInt(m_ctx, "#AD_User_ID");
int AD_Role_ID = Env.getContextAsInt(m_ctx, "#AD_Role_ID");
// Other Settings // Other Settings
Env.setContext(m_ctx, "#YYYY", "Y"); Env.setContext(m_ctx, "#YYYY", "Y");

View File

@ -19,15 +19,15 @@ package org.idempiere.fitrecorder;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.Writer; import java.io.Writer;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.logging.Level; import java.util.logging.Level;
import org.compiere.model.MClient; import org.compiere.model.MClient;
@ -36,6 +36,7 @@ import org.compiere.model.MPInstance;
import org.compiere.model.MPInstancePara; import org.compiere.model.MPInstancePara;
import org.compiere.model.MProcess; import org.compiere.model.MProcess;
import org.compiere.model.MProcessPara; import org.compiere.model.MProcessPara;
import org.compiere.model.MSession;
import org.compiere.model.MSysConfig; import org.compiere.model.MSysConfig;
import org.compiere.model.MTable; import org.compiere.model.MTable;
import org.compiere.model.MUser; import org.compiere.model.MUser;
@ -47,11 +48,11 @@ import org.compiere.util.CLogger;
import org.compiere.util.DB; import org.compiere.util.DB;
import org.compiere.util.DisplayType; import org.compiere.util.DisplayType;
import org.compiere.util.Env; import org.compiere.util.Env;
import org.compiere.util.Ini;
public class FitRecorder implements ModelValidator { public class FitRecorder implements ModelValidator {
private static FileOutputStream tempFile = null; private HashMap<Integer, Writer> writersPerSession;
private static Writer writer;
/** Logger */ /** Logger */
private static CLogger log = CLogger.getCLogger(FitRecorder.class); private static CLogger log = CLogger.getCLogger(FitRecorder.class);
@ -103,14 +104,10 @@ public class FitRecorder implements ModelValidator {
@Override @Override
public void initialize(ModelValidationEngine engine, MClient client) { public void initialize(ModelValidationEngine engine, MClient client) {
SimpleDateFormat format = new SimpleDateFormat ("yyyyMMddHHmmss"); writersPerSession=new HashMap<Integer, Writer>();
String dateTimeText = format.format(new Timestamp(System.currentTimeMillis()));
try { try {
File fileNameOr = File.createTempFile("fit_test_" + dateTimeText + "_", ".txt");
tempFile = new FileOutputStream(fileNameOr, true);
writer = new BufferedWriter(new OutputStreamWriter(tempFile, "UTF8"));
String sql = "SELECT ta.TableName" String sql = "SELECT ta.TableName"
+" FROM AD_Table ta" +" FROM AD_Table ta"
+" WHERE ta.IsActive='Y'" +" WHERE ta.IsActive='Y'"
@ -164,53 +161,6 @@ public class FitRecorder implements ModelValidator {
@Override @Override
public String login(int AD_Org_ID, int AD_Role_ID, int AD_User_ID) { public String login(int AD_Org_ID, int AD_Role_ID, int AD_User_ID) {
try {
writer.append("\n");
writer.append("\n");
writer.append("LOGIN");
writer.append("\n");
writer.append("!");
writer.append("|Login|");
writer.append("\n");
MUser user = MUser.get(Env.getCtx(), AD_User_ID);
writer.append("|User|");
if (MSysConfig.getBooleanValue(MSysConfig.USE_EMAIL_FOR_LOGIN, false))
writer.append(user.getEMail() + "|");
else
if (user.getLDAPUser() != null)
writer.append(user.getLDAPUser() + "|");
else
writer.append(user.getName() + "|");
writer.append("\n");
writer.append("|Password|");
writer.append(user.getPassword() + "|");
writer.append("\n");
writer.append("|AD_Client_ID|");
MClient client = MClient.get(Env.getCtx(), Env.getContextAsInt(Env.getCtx(), "#AD_Client_ID"));
writer.append("@Ref=AD_Client[Name='" + client.getName() + "'].AD_Client_ID|");
writer.append("\n");
writer.append("|AD_Org_ID|");
String orgName = DB.getSQLValueString(null, "SELECT Name FROM AD_Org WHERE AD_Org_ID=?", AD_Org_ID);
writer.append("@Ref=AD_Org[Name='" + orgName + "'].AD_Org_ID|");
writer.append("\n");
writer.append("|AD_Role_ID|");
writer.append("@Ref=AD_Role[Name='" + Env.getContext(Env.getCtx(), "#AD_Role_Name") + "'].AD_Role_ID|");
writer.append("\n");
int warehouseid = Env.getContextAsInt(Env.getCtx(), Env.M_WAREHOUSE_ID);
if (warehouseid > 0) {
MWarehouse warehouse = MWarehouse.get(Env.getCtx(), warehouseid);
writer.append("|M_Warehouse_ID|");
writer.append("@Ref=M_Warehouse[Name='" + warehouse.getName() + "'].M_Warehouse_ID|");
writer.append("\n");
}
writer.append("|*Login*|");
writer.append("\n");
writer.flush();
} catch (Exception e) {
return e.getLocalizedMessage();
}
return null; return null;
} }
@ -219,6 +169,10 @@ public class FitRecorder implements ModelValidator {
try { try {
if (type == TYPE_AFTER_NEW ) { if (type == TYPE_AFTER_NEW ) {
if (po instanceof MSession) {
loginFixture(po);
}
if (dontLogTables.contains(po.get_TableName().toUpperCase())) if (dontLogTables.contains(po.get_TableName().toUpperCase()))
return null; return null;
@ -230,15 +184,15 @@ public class FitRecorder implements ModelValidator {
if (po.get_TrxName().startsWith("WFP_")) if (po.get_TrxName().startsWith("WFP_"))
return null; return null;
writer.append("\n"); writeFile("\n");
writer.append("\n"); writeFile("\n");
writer.append("CREATE RECORD"); writeFile("CREATE RECORD");
writer.append("\n"); writeFile("\n");
writer.append("!"); writeFile("!");
writer.append("|Create Record||"); writeFile("|Create Record||");
writer.append("\n"); writeFile("\n");
writer.append("|*Table*|"); writeFile("|*Table*|");
writer.append(po.get_TableName() + "|"); writeFile(po.get_TableName() + "|");
MTable table = MTable.get(Env.getCtx(),po.get_Table_ID()); MTable table = MTable.get(Env.getCtx(),po.get_Table_ID());
for(MColumn column : table.getColumns(false)) { for(MColumn column : table.getColumns(false)) {
@ -260,19 +214,18 @@ public class FitRecorder implements ModelValidator {
String value = po.get_ValueAsString(colName); String value = po.get_ValueAsString(colName);
if (value != null && value.length() > 0) { if (value != null && value.length() > 0) {
writer.append("\n"); writeFile("\n");
writer.append("|" + colName + "|"); writeFile("|" + colName + "|");
if (DisplayType.isLookup(column.getAD_Reference_ID()) && DisplayType.List != column.getAD_Reference_ID()) { if (DisplayType.isLookup(column.getAD_Reference_ID()) && DisplayType.List != column.getAD_Reference_ID()) {
writer.append(resolveValue(po, table, column) + "|"); writeFile(resolveValue(po, table, column) + "|");
} else { } else {
writer.append(value +"|"); writeFile(value +"|");
} }
} }
} }
writer.append("\n"); writeFile("\n");
writer.append("|*Save*|"); writeFile("|*Save*|");
writer.append("\n"); writeFile("\n");
writer.flush();
} }
@ -281,15 +234,15 @@ public class FitRecorder implements ModelValidator {
MProcess pro = MProcess.get(Env.getCtx(), po.get_ValueAsInt("AD_Process_ID")); MProcess pro = MProcess.get(Env.getCtx(), po.get_ValueAsInt("AD_Process_ID"));
MPInstance pint = (MPInstance)po; MPInstance pint = (MPInstance)po;
writer.append("\n"); writeFile("\n");
writer.append("\n"); writeFile("\n");
writer.append("RUN PROCESS"); writeFile("RUN PROCESS");
writer.append("\n"); writeFile("\n");
writer.append("!"); writeFile("!");
writer.append("|Run Process|"); writeFile("|Run Process|");
writer.append("\n"); writeFile("\n");
writer.append("|*ProcessValue*|"); writeFile("|*ProcessValue*|");
writer.append(pro.getValue() + "|"); writeFile(pro.getValue() + "|");
MPInstancePara[] iparas = pint.getParameters(); MPInstancePara[] iparas = pint.getParameters();
for (MProcessPara para : pro.getParameters()) { for (MProcessPara para : pro.getParameters()) {
MPInstancePara ipara = null; MPInstancePara ipara = null;
@ -327,24 +280,32 @@ public class FitRecorder implements ModelValidator {
valueTo = ipara.getP_String_To(); valueTo = ipara.getP_String_To();
} }
if (value != null) { if (value != null) {
writer.append("\n"); writeFile("\n");
writer.append("|" + para.getColumnName() + "|"); writeFile("|" + para.getColumnName() + "|");
writer.append(value + "|"); writeFile(value + "|");
} }
if (para.isRange() && valueTo != null) { if (para.isRange() && valueTo != null) {
writer.append("\n"); writeFile("\n");
writer.append("|" + para.getColumnName() + "_2|"); writeFile("|" + para.getColumnName() + "_2|");
writer.append(valueTo + "|"); writeFile(valueTo + "|");
} }
} }
writer.append("\n"); writeFile("\n");
writer.append("|*Run*|"); writeFile("|*Run*|");
writer.append("\n"); writeFile("\n");
writer.flush();
} }
} }
}catch (Exception e) {
if (po instanceof MSession) {
if (type == TYPE_AFTER_CHANGE) {
MSession session = (MSession) po;
if (session.isProcessed()) {
closefile();
}
}
}
} catch (Exception e) {
return e.getLocalizedMessage(); return e.getLocalizedMessage();
} }
return null; return null;
@ -435,47 +396,46 @@ public class FitRecorder implements ModelValidator {
if (timing == TIMING_BEFORE_POST) if (timing == TIMING_BEFORE_POST)
action = "PO"; action = "PO";
writer.append("\n"); writeFile("\n");
writer.append("\n"); writeFile("\n");
if (action.equals("CO")) { if (action.equals("CO")) {
// run process // run process
String processValue = DB.getSQLValueString(po.get_TrxName(), String processValue = DB.getSQLValueString(po.get_TrxName(),
"SELECT p.Value FROM AD_Process p JOIN AD_Workflow w ON (p.AD_Workflow_ID=w.AD_Workflow_ID) WHERE w.AD_Table_ID=?", po.get_Table_ID()); "SELECT p.Value FROM AD_Process p JOIN AD_Workflow w ON (p.AD_Workflow_ID=w.AD_Workflow_ID) WHERE w.AD_Table_ID=?", po.get_Table_ID());
writer.append("RUN PROCESS"); writeFile("RUN PROCESS");
writer.append("\n"); writeFile("\n");
writer.append("!"); writeFile("!");
writer.append("|Run Process|"); writeFile("|Run Process|");
writer.append("\n"); writeFile("\n");
writer.append("|*ProcessValue*|"); writeFile("|*ProcessValue*|");
writer.append(processValue + "|"); writeFile(processValue + "|");
writer.append("\n"); writeFile("\n");
writer.append("|*RecordID*|"); writeFile("|*RecordID*|");
writer.append(resolveValueDoc(po.get_ID(), po.get_TableName()) + "|"); writeFile(resolveValueDoc(po.get_ID(), po.get_TableName()) + "|");
writer.append("\n"); writeFile("\n");
writer.append("|*DocAction*|"); writeFile("|*DocAction*|");
writer.append(action + "|"); writeFile(action + "|");
writer.append("\n"); writeFile("\n");
writer.append("|*Run*|"); writeFile("|*Run*|");
} else { } else {
// set doc action // set doc action
writer.append("SET DOC ACTION"); writeFile("SET DOC ACTION");
writer.append("\n"); writeFile("\n");
writer.append("!"); writeFile("!");
writer.append("|Set DocAction|"); writeFile("|Set DocAction|");
writer.append("\n"); writeFile("\n");
writer.append("|*Table*|"); writeFile("|*Table*|");
writer.append(po.get_TableName() + "|"); writeFile(po.get_TableName() + "|");
writer.append("\n"); writeFile("\n");
writer.append("|" + po.get_TableName() + "_ID|"); writeFile("|" + po.get_TableName() + "_ID|");
writer.append(resolveValueDoc(po.get_ID(), po.get_TableName()) + "|"); writeFile(resolveValueDoc(po.get_ID(), po.get_TableName()) + "|");
writer.append("\n"); writeFile("\n");
writer.append("|DocAction|"); writeFile("|DocAction|");
writer.append(action + "|"); writeFile(action + "|");
writer.append("\n"); writeFile("\n");
writer.append("|*Save*|"); writeFile("|*Save*|");
} }
writer.append("\n"); writeFile("\n");
writer.flush();
} }
} catch (Exception e) { } catch (Exception e) {
if (log.isLoggable(Level.INFO)) log.info(e.getLocalizedMessage()); if (log.isLoggable(Level.INFO)) log.info(e.getLocalizedMessage());
@ -514,4 +474,87 @@ public class FitRecorder implements ModelValidator {
return value; return value;
} }
public String loginFixture(PO po){
String msg=null;
String preference = Env.getPreference(Env.getCtx(), 0, "FitRecorder", false);
if (preference.equals("Y")) {
int session_ID = po.get_ID();
int user_ID = Env.getContextAsInt(Env.getCtx(), "#AD_User_ID");
int org_ID = Env.getContextAsInt(Env.getCtx(), "#AD_Org_ID");
MUser user = new MUser(Env.getCtx(), user_ID, null);
String name = Ini.getAdempiereHome() + "/log/fit_test_" + session_ID+ user.getName() + ".txt";
try {
File file = new File(name);
FileOutputStream fos = new FileOutputStream(file, true);
Writer writer = new BufferedWriter(new OutputStreamWriter(fos, "UTF8"));
Env.setContext(Env.getCtx(), "#AD_Session_ID", session_ID);
writersPerSession.put(session_ID, writer);
writeFile("\n");
writeFile("\n");
writeFile("LOGIN");
writeFile("\n");
writeFile("!");
writeFile("|Login|");
writeFile("\n");
writeFile("|User|");
if (MSysConfig.getBooleanValue(MSysConfig.USE_EMAIL_FOR_LOGIN,
false))
writeFile(user.getEMail() + "|");
else if (user.getLDAPUser() != null)
writeFile(user.getLDAPUser() + "|");
else
writeFile(user.getName() + "|");
writeFile("\n");
writeFile("|Password|");
writeFile(" |");
writeFile("\n");
writeFile("|AD_Client_ID|");
MClient client = MClient.get(Env.getCtx(),Env.getContextAsInt(Env.getCtx(), "#AD_Client_ID"));
writeFile("@Ref=AD_Client[Name='" + client.getName() + "'].AD_Client_ID|");
writeFile("\n");
writeFile("|AD_Org_ID|");
String orgName = DB.getSQLValueString(null,"SELECT Name FROM AD_Org WHERE AD_Org_ID=?", org_ID);
writeFile("@Ref=AD_Org[Name='" + orgName + "'].AD_Org_ID|");
writeFile("\n");
writeFile("|AD_Role_ID|");
writeFile("@Ref=AD_Role[Name='"+ Env.getContext(Env.getCtx(), "#AD_Role_Name")+ "'].AD_Role_ID|");
writeFile("\n");
int warehouseid = Env.getContextAsInt(Env.getCtx(),Env.M_WAREHOUSE_ID);
if (warehouseid > 0) {
MWarehouse warehouse = MWarehouse.get(Env.getCtx(),warehouseid);
writeFile("|M_Warehouse_ID|");
writeFile("@Ref=M_Warehouse[Name='" + warehouse.getName()+ "'].M_Warehouse_ID|");
writeFile("\n");
}
writeFile("|*Login*|");
writeFile("\n");
} catch (Exception e) {
return e.getLocalizedMessage();
}
}
return msg;
}
public void writeFile(String msg){
int session_ID=Env.getContextAsInt(Env.getCtx(), "#AD_Session_ID");
Writer writer = (Writer) writersPerSession.get(session_ID);
if (writer != null) {
try{
writer.append(msg);
writer.flush();
} catch (Exception e) {}
}
}
public void closefile(){
int Session_ID=Env.getContextAsInt(Env.getCtx(), "#AD_Session_ID");
Writer writer = (Writer) writersPerSession.get(Session_ID);
if (writer != null) {
try {
writer.close();
writersPerSession.remove(Session_ID);
} catch (IOException e) {}
}
}
} }