Skip to content

Commit

Permalink
Started with subscription resource REST endpoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
volsch committed Nov 7, 2018
1 parent 53099c1 commit a356f0a
Show file tree
Hide file tree
Showing 92 changed files with 3,621 additions and 402 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.dhis2.fhir.adapter.jackson;

/*
* Copyright (c) 2004-2018, 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 com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.Converter;

import java.util.ArrayList;
import java.util.List;

/**
* Converts lists (especially persistent bag) to a list.
* This is required when class name is serialized and used sorted set class
* cannot be instantiated otherwise.
*
* @author volsch
*/
public class PersistentBagConverter implements Converter<List<?>, List<?>>
{
@Override
public List<?> convert( List<?> value )
{
if ( value == null )
{
return null;
}
return new ArrayList<>( value );
}

@Override
public JavaType getInputType( TypeFactory typeFactory )
{
return typeFactory.constructCollectionType( List.class, Object.class );
}

@Override
public JavaType getOutputType( TypeFactory typeFactory )
{
return typeFactory.constructCollectionType( ArrayList.class, Object.class );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.dhis2.fhir.adapter.jackson;

/*
* Copyright (c) 2004-2018, 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 com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.Converter;

import javax.annotation.Nonnull;
import java.util.SortedSet;
import java.util.TreeSet;

/**
* Converts sorted sets (especially persistent sorted set) to a tree set.
* This is required when class name is serialized and used sorted set class
* cannot be instantiated otherwise.
*
* @param <T> the concrete type of the item class.
* @author volsch
*/
public class TypedPersistentSortedSetConverter<T> implements Converter<SortedSet<T>, TreeSet<T>>
{
private final Class<T> itemClass;

public TypedPersistentSortedSetConverter( @Nonnull Class<T> itemClass )
{
this.itemClass = itemClass;
}

@Override
public TreeSet<T> convert( SortedSet<T> value )
{
if ( value == null )
{
return null;
}
return new TreeSet<>( value );
}

@Override
public JavaType getInputType( TypeFactory typeFactory )
{
return typeFactory.constructCollectionType( SortedSet.class, itemClass );
}

@Override
public JavaType getOutputType( TypeFactory typeFactory )
{
return typeFactory.constructCollectionType( TreeSet.class, itemClass );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.dhis2.fhir.adapter.rest;

/*
* Copyright (c) 2004-2018, 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.springframework.http.HttpStatus;

import javax.annotation.Nonnull;

/**
* Thrown if the request cannot be handled because the input data is invalid.
* The exception contains status code 400 that should be returned to a client.
*
* @author volsch
*/
public class RestBadRequestException extends RestResponseEntityException
{
private static final long serialVersionUID = -7487909336857881610L;

public RestBadRequestException()
{
super();
}

public RestBadRequestException( String message )
{
super( message );
}

public RestBadRequestException( String message, Throwable cause )
{
super( message, cause );
}

public RestBadRequestException( Throwable cause )
{
super( cause );
}

@Nonnull
@Override
public HttpStatus getHttpStatus()
{
return HttpStatus.BAD_REQUEST;
}
}
59 changes: 59 additions & 0 deletions common/src/main/java/org/dhis2/fhir/adapter/rest/RestError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.dhis2.fhir.adapter.rest;

/*
* Copyright (c) 2004-2018, 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 com.fasterxml.jackson.annotation.JsonProperty;
import reactor.util.annotation.NonNull;

import javax.annotation.Nonnull;
import java.io.Serializable;

/**
* Error message that is serialized to the client in case of an error.
*
* @author volsch
*/
public class RestError implements Serializable
{
private static final long serialVersionUID = 1604874907010817024L;

private final String message;

public RestError( @Nonnull String message )
{
this.message = message;
}

@JsonProperty
@NonNull
public String getMessage()
{
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public class RestExceptionHandler extends ResponseEntityExceptionHandler
{
@ExceptionHandler( value = { RestResponseEntityException.class } )
@ResponseBody
public ResponseEntity<String> handleResponseEntityException( RestResponseEntityException e, WebRequest request )
public ResponseEntity<RestError> handleResponseEntityException( RestResponseEntityException e, WebRequest request )
{
return new ResponseEntity<>( e.getMessage(), e.getHttpStatus() );
return new ResponseEntity<>( new RestError( e.getMessage() ), e.getHttpStatus() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ public class RestExceptionHandlerTest
@Test
public void test()
{
final ResponseEntity<String> entity = restExceptionHandler.handleResponseEntityException( new RestResourceNotFoundException( "This is a test." ), webRequest );
final ResponseEntity<RestError> entity = restExceptionHandler.handleResponseEntityException( new RestResourceNotFoundException( "This is a test." ), webRequest );
Assert.assertEquals( HttpStatus.NOT_FOUND, entity.getStatusCode() );
Assert.assertEquals( "This is a test.", entity.getBody() );
Assert.assertNotNull( entity.getBody() );
Assert.assertEquals( "This is a test.", entity.getBody().getMessage() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import javax.annotation.Nonnull;
import java.io.Serializable;
import java.util.Objects;
Expand Down Expand Up @@ -57,7 +60,8 @@ public static Reference createIdReference( @Nonnull String name )
* @param name the code or name to which the reference refers to.
* @param type the type of the reference (either a reference by code or by name).
*/
public Reference( @Nonnull String name, @Nonnull ReferenceType type )
@JsonCreator
public Reference( @Nonnull @JsonProperty( "value" ) String name, @Nonnull @JsonProperty( "type" ) ReferenceType type )
{
this.value = name;
this.type = type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.dhis2.fhir.adapter.dhis.model.DhisResourceType;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -60,14 +63,21 @@
"WHERE r.fhirResourceType=:fhirResourceType AND r.applicableCodeSet IS NULL AND r.enabled=true" ),
@NamedQuery( name = AbstractRule.FIND_RULES_BY_TYPE_CODES_NAMED_QUERY, query = "SELECT r FROM AbstractRule r JOIN r.applicableCodeSet acs JOIN acs.codeSetValues csv ON csv.enabled=true JOIN csv.id.code c " +
"JOIN c.systemCodes sc ON sc.systemCode=:code JOIN sc.system s ON s.systemUri=:system AND s.enabled=true WHERE r.fhirResourceType=:fhirResourceType AND r.enabled=true" ) } )
public abstract class AbstractRule extends BaseMetadata implements Serializable, Comparable<AbstractRule>
@JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "dhisResourceType" )
@JsonSubTypes( {
@JsonSubTypes.Type( value = TrackedEntityRule.class, name = "TRACKED_ENTITY" ),
@JsonSubTypes.Type( value = ProgramStageRule.class, name = "PROGRAM_STAGE_EVENT" )
} )
public abstract class AbstractRule extends VersionedBaseMetadata implements Serializable, Comparable<AbstractRule>
{
private static final long serialVersionUID = 3426378271314934021L;

public static final String FIND_RULES_BY_TYPE_NAMED_QUERY = "findRulesByType";

public static final String FIND_RULES_BY_TYPE_CODES_NAMED_QUERY = "findRulesByTypeAndCodes";

public static final int MAX_NAME_LENGTH = 230;

private String name;
private String description;
private boolean enabled;
Expand All @@ -78,6 +88,16 @@ public abstract class AbstractRule extends BaseMetadata implements Serializable,
private CodeSet applicableCodeSet;
private ExecutableScript transformInScript;

protected AbstractRule()
{
super();
}

protected AbstractRule( @Nonnull DhisResourceType dhisResourceType )
{
this.dhisResourceType = dhisResourceType;
}

@Basic
@Column( name = "name", nullable = false, length = 230 )
public String getName()
Expand Down Expand Up @@ -140,8 +160,9 @@ public void setFhirResourceType( FhirResourceType fhirResourceType )
}

@Basic
@Column( name = "dhis_resource_type", nullable = false, length = 30 )
@Column( name = "dhis_resource_type", nullable = false, updatable = false, length = 30 )
@Enumerated( EnumType.STRING )
@JsonProperty( access = JsonProperty.Access.READ_ONLY )
public DhisResourceType getDhisResourceType()
{
return dhisResourceType;
Expand Down
Loading

0 comments on commit a356f0a

Please sign in to comment.