dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #28527
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14246: support CRUD operations for relationshipTypes (web-api)
------------------------------------------------------------
revno: 14246
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-03-17 13:55:35 +0100
message:
support CRUD operations for relationshipTypes (web-api)
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/relationship/RelationshipType.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/relationship/RelationshipTypeService.java
dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/relationship/DefaultRelationshipTypeService.java
dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/relationship/RelationshipServiceTest.java
dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/relationship/RelationshipStoreTest.java
dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/relationship/RelationshipTypeServiceTest.java
dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceServiceTest.java
dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/RelationshipTypeController.java
dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/TrackedEntityController.java
dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/java/org/hisp/dhis/trackedentity/action/relationship/AddRelationshipTypeAction.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-api/src/main/java/org/hisp/dhis/relationship/RelationshipType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/relationship/RelationshipType.java 2014-03-17 10:17:13 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/relationship/RelationshipType.java 2014-03-17 12:55:35 +0000
@@ -34,13 +34,14 @@
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import org.hisp.dhis.common.BaseIdentifiableObject;
import org.hisp.dhis.common.DxfNamespaces;
+import org.hisp.dhis.common.IdentifiableObject;
import org.hisp.dhis.common.view.DetailedView;
import org.hisp.dhis.common.view.ExportView;
/**
* @author Abyot Asalefew
*/
-@JacksonXmlRootElement( localName = "relationshipType", namespace = DxfNamespaces.DXF_2_0 )
+@JacksonXmlRootElement(localName = "relationshipType", namespace = DxfNamespaces.DXF_2_0)
public class RelationshipType
extends BaseIdentifiableObject
{
@@ -107,4 +108,18 @@
", bIsToA='" + bIsToA + '\'' +
'}';
}
+
+ @Override
+ public void mergeWith( IdentifiableObject other )
+ {
+ super.mergeWith( other );
+
+ if ( other.getClass().isInstance( this ) )
+ {
+ RelationshipType relationshipType = (RelationshipType) other;
+
+ this.aIsToB = relationshipType.getaIsToB();
+ this.bIsToA = relationshipType.getbIsToA();
+ }
+ }
}
\ No newline at end of file
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/relationship/RelationshipTypeService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/relationship/RelationshipTypeService.java 2013-11-04 03:13:27 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/relationship/RelationshipTypeService.java 2014-03-17 12:55:35 +0000
@@ -45,7 +45,7 @@
*
* @return A generated unique id of the added {@link RelationshipType}.
*/
- int saveRelationshipType( RelationshipType relationshipType );
+ int addRelationshipType( RelationshipType relationshipType );
/**
* Deletes a {@link RelationshipType}.
@@ -71,6 +71,15 @@
RelationshipType getRelationshipType( int id );
/**
+ * Returns a {@link RelationshipType}.
+ *
+ * @param uid the uid of the RelationshipType to return.
+ *
+ * @return the RelationshipType with the given id
+ */
+ RelationshipType getRelationshipType( String uid );
+
+ /**
* Retrieve a relationship
*
* @param aIsToB The A side
=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/relationship/DefaultRelationshipTypeService.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/relationship/DefaultRelationshipTypeService.java 2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/relationship/DefaultRelationshipTypeService.java 2014-03-17 12:55:35 +0000
@@ -28,13 +28,13 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-import static org.hisp.dhis.i18n.I18nUtils.i18n;
-
-import java.util.Collection;
-
import org.hisp.dhis.i18n.I18nService;
import org.springframework.transaction.annotation.Transactional;
+import java.util.Collection;
+
+import static org.hisp.dhis.i18n.I18nUtils.i18n;
+
/**
* @author Abyot Asalefew
* @version $Id$
@@ -80,7 +80,13 @@
return i18n( i18nService, relationshipTypeStore.get( id ) );
}
- public int saveRelationshipType( RelationshipType relationshipType )
+ @Override
+ public RelationshipType getRelationshipType( String uid )
+ {
+ return i18n( i18nService, relationshipTypeStore.getByUid( uid ) );
+ }
+
+ public int addRelationshipType( RelationshipType relationshipType )
{
return relationshipTypeStore.save( relationshipType );
}
=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/relationship/RelationshipServiceTest.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/relationship/RelationshipServiceTest.java 2014-02-07 20:25:49 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/relationship/RelationshipServiceTest.java 2014-03-17 12:55:35 +0000
@@ -97,7 +97,7 @@
entityInstanceService.saveTrackedEntityInstance( entityInstanceD );
relationshipType = createRelationshipType( 'A' );
- relationshipTypeService.saveRelationshipType( relationshipType );
+ relationshipTypeService.addRelationshipType( relationshipType );
relationshipA = new Relationship( entityInstanceA, relationshipType, entityInstanceB );
relationshipB = new Relationship( entityInstanceC, relationshipType, entityInstanceD );
=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/relationship/RelationshipStoreTest.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/relationship/RelationshipStoreTest.java 2014-02-07 20:25:49 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/relationship/RelationshipStoreTest.java 2014-03-17 12:55:35 +0000
@@ -95,7 +95,7 @@
entityInstanceService.saveTrackedEntityInstance( entityInstanceD );
relationshipType = createRelationshipType( 'A' );
- relationshipTypeService.saveRelationshipType( relationshipType );
+ relationshipTypeService.addRelationshipType( relationshipType );
relationshipA = new Relationship( entityInstanceA, relationshipType, entityInstanceB );
relationshipB = new Relationship( entityInstanceC, relationshipType, entityInstanceD );
=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/relationship/RelationshipTypeServiceTest.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/relationship/RelationshipTypeServiceTest.java 2013-11-22 03:43:09 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/relationship/RelationshipTypeServiceTest.java 2014-03-17 12:55:35 +0000
@@ -61,8 +61,8 @@
@Test
public void testSaveRelationshipType()
{
- int idA = relationshipTypeService.saveRelationshipType( relationshipTypeA );
- int idB = relationshipTypeService.saveRelationshipType( relationshipTypeB );
+ int idA = relationshipTypeService.addRelationshipType( relationshipTypeA );
+ int idB = relationshipTypeService.addRelationshipType( relationshipTypeB );
assertNotNull( relationshipTypeService.getRelationshipType( idA ) );
assertNotNull( relationshipTypeService.getRelationshipType( idB ) );
@@ -71,8 +71,8 @@
@Test
public void testDeleteRelationshipType()
{
- int idA = relationshipTypeService.saveRelationshipType( relationshipTypeA );
- int idB = relationshipTypeService.saveRelationshipType( relationshipTypeB );
+ int idA = relationshipTypeService.addRelationshipType( relationshipTypeA );
+ int idB = relationshipTypeService.addRelationshipType( relationshipTypeB );
assertNotNull( relationshipTypeService.getRelationshipType( idA ) );
assertNotNull( relationshipTypeService.getRelationshipType( idB ) );
@@ -91,7 +91,7 @@
@Test
public void testUpdateRelationshipType()
{
- int idA = relationshipTypeService.saveRelationshipType( relationshipTypeA );
+ int idA = relationshipTypeService.addRelationshipType( relationshipTypeA );
assertNotNull( relationshipTypeService.getRelationshipType( idA ) );
@@ -104,8 +104,8 @@
@Test
public void testGetRelationshipTypeById()
{
- int idA = relationshipTypeService.saveRelationshipType( relationshipTypeA );
- int idB = relationshipTypeService.saveRelationshipType( relationshipTypeB );
+ int idA = relationshipTypeService.addRelationshipType( relationshipTypeA );
+ int idB = relationshipTypeService.addRelationshipType( relationshipTypeB );
assertEquals( relationshipTypeA, relationshipTypeService.getRelationshipType( idA ) );
assertEquals( relationshipTypeB, relationshipTypeService.getRelationshipType( idB ) );
@@ -114,15 +114,15 @@
@Test
public void testGetRelationshipTypeByDescription()
{
- relationshipTypeService.saveRelationshipType( relationshipTypeA );
+ relationshipTypeService.addRelationshipType( relationshipTypeA );
assertEquals( relationshipTypeA, relationshipTypeService.getRelationshipType( "aIsToB", "bIsToA" ) );
}
@Test
public void testGetAllRelationshipTypes()
{
- relationshipTypeService.saveRelationshipType( relationshipTypeA );
- relationshipTypeService.saveRelationshipType( relationshipTypeB );
+ relationshipTypeService.addRelationshipType( relationshipTypeA );
+ relationshipTypeService.addRelationshipType( relationshipTypeB );
assertTrue( equals( relationshipTypeService.getAllRelationshipTypes(), relationshipTypeA, relationshipTypeB ) );
}
=== modified file 'dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceServiceTest.java'
--- dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceServiceTest.java 2014-02-07 20:25:49 +0000
+++ dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceServiceTest.java 2014-03-17 12:55:35 +0000
@@ -49,10 +49,6 @@
import org.hisp.dhis.program.ProgramStageInstance;
import org.hisp.dhis.relationship.RelationshipType;
import org.hisp.dhis.relationship.RelationshipTypeService;
-import org.hisp.dhis.trackedentity.TrackedEntityInstance;
-import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
-import org.hisp.dhis.trackedentity.TrackedEntityAttributeService;
-import org.hisp.dhis.trackedentity.TrackedEntityInstanceService;
import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue;
import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService;
import org.hisp.dhis.validation.ValidationCriteriaService;
@@ -334,7 +330,7 @@
int idB = entityInstanceService.saveTrackedEntityInstance( entityInstanceB1 );
RelationshipType relationshipType = createRelationshipType( 'A' );
- int relationshipTypeId = relationshipTypeService.saveRelationshipType( relationshipType );
+ int relationshipTypeId = relationshipTypeService.addRelationshipType( relationshipType );
TrackedEntityAttributeValue attributeValue = createTrackedEntityAttributeValue( 'A', entityInstanceA1,
entityInstanceAttribute );
@@ -351,7 +347,7 @@
int idB = entityInstanceService.saveTrackedEntityInstance( entityInstanceB1 );
RelationshipType relationshipType = createRelationshipType( 'A' );
- int relationshipTypeId = relationshipTypeService.saveRelationshipType( relationshipType );
+ int relationshipTypeId = relationshipTypeService.addRelationshipType( relationshipType );
entityInstanceA3.setName( "B" );
TrackedEntityAttributeValue attributeValue = createTrackedEntityAttributeValue( 'A', entityInstanceA3,
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/RelationshipTypeController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/RelationshipTypeController.java 2013-09-19 08:38:29 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/RelationshipTypeController.java 2014-03-17 12:55:35 +0000
@@ -29,9 +29,21 @@
*/
import org.hisp.dhis.api.controller.AbstractCrudController;
+import org.hisp.dhis.api.utils.ContextUtils;
+import org.hisp.dhis.dxf2.utils.JacksonUtils;
import org.hisp.dhis.relationship.RelationshipType;
+import org.hisp.dhis.relationship.RelationshipTypeService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.InputStream;
/**
* @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -41,4 +53,92 @@
public class RelationshipTypeController extends AbstractCrudController<RelationshipType>
{
public static final String RESOURCE_PATH = "/relationshipTypes";
+
+ @Autowired
+ private RelationshipTypeService relationshipTypeService;
+
+ //--------------------------------------------------------------------------
+ // POST
+ //--------------------------------------------------------------------------
+
+ @RequestMapping( method = RequestMethod.POST, consumes = { "application/xml", "text/xml" } )
+ @ResponseStatus( HttpStatus.CREATED )
+ public void postXmlObject( HttpServletResponse response, HttpServletRequest request, InputStream input ) throws Exception
+ {
+ RelationshipType relationshipType = JacksonUtils.fromXml( input, RelationshipType.class );
+ relationshipTypeService.addRelationshipType( relationshipType );
+
+ response.setHeader( "Location", ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + relationshipType.getUid() );
+ }
+
+ @RequestMapping( method = RequestMethod.POST, consumes = "application/json" )
+ @ResponseStatus( HttpStatus.CREATED )
+ public void postJsonObject( HttpServletResponse response, HttpServletRequest request, InputStream input ) throws Exception
+ {
+ RelationshipType relationshipType = JacksonUtils.fromJson( input, RelationshipType.class );
+ relationshipTypeService.addRelationshipType( relationshipType );
+
+ response.setHeader( "Location", ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + relationshipType.getUid() );
+ }
+
+ //--------------------------------------------------------------------------
+ // PUT
+ //--------------------------------------------------------------------------
+
+ @RequestMapping( value = "/{uid}", method = RequestMethod.PUT, consumes = { "application/xml", "text/xml" } )
+ @ResponseStatus( value = HttpStatus.NO_CONTENT )
+ public void putXmlObject( HttpServletResponse response, HttpServletRequest request, @PathVariable( "uid" ) String uid, InputStream input ) throws Exception
+ {
+ RelationshipType relationshipType = relationshipTypeService.getRelationshipType( uid );
+
+ if ( relationshipType == null )
+ {
+ ContextUtils.conflictResponse( response, "RelationshipType does not exist: " + uid );
+ return;
+ }
+
+ RelationshipType newRelationshipType = JacksonUtils.fromXml( input, RelationshipType.class );
+ newRelationshipType.setUid( relationshipType.getUid() );
+ relationshipType.mergeWith( newRelationshipType );
+
+ relationshipTypeService.updateRelationshipType( relationshipType );
+ }
+
+ @RequestMapping( value = "/{uid}", method = RequestMethod.PUT, consumes = "application/json" )
+ @ResponseStatus( value = HttpStatus.NO_CONTENT )
+ public void putJsonObject( HttpServletResponse response, HttpServletRequest request, @PathVariable( "uid" ) String uid, InputStream input ) throws Exception
+ {
+ RelationshipType relationshipType = relationshipTypeService.getRelationshipType( uid );
+
+ if ( relationshipType == null )
+ {
+ ContextUtils.conflictResponse( response, "RelationshipType does not exist: " + uid );
+ return;
+ }
+
+ RelationshipType newRelationshipType = JacksonUtils.fromJson( input, RelationshipType.class );
+ newRelationshipType.setUid( relationshipType.getUid() );
+ relationshipType.mergeWith( newRelationshipType );
+
+ relationshipTypeService.updateRelationshipType( relationshipType );
+ }
+
+ //--------------------------------------------------------------------------
+ // DELETE
+ //--------------------------------------------------------------------------
+
+ @RequestMapping( value = "/{uid}", method = RequestMethod.DELETE )
+ @ResponseStatus( value = HttpStatus.NO_CONTENT )
+ public void deleteObject( HttpServletResponse response, HttpServletRequest request, @PathVariable( "uid" ) String uid ) throws Exception
+ {
+ RelationshipType relationshipType = relationshipTypeService.getRelationshipType( uid );
+
+ if ( relationshipType == null )
+ {
+ ContextUtils.conflictResponse( response, "RelationshipType does not exist: " + uid );
+ return;
+ }
+
+ relationshipTypeService.deleteRelationshipType( relationshipType );
+ }
}
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/TrackedEntityController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/TrackedEntityController.java 2014-03-17 12:15:10 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/TrackedEntityController.java 2014-03-17 12:55:35 +0000
@@ -98,6 +98,7 @@
}
TrackedEntity newTrackedEntity = JacksonUtils.fromXml( input, TrackedEntity.class );
+ newTrackedEntity.setUid( trackedEntity.getUid() );
trackedEntity.mergeWith( newTrackedEntity );
trackedEntityService.updateTrackedEntity( trackedEntity );
@@ -116,6 +117,7 @@
}
TrackedEntity newTrackedEntity = JacksonUtils.fromJson( input, TrackedEntity.class );
+ newTrackedEntity.setUid( trackedEntity.getUid() );
trackedEntity.mergeWith( newTrackedEntity );
trackedEntityService.updateTrackedEntity( trackedEntity );
=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/java/org/hisp/dhis/trackedentity/action/relationship/AddRelationshipTypeAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/java/org/hisp/dhis/trackedentity/action/relationship/AddRelationshipTypeAction.java 2014-02-07 20:25:49 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/java/org/hisp/dhis/trackedentity/action/relationship/AddRelationshipTypeAction.java 2014-03-17 12:55:35 +0000
@@ -90,7 +90,7 @@
relationshipType.setbIsToA( bIsToA );
relationshipType.setName( name );
- relationshipTypeService.saveRelationshipType( relationshipType );
+ relationshipTypeService.addRelationshipType( relationshipType );
return SUCCESS;
}