Fixes issue 23996: An event handler has been replaced with database constraints
authorAugusto Mauch <augusto.mauch@openbravo.com>
Wed, 05 Jun 2013 14:03:22 +0200
changeset 20511 e426b7ccc826
parent 20510 f7a3e45cbddf
child 20512 02b4db5308b3
Fixes issue 23996: An event handler has been replaced with database constraints

The event handler TableDataOriginEventHandler has been replaced with database constraints. This is a better approach because it is simpler and easier to maint
ain.

The name of two of the messages has been updated so that it is the name as the constraint that throw them. The message used to warn the user that the table_name must be set if the data origin is 'Table' has been removed because now the table_name is mandatory again in the database.
modules/org.openbravo.client.application/src/org/openbravo/client/application/event/TableDataOriginEventHandler.java
src-db/database/model/tables/AD_TABLE.xml
src-db/database/sourcedata/AD_MESSAGE.xml
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/event/TableDataOriginEventHandler.java	Wed Jun 05 12:36:15 2013 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,73 +0,0 @@
-/*
- *************************************************************************
- * The contents of this file are subject to the Openbravo  Public  License
- * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
- * Version 1.1  with a permitted attribution clause; you may not  use this
- * file except in compliance with the License. You  may  obtain  a copy of
- * the License at http://www.openbravo.com/legal/license.html
- * Software distributed under the License  is  distributed  on  an "AS IS"
- * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
- * License for the specific  language  governing  rights  and  limitations
- * under the License.
- * The Original Code is Openbravo ERP.
- * The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2013 Openbravo SLU
- * All Rights Reserved.
- * Contributor(s):  Augusto Mauch.
- ************************************************************************
- */
-
-package org.openbravo.client.application.event;
-
-import javax.enterprise.event.Observes;
-
-import org.apache.log4j.Logger;
-import org.openbravo.base.exception.OBException;
-import org.openbravo.base.model.Entity;
-import org.openbravo.base.model.ModelProvider;
-import org.openbravo.client.application.ApplicationConstants;
-import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
-import org.openbravo.client.kernel.event.EntityUpdateEvent;
-import org.openbravo.dal.core.OBContext;
-import org.openbravo.database.ConnectionProvider;
-import org.openbravo.erpCommon.utility.Utility;
-import org.openbravo.model.ad.datamodel.Table;
-import org.openbravo.service.datasource.DataSource;
-import org.openbravo.service.db.DalConnectionProvider;
-
-public class TableDataOriginEventHandler extends EntityPersistenceEventObserver {
-
-  private static Entity[] entities = { ModelProvider.getInstance().getEntity(Table.ENTITY_NAME) };
-  protected Logger logger = Logger.getLogger(this.getClass());
-
-  @Override
-  protected Entity[] getObservedEntities() {
-    return entities;
-  }
-
-  public void onUpdate(@Observes EntityUpdateEvent event) {
-    if (!isValidEvent(event)) {
-      return;
-    }
-    final Table table = (Table) event.getTargetInstance();
-    String dataOriginType = table.getDataOriginType();
-    String tableName = table.getDBTableName();
-    String javaClassName = table.getJavaClassName();
-    DataSource datasource = table.getObserdsDatasource();
-    if (ApplicationConstants.TABLEBASEDTABLE.equals(dataOriginType) && (tableName == null)) {
-      String language = OBContext.getOBContext().getLanguage().getLanguage();
-      ConnectionProvider conn = new DalConnectionProvider(false);
-      throw new OBException(Utility.messageBD(conn, "OBUIAPP_TableNameMandatory", language));
-    }
-    if (ApplicationConstants.TABLEBASEDTABLE.equals(dataOriginType) && (javaClassName == null)) {
-      String language = OBContext.getOBContext().getLanguage().getLanguage();
-      ConnectionProvider conn = new DalConnectionProvider(false);
-      throw new OBException(Utility.messageBD(conn, "OBUIAPP_JavaClassNameMandatory", language));
-    }
-    if (ApplicationConstants.DATASOURCEBASEDTABLE.equals(dataOriginType) && (datasource == null)) {
-      String language = OBContext.getOBContext().getLanguage().getLanguage();
-      ConnectionProvider conn = new DalConnectionProvider(false);
-      throw new OBException(Utility.messageBD(conn, "OBUIAPP_DatasourceMandatory", language));
-    }
-  }
-}
--- a/src-db/database/model/tables/AD_TABLE.xml	Wed Jun 05 12:36:15 2013 +0200
+++ b/src-db/database/model/tables/AD_TABLE.xml	Wed Jun 05 14:03:22 2013 +0200
@@ -167,6 +167,8 @@
       <unique name="AD_TABLE_TREETYPE_UN">
         <unique-column name="TREETYPE"/>
       </unique>
+      <check name="AD_TABLE_CLASSNAME_CHK"><![CDATA[((DATAORIGINTYPE) = 'DATASOURCE') OR (CLASSNAME IS NOT NULL)]]></check>
+      <check name="AD_TABLE_DATASOURCE_CHK"><![CDATA[((DATAORIGINTYPE) = 'TABLE') OR (OBSERDS_DATASOURCE_ID IS NOT NULL)]]></check>
       <check name="AD_TABLE_ISACTIVE_CHK"><![CDATA[ISACTIVE IN ('Y', 'N')]]></check>
       <check name="AD_TABLE_ISAUDITINS_CHK"><![CDATA[ISAUDITINSERTS IN ('Y', 'N')]]></check>
       <check name="AD_TABLE_ISDELETEABLE_CHK"><![CDATA[ISDELETEABLE IN ('Y', 'N')]]></check>
--- a/src-db/database/sourcedata/AD_MESSAGE.xml	Wed Jun 05 12:36:15 2013 +0200
+++ b/src-db/database/sourcedata/AD_MESSAGE.xml	Wed Jun 05 14:03:22 2013 +0200
@@ -17145,18 +17145,6 @@
 <!--2413C331A08B4F3F98FC3F9A0F2A636C-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
 <!--2413C331A08B4F3F98FC3F9A0F2A636C--></AD_MESSAGE>
 
-<!--2445F46096B344E1AB2EBB0CDF9D94EE--><AD_MESSAGE>
-<!--2445F46096B344E1AB2EBB0CDF9D94EE-->  <AD_MESSAGE_ID><![CDATA[2445F46096B344E1AB2EBB0CDF9D94EE]]></AD_MESSAGE_ID>
-<!--2445F46096B344E1AB2EBB0CDF9D94EE-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
-<!--2445F46096B344E1AB2EBB0CDF9D94EE-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
-<!--2445F46096B344E1AB2EBB0CDF9D94EE-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
-<!--2445F46096B344E1AB2EBB0CDF9D94EE-->  <VALUE><![CDATA[OBUIAPP_TableNameMandatory]]></VALUE>
-<!--2445F46096B344E1AB2EBB0CDF9D94EE-->  <MSGTEXT><![CDATA[If the data origin type is 'Table' then the Table Name field is mandatory and cannot be left empty.]]></MSGTEXT>
-<!--2445F46096B344E1AB2EBB0CDF9D94EE-->  <MSGTYPE><![CDATA[E]]></MSGTYPE>
-<!--2445F46096B344E1AB2EBB0CDF9D94EE-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
-<!--2445F46096B344E1AB2EBB0CDF9D94EE-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
-<!--2445F46096B344E1AB2EBB0CDF9D94EE--></AD_MESSAGE>
-
 <!--24B4AED8FE3542C2933B4EA37DF641C5--><AD_MESSAGE>
 <!--24B4AED8FE3542C2933B4EA37DF641C5-->  <AD_MESSAGE_ID><![CDATA[24B4AED8FE3542C2933B4EA37DF641C5]]></AD_MESSAGE_ID>
 <!--24B4AED8FE3542C2933B4EA37DF641C5-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
@@ -18919,7 +18907,7 @@
 <!--58FF541F93764CF686479AF658451270-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
 <!--58FF541F93764CF686479AF658451270-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
 <!--58FF541F93764CF686479AF658451270-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
-<!--58FF541F93764CF686479AF658451270-->  <VALUE><![CDATA[OBUIAPP_DatasourceMandatory]]></VALUE>
+<!--58FF541F93764CF686479AF658451270-->  <VALUE><![CDATA[ad_table_datasource_chk]]></VALUE>
 <!--58FF541F93764CF686479AF658451270-->  <MSGTEXT><![CDATA[If the data origin type is 'Datasource' then the Datasource field is mandatory and cannot be left empty. ]]></MSGTEXT>
 <!--58FF541F93764CF686479AF658451270-->  <MSGTYPE><![CDATA[E]]></MSGTYPE>
 <!--58FF541F93764CF686479AF658451270-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
@@ -19715,7 +19703,7 @@
 <!--707E11D58E994C75B7745103E95B9DBA-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
 <!--707E11D58E994C75B7745103E95B9DBA-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
 <!--707E11D58E994C75B7745103E95B9DBA-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
-<!--707E11D58E994C75B7745103E95B9DBA-->  <VALUE><![CDATA[OBUIAPP_JavaClassNameMandatory]]></VALUE>
+<!--707E11D58E994C75B7745103E95B9DBA-->  <VALUE><![CDATA[ad_table_classname_chk]]></VALUE>
 <!--707E11D58E994C75B7745103E95B9DBA-->  <MSGTEXT><![CDATA[If the data origin type is 'Table' then the Java Class Name field is mandatory and cannot be left empty.]]></MSGTEXT>
 <!--707E11D58E994C75B7745103E95B9DBA-->  <MSGTYPE><![CDATA[E]]></MSGTYPE>
 <!--707E11D58E994C75B7745103E95B9DBA-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>