← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2240: A little cleanup

 

------------------------------------------------------------
revno: 2240
committer: Jo Størset <storset@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2010-12-01 01:06:40 +0530
message:
  A little cleanup
added:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/utils/PeriodUtil.java
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/model/DataElement.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/model/DataSet.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/model/DataSetValue.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/FacilityReportingServiceImpl.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/model/DataElement.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/model/DataElement.java	2010-11-27 04:15:31 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/model/DataElement.java	2010-11-30 19:36:40 +0000
@@ -4,6 +4,8 @@
 import java.io.IOException;
 import java.util.List;
 
+import javax.xml.bind.annotation.XmlAttribute;
+
 /*
  * Copyright (c) 2004-2010, University of Oslo
  * All rights reserved.
@@ -41,6 +43,7 @@
 
     private ModelList categoryOptionCombos;
 
+    @XmlAttribute
     public String getType()
     {
         return type;
@@ -61,6 +64,7 @@
         this.categoryOptionCombos = categoryOptionCombos;
     }
 
+    @XmlAttribute
     public boolean isCompulsory()
     {
         return compulsory;

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/model/DataSet.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/model/DataSet.java	2010-11-16 16:28:41 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/model/DataSet.java	2010-11-30 19:36:40 +0000
@@ -32,6 +32,9 @@
 import java.io.IOException;
 import java.util.List;
 
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlElementWrapper;
+
 public class DataSet
     extends Model
 {
@@ -50,6 +53,8 @@
         this.periodType = periodType;
     }
 
+    @XmlElementWrapper(name="sections")
+    @XmlElement(name="section")
     public List<Section> getSections()
     {
         return sections;

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/model/DataSetValue.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/model/DataSetValue.java	2010-11-16 16:28:41 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/model/DataSetValue.java	2010-11-30 19:36:40 +0000
@@ -37,16 +37,12 @@
     extends Model
 {
 
-    private String pName;
+    private String periodName;
 
     private boolean completed;
 
     private List<DataValue> dataValues = new ArrayList<DataValue>();
 
-    public DataSetValue()
-    {
-    }
-
     public boolean isCompleted()
     {
         return completed;
@@ -57,14 +53,14 @@
         this.completed = completed;
     }
 
-    public String getpName()
+    public String getPeriodName()
     {
-        return pName;
+        return periodName;
     }
 
-    public void setpName( String pName )
+    public void setPeriodName( String periodName )
     {
-        this.pName = pName;
+        this.periodName = periodName;
     }
 
     public void setDataValues( List<DataValue> dataValues )
@@ -81,7 +77,7 @@
     public void serialize( DataOutputStream dataOutputStream )
         throws IOException
     {
-        // FIXME: Get implementation from client
+        // FIXME: Implement..
     }
 
     @Override
@@ -91,7 +87,7 @@
 
         this.setId( din.readInt() );
         this.setName( din.readUTF() );
-        this.setpName( din.readUTF() );
+        this.setPeriodName( din.readUTF() );
         this.setCompleted( din.readBoolean() );
         int size = din.readInt();
 

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/FacilityReportingServiceImpl.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/FacilityReportingServiceImpl.java	2010-11-27 04:15:31 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/service/FacilityReportingServiceImpl.java	2010-11-30 19:36:40 +0000
@@ -29,10 +29,7 @@
 
 import static org.hisp.dhis.i18n.I18nUtils.i18n;
 
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
 import java.util.ArrayList;
-import java.util.Calendar;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
@@ -51,6 +48,7 @@
 import org.hisp.dhis.period.MonthlyPeriodType;
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodService;
+import org.hisp.dhis.period.PeriodType;
 import org.hisp.dhis.period.QuarterlyPeriodType;
 import org.hisp.dhis.period.WeeklyPeriodType;
 import org.hisp.dhis.period.YearlyPeriodType;
@@ -60,6 +58,7 @@
 import org.hisp.dhis.web.api.model.DataValue;
 import org.hisp.dhis.web.api.model.Section;
 import org.hisp.dhis.web.api.utils.LocaleUtil;
+import org.hisp.dhis.web.api.utils.PeriodUtil;
 import org.springframework.beans.factory.annotation.Required;
 
 public class FacilityReportingServiceImpl
@@ -95,11 +94,12 @@
 
         for ( org.hisp.dhis.dataset.DataSet dataSet : dataSetService.getDataSetsForMobile( unit ) )
         {
-            if ( dataSet.getPeriodType().getName().equals( "Daily" )
-                || dataSet.getPeriodType().getName().equals( "Weekly" )
-                || dataSet.getPeriodType().getName().equals( "Monthly" )
-                || dataSet.getPeriodType().getName().equals( "Yearly" )
-                || dataSet.getPeriodType().getName().equals( "Quarterly" ) )
+            PeriodType periodType = dataSet.getPeriodType();
+            if ( periodType instanceof DailyPeriodType
+                || periodType instanceof WeeklyPeriodType
+                || periodType instanceof MonthlyPeriodType
+                || periodType instanceof YearlyPeriodType
+                || periodType instanceof QuarterlyPeriodType )
             {
                 datasets.add( getDataSetForLocale( dataSet.getId(), locale ) );
             }
@@ -107,7 +107,7 @@
             {
                 log.warn( "Dataset '" + dataSet.getName()
                     + "' set to be reported from mobile, but not of a supported period type: "
-                    + dataSet.getPeriodType().getName() );
+                    + periodType.getName() );
             }
         }
 
@@ -171,7 +171,7 @@
 
             DataElement de = Mapping.getDataElement( dataElement );
 
-            // For facility Reporting, no data elements are mandetory
+            // For facility Reporting, no data elements are mandatory
             de.setCompulsory( false );
 
             dataElementList.add( de );
@@ -190,7 +190,7 @@
             return "INVALID_DATASET_ASSOCIATION";
         }
 
-        org.hisp.dhis.period.Period selectedPeriod = getPeriod( dataSetValue.getpName(), dataSet );
+        Period selectedPeriod = getPeriod( dataSetValue.getPeriodName(), dataSet.getPeriodType() );
 
         if ( selectedPeriod == null )
         {
@@ -274,129 +274,21 @@
         }
     }
 
-    public Period getPeriod( String periodName, org.hisp.dhis.dataset.DataSet dataSet )
+    public Period getPeriod( String periodName, PeriodType periodType )
     {
-        org.hisp.dhis.period.Period period = null;
-        org.hisp.dhis.period.Period persistedPeriod = null;
-        if ( dataSet.getPeriodType().getName().equals( "Daily" ) )
-        {
-            String pattern = "yyyy-MM-dd";
-            SimpleDateFormat formatter = new SimpleDateFormat( pattern );
-            Date date = new Date();
-
-            try
-            {
-
-                date = formatter.parse( periodName );
-                DailyPeriodType dailyPeriodType = new DailyPeriodType();
-                period = dailyPeriodType.createPeriod( date );
-
-            }
-            catch ( ParseException e )
-            {
-                e.printStackTrace();
-            }
-        }
-        else if ( dataSet.getPeriodType().getName().equals( "Weekly" ) )
-        {
-            try
-            {
-                int week = Integer.parseInt( periodName.substring( 0, periodName.indexOf( '-' ) ) );
-                int year = Integer
-                    .parseInt( periodName.substring( periodName.indexOf( '-' ) + 1, periodName.length() ) );
-
-                Calendar cal = Calendar.getInstance();
-                cal.set( Calendar.YEAR, year );
-                cal.set( Calendar.WEEK_OF_YEAR, week );
-                cal.setFirstDayOfWeek( Calendar.MONDAY );
-
-                WeeklyPeriodType weeklyPeriodType = new WeeklyPeriodType();
-                period = weeklyPeriodType.createPeriod( cal.getTime() );
-
-            }
-            catch ( Exception e )
-            {
-                e.printStackTrace();
-            }
-        }
-
-        else if ( dataSet.getPeriodType().getName().equals( "Monthly" ) )
-        {
-            try
-            {
-                int month = Integer.parseInt( periodName.substring( 0, periodName.indexOf( '-' ) ) );
-                int year = Integer
-                    .parseInt( periodName.substring( periodName.indexOf( '-' ) + 1, periodName.length() ) );
-
-                Calendar cal = Calendar.getInstance();
-                cal.set( Calendar.YEAR, year );
-                cal.set( Calendar.MONTH, month );
-
-                MonthlyPeriodType monthlyPeriodType = new MonthlyPeriodType();
-                period = monthlyPeriodType.createPeriod( cal.getTime() );
-
-            }
-            catch ( Exception e )
-            {
-                e.printStackTrace();
-            }
-        }
-
-        else if ( dataSet.getPeriodType().getName().equals( "Yearly" ) )
-        {
-            Calendar cal = Calendar.getInstance();
-            cal.set( Calendar.YEAR, Integer.parseInt( periodName ) );
-
-            YearlyPeriodType yearlyPeriodType = new YearlyPeriodType();
-
-            period = yearlyPeriodType.createPeriod( cal.getTime() );
-        }
-        else if ( dataSet.getPeriodType().getName().equals( "Quarterly" ) )
-        {
-            Calendar cal = Calendar.getInstance();
-
-            int month = 0;
-            if ( periodName.substring( 0, periodName.indexOf( " " ) ).equals( "Jan" ) )
-            {
-                month = 1;
-            }
-            else if ( periodName.substring( 0, periodName.indexOf( " " ) ).equals( "Apr" ) )
-            {
-                month = 4;
-            }
-            else if ( periodName.substring( 0, periodName.indexOf( " " ) ).equals( "Jul" ) )
-            {
-                month = 6;
-            }
-            else if ( periodName.substring( 0, periodName.indexOf( " " ) ).equals( "Oct" ) )
-            {
-                month = 10;
-            }
-
-            int year = Integer.parseInt( periodName.substring( periodName.lastIndexOf( " " ) + 1 ) );
-
-            cal.set( Calendar.MONTH, month );
-            cal.set( Calendar.YEAR, year );
-
-            QuarterlyPeriodType quarterlyPeriodType = new QuarterlyPeriodType();
-            if ( month != 0 )
-            {
-                period = quarterlyPeriodType.createPeriod( cal.getTime() );
-            }
-
-        }
-
-        if ( period != null )
-        {
-            persistedPeriod = periodService.getPeriod( period.getStartDate(), period.getEndDate(),
-                dataSet.getPeriodType() );
-
-            if ( persistedPeriod == null )
-            {
-                periodService.addPeriod( period );
-                persistedPeriod = periodService.getPeriod( period.getStartDate(), period.getEndDate(),
-                    dataSet.getPeriodType() );
-            }
+        Period period = PeriodUtil.getPeriod( periodName, periodType );
+
+        if ( period == null )
+        {
+            return null;
+        }
+
+        Period persistedPeriod = periodService.getPeriod( period.getStartDate(), period.getEndDate(), periodType );
+
+        if ( persistedPeriod == null )
+        {
+            periodService.addPeriod( period );
+            persistedPeriod = periodService.getPeriod( period.getStartDate(), period.getEndDate(), periodType );
         }
 
         return persistedPeriod;

=== added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/utils/PeriodUtil.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/utils/PeriodUtil.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/utils/PeriodUtil.java	2010-11-30 19:36:40 +0000
@@ -0,0 +1,116 @@
+package org.hisp.dhis.web.api.utils;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+
+import org.hisp.dhis.period.DailyPeriodType;
+import org.hisp.dhis.period.MonthlyPeriodType;
+import org.hisp.dhis.period.Period;
+import org.hisp.dhis.period.PeriodType;
+import org.hisp.dhis.period.QuarterlyPeriodType;
+import org.hisp.dhis.period.WeeklyPeriodType;
+import org.hisp.dhis.period.YearlyPeriodType;
+
+public class PeriodUtil
+{
+    public static Period getPeriod( String periodName, PeriodType periodType ) throws IllegalArgumentException
+    {
+
+        if ( periodType instanceof DailyPeriodType )
+        {
+            String pattern = "yyyy-MM-dd";
+            SimpleDateFormat formatter = new SimpleDateFormat( pattern );
+            Date date;
+            try
+            {
+                date = formatter.parse( periodName );
+            }
+            catch ( ParseException e )
+            {
+                throw new IllegalArgumentException( "Couldn't make a period of type " + periodType.getName()
+                    + " and name " + periodName, e );
+            }
+            DailyPeriodType dailyPeriodType = new DailyPeriodType();
+            return dailyPeriodType.createPeriod( date );
+
+        }
+
+        if ( periodType instanceof WeeklyPeriodType )
+        {
+            int week = Integer.parseInt( periodName.substring( 0, periodName.indexOf( '-' ) ) );
+            int year = Integer.parseInt( periodName.substring( periodName.indexOf( '-' ) + 1, periodName.length() ) );
+
+            Calendar cal = Calendar.getInstance();
+            cal.set( Calendar.YEAR, year );
+            cal.set( Calendar.WEEK_OF_YEAR, week );
+            cal.setFirstDayOfWeek( Calendar.MONDAY );
+
+            WeeklyPeriodType weeklyPeriodType = new WeeklyPeriodType();
+            return weeklyPeriodType.createPeriod( cal.getTime() );
+        }
+
+        if ( periodType instanceof MonthlyPeriodType )
+        {
+            int month = Integer.parseInt( periodName.substring( 0, periodName.indexOf( '-' ) ) );
+            int year = Integer.parseInt( periodName.substring( periodName.indexOf( '-' ) + 1, periodName.length() ) );
+
+            Calendar cal = Calendar.getInstance();
+            cal.set( Calendar.YEAR, year );
+            cal.set( Calendar.MONTH, month );
+
+            MonthlyPeriodType monthlyPeriodType = new MonthlyPeriodType();
+            return monthlyPeriodType.createPeriod( cal.getTime() );
+        }
+
+        if ( periodType instanceof YearlyPeriodType )
+        {
+            Calendar cal = Calendar.getInstance();
+            cal.set( Calendar.YEAR, Integer.parseInt( periodName ) );
+
+            YearlyPeriodType yearlyPeriodType = new YearlyPeriodType();
+
+            return yearlyPeriodType.createPeriod( cal.getTime() );
+        }
+
+        if ( periodType instanceof QuarterlyPeriodType )
+        {
+            Calendar cal = Calendar.getInstance();
+
+            int month = 0;
+            if ( periodName.substring( 0, periodName.indexOf( " " ) ).equals( "Jan" ) )
+            {
+                month = 1;
+            }
+            else if ( periodName.substring( 0, periodName.indexOf( " " ) ).equals( "Apr" ) )
+            {
+                month = 4;
+            }
+            else if ( periodName.substring( 0, periodName.indexOf( " " ) ).equals( "Jul" ) )
+            {
+                month = 6;
+            }
+            else if ( periodName.substring( 0, periodName.indexOf( " " ) ).equals( "Oct" ) )
+            {
+                month = 10;
+            }
+
+            int year = Integer.parseInt( periodName.substring( periodName.lastIndexOf( " " ) + 1 ) );
+
+            cal.set( Calendar.MONTH, month );
+            cal.set( Calendar.YEAR, year );
+
+            QuarterlyPeriodType quarterlyPeriodType = new QuarterlyPeriodType();
+            if ( month != 0 )
+            {
+                return quarterlyPeriodType.createPeriod( cal.getTime() );
+            }
+
+        }
+
+        throw new IllegalArgumentException( "Couldn't make a period of type " + periodType.getName() + " and name "
+            + periodName );
+    }
+
+}