Skip to contentPackage: BackupEntity
BackupEntity
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * SolidBlue 3: Data safety
5: * http://tidalwave.it/projects/solidblue3
6: *
7: * Copyright (C) 2023 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/solidblue3j-src
23: * git clone https://github.com/tidalwave-it/solidblue3j-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.datamanager.dao.impl.jpa;
28:
29: import jakarta.annotation.Nonnull;
30: import java.time.LocalDateTime;
31: import java.util.ArrayList;
32: import java.util.List;
33: import java.util.Objects;
34: import jakarta.persistence.Column;
35: import jakarta.persistence.Entity;
36: import jakarta.persistence.FetchType;
37: import jakarta.persistence.Id;
38: import jakarta.persistence.Index;
39: import jakarta.persistence.OneToMany;
40: import jakarta.persistence.OrderBy;
41: import jakarta.persistence.Table;
42: import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
43: import org.hibernate.Hibernate;
44: import org.hibernate.annotations.Cascade;
45: import org.hibernate.annotations.CascadeType;
46: import lombok.AllArgsConstructor;
47: import lombok.Getter;
48: import lombok.NoArgsConstructor;
49: import lombok.Setter;
50:
51: /***********************************************************************************************************************
52: *
53: * A JPA Entity for {@link it.tidalwave.datamanager.model.Backup}.
54: *
55: * @stereotype JPA Entity
56: * @author Fabrizio Giudici
57: *
58: **********************************************************************************************************************/
59: @Entity
60: @Table(name="backups", indexes = { @Index(name = "backups__id", columnList = "id"),
61: @Index(name = "backups__label", columnList = "label"),
62: @Index(name = "backups__volume_id", columnList = "volume_id")})
63: @NoArgsConstructor @AllArgsConstructor @Getter
64: @SuppressFBWarnings({"EI_EXPOSE_REP", "EI_EXPOSE_REP2"})
65: public class BackupEntity
66: {
67: @Id
68: @Column(length = 36)
69: private String id;
70:
71: @Column(unique = true, nullable = false, columnDefinition = "text")
72: private String label;
73:
74: @Column(name = "volume_id", length = 36, unique = true, nullable = false)
75: private String volumeId;
76:
77: private boolean encrypted;
78:
79: @Column(name = "base_path", nullable = false, columnDefinition = "text")
80: private String basePath;
81:
82: @Column(name = "creation_date", nullable = false)
83: private LocalDateTime creationDate;
84:
85: @Column(name = "registration_date", nullable = false)
86: private LocalDateTime registrationDate;
87:
88: @Column(name = "latest_check_date")
89: private LocalDateTime latestCheckDate;
90:
91: @Setter
92: @OneToMany(mappedBy = "backup", fetch = FetchType.LAZY)
93: @OrderBy("path asc")
94: @Cascade(CascadeType.PERSIST)
95: private List<BackupFileEntity> backupFiles = new ArrayList<>();
96:
97: public boolean isInitialized()
98: {
99: return Hibernate.isInitialized(backupFiles);
100: }
101:
102: @Override @Nonnull
103: public String toString()
104: {
105: return ("BackupEntity@%x(id=%s, label=%s, volumeId=%s, encrypted=%s, basePath=%s, creationDate=%s, " +
106: "registrationDate=%s, latestCheckDate=%s, backupFiles=%s)").formatted(
107: System.identityHashCode(this),
108: id, label, volumeId, encrypted, basePath, creationDate, registrationDate, latestCheckDate,
109:• Hibernate.isInitialized(backupFiles) ? backupFiles : "not initialized");
110: }
111:
112: // See https://vladmihalcea.com/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
113: @Override
114: public boolean equals (final Object other)
115: {
116:• if (this == other)
117: {
118: return true;
119: }
120:
121:• if (!(other instanceof BackupEntity))
122: {
123: return false;
124: }
125:
126:• return id != null && Objects.equals(id, ((BackupEntity)other).getId());
127: }
128:
129: @Override
130: public int hashCode()
131: {
132: return getClass().hashCode();
133: }
134: }