← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 8082: Auto-send SMS (WIP).

 

------------------------------------------------------------
revno: 8082
committer: Tran Chau <tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2012-09-14 15:13:33 +0700
message:
  Auto-send SMS (WIP).
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/setting/SystemSettingManager.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/outbound/OutboundSmsService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/outbound/OutboundSmsStore.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/scheduling/SendScheduledMessageTask.java
  dhis-2/dhis-services/dhis-service-patient/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/outbound/HibernateOutboundSmsStore.java
  dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/outbound/OutboundSmsServiceImpl.java
  dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/outbound/TestOutboundSmsService.java
  dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/SmsLibService.java
  dhis-2/dhis-services/dhis-service-sms/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/schedule/ScheduleSendMessageTasksAction.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-api/src/main/java/org/hisp/dhis/setting/SystemSettingManager.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/setting/SystemSettingManager.java	2012-09-10 10:28:33 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/setting/SystemSettingManager.java	2012-09-14 08:13:33 +0000
@@ -71,6 +71,7 @@
     final String KEY_CACHE_STRATEGY = "keyCacheStrategy";
     final String KEY_TIME_FOR_SENDING_MESSAGE = "timeSendingMessage";
     final String KEY_SEND_MESSAGE_SCHEDULED_TASKS = "sendMessageScheduled";
+    final String KEY_SCHEDULE_MESSAGE_TASKS = "scheduleMessage";
     final String KEY_SEND_MESSAGE_GATEWAY = "keySendMessageGateway";
 
     final int DEFAULT_MAX_NUMBER_OF_ATTEMPTS = 20;

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/outbound/OutboundSmsService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/outbound/OutboundSmsService.java	2012-09-14 04:57:46 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/outbound/OutboundSmsService.java	2012-09-14 08:13:33 +0000
@@ -52,6 +52,10 @@
         throws SmsServiceException;
 
     List<OutboundSms> getAllOutboundSms();
+    
+    int saveOutboundSms( OutboundSms sms);
 
-    int save( OutboundSms sms );
+    void updateOutboundSms( OutboundSms sms);
+    
+    List<OutboundSms> getOutboundSms( OutboundSmsStatus status );
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/outbound/OutboundSmsStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/outbound/OutboundSmsStore.java	2012-03-12 20:25:54 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/outbound/OutboundSmsStore.java	2012-09-14 08:13:33 +0000
@@ -32,9 +32,12 @@
 public interface OutboundSmsStore
 {
     int save( OutboundSms sms );
+    
+    void update( OutboundSms sms );
 
     List<OutboundSms> getAll();
 
     OutboundSms get( int id );
     
+    List<OutboundSms> get( OutboundSmsStatus status );    
 }

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/scheduling/SendScheduledMessageTask.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/scheduling/SendScheduledMessageTask.java	2012-09-12 04:14:03 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/scheduling/SendScheduledMessageTask.java	2012-09-14 08:13:33 +0000
@@ -30,6 +30,7 @@
 import static org.hisp.dhis.setting.SystemSettingManager.KEY_SEND_MESSAGE_GATEWAY;
 
 import java.util.Collection;
+import java.util.List;
 
 import org.hisp.dhis.program.ProgramStageInstanceService;
 import org.hisp.dhis.program.SchedulingProgramObject;
@@ -37,6 +38,7 @@
 import org.hisp.dhis.sms.SmsServiceException;
 import org.hisp.dhis.sms.outbound.OutboundSms;
 import org.hisp.dhis.sms.outbound.OutboundSmsService;
+import org.hisp.dhis.sms.outbound.OutboundSmsStatus;
 import org.springframework.jdbc.core.JdbcTemplate;
 
 /**
@@ -47,8 +49,8 @@
 public class SendScheduledMessageTask
     implements Runnable
 {
-    private final String DHIS_SYSTEM = "DHIS system";
-    
+    private final String DHIS_SYSTEM_SENDER = "DHIS-System";
+
     private SystemSettingManager systemSettingManager;
 
     public void setSystemSettingManager( SystemSettingManager systemSettingManager )
@@ -62,32 +64,44 @@
     {
         this.programStageInstanceService = programStageInstanceService;
     }
-    
+
     private JdbcTemplate jdbcTemplate;
 
     public void setJdbcTemplate( JdbcTemplate jdbcTemplate )
     {
         this.jdbcTemplate = jdbcTemplate;
     }
-    
+
     private OutboundSmsService outboundSmsService;
 
     public void setOutboundSmsService( OutboundSmsService outboundSmsService )
     {
         this.outboundSmsService = outboundSmsService;
     }
-    
+
     // -------------------------------------------------------------------------
     // Constructors
     // -------------------------------------------------------------------------
 
     public SendScheduledMessageTask( SystemSettingManager systemSettingManager,
-        ProgramStageInstanceService programStageInstanceService, OutboundSmsService outboundSmsService,JdbcTemplate jdbcTemplate )
+        ProgramStageInstanceService programStageInstanceService, JdbcTemplate jdbcTemplate,
+        OutboundSmsService outboundSmsService )
     {
         this.systemSettingManager = systemSettingManager;
         this.programStageInstanceService = programStageInstanceService;
+        this.jdbcTemplate = jdbcTemplate;
         this.outboundSmsService = outboundSmsService;
-        this.jdbcTemplate = jdbcTemplate;
+    }
+
+    // -------------------------------------------------------------------------
+    // Params
+    // -------------------------------------------------------------------------
+
+    private boolean sendingMessage;
+
+    public void setSendingMessage( boolean sendingMessage )
+    {
+        this.sendingMessage = sendingMessage;
     }
 
     // -------------------------------------------------------------------------
@@ -104,32 +118,47 @@
             Collection<SchedulingProgramObject> schedulingProgramObjects = programStageInstanceService
                 .getSendMesssageEvents();
 
-            for ( SchedulingProgramObject schedulingProgramObject : schedulingProgramObjects )
-            {
-                String message = schedulingProgramObject.getMessage();
-
-                String phoneNumber = schedulingProgramObject.getPhoneNumber();
-                
-                if ( phoneNumber != null && !phoneNumber.isEmpty() )
-                {
-                    try
-                    {
-                        OutboundSms outboundSms = new OutboundSms( message, phoneNumber );
-                        outboundSms.setSender( DHIS_SYSTEM );
-                        outboundSmsService.sendMessage( outboundSms, gatewayId );
-                        
-                        String sql = " INSERT INTO programstageinstance_outboundsms"
-                            + "( programstageinstanceid, outboundsmsid, sort_order) VALUES " + "("
-                            + schedulingProgramObject.getProgramStageInstanceId() + ", " + outboundSms.getId() + ",1) ";
-                        
-                       jdbcTemplate.execute( sql );
-                    }
-                    catch ( SmsServiceException e )
-                    {
-                        message = e.getMessage();
-                    }
+            if ( sendingMessage )
+            {
+                for ( SchedulingProgramObject schedulingProgramObject : schedulingProgramObjects )
+                {
+                    String message = schedulingProgramObject.getMessage();
+
+                    String phoneNumber = schedulingProgramObject.getPhoneNumber();
+
+                    if ( phoneNumber != null && !phoneNumber.isEmpty() )
+                    {
+                        try
+                        {
+                            OutboundSms outboundSms = new OutboundSms( message, phoneNumber );
+                            outboundSms.setSender( DHIS_SYSTEM_SENDER );
+                            outboundSmsService.saveOutboundSms( outboundSms );
+
+                            String sql = "INSERT INTO programstageinstance_outboundsms"
+                                + "( programstageinstanceid, outboundsmsid, sort_order) VALUES " + "("
+                                + schedulingProgramObject.getProgramStageInstanceId() + ", " + outboundSms.getId()
+                                + ",1) ";
+
+                            jdbcTemplate.execute( sql );
+                        }
+                        catch ( SmsServiceException e )
+                        {
+                            message = e.getMessage();
+                        }
+                    }
+                }
+            }
+            else
+            {
+
+                List<OutboundSms> outboundSmsList = outboundSmsService.getOutboundSms( OutboundSmsStatus.OUTBOUND );
+                for ( OutboundSms outboundSms : outboundSmsList )
+                {
+                    outboundSms.setStatus( OutboundSmsStatus.SENT );
+                    outboundSmsService.sendMessage( outboundSms, gatewayId );
                 }
             }
         }
+
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-patient/src/main/resources/META-INF/dhis/beans.xml	2012-09-14 03:27:35 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/resources/META-INF/dhis/beans.xml	2012-09-14 08:13:33 +0000
@@ -372,18 +372,24 @@
     <property name="tasks">
       <map>
         <entry key="sendMessageScheduled" value-ref="sendMessageScheduled" />
+        <entry key="scheduleMessage" value-ref="scheduleMessage" />
       </map>
     </property>
    </bean>
   
- 	<bean id="sendMessageScheduledTasks" class="org.hisp.dhis.patient.scheduling.SendScheduledMessageTask" >
+ 	<bean id="abstractScheduledMessageTask" class="org.hisp.dhis.patient.scheduling.SendScheduledMessageTask" >
  		<constructor-arg ref="org.hisp.dhis.program.ProgramStageInstanceService" />
  		<constructor-arg ref="org.hisp.dhis.setting.SystemSettingManager" />
  		<constructor-arg ref="org.hisp.dhis.sms.outbound.OutboundSmsService" />
  		<constructor-arg ref="jdbcTemplate" />
  	</bean>
  	
- 	<bean id="sendMessageScheduled" class="org.hisp.dhis.patient.scheduling.SendScheduledMessageTask" parent="sendMessageScheduledTasks">
+ 	<bean id="sendMessageScheduled" class="org.hisp.dhis.patient.scheduling.SendScheduledMessageTask" parent="abstractScheduledMessageTask">
+		<property name="sendingMessage" value="true"/>
+	</bean>  
+	
+	<bean id="scheduleMessage" class="org.hisp.dhis.patient.scheduling.SendScheduledMessageTask" parent="abstractScheduledMessageTask">
+		<property name="sendingMessage" value="false"/>
 	</bean>  
   	
     <!-- Startup -->

=== modified file 'dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/outbound/HibernateOutboundSmsStore.java'
--- dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/outbound/HibernateOutboundSmsStore.java	2012-03-09 09:40:27 +0000
+++ dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/outbound/HibernateOutboundSmsStore.java	2012-09-14 08:13:33 +0000
@@ -27,13 +27,15 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.sql.ResultSet;
+import java.sql.SQLException;
 import java.util.Date;
 import java.util.List;
 
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
-import org.hisp.dhis.sms.outbound.OutboundSms;
-import org.hisp.dhis.sms.outbound.OutboundSmsStore;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.core.RowMapper;
 
 public class HibernateOutboundSmsStore
     implements OutboundSmsStore
@@ -48,7 +50,14 @@
     {
         this.sessionFactory = sessionFactory;
     }
+    
+    private JdbcTemplate jdbcTemplate;
 
+    public void setJdbcTemplate( JdbcTemplate jdbcTemplate )
+    {
+        this.jdbcTemplate = jdbcTemplate;
+    }
+    
     @Override
     public int save( OutboundSms sms )
     {
@@ -79,4 +88,36 @@
         Session session = sessionFactory.getCurrentSession();
         return (List<OutboundSms>) session.createCriteria( OutboundSms.class ).list();
     }
+    
+    @Override
+    @SuppressWarnings( "unchecked" )
+    public List<OutboundSms> get( OutboundSmsStatus status )
+    {
+        String sql = "select id from outbound_sms where status = 1";
+
+        try
+        {
+            List<OutboundSms> OutboundSmsList = jdbcTemplate.query( sql, new RowMapper<OutboundSms>()
+            {
+                public OutboundSms mapRow( ResultSet rs, int rowNum )
+                    throws SQLException
+                {
+                    return get(rs.getInt( 1 ));
+                }
+            } );
+            
+            return OutboundSmsList;
+        }
+        catch ( Exception ex )
+        {
+            ex.printStackTrace();
+            return null;
+        }
+    }
+    
+    @Override
+    public void update( OutboundSms sms )
+    {
+        sessionFactory.getCurrentSession().update( sms );
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/outbound/OutboundSmsServiceImpl.java'
--- dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/outbound/OutboundSmsServiceImpl.java	2012-09-14 04:57:46 +0000
+++ dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/outbound/OutboundSmsServiceImpl.java	2012-09-14 08:13:33 +0000
@@ -53,7 +53,6 @@
     // Dependencies
     // -------------------------------------------------------------------------
 
-    @Autowired
     private OutboundSmsStore outboundSmsStore;
 
     public void setOutboundSmsStore( OutboundSmsStore outboundSmsStore )
@@ -102,7 +101,14 @@
             throw new SmsServiceNotEnabledException();
         }
 
-        outboundSmsStore.save( sms );
+        if ( sms.getId() == 0 )
+        {
+            outboundSmsStore.save( sms );
+        }
+        else
+        {
+            outboundSmsStore.update( sms );
+        }
 
         if ( transportService != null )
         {
@@ -111,15 +117,27 @@
 
         return "outboundsms_saved";
     }
-    
+
     @Override
     public List<OutboundSms> getAllOutboundSms()
     {
         return outboundSmsStore.getAll();
     }
+
+    @Override
+    public List<OutboundSms> getOutboundSms( OutboundSmsStatus status )
+    {
+        return outboundSmsStore.get( status );
+    }
+
+    @Override
+    public void updateOutboundSms( OutboundSms sms)
+    {
+        outboundSmsStore.update( sms );
+    }
     
     @Override
-    public int save(OutboundSms sms) {
+    public int saveOutboundSms(OutboundSms sms) {
         return outboundSmsStore.save( sms );
     }
     
@@ -137,8 +155,7 @@
         {
             log.debug( "Exception sending message " + sms, e );
             sms.setStatus( OutboundSmsStatus.ERROR );
-            this.save( sms );
-            
+            this.saveOutboundSms( sms );
             return "Exception sending message " + sms + e.getMessage();
         }
     }

=== modified file 'dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/outbound/TestOutboundSmsService.java'
--- dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/outbound/TestOutboundSmsService.java	2012-09-14 04:57:46 +0000
+++ dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/outbound/TestOutboundSmsService.java	2012-09-14 08:13:33 +0000
@@ -106,9 +106,23 @@
     }
 
     @Override
-    public int save( OutboundSms sms )
+    public int saveOutboundSms( OutboundSms sms )
     {
         // TODO Auto-generated method stub
         return 0;
     }
+
+    @Override
+    public void updateOutboundSms( OutboundSms sms )
+    {
+        // TODO Auto-generated method stub
+        
+    }
+
+    @Override
+    public List<OutboundSms> getOutboundSms( OutboundSmsStatus status )
+    {
+        // TODO Auto-generated method stub
+        return null;
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/SmsLibService.java'
--- dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/SmsLibService.java	2012-09-14 04:57:46 +0000
+++ dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/SmsLibService.java	2012-09-14 08:13:33 +0000
@@ -199,7 +199,7 @@
             sms.setStatus( OutboundSmsStatus.ERROR );
         }
 
-        this.save( sms );
+        this.saveOutboundSms( sms );
 
         return message;
     }
@@ -463,8 +463,20 @@
     }
 
     @Override
-    public int save( OutboundSms sms )
+    public int saveOutboundSms( OutboundSms sms )
     {
         return outboundSmsStore.save( sms );
     }
+
+    @Override
+    public void updateOutboundSms( OutboundSms sms )
+    {
+        outboundSmsStore.update( sms );
+    }
+
+    @Override
+    public List<OutboundSms> getOutboundSms( OutboundSmsStatus status )
+    {
+        return outboundSmsStore.get( status );
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-sms/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-sms/src/main/resources/META-INF/dhis/beans.xml	2012-09-14 04:57:46 +0000
+++ dhis-2/dhis-services/dhis-service-sms/src/main/resources/META-INF/dhis/beans.xml	2012-09-14 08:13:33 +0000
@@ -9,16 +9,19 @@
 
   <bean id="outboundSmsStore" class="org.hisp.dhis.sms.outbound.HibernateOutboundSmsStore">
     <property name="sessionFactory" ref="sessionFactory" />
+    <property name="jdbcTemplate" ref="jdbcTemplate" />
   </bean>
 
-  <bean id="org.hisp.dhis.sms.outbound.OutboundSmsService" class="org.hisp.dhis.sms.outbound.OutboundSmsServiceImpl" />
+  <bean id="org.hisp.dhis.sms.outbound.OutboundSmsService" class="org.hisp.dhis.sms.outbound.OutboundSmsServiceImpl">
+  	<property name="outboundSmsStore" ref="outboundSmsStore" />
+  </bean>
 
   <!-- Only for testing! OutboundSmsService stub just logging invocations, replacing the default one or use as the OutboundSmsTransportService.. -->
   <!-- <bean id="TestOutboundSmsService" class="org.hisp.dhis.sms.outbound.TestOutboundSmsService" /> -->
 
   <bean id="smsLibService" class="org.hisp.dhis.sms.smslib.SmsLibService">
     <property name="smppInboundMessageNotification" ref="org.hisp.dhis.sms.incoming.SMPPInboundNotification" />
-	<property name="outboundSmsStore" ref="org.hisp.dhis.sms.outbound.OutboundSmsStore" />
+	<property name="outboundSmsStore" ref="outboundSmsStore" />
   </bean>
 
   <bean id="org.hisp.dhis.sms.incoming.SMPPInboundNotification" class="org.hisp.dhis.sms.incoming.SMPPInboundNotification">

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/schedule/ScheduleSendMessageTasksAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/schedule/ScheduleSendMessageTasksAction.java	2012-09-11 01:58:59 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/schedule/ScheduleSendMessageTasksAction.java	2012-09-14 08:13:33 +0000
@@ -31,6 +31,7 @@
 import static org.hisp.dhis.setting.SystemSettingManager.KEY_SEND_MESSAGE_GATEWAY;
 import static org.hisp.dhis.setting.SystemSettingManager.KEY_TIME_FOR_SENDING_MESSAGE;
 import static org.hisp.dhis.setting.SystemSettingManager.KEY_SEND_MESSAGE_SCHEDULED_TASKS;
+import static org.hisp.dhis.setting.SystemSettingManager.KEY_SCHEDULE_MESSAGE_TASKS;
 
 import java.util.HashMap;
 import java.util.Map;
@@ -142,8 +143,8 @@
                 String time = (String) systemSettingManager.getSystemSetting( KEY_TIME_FOR_SENDING_MESSAGE,
                     DEFAULT_TIME_FOR_SENDING_MESSAGE );
 
+                // Schedule for sending messages
                 String[] infor = time.split( ":" );
-
                 String hour = infor[0].trim();
                 String minute = infor[1].trim();
 
@@ -151,15 +152,14 @@
                 {
                     hour = "0";
                 }
-
                 if ( minute.trim().equals( "00" ) )
                 {
                     minute = "0";
                 }
-
                 String cron = "0 " + Integer.parseInt( minute ) + " " + Integer.parseInt( hour ) + " ? * *";
 
                 keyCronMap.put( KEY_SEND_MESSAGE_SCHEDULED_TASKS, cron );
+                keyCronMap.put( KEY_SCHEDULE_MESSAGE_TASKS, "0 0 0 * * ?" );
 
                 schedulingManager.scheduleTasks( keyCronMap );
             }