dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #31334
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15953: add post create/update hooks for abstractCrudController, only run if create/update was successful...
------------------------------------------------------------
revno: 15953
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2014-07-03 14:59:24 +0700
message:
add post create/update hooks for abstractCrudController, only run if create/update was successful, add postCreateHook for program, create programInstance if type was single event and non-reg.
modified:
dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java
dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/ProgramController.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/webapi/controller/AbstractCrudController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java 2014-07-01 13:50:42 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java 2014-07-03 07:59:24 +0000
@@ -40,9 +40,10 @@
import org.hisp.dhis.common.Pager;
import org.hisp.dhis.common.PagerUtils;
import org.hisp.dhis.dxf2.fieldfilter.FieldFilterService;
-import org.hisp.dhis.dxf2.objectfilter.ObjectFilterService;
+import org.hisp.dhis.dxf2.importsummary.ImportStatus;
import org.hisp.dhis.dxf2.metadata.ImportService;
import org.hisp.dhis.dxf2.metadata.ImportTypeSummary;
+import org.hisp.dhis.dxf2.objectfilter.ObjectFilterService;
import org.hisp.dhis.dxf2.render.RenderService;
import org.hisp.dhis.hibernate.exception.CreateAccessDeniedException;
import org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException;
@@ -58,8 +59,8 @@
import org.hisp.dhis.user.CurrentUserService;
import org.hisp.dhis.webapi.controller.exception.NotFoundException;
import org.hisp.dhis.webapi.service.ContextService;
+import org.hisp.dhis.webapi.service.LinkService;
import org.hisp.dhis.webapi.utils.ContextUtils;
-import org.hisp.dhis.webapi.service.LinkService;
import org.hisp.dhis.webapi.webdomain.WebMetaData;
import org.hisp.dhis.webapi.webdomain.WebOptions;
import org.springframework.beans.factory.annotation.Autowired;
@@ -126,7 +127,7 @@
// GET
//--------------------------------------------------------------------------
- @RequestMapping(method = RequestMethod.GET)
+ @RequestMapping( method = RequestMethod.GET )
public @ResponseBody RootNode getObjectList(
@RequestParam Map<String, String> parameters, HttpServletResponse response, HttpServletRequest request )
{
@@ -320,7 +321,7 @@
// POST
//--------------------------------------------------------------------------
- @RequestMapping(method = RequestMethod.POST, consumes = { "application/xml", "text/xml" })
+ @RequestMapping( method = RequestMethod.POST, consumes = { "application/xml", "text/xml" } )
public void postXmlObject( HttpServletResponse response, HttpServletRequest request, InputStream input ) throws Exception
{
if ( !aclService.canCreate( currentUserService.getCurrentUser(), getEntityClass() ) )
@@ -330,10 +331,16 @@
T parsed = renderService.fromXml( request.getInputStream(), getEntityClass() );
ImportTypeSummary summary = importService.importObject( currentUserService.getCurrentUser().getUid(), parsed, ImportStrategy.CREATE );
+
+ if ( ImportStatus.SUCCESS.equals( summary.getStatus() ) )
+ {
+ postCreateEntity( parsed );
+ }
+
renderService.toXml( response.getOutputStream(), summary );
}
- @RequestMapping(method = RequestMethod.POST, consumes = "application/json")
+ @RequestMapping( method = RequestMethod.POST, consumes = "application/json" )
public void postJsonObject( HttpServletResponse response, HttpServletRequest request, InputStream input ) throws Exception
{
if ( !aclService.canCreate( currentUserService.getCurrentUser(), getEntityClass() ) )
@@ -343,6 +350,12 @@
T parsed = renderService.fromJson( request.getInputStream(), getEntityClass() );
ImportTypeSummary summary = importService.importObject( currentUserService.getCurrentUser().getUid(), parsed, ImportStrategy.CREATE );
+
+ if ( ImportStatus.SUCCESS.equals( summary.getStatus() ) )
+ {
+ postCreateEntity( parsed );
+ }
+
renderService.toJson( response.getOutputStream(), summary );
}
@@ -350,9 +363,9 @@
// PUT
//--------------------------------------------------------------------------
- @RequestMapping(value = "/{uid}", method = RequestMethod.PUT, consumes = { MediaType.APPLICATION_XML_VALUE, MediaType.TEXT_XML_VALUE })
- @ResponseStatus(value = HttpStatus.NO_CONTENT)
- public void putXmlObject( HttpServletResponse response, HttpServletRequest request, @PathVariable("uid") String uid, InputStream
+ @RequestMapping( value = "/{uid}", method = RequestMethod.PUT, consumes = { MediaType.APPLICATION_XML_VALUE, MediaType.TEXT_XML_VALUE } )
+ @ResponseStatus( value = HttpStatus.NO_CONTENT )
+ public void putXmlObject( HttpServletResponse response, HttpServletRequest request, @PathVariable( "uid" ) String uid, InputStream
input ) throws Exception
{
List<T> objects = getEntity( uid );
@@ -372,12 +385,18 @@
((BaseIdentifiableObject) parsed).setUid( uid );
ImportTypeSummary summary = importService.importObject( currentUserService.getCurrentUser().getUid(), parsed, ImportStrategy.UPDATE );
+
+ if ( ImportStatus.SUCCESS.equals( summary.getStatus() ) )
+ {
+ postUpdateEntity( parsed );
+ }
+
renderService.toXml( response.getOutputStream(), summary );
}
- @RequestMapping(value = "/{uid}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
- @ResponseStatus(value = HttpStatus.NO_CONTENT)
- public void putJsonObject( HttpServletResponse response, HttpServletRequest request, @PathVariable("uid") String uid, InputStream
+ @RequestMapping( value = "/{uid}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE )
+ @ResponseStatus( value = HttpStatus.NO_CONTENT )
+ public void putJsonObject( HttpServletResponse response, HttpServletRequest request, @PathVariable( "uid" ) String uid, InputStream
input ) throws Exception
{
List<T> objects = getEntity( uid );
@@ -397,6 +416,12 @@
((BaseIdentifiableObject) parsed).setUid( uid );
ImportTypeSummary summary = importService.importObject( currentUserService.getCurrentUser().getUid(), parsed, ImportStrategy.UPDATE );
+
+ if ( ImportStatus.SUCCESS.equals( summary.getStatus() ) )
+ {
+ postUpdateEntity( parsed );
+ }
+
renderService.toJson( response.getOutputStream(), summary );
}
@@ -404,9 +429,9 @@
// DELETE
//--------------------------------------------------------------------------
- @RequestMapping(value = "/{uid}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
- @ResponseStatus(value = HttpStatus.NO_CONTENT)
- public void deleteObject( HttpServletResponse response, HttpServletRequest request, @PathVariable("uid") String uid ) throws
+ @RequestMapping( value = "/{uid}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE )
+ @ResponseStatus( value = HttpStatus.NO_CONTENT )
+ public void deleteObject( HttpServletResponse response, HttpServletRequest request, @PathVariable( "uid" ) String uid ) throws
Exception
{
List<T> objects = getEntity( uid );
@@ -465,6 +490,16 @@
{
}
+ // TODO replace with hooks directly in idManager
+ protected void postCreateEntity( T entity )
+ {
+ }
+
+ // TODO replace with hooks directly in idManager
+ protected void postUpdateEntity( T entity )
+ {
+ }
+
//--------------------------------------------------------------------------
// Helpers
//--------------------------------------------------------------------------
@@ -556,7 +591,7 @@
private String entitySimpleName;
- @SuppressWarnings("unchecked")
+ @SuppressWarnings( "unchecked" )
protected Class<T> getEntityClass()
{
if ( entityClass == null )
@@ -588,7 +623,7 @@
return entitySimpleName;
}
- @SuppressWarnings("unchecked")
+ @SuppressWarnings( "unchecked" )
protected T getEntityInstance()
{
try
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/ProgramController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/ProgramController.java 2014-06-11 20:27:54 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/ProgramController.java 2014-07-03 07:59:24 +0000
@@ -32,14 +32,18 @@
import org.hisp.dhis.common.Pager;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.program.Program;
+import org.hisp.dhis.program.ProgramInstance;
+import org.hisp.dhis.program.ProgramInstanceService;
import org.hisp.dhis.schema.descriptors.ProgramSchemaDescriptor;
import org.hisp.dhis.webapi.controller.AbstractCrudController;
import org.hisp.dhis.webapi.webdomain.WebMetaData;
import org.hisp.dhis.webapi.webdomain.WebOptions;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList;
+import java.util.Date;
import java.util.Iterator;
import java.util.List;
@@ -51,6 +55,24 @@
public class ProgramController
extends AbstractCrudController<Program>
{
+ @Autowired
+ private ProgramInstanceService programInstanceService;
+
+ @Override
+ protected void postCreateEntity( Program program )
+ {
+ if ( program.isSingleEvent() && !program.isRegistration() )
+ {
+ ProgramInstance programInstance = new ProgramInstance();
+ programInstance.setEnrollmentDate( new Date() );
+ programInstance.setDateOfIncident( new Date() );
+ programInstance.setProgram( program );
+ programInstance.setStatus( ProgramInstance.STATUS_ACTIVE );
+
+ programInstanceService.addProgramInstance( programInstance );
+ }
+ }
+
protected List<Program> getEntityList( WebMetaData metaData, WebOptions options )
{
List<Program> entityList;