Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

WIP: PeriodUtils and not complete MeasureReport implementation for R4 #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
222 changes: 222 additions & 0 deletions common/src/main/java/org/dhis2/fhir/adapter/util/PeriodUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
package org.dhis2.fhir.adapter.util;

/*
* Copyright (c) 2004-2019, 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.springframework.util.StringUtils;

import javax.annotation.Nonnull;
import java.time.DayOfWeek;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.WeekFields;
import java.util.Date;

/**
* @author David Katuscak
*/
public class PeriodUtils
{
private static final String BI_MONTHLY_SUFFIX = "B";
private static final String SIX_MONTH_ONE_SUFFIX = "S1";
private static final String SIX_MONTH_TWO_SUFFIX = "S2";
private static final String Q1_SUFFIX = "Q1";
private static final String Q2_SUFFIX = "Q2";
private static final String Q3_SUFFIX = "Q3";
private static final String Q4_SUFFIX = "Q4";
private static final String BI_WEEK_PREFIX = "Bi";
private static final String WEEK_PREFIX = "W";

private static final ZoneId zoneId = ZoneId.systemDefault();

public static String getDHIS2PeriodString( @Nonnull Date periodStart, @Nonnull Date periodEnd )
{
ZonedDateTime startDate = ZonedDateTime.ofInstant( periodStart.toInstant(), zoneId );
ZonedDateTime endDate = ZonedDateTime.ofInstant( periodEnd.toInstant(), zoneId );

Period period = Period.between( startDate.toLocalDate(), endDate.toLocalDate() );

int totalMonths = period.getMonths();
int month = startDate.getMonthValue();

String periodString = String.valueOf( startDate.getYear() );

//Period is year
if ( totalMonths == 12 )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switch could be used instead of if/else.

{
return getYearPeriod( startDate, month, periodString );
}
//Period is six-month
else if ( totalMonths == 6 )
{
return getSixMonthPeriod( startDate, month, periodString );
}
else if ( totalMonths == 3 )
{
return getQuarterPeriod( startDate, month, periodString );
}
else if ( totalMonths == 2 )
{
return getBiMonthPeriod( startDate, month, periodString );
}
else if ( period.getMonths() == 1 )
{
return getMonthPeriod( month, periodString );
}
else if ( period.getMonths() == 0 )
{
if ( period.getDays() == 14 || period.getDays() == 7 )
{
return getWeekAndBiWeekPeriod( startDate, period, periodString );
}
else if ( period.getDays() == 1 )
{
return startDate.toLocalDate().format( DateTimeFormatter.BASIC_ISO_DATE );
}
}

throw new IllegalArgumentException( "Provided dates ('" + periodStart + "', '" + periodEnd +
"' ) do not represent a valid DHIS2 Period. " );
}

private static String getWeekAndBiWeekPeriod( ZonedDateTime startDate, Period period, String periodString )
{
int weekNumber = startDate.toLocalDate().get( WeekFields.ISO.weekOfWeekBasedYear() );
DayOfWeek dayOfWeek = startDate.getDayOfWeek();
if ( period.getDays() == 14 )
{
if ( (weekNumber % 2 ) == 1 )
{
return periodString + BI_WEEK_PREFIX + WEEK_PREFIX + weekNumber;
}

throw new IllegalArgumentException( "Bi-weekly period cannot start in odd week: " + startDate );
}
else
{
if ( dayOfWeek == DayOfWeek.MONDAY )
{
return periodString + WEEK_PREFIX + weekNumber;
}
else
{
String dayOfWeekShort = StringUtils.capitalize( dayOfWeek.name().substring( 0, 3 ) );
return periodString + dayOfWeekShort + WEEK_PREFIX + weekNumber;
}
}
}

private static String getYearPeriod( ZonedDateTime startDate, int month, String periodString )
{
//Regular year. Starts in January
if ( month == 1 )
{
return periodString;
}
//Financial year that starts in other month than January
else
{
return periodString + StringUtils.capitalize( startDate.getMonth().name().toLowerCase() );
}
}

private static String getSixMonthPeriod( ZonedDateTime startDate, int month, String periodString )
{
if ( month == 1 )
{
return periodString + SIX_MONTH_ONE_SUFFIX;
}
else if ( month == 7 )
{
return periodString + SIX_MONTH_TWO_SUFFIX;
}
else if ( month > 1 && month < 7 )
{
return periodString + StringUtils.capitalize( startDate.getMonth().name().toLowerCase() ) +
SIX_MONTH_ONE_SUFFIX;
}
else
{
return periodString + StringUtils.capitalize( startDate.getMonth().name().toLowerCase() ) +
SIX_MONTH_TWO_SUFFIX;
}
}

private static String getQuarterPeriod( ZonedDateTime startDate, int month, String periodString )
{
if ( ( startDate.getMonthValue() % 3 ) == 1 )
{
if ( month == 1 )
{
return periodString + Q1_SUFFIX;
}
else if ( month == 4 )
{
return periodString + Q2_SUFFIX;
}
else if ( month == 7 )
{
return periodString + Q3_SUFFIX;
}
else
{
return periodString + Q4_SUFFIX;
}
}
else
{
throw new IllegalArgumentException( "Quarter period cannot start in month: " + startDate.getMonth().name() );
}
}

private static String getBiMonthPeriod( ZonedDateTime startDate, int month, String periodString )
{
if ( ( startDate.getMonthValue() % 2 ) == 1 )
{
return getMonthPeriod( month, periodString ) + BI_MONTHLY_SUFFIX;
}
else
{
throw new IllegalArgumentException( "Bi-month period cannot start in month: " + startDate.getMonth().name() );
}
}

private static String getMonthPeriod( int month, String periodString )
{
if ( month < 10 )
{
return periodString + "0" + month;
}
else
{
return periodString + month;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ public DataValueSet( @Nonnull String id )
this.id = id;
}

public DataValueSet( boolean newResource )
public DataValueSet( @Nonnull String id, boolean newResource )
{
this.id = id;
this.newResource = newResource;
this.modified = newResource;
this.local = newResource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,36 +280,20 @@ else if ( enrollment.isModified() )

private boolean saveDataValueSet( @Nonnull DataValueSet dataValueSet )
{
if ( validateDataValueSet( dataValueSet ) )
if ( dataValueSet.isNewResource() )
{
if ( dataValueSet.isNewResource() )
{
logger.info( "Creating new DataValueSet." );
dataValueSetService.createOrUpdate( dataValueSet );
logger.info( "Created new DataValueSet for dataSetId: {}, orgUnit: {}, period: {}.",
dataValueSet.getDataSetId(), dataValueSet.getOrgUnitId(), dataValueSet.getPeriod() );
return true;
}
else if ( dataValueSet.isModified() )
{
logger.info( "Updating existing DataValueSet." );
dataValueSetService.createOrUpdate( dataValueSet );
logger.info( "Created new DataValueSet for dataSetId: {}, orgUnit: {}, period: {}.",
dataValueSet.getDataSetId(), dataValueSet.getOrgUnitId(), dataValueSet.getPeriod() );
return true;
}
logger.info( "Creating new DataValueSet." );
dataValueSetService.createOrUpdate( dataValueSet );
logger.info( "Created new DataValueSet for dataSetId: {}, orgUnit: {}, period: {}.",
dataValueSet.getDataSetId(), dataValueSet.getOrgUnitId(), dataValueSet.getPeriod() );
return true;
}

return false;
}

private boolean validateDataValueSet( @Nonnull DataValueSet dataValueSet )
{
if ( dataValueSet.getDataSetId() != null && !dataValueSet.getDataSetId().isEmpty() &&
dataValueSet.getOrgUnitId() != null && !dataValueSet.getOrgUnitId().isEmpty() &&
dataValueSet.getPeriod() != null && !dataValueSet.getPeriod().isEmpty() &&
dataValueSet.getDataValues() != null && !dataValueSet.getDataValues().isEmpty() )
else if ( dataValueSet.isModified() )
{
logger.info( "Updating existing DataValueSet." );
dataValueSetService.createOrUpdate( dataValueSet );
logger.info( "Created new DataValueSet for dataSetId: {}, orgUnit: {}, period: {}.",
dataValueSet.getDataSetId(), dataValueSet.getOrgUnitId(), dataValueSet.getPeriod() );
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.dhis2.fhir.adapter.fhir.server.provider.dstu3;
package org.dhis2.fhir.adapter.fhir.server.provider.r4;

/*
* Copyright (c) 2004-2019, University of Oslo
Expand Down Expand Up @@ -41,34 +41,32 @@
import org.dhis2.fhir.adapter.fhir.repository.DhisRepository;
import org.dhis2.fhir.adapter.fhir.repository.FhirRepository;
import org.dhis2.fhir.adapter.fhir.server.provider.AbstractReadWriteResourceProvider;
import org.hl7.fhir.dstu3.model.MeasureReport;
import org.springframework.stereotype.Component;
import org.hl7.fhir.r4.model.Measure;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;

/**
* DSTU3 resource provider
* r4 resource provider
*
* @author David Katuscak
*/
@Component
public class Dstu3MeasureReportResourceProvider extends AbstractReadWriteResourceProvider<MeasureReport>
public class R4MeasureResourceProvider extends AbstractReadWriteResourceProvider<Measure>
{
public Dstu3MeasureReportResourceProvider( @Nonnull FhirClientResourceRepository fhirClientResourceRepository,
public R4MeasureResourceProvider( @Nonnull FhirClientResourceRepository fhirClientResourceRepository,
@Nonnull FhirClientSystemRepository fhirClientSystemRepository,
@Nonnull FhirRepository fhirRepository, @Nonnull DhisRepository dhisRepository )
{
super( MeasureReport.class, fhirClientResourceRepository, fhirClientSystemRepository, fhirRepository, dhisRepository );
super( Measure.class, fhirClientResourceRepository, fhirClientSystemRepository, fhirRepository, dhisRepository );
}

@Nonnull
@Override
public FhirVersion getFhirVersion()
{
return FhirVersion.DSTU3;
return FhirVersion.R4;
}

@Search( allowUnknownParams = true )
Expand Down
Loading