details:   https://code.openbravo.com/erp/devel/pi/rev/240361165827
changeset: 25237:240361165827
user:      Augusto Mauch <augusto.mauch <at> openbravo.com>
date:      Fri Nov 07 13:17:56 2014 +0100
summary:   Related with issue 28119: DataSourceServiceProvider.getDataSource 
refactor #1

The DataSourceServiceProvider.getDataSource method is going to be refactored 
before fixing 28119, because its logic is very messy.

In this first part of the refactor the obtention of the DataSource and the 
obtention of the DataSourceService is going the be separated. The first part of 
the method now is in charge os retrieving the DataSource, and the second part 
obtains the DataSourceService associated with the DataSource.

details:   https://code.openbravo.com/erp/devel/pi/rev/d36de12150b1
changeset: 25238:d36de12150b1
user:      Augusto Mauch <augusto.mauch <at> openbravo.com>
date:      Fri Nov 07 14:00:05 2014 +0100
summary:   Related with issue 28119: Refactor #2 and part of actual fix

Two methods have been extracted: getRealDataSource and 
getDataSourceServiceFromDataSource. In the previous changeset the retrieval of 
the DataSource and DataSource service was separated, now each code is in its 
respective method. I would have liked to name the first method getDataSource, 
but that name was already taken by the public method that returns a 
DataSourceServe.

This changeset also contains the first part of the fix. The original problem 
was that the entity of the DataSourceService was being set before the 
DataSourceService was initialized. This used to be done in the part that 
retrieved the DataSource, now it has been removed. The entity will be set to 
the DataSourceService in the next changeset.

details:   https://code.openbravo.com/erp/devel/pi/rev/ac61bb2afe11
changeset: 25239:ac61bb2afe11
user:      Augusto Mauch <augusto.mauch <at> openbravo.com>
date:      Fri Nov 07 15:05:43 2014 +0100
summary:   Related with issue 28119: Do not fail is trying to retrieve a null 
entity

The ModelProvider.getEntity method, which is used to retrieve an entity based 
on its table name, used to fail if an entity with the provided table name did 
not exist. This was not done when retrieving an entity given its table id or 
its table table_name.

Now it is possible to call to the getEntity method with an invalid table name 
without failing if the entity does not exist. For this a new getEntity method 
has been overriden with a checkIfNotExists boolean parameter. The original 
signature has been preserved, and will call the new one with 
checkIfNotExists=true, to ensure that the default behaviour is the same as it 
used to be.

details:   https://code.openbravo.com/erp/devel/pi/rev/c77d6ec09d26
changeset: 25240:c77d6ec09d26
user:      Augusto Mauch <augusto.mauch <at> openbravo.com>
date:      Fri Nov 07 15:09:23 2014 +0100
summary:   Fixes issue 28119: Prevents potential NPE in 
DataSourceServiceProvider

The original problem was that is potentially possible to set call the setEntity 
method of a null DataSourceService. Now it is not possible, because that method 
will only be invoked after the DataSourceService has been properly initialized.

details:   https://code.openbravo.com/erp/devel/pi/rev/305f5c29bb31
changeset: 25241:305f5c29bb31
user:      Augusto Mauch <augusto.mauch <at> openbravo.com>
date:      Sun Nov 09 23:42:50 2014 +0100
summary:   Related with issue 28119: Further refactoring

The code that retrieved a datasource from the datasource id, datasource name 
and table name has been moved to the methods getDataSourceFromDataSourceId, 
getDataSourceFromDataSourceName and getDataSourceFromTableName respectively.

diffstat:

 
modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/DataSourceServiceProvider.java
 |  154 +++++++--
 src/org/openbravo/base/model/ModelProvider.java                                
                              |   18 +-
 2 files changed, 125 insertions(+), 47 deletions(-)

diffs (213 lines):

diff -r f73599ad9646 -r 305f5c29bb31 
modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/DataSourceServiceProvider.java
--- 
a/modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/DataSourceServiceProvider.java
      Mon Nov 17 16:46:52 2014 +0100
+++ 
b/modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/DataSourceServiceProvider.java
      Sun Nov 09 23:42:50 2014 +0100
@@ -26,6 +26,7 @@
 
 import org.hibernate.criterion.Restrictions;
 import org.openbravo.base.exception.OBException;
+import org.openbravo.base.model.Entity;
 import org.openbravo.base.model.ModelProvider;
 import org.openbravo.base.util.OBClassLoader;
 import org.openbravo.base.weld.WeldUtils;
@@ -54,63 +55,124 @@
    * Checks the internal cache for a datasource with the requested name and 
returns it if found. If
    * not found a new one is created, which is cached and then returned.
    * 
-   * @param name
+   * @param dataSourceIdentifier
    *          the name by which to search and identify the data source.
    * @return a {@link DataSourceService} object
    */
-  @SuppressWarnings("unchecked")
-  public DataSourceService getDataSource(String name) {
-    DataSourceService ds = dataSources.get(name);
-    if (ds == null) {
+  public DataSourceService getDataSource(String dataSourceIdentifier) {
+    DataSourceService dataSourceService = 
dataSources.get(dataSourceIdentifier);
+    if (dataSourceService == null) {
       OBContext.setAdminMode();
       try {
-        DataSource dataSource = OBDal.getInstance().get(DataSource.class, 
name);
-        if (dataSource == null) {
-
-          final OBCriteria<DataSource> obCriteria = 
OBDal.getInstance().createCriteria(
-              DataSource.class);
-          obCriteria.add(Restrictions.eq(DataSource.PROPERTY_NAME, name));
-          if (!obCriteria.list().isEmpty()) {
-            dataSource = obCriteria.list().get(0);
-          }
-        }
-        if (dataSource == null) {
-          final OBCriteria<Table> qTable = 
OBDal.getInstance().createCriteria(Table.class);
-          qTable.add(Restrictions.eq(Table.PROPERTY_NAME, name));
-          if (!qTable.list().isEmpty()) {
-            Table table = (Table) qTable.list().get(0);
-            if 
(ApplicationConstants.DATASOURCEBASEDTABLE.equals(table.getDataOriginType())) {
-              dataSource = table.getObserdsDatasource();
-              
ds.setEntity(ModelProvider.getInstance().getEntityByTableId(table.getId()));
-            } else if 
(ApplicationConstants.HQLBASEDTABLE.equals(table.getDataOriginType())) {
-              dataSource = OBDal.getInstance().get(DataSource.class,
-                  ApplicationConstants.HQL_TABLE_DATASOURCE_ID);
-            }
-          }
-          if (dataSource == null) {
-            ds = weldUtils.getInstance(DefaultDataSourceService.class);
-            ds.setName(name);
-            ds.setEntity(ModelProvider.getInstance().getEntity(name));
-            dataSources.put(name, ds);
-          }
-
-        } else {
-          if (dataSource.getJavaClassName() != null) {
-            final Class<DataSourceService> clz = (Class<DataSourceService>) 
OBClassLoader
-                .getInstance().loadClass(dataSource.getJavaClassName());
-            ds = weldUtils.getInstance(clz);
-          } else {
-            ds = new DefaultDataSourceService();
-          }
-          ds.setDataSource(dataSource);
-          dataSources.put(name, ds);
-        }
+        DataSource dataSource = getRealDataSource(dataSourceIdentifier);
+        dataSourceService = getDataSourceServiceFromDataSource(dataSource, 
dataSourceIdentifier);
+        dataSources.put(dataSourceIdentifier, dataSourceService);
       } catch (Exception e) {
         throw new OBException(e);
       } finally {
         OBContext.restorePreviousMode();
       }
     }
+    return dataSourceService;
+  }
+
+  /**
+   * Obtains a dataSource given a dataSource identifier.
+   * 
+   * This class should have been named getDataSource instead of 
getRealDataSource, but the name was
+   * already taken by a public method that returns a DataSourceService
+   * 
+   * @param dataSourceIdentifier
+   *          a string that identifies the dataSource. it can be either the ID 
of the DataSource,
+   *          the name of the DataSource or the name of the Table whose 
datasource is to be
+   *          retrieved
+   * @return the datasource associated with the provided identifier or null if 
there aren't any
+   */
+  private DataSource getRealDataSource(String dataSourceIdentifier) {
+    // Checks if the dataSourceIdentifier the ID of the DataSource
+    DataSource dataSource = 
getDataSourceFromDataSourceId(dataSourceIdentifier);
+    if (dataSource == null) {
+      // If it is not the ID of the DataSource, checks if it is its name
+      dataSource = getDataSourceFromDataSourceName(dataSourceIdentifier);
+      if (dataSource == null) {
+        // If the dataSourceIdentifier is not the DataSource ID nor its name, 
checks if it is the
+        // name of a Table
+        dataSource = getDataSourceFromTableName(dataSourceIdentifier);
+      }
+    }
+    return dataSource;
+  }
+
+  private DataSource getDataSourceFromDataSourceId(String dataSourceId) {
+    return OBDal.getInstance().get(DataSource.class, dataSourceId);
+  }
+
+  private DataSource getDataSourceFromDataSourceName(String dataSourceName) {
+    DataSource dataSource = null;
+    final OBCriteria<DataSource> obCriteria = 
OBDal.getInstance().createCriteria(DataSource.class);
+    obCriteria.add(Restrictions.eq(DataSource.PROPERTY_NAME, dataSourceName));
+    if (!obCriteria.list().isEmpty()) {
+      dataSource = obCriteria.list().get(0);
+    }
+    return dataSource;
+  }
+
+  private DataSource getDataSourceFromTableName(String tableName) {
+    DataSource dataSource = null;
+    final OBCriteria<Table> qTable = 
OBDal.getInstance().createCriteria(Table.class);
+    qTable.add(Restrictions.eq(Table.PROPERTY_NAME, tableName));
+    if (!qTable.list().isEmpty()) {
+      Table table = (Table) qTable.list().get(0);
+      if 
(ApplicationConstants.DATASOURCEBASEDTABLE.equals(table.getDataOriginType())) {
+        // If the table is based on a manual datasource, return that 
particular datasource
+        dataSource = table.getObserdsDatasource();
+      } else if 
(ApplicationConstants.HQLBASEDTABLE.equals(table.getDataOriginType())) {
+        // If the table is based on a HQL table, use the 'HQL Tables 
Datasource'
+        dataSource = OBDal.getInstance().get(DataSource.class,
+            ApplicationConstants.HQL_TABLE_DATASOURCE_ID);
+      }
+    }
+    return dataSource;
+  }
+
+  /**
+   * Returns a DataSourceService given a DataSource
+   * 
+   * @param dataSource
+   *          the dataSource whose DataSourceService is to be retrieved
+   * @param dataSourceIdentifier
+   *          the name that was used to retrieve the dataSource
+   * @return the DataSourceService associated with the provided DataSource, or 
the
+   *         DefaultDataSourceService otherwise
+   * @throws ClassNotFoundException
+   */
+  private DataSourceService getDataSourceServiceFromDataSource(DataSource 
dataSource,
+      String dataSourceIdentifier) throws ClassNotFoundException {
+    DataSourceService ds = null;
+    if (dataSource == null) {
+      // if no dataSource is provided, return the DefaultDataSourceService
+      ds = weldUtils.getInstance(DefaultDataSourceService.class);
+      ds.setName(dataSourceIdentifier);
+    } else {
+      // try to retrieve the DataSourceService through the dataSource java 
class name, otherwise
+      // return the DefaultDataSourceService
+      if (dataSource.getJavaClassName() != null) {
+        @SuppressWarnings("unchecked")
+        final Class<DataSourceService> clz = (Class<DataSourceService>) 
OBClassLoader.getInstance()
+            .loadClass(dataSource.getJavaClassName());
+        ds = weldUtils.getInstance(clz);
+      } else {
+        ds = new DefaultDataSourceService();
+      }
+      ds.setDataSource(dataSource);
+    }
+    // don't fail if the entity does not exist, just don't assign it to the 
DataSourceService
+    boolean checkIfNotExists = false;
+    Entity entity = 
ModelProvider.getInstance().getEntity(dataSourceIdentifier, checkIfNotExists);
+    if (entity != null) {
+      ds.setEntity(entity);
+    }
     return ds;
   }
+
 }
diff -r f73599ad9646 -r 305f5c29bb31 
src/org/openbravo/base/model/ModelProvider.java
--- a/src/org/openbravo/base/model/ModelProvider.java   Mon Nov 17 16:46:52 
2014 +0100
+++ b/src/org/openbravo/base/model/ModelProvider.java   Sun Nov 09 23:42:50 
2014 +0100
@@ -900,10 +900,26 @@
    * @throws CheckException
    */
   public Entity getEntity(String entityName) throws CheckException {
+    boolean checkIfNotExists = true;
+    return getEntity(entityName, checkIfNotExists);
+  }
+
+  /**
+   * Retrieves an Entity using the entityName. If not found then a 
CheckException is thrown if the
+   * checkIfNotExists parameter is true.
+   * 
+   * @param entityName
+   *          the name used for searching the Entity.
+   * @param checkIfNotExists
+   *          a boolean that is true calls to Check.fail if the entity does 
not exist
+   * @return the Entity object
+   * @throws CheckException
+   */
+  public Entity getEntity(String entityName, boolean checkIfNotExists) throws 
CheckException {
     if (model == null)
       getModel();
     final Entity entity = entitiesByName.get(entityName);
-    if (entity == null) {
+    if (entity == null && checkIfNotExists) {
       Check.fail("Mapping name: " + entityName + " not found in runtime 
model");
     }
     return entity;

------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk
_______________________________________________
Openbravo-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to