=== added file 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/DefaultProgramSchedulingManager.java' --- dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/DefaultProgramSchedulingManager.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/DefaultProgramSchedulingManager.java 2014-05-16 08:12:25 +0000 @@ -0,0 +1,139 @@ +package org.hisp.dhis.scheduling; + +/* + * Copyright (c) 2004-2014, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import static org.hisp.dhis.setting.SystemSettingManager.KEY_SEND_MESSAGE_SCHEDULED_TASKS; +import static org.hisp.dhis.system.scheduling.Scheduler.STATUS_NOT_STARTED; + +import java.util.HashMap; +import java.util.Map; + +import org.hisp.dhis.setting.SystemSettingManager; +import org.hisp.dhis.system.scheduling.Scheduler; + +/** + * @author Chau Thu Tran + * + * @version DefaultProgramSchedulingManager.java 12:51:02 PM Sep 10, 2012 $ + */ +public class DefaultProgramSchedulingManager implements ProgramSchedulingManager +{ + // ------------------------------------------------------------------------- + // Dependencies + // ------------------------------------------------------------------------- + + private SystemSettingManager systemSettingManager; + + public void setSystemSettingManager( SystemSettingManager systemSettingManager ) + { + this.systemSettingManager = systemSettingManager; + } + + private Scheduler scheduler; + + public void setScheduler( Scheduler scheduler ) + { + this.scheduler = scheduler; + } + + private Map tasks = new HashMap(); + + public void setTasks( Map tasks ) + { + this.tasks = tasks; + } + + // ------------------------------------------------------------------------- + // SchedulingManager implementation + // ------------------------------------------------------------------------- + + public void scheduleTasks() + { + Map keyCronMap = getScheduledTasks(); + + for ( String key : keyCronMap.keySet() ) + { + String cron = keyCronMap.get( key ); + Runnable task = tasks.get( key ); + + if ( cron != null && task != null ) + { + scheduler.scheduleTask( key, task, cron ); + } + } + } + + public void scheduleTasks( Map keyCronMap ) + { + systemSettingManager.saveSystemSetting( KEY_SEND_MESSAGE_SCHEDULED_TASKS, new HashMap( keyCronMap ) ); + + scheduleTasks(); + } + + public void stopTasks() + { + systemSettingManager.saveSystemSetting( KEY_SEND_MESSAGE_SCHEDULED_TASKS, null ); + + scheduler.stopAllTasks(); + } + + public void executeTasks() + { + Map keyCronMap = getScheduledTasks(); + + for ( String key : keyCronMap.keySet() ) + { + Runnable task = tasks.get( key ); + + if ( task != null ) + { + scheduler.executeTask( task ); + } + } + } + + @SuppressWarnings("unchecked") + public Map getScheduledTasks() + { + return (Map) systemSettingManager.getSystemSetting( KEY_SEND_MESSAGE_SCHEDULED_TASKS, new HashMap() ); + } + + public String getTaskStatus() + { + Map keyCronMap = getScheduledTasks(); + + if ( keyCronMap.size() == 0 ) + { + return STATUS_NOT_STARTED; + } + + return scheduler.getTaskStatus( keyCronMap.keySet().iterator().next() ); + } +} + === added file 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/ProgramSchedulingManager.java' --- dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/ProgramSchedulingManager.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/ProgramSchedulingManager.java 2014-05-16 08:12:25 +0000 @@ -0,0 +1,53 @@ +package org.hisp.dhis.scheduling; + +/* + * Copyright (c) 2004-2014, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import java.util.Map; + +/** + * @author Chau Thu Tran + * + * @version ProgramSchedulingManager.java 12:47:57 PM Sep 10, 2012 $ + */ +public interface ProgramSchedulingManager +{ + final String TASK_SENDING_MESSAGE = "sendingMessageTask"; + + void scheduleTasks(); + + void scheduleTasks( Map keyCronMap ); + + void stopTasks(); + + void executeTasks(); + + Map getScheduledTasks(); + + String getTaskStatus(); +} === added file 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/SendScheduledMessageTask.java' --- dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/SendScheduledMessageTask.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/SendScheduledMessageTask.java 2014-05-16 08:12:25 +0000 @@ -0,0 +1,263 @@ +package org.hisp.dhis.scheduling; + +/* + * Copyright (c) 2004-2014, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +import static org.hisp.dhis.sms.outbound.OutboundSms.DHIS_SYSTEM_SENDER; +import static org.hisp.dhis.system.notification.NotificationLevel.INFO; +import java.util.Collection; +import java.util.List; +import org.hisp.dhis.program.ProgramInstanceService; +import org.hisp.dhis.program.ProgramStageInstanceService; +import org.hisp.dhis.program.SchedulingProgramObject; +import org.hisp.dhis.scheduling.TaskId; +import org.hisp.dhis.sms.SmsSender; +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.hisp.dhis.system.notification.Notifier; +import org.hisp.dhis.system.util.Clock; +import org.hisp.dhis.system.util.SystemUtils; +import org.springframework.jdbc.core.JdbcTemplate; + +/** + * @author Chau Thu Tran + * + * @version SendScheduledMessageTask.java 12:57:53 PM Sep 10, 2012 $ + */ +public class SendScheduledMessageTask + implements Runnable +{ + private ProgramStageInstanceService programStageInstanceService; + + public void setProgramStageInstanceService( ProgramStageInstanceService programStageInstanceService ) + { + this.programStageInstanceService = programStageInstanceService; + } + + private ProgramInstanceService programInstanceService; + + public void setProgramInstanceService( ProgramInstanceService programInstanceService ) + { + this.programInstanceService = programInstanceService; + } + + private OutboundSmsService outboundSmsService; + + public void setOutboundSmsService( OutboundSmsService outboundSmsService ) + { + this.outboundSmsService = outboundSmsService; + } + + private SmsSender smsSender; + + public void setSmsSender( SmsSender smsSender ) + { + this.smsSender = smsSender; + } + + private JdbcTemplate jdbcTemplate; + + public void setJdbcTemplate( JdbcTemplate jdbcTemplate ) + { + this.jdbcTemplate = jdbcTemplate; + } + + private Notifier notifier; + + public void setNotifier( Notifier notifier ) + { + this.notifier = notifier; + } + + // ------------------------------------------------------------------------- + // Params + // ------------------------------------------------------------------------- + + private Boolean sendingMessage; + + public void setSendingMessage( Boolean sendingMessage ) + { + this.sendingMessage = sendingMessage; + } + + private TaskId taskId; + + public void setTaskId( TaskId taskId ) + { + this.taskId = taskId; + } + + // ------------------------------------------------------------------------- + // Runnable implementation + // ------------------------------------------------------------------------- + + @Override + public void run() + { + final int cpuCores = SystemUtils.getCpuCores(); + + Clock clock = new Clock().startClock().logTime( + "Aggregate process started, number of CPU cores: " + cpuCores + ", " + SystemUtils.getMemoryString() ); + + if ( sendingMessage ) + { + clock.logTime( "Start to send messages in outbound" ); + notifier.notify( taskId, INFO, "Start to send messages in outbound", true ); + + sendMessage(); + + clock.logTime( "Sending messages in outbound completed" ); + notifier.notify( taskId, INFO, "Sending messages in outbound completed", true ); + } + else + { + clock.logTime( "Start to prepare reminder messages" ); + notifier.clear( taskId ).notify( taskId, "Start to prepare reminder messages" ); + + scheduleProgramStageInstanceMessage(); + scheduleProgramInstanceMessage(); + + sendMessage(); + + clock.logTime( "Preparing reminder messages completed" ); + notifier.notify( taskId, INFO, "Preparing reminder messages completed", true ); + } + + } + + // ------------------------------------------------------------------------- + // Supportive methods + // ------------------------------------------------------------------------- + + private void scheduleProgramStageInstanceMessage() + { + notifier.notify( taskId, "Start to prepare reminder messages for events" ); + + Collection schedulingProgramObjects = programStageInstanceService + .getSendMesssageEvents(); + + for ( SchedulingProgramObject schedulingProgramObject : schedulingProgramObjects ) + { + String message = schedulingProgramObject.getMessage(); + + try + { + OutboundSms outboundSms = new OutboundSms( message, schedulingProgramObject.getPhoneNumber() ); + outboundSms.setSender( DHIS_SYSTEM_SENDER ); + outboundSmsService.saveOutboundSms( outboundSms ); + + String sortOrderSql = "SELECT max(sort_order) " + + "FROM programstageinstance_outboundsms where programstageinstanceid=" + + schedulingProgramObject.getProgramStageInstanceId(); + Integer sortOrder = jdbcTemplate.queryForObject( sortOrderSql, Integer.class ); + if ( sortOrder == null ) + { + sortOrder = 0; + } + sortOrder = sortOrder + 1; + + String sql = "INSERT INTO programstageinstance_outboundsms" + + "( programstageinstanceid, outboundsmsid, sort_order) VALUES " + "(" + + schedulingProgramObject.getProgramStageInstanceId() + ", " + outboundSms.getId() + "," + + sortOrder + ") "; + + jdbcTemplate.execute( sql ); + + notifier.notify( taskId, "Reminder messages for event of " + outboundSms.getRecipients() + + " is created " ); + } + catch ( SmsServiceException e ) + { + message = e.getMessage(); + } + } + + notifier.notify( taskId, INFO, "Preparing reminder messages for events completed", true ); + } + + private void scheduleProgramInstanceMessage() + { + notifier.notify( taskId, "Start to prepare reminder messages for enrollements" ); + + Collection schedulingProgramObjects = programInstanceService.getScheduleMesssages(); + + for ( SchedulingProgramObject schedulingProgramObject : schedulingProgramObjects ) + { + String message = schedulingProgramObject.getMessage(); + try + { + OutboundSms outboundSms = new OutboundSms( message, schedulingProgramObject.getPhoneNumber() ); + outboundSms.setSender( DHIS_SYSTEM_SENDER ); + outboundSmsService.saveOutboundSms( outboundSms ); + + String sortOrderSql = "select max(sort_order) " + + "from programinstance_outboundsms where programinstanceid=" + + schedulingProgramObject.getProgramInstanceId(); + Integer sortOrder = jdbcTemplate.queryForObject( sortOrderSql, Integer.class ); + if ( sortOrder == null ) + { + sortOrder = 0; + } + sortOrder = sortOrder + 1; + + String sql = "INSERT INTO programinstance_outboundsms" + + "( programinstanceid, outboundsmsid, sort_order) VALUES " + "(" + + schedulingProgramObject.getProgramInstanceId() + ", " + outboundSms.getId() + "," + sortOrder + + ") "; + + jdbcTemplate.execute( sql ); + + notifier.notify( taskId, "Reminder messages for enrollement of " + outboundSms.getRecipients() + + " is created " ); + } + catch ( SmsServiceException e ) + { + message = e.getMessage(); + } + } + + notifier.notify( taskId, INFO, "Sending reminder messages for enrollement completed", true ); + + } + + private void sendMessage() + { + List outboundSmsList = outboundSmsService.getOutboundSms( OutboundSmsStatus.OUTBOUND ); + + if ( outboundSmsList != null ) + { + for ( OutboundSms outboundSms : outboundSmsList ) + { + outboundSms.setStatus( OutboundSmsStatus.SENT ); + smsSender.sendMessage( outboundSms, null ); + } + } + } +} === modified file 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/resources/META-INF/dhis/beans.xml' --- dhis-2/dhis-services/dhis-service-eventreporting/src/main/resources/META-INF/dhis/beans.xml 2014-03-10 18:36:41 +0000 +++ dhis-2/dhis-services/dhis-service-eventreporting/src/main/resources/META-INF/dhis/beans.xml 2014-05-16 08:12:25 +0000 @@ -60,6 +60,43 @@ parent="abstractRunCaseAggregateConditionTask"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + tasks = new HashMap(); - - public void setTasks( Map tasks ) - { - this.tasks = tasks; - } - - // ------------------------------------------------------------------------- - // SchedulingManager implementation - // ------------------------------------------------------------------------- - - public void scheduleTasks() - { - Map keyCronMap = getScheduledTasks(); - - for ( String key : keyCronMap.keySet() ) - { - String cron = keyCronMap.get( key ); - Runnable task = tasks.get( key ); - - if ( cron != null && task != null ) - { - scheduler.scheduleTask( key, task, cron ); - } - } - } - - public void scheduleTasks( Map keyCronMap ) - { - systemSettingManager.saveSystemSetting( KEY_SEND_MESSAGE_SCHEDULED_TASKS, new HashMap( keyCronMap ) ); - - scheduleTasks(); - } - - public void stopTasks() - { - systemSettingManager.saveSystemSetting( KEY_SEND_MESSAGE_SCHEDULED_TASKS, null ); - - scheduler.stopAllTasks(); - } - - public void executeTasks() - { - Map keyCronMap = getScheduledTasks(); - - for ( String key : keyCronMap.keySet() ) - { - Runnable task = tasks.get( key ); - - if ( task != null ) - { - scheduler.executeTask( task ); - } - } - } - - @SuppressWarnings("unchecked") - public Map getScheduledTasks() - { - return (Map) systemSettingManager.getSystemSetting( KEY_SEND_MESSAGE_SCHEDULED_TASKS, new HashMap() ); - } - - public String getTaskStatus() - { - Map keyCronMap = getScheduledTasks(); - - if ( keyCronMap.size() == 0 ) - { - return STATUS_NOT_STARTED; - } - - return scheduler.getTaskStatus( keyCronMap.keySet().iterator().next() ); - } -} - === removed file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/scheduling/ProgramSchedulingManager.java' --- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/scheduling/ProgramSchedulingManager.java 2014-03-18 08:10:10 +0000 +++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/scheduling/ProgramSchedulingManager.java 1970-01-01 00:00:00 +0000 @@ -1,53 +0,0 @@ -package org.hisp.dhis.trackedentity.scheduling; - -/* - * Copyright (c) 2004-2014, University of Oslo - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * Neither the name of the HISP project nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -import java.util.Map; - -/** - * @author Chau Thu Tran - * - * @version ProgramSchedulingManager.java 12:47:57 PM Sep 10, 2012 $ - */ -public interface ProgramSchedulingManager -{ - final String TASK_SENDING_MESSAGE = "sendingMessageTask"; - - void scheduleTasks(); - - void scheduleTasks( Map keyCronMap ); - - void stopTasks(); - - void executeTasks(); - - Map getScheduledTasks(); - - String getTaskStatus(); -} === removed file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/scheduling/SendScheduledMessageTask.java' --- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/scheduling/SendScheduledMessageTask.java 2014-03-18 08:10:10 +0000 +++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/trackedentity/scheduling/SendScheduledMessageTask.java 1970-01-01 00:00:00 +0000 @@ -1,263 +0,0 @@ -package org.hisp.dhis.trackedentity.scheduling; - -/* - * Copyright (c) 2004-2014, University of Oslo - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * Neither the name of the HISP project nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -import static org.hisp.dhis.sms.outbound.OutboundSms.DHIS_SYSTEM_SENDER; -import static org.hisp.dhis.system.notification.NotificationLevel.INFO; -import java.util.Collection; -import java.util.List; -import org.hisp.dhis.program.ProgramInstanceService; -import org.hisp.dhis.program.ProgramStageInstanceService; -import org.hisp.dhis.program.SchedulingProgramObject; -import org.hisp.dhis.scheduling.TaskId; -import org.hisp.dhis.sms.SmsSender; -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.hisp.dhis.system.notification.Notifier; -import org.hisp.dhis.system.util.Clock; -import org.hisp.dhis.system.util.SystemUtils; -import org.springframework.jdbc.core.JdbcTemplate; - -/** - * @author Chau Thu Tran - * - * @version SendScheduledMessageTask.java 12:57:53 PM Sep 10, 2012 $ - */ -public class SendScheduledMessageTask - implements Runnable -{ - private ProgramStageInstanceService programStageInstanceService; - - public void setProgramStageInstanceService( ProgramStageInstanceService programStageInstanceService ) - { - this.programStageInstanceService = programStageInstanceService; - } - - private ProgramInstanceService programInstanceService; - - public void setProgramInstanceService( ProgramInstanceService programInstanceService ) - { - this.programInstanceService = programInstanceService; - } - - private OutboundSmsService outboundSmsService; - - public void setOutboundSmsService( OutboundSmsService outboundSmsService ) - { - this.outboundSmsService = outboundSmsService; - } - - private SmsSender smsSender; - - public void setSmsSender( SmsSender smsSender ) - { - this.smsSender = smsSender; - } - - private JdbcTemplate jdbcTemplate; - - public void setJdbcTemplate( JdbcTemplate jdbcTemplate ) - { - this.jdbcTemplate = jdbcTemplate; - } - - private Notifier notifier; - - public void setNotifier( Notifier notifier ) - { - this.notifier = notifier; - } - - // ------------------------------------------------------------------------- - // Params - // ------------------------------------------------------------------------- - - private Boolean sendingMessage; - - public void setSendingMessage( Boolean sendingMessage ) - { - this.sendingMessage = sendingMessage; - } - - private TaskId taskId; - - public void setTaskId( TaskId taskId ) - { - this.taskId = taskId; - } - - // ------------------------------------------------------------------------- - // Runnable implementation - // ------------------------------------------------------------------------- - - @Override - public void run() - { - final int cpuCores = SystemUtils.getCpuCores(); - - Clock clock = new Clock().startClock().logTime( - "Aggregate process started, number of CPU cores: " + cpuCores + ", " + SystemUtils.getMemoryString() ); - - if ( sendingMessage ) - { - clock.logTime( "Start to send messages in outbound" ); - notifier.notify( taskId, INFO, "Start to send messages in outbound", true ); - - sendMessage(); - - clock.logTime( "Sending messages in outbound completed" ); - notifier.notify( taskId, INFO, "Sending messages in outbound completed", true ); - } - else - { - clock.logTime( "Start to prepare reminder messages" ); - notifier.clear( taskId ).notify( taskId, "Start to prepare reminder messages" ); - - scheduleProgramStageInstanceMessage(); - scheduleProgramInstanceMessage(); - - sendMessage(); - - clock.logTime( "Preparing reminder messages completed" ); - notifier.notify( taskId, INFO, "Preparing reminder messages completed", true ); - } - - } - - // ------------------------------------------------------------------------- - // Supportive methods - // ------------------------------------------------------------------------- - - private void scheduleProgramStageInstanceMessage() - { - notifier.notify( taskId, "Start to prepare reminder messages for events" ); - - Collection schedulingProgramObjects = programStageInstanceService - .getSendMesssageEvents(); - - for ( SchedulingProgramObject schedulingProgramObject : schedulingProgramObjects ) - { - String message = schedulingProgramObject.getMessage(); - - try - { - OutboundSms outboundSms = new OutboundSms( message, schedulingProgramObject.getPhoneNumber() ); - outboundSms.setSender( DHIS_SYSTEM_SENDER ); - outboundSmsService.saveOutboundSms( outboundSms ); - - String sortOrderSql = "SELECT max(sort_order) " - + "FROM programstageinstance_outboundsms where programstageinstanceid=" - + schedulingProgramObject.getProgramStageInstanceId(); - Integer sortOrder = jdbcTemplate.queryForObject( sortOrderSql, Integer.class ); - if ( sortOrder == null ) - { - sortOrder = 0; - } - sortOrder = sortOrder + 1; - - String sql = "INSERT INTO programstageinstance_outboundsms" - + "( programstageinstanceid, outboundsmsid, sort_order) VALUES " + "(" - + schedulingProgramObject.getProgramStageInstanceId() + ", " + outboundSms.getId() + "," - + sortOrder + ") "; - - jdbcTemplate.execute( sql ); - - notifier.notify( taskId, "Reminder messages for event of " + outboundSms.getRecipients() - + " is created " ); - } - catch ( SmsServiceException e ) - { - message = e.getMessage(); - } - } - - notifier.notify( taskId, INFO, "Preparing reminder messages for events completed", true ); - } - - private void scheduleProgramInstanceMessage() - { - notifier.notify( taskId, "Start to prepare reminder messages for enrollements" ); - - Collection schedulingProgramObjects = programInstanceService.getScheduleMesssages(); - - for ( SchedulingProgramObject schedulingProgramObject : schedulingProgramObjects ) - { - String message = schedulingProgramObject.getMessage(); - try - { - OutboundSms outboundSms = new OutboundSms( message, schedulingProgramObject.getPhoneNumber() ); - outboundSms.setSender( DHIS_SYSTEM_SENDER ); - outboundSmsService.saveOutboundSms( outboundSms ); - - String sortOrderSql = "select max(sort_order) " - + "from programinstance_outboundsms where programinstanceid=" - + schedulingProgramObject.getProgramInstanceId(); - Integer sortOrder = jdbcTemplate.queryForObject( sortOrderSql, Integer.class ); - if ( sortOrder == null ) - { - sortOrder = 0; - } - sortOrder = sortOrder + 1; - - String sql = "INSERT INTO programinstance_outboundsms" - + "( programinstanceid, outboundsmsid, sort_order) VALUES " + "(" - + schedulingProgramObject.getProgramInstanceId() + ", " + outboundSms.getId() + "," + sortOrder - + ") "; - - jdbcTemplate.execute( sql ); - - notifier.notify( taskId, "Reminder messages for enrollement of " + outboundSms.getRecipients() - + " is created " ); - } - catch ( SmsServiceException e ) - { - message = e.getMessage(); - } - } - - notifier.notify( taskId, INFO, "Sending reminder messages for enrollement completed", true ); - - } - - private void sendMessage() - { - List outboundSmsList = outboundSmsService.getOutboundSms( OutboundSmsStatus.OUTBOUND ); - - if ( outboundSmsList != null ) - { - for ( OutboundSms outboundSms : outboundSmsList ) - { - outboundSms.setStatus( OutboundSmsStatus.SENT ); - smsSender.sendMessage( outboundSms, null ); - } - } - } -} === modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/resources/META-INF/dhis/beans.xml' --- dhis-2/dhis-services/dhis-service-tracker/src/main/resources/META-INF/dhis/beans.xml 2014-05-13 12:54:58 +0000 +++ dhis-2/dhis-services/dhis-service-tracker/src/main/resources/META-INF/dhis/beans.xml 2014-05-16 08:12:25 +0000 @@ -312,45 +312,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + ref="org.hisp.dhis.scheduling.ProgramSchedulingManager" /> + ref="org.hisp.dhis.scheduling.ProgramSchedulingManager" />