← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16959: add hooks in AbstractCrudController for pre-create and pre-update of entity

 

------------------------------------------------------------
revno: 16959
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-10-06 12:18:37 +0700
message:
  add hooks in AbstractCrudController for pre-create and pre-update of entity
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.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-10-02 10:36:25 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java	2014-10-06 05:18:37 +0000
@@ -130,7 +130,7 @@
     // GET
     //--------------------------------------------------------------------------
 
-    @RequestMapping( method = RequestMethod.GET )
+    @RequestMapping(method = RequestMethod.GET)
     public @ResponseBody RootNode getObjectList(
         @RequestParam Map<String, String> parameters, HttpServletResponse response, HttpServletRequest request )
     {
@@ -251,9 +251,9 @@
         return rootNode;
     }
 
-    @RequestMapping( value = "/{uid}", method = RequestMethod.GET )
+    @RequestMapping(value = "/{uid}", method = RequestMethod.GET)
     public @ResponseBody RootNode getObject(
-        @PathVariable( "uid" ) String pvUid,
+        @PathVariable("uid") String pvUid,
         @RequestParam Map<String, String> parameters,
         HttpServletRequest request, HttpServletResponse response ) throws Exception
     {
@@ -268,10 +268,10 @@
         return getObjectInternal( pvUid, parameters, filters, fields );
     }
 
-    @RequestMapping( value = "/{uid}/{property}", method = RequestMethod.GET )
+    @RequestMapping(value = "/{uid}/{property}", method = RequestMethod.GET)
     public @ResponseBody RootNode getObjectProperty(
-        @PathVariable( "uid" ) String uid,
-        @PathVariable( "property" ) String pvProperty,
+        @PathVariable("uid") String uid,
+        @PathVariable("property") String pvProperty,
         @RequestParam Map<String, String> parameters, HttpServletRequest request, HttpServletResponse response ) throws Exception
     {
         return getObjectInternal( uid, parameters, Lists.<String>newArrayList(), Lists.newArrayList( pvProperty + "[:all]" ) );
@@ -335,7 +335,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() ) )
@@ -344,6 +344,9 @@
         }
 
         T parsed = renderService.fromXml( request.getInputStream(), getEntityClass() );
+
+        preCreateEntity( parsed );
+
         ImportTypeSummary summary = importService.importObject( currentUserService.getCurrentUser().getUid(), parsed, ImportStrategy.CREATE );
 
         if ( ImportStatus.SUCCESS.equals( summary.getStatus() ) )
@@ -360,7 +363,7 @@
         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() ) )
@@ -369,6 +372,9 @@
         }
 
         T parsed = renderService.fromJson( request.getInputStream(), getEntityClass() );
+
+        preCreateEntity( parsed );
+
         ImportTypeSummary summary = importService.importObject( currentUserService.getCurrentUser().getUid(), parsed, ImportStrategy.CREATE );
 
         if ( ImportStatus.SUCCESS.equals( summary.getStatus() ) )
@@ -389,9 +395,8 @@
     // 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 })
+    public void putXmlObject( HttpServletResponse response, HttpServletRequest request, @PathVariable("uid") String uid, InputStream
         input ) throws Exception
     {
         List<T> objects = getEntity( uid );
@@ -410,6 +415,8 @@
         T parsed = renderService.fromXml( request.getInputStream(), getEntityClass() );
         ((BaseIdentifiableObject) parsed).setUid( uid );
 
+        preUpdateEntity( parsed );
+
         ImportTypeSummary summary = importService.importObject( currentUserService.getCurrentUser().getUid(), parsed, ImportStrategy.UPDATE );
 
         if ( ImportStatus.SUCCESS.equals( summary.getStatus() ) )
@@ -420,9 +427,8 @@
         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)
+    public void putJsonObject( HttpServletResponse response, HttpServletRequest request, @PathVariable("uid") String uid, InputStream
         input ) throws Exception
     {
         List<T> objects = getEntity( uid );
@@ -441,6 +447,8 @@
         T parsed = renderService.fromJson( request.getInputStream(), getEntityClass() );
         ((BaseIdentifiableObject) parsed).setUid( uid );
 
+        preUpdateEntity( parsed );
+
         ImportTypeSummary summary = importService.importObject( currentUserService.getCurrentUser().getUid(), parsed, ImportStrategy.UPDATE );
 
         if ( ImportStatus.SUCCESS.equals( summary.getStatus() ) )
@@ -455,9 +463,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 )
+    @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 );
@@ -480,11 +488,11 @@
     // Identifiable object collections add, delete
     //--------------------------------------------------------------------------
 
-    @RequestMapping( value = "/{uid}/{property}/{itemId}", method = RequestMethod.GET )
+    @RequestMapping(value = "/{uid}/{property}/{itemId}", method = RequestMethod.GET)
     public @ResponseBody RootNode getCollectionItem(
-        @PathVariable( "uid" ) String pvUid,
-        @PathVariable( "property" ) String pvProperty,
-        @PathVariable( "itemId" ) String pvItemId,
+        @PathVariable("uid") String pvUid,
+        @PathVariable("property") String pvProperty,
+        @PathVariable("itemId") String pvItemId,
         @RequestParam Map<String, String> parameters, HttpServletResponse response ) throws Exception
     {
         RootNode rootNode = getObjectInternal( pvUid, parameters, Lists.<String>newArrayList(), Lists.newArrayList( pvProperty + "[:all]" ) );
@@ -513,13 +521,13 @@
         return rootNode;
     }
 
-    @RequestMapping( value = "/{uid}/{property}/{itemId}", method = { RequestMethod.POST, RequestMethod.PUT } )
-    @ResponseStatus( value = HttpStatus.NO_CONTENT )
-    @SuppressWarnings( "unchecked" )
+    @RequestMapping(value = "/{uid}/{property}/{itemId}", method = { RequestMethod.POST, RequestMethod.PUT })
+    @ResponseStatus(value = HttpStatus.NO_CONTENT)
+    @SuppressWarnings("unchecked")
     public void addCollectionItem(
-        @PathVariable( "uid" ) String pvUid,
-        @PathVariable( "property" ) String pvProperty,
-        @PathVariable( "itemId" ) String pvItemId, HttpServletResponse response ) throws Exception
+        @PathVariable("uid") String pvUid,
+        @PathVariable("property") String pvProperty,
+        @PathVariable("itemId") String pvItemId, HttpServletResponse response ) throws Exception
     {
         List<T> objects = getEntity( pvUid );
 
@@ -571,13 +579,13 @@
         manager.update( objects.get( 0 ) );
     }
 
-    @RequestMapping( value = "/{uid}/{property}/{itemId}", method = RequestMethod.DELETE )
-    @ResponseStatus( value = HttpStatus.NO_CONTENT )
-    @SuppressWarnings( "unchecked" )
+    @RequestMapping(value = "/{uid}/{property}/{itemId}", method = RequestMethod.DELETE)
+    @ResponseStatus(value = HttpStatus.NO_CONTENT)
+    @SuppressWarnings("unchecked")
     public void deleteCollectionItem(
-        @PathVariable( "uid" ) String pvUid,
-        @PathVariable( "property" ) String pvProperty,
-        @PathVariable( "itemId" ) String pvItemId, HttpServletResponse response ) throws Exception
+        @PathVariable("uid") String pvUid,
+        @PathVariable("property") String pvProperty,
+        @PathVariable("itemId") String pvItemId, HttpServletResponse response ) throws Exception
     {
         List<T> objects = getEntity( pvUid );
 
@@ -675,12 +683,18 @@
     {
     }
 
-    // TODO replace with hooks directly in idManager
+    protected void preCreateEntity( T entity )
+    {
+    }
+
     protected void postCreateEntity( T entity )
     {
     }
 
-    // TODO replace with hooks directly in idManager
+    protected void preUpdateEntity( T entity )
+    {
+    }
+
     protected void postUpdateEntity( T entity )
     {
     }
@@ -783,7 +797,7 @@
 
     private String entitySimpleName;
 
-    @SuppressWarnings( "unchecked" )
+    @SuppressWarnings("unchecked")
     protected Class<T> getEntityClass()
     {
         if ( entityClass == null )
@@ -815,7 +829,7 @@
         return entitySimpleName;
     }
 
-    @SuppressWarnings( "unchecked" )
+    @SuppressWarnings("unchecked")
     protected T getEntityInstance()
     {
         try