← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19644: SQL view. Impl function for refreshing materalized views in list of sql views.

 

------------------------------------------------------------
revno: 19644
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-07-14 16:50:19 +0200
message:
  SQL view. Impl function for refreshing materalized views in list of sql views.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/sqlview/SqlView.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/sqlview/SqlViewService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/sqlview/SqlViewStore.java
  dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/sqlview/DefaultSqlViewService.java
  dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/sqlview/jdbc/JdbcSqlViewStore.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SqlViewController.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/resources/org/hisp/dhis/dataadmin/i18n_module.properties
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/javascript/sqlView.js
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/sqlView.vm


--
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-api/src/main/java/org/hisp/dhis/sqlview/SqlView.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/sqlview/SqlView.java	2015-03-10 11:36:58 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/sqlview/SqlView.java	2015-07-14 14:50:19 +0000
@@ -183,6 +183,14 @@
     {
         return SqlViewType.QUERY.equals( type );
     }
+
+    /**
+     * Indicates whether this SQL view is a materalized view.
+     */
+    public boolean isMaterializedView()
+    {
+        return SqlViewType.MATERIALIZED_VIEW.equals( type );
+    }
     
     // -------------------------------------------------------------------------
     // Getters and setters

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/sqlview/SqlViewService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/sqlview/SqlViewService.java	2015-06-11 11:48:28 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/sqlview/SqlViewService.java	2015-07-14 14:50:19 +0000
@@ -135,5 +135,19 @@
      */
     Set<String> getVariables( String sql );
     
+    /**
+     * Tests whether the given SQL view syntax is valid.
+     * 
+     * @param sql the SQL view.
+     * @return null if valid, a non-null descriptive string if invalid.
+     */
     String testSqlGrammar( String sql );
+    
+    /**
+     * Refreshes the materialized view.
+     * 
+     * @param sqlView the SQL view.
+     * @return true if the materialized view was refreshed, false if not.
+     */
+    boolean refreshMaterializedView( SqlView sqlView );
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/sqlview/SqlViewStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/sqlview/SqlViewStore.java	2015-03-10 11:36:58 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/sqlview/SqlViewStore.java	2015-07-14 14:50:19 +0000
@@ -58,4 +58,6 @@
      * @return a non-null description if invalid, and null if valid.
      */
     String testSqlGrammar( String sql );
+    
+    boolean refreshMaterializedView( SqlView sqlView );
 }

=== modified file 'dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/sqlview/DefaultSqlViewService.java'
--- dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/sqlview/DefaultSqlViewService.java	2015-06-11 11:46:36 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/sqlview/DefaultSqlViewService.java	2015-07-14 14:50:19 +0000
@@ -63,10 +63,22 @@
     }
 
     // -------------------------------------------------------------------------
-    // Implement methods
+    // CRUD methods
     // -------------------------------------------------------------------------
 
     @Override
+    public int saveSqlView( SqlView sqlView )
+    {
+        return sqlViewStore.save( sqlView );
+    }
+
+    @Override
+    public void updateSqlView( SqlView sqlView )
+    {
+        sqlViewStore.update( sqlView );
+    }
+
+    @Override
     public void deleteSqlView( SqlView sqlView )
     {
         if ( !sqlView.isQuery() )
@@ -108,18 +120,6 @@
     }
 
     @Override
-    public int saveSqlView( SqlView sqlView )
-    {
-        return sqlViewStore.save( sqlView );
-    }
-
-    @Override
-    public void updateSqlView( SqlView sqlView )
-    {
-        sqlViewStore.update( sqlView );
-    }
-
-    @Override
     public int getSqlViewCount()
     {
         return sqlViewStore.getCount();
@@ -144,7 +144,7 @@
     }
     
     // -------------------------------------------------------------------------
-    // SqlView expanded
+    // Service methods
     // -------------------------------------------------------------------------
 
     @Override
@@ -290,4 +290,15 @@
     {
         sqlViewStore.dropViewTable( sqlView );
     }
+
+    @Override
+    public boolean refreshMaterializedView( SqlView sqlView )
+    {
+        if ( sqlView == null || !sqlView.isMaterializedView() )
+        {
+            return false;
+        }
+        
+        return sqlViewStore.refreshMaterializedView( sqlView );
+    }
 }
\ No newline at end of file

=== modified file 'dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/sqlview/jdbc/JdbcSqlViewStore.java'
--- dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/sqlview/jdbc/JdbcSqlViewStore.java	2015-06-15 13:44:20 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/sqlview/jdbc/JdbcSqlViewStore.java	2015-07-14 14:50:19 +0000
@@ -182,4 +182,25 @@
             log.warn( "Could not drop view: " + viewName, ex );
         }
     }
+
+    @Override
+    public boolean refreshMaterializedView( SqlView sqlView )
+    {
+        final String sql = "REFRESH MATERIALIZED VIEW " + sqlView.getViewName();
+        
+        log.debug( "Refresh materialized view: " + sql );
+        
+        try
+        {
+            jdbcTemplate.update( sql );
+            
+            return true;
+        }
+        catch ( Exception ex )
+        {
+            log.warn( "Could not refresh materialized view: " + sqlView.getViewName(), ex );
+            
+            return false;
+        }
+    }
 }
\ No newline at end of file

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SqlViewController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SqlViewController.java	2015-07-09 07:12:16 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/SqlViewController.java	2015-07-14 14:50:19 +0000
@@ -228,4 +228,27 @@
             webMessageService.send( WebMessageUtils.created( "SQL view created" ), response, request );
         }
     }
+    
+    @RequestMapping( value = "/{uid}/refresh", method = RequestMethod.POST )
+    public void refreshMaterializedView( @PathVariable( "uid" ) String uid,
+        HttpServletResponse response, HttpServletRequest request ) throws WebMessageException    
+    {
+        SqlView sqlView = sqlViewService.getSqlViewByUid( uid );
+
+        if ( sqlView == null )
+        {
+            throw new WebMessageException( WebMessageUtils.notFound( "SQL view not found" ) );
+        }
+
+        boolean result = sqlViewService.refreshMaterializedView( sqlView );
+        
+        if ( !result )
+        {
+            throw new WebMessageException( WebMessageUtils.conflict( "View could not be refreshed" ) );
+        }
+        else
+        {
+            webMessageService.send( WebMessageUtils.ok( "Materalized view refreshed" ), response, request );
+        }
+    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/resources/org/hisp/dhis/dataadmin/i18n_module.properties'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/resources/org/hisp/dhis/dataadmin/i18n_module.properties	2015-07-08 08:51:00 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/resources/org/hisp/dhis/dataadmin/i18n_module.properties	2015-07-14 14:50:19 +0000
@@ -386,4 +386,5 @@
 view_name=View name
 user_account_invitations=User account invitations
 pending_invitations=Pending invitations
-expired_invitations=Expired invitations
\ No newline at end of file
+expired_invitations=Expired invitations
+refresh=Refresh
\ No newline at end of file

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/javascript/sqlView.js'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/javascript/sqlView.js	2015-02-12 09:51:39 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/javascript/sqlView.js	2015-07-14 14:50:19 +0000
@@ -66,6 +66,21 @@
   );
 }
 
+function refreshMaterializedView( context ) {
+	var url = '../api/sqlViews/' + context.uid + '/refresh';
+	
+	$.ajax({
+      type: 'post',
+      url: url,
+      success: function() {
+    	  setHeaderDelayMessage( 'Materialized SQL view refreshed' );
+      },
+      error: function() {
+    	  setHeaderDelayMessage( 'Materialized SQL could not be refreshed' );
+      }
+	});
+}
+
 function showUpdateSqlViewForm(context) {
   location.href = 'showUpdateSqlViewForm.action?id=' + context.id;
 }

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/sqlView.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/sqlView.vm	2015-02-11 22:32:01 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/webapp/dhis-web-maintenance-dataadmin/sqlView.vm	2015-07-14 14:50:19 +0000
@@ -21,6 +21,7 @@
   <ul id="contextMenuItems" class="contextMenuItems">
     <li data-enabled="canManage"><a data-target-fn="showSharingDialogWithContext"><i class="fa fa-share"></i>&nbsp;&nbsp;$i18n.getString( "sharing_settings" )</a></li>
     <li data-enabled="canRun"><a data-target-fn="runSqlViewQuery"><i class="fa fa-caret-square-o-right"></i>&nbsp;&nbsp;$i18n.getString( "run" )</a></li>
+    <li data-enabled="canRefresh"><a data-target-fn="refreshMaterializedView"><i class="fa fa-refresh"></i>&nbsp;&nbsp;$i18n.getString( "refresh" )</a></li>
     <li><a data-target-fn="showDataSqlViewForm"><i class="fa fa-table"></i>&nbsp;&nbsp;$i18n.getString( "show_sql_view" )</a></li>
     <li data-enabled="canUpdate"><a data-target-fn="showUpdateSqlViewForm"><i class="fa fa-edit"></i>&nbsp;&nbsp;$i18n.getString( "edit" )</a></li>
     <li data-enabled="canDelete"><a data-target-fn="removeSqlViewObject"><i class="fa fa-trash-o"></i>&nbsp;&nbsp;$i18n.getString( "remove" )</a></li>
@@ -47,10 +48,12 @@
 				<tbody id="list">
 					#foreach( $sqlViewObject in $sqlViewObjectList )
 					#set( $canRun = $sqlViewObject.query == false )
+					#set( $canRefresh = $sqlViewObject.materializedView == true )					
 					<tr id="tr${sqlViewObject.id}">
                     <td data-id="$!sqlViewObject.id" data-uid="$!sqlViewObject.uid" data-type="SqlView" data-name="$encoder.htmlEncode( $!sqlViewObject.displayName )"
                       data-can-manage="$security.canManage( $sqlViewObject )"
                       data-can-run="$canRun"
+                      data-can-refresh="$canRefresh"
                       data-can-update="$security.canUpdate( $sqlViewObject )"
                       data-can-delete="$security.canDelete( $sqlViewObject )">
                       $encoder.htmlEncode( $!sqlViewObject.displayName )