← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21665: remove option.links, instead check fields to see if href/access is requested, and add if required

 

------------------------------------------------------------
revno: 21665
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2016-01-08 21:23:25 +0700
message:
  remove option.links, instead check fields to see if href/access is requested, and add if required
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/dataelement/DataElementGroupController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/webdomain/WebOptions.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	2016-01-05 07:53:54 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java	2016-01-08 14:23:25 +0000
@@ -191,12 +191,7 @@
         postProcessEntities( entities );
         postProcessEntities( entities, options, rpParameters, translateParams );
 
-        if ( fields.contains( "access" ) )
-        {
-            options.getOptions().put( "viewClass", "sharing" );
-        }
-
-        handleLinksAndAccess( options, entities );
+        handleLinksAndAccess( entities, fields, false );
 
         linkService.generatePagerLinks( pager, getEntityClass() );
 
@@ -433,15 +428,7 @@
 
         entities = (List<T>) queryService.query( query );
 
-        if ( options.hasLinks() )
-        {
-            linkService.generateLinks( entities, true );
-        }
-
-        if ( aclService.isSupported( getEntityClass() ) )
-        {
-            addAccessProperties( entities );
-        }
+        handleLinksAndAccess( entities, fields, true );
 
         for ( T entity : entities )
         {
@@ -931,14 +918,40 @@
         }
     }
 
-    protected void handleLinksAndAccess( WebOptions options, List<T> entityList )
-    {
-        if ( options != null && options.hasLinks() )
-        {
-            linkService.generateLinks( entityList, false );
-        }
-
-        if ( entityList != null && aclService.isSupported( getEntityClass() ) )
+    private boolean fieldsContains( String match, List<String> fields )
+    {
+        for ( String field : fields )
+        {
+            if ( field.contains( match ) )
+            {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    protected boolean hasHref( List<String> fields )
+    {
+        return fieldsContains( "href", fields );
+    }
+
+    protected boolean hasAccess( List<String> fields )
+    {
+        return fieldsContains( "access", fields );
+    }
+
+    protected void handleLinksAndAccess( List<T> entityList, List<String> fields, boolean deep )
+    {
+        boolean generateLinks = hasHref( fields );
+        boolean generateAccess = hasAccess( fields );
+
+        if ( generateLinks )
+        {
+            linkService.generateLinks( entityList, deep );
+        }
+
+        if ( generateAccess && aclService.isSupported( getEntityClass() ) )
         {
             addAccessProperties( entityList );
         }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/dataelement/DataElementGroupController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/dataelement/DataElementGroupController.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/dataelement/DataElementGroupController.java	2016-01-08 14:23:25 +0000
@@ -100,11 +100,7 @@
         }
 
         metaData.setDataElementOperands( dataElementOperands );
-
-        if ( options.hasLinks() )
-        {
-            linkService.generateLinks( metaData, false );
-        }
+        linkService.generateLinks( metaData, false );
 
         model.addAttribute( "model", metaData );
         model.addAttribute( "viewClass", options.getViewClass( "basic" ) );
@@ -150,11 +146,7 @@
         }
 
         metaData.setDataElementOperands( dataElementOperands );
-
-        if ( options.hasLinks() )
-        {
-            linkService.generateLinks( metaData, false );
-        }
+        linkService.generateLinks( metaData, false );
 
         model.addAttribute( "model", metaData );
         model.addAttribute( "viewClass", options.getViewClass( "basic" ) );

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java	2016-01-04 10:49:18 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java	2016-01-08 14:23:25 +0000
@@ -28,13 +28,14 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import com.google.common.io.ByteSource;
 import org.apache.commons.io.IOUtils;
+import org.hisp.dhis.common.IdSchemes;
 import org.hisp.dhis.common.OrganisationUnitSelectionMode;
 import org.hisp.dhis.commons.util.StreamUtils;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.dataelement.DataElementService;
-import org.hisp.dhis.common.IdSchemes;
 import org.hisp.dhis.dxf2.common.ImportOptions;
 import org.hisp.dhis.dxf2.events.event.DataValue;
 import org.hisp.dhis.dxf2.events.event.Event;
@@ -55,6 +56,10 @@
 import org.hisp.dhis.dxf2.webmessage.WebMessageException;
 import org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse;
 import org.hisp.dhis.event.EventStatus;
+import org.hisp.dhis.fileresource.FileResource;
+import org.hisp.dhis.fileresource.FileResourceDomain;
+import org.hisp.dhis.fileresource.FileResourceService;
+import org.hisp.dhis.fileresource.FileResourceStorageStatus;
 import org.hisp.dhis.importexport.ImportStrategy;
 import org.hisp.dhis.program.Program;
 import org.hisp.dhis.program.ProgramStageInstanceService;
@@ -77,16 +82,9 @@
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
-import org.hisp.dhis.fileresource.FileResource;
-import org.hisp.dhis.fileresource.FileResourceDomain;
-import org.hisp.dhis.fileresource.FileResourceService;
-import org.hisp.dhis.fileresource.FileResourceStorageStatus;
-
-import com.google.common.io.ByteSource;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -142,7 +140,7 @@
 
     @Autowired
     private ProgramStageInstanceService programStageInstanceService;
-    
+
     @Autowired
     private FileResourceService fileResourceService;
 
@@ -189,12 +187,9 @@
 
         Events events = eventService.getEvents( params );
 
-        if ( options.hasLinks() )
+        for ( Event event : events.getEvents() )
         {
-            for ( Event event : events.getEvents() )
-            {
-                event.setHref( ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + event.getEvent() );
-            }
+            event.setHref( ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + event.getEvent() );
         }
 
         if ( !skipMeta && params.getProgram() != null )
@@ -318,10 +313,7 @@
             throw new WebMessageException( WebMessageUtils.notFound( "Event not found for ID " + uid ) );
         }
 
-        if ( options.hasLinks() )
-        {
-            event.setHref( ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + uid );
-        }
+        event.setHref( ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + uid );
 
         model.addAttribute( "model", event );
         model.addAttribute( "viewClass", options.getViewClass( "detailed" ) );
@@ -348,12 +340,12 @@
         return metaData;
     }
 
-    @RequestMapping( value = "/files", method = RequestMethod.GET)
+    @RequestMapping( value = "/files", method = RequestMethod.GET )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_TRACKED_ENTITY_DATAVALUE_ADD')" )
     public void getEventDataValueFile( @RequestParam String eventUid, @RequestParam String dataElementUid, HttpServletResponse response, HttpServletRequest request ) throws Exception
     {
         Event event = eventService.getEvent( eventUid );
-        
+
         if ( event == null )
         {
             throw new WebMessageException( WebMessageUtils.notFound( "Event not found for ID " + eventUid ) );
@@ -365,32 +357,32 @@
         {
             throw new WebMessageException( WebMessageUtils.notFound( "DataElement not found for ID " + dataElementUid ) );
         }
-        
+
         if ( !dataElement.isFileType() )
         {
             throw new WebMessageException( WebMessageUtils.conflict( "DataElement must be of type file" ) );
-        }        
-        
+        }
+
         // ---------------------------------------------------------------------
         // Get file resource
         // ---------------------------------------------------------------------
 
         String uid = null;
-        
+
         for ( DataValue value : event.getDataValues() )
         {
             if ( value.getDataElement() == dataElement.getUid() )
             {
                 uid = value.getValue();
                 break;
-            }            
+            }
         }
-        
-        if( uid == null)
+
+        if ( uid == null )
         {
             throw new WebMessageException( WebMessageUtils.conflict( "DataElement must be of type file" ) );
         }
-        
+
 
         FileResource fileResource = fileResourceService.getFileResource( uid );
 
@@ -463,9 +455,9 @@
         {
             IOUtils.closeQuietly( inputStream );
         }
-        
+
     }
-    
+
     // -------------------------------------------------------------------------
     // CREATE
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/webdomain/WebOptions.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/webdomain/WebOptions.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/webdomain/WebOptions.java	2016-01-08 14:23:25 +0000
@@ -48,16 +48,6 @@
     // Getters for standard web options
     //--------------------------------------------------------------------------
 
-    public boolean hasLinks( boolean defaultValue )
-    {
-        return stringAsBoolean( options.get( "links" ), defaultValue );
-    }
-
-    public boolean hasLinks()
-    {
-        return stringAsBoolean( options.get( "links" ), true );
-    }
-
     public boolean hasPaging()
     {
         return stringAsBoolean( options.get( "paging" ), true );