436 lines
13 KiB
Java
436 lines
13 KiB
Java
package andromedia.midsuit.process;
|
|
|
|
import java.lang.reflect.Constructor;
|
|
import java.lang.reflect.Method;
|
|
import java.math.BigDecimal;
|
|
import java.sql.ResultSet;
|
|
import java.sql.Timestamp;
|
|
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.Properties;
|
|
import java.util.Set;
|
|
import java.util.logging.Level;
|
|
|
|
import org.adempiere.base.DefaultModelFactory;
|
|
import org.adempiere.exceptions.AdempiereException;
|
|
import org.compiere.model.MEntityType;
|
|
import org.compiere.model.MTable;
|
|
import org.compiere.model.PO;
|
|
import org.compiere.model.Query;
|
|
import org.compiere.process.ProcessInfoParameter;
|
|
import org.compiere.process.SvrProcess;
|
|
import org.compiere.util.CCache;
|
|
import org.compiere.util.CLogger;
|
|
import org.compiere.util.Env;
|
|
import org.compiere.util.Trx;
|
|
import org.compiere.util.Util;
|
|
import org.zkoss.json.JSONObject;
|
|
import org.zkoss.json.parser.JSONParser;
|
|
|
|
public class MID_InsertIntoAllTable extends SvrProcess{
|
|
private static CCache<String,Class<?>> s_classCache = new CCache<String,Class<?>>(null, "PO_Class", 20, false);
|
|
private final static CLogger s_log = CLogger.getCLogger(DefaultModelFactory.class);
|
|
private String jsonString = "";
|
|
private String tableName = "";
|
|
@Override
|
|
protected void prepare() {
|
|
ProcessInfoParameter[] para = getParameter();
|
|
for (int i = 0; i < para.length; i++) {
|
|
String name = para[i].getParameterName();
|
|
if (para[i].getParameter() == null)
|
|
;
|
|
if (para[i].getParameterName().equals("jsonString"))
|
|
jsonString = para[i].getParameterAsString();
|
|
else
|
|
log.log(Level.SEVERE, "Unknown Parameter: " + name);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected String doIt() throws Exception {
|
|
JSONParser parser = new JSONParser();
|
|
JSONObject JSON = (JSONObject) parser.parse(jsonString);
|
|
if(JSON!=null) {
|
|
PO po = null;
|
|
|
|
tableName = JSON.get("TableName").toString();
|
|
JSONObject Data = (JSONObject) JSON.get("Data");
|
|
String Mode = JSON.get("Mode").toString();
|
|
String whereClause = JSON.get("WhereClause").toString();
|
|
if(Mode.equals("I")) {
|
|
po = getPO(tableName, 0, get_TrxName());
|
|
|
|
}
|
|
else if (Mode.equals("U")) {
|
|
if(whereClause.length()>0) {
|
|
JSONObject params = (JSONObject) JSON.get("Params");
|
|
Set<Object> whereParams = params.keySet();
|
|
Iterator<Object> keysWhereParams = whereParams.iterator();
|
|
List<Object> parameters = new ArrayList<Object>();
|
|
while (keysWhereParams.hasNext()){
|
|
String paramsColumn = keysWhereParams.next().toString();
|
|
String forSplit = params.get(paramsColumn).toString();
|
|
Object p = params.get(paramsColumn);
|
|
if(forSplit.split("-").length==3 && forSplit.split(":").length==3)
|
|
parameters.add((Timestamp) params.get(paramsColumn));
|
|
else{
|
|
if(paramsColumn.contains("Date") || paramsColumn.contains("Time"))
|
|
parameters.add((Timestamp) params.get(paramsColumn));
|
|
else{
|
|
try {
|
|
parameters.add(Integer.parseInt(params.get(paramsColumn).toString()));
|
|
continue;
|
|
} catch (NumberFormatException e) {}
|
|
|
|
try{
|
|
parameters.add(new BigDecimal(params.get(paramsColumn).toString()));
|
|
continue;
|
|
}catch (Exception e){}
|
|
|
|
parameters.add(params.get(paramsColumn));
|
|
}
|
|
}
|
|
}
|
|
|
|
Object[] paramValues = parameters.toArray(new Object[parameters.size()]);
|
|
String r="";
|
|
|
|
int[] IDs = new Query(getCtx(), tableName, whereClause,get_TrxName())
|
|
.setParameters(paramValues)
|
|
.setOnlyActiveRecords(true)
|
|
.getIDs();
|
|
|
|
for(int ID : IDs) {
|
|
PO obj = null;
|
|
obj = getPO(tableName, ID, get_TrxName());
|
|
Set<Object> keySetMass= Data.keySet();
|
|
Iterator<Object> keysMass = keySetMass.iterator();
|
|
while(keysMass.hasNext()) {
|
|
String columnInsert = keysMass.next().toString();
|
|
obj.set_ValueNoCheck(columnInsert, Data.get(columnInsert).toString());
|
|
if(columnInsert.equals("Record_ID"))
|
|
continue;
|
|
else if (columnInsert.contains("Date") || columnInsert.contains("Time"))
|
|
{
|
|
Timestamp time = Timestamp.valueOf(Data.get(columnInsert).toString());
|
|
obj.set_ValueOfColumn(columnInsert, time);
|
|
}
|
|
else{
|
|
if(obj.set_ValueOfColumnReturningBoolean(columnInsert, Data.get(columnInsert))){ }
|
|
else if(obj.set_ValueOfColumnReturningBoolean(columnInsert, new BigDecimal(Data.get(columnInsert).toString()))){ }
|
|
else{throw new AdempiereException("Cannot Fill Column "+columnInsert);}
|
|
if(obj.get_Value(columnInsert)==null) throw new AdempiereException("Cannot Fill Column "+columnInsert);
|
|
}
|
|
// r = r+" "+columnInsert+":"+Data.get(columnInsert).toString();
|
|
}
|
|
// if(r.length()>0) return r;
|
|
if(!obj.save(get_TrxName())){
|
|
throw new AdempiereException("Failed to Update !!!");
|
|
}
|
|
}
|
|
return "Update Mass Data Success !!";
|
|
}else {
|
|
int Record_ID = Integer.parseInt(Data.get("Record_ID").toString());
|
|
po = getPO(tableName, Record_ID, get_TrxName());
|
|
}
|
|
}
|
|
|
|
Set<Object> keySet= Data.keySet();
|
|
Iterator<Object> keys = keySet.iterator();
|
|
if(whereClause.length()==0){
|
|
String columnset = "";
|
|
while(keys.hasNext()) {
|
|
String columnInsert = keys.next().toString();
|
|
if(columnInsert.equals("Record_ID"))
|
|
continue;
|
|
else if (columnInsert.contains("Date") || columnInsert.contains("Time"))
|
|
{
|
|
Timestamp time = Timestamp.valueOf(Data.get(columnInsert).toString());
|
|
po.set_ValueOfColumn(columnInsert, time);
|
|
}
|
|
else{
|
|
if(po.set_ValueOfColumnReturningBoolean(columnInsert, Data.get(columnInsert))){ }
|
|
else if(po.set_ValueOfColumnReturningBoolean(columnInsert, new BigDecimal(Data.get(columnInsert).toString()))){ }
|
|
else{throw new AdempiereException("Cannot Fill Column "+columnInsert);}
|
|
if(po.get_Value(columnInsert)==null) throw new AdempiereException("Cannot Fill Column "+columnInsert);
|
|
|
|
}
|
|
}
|
|
if(!po.save(get_TrxName()))
|
|
throw new AdempiereException(po.get_Logger().retrieveError().getName());
|
|
|
|
// if(po.get_Value("DocAction")!=null) {
|
|
// Class<?> clazz = getClass(tableName);
|
|
// if (clazz == null)
|
|
// {
|
|
// return null;
|
|
// }
|
|
//
|
|
// Constructor<?> constructor = clazz.getDeclaredConstructor(new Class[]{Properties.class, ResultSet.class, String.class});
|
|
// Object classDocument = constructor.newInstance(new Object[] {Env.getCtx(), po.get_ID(), get_TrxName()});
|
|
// Method meth = clazz.getDeclaredMethod("processIt", String.class);
|
|
// try {
|
|
// meth.invoke(classDocument, po.get_ValueAsString("DocAction"));
|
|
// }catch(Exception e) {
|
|
// return "Failed to Process Document !!";
|
|
// }
|
|
// }
|
|
|
|
if(Trx.get(get_TrxName(), false).commit()){
|
|
return String.valueOf(po.get_ID());
|
|
}else
|
|
throw new AdempiereException(po.get_Logger().retrieveError().getName());
|
|
|
|
|
|
}
|
|
}else {
|
|
return "No JSON to be processed !";
|
|
}
|
|
|
|
/**
|
|
* Condition to be met
|
|
* 1. TableName must not null / length>0
|
|
* 2. Data Exists
|
|
* 3. Mode to be used
|
|
*/
|
|
|
|
return null;
|
|
}
|
|
|
|
private static final String[] s_packages = new String[] {
|
|
|
|
"org.compiere.model", "org.compiere.wf",
|
|
"org.compiere.print", "org.compiere.impexp",
|
|
"compiere.model", // globalqss allow compatibility with other plugins
|
|
"adempiere.model", // Extensions
|
|
"org.adempiere.model",
|
|
"andromedia.midsuit.model"
|
|
};
|
|
|
|
public Class<?> getClass(String tableName) {
|
|
// Not supported
|
|
if (tableName == null || tableName.endsWith("_Trl"))
|
|
return null;
|
|
|
|
//check cache
|
|
Class<?> cache = s_classCache.get(tableName);
|
|
if (cache != null)
|
|
{
|
|
//Object.class indicate no generated PO class for tableName
|
|
if (cache.equals(Object.class))
|
|
return null;
|
|
else
|
|
return cache;
|
|
}
|
|
|
|
MTable table = MTable.get(Env.getCtx(), tableName);
|
|
String entityType = table.getEntityType();
|
|
|
|
if (!MEntityType.ENTITYTYPE_Dictionary.equals(entityType))
|
|
{
|
|
MEntityType et = MEntityType.get(Env.getCtx(), entityType);
|
|
String etmodelpackage = et.getModelPackage();
|
|
if (etmodelpackage != null)
|
|
{
|
|
Class<?> clazz = null;
|
|
clazz = getPOclass(etmodelpackage + ".M" + Util.replace(tableName, "_", ""), tableName);
|
|
if (clazz != null) {
|
|
s_classCache.put(tableName, clazz);
|
|
return clazz;
|
|
}
|
|
clazz = getPOclass(etmodelpackage + ".X_" + tableName, tableName);
|
|
if (clazz != null) {
|
|
s_classCache.put(tableName, clazz);
|
|
return clazz;
|
|
}
|
|
s_log.warning("No class for table with it entity: " + tableName);
|
|
}
|
|
}
|
|
//end [ 1784588 ]
|
|
|
|
// Strip table name prefix (e.g. AD_) Customizations are 3/4
|
|
String className = tableName;
|
|
int index = className.indexOf('_');
|
|
if (index > 0)
|
|
{
|
|
if (index < 3) // AD_, A_
|
|
className = className.substring(index+1);
|
|
/* DELETEME: this part is useless - teo_sarca, [ 1648850 ]
|
|
else
|
|
{
|
|
String prefix = className.substring(0,index);
|
|
if (prefix.equals("Fact")) // keep custom prefix
|
|
className = className.substring(index+1);
|
|
}
|
|
*/
|
|
}
|
|
// Remove underlines
|
|
className = Util.replace(className, "_", "");
|
|
|
|
// Search packages
|
|
for (int i = 0; i < s_packages.length; i++)
|
|
{
|
|
StringBuffer name = new StringBuffer(s_packages[i]).append(".M").append(className);
|
|
Class<?> clazz = getPOclass(name.toString(), tableName);
|
|
if (clazz != null)
|
|
{
|
|
s_classCache.put(tableName, clazz);
|
|
return clazz;
|
|
}
|
|
}
|
|
|
|
|
|
// Adempiere Extension
|
|
Class<?> clazz = getPOclass("adempiere.model.X_" + tableName, tableName);
|
|
if (clazz != null)
|
|
{
|
|
s_classCache.put(tableName, clazz);
|
|
return clazz;
|
|
}
|
|
|
|
//hengsin - allow compatibility with compiere plugins
|
|
//Compiere Extension
|
|
clazz = getPOclass("compiere.model.X_" + tableName, tableName);
|
|
if (clazz != null)
|
|
{
|
|
s_classCache.put(tableName, clazz);
|
|
return clazz;
|
|
}
|
|
|
|
// Default
|
|
clazz = getPOclass("org.compiere.model.X_" + tableName, tableName);
|
|
if (clazz != null)
|
|
{
|
|
s_classCache.put(tableName, clazz);
|
|
return clazz;
|
|
}
|
|
|
|
clazz = getPOclass("andromedia.midsuit.model.X_" + tableName, tableName);
|
|
if (clazz != null)
|
|
{
|
|
s_classCache.put(tableName, clazz);
|
|
return clazz;
|
|
}
|
|
//Object.class to indicate no PO class for tableName
|
|
s_classCache.put(tableName, Object.class);
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Get PO class
|
|
* @param className fully qualified class name
|
|
* @param tableName Optional. If specified, the loaded class will be validated for that table name
|
|
* @return class or null
|
|
*/
|
|
private Class<?> getPOclass (String className, String tableName)
|
|
{
|
|
try
|
|
{
|
|
Class<?> clazz = Class.forName(className);
|
|
// Validate if the class is for specified tableName
|
|
if (tableName != null)
|
|
{
|
|
String classTableName = clazz.getField("Table_Name").get(null).toString();
|
|
if (!tableName.equals(classTableName))
|
|
{
|
|
if (s_log.isLoggable(Level.FINEST)) s_log.finest("Invalid class for table: " + className+" (tableName="+tableName+", classTableName="+classTableName+")");
|
|
return null;
|
|
}
|
|
}
|
|
// Make sure that it is a PO class
|
|
Class<?> superClazz = clazz.getSuperclass();
|
|
while (superClazz != null)
|
|
{
|
|
if (superClazz == PO.class)
|
|
{
|
|
if (s_log.isLoggable(Level.FINE)) s_log.fine("Use: " + className);
|
|
return clazz;
|
|
}
|
|
superClazz = superClazz.getSuperclass();
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
}
|
|
if (s_log.isLoggable(Level.FINEST)) s_log.finest("Not found: " + className);
|
|
return null;
|
|
} // getPOclass
|
|
|
|
public PO getPO(String tableName, int Record_ID, String trxName) {
|
|
Class<?> clazz = getClass(tableName);
|
|
if (clazz == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
boolean errorLogged = false;
|
|
try
|
|
{
|
|
Constructor<?> constructor = null;
|
|
try
|
|
{
|
|
constructor = clazz.getDeclaredConstructor(new Class[]{Properties.class, int.class, String.class});
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
String msg = e.getMessage();
|
|
if (msg == null)
|
|
msg = e.toString();
|
|
s_log.warning("No transaction Constructor for " + clazz + " (" + msg + ")");
|
|
}
|
|
|
|
PO po = constructor!=null ? (PO)constructor.newInstance(new Object[] {Env.getCtx(), new Integer(Record_ID), trxName}) : null;
|
|
return po;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
if (e.getCause() != null)
|
|
{
|
|
Throwable t = e.getCause();
|
|
s_log.log(Level.SEVERE, "(id) - Table=" + tableName + ",Class=" + clazz, t);
|
|
errorLogged = true;
|
|
if (t instanceof Exception)
|
|
s_log.saveError("Error", (Exception)e.getCause());
|
|
else
|
|
s_log.saveError("Error", "Table=" + tableName + ",Class=" + clazz);
|
|
}
|
|
else
|
|
{
|
|
s_log.log(Level.SEVERE, "(id) - Table=" + tableName + ",Class=" + clazz, e);
|
|
errorLogged = true;
|
|
s_log.saveError("Error", "Table=" + tableName + ",Class=" + clazz);
|
|
}
|
|
}
|
|
if (!errorLogged)
|
|
s_log.log(Level.SEVERE, "(id) - Not found - Table=" + tableName
|
|
+ ", Record_ID=" + Record_ID);
|
|
return null;
|
|
}
|
|
|
|
public PO getPO(String tableName, ResultSet rs, String trxName) {
|
|
Class<?> clazz = getClass(tableName);
|
|
if (clazz == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
boolean errorLogged = false;
|
|
try
|
|
{
|
|
Constructor<?> constructor = clazz.getDeclaredConstructor(new Class[]{Properties.class, ResultSet.class, String.class});
|
|
PO po = (PO)constructor.newInstance(new Object[] {Env.getCtx(), rs, trxName});
|
|
return po;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
s_log.log(Level.SEVERE, "(rs) - Table=" + tableName + ",Class=" + clazz, e);
|
|
errorLogged = true;
|
|
s_log.saveError("Error", "Table=" + tableName + ",Class=" + clazz);
|
|
}
|
|
if (!errorLogged)
|
|
s_log.log(Level.SEVERE, "(rs) - Not found - Table=" + tableName);
|
|
return null;
|
|
}
|
|
}
|