Calculate Measure based on the script to PA

https://sourceforge.net/tracker/?func=detail&aid=2905227&group_id=176962&atid=879335
This commit is contained in:
vpj-cd 2009-11-28 17:21:32 +00:00
parent 75eaad16b9
commit 9669815872
6 changed files with 153 additions and 6 deletions

View File

@ -0,0 +1,31 @@
/******************************************************************************
* Product: Adempiere ERP & CRM Smart Business Solution *
* This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program is distributed in the hope *
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
* For the text or an alternative of this public license, you may reach us *
* Copyright (C) 2003-2007 e-Evolution,SC. All Rights Reserved. *
* Contributor(s): Victor Perez www.e-evolution.com *
*****************************************************************************/
package org.adempiere.util;
import java.math.BigDecimal;
/**
* Custom Measure Interface
*
* @author Jorg Janke
*/
public interface MeasureInterface
{
/**
* @return measure value
*/
public BigDecimal getValue();
} // ReplenishmentInterface

View File

@ -35,9 +35,9 @@ public interface I_AD_Rule
KeyNamePair Model = new KeyNamePair(Table_ID, Table_Name); KeyNamePair Model = new KeyNamePair(Table_ID, Table_Name);
/** AccessLevel = 4 - System /** AccessLevel = 6 - System - Client
*/ */
BigDecimal accessLevel = BigDecimal.valueOf(4); BigDecimal accessLevel = BigDecimal.valueOf(6);
/** Load Meta Data */ /** Load Meta Data */

View File

@ -22,14 +22,20 @@ import java.sql.ResultSet;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Properties; import java.util.Properties;
import java.util.StringTokenizer;
import java.util.logging.Level; import java.util.logging.Level;
import javax.script.ScriptEngine;
import org.adempiere.apps.graph.GraphColumn; import org.adempiere.apps.graph.GraphColumn;
import org.adempiere.util.MeasureInterface;
import org.compiere.util.CCache; import org.compiere.util.CCache;
import org.compiere.util.DB; import org.compiere.util.DB;
import org.compiere.util.Env; import org.compiere.util.Env;
import org.compiere.util.Msg; import org.compiere.util.Msg;
import org.compiere.util.TimeUtil; import org.compiere.util.TimeUtil;
import org.compiere.util.Util;
/** /**
* Performance Measure * Performance Measure
@ -39,6 +45,9 @@ import org.compiere.util.TimeUtil;
* *
* @author Teo Sarca, SC ARHIPAC SERVICE SRL * @author Teo Sarca, SC ARHIPAC SERVICE SRL
* <li>BF [ 1887674 ] Deadlock when try to modify PA Goal's Measure Target * <li>BF [ 1887674 ] Deadlock when try to modify PA Goal's Measure Target
* @author victor.perez@e-evolution.com, www.e-evolution.com
* <li>FR [ 2905227 ] Calculate Measure based on the script to PA
* <li>https://sourceforge.net/tracker/?func=detail&aid=2905227&group_id=176962&atid=879335
*/ */
public class MMeasure extends X_PA_Measure public class MMeasure extends X_PA_Measure
{ {
@ -355,6 +364,8 @@ public class MMeasure extends X_PA_Measure
return updateRequests(); return updateRequests();
else if (MEASURETYPE_Project.equals(mt)) else if (MEASURETYPE_Project.equals(mt))
return updateProjects(); return updateProjects();
else if(MEASURETYPE_UserDefined.equals(mt))
return updateUserDefined();
// Projects // Projects
} }
catch (Exception e) catch (Exception e)
@ -568,5 +579,88 @@ public class MMeasure extends X_PA_Measure
} }
return true; return true;
} // updateProjects } // updateProjects
/**
* Update/save update User Defined
* @return true if updated
*/
private boolean updateUserDefined()
{
MGoal[] goals = MGoal.getMeasureGoals (getCtx(), getPA_Measure_ID());
for (MGoal goal:goals)
{
BigDecimal amt = Env.ZERO;
PO po = new MTable(getCtx(),get_Table_ID(),get_TrxName()).getPO(get_ID(), get_TrxName());
StringTokenizer st = new StringTokenizer(getCalculationClass(), ";,", false);
while (st.hasMoreTokens()) // for each class
{
String cmd = st.nextToken().trim();
String retValue = "";
if (cmd.toLowerCase().startsWith(MRule.SCRIPT_PREFIX)) {
MRule rule = MRule.get(getCtx(), cmd.substring(MRule.SCRIPT_PREFIX.length()));
if (rule == null) {
retValue = "Script " + cmd + " not found";
log.log(Level.SEVERE, retValue);
break;
}
if ( ! (rule.getEventType().equals(MRule.EVENTTYPE_ModelValidatorTableEvent)
&& rule.getRuleType().equals(MRule.RULETYPE_JSR223ScriptingAPIs))) {
retValue = "Script " + cmd
+ " must be of type JSR 223 and event measure";
log.log(Level.SEVERE, retValue);
break;
}
ScriptEngine engine = rule.getScriptEngine();
MRule.setContext(engine, po.getCtx(), 0);
engine.put(MRule.ARGUMENTS_PREFIX + "Ctx", po.getCtx());
engine.put(MRule.ARGUMENTS_PREFIX + "PO", po);
try
{
Object value = engine.eval(rule.getScript());
amt = (BigDecimal)value;
}
catch (Exception e)
{
log.log(Level.SEVERE, "", e);
retValue = "Script Invalid: " + e.toString();
return false;
}
}
else
{
MeasureInterface custom = null;
try
{
Class<?> clazz = Class.forName(cmd);
custom = (MeasureInterface)clazz.newInstance();
}
catch (Exception e)
{
log.log(Level.SEVERE, "No custom measure class "
+ cmd + " - " + e.toString(), e);
return false;
}
try
{
amt = custom.getValue();
}
catch (Exception e)
{
log.log(Level.SEVERE, custom.toString(), e);
return false;
}
}
if (!Util.isEmpty(retValue)) // interrupt on first error
{
log.severe (retValue);
return false;
}
}
goal.setMeasureActual(amt);
goal.save(get_TrxName());
}
return true;
} // updateUserDefinedGoals
} // MMeasure } // MMeasure

View File

@ -30,7 +30,7 @@ public class X_AD_Rule extends PO implements I_AD_Rule, I_Persistent
/** /**
* *
*/ */
private static final long serialVersionUID = 20091106L; private static final long serialVersionUID = 20091128L;
/** Standard Constructor */ /** Standard Constructor */
public X_AD_Rule (Properties ctx, int AD_Rule_ID, String trxName) public X_AD_Rule (Properties ctx, int AD_Rule_ID, String trxName)
@ -55,7 +55,7 @@ public class X_AD_Rule extends PO implements I_AD_Rule, I_Persistent
} }
/** AccessLevel /** AccessLevel
* @return 4 - System * @return 6 - System - Client
*/ */
protected int get_AccessLevel() protected int get_AccessLevel()
{ {
@ -179,6 +179,8 @@ public class X_AD_Rule extends PO implements I_AD_Rule, I_Persistent
public static final String EVENTTYPE_ModelValidatorLoginEvent = "L"; public static final String EVENTTYPE_ModelValidatorLoginEvent = "L";
/** Human Resource & Payroll = H */ /** Human Resource & Payroll = H */
public static final String EVENTTYPE_HumanResourcePayroll = "H"; public static final String EVENTTYPE_HumanResourcePayroll = "H";
/** Measure for Performance Analysis = M */
public static final String EVENTTYPE_MeasureForPerformanceAnalysis = "M";
/** Set Event Type. /** Set Event Type.
@param EventType @param EventType
Type of Event Type of Event

View File

@ -0,0 +1,10 @@
-- Nov 28, 2009 11:03:18 AM CST
-- Improve Performace Analisis
INSERT INTO AD_Ref_List (AD_Client_ID,AD_Org_ID,AD_Ref_List_ID,AD_Reference_ID,Created,CreatedBy,EntityType,IsActive,Name,Updated,UpdatedBy,Value) VALUES (0,0,53556,53236,TO_DATE('2009-11-28 11:03:06','YYYY-MM-DD HH24:MI:SS'),0,'D','Y','Measure for Performance Analysis',TO_DATE('2009-11-28 11:03:06','YYYY-MM-DD HH24:MI:SS'),0,'M')
;
-- Nov 28, 2009 11:03:18 AM CST
-- Improve Performace Analisis
INSERT INTO AD_Ref_List_Trl (AD_Language,AD_Ref_List_ID, Description,Name, IsTranslated,AD_Client_ID,AD_Org_ID,Created,Createdby,Updated,UpdatedBy) SELECT l.AD_Language,t.AD_Ref_List_ID, t.Description,t.Name, 'N',t.AD_Client_ID,t.AD_Org_ID,t.Created,t.Createdby,t.Updated,t.UpdatedBy FROM AD_Language l, AD_Ref_List t WHERE l.IsActive='Y' AND l.IsSystemLanguage='Y' AND l.IsBaseLanguage='N' AND t.AD_Ref_List_ID=53556 AND NOT EXISTS (SELECT * FROM AD_Ref_List_Trl tt WHERE tt.AD_Language=l.AD_Language AND tt.AD_Ref_List_ID=t.AD_Ref_List_ID)
;

View File

@ -0,0 +1,10 @@
-- Nov 28, 2009 11:03:18 AM CST
-- Improve Performace Analisis
INSERT INTO AD_Ref_List (AD_Client_ID,AD_Org_ID,AD_Ref_List_ID,AD_Reference_ID,Created,CreatedBy,EntityType,IsActive,Name,Updated,UpdatedBy,Value) VALUES (0,0,53556,53236,TO_TIMESTAMP('2009-11-28 11:03:06','YYYY-MM-DD HH24:MI:SS'),0,'D','Y','Measure for Performance Analysis',TO_TIMESTAMP('2009-11-28 11:03:06','YYYY-MM-DD HH24:MI:SS'),0,'M')
;
-- Nov 28, 2009 11:03:18 AM CST
-- Improve Performace Analisis
INSERT INTO AD_Ref_List_Trl (AD_Language,AD_Ref_List_ID, Description,Name, IsTranslated,AD_Client_ID,AD_Org_ID,Created,Createdby,Updated,UpdatedBy) SELECT l.AD_Language,t.AD_Ref_List_ID, t.Description,t.Name, 'N',t.AD_Client_ID,t.AD_Org_ID,t.Created,t.Createdby,t.Updated,t.UpdatedBy FROM AD_Language l, AD_Ref_List t WHERE l.IsActive='Y' AND l.IsSystemLanguage='Y' AND l.IsBaseLanguage='N' AND t.AD_Ref_List_ID=53556 AND NOT EXISTS (SELECT * FROM AD_Ref_List_Trl tt WHERE tt.AD_Language=l.AD_Language AND tt.AD_Ref_List_ID=t.AD_Ref_List_ID)
;