Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Commit

Permalink
Added more check and conversions for tracked entity instances.
Browse files Browse the repository at this point in the history
  • Loading branch information
volsch committed Oct 8, 2018
1 parent 3b56688 commit c3efc1c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public class TrackedEntityServiceImpl implements TrackedEntityService
protected static final String FIND_BY_ATTR_VALUE_URI = "/trackedEntityInstances.json?" +
"trackedEntityType={typeId}&ouMode=ACCESSIBLE&filter={attrId}:EQ:{attrValue}&pageSize={maxResult}";

protected static final int MAX_RESERVE_RETRIES = 10;

private final RestTemplate restTemplate;

@Autowired
Expand All @@ -77,8 +79,28 @@ public TrackedEntityServiceImpl( @Nonnull @Qualifier( "userDhis2RestTemplate" )
public TrackedEntityInstance createNewInstance( @Nonnull TrackedEntityType type )
{
final TrackedEntityInstance instance = new TrackedEntityInstance( type.getId(), null, true );
type.getAttributes().stream().filter( a -> a.getAttribute().isGenerated() ).forEach( a -> instance.getAttributes().add(
new TrackedEntityAttributeValue( a.getAttributeId(), getReservedValue( a.getAttributeId() ) ) ) );
type.getAttributes().stream().filter( a -> a.getAttribute().isGenerated() ).forEach( a -> {
boolean retry = true;
for ( int i = 0; (i < MAX_RESERVE_RETRIES) && retry; i++ )
{
final String reservedValue = getReservedValue( a.getAttributeId() );
retry = false;
switch ( a.getValueType() )
{
case INTEGER:
case INTEGER_NEGATIVE:
case INTEGER_POSITIVE:
case INTEGER_ZERO_OR_POSITIVE:
case NUMBER:
retry = reservedValue.startsWith( "0" );
break;
}
if ( !retry )
{
instance.getAttributes().add( new TrackedEntityAttributeValue( a.getAttributeId(), reservedValue ) );
}
}
} );
return instance;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

public class StringToLocationConverter extends TypedConverter<String, Location>
{
private final Pattern locationPattern = Pattern.compile( "\\[\\s*([\\d.]+)\\s*,\\s*([\\d.]+)\\s*\\]" );
private final Pattern locationPattern = Pattern.compile( "\\s*\\[\\s*([+\\-\\d.eE]+)\\s*,\\s*([+\\-\\d.eE]+)\\s*]\\s*" );

public StringToLocationConverter()
{
Expand Down

0 comments on commit c3efc1c

Please sign in to comment.