← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14605: Centralizing and using generic store at store layer, removes boiler-plate code

 

------------------------------------------------------------
revno: 14605
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2014-04-02 13:58:10 +0200
message:
  Centralizing and using generic store at store layer, removes boiler-plate code
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateDataElementOperandStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateDataElementStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/hibernate/HibernateDataSetStore.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateDataElementOperandStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateDataElementOperandStore.java	2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateDataElementOperandStore.java	2014-04-02 11:58:10 +0000
@@ -49,9 +49,8 @@
     public List<DataElementOperand> getAllOrderedName()
     {
         String hql = "from DataElementOperand d";
-        Query query = getQuery( hql );
-
-        return query.list();
+        
+        return getQuery( hql ).list();
     }
 
     @Override
@@ -59,6 +58,7 @@
     public List<DataElementOperand> getAllOrderedName( int first, int max )
     {
         String hql = "from DataElementOperand d";
+        
         Query query = getQuery( hql );
         query.setFirstResult( first );
         query.setMaxResults( max );
@@ -71,6 +71,7 @@
     public Collection<DataElementOperand> getByDataElementGroup( DataElementGroup dataElementGroup )
     {
         String hql = "select d from DataElementOperand d, DataElementGroup deg where deg=:dataElementGroup and d.dataElement in elements(deg.members)";
+        
         Query query = getQuery( hql );
         query.setEntity( "dataElementGroup", dataElementGroup );
 

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateDataElementStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateDataElementStore.java	2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateDataElementStore.java	2014-04-02 11:58:10 +0000
@@ -28,11 +28,18 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import static org.hisp.dhis.system.util.ConversionUtils.getIdentifiers;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hibernate.Criteria;
 import org.hibernate.Query;
-import org.hibernate.Session;
 import org.hibernate.criterion.Order;
 import org.hibernate.criterion.Restrictions;
 import org.hisp.dhis.common.ListMap;
@@ -41,17 +48,10 @@
 import org.hisp.dhis.dataelement.DataElementCategoryCombo;
 import org.hisp.dhis.dataelement.DataElementStore;
 import org.hisp.dhis.dataset.DataSet;
-import org.hisp.dhis.system.util.ConversionUtils;
 import org.hisp.dhis.system.util.TextUtils;
 import org.springframework.jdbc.BadSqlGrammarException;
 import org.springframework.jdbc.core.RowCallbackHandler;
 
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Set;
-
 /**
  * @author Torgeir Lorange Ostby
  */
@@ -68,81 +68,48 @@
     @SuppressWarnings("unchecked")
     public Collection<DataElement> searchDataElementsByName( String key )
     {
-        Session session = sessionFactory.getCurrentSession();
-
-        Criteria criteria = session.createCriteria( DataElement.class );
-        criteria.add( Restrictions.ilike( "name", "%" + key + "%" ) );
-
-        return criteria.list();
+        return getCriteria( Restrictions.ilike( "name", "%" + key + "%" ) ).list();
     }
 
     @SuppressWarnings("unchecked")
     public Collection<DataElement> getAggregateableDataElements()
     {
-        Session session = sessionFactory.getCurrentSession();
-
         Set<String> types = new HashSet<String>();
 
         types.add( DataElement.VALUE_TYPE_INT );
         types.add( DataElement.VALUE_TYPE_BOOL );
-
-        Criteria criteria = session.createCriteria( DataElement.class );
-
-        criteria.add( Restrictions.in( "type", types ) );
-
-        return criteria.list();
+        
+        return getCriteria( Restrictions.in( "type", types ) ).list();
     }
 
     @SuppressWarnings("unchecked")
     public Collection<DataElement> getAllActiveDataElements()
     {
-        Session session = sessionFactory.getCurrentSession();
-
-        Criteria criteria = session.createCriteria( DataElement.class );
-        criteria.add( Restrictions.eq( "active", true ) );
-
-        return criteria.list();
+        return getCriteria( Restrictions.eq( "active", true ) ).list();
     }
 
     @SuppressWarnings("unchecked")
     public Collection<DataElement> getDataElementsByAggregationOperator( String aggregationOperator )
     {
-        Session session = sessionFactory.getCurrentSession();
-
-        Criteria criteria = session.createCriteria( DataElement.class );
-        criteria.add( Restrictions.eq( "aggregationOperator", aggregationOperator ) );
-
-        return criteria.list();
+        return getCriteria( Restrictions.eq( "aggregationOperator", aggregationOperator ) ).list();
     }
 
     @SuppressWarnings("unchecked")
     public Collection<DataElement> getDataElementsByType( String type )
     {
-        Session session = sessionFactory.getCurrentSession();
-
-        Criteria criteria = session.createCriteria( DataElement.class );
-        criteria.add( Restrictions.eq( "type", type ) );
-
-        return criteria.list();
+        return getCriteria( Restrictions.eq( "type", type ) ).list();
     }
 
     @SuppressWarnings( "unchecked" )
     public Collection<DataElement> getDataElementsByDomainType( String domainType )
     {
-        Session session = sessionFactory.getCurrentSession();
-
-        Criteria criteria = session.createCriteria( DataElement.class );
-        criteria.add( Restrictions.eq( "domainType", domainType ) );
-
-        return criteria.list();
+        return getCriteria( Restrictions.eq( "domainType", domainType ) ).list();
     }
 
     @SuppressWarnings("unchecked")
     public Collection<DataElement> getDataElementsByDomainType( String domainType, int first, int max )
     {
-        Session session = sessionFactory.getCurrentSession();
-
-        Criteria criteria = session.createCriteria( DataElement.class );
+        Criteria criteria = getCriteria();
         criteria.add( Restrictions.eq( "domainType", domainType ) );
 
         criteria.setFirstResult( first );
@@ -155,12 +122,7 @@
     @SuppressWarnings("unchecked")
     public Collection<DataElement> getDataElementByCategoryCombo( DataElementCategoryCombo categoryCombo )
     {
-        Session session = sessionFactory.getCurrentSession();
-
-        Criteria criteria = session.createCriteria( DataElement.class );
-        criteria.add( Restrictions.eq( "categoryCombo", categoryCombo ) );
-
-        return criteria.list();
+        return getCriteria( Restrictions.eq( "categoryCombo", categoryCombo ) ).list();
     }
 
     @SuppressWarnings("unchecked")
@@ -168,24 +130,24 @@
     {
         String hql = "from DataElement d where d.groupSets.size > 0";
 
-        return getQuery( hql ).setCacheable( true ).list();
+        return getQuery( hql ).list();
     }
 
     public void setZeroIsSignificantForDataElements( Collection<Integer> dataElementIds )
     {
-        Session session = sessionFactory.getCurrentSession();
-
-        String sql = "update DataElement set zeroIsSignificant = false";
-
-        Query query = session.createQuery( sql );
+        String hql = "update DataElement set zeroIsSignificant = false";
+
+        Query query = getQuery( hql );
 
         query.executeUpdate();
+        
+        //TODO improve
 
         if ( !dataElementIds.isEmpty() )
         {
-            sql = "update DataElement set zeroIsSignificant=true where id in (:dataElementIds)";
+            hql = "update DataElement set zeroIsSignificant=true where id in (:dataElementIds)";
 
-            query = session.createQuery( sql );
+            query = getQuery( hql );
             query.setParameterList( "dataElementIds", dataElementIds );
 
             query.executeUpdate();
@@ -198,7 +160,6 @@
         Criteria criteria = getCriteria();
         criteria.add( Restrictions.eq( "zeroIsSignificant", zeroIsSignificant ) );
         criteria.add( Restrictions.eq( "type", DataElement.VALUE_TYPE_INT ) );
-        criteria.setCacheable( true );
 
         return criteria.list();
     }
@@ -232,8 +193,7 @@
     {
         String hql = "select distinct de from DataElement de join de.dataSets ds where ds.id in (:ids)";
 
-        return sessionFactory.getCurrentSession().createQuery( hql )
-            .setParameterList( "ids", ConversionUtils.getIdentifiers( DataSet.class, dataSets ) ).list();
+        return getQuery( hql ).setParameterList( "ids", getIdentifiers( DataSet.class, dataSets ) ).list();
     }
 
     @SuppressWarnings("unchecked")
@@ -289,6 +249,7 @@
 
         Query query = getQuery( hql );
         query.setInteger( "dataSetId", dataSet.getId() );
+        
         if ( max != null )
         {
             query.setMaxResults( max );
@@ -300,11 +261,6 @@
     @Override
     public int getCountByDomainType( String domainType )
     {
-        Session session = sessionFactory.getCurrentSession();
-
-        Criteria criteria = session.createCriteria( DataElement.class );
-        criteria.add( Restrictions.eq( "domainType", domainType ) );
-
-        return criteria.list().size();
+        return getCriteria( Restrictions.eq( "domainType", domainType ) ).list().size(); // TODO improve
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/hibernate/HibernateDataSetStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/hibernate/HibernateDataSetStore.java	2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/hibernate/HibernateDataSetStore.java	2014-04-02 11:58:10 +0000
@@ -28,9 +28,10 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import org.hibernate.Criteria;
-import org.hibernate.Query;
-import org.hibernate.Session;
+import static org.hisp.dhis.system.util.ConversionUtils.getIdentifiers;
+
+import java.util.Collection;
+
 import org.hibernate.criterion.Restrictions;
 import org.hisp.dhis.common.hibernate.HibernateIdentifiableObjectStore;
 import org.hisp.dhis.dataset.DataSet;
@@ -38,9 +39,6 @@
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.period.PeriodService;
 import org.hisp.dhis.period.PeriodType;
-import org.hisp.dhis.system.util.ConversionUtils;
-
-import java.util.Collection;
 
 /**
  * @author Kristian Nordal
@@ -89,12 +87,7 @@
     {
         periodType = periodService.reloadPeriodType( periodType );
 
-        Session session = sessionFactory.getCurrentSession();
-
-        Criteria criteria = session.createCriteria( DataSet.class );
-        criteria.add( Restrictions.eq( "periodType", periodType ) );
-
-        return criteria.list();
+        return getCriteria( Restrictions.eq( "periodType", periodType ) ).list();
     }
 
     @SuppressWarnings("unchecked")
@@ -102,26 +95,22 @@
     {
         String hql = "select distinct d from DataSet d join d.sources s where s.id in (:ids)";
 
-        return sessionFactory.getCurrentSession().createQuery( hql )
-            .setParameterList( "ids", ConversionUtils.getIdentifiers( OrganisationUnit.class, sources ) ).list();
+        return getQuery( hql ).setParameterList( "ids", getIdentifiers( OrganisationUnit.class, sources ) ).list();
     }
 
     @SuppressWarnings("unchecked")
     public Collection<DataSet> getDataSetsForMobile( OrganisationUnit source )
     {
         String hql = "from DataSet d where :source in elements(d.sources) and d.mobile = true";
-        Query query = sessionFactory.getCurrentSession().createQuery( hql );
-        query.setEntity( "source", source );
-
-        return query.list();
+        
+        return getQuery( hql ).setEntity( "source", source ).list();
     }
 
     @SuppressWarnings("unchecked")
     public Collection<DataSet> getDataSetsForMobile()
     {
         String hql = "from DataSet d where d.mobile = true";
-        Query query = sessionFactory.getCurrentSession().createQuery( hql );
-
-        return query.list();
+        
+        return getQuery( hql ).list();
     }
 }