dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #36541
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18686: wip, MergeService (automatic mergeWith based on Schema)
------------------------------------------------------------
revno: 18686
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-03-26 18:50:23 +0700
message:
wip, MergeService (automatic mergeWith based on Schema)
added:
dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultMergeService.java
dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/MergeService.java
modified:
dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java
dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/schema/DefaultSchemaValidator.java
dhis-2/dhis-services/dhis-service-dxf2/src/main/resources/META-INF/dhis/beans.xml
--
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
=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultMergeService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultMergeService.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultMergeService.java 2015-03-26 11:50:23 +0000
@@ -0,0 +1,102 @@
+package org.hisp.dhis.dxf2.metadata;
+
+/*
+ * Copyright (c) 2004-2015, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import org.hisp.dhis.common.MergeStrategy;
+import org.hisp.dhis.schema.Property;
+import org.hisp.dhis.schema.Schema;
+import org.hisp.dhis.schema.SchemaService;
+import org.hisp.dhis.system.util.ReflectionUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import java.util.Collection;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class DefaultMergeService implements MergeService
+{
+ @Autowired
+ private SchemaService schemaService;
+
+ @Override
+ @SuppressWarnings( "unchecked" )
+ public <T> void merge( T source, T target, MergeStrategy mergeStrategy )
+ {
+ if ( source == null || target == null )
+ {
+ return;
+ }
+
+ Schema schema = schemaService.getDynamicSchema( source.getClass() );
+
+ for ( Property property : schema.getProperties() )
+ {
+ if ( property.isCollection() )
+ {
+ Collection sourceObject = ReflectionUtils.invokeMethod( source, property.getGetterMethod() );
+
+ if ( sourceObject == null )
+ {
+ continue;
+ }
+
+ sourceObject.clear();
+
+ if ( property.isOwner() )
+ {
+ if ( property.isManyToMany() )
+ {
+ Collection targetObject = ReflectionUtils.invokeMethod( target, property.getGetterMethod() );
+ sourceObject.addAll( targetObject );
+ }
+ else
+ {
+ // one-to-many
+ }
+ }
+
+ ReflectionUtils.invokeMethod( source, property.getSetterMethod(), sourceObject );
+ }
+ else
+ {
+ Object targetObject = ReflectionUtils.invokeMethod( target, property.getGetterMethod() );
+
+ if ( mergeStrategy.isReplace() )
+ {
+ ReflectionUtils.invokeMethod( source, property.getSetterMethod(), targetObject );
+ }
+ else if ( mergeStrategy.isMerge() && targetObject != null )
+ {
+ ReflectionUtils.invokeMethod( source, property.getSetterMethod(), targetObject );
+ }
+ }
+ }
+ }
+}
=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/MergeService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/MergeService.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/MergeService.java 2015-03-26 11:50:23 +0000
@@ -0,0 +1,39 @@
+package org.hisp.dhis.dxf2.metadata;
+
+/*
+ * Copyright (c) 2004-2015, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import org.hisp.dhis.common.MergeStrategy;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public interface MergeService
+{
+ <T> void merge( T source, T target, MergeStrategy mergeStrategy );
+}
=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java 2015-03-17 11:18:29 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java 2015-03-26 11:50:23 +0000
@@ -40,12 +40,10 @@
import org.hisp.dhis.attribute.AttributeValue;
import org.hisp.dhis.common.BaseIdentifiableObject;
import org.hisp.dhis.common.IdentifiableObject;
-import org.hisp.dhis.common.IdentifiableObjectManager;
import org.hisp.dhis.common.NameableObject;
import org.hisp.dhis.dashboard.DashboardItem;
import org.hisp.dhis.dataelement.CategoryOptionGroupSet;
import org.hisp.dhis.dataelement.DataElementCategoryDimension;
-import org.hisp.dhis.dataelement.DataElementCategoryService;
import org.hisp.dhis.dataelement.DataElementOperand;
import org.hisp.dhis.dataelement.DataElementOperandService;
import org.hisp.dhis.dataentryform.DataEntryForm;
@@ -55,6 +53,7 @@
import org.hisp.dhis.dxf2.importsummary.ImportConflict;
import org.hisp.dhis.dxf2.metadata.ImportTypeSummary;
import org.hisp.dhis.dxf2.metadata.Importer;
+import org.hisp.dhis.dxf2.metadata.MergeService;
import org.hisp.dhis.dxf2.metadata.ObjectBridge;
import org.hisp.dhis.dxf2.metadata.handlers.ObjectHandler;
import org.hisp.dhis.dxf2.metadata.handlers.ObjectHandlerUtils;
@@ -149,10 +148,7 @@
private List<ObjectHandler<T>> objectHandlers;
@Autowired
- private DataElementCategoryService categoryService;
-
- @Autowired
- private IdentifiableObjectManager manager;
+ private MergeService mergeService;
//-------------------------------------------------------------------------------------------------------
// Constructor
@@ -441,11 +437,13 @@
{
User persistedObjectUser = persistedObject.getUser();
persistedObject.mergeWith( object, options.getMergeStrategy() );
+ // mergeService.merge( persistedObject, object, options.getMergeStrategy() );
persistedObject.setUser( persistedObjectUser );
}
else
{
persistedObject.mergeWith( object, options.getMergeStrategy() );
+ // mergeService.merge( persistedObject, object, options.getMergeStrategy() );
persistedObject.mergeSharingWith( object );
}
@@ -470,6 +468,7 @@
}
((User) persistedObject).getUserCredentials().mergeWith( userCredentials, options.getMergeStrategy() );
+ // mergeService.merge( ((User) persistedObject).getUserCredentials(), userCredentials, options.getMergeStrategy() );
reattachCollectionFields( ((User) persistedObject).getUserCredentials(), collectionFieldsUserCredentials, user );
sessionFactory.getCurrentSession().saveOrUpdate( ((User) persistedObject).getUserCredentials() );
=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/schema/DefaultSchemaValidator.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/schema/DefaultSchemaValidator.java 2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/schema/DefaultSchemaValidator.java 2015-03-26 11:50:23 +0000
@@ -117,18 +117,15 @@
{
validationViolations.add( new ValidationViolation( property.getName(), "Not a valid email.", value ) );
}
-
- if ( PropertyType.URL == property.getPropertyType() && !GenericValidator.isUrl( value ) )
+ else if ( PropertyType.URL == property.getPropertyType() && !isUrl( value ) )
{
validationViolations.add( new ValidationViolation( property.getName(), "Not a valid URL.", value ) );
}
-
- if ( PropertyType.PASSWORD == property.getPropertyType() && !ValidationUtils.passwordIsValid( value ) )
+ else if ( PropertyType.PASSWORD == property.getPropertyType() && !ValidationUtils.passwordIsValid( value ) )
{
validationViolations.add( new ValidationViolation( property.getName(), "Not a valid password.", value ) );
}
-
- if ( PropertyType.COLOR == property.getPropertyType() && !ValidationUtils.isValidHexColor( value ) )
+ else if ( PropertyType.COLOR == property.getPropertyType() && !ValidationUtils.isValidHexColor( value ) )
{
validationViolations.add( new ValidationViolation( property.getName(), "Not a valid hex color.", value ) );
}
@@ -143,6 +140,12 @@
return validationViolations;
}
+ // Commons validator have some issues in latest version, replacing with a very simple test for now
+ private boolean isUrl( String url )
+ {
+ return !StringUtils.isEmpty( url ) && (url.startsWith( "http://" ) || url.startsWith( "https://" ));
+ }
+
private Collection<? extends ValidationViolation> validateCollection( Object object, Property property )
{
List<ValidationViolation> validationViolations = new ArrayList<>();
=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/resources/META-INF/dhis/beans.xml 2015-03-13 08:24:36 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/resources/META-INF/dhis/beans.xml 2015-03-26 11:50:23 +0000
@@ -4,6 +4,8 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
+ <bean id="org.hisp.dhis.dxf2.metadata.MergeService" class="org.hisp.dhis.dxf2.metadata.DefaultMergeService" />
+
<bean id="org.hisp.dhis.dxf2.gml.GmlImportService" class="org.hisp.dhis.dxf2.gml.DefaultGmlImportService" />
<bean id="org.hisp.dhis.dxf2.events.event.csv.CsvEventService" class="org.hisp.dhis.dxf2.events.event.csv.DefaultCsvEventService" />