← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15097: minor cleanup in eventService, support importOptions (scheme) for updates

 

------------------------------------------------------------
revno: 15097
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-04-29 16:56:30 +0545
message:
  minor cleanup in eventService, support importOptions (scheme) for updates
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/NoRegistrationSingleEventServiceTest.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/EventController.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-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java	2014-04-18 10:14:28 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java	2014-04-29 11:11:30 +0000
@@ -275,29 +275,7 @@
             }
         }
 
-        OrganisationUnit organisationUnit = null;
-
-        if ( IdentifiableObject.IdentifiableProperty.UUID.equals( importOptions.getOrgUnitIdScheme() ) )
-        {
-            organisationUnit = organisationUnitService.getOrganisationUnitByUuid( event.getOrgUnit() );
-        }
-        else if ( IdentifiableObject.IdentifiableProperty.CODE.equals( importOptions.getOrgUnitIdScheme() ) )
-        {
-            organisationUnit = organisationUnitService.getOrganisationUnitByCode( event.getOrgUnit() );
-        }
-        else if ( IdentifiableObject.IdentifiableProperty.NAME.equals( importOptions.getOrgUnitIdScheme() ) )
-        {
-            List<OrganisationUnit> organisationUnitByName = organisationUnitService.getOrganisationUnitByName( event.getOrgUnit() );
-
-            if ( organisationUnitByName.size() == 1 )
-            {
-                organisationUnit = organisationUnitByName.get( 0 );
-            }
-        }
-        else
-        {
-            organisationUnit = organisationUnitService.getOrganisationUnit( event.getOrgUnit() );
-        }
+        OrganisationUnit organisationUnit = getOrganisationUnit( importOptions.getOrgUnitIdScheme(), event.getOrgUnit() );
 
         if ( organisationUnit == null )
         {
@@ -321,12 +299,12 @@
     {
         return getEvents( program, null, null, null, Arrays.asList( organisationUnit ), null, null, null, null );
     }
-    
+
     @Override
-    public Events getEvents( Program program, ProgramStage programStage, ProgramStatus programStatus, Boolean followUp, List<OrganisationUnit> organisationUnits, 
+    public Events getEvents( Program program, ProgramStage programStage, ProgramStatus programStatus, Boolean followUp, List<OrganisationUnit> organisationUnits,
         TrackedEntityInstance trackedEntityInstance, Date startDate, Date endDate, EventStatus status )
     {
-        List<Event> eventList = eventStore.getAll( program, programStage, programStatus, followUp, organisationUnits, 
+        List<Event> eventList = eventStore.getAll( program, programStage, programStatus, followUp, organisationUnits,
             trackedEntityInstance, startDate, endDate, status );
         Events events = new Events();
         events.setEvents( eventList );
@@ -355,21 +333,27 @@
     @Override
     public void updateEvent( Event event, boolean singleValue )
     {
-        ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance( event
-            .getEvent() );
+        updateEvent( event, singleValue, null );
+    }
+
+    @Override
+    public void updateEvent( Event event, boolean singleValue, ImportOptions importOptions )
+    {
+        ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance( event.getEvent() );
 
         if ( programStageInstance == null )
         {
             return;
         }
 
-        OrganisationUnit organisationUnit;
-
-        if ( event.getOrgUnit() != null )
+        if ( importOptions == null )
         {
-            organisationUnit = organisationUnitService.getOrganisationUnit( event.getOrgUnit() );
+            importOptions = new ImportOptions();
         }
-        else
+
+        OrganisationUnit organisationUnit = getOrganisationUnit( importOptions.getOrgUnitIdScheme(), event.getOrgUnit() );
+
+        if ( organisationUnit == null )
         {
             organisationUnit = programStageInstance.getOrganisationUnit();
         }
@@ -793,4 +777,33 @@
             programInstanceService.updateProgramInstance( programInstance );
         }
     }
+
+    private OrganisationUnit getOrganisationUnit( IdentifiableObject.IdentifiableProperty scheme, String value )
+    {
+        OrganisationUnit organisationUnit = null;
+
+        if ( IdentifiableObject.IdentifiableProperty.UUID.equals( scheme ) )
+        {
+            organisationUnit = organisationUnitService.getOrganisationUnitByUuid( value );
+        }
+        else if ( IdentifiableObject.IdentifiableProperty.CODE.equals( scheme ) )
+        {
+            organisationUnit = organisationUnitService.getOrganisationUnitByCode( value );
+        }
+        else if ( IdentifiableObject.IdentifiableProperty.NAME.equals( scheme ) )
+        {
+            List<OrganisationUnit> organisationUnitByName = organisationUnitService.getOrganisationUnitByName( value );
+
+            if ( organisationUnitByName.size() == 1 )
+            {
+                organisationUnit = organisationUnitByName.get( 0 );
+            }
+        }
+        else
+        {
+            organisationUnit = organisationUnitService.getOrganisationUnit( value );
+        }
+
+        return organisationUnit;
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventService.java	2014-04-18 10:14:28 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/EventService.java	2014-04-29 11:11:30 +0000
@@ -28,11 +28,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Date;
-import java.util.List;
-
 import org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance;
 import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
@@ -45,6 +40,11 @@
 import org.hisp.dhis.program.ProgramStatus;
 import org.hisp.dhis.scheduling.TaskId;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Date;
+import java.util.List;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
@@ -55,8 +55,8 @@
     // -------------------------------------------------------------------------
 
     Events getEvents( Program program, OrganisationUnit organisationUnit );
-    
-    Events getEvents( Program program, ProgramStage programStage, ProgramStatus programStatus, Boolean followUp, List<OrganisationUnit> organisationUnit, 
+
+    Events getEvents( Program program, ProgramStage programStage, ProgramStatus programStatus, Boolean followUp, List<OrganisationUnit> organisationUnit,
         TrackedEntityInstance trackedEntityInstance, Date startDate, Date endDate, EventStatus status );
 
     Event getEvent( String uid );
@@ -97,6 +97,8 @@
 
     void updateEvent( Event event, boolean singleValue );
 
+    void updateEvent( Event event, boolean singleValue, ImportOptions importOptions );
+
     // -------------------------------------------------------------------------
     // DELETE
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/NoRegistrationSingleEventServiceTest.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/NoRegistrationSingleEventServiceTest.java	2014-04-04 20:46:36 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/NoRegistrationSingleEventServiceTest.java	2014-04-29 11:11:30 +0000
@@ -183,7 +183,7 @@
 
         event = eventService.getEvent( importSummary.getReference() );
         event.getDataValues().get( 0 ).setValue( "254" );
-        eventService.updateEvent( event, false );
+        eventService.updateEvent( event, false, importOptions );
 
         event = eventService.getEvent( importSummary.getReference() );
         assertEquals( "254", event.getDataValues().get( 0 ).getValue() );

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/EventController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/EventController.java	2014-04-18 15:26:30 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/EventController.java	2014-04-29 11:11:30 +0000
@@ -89,7 +89,7 @@
 public class EventController
 {
     public static final String RESOURCE_PATH = "/events";
-    
+
     private static final String META_DATA_KEY_DE = "de";
 
     //--------------------------------------------------------------------------
@@ -113,7 +113,7 @@
 
     @Autowired
     private OrganisationUnitService organisationUnitService;
-    
+
     @Autowired
     private DataElementService dataElementService;
 
@@ -138,7 +138,7 @@
         @RequestParam Map<String, String> parameters, Model model, HttpServletRequest request )
     {
         WebOptions options = new WebOptions( parameters );
-        
+
         Program pr = manager.get( Program.class, program );
         ProgramStage prs = manager.get( ProgramStage.class, programStage );
         List<OrganisationUnit> organisationUnits = new ArrayList<OrganisationUnit>();
@@ -173,7 +173,7 @@
         }
 
         Events events = eventService.getEvents( pr, prs, programStatus, followUp, organisationUnits, tei, startDate, endDate, status );
-        
+
         if ( options.hasLinks() )
         {
             for ( Event event : events.getEvents() )
@@ -181,19 +181,19 @@
                 event.setHref( ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + event.getEvent() );
             }
         }
-        
+
         if ( options.hasPaging() )
-        {      	
+        {
             Pager pager = new Pager( options.getPage(), events.getEvents().size(), options.getPageSize() );
             events.setPager( pager );
-            events.setEvents( PagerUtils.pageCollection( events.getEvents(), pager ) );        	
-        }        
-        
+            events.setEvents( PagerUtils.pageCollection( events.getEvents(), pager ) );
+        }
+
         if ( !skipMeta && pr != null )
         {
             events.setMetaData( getMetaData( pr ) );
         }
-        
+
         model.addAttribute( "model", events );
         model.addAttribute( "viewClass", options.getViewClass( "detailed" ) );
 
@@ -228,11 +228,11 @@
     private Map<Object, Object> getMetaData( Program program )
     {
         Map<Object, Object> metaData = new HashMap<Object, Object>();
-        
+
         if ( program != null )
         {
             Map<String, String> dataElements = new HashMap<String, String>();
-            
+
             for ( DataElement de : program.getAllDataElements() )
             {
                 dataElements.put( de.getUid(), de.getDisplayName() );
@@ -240,10 +240,10 @@
 
             metaData.put( META_DATA_KEY_DE, dataElements );
         }
-        
+
         return metaData;
     }
-    
+
     // -------------------------------------------------------------------------
     // CREATE
     // -------------------------------------------------------------------------
@@ -344,7 +344,7 @@
 
     @RequestMapping( value = "/{uid}", method = RequestMethod.PUT, consumes = { "application/xml", "text/xml" } )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_TRACKED_ENTITY_DATAVALUE_ADD')" )
-    public void putXmlEvent( HttpServletResponse response, HttpServletRequest request, @PathVariable( "uid" ) String uid ) throws IOException
+    public void putXmlEvent( HttpServletResponse response, HttpServletRequest request, @PathVariable( "uid" ) String uid, ImportOptions importOptions ) throws IOException
     {
         Event event = eventService.getEvent( uid );
 
@@ -357,13 +357,13 @@
         Event updatedEvent = JacksonUtils.fromXml( request.getInputStream(), Event.class );
         updatedEvent.setEvent( uid );
 
-        eventService.updateEvent( updatedEvent, false );
+        eventService.updateEvent( updatedEvent, false, importOptions );
         ContextUtils.okResponse( response, "Event updated: " + uid );
     }
 
     @RequestMapping( value = "/{uid}", method = RequestMethod.PUT, consumes = "application/json" )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_TRACKED_ENTITY_DATAVALUE_ADD')" )
-    public void putJsonEvent( HttpServletResponse response, HttpServletRequest request, @PathVariable( "uid" ) String uid ) throws IOException
+    public void putJsonEvent( HttpServletResponse response, HttpServletRequest request, @PathVariable( "uid" ) String uid, ImportOptions importOptions ) throws IOException
     {
         Event event = eventService.getEvent( uid );
 
@@ -376,10 +376,10 @@
         Event updatedEvent = JacksonUtils.fromJson( request.getInputStream(), Event.class );
         updatedEvent.setEvent( uid );
 
-        eventService.updateEvent( updatedEvent, false );
+        eventService.updateEvent( updatedEvent, false, importOptions );
         ContextUtils.okResponse( response, "Event updated: " + uid );
     }
-    
+
     @RequestMapping( value = "/{uid}/{dataElementUid}", method = RequestMethod.PUT, consumes = "application/json" )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_TRACKED_ENTITY_DATAVALUE_ADD')" )
     public void putJsonEventSingleValue( HttpServletResponse response, HttpServletRequest request, @PathVariable( "uid" ) String uid, @PathVariable( "dataElementUid" ) String dataElementUid ) throws IOException
@@ -391,9 +391,9 @@
             ContextUtils.notFoundResponse( response, "Event not found for uid: " + uid );
             return;
         }
-        
+
         DataElement dataElement = dataElementService.getDataElement( dataElementUid );
-        
+
         if( dataElement == null )
         {
             ContextUtils.notFoundResponse( response, "DataElement not found for uid: " + dataElementUid );
@@ -404,8 +404,8 @@
         updatedEvent.setEvent( uid );
 
         eventService.updateEvent( updatedEvent, true );
-        ContextUtils.okResponse( response, "Event updated: " + uid );        
-        
+        ContextUtils.okResponse( response, "Event updated: " + uid );
+
     }
 
     // -------------------------------------------------------------------------