← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18083: validation service in /api/maintenance, wip

 

------------------------------------------------------------
revno: 18083
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2015-01-21 17:59:38 +0700
message:
  validation service in /api/maintenance, wip
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/MaintenanceController.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/MaintenanceController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/MaintenanceController.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/MaintenanceController.java	2015-01-21 10:59:38 +0000
@@ -30,14 +30,33 @@
 
 import org.hisp.dhis.analytics.partition.PartitionManager;
 import org.hisp.dhis.cache.HibernateCacheManager;
+import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.dataelement.DataElementCategoryService;
+import org.hisp.dhis.dxf2.metadata.ExportService;
+import org.hisp.dhis.dxf2.metadata.MetaData;
+import org.hisp.dhis.dxf2.metadata.Options;
+import org.hisp.dhis.dxf2.render.RenderService;
+import org.hisp.dhis.dxf2.schema.SchemaValidator;
+import org.hisp.dhis.dxf2.schema.ValidationViolation;
 import org.hisp.dhis.maintenance.MaintenanceService;
+import org.hisp.dhis.schema.Property;
+import org.hisp.dhis.schema.Schema;
+import org.hisp.dhis.schema.SchemaService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.MediaType;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 /**
  * @author Lars Helge Overland
  */
@@ -49,7 +68,7 @@
 
     @Autowired
     private MaintenanceService maintenanceService;
-    
+
     @Autowired
     private DataElementCategoryService categoryService;
 
@@ -57,8 +76,20 @@
     private HibernateCacheManager cacheManager;
 
     @Autowired
-    private PartitionManager partitionManager;    
-    
+    private PartitionManager partitionManager;
+
+    @Autowired
+    private SchemaValidator schemaValidator;
+
+    @Autowired
+    private SchemaService schemaService;
+
+    @Autowired
+    private ExportService exportService;
+
+    @Autowired
+    private RenderService renderService;
+
     @RequestMapping( value = "/periodPruning", method = { RequestMethod.PUT, RequestMethod.POST } )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_PERFORM_MAINTENANCE')" )
     public void prunePeriods()
@@ -86,5 +117,48 @@
     {
         cacheManager.clearCache();
         partitionManager.clearCaches();
-    }    
+    }
+
+    @RequestMapping( value = "/validateMetadata", method = { RequestMethod.PUT, RequestMethod.POST } )
+    @PreAuthorize( "hasRole('ALL') or hasRole('F_PERFORM_MAINTENANCE')" )
+    public void runValidateMetadata( HttpServletResponse response ) throws InvocationTargetException, IllegalAccessException, IOException
+    {
+        Options options = new Options();
+        options.setAssumeTrue( true );
+
+        MetaData metaData = exportService.getMetaData( options );
+        Schema schema = schemaService.getDynamicSchema( MetaData.class );
+
+        Map<String, Map<String, List<ValidationViolation>>> output = new HashMap<>();
+
+        for ( Property property : schema.getProperties() )
+        {
+            if ( !property.isCollection() )
+            {
+                continue;
+            }
+
+            output.put( property.getName(), new HashMap<String, List<ValidationViolation>>() );
+
+            Collection<?> collection = (Collection<?>) property.getGetterMethod().invoke( metaData );
+
+            for ( Object object : collection )
+            {
+                if ( !IdentifiableObject.class.isInstance( object ) )
+                {
+                    continue;
+                }
+
+                List<ValidationViolation> validationViolations = schemaValidator.validate( object );
+
+                if ( !validationViolations.isEmpty() )
+                {
+                    output.get( property.getName() ).put( ((IdentifiableObject) object).getUid(), validationViolations );
+                }
+            }
+        }
+
+        response.setContentType( MediaType.APPLICATION_JSON_VALUE );
+        renderService.toJson( response.getOutputStream(), output );
+    }
 }