dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #30623
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15560: changed parameter parsing in inclusion/exclusion filter
------------------------------------------------------------
revno: 15560
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2014-06-05 13:11:33 +0200
message:
changed parameter parsing in inclusion/exclusion filter
modified:
dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/filter/DefaultFilterService.java
dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/filter/FilterService.java
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/utils/ContextService.java
dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/DefaultContextService.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/filter/DefaultFilterService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/filter/DefaultFilterService.java 2014-06-04 10:49:04 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/filter/DefaultFilterService.java 2014-06-05 11:11:33 +0000
@@ -28,6 +28,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.hisp.dhis.common.IdentifiableObject;
@@ -81,8 +82,11 @@
@Override
public <T extends IdentifiableObject> CollectionNode filterProperties( Class<?> klass, List<T> objects,
- String include, String exclude )
+ List<String> includes, List<String> excludes )
{
+ String include = includes == null ? "" : Joiner.on( "," ).join( includes );
+ String exclude = excludes == null ? "" : Joiner.on( "," ).join( excludes );
+
Schema rootSchema = schemaService.getDynamicSchema( klass );
CollectionNode collectionNode = new CollectionNode( rootSchema.getPlural() ); // replace with 'xml' collection name
=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/filter/FilterService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/filter/FilterService.java 2014-06-03 14:33:29 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/filter/FilterService.java 2014-06-05 11:11:33 +0000
@@ -60,5 +60,5 @@
* @return List of objects with only wanted properties
*/
<T extends IdentifiableObject> CollectionNode filterProperties( Class<?> klass, List<T> objects,
- String include, String exclude );
+ List<String> include, List<String> exclude );
}
=== 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-06-04 10:49:04 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java 2014-06-05 11:11:33 +0000
@@ -28,7 +28,6 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import org.hisp.dhis.acl.Access;
import org.hisp.dhis.acl.AclService;
@@ -46,7 +45,6 @@
import org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException;
import org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException;
import org.hisp.dhis.importexport.ImportStrategy;
-import org.hisp.dhis.node.NodeService;
import org.hisp.dhis.node.types.ComplexNode;
import org.hisp.dhis.node.types.RootNode;
import org.hisp.dhis.node.types.SimpleNode;
@@ -54,6 +52,7 @@
import org.hisp.dhis.system.util.ReflectionUtils;
import org.hisp.dhis.user.CurrentUserService;
import org.hisp.dhis.webapi.controller.exception.NotFoundException;
+import org.hisp.dhis.webapi.utils.ContextService;
import org.hisp.dhis.webapi.utils.ContextUtils;
import org.hisp.dhis.webapi.utils.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
@@ -65,6 +64,7 @@
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.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import javax.servlet.http.HttpServletRequest;
@@ -81,7 +81,7 @@
*/
public abstract class AbstractCrudController<T extends IdentifiableObject>
{
- private static final String DEFAULT_LIST_INCLUDE = Joiner.on( "," ).join( FilterService.IDENTIFIABLE_PROPERTIES );
+ private static final List<String> DEFAULT_LIST_INCLUDES = Lists.newArrayList( FilterService.IDENTIFIABLE_PROPERTIES );
//--------------------------------------------------------------------------
// Dependencies
@@ -112,7 +112,7 @@
protected ImportService importService;
@Autowired
- protected NodeService nodeService;
+ protected ContextService contextService;
//--------------------------------------------------------------------------
// GET
@@ -141,18 +141,19 @@
}
@RequestMapping( method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE } )
- public void getObjectListJson(
- @RequestParam( required = false ) String include,
- @RequestParam( required = false ) String exclude,
- @RequestParam( value = "filter", required = false ) List<String> filters,
+ public @ResponseBody RootNode getObjectListJson(
@RequestParam Map<String, String> parameters, Model model, HttpServletResponse response, HttpServletRequest request ) throws IOException
{
+ List<String> includes = Lists.newArrayList( contextService.getParameterValues( "include" ) );
+ List<String> excludes = Lists.newArrayList( contextService.getParameterValues( "exclude" ) );
+ List<String> filters = Lists.newArrayList( contextService.getParameterValues( "filter" ) );
+
WebOptions options = new WebOptions( parameters );
WebMetaData metaData = new WebMetaData();
- if ( include == null && exclude == null )
+ if ( includes.isEmpty() && excludes.isEmpty() )
{
- include = DEFAULT_LIST_INCLUDE;
+ includes = DEFAULT_LIST_INCLUDES;
}
boolean hasPaging = options.hasPaging();
@@ -170,16 +171,16 @@
}
List<T> entityList = getEntityList( metaData, options );
+ Pager pager = metaData.getPager();
// enable object filter
- if ( filters != null && !filters.isEmpty() )
+ if ( !filters.isEmpty() )
{
entityList = filterService.filterObjects( entityList, filters );
if ( hasPaging )
{
- Pager pager = new Pager( options.getPage(), entityList.size(), options.getPageSize() );
- metaData.setPager( pager );
+ pager = new Pager( options.getPage(), entityList.size(), options.getPageSize() );
entityList = PagerUtils.pageCollection( entityList, pager );
}
}
@@ -187,11 +188,7 @@
postProcessEntities( entityList );
postProcessEntities( entityList, options, parameters );
- response.setContentType( MediaType.APPLICATION_JSON_VALUE + "; charset=UTF-8" );
-
- ReflectionUtils.invokeSetterMethod( schemaService.getSchema( getEntityClass() ).getPlural(), metaData, entityList );
-
- if ( include != null && include.contains( "access" ) )
+ if ( includes != null && includes.contains( "access" ) )
{
options.getOptions().put( "viewClass", "sharing" );
}
@@ -200,21 +197,19 @@
RootNode rootNode = new RootNode( "metadata" );
- if ( hasPaging )
+ if ( pager != null )
{
ComplexNode pagerNode = rootNode.addChild( new ComplexNode( "pager" ) );
- pagerNode.addChild( new SimpleNode( "page", metaData.getPager().getPage() ) );
- pagerNode.addChild( new SimpleNode( "pageCount", metaData.getPager().getPageCount() ) );
- pagerNode.addChild( new SimpleNode( "total", metaData.getPager().getTotal() ) );
- pagerNode.addChild( new SimpleNode( "nextPage", metaData.getPager().getNextPage() ) );
- pagerNode.addChild( new SimpleNode( "prevPage", metaData.getPager().getPrevPage() ) );
+ pagerNode.addChild( new SimpleNode( "page", pager.getPage() ) );
+ pagerNode.addChild( new SimpleNode( "pageCount", pager.getPageCount() ) );
+ pagerNode.addChild( new SimpleNode( "total", pager.getTotal() ) );
+ pagerNode.addChild( new SimpleNode( "nextPage", pager.getNextPage() ) );
+ pagerNode.addChild( new SimpleNode( "prevPage", pager.getPrevPage() ) );
}
- rootNode.addChild( filterService.filterProperties( getEntityClass(), entityList, include, exclude ) );
+ rootNode.addChild( filterService.filterProperties( getEntityClass(), entityList, includes, excludes ) );
- // response.setContentType( MediaType.APPLICATION_XML_VALUE );
- // nodeService.serialize( rootNode, MediaType.APPLICATION_XML_VALUE, response.getOutputStream() );
- nodeService.serialize( rootNode, MediaType.APPLICATION_JSON_VALUE, response.getOutputStream() );
+ return rootNode;
}
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/ContextService.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/ContextService.java 2014-05-31 16:27:04 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/ContextService.java 2014-06-05 11:11:33 +0000
@@ -29,6 +29,7 @@
*/
import javax.servlet.http.HttpServletRequest;
+import java.util.Set;
/**
* @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -57,4 +58,13 @@
* @return HttpServletRequest
*/
HttpServletRequest getRequest();
+
+ /**
+ * Returns a list of parameters with a given name, if the parameter doesn't exist, it will
+ * return a empty list.
+ *
+ * @param name Parameter to get
+ * @return List of parameter values, or empty if not found
+ */
+ Set<String> getParameterValues( String name );
}
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/DefaultContextService.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/DefaultContextService.java 2014-05-31 16:27:04 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/DefaultContextService.java 2014-06-05 11:11:33 +0000
@@ -28,11 +28,15 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
*/
+import com.google.common.base.Splitter;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
+import java.util.Set;
/**
* @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -40,6 +44,8 @@
@Service
public class DefaultContextService implements ContextService
{
+ private static final Splitter COMMA_SPLITTER = Splitter.on( "," );
+
@Override
public String getServletPath()
{
@@ -92,4 +98,23 @@
{
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
}
+
+ @Override
+ public Set<String> getParameterValues( String name )
+ {
+ if ( getRequest().getParameterValues( name ) == null )
+ {
+ return Sets.newHashSet();
+ }
+
+ Set<String> parameter = Sets.newHashSet();
+ String[] parameterValues = getRequest().getParameterValues( name );
+
+ for ( String value : parameterValues )
+ {
+ parameter.addAll( Lists.newArrayList( COMMA_SPLITTER.split( value ) ) );
+ }
+
+ return parameter;
+ }
}