package at.gv.egiz.moazs; import at.gv.zustellung.msg.xsd.DeliveryAnswerType; import at.gv.zustellung.msg.xsd.PreAdviceNoteSentType; import at.gv.zustellung.tnvz.xsd.PersonResultType; import org.springframework.lang.Nullable; public class MoaZSException extends RuntimeException { public static final String ERROR_MZS_MIMETYPE_MISSMATCH = "8001"; public static final String ERROR_MZS_NO_TNVZ_PERSON_QUERY_RESULTS = "8002"; public static final String ERROR_MOASP_SIGNATURE_INVALID = "7001"; @Nullable private final String errorCode; @Nullable private final PreAdviceNoteSentType preAdviceNoteSent; @Nullable private final String deliverySystem; @Nullable private final String gz; @Nullable private final String zsDeliveryID; @Nullable private final String appDeliveryID; private MoaZSException(Builder builder) { super(builder.message, builder.cause); this.errorCode = builder.errorCode; this.preAdviceNoteSent = builder.preAdviceNoteSent; this.deliverySystem = builder.deliverySystem; this.gz = builder.gz; this.zsDeliveryID = builder.zsDeliveryID; this.appDeliveryID = builder.appDeliveryID; } public static MoaZSException moaZSException(String message, Throwable cause) { return moaZSExceptionBuilder(message).withCause(cause).build(); } public static MoaZSException moaZSException(String formatString, Object... objects) { return moaZSExceptionBuilder(formatString, objects).build(); } public static MoaZSException moaZSException(String message) { return moaZSExceptionBuilder(message).build(); } public static Builder moaZSExceptionBuilder(String formatString, Object... objects) { return new Builder().withMessage(String.format(formatString, objects)); } public static Builder moaZSExceptionBuilder(String message) { return new Builder().withMessage(message); } public static Builder moaZSExceptionBuilder() { return new Builder(); } @Nullable public String getErrorCode() { return errorCode; } @Nullable public PreAdviceNoteSentType getPreAdviceNoteSent() { return preAdviceNoteSent; } @Nullable public String getDeliverySystem() { return deliverySystem; } @Nullable public String getGz() { return gz; } @Nullable public String getZsDeliveryID() { return zsDeliveryID; } @Nullable public String getAppDeliveryID() { return appDeliveryID; } public static class Builder { private String message; private Throwable cause; private String errorCode; private PreAdviceNoteSentType preAdviceNoteSent; private String deliverySystem; private String gz; private String zsDeliveryID; private String appDeliveryID; private Builder() { } public Builder(MoaZSException exception){ this.message = exception.getMessage(); this.cause = exception.getCause(); this.errorCode = exception.getErrorCode(); this.preAdviceNoteSent = exception.getPreAdviceNoteSent(); this.deliverySystem = exception.getDeliverySystem(); this.gz = exception.getGz(); this.zsDeliveryID = exception.getZsDeliveryID(); this.appDeliveryID = exception.getAppDeliveryID(); } public Builder withMessage(String message) { this.message = message; return this; } public Builder withMessage(String formatString, Object... objects ) { this.message = String.format(formatString, objects); return this; } public Builder withCause(Throwable cause) { this.cause = cause; return this; } public Builder withErrorCode(String errorCode) { this.errorCode = errorCode; return this; } public Builder withPreAdviceNoteSent(PersonResultType personResult) { if (personResult.getError() != null) { this.preAdviceNoteSent = personResult.getError().getPreAdviceNoteSent(); } return this; } public Builder withDeliverySystem(at.gv.zustellung.app2mzs.xsd.DeliveryRequestType mzsRequest) { this.deliverySystem = mzsRequest.getConfig().getMSGClient().getURL(); return this; } public Builder withAllParametersInAnswer(DeliveryAnswerType answer) { this.deliverySystem = answer.getDeliverySystem(); this.gz = answer.getGZ(); this.zsDeliveryID = answer.getZSDeliveryID(); this.appDeliveryID = answer.getAppDeliveryID(); return this; } public Builder withAppDeliveryID(String appDeliveryID) { this.appDeliveryID = appDeliveryID; return this; } public MoaZSException build() { return new MoaZSException(this); } } }