Skip to content

Commit

Permalink
5364-app - Add Support for additional core fields
Browse files Browse the repository at this point in the history
general improvements
* JsonRetrieverService - fix NPE-bug when a location has no GLN
* RestResponseEntityExceptionHandler - also write exceptions to the log
* JsonResponseBPartner, JsonResponseContact, JsonResponseLocation - make sure that the changeInfo is last (cosmetic)

#5364
  • Loading branch information
metas-ts committed Jul 4, 2019
1 parent 2538352 commit b28b545
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,16 @@
import de.metas.rest_api.bpartner.response.JsonResponseBPartner;
import de.metas.rest_api.bpartner.response.JsonResponseComposite;
import de.metas.rest_api.bpartner.response.JsonResponseComposite.JsonResponseCompositeBuilder;
import de.metas.rest_api.bpartner.response.JsonResponseContact;
import de.metas.rest_api.bpartner.response.JsonResponseLocation;
import de.metas.rest_api.changelog.JsonChangeInfo;
import de.metas.rest_api.changelog.JsonChangeInfo.JsonChangeInfoBuilder;
import de.metas.rest_api.changelog.JsonChangeLogItem;
import de.metas.rest_api.changelog.JsonChangeLogItem.JsonChangeLogItemBuilder;
import de.metas.rest_api.bpartner.response.JsonResponseContact;
import de.metas.rest_api.bpartner.response.JsonResponseLocation;
import de.metas.rest_api.utils.IdentifierString;
import de.metas.rest_api.utils.JsonConverters;
import de.metas.rest_api.utils.JsonExternalIds;
import de.metas.user.UserId;

import de.metas.util.collections.CollectionUtils;
import de.metas.util.rest.ExternalId;
import lombok.Getter;
Expand Down Expand Up @@ -147,7 +146,7 @@ public JsonRetrieverService(
this.recordChangeLogRepository = recordChangeLogRepository;
this.identifier = identifier;

this.cache = new BPartnerCompositeCache(identifier);
cache = new BPartnerCompositeCache(identifier);
}

public Optional<JsonResponseComposite> retrieveJsonBPartnerComposite(@NonNull final String bpartnerIdentifierStr)
Expand Down Expand Up @@ -438,9 +437,9 @@ private final Collection<BPartnerCompositeLookupKey> extractBPartnerLookupKeys(@
}
}

for (BPartnerLocation location : bPartnerComposite.getLocations())
for (final BPartnerLocation location : bPartnerComposite.getLocations())
{
if (isEmpty(location.getGln(), true))
if (!isEmpty(location.getGln(), true))
{
result.add(BPartnerCompositeLookupKey.ofGln(location.getGln().trim()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import static de.metas.util.lang.CoalesceUtil.coalesceSuppliers;

import org.adempiere.exceptions.DBUniqueConstraintException;
import org.slf4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import de.metas.bpartner.service.BPartnerIdNotFoundException;
import de.metas.logging.LogManager;
import lombok.NonNull;

/*
Expand Down Expand Up @@ -36,51 +38,62 @@
@ControllerAdvice
public class RestResponseEntityExceptionHandler
{

private static final Logger logger = LogManager.getLogger(RestResponseEntityExceptionHandler.class);

@ExceptionHandler(BPartnerIdNotFoundException.class)
public ResponseEntity<String> handleBPartnerInfoNotFoundException(@NonNull final BPartnerIdNotFoundException e)
{
logger.error(e.getMessage(), e);
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
}

@ExceptionHandler(PermissionNotGrantedException.class)
public ResponseEntity<String> handlePermissionNotGrantedException(@NonNull final PermissionNotGrantedException e)
{
logger.error(e.getMessage(), e);
return new ResponseEntity<>(e.getMessage(), HttpStatus.FORBIDDEN);
}

@ExceptionHandler(MissingPropertyException.class)
public ResponseEntity<String> handleMissingPropertyException(@NonNull final MissingPropertyException e)
{
logger.error(e.getMessage(), e);
return new ResponseEntity<>(e.getMessage(), HttpStatus.UNPROCESSABLE_ENTITY);
}

@ExceptionHandler(MissingResourceException.class)
public ResponseEntity<String> handleMissingPropertyException(@NonNull final MissingResourceException e)
{
logger.error(e.getMessage(), e);
return new ResponseEntity<>(e.getMessage(), HttpStatus.UNPROCESSABLE_ENTITY);
}

@ExceptionHandler(InvalidIdentifierException.class)
public ResponseEntity<String> InvalidIdentifierException(@NonNull final InvalidIdentifierException e)
{
final String msg = "Invalid identifier: " + e.getMessage();
logger.error(msg, e);
if (e.getMessage().startsWith("tea"))
{
return new ResponseEntity<>("Invalid identifier: " + e.getMessage(), HttpStatus.I_AM_A_TEAPOT); // whohoo, finally!
return new ResponseEntity<>(msg, HttpStatus.I_AM_A_TEAPOT); // whohoo, finally!
}
return new ResponseEntity<>("Invalid identifier: " + e.getMessage(), HttpStatus.NOT_FOUND);
return new ResponseEntity<>(msg, HttpStatus.NOT_FOUND);
}

@ExceptionHandler(DBUniqueConstraintException.class)
public ResponseEntity<String> handleDBUniqueConstraintException(@NonNull final DBUniqueConstraintException e)
{
final String msg = "At least one record already existed in the system:" + e.getMessage();
logger.error(msg, e);
return new ResponseEntity<>(msg, HttpStatus.UNPROCESSABLE_ENTITY);
}

@ExceptionHandler(Exception.class)
public ResponseEntity<String> handleException(@NonNull final Exception e)
{
final String msg = coalesceSuppliers(() -> e.getMessage(), () -> e.getClass().getSimpleName());
logger.error(msg, e);
return new ResponseEntity<>(msg, HttpStatus.INTERNAL_SERVER_ERROR);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public class JsonResponseBPartner
String group;

@JsonInclude(Include.NON_NULL)
@ApiModelProperty(position = 20) // shall be last
JsonChangeInfo changeInfo;

@JsonCreator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public class JsonResponseContact
String phone;

@JsonInclude(Include.NON_NULL)
@ApiModelProperty(position = 20) // shall be last
JsonChangeInfo changeInfo;

@Builder(toBuilder = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public class JsonResponseLocation
private String gln;

@JsonInclude(Include.NON_NULL)
@ApiModelProperty(position = 20) // shall be last
JsonChangeInfo changeInfo;

@Builder(toBuilder = true)
Expand Down

0 comments on commit b28b545

Please sign in to comment.