← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17072: implemented bulk complete registrations

 

------------------------------------------------------------
revno: 17072
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-10-13 19:24:47 +0700
message:
  implemented bulk complete registrations
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/CompleteDataSetRegistration.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/Period.java
  dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/dataset/hibernate/CompleteDataSetRegistration.hbm.xml
  dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/period/hibernate/Period.hbm.xml
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/CompleteDataSetRegistrationController.java


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

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/CompleteDataSetRegistration.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/CompleteDataSetRegistration.java	2014-10-13 05:01:18 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/CompleteDataSetRegistration.java	2014-10-13 12:24:47 +0000
@@ -201,7 +201,6 @@
     }
 
     @JsonProperty
-    @JsonSerialize( as = BaseIdentifiableObject.class )
     @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public Period getPeriod()
     {
@@ -215,7 +214,7 @@
 
     @JsonProperty( value = "organisationUnit" )
     @JsonSerialize( as = BaseIdentifiableObject.class )
-    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
+    @JacksonXmlProperty( localName = "organisationUnit", namespace = DxfNamespaces.DXF_2_0 )
     public OrganisationUnit getSource()
     {
         return source;

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/Period.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/Period.java	2014-09-22 06:49:26 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/Period.java	2014-10-13 12:24:47 +0000
@@ -28,9 +28,12 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.text.SimpleDateFormat;
-import java.util.Date;
-
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonView;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
 import org.hisp.dhis.common.BaseNameableObject;
 import org.hisp.dhis.common.DxfNamespaces;
 import org.hisp.dhis.common.Weighted;
@@ -41,17 +44,13 @@
 import org.joda.time.DateTime;
 import org.joda.time.Days;
 
-import com.fasterxml.jackson.annotation.JsonProperty;
-import com.fasterxml.jackson.annotation.JsonView;
-import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
-import com.fasterxml.jackson.databind.annotation.JsonSerialize;
-import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
-import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 
 /**
  * @author Kristian Nordal
  */
-@JacksonXmlRootElement( localName = "period", namespace = DxfNamespaces.DXF_2_0)
+@JacksonXmlRootElement( localName = "period", namespace = DxfNamespaces.DXF_2_0 )
 public class Period
     extends BaseNameableObject
     implements Weighted
@@ -77,7 +76,7 @@
      * Required. Must be unique together with startDate.
      */
     private Date endDate;
-    
+
     /**
      * Transient string holding the ISO representation of the period.
      */
@@ -126,12 +125,17 @@
         return getIsoDate();
     }
 
+    public String getRealUid()
+    {
+        return uid;
+    }
+
     @Override
     public String getCode()
     {
         return getIsoDate();
     }
-    
+
     @Override
     public String getName()
     {
@@ -195,7 +199,7 @@
     {
         return getMediumDateString( startDate );
     }
-    
+
     /**
      * Returns end date formatted as string.
      *
@@ -227,7 +231,7 @@
      *
      * @param type the period type.
      * @return the potential number of periods of the given period type spanned
-     *         by this period.
+     * by this period.
      */
     public int getPeriodSpan( PeriodType type )
     {
@@ -239,7 +243,7 @@
     /**
      * Returns the number of days in the period, i.e. the days between the start
      * and end date.
-     * 
+     *
      * @return number of days in period.
      */
     public int getDaysInPeriod()
@@ -247,7 +251,7 @@
         Days days = Days.daysBetween( new DateTime( startDate ), new DateTime( endDate ) );
         return days.getDays() + 1;
     }
-    
+
     /**
      * Validates this period. TODO Make more comprehensive.
      */
@@ -257,15 +261,15 @@
         {
             return false;
         }
-        
+
         if ( !DailyPeriodType.NAME.equals( periodType.getName() ) && getDaysInPeriod() < 2 )
         {
             return false;
         }
-        
+
         return true;
     }
-    
+
     // -------------------------------------------------------------------------
     // hashCode, equals and toString
     // -------------------------------------------------------------------------
@@ -319,7 +323,21 @@
     // -------------------------------------------------------------------------
 
     @JsonProperty
-    @JsonView( {DetailedView.class, ExportView.class} )
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
+    public Date getStartDate()
+    {
+        return startDate;
+    }
+
+    public void setStartDate( Date startDate )
+    {
+        this.startDate = startDate;
+    }
+
+    @JsonProperty
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public Date getEndDate()
     {
         return endDate;
@@ -333,8 +351,7 @@
     @JsonProperty
     @JsonSerialize( using = JacksonPeriodTypeSerializer.class )
     @JsonDeserialize( using = JacksonPeriodTypeDeserializer.class )
-    @JsonView( {DetailedView.class, ExportView.class} )
-    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0)
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public PeriodType getPeriodType()
     {
         return periodType;
@@ -344,16 +361,4 @@
     {
         this.periodType = periodType;
     }
-
-    @JsonProperty
-    @JsonView( {DetailedView.class, ExportView.class} )
-    public Date getStartDate()
-    {
-        return startDate;
-    }
-
-    public void setStartDate( Date startDate )
-    {
-        this.startDate = startDate;
-    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/dataset/hibernate/CompleteDataSetRegistration.hbm.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/dataset/hibernate/CompleteDataSetRegistration.hbm.xml	2014-06-23 19:13:26 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/dataset/hibernate/CompleteDataSetRegistration.hbm.xml	2014-10-13 12:24:47 +0000
@@ -13,12 +13,13 @@
         foreign-key="fk_datasetcompleteregistration_periodid" />
       <key-many-to-one name="source" class="org.hisp.dhis.organisationunit.OrganisationUnit" column="sourceid"
         foreign-key="fk_completedatasetregistration_organisationunitid" />
-      <key-many-to-one  name="attributeOptionCombo" class="org.hisp.dhis.dataelement.DataElementCategoryOptionCombo" column="attributeoptioncomboid" 
+      <key-many-to-one name="attributeOptionCombo" class="org.hisp.dhis.dataelement.DataElementCategoryOptionCombo"
+        column="attributeoptioncomboid"
         foreign-key="fk_completedatasetregistration_attributeoptioncomboid" />
     </composite-id>
 
     <property name="date" column="date" type="timestamp" />
-	<property name="storedBy" column="storedby" />
+    <property name="storedBy" column="storedby" />
 
   </class>
 </hibernate-mapping>

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/period/hibernate/Period.hbm.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/period/hibernate/Period.hbm.xml	2011-05-28 21:25:46 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/period/hibernate/Period.hbm.xml	2014-10-13 12:24:47 +0000
@@ -11,6 +11,7 @@
     <id name="id" column="periodid">
       <generator class="native" />
     </id>
+
     <properties name="uniqueGroup" unique="true">
       <many-to-one name="periodType" class="org.hisp.dhis.period.PeriodType" lazy="false" column="periodtypeid"
         foreign-key="fk_period_periodtypeid" />

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/CompleteDataSetRegistrationController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/CompleteDataSetRegistrationController.java	2014-10-13 05:01:18 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/CompleteDataSetRegistrationController.java	2014-10-13 12:24:47 +0000
@@ -29,6 +29,7 @@
  */
 
 import org.apache.commons.lang.StringUtils;
+import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.common.view.BasicView;
 import org.hisp.dhis.datacompletion.CompleteDataSetRegistrationRequest;
@@ -53,18 +54,22 @@
 import org.hisp.dhis.webapi.utils.InputUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.format.annotation.DateTimeFormat;
+import org.springframework.http.HttpStatus;
 import org.springframework.http.MediaType;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.client.HttpClientErrorException;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Calendar;
 import java.util.Date;
+import java.util.GregorianCalendar;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -140,6 +145,7 @@
         response.setContentType( CONTENT_TYPE_JSON );
         CompleteDataSetRegistrations completeDataSetRegistrations = getCompleteDataSetRegistrations( dataSet, period,
             startDate, endDate, orgUnit, children );
+
         JacksonUtils.toJsonWithView( response.getOutputStream(), completeDataSetRegistrations, BasicView.class );
     }
 
@@ -199,6 +205,44 @@
 
     private void saveCompleteDataSetRegistrations( CompleteDataSetRegistrations completeDataSetRegistrations )
     {
+        for ( CompleteDataSetRegistration completeDataSetRegistration : completeDataSetRegistrations.getCompleteDataSetRegistrations() )
+        {
+            Period period = getObject( completeDataSetRegistration.getPeriod() );
+            DataSet dataSet = getObject( completeDataSetRegistration.getDataSet() );
+            DataElementCategoryOptionCombo attributeOptionCombo = getObject( completeDataSetRegistration.getAttributeOptionCombo() );
+            OrganisationUnit organisationUnit = getObject( completeDataSetRegistration.getSource() );
+
+            Date date = completeDataSetRegistration.getDate();
+            String storedBy = completeDataSetRegistration.getStoredBy();
+
+            registerCompleteDataSet( dataSet, period, organisationUnit, attributeOptionCombo, storedBy, date );
+        }
+    }
+
+    @SuppressWarnings( "unchecked" )
+    private <TT extends IdentifiableObject> TT getObject( TT object )
+    {
+        if ( object == null )
+        {
+            return null;
+        }
+
+        if ( Period.class.isInstance( object ) )
+        {
+            return (TT) periodService.reloadIsoPeriod( ((Period) object).getRealUid() );
+        }
+
+        if ( object.getUid() != null )
+        {
+            return (TT) manager.get( object.getClass(), object.getUid() );
+        }
+
+        if ( object.getCode() != null )
+        {
+            return (TT) manager.get( object.getClass(), object.getCode() );
+        }
+
+        return null;
     }
 
     @RequestMapping( method = RequestMethod.POST, produces = "text/plain" )
@@ -329,8 +373,7 @@
 
             String cc = completeDataSetRegistrationRequest.getCc();
             String cp = completeDataSetRegistrationRequest.getCp();
-            DataElementCategoryOptionCombo attributeOptionCombo = inputUtils
-                .getAttributeOptionCombo( response, cc, cp );
+            DataElementCategoryOptionCombo attributeOptionCombo = inputUtils.getAttributeOptionCombo( response, cc, cp );
 
             if ( attributeOptionCombo == null )
             {
@@ -465,29 +508,63 @@
     // -------------------------------------------------------------------------
     // Supportive methods
     // -------------------------------------------------------------------------
+
     private CompleteDataSetRegistration registerCompleteDataSet( DataSet dataSet, Period period,
-        OrganisationUnit orgUnit, DataElementCategoryOptionCombo attributeOptionCombo, String storedBy,
-        Date completionDate )
+        OrganisationUnit orgUnit, DataElementCategoryOptionCombo attributeOptionCombo, String storedBy, Date completionDate )
     {
         I18nFormat format = i18nManager.getI18nFormat();
 
-        if ( registrationService.getCompleteDataSetRegistration( dataSet, period, orgUnit, attributeOptionCombo ) ==
-            null )
-        {
-            CompleteDataSetRegistration registration = new CompleteDataSetRegistration();
+        if ( dataSet == null )
+        {
+            throw new HttpClientErrorException( HttpStatus.BAD_REQUEST, "DataSet can not be null." );
+        }
+
+        if ( period == null )
+        {
+            throw new HttpClientErrorException( HttpStatus.BAD_REQUEST, "period can not be null." );
+        }
+
+        if ( orgUnit == null )
+        {
+            throw new HttpClientErrorException( HttpStatus.BAD_REQUEST, "organisationUnit can not be null" );
+        }
+
+        if ( attributeOptionCombo == null )
+        {
+            throw new HttpClientErrorException( HttpStatus.BAD_REQUEST, "attributeOptionCombo can not be null" );
+        }
+
+        CompleteDataSetRegistration registration = registrationService.getCompleteDataSetRegistration( dataSet, period,
+            orgUnit, attributeOptionCombo );
+
+        if ( registration == null )
+        {
+            registration = new CompleteDataSetRegistration();
 
             registration.setDataSet( dataSet );
             registration.setPeriod( period );
             registration.setSource( orgUnit );
             registration.setAttributeOptionCombo( attributeOptionCombo );
-            registration.setDate( completionDate );
-            registration.setStoredBy( storedBy );
-            registration.setPeriodName( format.formatPeriod( registration.getPeriod() ) );
-
-            return registration;
-        }
-
-        return null;
+
+            registration.setDate( completionDate != null ? completionDate : new Date() );
+            registration.setStoredBy( storedBy != null ? storedBy : currentUserService.getCurrentUsername() );
+            registration.setPeriodName( format.formatPeriod( registration.getPeriod() ) );
+
+            System.err.println( "saveCompleteDataSetRegistration" );
+            registrationService.saveCompleteDataSetRegistration( registration );
+        }
+        else
+        {
+            registration.setDate( completionDate != null ? completionDate : new Date() );
+            registration.setStoredBy( storedBy != null ? storedBy : currentUserService.getCurrentUsername() );
+            registration.setPeriodName( format.formatPeriod( registration.getPeriod() ) );
+
+            System.err.println( "updateCompleteDataSetRegistration" );
+            registrationService.updateCompleteDataSetRegistration( registration );
+        }
+
+
+        return registration;
     }
 
     private void unRegisterCompleteDataSet( Set<DataSet> dataSets, Period period,