=== removed directory 'dhis-2/dhis-services/dhis-service-eventreporting' === removed file 'dhis-2/dhis-services/dhis-service-eventreporting/pom.xml' --- dhis-2/dhis-services/dhis-service-eventreporting/pom.xml 2016-01-15 00:52:40 +0000 +++ dhis-2/dhis-services/dhis-service-eventreporting/pom.xml 1970-01-01 00:00:00 +0000 @@ -1,37 +0,0 @@ - - 4.0.0 - - - org.hisp.dhis - dhis-services - 2.23-SNAPSHOT - - - dhis-service-eventreporting - jar - DHIS Event Reporting Service - - - - - - - org.hisp.dhis - dhis-api - - - org.hisp.dhis - dhis-service-core - - - org.hisp.dhis - dhis-service-analytics - - - - - ../../ - - === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src' === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src/main' === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/java' === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org' === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp' === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis' === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation' === removed file 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/CaseAggregationConditionDeletionHandler.java' --- dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/CaseAggregationConditionDeletionHandler.java 2016-01-04 02:27:49 +0000 +++ dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/CaseAggregationConditionDeletionHandler.java 1970-01-01 00:00:00 +0000 @@ -1,148 +0,0 @@ -package org.hisp.dhis.caseaggregation; - -/* - * Copyright (c) 2004-2016, 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.Collection; - -import org.hisp.dhis.dataelement.DataElement; -import org.hisp.dhis.dataelement.DataElementCategoryCombo; -import org.hisp.dhis.program.Program; -import org.hisp.dhis.system.deletion.DeletionHandler; -import org.hisp.dhis.trackedentity.TrackedEntityAttribute; - -/** - * @author Chau Thu Tran - */ -public class CaseAggregationConditionDeletionHandler - extends DeletionHandler -{ - // ------------------------------------------------------------------------- - // Dependencies - // ------------------------------------------------------------------------- - - private CaseAggregationConditionService aggregationConditionService; - - public void setAggregationConditionService( CaseAggregationConditionService aggregationConditionService ) - { - this.aggregationConditionService = aggregationConditionService; - } - - // ------------------------------------------------------------------------- - // DeletionHandler implementation - // ------------------------------------------------------------------------- - - @Override - protected String getClassName() - { - return CaseAggregationCondition.class.getSimpleName(); - } - - @Override - public String allowDeleteDataElement( DataElement dataElement ) - { - Collection conditions = aggregationConditionService - .getCaseAggregationCondition( dataElement ); - - if ( conditions != null && conditions.size() > 0 ) - { - return ERROR; - } - - conditions = aggregationConditionService.getAllCaseAggregationCondition(); - - for ( CaseAggregationCondition condition : conditions ) - { - Collection dataElements = aggregationConditionService.getDataElementsInCondition( condition - .getAggregationExpression() ); - - if ( dataElements != null && dataElements.contains( dataElement ) ) - { - return ERROR; - } - } - - return null; - } - - @Override - public String allowDeleteDataElementCategoryCombo( DataElementCategoryCombo categoryCombo ) - { - Collection conditions = aggregationConditionService.getAllCaseAggregationCondition(); - - for ( CaseAggregationCondition condition : conditions ) - { - if ( categoryCombo.getOptionCombos().contains( condition.getOptionCombo() ) ) - { - return ERROR; - } - } - - return null; - } - - @Override - public String allowDeleteProgram( Program program ) - { - Collection conditions = aggregationConditionService - .getAllCaseAggregationCondition(); - - for ( CaseAggregationCondition condition : conditions ) - { - Collection programs = aggregationConditionService.getProgramsInCondition( condition - .getAggregationExpression() ); - - if ( programs != null && programs.contains( program ) ) - { - return condition.getName(); - } - } - - return null; - } - - @Override - public String allowDeleteTrackedEntityAttribute( TrackedEntityAttribute attribute ) - { - Collection conditions = aggregationConditionService - .getAllCaseAggregationCondition(); - - for ( CaseAggregationCondition condition : conditions ) - { - Collection attributes = aggregationConditionService.getTrackedEntityAttributesInCondition( condition - .getAggregationExpression() ); - - if ( attributes != null && attributes.contains( attribute ) ) - { - return condition.getName(); - } - } - - return null; - } -} === removed file 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/DefaultCaseAggregationConditionService.java' --- dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/DefaultCaseAggregationConditionService.java 2016-01-04 02:27:49 +0000 +++ dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/DefaultCaseAggregationConditionService.java 1970-01-01 00:00:00 +0000 @@ -1,665 +0,0 @@ -package org.hisp.dhis.caseaggregation; - -/* - * Copyright (c) 2004-2016, 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.caseaggregation.CaseAggregationCondition.OBJECT_ORGUNIT_COMPLETE_PROGRAM_STAGE; -import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.OBJECT_PROGRAM; -import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.OBJECT_PROGRAM_STAGE; -import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.OBJECT_PROGRAM_STAGE_DATAELEMENT; -import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.OBJECT_TRACKED_ENTITY_ATTRIBUTE; -import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.PARAM_PERIOD_END_DATE; -import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.PARAM_PERIOD_START_DATE; -import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.PARAM_PERIOD_ID; -import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.PARAM_PERIOD_ISO_DATE; -import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.SEPARATOR_ID; -import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.SEPARATOR_OBJECT; -import static org.hisp.dhis.i18n.I18nUtils.i18n; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.List; -import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.concurrent.Future; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.hisp.dhis.common.Grid; -import org.hisp.dhis.dataelement.DataElement; -import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo; -import org.hisp.dhis.dataelement.DataElementCategoryService; -import org.hisp.dhis.dataelement.DataElementService; -import org.hisp.dhis.i18n.I18n; -import org.hisp.dhis.i18n.I18nFormat; -import org.hisp.dhis.i18n.I18nService; -import org.hisp.dhis.organisationunit.OrganisationUnit; -import org.hisp.dhis.period.Period; -import org.hisp.dhis.period.PeriodService; -import org.hisp.dhis.program.Program; -import org.hisp.dhis.program.ProgramService; -import org.hisp.dhis.program.ProgramStage; -import org.hisp.dhis.program.ProgramStageService; -import org.hisp.dhis.scheduling.TaskId; -import org.hisp.dhis.system.notification.Notifier; -import org.hisp.dhis.system.util.DateUtils; -import org.hisp.dhis.commons.util.ConcurrentUtils; -import org.hisp.dhis.system.util.SystemUtils; -import org.hisp.dhis.trackedentity.TrackedEntityAttribute; -import org.hisp.dhis.trackedentity.TrackedEntityAttributeService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.scheduling.annotation.Async; -import org.springframework.transaction.annotation.Transactional; - -/** - * @author Chau Thu Tran - */ -@Transactional -public class DefaultCaseAggregationConditionService - implements CaseAggregationConditionService -{ - private static final String INVALID_CONDITION = "Invalid condition"; - - private static final String IN_CONDITION_GET_ALL = "*"; - - // ------------------------------------------------------------------------- - // Dependencies - // ------------------------------------------------------------------------- - - private CaseAggregationConditionStore aggregationConditionStore; - - private DataElementService dataElementService; - - private ProgramStageService programStageService; - - private ProgramService programService; - - private TrackedEntityAttributeService attributeService; - - private PeriodService periodService; - - private I18nService i18nService; - - @Autowired - private DataElementCategoryService categoryService; - - @Autowired - private Notifier notifier; - - // ------------------------------------------------------------------------- - // Getters && Setters - // ------------------------------------------------------------------------- - - public void setAggregationConditionStore( CaseAggregationConditionStore aggregationConditionStore ) - { - this.aggregationConditionStore = aggregationConditionStore; - } - - public void setAttributeService( TrackedEntityAttributeService attributeService ) - { - this.attributeService = attributeService; - } - - public void setProgramService( ProgramService programService ) - { - this.programService = programService; - } - - public void setProgramStageService( ProgramStageService programStageService ) - { - this.programStageService = programStageService; - } - - public void setDataElementService( DataElementService dataElementService ) - { - this.dataElementService = dataElementService; - } - - public void setPeriodService( PeriodService periodService ) - { - this.periodService = periodService; - } - - public void setI18nService( I18nService service ) - { - i18nService = service; - } - - // ------------------------------------------------------------------------- - // Implementation Methods - // ------------------------------------------------------------------------- - - @Override - public int addCaseAggregationCondition( CaseAggregationCondition caseAggregationCondition ) - { - return aggregationConditionStore.save( caseAggregationCondition ); - } - - @Override - public void deleteCaseAggregationCondition( CaseAggregationCondition caseAggregationCondition ) - { - aggregationConditionStore.delete( caseAggregationCondition ); - } - - @Override - public Collection getAllCaseAggregationCondition() - { - return i18n( i18nService, aggregationConditionStore.getAll() ); - } - - @Override - public CaseAggregationCondition getCaseAggregationCondition( int id ) - { - return i18n( i18nService, aggregationConditionStore.get( id ) ); - } - - @Override - public CaseAggregationCondition getCaseAggregationCondition( String name ) - { - return i18n( i18nService, aggregationConditionStore.getByName( name ) ); - } - - @Override - public CaseAggregationCondition getCaseAggregationConditionByUid( String uid ) - { - return i18n( i18nService, aggregationConditionStore.getByUid( uid ) ); - } - - @Override - public void updateCaseAggregationCondition( CaseAggregationCondition caseAggregationCondition ) - { - aggregationConditionStore.update( caseAggregationCondition ); - } - - @Override - public Collection getCaseAggregationCondition( DataElement dataElement ) - { - return i18n( i18nService, aggregationConditionStore.get( dataElement ) ); - } - - @Override - public CaseAggregationCondition getCaseAggregationCondition( DataElement dataElement, - DataElementCategoryOptionCombo optionCombo ) - { - return i18n( i18nService, aggregationConditionStore.get( dataElement, optionCombo ) ); - } - - @Override - public String getConditionDescription( String condition ) - { - StringBuffer description = new StringBuffer(); - - Pattern patternCondition = Pattern.compile( CaseAggregationCondition.regExp ); - - Matcher matcher = patternCondition.matcher( condition ); - - while ( matcher.find() ) - { - String match = matcher.group(); - match = match.replaceAll( "[\\[\\]]", "" ); - - String[] info = match.split( SEPARATOR_OBJECT ); - - if ( info[0].equalsIgnoreCase( OBJECT_PROGRAM_STAGE_DATAELEMENT ) ) - { - String[] ids = info[1].split( SEPARATOR_ID ); - - int programId = Integer.parseInt( ids[0] ); - Program program = programService.getProgram( programId ); - - String programStageId = ids[1]; - String programStageName = ""; - if ( !programStageId.equals( IN_CONDITION_GET_ALL ) ) - { - ProgramStage ps = programStageService.getProgramStage( Integer.parseInt( programStageId ) ); - if ( ps != null ) - { - programStageName = ps.getName(); - } - } - else - { - programStageName = programStageId; - } - - int dataElementId = Integer.parseInt( ids[2] ); - DataElement dataElement = dataElementService.getDataElement( dataElementId ); - - if ( program == null || dataElement == null || programStageName.isEmpty() ) - { - return INVALID_CONDITION; - } - - matcher.appendReplacement( description, "[" + program.getDisplayName() + SEPARATOR_ID - + programStageName + SEPARATOR_ID + dataElement.getName() + "]" ); - } - else - { - String[] ids = info[1].split( SEPARATOR_ID ); - - if ( info[0].equalsIgnoreCase( OBJECT_TRACKED_ENTITY_ATTRIBUTE ) ) - { - int objectId = Integer.parseInt( ids[0] ); - - TrackedEntityAttribute attribute = attributeService.getTrackedEntityAttribute( objectId ); - - if ( attribute == null ) - { - return INVALID_CONDITION; - } - - matcher.appendReplacement( description, "[" + OBJECT_TRACKED_ENTITY_ATTRIBUTE + SEPARATOR_OBJECT - + attribute.getDisplayName() + "]" ); - } - else if ( info[0].equalsIgnoreCase( OBJECT_PROGRAM ) ) - { - int objectId = Integer.parseInt( ids[0] ); - - Program program = programService.getProgram( objectId ); - - if ( program == null ) - { - return INVALID_CONDITION; - } - - String programDes = OBJECT_PROGRAM + SEPARATOR_ID + program.getDisplayName(); - if ( ids.length == 2 ) - { - programDes += SEPARATOR_OBJECT + ids[1]; - } - matcher.appendReplacement( description, "[" + programDes + "]" ); - } - else if ( info[0].equalsIgnoreCase( OBJECT_PROGRAM_STAGE ) - || info[0].equalsIgnoreCase( OBJECT_ORGUNIT_COMPLETE_PROGRAM_STAGE ) ) - { - int objectId = Integer.parseInt( ids[0] ); - ProgramStage programStage = programStageService.getProgramStage( objectId ); - - if ( programStage == null ) - { - return INVALID_CONDITION; - } - - String count = (ids.length == 2) ? SEPARATOR_ID + ids[1] : ""; - matcher.appendReplacement( description, - "[" + info[0] + SEPARATOR_OBJECT + programStage.getDisplayName() + count + "]" ); - } - } - - } - - matcher.appendTail( description ); - - return description.toString(); - } - - @Override - public Collection getDataElementsInCondition( String aggregationExpression ) - { - String regExp = "\\[" + OBJECT_PROGRAM_STAGE_DATAELEMENT + SEPARATOR_OBJECT + "[0-9]+" + SEPARATOR_ID - + "[0-9]+" + SEPARATOR_ID + "[0-9]+" + "\\]"; - - Collection dataElements = new HashSet<>(); - - // --------------------------------------------------------------------- - // parse expressions - // --------------------------------------------------------------------- - - Pattern pattern = Pattern.compile( regExp ); - - Matcher matcher = pattern.matcher( aggregationExpression ); - - while ( matcher.find() ) - { - String match = matcher.group(); - match = match.replaceAll( "[\\[\\]]", "" ); - - String[] info = match.split( SEPARATOR_OBJECT ); - String[] ids = info[1].split( SEPARATOR_ID ); - - int dataElementId = Integer.parseInt( ids[2] ); - DataElement dataElement = dataElementService.getDataElement( dataElementId ); - - dataElements.add( dataElement ); - } - - return dataElements; - } - - @Override - public Collection getProgramsInCondition( String aggregationExpression ) - { - String regExp = "\\[(" + OBJECT_PROGRAM + "|" + OBJECT_PROGRAM_STAGE_DATAELEMENT + ")" + SEPARATOR_OBJECT - + "[a-zA-Z0-9\\- ]+"; - - Collection programs = new HashSet<>(); - - // --------------------------------------------------------------------- - // parse expressions - // --------------------------------------------------------------------- - - Pattern pattern = Pattern.compile( regExp ); - - Matcher matcher = pattern.matcher( aggregationExpression ); - - while ( matcher.find() ) - { - String match = matcher.group(); - match = match.replaceAll( "[\\[\\]]", "" ); - - String[] info = match.split( SEPARATOR_OBJECT ); - String[] ids = info[1].split( SEPARATOR_ID ); - - int programId = Integer.parseInt( ids[0] ); - Program program = programService.getProgram( programId ); - - programs.add( program ); - } - - return programs; - } - - @Override - public Collection getTrackedEntityAttributesInCondition( String aggregationExpression ) - { - String regExp = "\\[" + OBJECT_TRACKED_ENTITY_ATTRIBUTE + SEPARATOR_OBJECT + "[0-9]+\\]"; - - Collection attributes = new HashSet<>(); - - // --------------------------------------------------------------------- - // parse expressions - // --------------------------------------------------------------------- - - Pattern pattern = Pattern.compile( regExp ); - - Matcher matcher = pattern.matcher( aggregationExpression ); - - while ( matcher.find() ) - { - String match = matcher.group(); - match = match.replaceAll( "[\\[\\]]", "" ); - - String[] info = match.split( SEPARATOR_OBJECT ); - - int attributeId = Integer.parseInt( info[1] ); - TrackedEntityAttribute attribute = attributeService.getTrackedEntityAttribute( attributeId ); - - attributes.add( attribute ); - } - - return attributes; - } - - @Override - public Collection getCaseAggregationConditions( Collection dataElements, String key,Integer first, Integer max ) - { - return i18n( i18nService, aggregationConditionStore.get( dataElements, key, first, max ) ); - } - - @Override - public int countCaseAggregationCondition( Collection dataElements, String key ) - { - return aggregationConditionStore.count( dataElements, key ); - } - - @Override - public void aggregate( List caseAggregateSchedules, String taskStrategy, TaskId taskId ) - { - // Generate the periods - - for( CaseAggregateSchedule schedule : caseAggregateSchedules ){ - List periods = new ArrayList<>(aggregationConditionStore.getPeriods( schedule.getPeriodTypeName(), - taskStrategy )); - periodService.reloadPeriods( periods ); - } - - ConcurrentLinkedQueue queryBuilderQ = new ConcurrentLinkedQueue<>( - caseAggregateSchedules ); - List> futures = new ArrayList<>(); - - for ( int i = 0; i < getProcessNo(); i++ ) - { - Future future = aggregateValueManager( queryBuilderQ, taskStrategy, taskId ); - if ( future != null ) - { - futures.add( future ); - } - } - - ConcurrentUtils.waitForCompletion( futures ); - } - - @Override - public List getAggregateValue( Collection caseAggregationConditions, Collection orgunitIds, - Collection periods, I18nFormat format, I18n i18n ) - { - Collection _orgunitIds = aggregationConditionStore.getServiceOrgunit(); - _orgunitIds.retainAll( orgunitIds ); - if ( _orgunitIds.size() > 0 ) - { - int attributeOptioncomboId = categoryService.getDefaultDataElementCategoryOptionCombo().getId(); - List grids = new ArrayList<>(); - for ( CaseAggregationCondition condition : caseAggregationConditions ) - { - String sql = aggregationConditionStore.parseExpressionToSql( false, condition, attributeOptioncomboId, _orgunitIds ); - for ( Period period : periods ) - { - period = periodService.reloadPeriod( period ); - String periodSQL = sql; - periodSQL = replacePeriodSql( periodSQL, period ); - - Grid grid = aggregationConditionStore.getAggregateValue( periodSQL, format, i18n ); - grid.setTitle( condition.getDisplayName() ); - grid.setSubtitle( format.formatPeriod( period ) ); - - grids.add( grid ); - } - } - - return grids; - } - - return null; - } - - @Override - public Grid getAggregateValueDetails( CaseAggregationCondition aggregationCondition, OrganisationUnit orgunit, - Period period, I18nFormat format, I18n i18n ) - { - periodService.reloadPeriod( period ); - - boolean nonRegistrationProgram = singleEventWithoutRegistrationProgram( aggregationCondition.getAggregationExpression() ); - return aggregationConditionStore.getAggregateValueDetails( aggregationCondition, orgunit, period, nonRegistrationProgram, format, i18n ); - } - - @Override - public void insertAggregateValue( Collection caseAggregationConditions, - Collection orgunitIds, Collection periods ) - { - Collection _orgunitIds = aggregationConditionStore.getServiceOrgunit(); - _orgunitIds.retainAll( orgunitIds ); - if ( _orgunitIds.size() > 0 ) - { - DataElementCategoryOptionCombo attributeOptioncombo = categoryService.getDefaultDataElementCategoryOptionCombo(); - for( CaseAggregationCondition caseAggregationCondition : caseAggregationConditions ) - { - String sql = aggregationConditionStore.parseExpressionToSql(true, caseAggregationCondition, attributeOptioncombo.getId(), _orgunitIds ); - - for ( Period period : periods ) - { - period = periodService.reloadPeriod( period ); - sql = replacePeriodSql( sql, period ); - aggregationConditionStore.insertAggregateValue( sql, caseAggregationCondition.getAggregationDataElement(), - caseAggregationCondition.getOptionCombo(), attributeOptioncombo, _orgunitIds, period ); - } - } - } - } - - @Override - public String parseExpressionDetailsToSql( String caseExpression, String operator, Integer orgunitId, Period period ) - { - periodService.reloadPeriod( period ); - - boolean nonRegistrationProgram = singleEventWithoutRegistrationProgram( caseExpression ); - return aggregationConditionStore.parseExpressionDetailsToSql( caseExpression, operator, orgunitId, period, nonRegistrationProgram ); - } - - @Override - public String parseExpressionToSql( boolean isInsert, String caseExpression, String operator, - Integer aggregateDeId, String aggregateDeName, Integer optionComboId, String optionComboName, Integer deSumId, - Collection orgunitIds, Period period ) - { - periodService.reloadPeriod( period ); - - int attributeOptioncomboId = categoryService.getDefaultDataElementCategoryOptionCombo().getId(); - - String sql = aggregationConditionStore.parseExpressionToSql( isInsert, caseExpression, operator, aggregateDeId, - aggregateDeName, optionComboId, optionComboName, attributeOptioncomboId, deSumId, orgunitIds ); - - return replacePeriodSql( sql, period ); - } - - @Override - public List executeSQL( String sql ) - { - return aggregationConditionStore.executeSQL( sql ); - } - - // ------------------------------------------------------------------------- - // Support Methods - // ------------------------------------------------------------------------- - - private String replacePeriodSql( String sql, Period period ) - { - sql = sql.replaceAll( "COMBINE", "" ); - sql = sql.replaceAll( PARAM_PERIOD_START_DATE, DateUtils.getMediumDateString( period.getStartDate() )); - sql = sql.replaceAll( PARAM_PERIOD_END_DATE, DateUtils.getMediumDateString( period.getEndDate() )); - sql = sql.replaceAll( PARAM_PERIOD_ID, period.getId() + "" ); - sql = sql.replaceAll( PARAM_PERIOD_ISO_DATE, period.getIsoDate() ); - - return sql; - } - - @Async - private Future aggregateValueManager( ConcurrentLinkedQueue caseAggregateSchedule, - String taskStrategy, TaskId taskId ) - { - int attributeOptioncomboId = categoryService.getDefaultDataElementCategoryOptionCombo().getId(); - - taskLoop: while ( true ) - { - CaseAggregateSchedule condition = caseAggregateSchedule.poll(); - if ( condition == null ) - { - break taskLoop; - } - - Collection periods = aggregationConditionStore.getPeriods( condition.getPeriodTypeName(), - taskStrategy ); - if( taskId != null ) - { - notifier.notify( taskId, "Importing data for " + condition.getCaseAggregateName() ); - } - aggregationConditionStore.runAggregate( null, condition, periods, attributeOptioncomboId ); - } - - return null; - } - - private int getProcessNo() - { - return Math.max( (SystemUtils.getCpuCores() - 1), 1 ); - } - - public Integer calValue( Collection entityInstanceIds, String operator ) - { - return entityInstanceIds.size(); - } - - private boolean singleEventWithoutRegistrationProgram( String expression ) - { - Pattern patternCondition = Pattern.compile( CaseAggregationCondition.regExp ); - - Matcher matcher = patternCondition.matcher( expression ); - - while ( matcher.find() ) - { - String match = matcher.group(); - match = match.replaceAll( "[\\[\\]]", "" ); - - String[] info = match.split( SEPARATOR_OBJECT ); - - if ( info[0].equalsIgnoreCase( OBJECT_PROGRAM_STAGE_DATAELEMENT ) ) - { - String[] ids = info[1].split( SEPARATOR_ID ); - - int programId = Integer.parseInt( ids[0] ); - Program program = programService.getProgram( programId ); - - if ( program != null && !program.isRegistration() ) - { - return true; - } - } - else - { - String[] ids = info[1].split( SEPARATOR_ID ); - - if ( info[0].equalsIgnoreCase( OBJECT_TRACKED_ENTITY_ATTRIBUTE ) ) - { - return false; - } - else if ( info[0].equalsIgnoreCase( OBJECT_PROGRAM ) ) - { - int objectId = Integer.parseInt( ids[0] ); - - Program program = programService.getProgram( objectId ); - - if ( program != null && !program.isRegistration() ) - { - return true; - } - } - else if ( info[0].equalsIgnoreCase( OBJECT_PROGRAM_STAGE ) - || info[0].equalsIgnoreCase( OBJECT_ORGUNIT_COMPLETE_PROGRAM_STAGE ) ) - { - int objectId = Integer.parseInt( ids[0] ); - ProgramStage programStage = programStageService.getProgramStage( objectId ); - if (programStage!=null && !programStage.getProgram().isRegistration() ) - { - return true; - } - } - } - - } - - return false; - } -} === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/hibernate' === removed file 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/hibernate/HibernateCaseAggregationConditionStore.java' --- dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/hibernate/HibernateCaseAggregationConditionStore.java 2016-01-04 02:27:49 +0000 +++ dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/caseaggregation/hibernate/HibernateCaseAggregationConditionStore.java 1970-01-01 00:00:00 +0000 @@ -1,1424 +0,0 @@ -package org.hisp.dhis.caseaggregation.hibernate; - -/* - * Copyright (c) 2004-2016, 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.caseaggregation.CaseAggregationCondition.OBJECT_ORGUNIT_COMPLETE_PROGRAM_STAGE; -import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.OBJECT_PROGRAM; -import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.OBJECT_PROGRAM_PROPERTY; -import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.OBJECT_PROGRAM_STAGE; -import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.OBJECT_PROGRAM_STAGE_DATAELEMENT; -import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.OBJECT_PROGRAM_STAGE_PROPERTY; -import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.OBJECT_TRACKED_ENTITY_ATTRIBUTE; -import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.OBJECT_TRACKED_ENTITY_PROGRAM_STAGE_PROPERTY; -import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.PARAM_PERIOD_END_DATE; -import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.PARAM_PERIOD_ID; -import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.PARAM_PERIOD_ISO_DATE; -import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.PARAM_PERIOD_START_DATE; -import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.SEPARATOR_ID; -import static org.hisp.dhis.caseaggregation.CaseAggregationCondition.SEPARATOR_OBJECT; -import static org.hisp.dhis.scheduling.CaseAggregateConditionSchedulingManager.TASK_AGGREGATE_QUERY_BUILDER_LAST_12_MONTH; -import static org.hisp.dhis.scheduling.CaseAggregateConditionSchedulingManager.TASK_AGGREGATE_QUERY_BUILDER_LAST_3_MONTH; -import static org.hisp.dhis.scheduling.CaseAggregateConditionSchedulingManager.TASK_AGGREGATE_QUERY_BUILDER_LAST_6_MONTH; -import static org.hisp.dhis.scheduling.CaseAggregateConditionSchedulingManager.TASK_AGGREGATE_QUERY_BUILDER_LAST_MONTH; - -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Collection; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.hibernate.Criteria; -import org.hibernate.criterion.Order; -import org.hibernate.criterion.Projections; -import org.hibernate.criterion.Restrictions; -import org.hisp.dhis.caseaggregation.CaseAggregateSchedule; -import org.hisp.dhis.caseaggregation.CaseAggregationCondition; -import org.hisp.dhis.caseaggregation.CaseAggregationConditionStore; -import org.hisp.dhis.common.Grid; -import org.hisp.dhis.common.GridHeader; -import org.hisp.dhis.common.ValueType; -import org.hisp.dhis.common.hibernate.HibernateIdentifiableObjectStore; -import org.hisp.dhis.commons.util.TextUtils; -import org.hisp.dhis.dataelement.DataElement; -import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo; -import org.hisp.dhis.dataelement.DataElementService; -import org.hisp.dhis.datavalue.DataValue; -import org.hisp.dhis.datavalue.DataValueService; -import org.hisp.dhis.i18n.I18n; -import org.hisp.dhis.i18n.I18nFormat; -import org.hisp.dhis.jdbc.StatementBuilder; -import org.hisp.dhis.organisationunit.OrganisationUnit; -import org.hisp.dhis.organisationunit.OrganisationUnitService; -import org.hisp.dhis.period.CalendarPeriodType; -import org.hisp.dhis.period.Period; -import org.hisp.dhis.period.PeriodService; -import org.hisp.dhis.period.PeriodType; -import org.hisp.dhis.system.grid.ListGrid; -import org.hisp.dhis.system.util.DateUtils; -import org.hisp.dhis.trackedentity.TrackedEntityAttribute; -import org.hisp.dhis.trackedentity.TrackedEntityAttributeService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.core.RowMapper; -import org.springframework.jdbc.support.rowset.SqlRowSet; - -/** - * @author Chau Thu Tran - */ -public class HibernateCaseAggregationConditionStore - extends HibernateIdentifiableObjectStore - implements CaseAggregationConditionStore -{ - private static final String IS_NULL = "is null"; - - private static final String IN_CONDITION_GET_ALL = "*"; - - private static final String IN_CONDITION_START_SIGN = "@"; - - private static final String IN_CONDITION_END_SIGN = "#"; - - private static final String IN_CONDITION_COUNT_X_TIMES = "COUNT"; - - // ------------------------------------------------------------------------- - // Dependencies - // ------------------------------------------------------------------------- - - private JdbcTemplate jdbcTemplate; - - @Override - public void setJdbcTemplate( JdbcTemplate jdbcTemplate ) - { - this.jdbcTemplate = jdbcTemplate; - } - - private StatementBuilder statementBuilder; - - public void setStatementBuilder( StatementBuilder statementBuilder ) - { - this.statementBuilder = statementBuilder; - } - - private DataElementService dataElementService; - - public void setDataElementService( DataElementService dataElementService ) - { - this.dataElementService = dataElementService; - } - - @Autowired - DataValueService dataValueService; - - @Autowired - private PeriodService periodService; - - @Autowired - private OrganisationUnitService orgunitService; - - @Autowired - private TrackedEntityAttributeService attributeService; - - // ------------------------------------------------------------------------- - // Implementation Methods - // ------------------------------------------------------------------------- - - @SuppressWarnings( "unchecked" ) - @Override - public Collection get( DataElement dataElement ) - { - return getCriteria( Restrictions.eq( "aggregationDataElement", dataElement ) ).list(); - } - - @Override - public int count( Collection dataElements, String key ) - { - Criteria criteria = getCriteria(); - - if ( dataElements != null ) - { - criteria.add( Restrictions.in( "aggregationDataElement", dataElements ) ); - } - if ( key != null ) - { - criteria.add( Restrictions.ilike( "name", "%" + key + "%" ) ); - } - - Number rs = (Number) criteria.setProjection( Projections.rowCount() ).uniqueResult(); - return rs != null ? rs.intValue() : 0; - } - - @Override - public CaseAggregationCondition get( DataElement dataElement, DataElementCategoryOptionCombo optionCombo ) - { - return (CaseAggregationCondition) getCriteria( Restrictions.eq( "aggregationDataElement", dataElement ), - Restrictions.eq( "optionCombo", optionCombo ) ).uniqueResult(); - } - - @SuppressWarnings( "unchecked" ) - @Override - public Collection get( Collection dataElements, String key, Integer first, - Integer max ) - { - Criteria criteria = getCriteria(); - - if ( dataElements != null ) - { - criteria.add( Restrictions.in( "aggregationDataElement", dataElements ) ); - } - if ( key != null ) - { - criteria.add( Restrictions.ilike( "name", "%" + key + "%" ) ); - } - - if ( first != null && max != null ) - { - criteria.setFirstResult( first ); - criteria.setMaxResults( max ); - } - - criteria.addOrder( Order.desc( "name" ) ); - - return criteria.list(); - } - - @Override - public Grid getAggregateValue( String sql, I18nFormat format, I18n i18n ) - { - Grid grid = new ListGrid(); - - grid.addHeader( new GridHeader( i18n.getString( "dataelementid" ), true, true ) ); - grid.addHeader( new GridHeader( i18n.getString( "categoryoptioncomboid" ), true, true ) ); - grid.addHeader( new GridHeader( i18n.getString( "periodid" ), true, true ) ); - grid.addHeader( new GridHeader( i18n.getString( "organisationunitid" ), true, true ) ); - grid.addHeader( new GridHeader( i18n.getString( "storedby" ), true, true ) ); - grid.addHeader( new GridHeader( i18n.getString( "dataelementname" ), false, true ) ); - grid.addHeader( new GridHeader( i18n.getString( "categoryoptioncomboname" ), false, true ) ); - grid.addHeader( new GridHeader( i18n.getString( "organisationunitname" ), false, true ) ); - grid.addHeader( new GridHeader( i18n.getString( "value" ), false, true ) ); - - SqlRowSet rs = jdbcTemplate.queryForRowSet( sql ); - grid.addRows( rs ); - - return grid; - } - - @Override - public Grid getAggregateValueDetails( CaseAggregationCondition aggregationCondition, OrganisationUnit orgunit, - Period period, boolean nonRegistrationProgram, I18nFormat format, I18n i18n ) - { - Grid grid = new ListGrid(); - grid.setTitle( orgunit.getName() + " - " + aggregationCondition.getDisplayName() ); - grid.setSubtitle( format.formatPeriod( period ) ); - - String sql = parseExpressionDetailsToSql( aggregationCondition.getAggregationExpression(), - aggregationCondition.getOperator(), orgunit.getId(), period, nonRegistrationProgram ); - - SqlRowSet rs = jdbcTemplate.queryForRowSet( sql ); - - for ( String colName : rs.getMetaData().getColumnNames() ) - { - grid.addHeader( new GridHeader( i18n.getString( colName ), false, true ) ); - } - - addRows( rs, grid ); - - return grid; - } - - public void addRows( SqlRowSet rs, Grid grid ) - { - int cols = rs.getMetaData().getColumnCount(); - String idValue = ""; - int index = 1; - while ( rs.next() ) - { - grid.addRow(); - for ( int i = 1; i <= cols; i++ ) - { - Object value = rs.getObject( i ); - if ( i == 1 ) - { - if ( !value.toString().equals( idValue ) ) - { - grid.addValue( index ); - idValue = value.toString(); - index++; - } - else - { - grid.addValue( "" ); - } - } - else - { - grid.addValue( value ); - } - } - } - } - - @Override - public void insertAggregateValue( String sql, DataElement dataElement, DataElementCategoryOptionCombo optionCombo, - DataElementCategoryOptionCombo attributeOptionCombo, Collection orgunitIds, Period period ) - { - try - { - SqlRowSet row = jdbcTemplate.queryForRowSet( sql ); - while ( row.next() ) - { - int value = row.getInt( "value" ); - OrganisationUnit source = orgunitService.getOrganisationUnit( row.getInt( "sourceid" ) ); - - DataValue dataValue = dataValueService.getDataValue( dataElement, period, source, optionCombo ); - - if ( dataValue == null ) - { - dataValue = new DataValue( dataElement, period, source, optionCombo, attributeOptionCombo ); - dataValue.setValue( value + "" ); - dataValue.setStoredBy( row.getString( "storedby" ) ); - dataValueService.addDataValue( dataValue ); - } - else if ( dataValue != null && value == 0 && !dataElement.isZeroIsSignificant() ) - { - dataValueService.deleteDataValue( dataValue ); - } - else if ( dataValue != null ) - { - dataValue.setValue( value + "" ); - dataValueService.updateDataValue( dataValue ); - } - } - } - catch ( Exception ex ) - { - ex.printStackTrace(); - } - } - - @Override - public void insertAggregateValue( String sql, int dataElementId, int optionComboId, Collection orgunitIds, - Period period ) - { - try - { - final String deleteDataValueSql = "delete from datavalue where dataelementid=" + dataElementId - + " and categoryoptioncomboid=" + optionComboId + " and sourceid in (" - + TextUtils.getCommaDelimitedString( orgunitIds ) + ") " + "and periodid = " + period.getId(); - jdbcTemplate.update( deleteDataValueSql ); - - jdbcTemplate.update( sql ); - - } - catch ( Exception ex ) - { - ex.printStackTrace(); - } - } - - @Override - public String parseExpressionToSql( boolean isInsert, CaseAggregationCondition aggregationCondition, - int attributeOptionComboId, Collection orgunitIds ) - { - String caseExpression = aggregationCondition.getAggregationExpression(); - String operator = aggregationCondition.getOperator(); - Integer aggregateDeId = aggregationCondition.getAggregationDataElement().getId(); - String aggregateDeName = aggregationCondition.getAggregationDataElement().getName(); - Integer optionComboId = aggregationCondition.getOptionCombo().getId(); - String optionComboName = aggregationCondition.getOptionCombo().getName(); - Integer deSumId = (aggregationCondition.getDeSum() == null) ? null : aggregationCondition.getDeSum().getId(); - - return parseExpressionToSql( isInsert, caseExpression, operator, aggregateDeId, aggregateDeName, optionComboId, - optionComboName, attributeOptionComboId, deSumId, orgunitIds ); - } - - @Override - public String parseExpressionToSql( boolean isInsert, String caseExpression, String operator, Integer aggregateDeId, - String aggregateDeName, Integer optionComboId, String optionComboName, int attributeOptioncomboId, Integer deSumId, - Collection orgunitIds ) - { - String select = "SELECT '" + aggregateDeId + "' as dataelementid, '" + optionComboId - + "' as categoryoptioncomboid, '" + attributeOptioncomboId - + "' as attributeoptioncomboid, ou.organisationunitid as sourceid, '" + PARAM_PERIOD_ID + "' as periodid,'" - + CaseAggregationCondition.AUTO_STORED_BY + "' as storedby, "; - - if ( isInsert ) - { - select = "INSERT INTO datavalue (dataelementid, categoryoptioncomboid, attributeoptioncomboid, sourceid, periodid, storedby, lastupdated, followup, created, value) " - + select + " now(), false, now(), "; - } - else - { - select += "'" + PARAM_PERIOD_ISO_DATE + "' as periodIsoDate,'" + aggregateDeName + "' as dataelementname, '" - + optionComboName + "' as categoryoptioncomboname, ou.name as organisationunitname, "; - } - - String sql = select + " ( select "; - if ( operator.equals( CaseAggregationCondition.AGGRERATION_COUNT ) - || operator.equals( CaseAggregationCondition.AGGRERATION_SUM ) ) - { - if ( hasOrgunitProgramStageCompleted( caseExpression ) ) - { - sql += createSQL( caseExpression, operator, orgunitIds ); - } - else - { - if ( operator.equals( CaseAggregationCondition.AGGRERATION_COUNT ) ) - { - sql += " count(distinct(pi.trackedentityinstanceid)) as value "; - } - else - { - sql += " count(psi.programinstanceid) as value "; - } - - sql += "FROM "; - boolean hasDataelement = hasDataelementCriteria( caseExpression ); - boolean hasEntityInstance = hasEntityInstanceCriteria( caseExpression ); - - if ( hasEntityInstance && hasDataelement ) - { - sql += " programinstance as pi "; - sql += " INNER JOIN trackedentityinstance p on p.trackedentityinstanceid=pi.trackedentityinstanceid "; - sql += " INNER JOIN programstageinstance psi ON pi.programinstanceid=psi.programinstanceid "; - } - else if ( hasEntityInstance ) - { - sql += " programinstance as pi INNER JOIN trackedentityinstance p on p.trackedentityinstanceid=pi.trackedentityinstanceid "; - } - else - { - sql += " programinstance as pi "; - sql += " INNER JOIN programstageinstance psi ON pi.programinstanceid=psi.programinstanceid "; - } - - sql += " WHERE " + createSQL( caseExpression, operator, orgunitIds ); - - sql += "GROUP BY ou.organisationunitid ) from organisationunit ou where ou.organisationunitid in ( " + TextUtils.getCommaDelimitedString( orgunitIds ) + " ) "; - } - } - else - { - sql += " " + operator + "( cast( pdv.value as " + statementBuilder.getDoubleColumnType() + " ) ) "; - sql += "FROM trackedentitydatavalue pdv "; - sql += " INNER JOIN programstageinstance psi "; - sql += " ON psi.programstageinstanceid = pdv.programstageinstanceid "; - sql += "WHERE executiondate >='" + PARAM_PERIOD_START_DATE + "' "; - sql += " AND executiondate <='" + PARAM_PERIOD_END_DATE + "' AND pdv.dataelementid=" + deSumId; - - if ( caseExpression != null && !caseExpression.isEmpty() ) - { - sql += " AND " + createSQL( caseExpression, operator, orgunitIds ); - } - - sql += "GROUP BY ou.organisationunitid ) from organisationunit ou where ou.organisationunitid in ( " + TextUtils.getCommaDelimitedString( orgunitIds ) + " ) "; - - } - - return sql; - } - - @Override - public void runAggregate( Collection orgunitIds, CaseAggregateSchedule condition, Collection periods, int attributeOptioncomboId ) - { - Collection _orgunitIds = getServiceOrgunit(); - - if ( orgunitIds == null ) - { - orgunitIds = new HashSet<>(); - orgunitIds.addAll( _orgunitIds ); - } - else - { - orgunitIds.retainAll( _orgunitIds ); - } - - if ( !orgunitIds.isEmpty() ) - { - String sql = "select caseaggregationconditionid, aggregationdataelementid, optioncomboid, " - + "aggregationexpression as caseexpression, operator as caseoperator, desum as desumid " - + "from caseaggregationcondition where caseaggregationconditionid = " + condition.getCaseAggregateId(); - - SqlRowSet rs = jdbcTemplate.queryForRowSet( sql ); - - if ( rs.next() ) - { - String caseExpression = rs.getString( "caseexpression" ); - int dataElementId = rs.getInt( "aggregationdataelementid" ); - int optionComboId = rs.getInt( "optioncomboid" ); - String caseOperator = rs.getString( "caseoperator" ); - int deSumId = rs.getInt( "desumid" ); - String insertParamsSql = parseExpressionToSql( true, caseExpression, caseOperator, dataElementId, "de_name", optionComboId, "optioncombo_name", attributeOptioncomboId, deSumId, _orgunitIds ); - - for ( Period period : periods ) - { - String insertSql = replacePeriodSql( insertParamsSql, period ); - insertAggregateValue( insertSql, dataElementId, optionComboId, _orgunitIds, period ); - } - - } - } - } - - /** - * Return standard SQL from query builder formula - * - * @param caseExpression The query builder expression - * @param operator There are six operators, includes Number of persons, - * Number of visits, Sum, Average, Minimum and Maximum of data - * element values. - * @param deType Aggregate Data element type - * @param orgunitIds The ids of organisation units where to aggregate data - * value - */ - private String createSQL( String caseExpression, String operator, Collection orgunitIds ) - { - caseExpression = caseExpression.replaceAll( "\"", "'" ); - boolean orgunitCompletedProgramStage = false; - - StringBuffer sqlResult = new StringBuffer(); - - String sqlOrgunitCompleted = ""; - - // Date dataElement - dateOfIncident/executionDate/enrollmentDate - - Map minusDe1SQLMap = new HashMap<>(); - int idxDe1 = 0; - Pattern patternMinus = Pattern.compile( CaseAggregationCondition.minusDataelementRegExp1 ); - Matcher matcherMinus = patternMinus.matcher( caseExpression ); - while ( matcherMinus.find() ) - { - String programIdStr = matcherMinus.group( 1 ); - String programStageIdStr = matcherMinus.group( 2 ); - String dataElementId = matcherMinus.group( 3 ); - String dateProperty = matcherMinus.group( 5 ); - String compareSide = matcherMinus.group( 6 ) + matcherMinus.group( 7 ); - - Integer programId = null; - Integer programStageId = null; - if ( !programIdStr.equals( IN_CONDITION_GET_ALL ) ) - { - programId = Integer.parseInt( programIdStr ); - } - - if ( !programStageIdStr.equals( IN_CONDITION_GET_ALL ) ) - { - programStageId = Integer.parseInt( programStageIdStr ); - } - - minusDe1SQLMap.put( - idxDe1, - getConditionForMinusDataElement1( orgunitIds, programId, programStageId, Integer.parseInt( dataElementId ), - dateProperty, compareSide ) ); - - caseExpression = caseExpression.replace( matcherMinus.group( 0 ), CaseAggregationCondition.MINUS_DATAELEMENT_OPERATOR_TYPE_ONE - + "_" + idxDe1 ); - - idxDe1++; - } - - // dateOfIncident/executionDate/enrollmentDate - Date dataElement - - Map minusDe2SQLMap = new HashMap<>(); - int idxDe2 = 0; - Pattern patternDE1Map = Pattern.compile( CaseAggregationCondition.minusDataelementRegExp2 ); - Matcher matcherMinusDE1 = patternDE1Map.matcher( caseExpression ); - while ( matcherMinusDE1.find() ) - { - String dateProperty = matcherMinusDE1.group( 1 ); - String programIdStr = matcherMinusDE1.group( 3 ); - String programStageIdStr = matcherMinusDE1.group( 4 ); - String dataElementId = matcherMinusDE1.group( 5 ); - String compareSide = matcherMinusDE1.group( 6 ) + matcherMinusDE1.group( 7 ); - - Integer programId = null; - Integer programStageId = null; - if ( !programIdStr.equals( IN_CONDITION_GET_ALL ) ) - { - programId = Integer.parseInt( programIdStr ); - } - - if ( !programStageIdStr.equals( IN_CONDITION_GET_ALL ) ) - { - programStageId = Integer.parseInt( programStageIdStr ); - } - - minusDe2SQLMap.put( - idxDe2, - getConditionForMinusDataElement2( orgunitIds, programId, programStageId, Integer.parseInt( dataElementId ), - dateProperty, compareSide ) ); - - caseExpression = caseExpression.replace( matcherMinusDE1.group( 0 ), - CaseAggregationCondition.MINUS_DATAELEMENT_OPERATOR_TYPE_TWO + "_" + idxDe2 ); - - idxDe2++; - } - - // Date dataElement - Date dataElement - - Map minus2DeSQLMap = new HashMap<>(); - int idx2De = 0; - Pattern patternMinus2 = Pattern.compile( CaseAggregationCondition.minus2DataelementRegExp ); - Matcher matcherMinus2 = patternMinus2.matcher( caseExpression ); - - while ( matcherMinus2.find() ) - { - String[] ids1 = matcherMinus2.group( 2 ).split( SEPARATOR_ID ); - String[] ids2 = matcherMinus2.group( 5 ).split( SEPARATOR_ID ); - - minus2DeSQLMap.put( - idx2De, - getConditionForMisus2DataElement( orgunitIds, ids1[1], ids1[2], ids2[1], ids2[2], - matcherMinus2.group( 6 ) + matcherMinus2.group( 7 ) ) ); - caseExpression = caseExpression.replace( matcherMinus2.group( 0 ), - CaseAggregationCondition.MINUS_2DATAELEMENT_OPERATOR + "_" + idx2De ); - - idx2De++; - } - - // currentDate/ dateOfIncident/executionDate/enrollmentDate - Date attribute - - Map minusAttr1SQLMap = new HashMap<>(); - int idxAttr1 = 0; - Pattern patternMinus3 = Pattern.compile( CaseAggregationCondition.minusAttributeRegExp1 ); - Matcher matcherMinus3 = patternMinus3.matcher( caseExpression ); - while ( matcherMinus3.find() ) - { - String property = matcherMinus3.group( 1 ); - String attributeId = matcherMinus3.group( 3 ); - String compareSide = matcherMinus3.group( 4 ) + matcherMinus3.group( 5 ); - minusAttr1SQLMap.put( - idxAttr1, - getConditionForMisusAttribute1( attributeId, property, compareSide ) ); - - caseExpression = caseExpression.replace( matcherMinus3.group( 0 ), - CaseAggregationCondition.MINUS_ATTRIBUTE_OPERATOR_TYPE_ONE + "_" + idxAttr1 ); - - idxAttr1++; - } - - - // Date attribute - currentDate/ dateOfIncident/executionDate/enrollmentDate - - Map minusAttr2SQLMap = new HashMap<>(); - int idxAttr2 = 0; - Pattern patternAttr2Minus = Pattern.compile( CaseAggregationCondition.minusAttributeRegExp2 ); - - Matcher matcherAttr2Minus = patternAttr2Minus.matcher( caseExpression ); - while ( matcherAttr2Minus.find() ) - { - minusAttr2SQLMap.put( - idxAttr2, - getConditionForMisusAttribute2( matcherAttr2Minus.group( 1 ), matcherAttr2Minus.group( 3 ), matcherAttr2Minus.group( 4 ) + matcherAttr2Minus.group( 5 ) ) ); - - caseExpression = caseExpression.replace( matcherAttr2Minus.group( 0 ), - CaseAggregationCondition.MINUS_ATTRIBUTE_OPERATOR_TYPE_TWO + "_" + idxAttr2 ); - - idxAttr2++; - } - - - // Date attribute - Date attribute - - Map minus2AttrSQLMap = new HashMap<>(); - int idx2Attr = 0; - Pattern patternAttrMinus2 = Pattern.compile( CaseAggregationCondition.minus2AttributeRegExp ); - Matcher matcherAttrMinus2 = patternAttrMinus2.matcher( caseExpression ); - while ( matcherAttrMinus2.find() ) - { - String attribute1 = matcherAttrMinus2.group( 2 ); - String attribute2 = matcherAttrMinus2.group( 5 ); - String compareSide = matcherAttrMinus2.group( 6 ) + matcherAttrMinus2.group( 7 ); - minus2AttrSQLMap.put( idx2Attr, getConditionForMisus2Attribute( attribute1, attribute2, compareSide ) ); - - caseExpression = caseExpression.replace( matcherAttrMinus2.group( 0 ), - CaseAggregationCondition.MINUS_2ATTRIBUTE_OPERATOR + "_" + idx2Attr ); - - idx2Attr++; - } - - - // Run nornal expression - String[] expression = caseExpression.split( "(AND|OR)" ); - caseExpression = caseExpression.replaceAll( "AND", " ) AND " ); - caseExpression = caseExpression.replaceAll( "OR", " ) OR " ); - - // --------------------------------------------------------------------- - // parse expressions - // --------------------------------------------------------------------- - - Pattern patternCondition = Pattern.compile( CaseAggregationCondition.regExp ); - - Matcher matcherCondition = patternCondition.matcher( caseExpression ); - - String condition = ""; - - int index = 0; - while ( matcherCondition.find() ) - { - String match = matcherCondition.group(); - - match = match.replaceAll( "[\\[\\]]", "" ); - - String[] info = match.split( SEPARATOR_OBJECT ); - if ( info[0].equalsIgnoreCase( OBJECT_TRACKED_ENTITY_ATTRIBUTE ) ) - { - String attributeId = info[1]; - - String compareValue = expression[index].replace( "[" + match + "]", "" ).trim(); - - boolean isExist = compareValue.equals( IS_NULL ) ? false : true; - condition = getConditionForTrackedEntityAttribute( attributeId, orgunitIds, isExist ); - } - else if ( info[0].equalsIgnoreCase( OBJECT_PROGRAM_STAGE_DATAELEMENT ) ) - { - String[] ids = info[1].split( SEPARATOR_ID ); - - int programId = Integer.parseInt( ids[0] ); - String programStageId = ids[1]; - int dataElementId = Integer.parseInt( ids[2] ); - - String compareValue = expression[index].replace( "[" + match + "]", "" ).trim(); - - boolean isExist = compareValue.equals( IS_NULL ) ? false : true; - condition = getConditionForDataElement( isExist, programId, programStageId, dataElementId, orgunitIds ); - } - - else if ( info[0].equalsIgnoreCase( OBJECT_PROGRAM_PROPERTY ) ) - { - condition = getConditionForProgramProperty( operator, info[1] ); - } - else if ( info[0].equalsIgnoreCase( OBJECT_PROGRAM ) ) - { - String[] ids = info[1].split( SEPARATOR_ID ); - condition = getConditionForProgram( ids[0], operator, orgunitIds ); - if ( ids.length > 1 ) - { - condition += ids[1]; - } - } - else if ( info[0].equalsIgnoreCase( OBJECT_PROGRAM_STAGE ) ) - { - String[] ids = info[1].split( SEPARATOR_ID ); - if ( ids.length == 2 && ids[1].equals( IN_CONDITION_COUNT_X_TIMES ) ) - { - condition = getConditionForCountProgramStage( ids[0], operator, orgunitIds ); - } - else - { - condition = getConditionForProgramStage( ids[0], orgunitIds ); - } - } - else if ( info[0].equalsIgnoreCase( OBJECT_PROGRAM_STAGE_PROPERTY ) ) - { - condition = getConditionForProgramStageProperty( info[1], operator, orgunitIds ); - } - else if ( info[0].equalsIgnoreCase( OBJECT_TRACKED_ENTITY_PROGRAM_STAGE_PROPERTY ) ) - { - condition = getConditionForTrackedEntityProgramStageProperty( info[1], operator ); - } - else if ( info[0].equalsIgnoreCase( OBJECT_ORGUNIT_COMPLETE_PROGRAM_STAGE ) ) - { - sqlOrgunitCompleted += getConditionForOrgunitProgramStageCompleted( info[1], operator, orgunitIds, - orgunitCompletedProgramStage ); - orgunitCompletedProgramStage = true; - } - - matcherCondition.appendReplacement( sqlResult, condition ); - - index++; - } - - matcherCondition.appendTail( sqlResult ); - - if ( !sqlOrgunitCompleted.isEmpty() ) - { - sqlOrgunitCompleted = sqlOrgunitCompleted.substring( 0, sqlOrgunitCompleted.length() - 2 ); - } - - sqlResult.append( sqlOrgunitCompleted ); - - String sql = sqlResult.toString(); - - sql = sql.replaceAll( IN_CONDITION_START_SIGN, "(" ); - sql = sql.replaceAll( IN_CONDITION_END_SIGN, ")" ); - sql = sql.replaceAll( IS_NULL, " " ); - for ( int i = 0; i < idxDe1; i++ ) - { - sql = sql.replace( CaseAggregationCondition.MINUS_DATAELEMENT_OPERATOR_TYPE_ONE + "_" + i, minusDe1SQLMap.get( i ) ); - } - - - for ( int i = 0; i < idxDe2; i++ ) - { - sql = sql.replace( CaseAggregationCondition.MINUS_DATAELEMENT_OPERATOR_TYPE_TWO + "_" + i, - minusDe2SQLMap.get( i ) ); - } - - for ( int i = 0; i < idx2De; i++ ) - { - sql = sql - .replace( CaseAggregationCondition.MINUS_2DATAELEMENT_OPERATOR + "_" + i, minus2DeSQLMap.get( i ) ); - } - - for ( int i = 0; i < idxAttr1; i++ ) - { - sql = sql - .replace( CaseAggregationCondition.MINUS_ATTRIBUTE_OPERATOR_TYPE_ONE + "_" + i, minusAttr1SQLMap.get( i ) ); - } - - for ( int i = 0; i < idxAttr2; i++ ) - { - sql = sql - .replace( CaseAggregationCondition.MINUS_ATTRIBUTE_OPERATOR_TYPE_TWO + "_" + i, minusAttr2SQLMap.get( i ) ); - } - - for ( int i = 0; i < idx2Attr; i++ ) - { - sql = sql - .replace( CaseAggregationCondition.MINUS_2ATTRIBUTE_OPERATOR + "_" + i, minus2AttrSQLMap.get( i ) ); - } - - sql = sql.replaceAll( CaseAggregationCondition.CURRENT_DATE, "now()" ); - - sql += " ) "; - if ( hasDataelementCriteria( caseExpression ) ) - { - sql += " and psi.organisationunitid=ou.organisationunitid "; - } - else - { - sql += " and pi.organisationunitid=ou.organisationunitid "; - } - - return sql; - } - - /** - * Return standard SQL of the expression to compare data value as null - */ - private String getConditionForDataElement( boolean isExist, int programId, String programStageId, - int dataElementId, Collection orgunitIds ) - { - String keyExist = isExist ? "EXISTS" : "NOT EXISTS"; - - String sql = " " + keyExist + " ( SELECT * " - + "FROM trackedentitydatavalue _pdv inner join programstageinstance _psi " - + "ON _pdv.programstageinstanceid=_psi.programstageinstanceid JOIN programinstance _pi " - + "ON _pi.programinstanceid=_psi.programinstanceid " - + "WHERE psi.programstageinstanceid=_pdv.programstageinstanceid AND _pdv.dataelementid=" + dataElementId - + " AND _psi.organisationunitid in (" + TextUtils.getCommaDelimitedString( orgunitIds ) + ") " - + " AND _pi.programid = " + programId + " AND _psi.executionDate>='" + PARAM_PERIOD_START_DATE - + "' AND _psi.executionDate <= '" + PARAM_PERIOD_END_DATE + "' "; - - if ( !programStageId.equals( IN_CONDITION_GET_ALL ) ) - { - sql += " AND _psi.programstageid = " + programStageId; - } - - if ( isExist ) - { - ValueType valueType = dataElementService.getDataElement( dataElementId ).getValueType(); - - if ( valueType.isNumeric() ) - { - sql += " AND ( cast( _pdv.value as " + statementBuilder.getDoubleColumnType() + " ) ) "; - } - else - { - sql += " AND _pdv.value "; - } - } - - if ( !isExist ) - { - sql = "(" + sql + " ) AND " + getConditionForProgramStage( programStageId, orgunitIds ) + ")"; - } - - return sql; - } - - /** - * Return standard SQL of a dynamic tracked-entity-attribute expression. E.g - * [CA:1] OR [CA:1.age] - */ - private String getConditionForTrackedEntityAttribute( String attributeId, Collection orgunitIds, - boolean isExist ) - { - String sql = " SELECT * FROM trackedentityattributevalue _pav "; - - if ( attributeId.split( SEPARATOR_ID ).length == 2 ) - { - if ( attributeId.split( SEPARATOR_ID )[1].equals( CaseAggregationCondition.FORMULA_VISIT ) ) - { - sql += " inner join programinstance _pi on _pav.trackedentityinstanceid=_pi.trackedentityinstanceid "; - sql += " inner join programstageinstance _psi on _pi.programinstanceid=_psi.programinstanceid "; - - attributeId = attributeId.split( SEPARATOR_ID )[0]; - sql += " WHERE _pav.trackedentityinstanceid=pi.trackedentityinstanceid AND _pav.trackedentityattributeid=" - + attributeId + " AND DATE(_psi.executiondate) - DATE( _pav.value ) "; - } - else if ( attributeId.split( SEPARATOR_ID )[1].equals( CaseAggregationCondition.FORMULA_AGE ) ) - { - sql += " inner join programinstance _pi on _pav.trackedentityinstanceid=_pi.trackedentityinstanceid "; - - attributeId = attributeId.split( SEPARATOR_ID )[0]; - sql += " WHERE _pav.trackedentityinstanceid=pi.trackedentityinstanceid AND _pav.trackedentityattributeid=" - + attributeId + " AND DATE(_psi.enrollmentdate) - DATE( _pav.value ) "; - } - } - else - { - sql += " WHERE _pav.trackedentityinstanceid=pi.trackedentityinstanceid AND _pav.trackedentityattributeid=" - + attributeId; - - if ( isExist ) - { - TrackedEntityAttribute attribute = attributeService.getTrackedEntityAttribute( Integer.parseInt( attributeId ) ); - - if ( ValueType.NUMBER == attribute.getValueType() ) - { - sql += " AND cast( _pav.value as " + statementBuilder.getDoubleColumnType() + " ) "; - } - else - { - sql += " AND _pav.value "; - } - } - } - - if ( isExist ) - { - sql = " EXISTS ( " + sql; - } - else - { - sql = " NOT EXISTS ( " + sql; - } - - return sql; - } - - /** - * Return standard SQL of the program-property expression. E.g - * [PC:executionDate] - */ - private String getConditionForTrackedEntityProgramStageProperty( String propertyName, String operator ) - { - String sql = " EXISTS ( SELECT _psi.programstageinstanceid from programstageinstance _psi " - + "WHERE _psi.programstageinstanceid=psi.programstageinstanceid AND ( _psi.executionDate BETWEEN '" - + PARAM_PERIOD_START_DATE + "' AND '" + PARAM_PERIOD_END_DATE + "') AND " + propertyName; - - return sql; - } - - /** - * Return standard SQL of the program expression. E.g - * [PP:DATE@enrollmentdate#-DATE@dateofincident#] for geting the number of - * days between date of enrollment and date of incident. - */ - private String getConditionForProgramProperty( String operator, String property ) - { - String sql = " EXISTS ( SELECT _pi.programinstanceid FROM programinstance as _pi WHERE _pi.programinstanceid=pi.programinstanceid AND " - + "pi.enrollmentdate >= '" - + PARAM_PERIOD_START_DATE - + "' AND pi.enrollmentdate <= '" - + PARAM_PERIOD_END_DATE + "' AND " + property + " "; - - return sql; - } - - /** - * Return standard SQL to retrieve the number of persons enrolled into the - * program. E.g [PG:1] - */ - private String getConditionForProgram( String programId, String operator, Collection orgunitIds ) - { - String sql = " EXISTS ( SELECT * FROM programinstance as _pi inner join trackedentityinstance _p on _p.trackedentityinstanceid=_pi.trackedentityinstanceid " - + "WHERE _pi.trackedentityinstanceid=pi.trackedentityinstanceid AND _pi.programid=" - + programId - + " AND _pi.organisationunitid in (" - + TextUtils.getCommaDelimitedString( orgunitIds ) - + ") AND _pi.enrollmentdate >= '" - + PARAM_PERIOD_START_DATE - + "' AND _pi.enrollmentdate <= '" - + PARAM_PERIOD_END_DATE + "' "; - - return sql; - } - - /** - * Return standard SQL to retrieve the number of visits a program-stage. E.g - * [PS:1] - */ - private String getConditionForProgramStage( String programStageId, Collection orgunitIds ) - { - String sql = " EXISTS ( SELECT _psi.programstageinstanceid FROM programstageinstance _psi " - + "WHERE _psi.programstageinstanceid=psi.programstageinstanceid "; - if ( !programStageId.equals( IN_CONDITION_GET_ALL ) ) - { - sql += "AND _psi.programstageid=" + programStageId; - } - - sql += " AND _psi.executiondate >= '" + PARAM_PERIOD_START_DATE - + "' AND _psi.executiondate <= '" + PARAM_PERIOD_END_DATE + "' AND _psi.organisationunitid in (" - + TextUtils.getCommaDelimitedString( orgunitIds ) + ") "; - - return sql; - } - - /** - * Return standard SQL to retrieve the x-time of a person visited one - * program-stage. E.g a mother came to a hospital 3th time for third - * trimester. - */ - private String getConditionForCountProgramStage( String programStageId, String operator, - Collection orgunitIds ) - { - String sql = " EXISTS ( SELECT _psi.programstageinstanceid FROM programstageinstance as _psi " - + "WHERE psi.programstageinstanceid=_psi.programstageinstanceid AND _psi.organisationunitid in (" - + TextUtils.getCommaDelimitedString( orgunitIds ) + ") and _psi.programstageid = " + programStageId + " " - + "AND _psi.executionDate >= '" + PARAM_PERIOD_START_DATE + "' AND _psi.executionDate <= '" - + PARAM_PERIOD_END_DATE + "' " + "GROUP BY _psi.programinstanceid,_psi.programstageinstanceid " - + "HAVING count(_psi.programstageinstanceid) "; - - return sql; - - } - - /** - * Return standard SQL to retrieve the number of days between report-date - * and due-date. E.g [PSP:DATE@executionDate#-DATE@dueDate#] - */ - private String getConditionForProgramStageProperty( String property, String operator, Collection orgunitIds ) - { - String sql = " EXISTS ( SELECT * FROM programstageinstance _psi " - + "WHERE psi.programstageinstanceid=_psi.programstageinstanceid AND _psi.executiondate >= '" - + PARAM_PERIOD_START_DATE + "' AND _psi.executiondate <= '" + PARAM_PERIOD_END_DATE - + "' AND _psi.organisationunitid in (" + TextUtils.getCommaDelimitedString( orgunitIds ) + ") AND " - + property + " "; - - return sql; - } - - /** - * Return standard SQL to retrieve the number of children orgunits has all - * program-stage-instance completed and due-date. E.g [PSIC:1] - * - * @flag True if there are many stages in the expression - */ - private String getConditionForOrgunitProgramStageCompleted( String programStageId, String operator, - Collection orgunitIds, boolean flag ) - { - String sql = ""; - if ( !flag ) - { - sql = " '1' FROM organisationunit ou WHERE ou.organisationunitid in (" - + TextUtils.getCommaDelimitedString( orgunitIds ) + ") "; - } - - sql += " AND EXISTS ( SELECT programstageinstanceid FROM programstageinstance _psi " - + " WHERE _psi.organisationunitid=ou.organisationunitid AND _psi.programstageid = " + programStageId - + " AND _psi.completed=true AND _psi.executiondate >= '" + PARAM_PERIOD_START_DATE - + "' AND _psi.executiondate <= '" + PARAM_PERIOD_END_DATE + "' ) "; - - return sql; - } - - private String getConditionForMinusDataElement1( Collection orgunitIds, Integer programId, Integer programStageId, - Integer dataElementId, String dateProperty, String compareSide ) - { - String sql = " EXISTS ( SELECT _pdv.value FROM trackedentitydatavalue _pdv inner join programstageinstance _psi " - + " ON _pdv.programstageinstanceid=_psi.programstageinstanceid " - + " JOIN programinstance _pi ON _pi.programinstanceid=_psi.programinstanceid " - + " WHERE psi.programstageinstanceid=_pdv.programstageinstanceid " - + " AND _pdv.dataelementid=" + dataElementId - + " AND _psi.organisationunitid in (" + TextUtils.getCommaDelimitedString( orgunitIds ) - + ") "; - - - if ( programId != null ) - { - sql += " AND _pi.programid = " + programId; - } - - if ( programId != null ) - { - sql += " AND _psi.programstageid = " + programStageId; - } - - sql += " AND ( _psi.executionDate BETWEEN '" + PARAM_PERIOD_START_DATE + "' AND '" + PARAM_PERIOD_END_DATE - + "') " + " AND ( DATE(_pdv.value) - DATE(" + dateProperty + ") " + compareSide + " ) "; - - return sql; - } - - private String getConditionForMinusDataElement2( Collection orgunitIds, Integer programId, Integer programStageId, - Integer dataElementId, String dateProperty, String compareSide ) - { - String sql = " EXISTS ( SELECT _pdv.value FROM trackedentitydatavalue _pdv inner join programstageinstance _psi " - + " ON _pdv.programstageinstanceid=_psi.programstageinstanceid " - + " JOIN programinstance _pi ON _pi.programinstanceid=_psi.programinstanceid " - + " WHERE psi.programstageinstanceid=_pdv.programstageinstanceid " - + " AND _pdv.dataelementid=" + dataElementId - + " AND _psi.organisationunitid in (" + TextUtils.getCommaDelimitedString( orgunitIds ) - + ") "; - - - if ( programId != null ) - { - sql += " AND _pi.programid = " + programId; - } - - if ( programId != null ) - { - sql += " AND _psi.programstageid = " + programStageId; - } - - sql += " AND ( _psi.executionDate BETWEEN '" + PARAM_PERIOD_START_DATE + "' AND '" + PARAM_PERIOD_END_DATE - + "') " + " AND ( DATE(" + dateProperty + ") - DATE(_pdv.value) " + compareSide + " ) "; - - return sql; - } - - private String getConditionForMisus2DataElement( Collection orgunitIds, String programStageId1, - String dataElementId1, String programStageId2, String dataElementId2, String compareSide ) - { - return " EXISTS ( SELECT * FROM ( SELECT _pdv.value FROM trackedentitydatavalue _pdv " - + " INNER JOIN programstageinstance _psi ON _pdv.programstageinstanceid=_psi.programstageinstanceid " - + " JOIN programinstance _pi ON _pi.programinstanceid=_psi.programinstanceid " - + " WHERE _pi.programinstanceid=pi.programinstanceid AND _pdv.dataelementid= " - + dataElementId1 - + " AND _psi.organisationunitid in (" - + TextUtils.getCommaDelimitedString( orgunitIds ) - + ") " - + " AND _psi.programstageid = " - + programStageId1 - + " AND _psi.executionDate>='" - + PARAM_PERIOD_START_DATE - + "' " - + " AND _psi.executionDate <= '" - + PARAM_PERIOD_END_DATE - + "' ) AS d1 cross join " - + " ( SELECT _pdv.value FROM trackedentitydatavalue _pdv INNER JOIN programstageinstance _psi " - + " ON _pdv.programstageinstanceid=_psi.programstageinstanceid " - + " JOIN programinstance _pi ON _pi.programinstanceid=_psi.programinstanceid " - + " WHERE _pi.programinstanceid=pi.programinstanceid and _pdv.dataelementid= " - + dataElementId2 - + " AND _psi.organisationunitid in (" - + TextUtils.getCommaDelimitedString( orgunitIds ) - + ") " - + " AND _psi.programstageid = " - + programStageId2 - + " AND _psi.executionDate>='" - + PARAM_PERIOD_START_DATE - + "' " - + " AND _psi.executionDate <= '" - + PARAM_PERIOD_END_DATE - + "' ) AS d2 WHERE DATE(d1.value ) - DATE(d2.value) " + compareSide; - } - - private String getConditionForMisus2Attribute( String attribute1, String attribute2, String compareSide ) - { - return " EXISTS ( SELECT * FROM ( SELECT _teav.value FROM trackedentityattributevalue _teav " - + " WHERE _teav.trackedentityinstanceid=p.trackedentityinstanceid " - + " and _teav.trackedentityattributeid = " + attribute1 + " ) as a1 , " - + " ( SELECT _teav.value FROM trackedentityattributevalue _teav " - + " WHERE _teav.trackedentityinstanceid=p.trackedentityinstanceid " - + " and _teav.trackedentityattributeid = " + attribute2 + " ) as a2 " - + " WHERE DATE(a1.value ) - DATE(a2.value) " + compareSide; - } - - private String getConditionForMisusAttribute1( String attribute, String dateProperty, String compareSide ) - { - if ( dateProperty.equals( CaseAggregationCondition.OBJECT_PROGRAM_PROPERTY_INCIDENT_DATE ) - || dateProperty.equals( CaseAggregationCondition.OBJECT_PROGRAM_PROPERTY_ENROLLEMENT_DATE ) ) - { - return " EXISTS ( select * from trackedentityattributevalue _teav " - + "inner join programinstance _pi on _teav.trackedentityinstanceid=_pi.trackedentityinstanceid " - + "where _teav.trackedentityattributeid=" + attribute + " and date(" + dateProperty + ") - date(_teav.value) " + compareSide; - } - else if ( dateProperty.equals( CaseAggregationCondition.OBJECT_PROGRAM_PROPERTY_REPORT_DATE ) ) - { - return " EXISTS ( select * from trackedentityattributevalue _teav " - + "inner join programinstance _pi on _teav.trackedentityinstanceid=_pi.trackedentityinstanceid " - + "inner join programstageinstance _psi on _psi.programinstanceid=_pi.programinstanceid " - + "where _teav.trackedentityattributeid=" + attribute + " and date(" + dateProperty + ") - date(_teav.value) " + compareSide; - } - - return " EXISTS (select * from trackedentityattributevalue _teav where _teav.trackedentityattributeid=" - + attribute + " and date(now()) - date(_teav.value) " + compareSide; - } - - private String getConditionForMisusAttribute2( String attribute, String dateProperty, String compareSide ) - { - if ( dateProperty.equals( CaseAggregationCondition.OBJECT_PROGRAM_PROPERTY_INCIDENT_DATE ) - || dateProperty.equals( CaseAggregationCondition.OBJECT_PROGRAM_PROPERTY_ENROLLEMENT_DATE ) ) - { - return " EXISTS ( select * from trackedentityattributevalue _teav " - + "inner join programinstance _pi on _teav.trackedentityinstanceid=_pi.trackedentityinstanceid " - + "where _teav.trackedentityattributeid=" + attribute + " and date(_teav.value) - date(" + dateProperty + ") " + compareSide; - } - else if ( dateProperty.equals( CaseAggregationCondition.OBJECT_PROGRAM_PROPERTY_REPORT_DATE ) ) - { - return " EXISTS ( select * from trackedentityattributevalue _teav " - + "inner join programinstance _pi on _teav.trackedentityinstanceid=_pi.trackedentityinstanceid " - + "inner join programstageinstance _psi on _psi.programinstanceid=_pi.programinstanceid " - + "where _teav.trackedentityattributeid=" + attribute + " and date(_teav.value) - date(" + dateProperty + ") " + compareSide; - } - - return " EXISTS (select * from trackedentityattributevalue _teav where _teav.trackedentityattributeid=" - + attribute + " and date(_teav.value) - date(now()) " + compareSide; - } - - /** - * Return the Ids of organisation units which entity instances registered or - * events happened. - */ - @Override - public Collection getServiceOrgunit() - { - String sql = "(select distinct organisationunitid from trackedentityinstance)"; - sql += " UNION "; - sql += "(select distinct organisationunitid from programstageinstance where organisationunitid is not null)"; - - Collection orgunitIds = jdbcTemplate.query( sql, new RowMapper() - { - @Override - public Integer mapRow( ResultSet rs, int rowNum ) - throws SQLException - { - return rs.getInt( 1 ); - } - } ); - - return orgunitIds; - } - - @Override - public String parseExpressionDetailsToSql( String caseExpression, String operator, Integer orgunitId, - Period period, boolean nonRegistrationProgram ) - { - String sql = "SELECT "; - - Collection orgunitIds = new HashSet<>(); - orgunitIds.add( orgunitId ); - - if ( hasOrgunitProgramStageCompleted( caseExpression ) ) - { - sql += "ou.name " + createSQL( caseExpression, operator, orgunitIds ); - } - else if ( nonRegistrationProgram ) - { - sql += " pdv.programstageinstanceid as event, pgs.name as program_stage, de.name as data_element, pdv.value, psi.executiondate as report_date "; - sql += " FROM programstageinstance psi inner join programinstance pi on pi.programinstanceid=psi.programinstanceid "; - sql += " INNER JOIN organisationunit ou ON ou.organisationunitid=psi.organisationunitid "; - sql += " INNER JOIN trackedentitydatavalue pdv ON pdv.programstageinstanceid=psi.programstageinstanceid "; - sql += " INNER JOIN dataelement de ON de.dataelementid=pdv.dataelementid "; - sql += " INNER JOIN program pg on pg.programid=pi.programid "; - sql += " INNER JOIN programstage pgs ON pgs.programid=pg.programid "; - } - else - { - sql += " p.trackedentityinstanceid as tracked_entity_instance, tea.name as attribute, "; - sql += "teav.value as value, pg.name as program "; - sql += "FROM trackedentityinstance p "; - sql += "INNER JOIN trackedentityattributevalue teav on p.trackedentityinstanceid=teav.trackedentityinstanceid "; - sql += "INNER JOIN trackedentityattribute tea on tea.trackedentityattributeid=teav.trackedentityattributeid "; - sql += "INNER JOIN programinstance as pi on p.trackedentityinstanceid=pi.trackedentityinstanceid "; - sql += "INNER JOIN program pg on pg.programid=pi.programid "; - sql += "INNER JOIN programstage pgs on pgs.programid=pg.programid "; - - if ( hasDataelementCriteria( caseExpression ) ) - { - sql += " INNER JOIN programstageinstance psi on pi.programinstanceid=psi.programinstanceid "; - sql += " INNER JOIN organisationunit ou on ou.organisationunitid=psi.organisationunitid "; - sql += " INNER JOIN trackedentitydatavalue pdv on pdv.programstageinstanceid=psi.programstageinstanceid "; - } - } - - sql += " WHERE " + createSQL( caseExpression, operator, orgunitIds ); - - sql = sql.replaceAll( "COMBINE", "" ); - - if ( nonRegistrationProgram ) - { - sql += " ORDER BY pdv.programstageinstanceid"; - } - else - { - sql += " ORDER BY p.trackedentityinstanceid "; - } - - sql = replacePeriodSql( sql, period ); - - return sql; - } - - @Override - public List executeSQL( String sql ) - { - try - { - List entityInstanceIds = jdbcTemplate.query( sql, new RowMapper() - { - @Override - public Integer mapRow( ResultSet rs, int rowNum ) - throws SQLException - { - return rs.getInt( 1 ); - } - } ); - - return entityInstanceIds; - } - catch ( Exception ex ) - { - ex.printStackTrace(); - return null; - } - } - - @Override - public Collection getPeriods( String periodTypeName, String taskStrategy ) - { - Calendar calStartDate = Calendar.getInstance(); - - if ( TASK_AGGREGATE_QUERY_BUILDER_LAST_MONTH.equals( taskStrategy ) ) - { - calStartDate.add( Calendar.MONTH, -1 ); - } - else if ( TASK_AGGREGATE_QUERY_BUILDER_LAST_3_MONTH.equals( taskStrategy ) ) - { - calStartDate.add( Calendar.MONTH, -3 ); - } - else if ( TASK_AGGREGATE_QUERY_BUILDER_LAST_6_MONTH.equals( taskStrategy ) ) - { - calStartDate.add( Calendar.MONTH, -6 ); - } - else if ( TASK_AGGREGATE_QUERY_BUILDER_LAST_12_MONTH.equals( taskStrategy ) ) - { - calStartDate.add( Calendar.MONTH, -12 ); - } - - Date startDate = calStartDate.getTime(); - - Calendar calEndDate = Calendar.getInstance(); - - Date endDate = calEndDate.getTime(); - - CalendarPeriodType periodType = (CalendarPeriodType) PeriodType.getPeriodTypeByName( periodTypeName ); - List periods = new ArrayList<>( periodType.generatePeriods( startDate, endDate ) ); - periods = periodService.reloadPeriods( periods ); - - return periods; - } - - private boolean hasOrgunitProgramStageCompleted( String expresstion ) - { - Pattern pattern = Pattern.compile( CaseAggregationCondition.regExp ); - Matcher matcher = pattern.matcher( expresstion ); - while ( matcher.find() ) - { - String match = matcher.group(); - - match = match.replaceAll( "[\\[\\]]", "" ); - - String[] info = match.split( SEPARATOR_OBJECT ); - - if ( info[0].equalsIgnoreCase( CaseAggregationCondition.OBJECT_ORGUNIT_COMPLETE_PROGRAM_STAGE ) ) - { - return true; - } - } - - return false; - } - - private boolean hasEntityInstanceCriteria( String expresstion ) - { - Pattern pattern = Pattern.compile( CaseAggregationCondition.regExp ); - Matcher matcher = pattern.matcher( expresstion ); - while ( matcher.find() ) - { - String match = matcher.group(); - - match = match.replaceAll( "[\\[\\]]", "" ); - - String[] info = match.split( SEPARATOR_OBJECT ); - - if ( info[0].equalsIgnoreCase( CaseAggregationCondition.OBJECT_TRACKED_ENTITY_ATTRIBUTE ) ) - { - return true; - } - } - - return false; - } - - private boolean hasDataelementCriteria( String expression ) - { - Pattern pattern = Pattern.compile( CaseAggregationCondition.regExp ); - Matcher matcher = pattern.matcher( expression ); - while ( matcher.find() ) - { - String match = matcher.group(); - - match = match.replaceAll( "[\\[\\]]", "" ); - String[] info = match.split( SEPARATOR_OBJECT ); - - if ( info[0].equalsIgnoreCase( CaseAggregationCondition.OBJECT_PROGRAM_STAGE_DATAELEMENT ) - || info[0].equalsIgnoreCase( CaseAggregationCondition.OBJECT_PROGRAM_STAGE ) - || info[0].equalsIgnoreCase( CaseAggregationCondition.OBJECT_PROGRAM_STAGE_PROPERTY ) - || info[0].equalsIgnoreCase( CaseAggregationCondition.OBJECT_TRACKED_ENTITY_PROGRAM_STAGE_PROPERTY ) - || info[0].equalsIgnoreCase( CaseAggregationCondition.OBJECT_ORGUNIT_COMPLETE_PROGRAM_STAGE ) ) - { - return true; - } - } - - return false; - } - - private String replacePeriodSql( String sql, Period period ) - { - sql = sql.replaceAll( "COMBINE", "" ); - sql = sql.replaceAll( PARAM_PERIOD_START_DATE, DateUtils.getMediumDateString( period.getStartDate() ) ); - sql = sql.replaceAll( PARAM_PERIOD_END_DATE, DateUtils.getMediumDateString( period.getEndDate() ) ); - sql = sql.replaceAll( PARAM_PERIOD_ID, period.getId() + "" ); - sql = sql.replaceAll( PARAM_PERIOD_ISO_DATE, period.getIsoDate() ); - - return sql; - } -} === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling' === removed file 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/CaseAggregateConditionSchedulingManager.java' --- dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/CaseAggregateConditionSchedulingManager.java 2016-01-04 02:27:49 +0000 +++ dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/CaseAggregateConditionSchedulingManager.java 1970-01-01 00:00:00 +0000 @@ -1,59 +0,0 @@ -package org.hisp.dhis.scheduling; - -/* - * Copyright (c) 2004-2016, 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; - -import org.hisp.dhis.system.scheduling.ScheduledTaskStatus; - -/** - * @author Chau Thu Tran - * - * @version CaseAggregateConditionSchedulingManager.java 10:42:58 AM Oct 10, 2012 $ - */ -public interface CaseAggregateConditionSchedulingManager -{ - final String TASK_AGGREGATE_QUERY_BUILDER = "aggregateQueryBuilder"; - final String TASK_AGGREGATE_QUERY_BUILDER_LAST_MONTH = "lastMonth"; - final String TASK_AGGREGATE_QUERY_BUILDER_LAST_3_MONTH = "last3Month"; - final String TASK_AGGREGATE_QUERY_BUILDER_LAST_6_MONTH = "last6Month"; - final String TASK_AGGREGATE_QUERY_BUILDER_LAST_12_MONTH = "last12Month"; - - void scheduleTasks(); - - void scheduleTasks( Map keyCronMap ); - - void stopTasks(); - - void executeTasks(); - - Map getScheduledTasks(); - - ScheduledTaskStatus getTaskStatus(); -} === removed file 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/CaseAggregateConditionTask.java' --- dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/CaseAggregateConditionTask.java 2016-01-04 02:27:49 +0000 +++ dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/CaseAggregateConditionTask.java 1970-01-01 00:00:00 +0000 @@ -1,127 +0,0 @@ -package org.hisp.dhis.scheduling; - -/* - * Copyright (c) 2004-2016, 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.system.notification.NotificationLevel.INFO; - -import java.util.ArrayList; -import java.util.List; - -import org.hisp.dhis.caseaggregation.CaseAggregateSchedule; -import org.hisp.dhis.caseaggregation.CaseAggregationConditionService; -import org.hisp.dhis.setting.SettingKey; -import org.hisp.dhis.setting.SystemSettingManager; -import org.hisp.dhis.system.notification.Notifier; -import org.hisp.dhis.system.util.Clock; -import org.hisp.dhis.system.util.SystemUtils; -import org.hisp.dhis.user.CurrentUserService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.support.rowset.SqlRowSet; - -/** - * @author Chau Thu Tran - * - * @version CaseAggregateConditionTask.java 9:52:10 AM Oct 10, 2012 $ - */ -public class CaseAggregateConditionTask - implements Runnable -{ - private CaseAggregationConditionService aggregationConditionService; - - public void setAggregationConditionService( CaseAggregationConditionService aggregationConditionService ) - { - this.aggregationConditionService = aggregationConditionService; - } - - private SystemSettingManager systemSettingManager; - - public void setSystemSettingManager( SystemSettingManager systemSettingManager ) - { - this.systemSettingManager = systemSettingManager; - } - - private JdbcTemplate jdbcTemplate; - - public void setJdbcTemplate( JdbcTemplate jdbcTemplate ) - { - this.jdbcTemplate = jdbcTemplate; - } - - private Notifier notifier; - - public void setNotifier( Notifier notifier ) - { - this.notifier = notifier; - } - - @Autowired - private CurrentUserService currentUserService; - - - // ------------------------------------------------------------------------- - // Runnable implementation - // ------------------------------------------------------------------------- - - @Override - public void run() - { - final int cpuCores = SystemUtils.getCpuCores(); - - TaskId taskId = new TaskId( TaskCategory.AGGREGATE_QUERY_BUILDER, currentUserService.getCurrentUser() ); - notifier.clear( taskId ); - - Clock clock = new Clock().startClock().logTime( - "Aggregate process started, number of CPU cores: " + cpuCores + ", " + SystemUtils.getMemoryString() ); - notifier.clear( taskId ).notify( taskId, "Aggregate process started" ); - - String taskStrategy = (String) systemSettingManager.getSystemSetting( SettingKey.SCHEDULE_AGGREGATE_QUERY_BUILDER_TASK_STRATEGY ); - - String sql = "select cagg.caseaggregationconditionid as caseaggregationconditionid, cagg.name as caseaggregationconditionname, pt.name as periodtypename "; - sql += "from caseaggregationcondition cagg inner join datasetmembers dm "; - sql += " on cagg.aggregationdataelementid=dm.dataelementid inner join dataset ds "; - sql += " on ds.datasetid = dm.datasetid inner join periodtype pt "; - sql += " on pt.periodtypeid=ds.periodtypeid"; - - SqlRowSet rsCondition = jdbcTemplate.queryForRowSet( sql ); - List caseAggregateSchedule = new ArrayList<>(); - while ( rsCondition.next() ) - { - CaseAggregateSchedule condition = new CaseAggregateSchedule( rsCondition.getInt( "caseaggregationconditionid" ), - rsCondition.getString( "caseaggregationconditionname"), rsCondition.getString( "periodtypename" ) ); - caseAggregateSchedule.add( condition ); - } - - aggregationConditionService.aggregate( caseAggregateSchedule, taskStrategy, taskId ); - - clock.logTime( "Improrted aggregate data completed " ); - - notifier.notify( taskId, INFO, "Improrted aggregate data completed", true ); - } -} === removed file 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/DefaultCaseAggregateConditionSchedulingManager.java' --- dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/DefaultCaseAggregateConditionSchedulingManager.java 2016-01-04 02:27:49 +0000 +++ dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/DefaultCaseAggregateConditionSchedulingManager.java 1970-01-01 00:00:00 +0000 @@ -1,147 +0,0 @@ -package org.hisp.dhis.scheduling; - -/* - * Copyright (c) 2004-2016, 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.HashMap; -import java.util.Map; - -import org.hisp.dhis.setting.SettingKey; -import org.hisp.dhis.setting.SystemSettingManager; -import org.hisp.dhis.system.scheduling.ScheduledTaskStatus; -import org.hisp.dhis.system.scheduling.Scheduler; - -/** - * @author Chau Thu Tran - * - * @version DefaultCaseAggregateConditionSchedulingManager.java 10:42:25 AM Oct - * 10, 2012 $ - */ -public class DefaultCaseAggregateConditionSchedulingManager - implements CaseAggregateConditionSchedulingManager -{ - // ------------------------------------------------------------------------- - // 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 - // ------------------------------------------------------------------------- - - @Override - 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 ); - } - } - } - - @Override - public void scheduleTasks( Map keyCronMap ) - { - systemSettingManager.saveSystemSetting( SettingKey.SCHEDULE_AGGREGATE_QUERY_BUILDER_TASKS, - new HashMap<>( keyCronMap ) ); - - scheduleTasks(); - } - - @Override - public void stopTasks() - { - systemSettingManager.saveSystemSetting( SettingKey.SCHEDULE_AGGREGATE_QUERY_BUILDER_TASKS , null ); - - scheduler.stopAllTasks(); - } - - @Override - public void executeTasks() - { - Map keyCronMap = getScheduledTasks(); - - for ( String key : keyCronMap.keySet() ) - { - Runnable task = tasks.get( key ); - - if ( task != null ) - { - scheduler.executeTask( task ); - } - } - } - - @Override - @SuppressWarnings( "unchecked" ) - public Map getScheduledTasks() - { - return (Map) systemSettingManager.getSystemSetting( SettingKey.SCHEDULE_AGGREGATE_QUERY_BUILDER_TASKS, - new HashMap() ); - } - - @Override - public ScheduledTaskStatus getTaskStatus() - { - Map keyCronMap = getScheduledTasks(); - - if ( keyCronMap.size() == 0 ) - { - return ScheduledTaskStatus.NOT_STARTED; - } - - return scheduler.getTaskStatus( keyCronMap.keySet().iterator().next() ); - } -} === removed 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 2016-02-10 14:57:19 +0000 +++ dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/DefaultProgramSchedulingManager.java 1970-01-01 00:00:00 +0000 @@ -1,139 +0,0 @@ -package org.hisp.dhis.scheduling; - -/* - * Copyright (c) 2004-2016, 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.HashMap; -import java.util.Map; - -import org.hisp.dhis.setting.SettingKey; -import org.hisp.dhis.setting.SystemSettingManager; -import org.hisp.dhis.system.scheduling.ScheduledTaskStatus; -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 - // ------------------------------------------------------------------------- - - @Override - 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 ); - } - } - } - - @Override - public void scheduleTasks( Map keyCronMap ) - { - systemSettingManager.saveSystemSetting( SettingKey.SEND_MESSAGE_SCHEDULED_TASKS, new HashMap<>( keyCronMap ) ); - - scheduleTasks(); - } - - @Override - public void stopTasks() - { - systemSettingManager.saveSystemSetting( SettingKey.SEND_MESSAGE_SCHEDULED_TASKS, null ); - - scheduler.stopAllTasks(); - } - - @Override - public void executeTasks() - { - Runnable task = tasks.get( "sendMessageScheduledNow" ); - - if ( task != null ) - { - scheduler.executeTask( task ); - } - } - - @Override - @SuppressWarnings("unchecked") - public Map getScheduledTasks() - { - return (Map) systemSettingManager.getSystemSetting( SettingKey.SEND_MESSAGE_SCHEDULED_TASKS, new HashMap() ); - } - - @Override - public ScheduledTaskStatus getTaskStatus() - { - Map keyCronMap = getScheduledTasks(); - - if ( keyCronMap.size() == 0 ) - { - return ScheduledTaskStatus.NOT_STARTED; - } - - return scheduler.getTaskStatus( keyCronMap.keySet().iterator().next() ); - } -} - === removed 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 2016-01-04 02:27:49 +0000 +++ dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/ProgramSchedulingManager.java 1970-01-01 00:00:00 +0000 @@ -1,55 +0,0 @@ -package org.hisp.dhis.scheduling; - -/* - * Copyright (c) 2004-2016, 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; - -import org.hisp.dhis.system.scheduling.ScheduledTaskStatus; - -/** - * @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(); - - ScheduledTaskStatus getTaskStatus(); -} === removed 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 2016-02-10 14:57:19 +0000 +++ dhis-2/dhis-services/dhis-service-eventreporting/src/main/java/org/hisp/dhis/scheduling/SendScheduledMessageTask.java 1970-01-01 00:00:00 +0000 @@ -1,293 +0,0 @@ -package org.hisp.dhis.scheduling; - -/* - * Copyright (c) 2004-2016, 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.Date; -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.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 Boolean sendNow; - - public void setSendNow( Boolean sendNow ) - { - this.sendNow = sendNow; - } - - 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 ( sendNow ) - { - clock.logTime( "Starting to prepare reminder messages" ); - notifier.clear( taskId ).notify( taskId, "Start to prepare reminder messages" ); - - scheduleProgramStageInstanceMessage(); - scheduleProgramInstanceMessage(); - - clock.logTime( "Preparing reminder messages completed" ); - notifier.notify( taskId, INFO, "Preparing reminder messages completed", true ); - - clock.logTime( "Starting 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 ); - - return; - } - - if ( sendingMessage ) - { - clock.logTime( "Starting 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( "Starting to prepare reminder messages" ); - notifier.clear( taskId ).notify( taskId, "Start to prepare reminder messages" ); - - scheduleProgramStageInstanceMessage(); - scheduleProgramInstanceMessage(); - - 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 - .getSendMessageEvents(); - - 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 remigetScheduledTasksnder messages for enrollements" ); - - Collection schedulingProgramObjects = programInstanceService.getScheduledMessages(); - - 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.setDate( new Date() ); - outboundSms.setStatus( OutboundSmsStatus.SENT ); - smsSender.sendMessage( outboundSms, null ); - } - } - } -} === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/resources' === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/resources/META-INF' === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/resources/META-INF/dhis' === removed 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 2016-02-17 10:10:29 +0000 +++ dhis-2/dhis-services/dhis-service-eventreporting/src/main/resources/META-INF/dhis/beans.xml 1970-01-01 00:00:00 +0000 @@ -1,118 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/resources/org' === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/resources/org/hisp' === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/resources/org/hisp/dhis' === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/resources/org/hisp/dhis/caseaggregation' === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/resources/org/hisp/dhis/caseaggregation/hibernate' === removed file 'dhis-2/dhis-services/dhis-service-eventreporting/src/main/resources/org/hisp/dhis/caseaggregation/hibernate/CaseAggregationCondition.hbm.xml' --- dhis-2/dhis-services/dhis-service-eventreporting/src/main/resources/org/hisp/dhis/caseaggregation/hibernate/CaseAggregationCondition.hbm.xml 2013-08-21 12:29:12 +0000 +++ dhis-2/dhis-services/dhis-service-eventreporting/src/main/resources/org/hisp/dhis/caseaggregation/hibernate/CaseAggregationCondition.hbm.xml 1970-01-01 00:00:00 +0000 @@ -1,32 +0,0 @@ - -] - > - - - - - - - - &identifiableProperties; - - - - - - - - - - - - - - - === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src/test' === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src/test/java' === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src/test/java/org' === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src/test/java/org/hisp' === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src/test/java/org/hisp/dhis' === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src/test/java/org/hisp/dhis/caseaggregation' === removed file 'dhis-2/dhis-services/dhis-service-eventreporting/src/test/java/org/hisp/dhis/caseaggregation/CaseAggregationConditionServiceTest.java' --- dhis-2/dhis-services/dhis-service-eventreporting/src/test/java/org/hisp/dhis/caseaggregation/CaseAggregationConditionServiceTest.java 2016-01-04 02:27:49 +0000 +++ dhis-2/dhis-services/dhis-service-eventreporting/src/test/java/org/hisp/dhis/caseaggregation/CaseAggregationConditionServiceTest.java 1970-01-01 00:00:00 +0000 @@ -1,449 +0,0 @@ -package org.hisp.dhis.caseaggregation; - -/* - * Copyright (c) 2004-2016, 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 org.hisp.dhis.DhisSpringTest; -import org.hisp.dhis.common.ValueType; -import org.hisp.dhis.dataelement.DataElement; -import org.hisp.dhis.dataelement.DataElementCategory; -import org.hisp.dhis.dataelement.DataElementCategoryCombo; -import org.hisp.dhis.dataelement.DataElementCategoryOption; -import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo; -import org.hisp.dhis.dataelement.DataElementCategoryService; -import org.hisp.dhis.dataelement.DataElementDomain; -import org.hisp.dhis.dataelement.DataElementService; -import org.hisp.dhis.organisationunit.OrganisationUnit; -import org.hisp.dhis.organisationunit.OrganisationUnitService; -import org.hisp.dhis.period.DailyPeriodType; -import org.hisp.dhis.period.Period; -import org.hisp.dhis.period.PeriodService; -import org.hisp.dhis.period.PeriodType; -import org.hisp.dhis.program.Program; -import org.hisp.dhis.program.ProgramInstance; -import org.hisp.dhis.program.ProgramInstanceService; -import org.hisp.dhis.program.ProgramService; -import org.hisp.dhis.program.ProgramStage; -import org.hisp.dhis.program.ProgramStageInstance; -import org.hisp.dhis.program.ProgramStageInstanceService; -import org.hisp.dhis.program.ProgramStageService; -import org.hisp.dhis.trackedentity.TrackedEntityAttribute; -import org.hisp.dhis.trackedentity.TrackedEntityAttributeService; -import org.hisp.dhis.trackedentity.TrackedEntityInstance; -import org.hisp.dhis.trackedentity.TrackedEntityInstanceService; -import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue; -import org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue; -import org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValueService; -import org.joda.time.DateTime; -import org.junit.Test; -import org.springframework.beans.factory.annotation.Autowired; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import java.util.Collection; -import java.util.HashSet; -import java.util.Set; - -/** - * @author Chau Thu Tran - */ -public class CaseAggregationConditionServiceTest - extends DhisSpringTest -{ - @Autowired - private CaseAggregationConditionService aggConditionServiceService; - - @Autowired - private OrganisationUnitService organisationUnitService; - - @Autowired - private ProgramService programService; - - @Autowired - private ProgramStageService programStageService; - - @Autowired - private TrackedEntityInstanceService entityInstanceService; - - @Autowired - private DataElementService dataElementService; - - @Autowired - private DataElementCategoryService categoryService; - - @Autowired - private ProgramInstanceService programInstanceService; - - @Autowired - private ProgramStageInstanceService programStageInstanceService; - - @Autowired - private TrackedEntityDataValueService dataValueService; - - @Autowired - private TrackedEntityAttributeService attributeService; - - @Autowired - private PeriodService periodService; - - private TrackedEntityAttribute attribute; - - private DataElement dataElementA; - - private DataElement dataElementB; - - private DataElement dataElementC; - - private DataElement dataElementD; - - private DataElementCategoryOptionCombo categoryOptionCombo; - - private Program program; - - private CaseAggregationCondition conditionA; - - private CaseAggregationCondition conditionB; - - private OrganisationUnit organisationUnit; - - private Period period; - - private int stageBId; - - @Override - public void setUpTest() - { - organisationUnit = createOrganisationUnit( 'A' ); - organisationUnitService.addOrganisationUnit( organisationUnit ); - - // --------------------------------------------------------------------- - // Data elements && Category - // --------------------------------------------------------------------- - - DataElementCategoryOption categoryOptionA = new DataElementCategoryOption( "Male" ); - DataElementCategoryOption categoryOptionB = new DataElementCategoryOption( "Female" ); - DataElementCategoryOption categoryOptionC = new DataElementCategoryOption( "0-20" ); - DataElementCategoryOption categoryOptionD = new DataElementCategoryOption( "20-100" ); - - categoryService.addDataElementCategoryOption( categoryOptionA ); - categoryService.addDataElementCategoryOption( categoryOptionB ); - categoryService.addDataElementCategoryOption( categoryOptionC ); - categoryService.addDataElementCategoryOption( categoryOptionD ); - - DataElementCategory categoryA = new DataElementCategory( "Gender" ); - DataElementCategory categoryB = new DataElementCategory( "Agegroup" ); - - categoryA.getCategoryOptions().add( categoryOptionA ); - categoryA.getCategoryOptions().add( categoryOptionB ); - categoryB.getCategoryOptions().add( categoryOptionC ); - categoryB.getCategoryOptions().add( categoryOptionD ); - - categoryService.addDataElementCategory( categoryA ); - categoryService.addDataElementCategory( categoryB ); - - DataElementCategoryCombo categoryComboA = new DataElementCategoryCombo( "GenderAgegroup" ); - - categoryComboA.getCategories().add( categoryA ); - categoryComboA.getCategories().add( categoryB ); - - categoryService.addDataElementCategoryCombo( categoryComboA ); - - categoryOptionCombo = new DataElementCategoryOptionCombo(); - Set categoryOptions = new HashSet<>(); - categoryOptions.add( categoryOptionA ); - categoryOptions.add( categoryOptionB ); - categoryOptionCombo.setCategoryCombo( categoryComboA ); - categoryOptionCombo.setCategoryOptions( categoryOptions ); - categoryService.addDataElementCategoryOptionCombo( categoryOptionCombo ); - - dataElementA = createDataElement( 'A' ); - dataElementA.setValueType( ValueType.TEXT ); - dataElementA.setDomainType( DataElementDomain.TRACKER ); - - dataElementB = createDataElement( 'B' ); - dataElementB.setValueType( ValueType.TEXT ); - dataElementB.setDomainType( DataElementDomain.TRACKER ); - - dataElementC = createDataElement( 'C' ); - dataElementC.setDomainType( DataElementDomain.AGGREGATE ); - - dataElementD = createDataElement( 'D' ); - dataElementD.setDomainType( DataElementDomain.AGGREGATE ); - - int deAId = dataElementService.addDataElement( dataElementA ); - int deBId = dataElementService.addDataElement( dataElementB ); - dataElementService.addDataElement( dataElementC ); - dataElementService.addDataElement( dataElementD ); - - // --------------------------------------------------------------------- - // TrackedEntityInstance - // --------------------------------------------------------------------- - - TrackedEntityInstance entityInstance = createTrackedEntityInstance( 'A', organisationUnit ); - entityInstanceService.addTrackedEntityInstance( entityInstance ); - - // --------------------------------------------------------------------- - // Attribue value - // --------------------------------------------------------------------- - - attribute = createTrackedEntityAttribute( 'A' ); - int attributeId = attributeService.addTrackedEntityAttribute( attribute ); - - TrackedEntityAttributeValue attributeValue = createTrackedEntityAttributeValue( 'A', entityInstance, attribute ); - Set attributeValues = new HashSet<>(); - attributeValues.add( attributeValue ); - - // --------------------------------------------------------------------- - // Program && Program stages - // --------------------------------------------------------------------- - - program = createProgram( 'A', new HashSet<>(), organisationUnit ); - int programId = programService.addProgram( program ); - - ProgramStage stageA = new ProgramStage( "Stage-A", program ); - int stageAId = programStageService.saveProgramStage( stageA ); - - ProgramStage stageB = new ProgramStage( "Stage-B", program ); - stageBId = programStageService.saveProgramStage( stageB ); - - Set programStages = new HashSet<>(); - programStages.add( stageA ); - programStages.add( stageB ); - program.setProgramStages( programStages ); - programService.updateProgram( program ); - - // --------------------------------------------------------------------- - // Program Instance && data values - // --------------------------------------------------------------------- - - DateTime today = DateTime.now(); - ProgramInstance programInstance = programInstanceService.enrollTrackedEntityInstance( entityInstance, program, - today.toDate(), today.toDate(), organisationUnit ); - - ProgramStageInstance stageInstanceA = programStageInstanceService.createProgramStageInstance( programInstance, - stageA, today.toDate(), today.toDate(), organisationUnit ); - ProgramStageInstance stageInstanceB = programStageInstanceService.createProgramStageInstance( programInstance, - stageB, today.toDate(), today.toDate(), organisationUnit ); - - TrackedEntityDataValue dataValueA = new TrackedEntityDataValue( stageInstanceA, dataElementA, "A" ); - TrackedEntityDataValue dataValueB = new TrackedEntityDataValue( stageInstanceA, dataElementB, "B" ); - TrackedEntityDataValue dataValueC = new TrackedEntityDataValue( stageInstanceB, dataElementA, "C" ); - TrackedEntityDataValue dataValueD = new TrackedEntityDataValue( stageInstanceB, dataElementB, "D" ); - - dataValueService.saveTrackedEntityDataValue( dataValueA ); - dataValueService.saveTrackedEntityDataValue( dataValueB ); - dataValueService.saveTrackedEntityDataValue( dataValueC ); - dataValueService.saveTrackedEntityDataValue( dataValueD ); - - // --------------------------------------------------------------------- - // Period - // --------------------------------------------------------------------- - - PeriodType periodType = periodService.getPeriodTypeByName( DailyPeriodType.NAME ); - period = new Period(); - period.setPeriodType( periodType ); - period.setStartDate( today.toDate() ); - period.setEndDate( today.toDate() ); - periodService.addPeriod( period ); - - // --------------------------------------------------------------------- - // CaseAggregationCondition - // --------------------------------------------------------------------- - - String expression = "[" + CaseAggregationCondition.OBJECT_PROGRAM_STAGE_DATAELEMENT - + CaseAggregationCondition.SEPARATOR_OBJECT + programId + "." + stageAId + "." + deAId + "] is not null"; - expression += " AND [" + CaseAggregationCondition.OBJECT_PROGRAM_STAGE_DATAELEMENT - + CaseAggregationCondition.SEPARATOR_OBJECT + programId + "." + stageAId + "." + deBId + "] is not null"; - expression += " AND [" + CaseAggregationCondition.OBJECT_TRACKED_ENTITY_ATTRIBUTE - + CaseAggregationCondition.SEPARATOR_OBJECT + attributeId + "] is not null"; - conditionA = new CaseAggregationCondition( "A", CaseAggregationCondition.AGGRERATION_COUNT, expression, - dataElementC, categoryOptionCombo ); - - expression = "[" + CaseAggregationCondition.OBJECT_PROGRAM_STAGE_DATAELEMENT - + CaseAggregationCondition.SEPARATOR_OBJECT + programId + "." + stageBId + "." + deAId + "] is not null"; - conditionB = new CaseAggregationCondition( "B", CaseAggregationCondition.AGGRERATION_COUNT, expression, - dataElementD, categoryOptionCombo ); - } - - @Test - public void testAddCaseAggregationCondition() - { - int idA = aggConditionServiceService.addCaseAggregationCondition( conditionA ); - int idB = aggConditionServiceService.addCaseAggregationCondition( conditionB ); - - assertNotNull( aggConditionServiceService.getCaseAggregationCondition( idA ) ); - assertNotNull( aggConditionServiceService.getCaseAggregationCondition( idB ) ); - } - - @Test - public void testUpdateCaseAggregationCondition() - { - int idA = aggConditionServiceService.addCaseAggregationCondition( conditionA ); - - assertNotNull( aggConditionServiceService.getCaseAggregationCondition( idA ) ); - - conditionA.setName( "B" ); - aggConditionServiceService.updateCaseAggregationCondition( conditionA ); - - assertEquals( "B", aggConditionServiceService.getCaseAggregationCondition( idA ).getName() ); - } - - @Test - public void testDeleteCaseAggregationCondition() - { - int idA = aggConditionServiceService.addCaseAggregationCondition( conditionA ); - int idB = aggConditionServiceService.addCaseAggregationCondition( conditionB ); - - assertNotNull( aggConditionServiceService.getCaseAggregationCondition( idA ) ); - assertNotNull( aggConditionServiceService.getCaseAggregationCondition( idB ) ); - - aggConditionServiceService.deleteCaseAggregationCondition( conditionA ); - - assertNull( aggConditionServiceService.getCaseAggregationCondition( idA ) ); - assertNotNull( aggConditionServiceService.getCaseAggregationCondition( idB ) ); - - aggConditionServiceService.deleteCaseAggregationCondition( conditionB ); - - assertNull( aggConditionServiceService.getCaseAggregationCondition( idA ) ); - assertNull( aggConditionServiceService.getCaseAggregationCondition( idB ) ); - } - - @Test - public void testGetCaseAggregationConditionById() - { - int idA = aggConditionServiceService.addCaseAggregationCondition( conditionA ); - int idB = aggConditionServiceService.addCaseAggregationCondition( conditionB ); - - assertEquals( conditionA, aggConditionServiceService.getCaseAggregationCondition( idA ) ); - assertEquals( conditionB, aggConditionServiceService.getCaseAggregationCondition( idB ) ); - } - - @Test - public void testGetCaseAggregationConditionByName() - { - int idA = aggConditionServiceService.addCaseAggregationCondition( conditionA ); - - assertNotNull( aggConditionServiceService.getCaseAggregationCondition( idA ) ); - assertEquals( conditionA, aggConditionServiceService.getCaseAggregationCondition( "A" ) ); - } - - @Test - public void testGetCaseAggregationConditionByUid() - { - conditionA.setUid( "ABC" ); - aggConditionServiceService.addCaseAggregationCondition( conditionA ); - - assertEquals( conditionA, aggConditionServiceService.getCaseAggregationConditionByUid( "ABC" ) ); - } - - @Test - public void testGetAllCaseAggregationCondition() - { - aggConditionServiceService.addCaseAggregationCondition( conditionA ); - aggConditionServiceService.addCaseAggregationCondition( conditionB ); - - assertTrue( equals( aggConditionServiceService.getAllCaseAggregationCondition(), conditionA, conditionB ) ); - } - - @Test - public void testGetCaseAggregationConditionByDe() - { - aggConditionServiceService.addCaseAggregationCondition( conditionA ); - aggConditionServiceService.addCaseAggregationCondition( conditionB ); - - assertTrue( equals( aggConditionServiceService.getCaseAggregationCondition( dataElementC ), conditionA ) ); - assertTrue( equals( aggConditionServiceService.getCaseAggregationCondition( dataElementD ), conditionB ) ); - } - - @Test - public void testGetCaseAggregationConditionByDeOptionCombo() - { - aggConditionServiceService.addCaseAggregationCondition( conditionA ); - aggConditionServiceService.addCaseAggregationCondition( conditionB ); - - CaseAggregationCondition condition = aggConditionServiceService.getCaseAggregationCondition( dataElementC, - categoryOptionCombo ); - assertEquals( conditionA, condition ); - - condition = aggConditionServiceService.getCaseAggregationCondition( dataElementD, categoryOptionCombo ); - assertEquals( conditionB, condition ); - } - - @Test - public void testGetCaseAggregationCondition() - { - aggConditionServiceService.addCaseAggregationCondition( conditionA ); - aggConditionServiceService.addCaseAggregationCondition( conditionB ); - - Collection dataElements = new HashSet<>(); - dataElements.add( dataElementC ); - dataElements.add( dataElementD ); - - assertTrue( equals( aggConditionServiceService.getCaseAggregationConditions( dataElements, null, null, null ), - conditionA, conditionB ) ); - } - - @Test - public void testGetDataElementsInCondition() - { - aggConditionServiceService.addCaseAggregationCondition( conditionA ); - aggConditionServiceService.addCaseAggregationCondition( conditionB ); - - Collection dataElements = aggConditionServiceService.getDataElementsInCondition( conditionA - .getAggregationExpression() ); - assertTrue( equals( dataElements, dataElementA, dataElementB ) ); - } - - @Test - public void testGetProgramsInCondition() - { - Collection programs = aggConditionServiceService.getProgramsInCondition( conditionA - .getAggregationExpression() ); - assertTrue( equals( programs, program ) ); - } - - @Test - public void testGetTrackedEntityAttributesInCondition() - { - Collection attributes = aggConditionServiceService - .getTrackedEntityAttributesInCondition( conditionA.getAggregationExpression() ); - assertTrue( equals( attributes, attribute ) ); - } - - @Test - public void testGetConditionDescription() - { - String actual = aggConditionServiceService.getConditionDescription( conditionB.getAggregationExpression() ); - String expected = "[ProgramA.Stage-B.DataElementA] is not null"; - assertEquals( expected, actual ); - } - -} === removed file 'dhis-2/dhis-services/dhis-service-eventreporting/src/test/java/org/hisp/dhis/caseaggregation/CaseAggregationConditionStoreTest.java' --- dhis-2/dhis-services/dhis-service-eventreporting/src/test/java/org/hisp/dhis/caseaggregation/CaseAggregationConditionStoreTest.java 2016-01-04 02:27:49 +0000 +++ dhis-2/dhis-services/dhis-service-eventreporting/src/test/java/org/hisp/dhis/caseaggregation/CaseAggregationConditionStoreTest.java 1970-01-01 00:00:00 +0000 @@ -1,333 +0,0 @@ -package org.hisp.dhis.caseaggregation; - -/* - * Copyright (c) 2004-2016, 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.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import java.util.Collection; -import java.util.HashSet; -import java.util.Set; - -import org.hisp.dhis.DhisSpringTest; -import org.hisp.dhis.common.ValueType; -import org.hisp.dhis.dataelement.DataElement; -import org.hisp.dhis.dataelement.DataElementCategory; -import org.hisp.dhis.dataelement.DataElementCategoryCombo; -import org.hisp.dhis.dataelement.DataElementCategoryOption; -import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo; -import org.hisp.dhis.dataelement.DataElementCategoryService; -import org.hisp.dhis.dataelement.DataElementDomain; -import org.hisp.dhis.dataelement.DataElementService; -import org.hisp.dhis.organisationunit.OrganisationUnit; -import org.hisp.dhis.organisationunit.OrganisationUnitService; -import org.hisp.dhis.period.DailyPeriodType; -import org.hisp.dhis.period.Period; -import org.hisp.dhis.period.PeriodService; -import org.hisp.dhis.period.PeriodType; -import org.hisp.dhis.program.Program; -import org.hisp.dhis.program.ProgramInstance; -import org.hisp.dhis.program.ProgramInstanceService; -import org.hisp.dhis.program.ProgramService; -import org.hisp.dhis.program.ProgramStage; -import org.hisp.dhis.program.ProgramStageInstance; -import org.hisp.dhis.program.ProgramStageInstanceService; -import org.hisp.dhis.program.ProgramStageService; -import org.hisp.dhis.trackedentity.TrackedEntityAttribute; -import org.hisp.dhis.trackedentity.TrackedEntityAttributeService; -import org.hisp.dhis.trackedentity.TrackedEntityInstance; -import org.hisp.dhis.trackedentity.TrackedEntityInstanceService; -import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue; -import org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue; -import org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValueService; -import org.joda.time.DateTime; -import org.junit.Test; -import org.springframework.beans.factory.annotation.Autowired; - -/** - * @author Chau Thu Tran - * - * @version $ CaseAggregationConditionStoreTest.java Nov 29, 2013 10:01:48 AM $ - */ -public class CaseAggregationConditionStoreTest - extends DhisSpringTest -{ - @Autowired - private CaseAggregationConditionStore aggConditionServiceStore; - - @Autowired - private OrganisationUnitService organisationUnitService; - - @Autowired - private ProgramService programService; - - @Autowired - private ProgramStageService programStageService; - - @Autowired - private TrackedEntityInstanceService entityInstanceService; - - @Autowired - private DataElementService dataElementService; - - @Autowired - private DataElementCategoryService categoryService; - - @Autowired - private ProgramInstanceService programInstanceService; - - @Autowired - private ProgramStageInstanceService programStageInstanceService; - - @Autowired - private TrackedEntityDataValueService dataValueService; - - @Autowired - private TrackedEntityAttributeService attributeService; - - @Autowired - private PeriodService periodService; - - private TrackedEntityAttribute attribute; - - private DataElement dataElementA; - - private DataElement dataElementB; - - private DataElement dataElementC; - - private DataElement dataElementD; - - private DataElementCategoryOptionCombo categoryOptionCombo; - - private Program program; - - private CaseAggregationCondition conditionA; - - private CaseAggregationCondition conditionB; - - private OrganisationUnit organisationUnit; - - private Period period; - - private int stageBId; - - @Override - public void setUpTest() - { - organisationUnit = createOrganisationUnit( 'A' ); - organisationUnitService.addOrganisationUnit( organisationUnit ); - - // --------------------------------------------------------------------- - // Data elements && Category - // --------------------------------------------------------------------- - - DataElementCategoryOption categoryOptionA = new DataElementCategoryOption( "Male" ); - DataElementCategoryOption categoryOptionB = new DataElementCategoryOption( "Female" ); - DataElementCategoryOption categoryOptionC = new DataElementCategoryOption( "0-20" ); - DataElementCategoryOption categoryOptionD = new DataElementCategoryOption( "20-100" ); - - categoryService.addDataElementCategoryOption( categoryOptionA ); - categoryService.addDataElementCategoryOption( categoryOptionB ); - categoryService.addDataElementCategoryOption( categoryOptionC ); - categoryService.addDataElementCategoryOption( categoryOptionD ); - - DataElementCategory categoryA = new DataElementCategory( "Gender" ); - DataElementCategory categoryB = new DataElementCategory( "Agegroup" ); - - categoryA.getCategoryOptions().add( categoryOptionA ); - categoryA.getCategoryOptions().add( categoryOptionB ); - categoryB.getCategoryOptions().add( categoryOptionC ); - categoryB.getCategoryOptions().add( categoryOptionD ); - - categoryService.addDataElementCategory( categoryA ); - categoryService.addDataElementCategory( categoryB ); - - DataElementCategoryCombo categoryComboA = new DataElementCategoryCombo( "GenderAgegroup" ); - - categoryComboA.getCategories().add( categoryA ); - categoryComboA.getCategories().add( categoryB ); - - categoryService.addDataElementCategoryCombo( categoryComboA ); - - categoryOptionCombo = new DataElementCategoryOptionCombo(); - Set categoryOptions = new HashSet<>(); - categoryOptions.add( categoryOptionA ); - categoryOptions.add( categoryOptionB ); - categoryOptionCombo.setCategoryCombo( categoryComboA ); - categoryOptionCombo.setCategoryOptions( categoryOptions ); - categoryService.addDataElementCategoryOptionCombo( categoryOptionCombo ); - - dataElementA = createDataElement( 'A' ); - dataElementA.setValueType( ValueType.TEXT ); - dataElementA.setDomainType( DataElementDomain.TRACKER ); - - dataElementB = createDataElement( 'B' ); - dataElementB.setValueType( ValueType.TEXT ); - dataElementB.setDomainType( DataElementDomain.TRACKER ); - - dataElementC = createDataElement( 'C' ); - dataElementC.setDomainType( DataElementDomain.AGGREGATE ); - - dataElementD = createDataElement( 'D' ); - dataElementD.setDomainType( DataElementDomain.AGGREGATE ); - - int deAId = dataElementService.addDataElement( dataElementA ); - int deBId = dataElementService.addDataElement( dataElementB ); - dataElementService.addDataElement( dataElementC ); - dataElementService.addDataElement( dataElementD ); - - // --------------------------------------------------------------------- - // TrackedEntityInstance - // --------------------------------------------------------------------- - - TrackedEntityInstance entityInstance = createTrackedEntityInstance( 'A', organisationUnit ); - entityInstanceService.addTrackedEntityInstance( entityInstance ); - - // --------------------------------------------------------------------- - // Attribue value - // --------------------------------------------------------------------- - - attribute = createTrackedEntityAttribute( 'A' ); - int attributeId = attributeService.addTrackedEntityAttribute( attribute ); - - TrackedEntityAttributeValue attributeValue = createTrackedEntityAttributeValue( 'A', entityInstance, attribute ); - Set attributeValues = new HashSet<>(); - attributeValues.add( attributeValue ); - - // --------------------------------------------------------------------- - // Program && Program stages - // --------------------------------------------------------------------- - - program = createProgram( 'A', new HashSet<>(), organisationUnit ); - int programId = programService.addProgram( program ); - - ProgramStage stageA = new ProgramStage( "Stage-A", program ); - int stageAId = programStageService.saveProgramStage( stageA ); - - ProgramStage stageB = new ProgramStage( "Stage-B", program ); - stageBId = programStageService.saveProgramStage( stageB ); - - Set programStages = new HashSet<>(); - programStages.add( stageA ); - programStages.add( stageB ); - program.setProgramStages( programStages ); - programService.updateProgram( program ); - - // --------------------------------------------------------------------- - // Program Instance && data values - // --------------------------------------------------------------------- - - DateTime today = DateTime.now(); - today.withTimeAtStartOfDay(); - ProgramInstance programInstance = programInstanceService.enrollTrackedEntityInstance( entityInstance, program, - today.toDate(), today.toDate(), organisationUnit ); - - ProgramStageInstance stageInstanceA = programStageInstanceService.createProgramStageInstance( programInstance, - stageA, today.toDate(), today.toDate(), organisationUnit ); - ProgramStageInstance stageInstanceB = programStageInstanceService.createProgramStageInstance( programInstance, - stageB, today.toDate(), today.toDate(), organisationUnit ); - - TrackedEntityDataValue dataValueA = new TrackedEntityDataValue( stageInstanceA, dataElementA, "A" ); - TrackedEntityDataValue dataValueB = new TrackedEntityDataValue( stageInstanceA, dataElementB, "B" ); - TrackedEntityDataValue dataValueC = new TrackedEntityDataValue( stageInstanceB, dataElementA, "C" ); - TrackedEntityDataValue dataValueD = new TrackedEntityDataValue( stageInstanceB, dataElementB, "D" ); - - dataValueService.saveTrackedEntityDataValue( dataValueA ); - dataValueService.saveTrackedEntityDataValue( dataValueB ); - dataValueService.saveTrackedEntityDataValue( dataValueC ); - dataValueService.saveTrackedEntityDataValue( dataValueD ); - - // --------------------------------------------------------------------- - // Period - // --------------------------------------------------------------------- - - PeriodType periodType = periodService.getPeriodTypeByName( DailyPeriodType.NAME ); - period = new Period(); - period.setPeriodType( periodType ); - period.setStartDate( today.toDate() ); - period.setEndDate( today.toDate() ); - periodService.addPeriod( period ); - - // --------------------------------------------------------------------- - // CaseAggregationCondition - // --------------------------------------------------------------------- - - String expression = "[" + CaseAggregationCondition.OBJECT_PROGRAM_STAGE_DATAELEMENT - + CaseAggregationCondition.SEPARATOR_OBJECT + programId + "." + stageAId + "." + deAId + "] is not null"; - expression += " AND [" + CaseAggregationCondition.OBJECT_PROGRAM_STAGE_DATAELEMENT - + CaseAggregationCondition.SEPARATOR_OBJECT + programId + "." + stageAId + "." + deBId + "] is not null"; - expression += " AND [" + CaseAggregationCondition.OBJECT_TRACKED_ENTITY_ATTRIBUTE - + CaseAggregationCondition.SEPARATOR_OBJECT + attributeId + "] is not null"; - conditionA = new CaseAggregationCondition( "A", CaseAggregationCondition.AGGRERATION_COUNT, expression, - dataElementC, categoryOptionCombo ); - - expression = "[" + CaseAggregationCondition.OBJECT_PROGRAM_STAGE_DATAELEMENT - + CaseAggregationCondition.SEPARATOR_OBJECT + programId + "." + stageBId + "." + deAId + "] is not null"; - conditionB = new CaseAggregationCondition( "B", CaseAggregationCondition.AGGRERATION_COUNT, expression, - dataElementD, categoryOptionCombo ); - } - - @Test - public void testGetCaseAggregationConditionByDe() - { - aggConditionServiceStore.save( conditionA ); - aggConditionServiceStore.save( conditionB ); - - assertTrue( equals( aggConditionServiceStore.get( dataElementC ), conditionA ) ); - assertTrue( equals( aggConditionServiceStore.get( dataElementD ), conditionB ) ); - } - - @Test - public void testGetCaseAggregationConditionByDeOptionCombo() - { - aggConditionServiceStore.save( conditionA ); - aggConditionServiceStore.save( conditionB ); - - CaseAggregationCondition condition = aggConditionServiceStore.get( dataElementC, categoryOptionCombo ); - assertEquals( conditionA, condition ); - - condition = aggConditionServiceStore.get( dataElementD, categoryOptionCombo ); - assertEquals( conditionB, condition ); - } - - @Test - public void testGetCaseAggregationCondition() - { - aggConditionServiceStore.save( conditionA ); - aggConditionServiceStore.save( conditionB ); - - Collection dataElements = new HashSet<>(); - dataElements.add( dataElementC ); - dataElements.add( dataElementD ); - - assertTrue( equals( aggConditionServiceStore.get( dataElements, null, null, null ), conditionA, conditionB ) ); - } - -} === removed directory 'dhis-2/dhis-services/dhis-service-eventreporting/src/test/resources' === modified file 'dhis-2/dhis-services/pom.xml' --- dhis-2/dhis-services/pom.xml 2016-01-15 00:52:40 +0000 +++ dhis-2/dhis-services/pom.xml 2016-02-22 11:40:17 +0000 @@ -28,7 +28,6 @@ dhis-service-administration dhis-service-analytics - dhis-service-eventreporting dhis-service-reporting @@ -48,4 +47,4 @@ ../ - \ No newline at end of file +