UnitError.java

  1. /*
  2.  * Copyright 2005-2025 the original author or authors.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  * http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package org.openwms.common.transport;

  17. import jakarta.persistence.CascadeType;
  18. import jakarta.persistence.Column;
  19. import jakarta.persistence.Entity;
  20. import jakarta.persistence.ForeignKey;
  21. import jakarta.persistence.JoinColumn;
  22. import jakarta.persistence.ManyToOne;
  23. import jakarta.persistence.Table;
  24. import org.ameba.integration.jpa.ApplicationEntity;

  25. import java.io.Serializable;
  26. import java.util.Objects;

  27. /**
  28.  * An UnitError represents an error occurring on a {@code TransportUnit}.
  29.  *
  30.  * @author Heiko Scherrer
  31.  */
  32. @Entity
  33. @Table(name = "COM_UNIT_ERROR")
  34. public class UnitError extends ApplicationEntity implements Serializable {

  35.     /** Separator to use in toString method. */
  36.     static final String SEPARATOR = "::";

  37.     /** Error number. */
  38.     @Column(name = "C_ERROR_NO")
  39.     private String errorNo;

  40.     /** Error message text. */
  41.     @Column(name = "C_ERROR_TEXT")
  42.     private String errorText;

  43.     @ManyToOne(optional = false, cascade = CascadeType.PERSIST)
  44.     @JoinColumn(name = "C_TU_ID", foreignKey = @ForeignKey(name = "FK_TU_ERROR_PK"))
  45.     private TransportUnit transportUnit;

  46.     /*~ ----------------------------- constructors ------------------- */

  47.     /** Dear JPA... */
  48.     protected UnitError() {}

  49.     private UnitError(Builder builder) {
  50.         this.errorNo = builder.errorNo;
  51.         this.errorText = builder.errorText;
  52.         this.transportUnit = builder.transportUnit;
  53.     }

  54.     public static Builder newBuilder() {
  55.         return new Builder();
  56.     }

  57.     /*~ ----------------------------- accessors ------------------- */
  58.     public String getErrorNo() {
  59.         return errorNo;
  60.     }

  61.     public String getErrorText() {
  62.         return errorText;
  63.     }

  64.     /**
  65.      * Set the TransportUnit for this error.
  66.      *
  67.      * @param transportUnit The TransportUnit instance
  68.      */
  69.     void setTransportUnit(TransportUnit transportUnit) {
  70.         this.transportUnit = transportUnit;
  71.     }

  72.     public TransportUnit getTransportUnit() {
  73.         return transportUnit;
  74.     }

  75.     /**
  76.      * {@inheritDoc}
  77.      */
  78.     @Override
  79.     public boolean equals(Object o) {
  80.         if (this == o) return true;
  81.         if (o == null || getClass() != o.getClass()) return false;
  82.         UnitError unitError = (UnitError) o;
  83.         return Objects.equals(errorNo, unitError.errorNo) &&
  84.                 Objects.equals(errorText, unitError.errorText);
  85.     }

  86.     /**
  87.      * {@inheritDoc}
  88.      */
  89.     @Override
  90.     public int hashCode() {
  91.         return Objects.hash(errorNo, errorText);
  92.     }

  93.     /**
  94.      * {@inheritDoc}
  95.      */
  96.     @Override
  97.     public String toString() {
  98.         return errorNo + SEPARATOR + errorText;
  99.     }


  100.     /**
  101.      * {@code UnitError} builder static inner class.
  102.      */
  103.     public static final class Builder {

  104.         private String errorNo;
  105.         private String errorText;
  106.         private TransportUnit transportUnit;

  107.         private Builder() {
  108.         }

  109.         public Builder errorNo(String val) {
  110.             errorNo = val;
  111.             return this;
  112.         }

  113.         public Builder errorText(String val) {
  114.             errorText = val;
  115.             return this;
  116.         }

  117.         public Builder transportUnit(TransportUnit val) {
  118.             transportUnit = val;
  119.             return this;
  120.         }

  121.         public UnitError build() {
  122.             return new UnitError(this);
  123.         }
  124.     }
  125. }