diff --git a/base/src/org/compiere/model/I_AD_ModelValidator.java b/base/src/org/compiere/model/I_AD_ModelValidator.java new file mode 100644 index 0000000000..89e628cc57 --- /dev/null +++ b/base/src/org/compiere/model/I_AD_ModelValidator.java @@ -0,0 +1,130 @@ +/********************************************************************** + * This file is part of Adempiere ERP Bazaar * + * http://www.adempiere.org * + * * + * Copyright (C) Trifon Trifonov. * + * Copyright (C) Contributors * + * * + * This program is free software; + you can redistribute it and/or * + * modify it under the terms of the GNU General Public License * + * as published by the Free Software Foundation; + either version 2 * + * of the License, or (at your option) any later version. * + * * + * 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., 51 Franklin Street, Fifth Floor, Boston, * + * MA 02110-1301, USA. * + * * + * Contributors: * + * - Trifon Trifonov (trifonnt@users.sourceforge.net) * + * * + * Sponsors: * + * - Company (http://www.site.com) * + **********************************************************************/ +package org.compiere.model; + +import java.math.BigDecimal; +import org.compiere.util.KeyNamePair; + +/** Generated Interface for AD_ModelValidator + * @author Trifon Trifonov (generated) + * @version Release 3.3.0 + */ +public interface I_AD_ModelValidator +{ + + /** TableName=AD_ModelValidator */ + public static final String Table_Name = "AD_ModelValidator"; + + /** AD_Table_ID=53014 */ + public static final int Table_ID = MTable.getTable_ID(Table_Name); + + KeyNamePair Model = new KeyNamePair(Table_ID, Table_Name); + + /** AccessLevel = 4 - System + */ + BigDecimal accessLevel = BigDecimal.valueOf(4); + + /** Load Meta Data */ + + /** Column name AD_ModelValidator_ID */ + public static final String COLUMNNAME_AD_ModelValidator_ID = "AD_ModelValidator_ID"; + + /** Set Model Validator */ + public void setAD_ModelValidator_ID (int AD_ModelValidator_ID); + + /** Get Model Validator */ + public int getAD_ModelValidator_ID(); + + /** Column name Description */ + public static final String COLUMNNAME_Description = "Description"; + + /** Set Description. + * Optional short description of the record + */ + public void setDescription (String Description); + + /** Get Description. + * Optional short description of the record + */ + public String getDescription(); + + /** Column name EntityType */ + public static final String COLUMNNAME_EntityType = "EntityType"; + + /** Set Entity Type. + * Dictionary Entity Type; + Determines ownership and synchronization + */ + public void setEntityType (String EntityType); + + /** Get Entity Type. + * Dictionary Entity Type; + Determines ownership and synchronization + */ + public String getEntityType(); + + /** Column name Help */ + public static final String COLUMNNAME_Help = "Help"; + + /** Set Comment/Help. + * Comment or Hint + */ + public void setHelp (String Help); + + /** Get Comment/Help. + * Comment or Hint + */ + public String getHelp(); + + /** Column name ModelValidationClass */ + public static final String COLUMNNAME_ModelValidationClass = "ModelValidationClass"; + + /** Set Model Validation Class */ + public void setModelValidationClass (String ModelValidationClass); + + /** Get Model Validation Class */ + public String getModelValidationClass(); + + /** Column name Name */ + public static final String COLUMNNAME_Name = "Name"; + + /** Set Name. + * Alphanumeric identifier of the entity + */ + public void setName (String Name); + + /** Get Name. + * Alphanumeric identifier of the entity + */ + public String getName(); +} diff --git a/base/src/org/compiere/model/ModelValidationEngine.java b/base/src/org/compiere/model/ModelValidationEngine.java index f70f866fe9..b7c5223e70 100644 --- a/base/src/org/compiere/model/ModelValidationEngine.java +++ b/base/src/org/compiere/model/ModelValidationEngine.java @@ -19,6 +19,8 @@ package org.compiere.model; import java.beans.*; import java.util.*; import java.util.logging.*; + +import org.compiere.acct.Fact; import org.compiere.util.*; /** @@ -34,6 +36,7 @@ import org.compiere.util.*; */ public class ModelValidationEngine { + /** * Get Singleton * @return engine @@ -56,39 +59,75 @@ public class ModelValidationEngine private ModelValidationEngine () { super (); - // Go through all Clients and start Validators + // Load global validators + + MTable table = MTable.get(new Properties(), X_AD_ModelValidator.Table_ID); + Query query = table.createQuery("IsActive='Y'", null); + try { + List entityTypes = query.list(); + for (PO po : entityTypes) + { + X_AD_ModelValidator entityType = (X_AD_ModelValidator)po; + String className = entityType.getModelValidationClass(); + if (className == null || className.length() == 0) + continue; + loadValidatorClass(null, className); + } + } catch (Exception e) + { + log.warning(e.getLocalizedMessage()); + } + + // Go through all Clients and start Validators MClient[] clients = MClient.getAll(new Properties()); for (int i = 0; i < clients.length; i++) { String classNames = clients[i].getModelValidationClasses(); if (classNames == null || classNames.length() == 0) continue; - StringTokenizer st = new StringTokenizer(classNames, ";"); - while (st.hasMoreTokens()) - { - String className = null; - try - { - className = st.nextToken(); - if (className == null) - continue; - className = className.trim(); - if (className.length() == 0) - continue; - // - Class clazz = Class.forName(className); - ModelValidator validator = (ModelValidator)clazz.newInstance(); - initialize(validator, clients[i]); - } - catch (Exception e) - { - log.log(Level.SEVERE, className + ": " + e.getMessage()); - } - } + loadValidatorClasses(clients[i], classNames); } log.config(toString()); } // ModelValidatorEngine + private void loadValidatorClasses(MClient client, String classNames) + { + StringTokenizer st = new StringTokenizer(classNames, ";"); + while (st.hasMoreTokens()) + { + String className = null; + try + { + className = st.nextToken(); + if (className == null) + continue; + className = className.trim(); + if (className.length() == 0) + continue; + // + loadValidatorClass(client, className); + } + catch (Exception e) + { + log.log(Level.SEVERE, className + ": " + e.getMessage()); + } + } + } + + private void loadValidatorClass(MClient client, String className) { + try + { + // + Class clazz = Class.forName(className); + ModelValidator validator = (ModelValidator)clazz.newInstance(); + initialize(validator, client); + } + catch (Exception e) + { + log.log(Level.SEVERE, className + ": " + e.getMessage()); + } + } + /** Logger */ private static CLogger log = CLogger.getCLogger(ModelValidationEngine.class); /** Change Support */ @@ -100,7 +139,11 @@ public class ModelValidationEngine private Hashtable> m_modelChangeListeners = new Hashtable>(); /** Document Validation Listeners */ private Hashtable> m_docValidateListeners = new Hashtable>(); - + /** Accounting Facts Validation Listeners */ + private Hashtable>m_factsValidateListeners = new Hashtable>(); + + private ArrayList m_globalValidators = new ArrayList(); + /** * Initialize and add validator * @param validator @@ -110,6 +153,8 @@ public class ModelValidationEngine { validator.initialize(this, client); m_validators.add(validator); + if (client == null) + m_globalValidators.add(validator); } // initialize /** @@ -125,7 +170,8 @@ public class ModelValidationEngine for (int i = 0; i < m_validators.size(); i++) { ModelValidator validator = (ModelValidator)m_validators.get(i); - if (AD_Client_ID == validator.getAD_Client_ID()) + if (AD_Client_ID == validator.getAD_Client_ID() + || m_globalValidators.contains(validator)) { String error = validator.login(AD_Org_ID, AD_Role_ID, AD_User_ID); if (error != null && error.length() > 0) @@ -146,7 +192,10 @@ public class ModelValidationEngine if (tableName == null || listener == null) return; // - String propertyName = tableName + listener.getAD_Client_ID(); + String propertyName = + m_globalValidators.contains(listener) + ? tableName + "*" + : tableName + listener.getAD_Client_ID(); ArrayList list = (ArrayList)m_modelChangeListeners.get(propertyName); if (list == null) { @@ -167,7 +216,10 @@ public class ModelValidationEngine { if (tableName == null || listener == null) return; - String propertyName = tableName + listener.getAD_Client_ID(); + String propertyName = + m_globalValidators.contains(listener) + ? tableName + "*" + : tableName + listener.getAD_Client_ID(); ArrayList list = (ArrayList)m_modelChangeListeners.get(propertyName); if (list == null) return; @@ -187,24 +239,44 @@ public class ModelValidationEngine { if (po == null || m_modelChangeListeners.size() == 0) return null; - // - String propertyName = po.get_TableName() + po.getAD_Client_ID(); - ArrayList list = (ArrayList)m_modelChangeListeners.get(propertyName); - if (list == null) - return null; - // + String propertyName = po.get_TableName() + "*"; + ArrayList list = m_modelChangeListeners.get(propertyName); + if (list != null) + { + //ad_entitytype.modelvalidationclasses + String error = fireModelChange(po, changeType, list); + if (error != null && error.length() > 0) + return error; + } + + propertyName = po.get_TableName() + po.getAD_Client_ID(); + list = m_modelChangeListeners.get(propertyName); + if (list != null) + { + //ad_client.modelvalidationclasses + String error = fireModelChange(po, changeType, list); + if (error != null && error.length() > 0) + return error; + } + + return null; + } // fireModelChange + + private String fireModelChange(PO po, int changeType, ArrayList list) + { for (int i = 0; i < list.size(); i++) { try { - ModelValidator validator = (ModelValidator)list.get(i); - if (validator.getAD_Client_ID() == po.getAD_Client_ID()) + ModelValidator validator = list.get(i); + if (validator.getAD_Client_ID() == po.getAD_Client_ID() + || m_globalValidators.contains(validator)) { String error = validator.modelChange(po, changeType); - if (error != null && error.length() > 0) - return error; - } + if (error != null && error.length() > 0) + return error; + } } catch (Exception e) { @@ -215,7 +287,7 @@ public class ModelValidationEngine } } return null; - } // fireModelChange + } /************************************************************************** @@ -228,7 +300,10 @@ public class ModelValidationEngine if (tableName == null || listener == null) return; // - String propertyName = tableName + listener.getAD_Client_ID(); + String propertyName = + m_globalValidators.contains(listener) + ? tableName + "*" + : tableName + listener.getAD_Client_ID(); ArrayList list = (ArrayList)m_docValidateListeners.get(propertyName); if (list == null) { @@ -249,14 +324,17 @@ public class ModelValidationEngine { if (tableName == null || listener == null) return; - String propertyName = tableName + listener.getAD_Client_ID(); + String propertyName = + m_globalValidators.contains(listener) + ? tableName + "*" + : tableName + listener.getAD_Client_ID(); ArrayList list = (ArrayList)m_docValidateListeners.get(propertyName); if (list == null) return; list.remove(listener); if (list.size() == 0) m_docValidateListeners.remove(propertyName); - } // removeModelValidator + } // removeDocValidate /** * Fire Document Validation. @@ -269,25 +347,45 @@ public class ModelValidationEngine { if (po == null || m_docValidateListeners.size() == 0) return null; - // - String propertyName = po.get_TableName() + po.getAD_Client_ID(); - ArrayList list = (ArrayList)m_docValidateListeners.get(propertyName); - if (list == null) - return null; - // + String propertyName = po.get_TableName() + "*"; + ArrayList list = m_docValidateListeners.get(propertyName); + if (list != null) + { + //ad_entitytype.modelvalidationclasses + String error = fireDocValidate(po, docTiming, list); + if (error != null && error.length() > 0) + return error; + } + + propertyName = po.get_TableName() + po.getAD_Client_ID(); + list = m_docValidateListeners.get(propertyName); + if (list != null) + { + //ad_client.modelvalidationclasses + String error = fireDocValidate(po, docTiming, list); + if (error != null && error.length() > 0) + return error; + } + + return null; + } // fireDocValidate + + private String fireDocValidate(PO po, int docTiming, ArrayList list) + { for (int i = 0; i < list.size(); i++) { ModelValidator validator = null; try { - validator = (ModelValidator)list.get(i); - if (validator.getAD_Client_ID() == po.getAD_Client_ID()) + validator = list.get(i); + if (validator.getAD_Client_ID() == po.getAD_Client_ID() + || m_globalValidators.contains(validator)) { String error = validator.docValidate(po, docTiming); - if (error != null && error.length() > 0) - return error; - } + if (error != null && error.length() > 0) + return error; + } } catch (Exception e) { @@ -300,7 +398,118 @@ public class ModelValidationEngine } } return null; - } // fireModelChange + } + + /************************************************************************** + * Add Accounting Facts Validation Listener + * @param tableName table name + * @param listener listener + */ + public void addFactsValidate (String tableName, FactsValidator listener) + { + if (tableName == null || listener == null) + return; + // + String propertyName = + m_globalValidators.contains(listener) + ? tableName + "*" + : tableName + listener.getAD_Client_ID(); + ArrayList list = (ArrayList)m_factsValidateListeners.get(propertyName); + if (list == null) + { + list = new ArrayList(); + list.add(listener); + m_factsValidateListeners.put(propertyName, list); + } + else + list.add(listener); + } // addFactsValidate + + /** + * Remove Accounting Facts Validation Listener + * @param tableName table name + * @param listener listener + */ + public void removeFactsValidate (String tableName, FactsValidator listener) + { + if (tableName == null || listener == null) + return; + String propertyName = + m_globalValidators.contains(listener) + ? tableName + "*" + : tableName + listener.getAD_Client_ID(); + ArrayList list = (ArrayList)m_factsValidateListeners.get(propertyName); + if (list == null) + return; + list.remove(listener); + if (list.size() == 0) + m_factsValidateListeners.remove(propertyName); + } // removeFactsValidate + + /** + * Fire Accounting Facts Validation. + * Call factsValidate method of added validators + * @param schema + * @param facts + * @param doc + * @param po + * @return error message or null + */ + public String fireFactsValidate (MAcctSchema schema, List facts, PO po) + { + if (schema == null || facts == null || po == null || m_factsValidateListeners.size() == 0) + return null; + + String propertyName = po.get_TableName() + "*"; + ArrayList list = (ArrayList)m_factsValidateListeners.get(propertyName); + if (list != null) + { + //ad_entitytype.modelvalidationclasses + String error = fireFactsValidate(schema, facts, po, list); + if (error != null && error.length() > 0) + return error; + } + + propertyName = po.get_TableName() + po.getAD_Client_ID(); + list = (ArrayList)m_factsValidateListeners.get(propertyName); + if (list != null) + { + //ad_client.modelvalidationclasses + String error = fireFactsValidate(schema, facts, po, list); + if (error != null && error.length() > 0) + return error; + } + + return null; + } // fireFactsValidate + + private String fireFactsValidate(MAcctSchema schema, List facts, PO po, ArrayList list) + { + for (int i = 0; i < list.size(); i++) + { + FactsValidator validator = null; + try + { + validator = list.get(i); + if (validator.getAD_Client_ID() == po.getAD_Client_ID()) + { + String error = validator.factsValidate(schema, facts, po); + if (error != null && error.length() > 0) + return error; + } + } + catch (Exception e) + { + // Exeptions are errors and should stop the document processing - teo_sarca [ 1679692 ] + // log.log(Level.SEVERE, validator.toString(), e); + String error = e.getMessage(); + if (error == null) + error = e.toString(); + return error; + } + } + return null; + } /** * String Representation @@ -371,7 +580,8 @@ public class ModelValidationEngine for (int i = 0; i < m_validators.size(); i++) { ModelValidator validator = (ModelValidator)m_validators.get(i); - if (AD_Client_ID == validator.getAD_Client_ID()) + if (AD_Client_ID == validator.getAD_Client_ID() + || m_globalValidators.contains(validator)) { java.lang.reflect.Method m = null; try { @@ -390,5 +600,5 @@ public class ModelValidationEngine } } } - + } // ModelValidatorEngine diff --git a/base/src/org/compiere/model/X_AD_Client.java b/base/src/org/compiere/model/X_AD_Client.java index b3142a2dba..423109c44d 100644 --- a/base/src/org/compiere/model/X_AD_Client.java +++ b/base/src/org/compiere/model/X_AD_Client.java @@ -382,10 +382,10 @@ if (MMPolicy == null) throw new IllegalArgumentException ("MMPolicy is mandatory */ public void setModelValidationClasses (String ModelValidationClasses) { - if (ModelValidationClasses != null && ModelValidationClasses.length() > 255) + if (ModelValidationClasses != null && ModelValidationClasses.length() > 2000) { - log.warning("Length > 255 - truncated"); - ModelValidationClasses = ModelValidationClasses.substring(0, 254); + log.warning("Length > 2000 - truncated"); + ModelValidationClasses = ModelValidationClasses.substring(0, 2000); } set_Value (COLUMNNAME_ModelValidationClasses, ModelValidationClasses); } diff --git a/base/src/org/compiere/model/X_AD_ModelValidator.java b/base/src/org/compiere/model/X_AD_ModelValidator.java new file mode 100644 index 0000000000..e5a708d15b --- /dev/null +++ b/base/src/org/compiere/model/X_AD_ModelValidator.java @@ -0,0 +1,220 @@ +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2007 ComPiere, Inc. All Rights Reserved. * + * 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 * + * ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA * + * or via info@compiere.org or http://www.compiere.org/license.html * + *****************************************************************************/ +/** Generated Model - DO NOT CHANGE */ +package org.compiere.model; + +import java.sql.ResultSet; +import java.util.Properties; +import org.compiere.util.KeyNamePair; + +/** Generated Model for AD_ModelValidator + * @author Adempiere (generated) + * @version Release 3.3.0 - $Id$ */ +public class X_AD_ModelValidator extends PO implements I_AD_ModelValidator, I_Persistent +{ + + /** + * + */ + private static final long serialVersionUID = 1L; + + /** Standard Constructor */ + public X_AD_ModelValidator (Properties ctx, int AD_ModelValidator_ID, String trxName) + { + super (ctx, AD_ModelValidator_ID, trxName); + /** if (AD_ModelValidator_ID == 0) + { + setAD_ModelValidator_ID (0); + setEntityType (null); + setModelValidationClass (null); + setName (null); + } */ + } + + /** Load Constructor */ + public X_AD_ModelValidator (Properties ctx, ResultSet rs, String trxName) + { + super (ctx, rs, trxName); + } + + /** AccessLevel + * @return 4 - System + */ + protected int get_AccessLevel() + { + return accessLevel.intValue(); + } + + /** Load Meta Data */ + protected POInfo initPO (Properties ctx) + { + POInfo poi = POInfo.getPOInfo (ctx, Table_ID); + return poi; + } + + public String toString() + { + StringBuffer sb = new StringBuffer ("X_AD_ModelValidator[") + .append(get_ID()).append("]"); + return sb.toString(); + } + + /** Set Model Validator. + @param AD_ModelValidator_ID Model Validator */ + public void setAD_ModelValidator_ID (int AD_ModelValidator_ID) + { + if (AD_ModelValidator_ID < 1) + throw new IllegalArgumentException ("AD_ModelValidator_ID is mandatory."); + set_ValueNoCheck (COLUMNNAME_AD_ModelValidator_ID, Integer.valueOf(AD_ModelValidator_ID)); + } + + /** Get Model Validator. + @return Model Validator */ + public int getAD_ModelValidator_ID () + { + Integer ii = (Integer)get_Value(COLUMNNAME_AD_ModelValidator_ID); + if (ii == null) + return 0; + return ii.intValue(); + } + + /** Set Description. + @param Description + Optional short description of the record + */ + public void setDescription (String Description) + { + + if (Description != null && Description.length() > 255) + { + log.warning("Length > 255 - truncated"); + Description = Description.substring(0, 255); + } + set_Value (COLUMNNAME_Description, Description); + } + + /** Get Description. + @return Optional short description of the record + */ + public String getDescription () + { + return (String)get_Value(COLUMNNAME_Description); + } + + /** Set Entity Type. + @param EntityType + Dictionary Entity Type; Determines ownership and synchronization + */ + public void setEntityType (String EntityType) + { + if (EntityType == null) + throw new IllegalArgumentException ("EntityType is mandatory."); + + if (EntityType.length() > 40) + { + log.warning("Length > 40 - truncated"); + EntityType = EntityType.substring(0, 40); + } + set_ValueNoCheck (COLUMNNAME_EntityType, EntityType); + } + + /** Get Entity Type. + @return Dictionary Entity Type; Determines ownership and synchronization + */ + public String getEntityType () + { + return (String)get_Value(COLUMNNAME_EntityType); + } + + /** Set Comment/Help. + @param Help + Comment or Hint + */ + public void setHelp (String Help) + { + + if (Help != null && Help.length() > 2000) + { + log.warning("Length > 2000 - truncated"); + Help = Help.substring(0, 2000); + } + set_Value (COLUMNNAME_Help, Help); + } + + /** Get Comment/Help. + @return Comment or Hint + */ + public String getHelp () + { + return (String)get_Value(COLUMNNAME_Help); + } + + /** Set Model Validation Class. + @param ModelValidationClass Model Validation Class */ + public void setModelValidationClass (String ModelValidationClass) + { + if (ModelValidationClass == null) + throw new IllegalArgumentException ("ModelValidationClass is mandatory."); + + if (ModelValidationClass.length() > 255) + { + log.warning("Length > 255 - truncated"); + ModelValidationClass = ModelValidationClass.substring(0, 255); + } + set_Value (COLUMNNAME_ModelValidationClass, ModelValidationClass); + } + + /** Get Model Validation Class. + @return Model Validation Class */ + public String getModelValidationClass () + { + return (String)get_Value(COLUMNNAME_ModelValidationClass); + } + + /** Set Name. + @param Name + Alphanumeric identifier of the entity + */ + public void setName (String Name) + { + if (Name == null) + throw new IllegalArgumentException ("Name is mandatory."); + + if (Name.length() > 120) + { + log.warning("Length > 120 - truncated"); + Name = Name.substring(0, 120); + } + set_Value (COLUMNNAME_Name, Name); + } + + /** Get Name. + @return Alphanumeric identifier of the entity + */ + public String getName () + { + return (String)get_Value(COLUMNNAME_Name); + } + + /** Get Record ID/ColumnName + @return ID/ColumnName pair + */ + public KeyNamePair getKeyNamePair() + { + return new KeyNamePair(get_ID(), getName()); + } +} \ No newline at end of file