diff --git a/base/src/org/adempiere/util/MeasureInterface.java b/base/src/org/adempiere/util/MeasureInterface.java new file mode 100755 index 0000000000..ad83c2d5c6 --- /dev/null +++ b/base/src/org/adempiere/util/MeasureInterface.java @@ -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 diff --git a/base/src/org/compiere/model/I_AD_Rule.java b/base/src/org/compiere/model/I_AD_Rule.java index 6687e3cbcb..68f0aa8cd7 100644 --- a/base/src/org/compiere/model/I_AD_Rule.java +++ b/base/src/org/compiere/model/I_AD_Rule.java @@ -35,9 +35,9 @@ public interface I_AD_Rule 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 */ diff --git a/base/src/org/compiere/model/MMeasure.java b/base/src/org/compiere/model/MMeasure.java index 09af3aaa59..c7121f2418 100644 --- a/base/src/org/compiere/model/MMeasure.java +++ b/base/src/org/compiere/model/MMeasure.java @@ -22,14 +22,20 @@ import java.sql.ResultSet; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Properties; +import java.util.StringTokenizer; import java.util.logging.Level; +import javax.script.ScriptEngine; + import org.adempiere.apps.graph.GraphColumn; +import org.adempiere.util.MeasureInterface; import org.compiere.util.CCache; import org.compiere.util.DB; import org.compiere.util.Env; import org.compiere.util.Msg; import org.compiere.util.TimeUtil; +import org.compiere.util.Util; + /** * Performance Measure @@ -39,6 +45,9 @@ import org.compiere.util.TimeUtil; * * @author Teo Sarca, SC ARHIPAC SERVICE SRL *
  • BF [ 1887674 ] Deadlock when try to modify PA Goal's Measure Target + * @author victor.perez@e-evolution.com, www.e-evolution.com + *
  • FR [ 2905227 ] Calculate Measure based on the script to PA + *
  • https://sourceforge.net/tracker/?func=detail&aid=2905227&group_id=176962&atid=879335 */ public class MMeasure extends X_PA_Measure { @@ -355,6 +364,8 @@ public class MMeasure extends X_PA_Measure return updateRequests(); else if (MEASURETYPE_Project.equals(mt)) return updateProjects(); + else if(MEASURETYPE_UserDefined.equals(mt)) + return updateUserDefined(); // Projects } catch (Exception e) @@ -363,7 +374,7 @@ public class MMeasure extends X_PA_Measure } return false; } // updateGoals - + /** * Update/save Manual Goals * @return true if updated @@ -568,5 +579,88 @@ public class MMeasure extends X_PA_Measure } return true; } // 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 diff --git a/base/src/org/compiere/model/X_AD_Rule.java b/base/src/org/compiere/model/X_AD_Rule.java index 503fd2f25e..3313ebcf63 100644 --- a/base/src/org/compiere/model/X_AD_Rule.java +++ b/base/src/org/compiere/model/X_AD_Rule.java @@ -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 */ 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 - * @return 4 - System + * @return 6 - System - Client */ 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"; /** Human Resource & Payroll = H */ public static final String EVENTTYPE_HumanResourcePayroll = "H"; + /** Measure for Performance Analysis = M */ + public static final String EVENTTYPE_MeasureForPerformanceAnalysis = "M"; /** Set Event Type. @param EventType Type of Event diff --git a/migration/354a-trunk/oracle/612_FR2905227_EventTypeForMeasurePA.sql b/migration/354a-trunk/oracle/612_FR2905227_EventTypeForMeasurePA.sql new file mode 100644 index 0000000000..1c43891ace --- /dev/null +++ b/migration/354a-trunk/oracle/612_FR2905227_EventTypeForMeasurePA.sql @@ -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) +; + diff --git a/migration/354a-trunk/postgresql/612_FR2905227_EventTypeForMeasurePA.sql b/migration/354a-trunk/postgresql/612_FR2905227_EventTypeForMeasurePA.sql new file mode 100644 index 0000000000..0997b457f5 --- /dev/null +++ b/migration/354a-trunk/postgresql/612_FR2905227_EventTypeForMeasurePA.sql @@ -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) +; +