Dans ce troisième article, nous allons nous intéresser aux entités de l’application.
Entités persistées#
Dans LoFiDroX, il y a deux types d’entités: celles qui sont des représentations des données en base et celles qui sont des représentations des contenus JSON échangés avec le front-end. Nous allons nous intéresser au premier type, composé de trois entités.
Entité FileDescriptor#
Cette entité représente un fichier qui a été uploadé par un utilisateur.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
| package ch.gobothegeek.lofidrox.model.entities;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Objects;
// Described a user in this application
@Entity
@Table(name = "PUBLIC.PUBLIC.FILEDESCRIPTOR")
@IdClass(FileDescriptorPK.class)
public class FileDescriptor implements Serializable {
@Id @Column(name = "ID") @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; // unique file number
@Column(name = "NAME") private String name; // filename to show
@Column(name = "FILEPATH") private String path; // path to access file on disk
@Column(name = "USERFROM") private String source; // user who sent this file
@Column(name = "SENTON") @Temporal(TemporalType.TIMESTAMP) private Date sendOn; // timestamp when file was received on server
@Column(name = "DATATYPE") private String dataType; // type of base64 data
@OneToMany(fetch = FetchType.EAGER, targetEntity = FileRecipient.class, mappedBy = "file")
private List<FileRecipient> recipients;
public FileDescriptor() { }
public FileDescriptor(Integer id, String name, String path, String source, Date sendOn, String dataType) {
this.id = id;
this.name = name;
this.path = path;
this.source = source;
this.sendOn = sendOn;
this.dataType = dataType;
}
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getPath() { return path; }
public void setPath(String path) { this.path = path; }
public String getSource() { return source; }
public void setSource(String source) { this.source = source; }
public Date getSendOn() { return sendOn; }
public void setSendOn(Date sendOn) { this.sendOn = sendOn; }
public String getDataType() { return dataType; }
public void setDataType(String dataType) { this.dataType = dataType; }
public List<FileRecipient> getRecipients() { return recipients; }
public void setRecipients(List<FileRecipient> recipients) { this.recipients = recipients; }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FileDescriptor file = (FileDescriptor) o;
return Objects.equals(path, file.path);
}
@Override
public int hashCode() {
return Objects.hash(path);
}
@Override
public String toString() {
return "FileDb{" + "id=" + id + ", name='" + name + '\'' + ", path='" + path + '\'' + ", source='" + source + '\'' + '}';
}
}
|
Cette entité est définie comme ceci:
- id: un identifiant unique auto-incrémenté.
- name: le nom du fichier qui sera affiché.
- path: le chemin vers le fichier (path ne contient pas name).
- source: l’identifiant de l’utilisateur qui a déposé le fichier.
- sentOn: la date de dépôt du fichier.
- dataType: le mime-type du fichier.
Cette entité possède également un mapping nommé recipients
qui contient la liste des destinataires du fichier.
Entité FileRecipient#
Cette entité représente le destinataire d’un fichier
1
| package ch.gobothegeek.lofidrox.model.entities;import javax.persistence.*;import java.io.Serializable;import java.util.Objects;// Described a link between a file and a receiver@Entity@Table(name = "PUBLIC.PUBLIC.FILERECIPIENT")@IdClass(FileRecipientPK.class)public class FileRecipient implements Serializable { @Id @Column(name = "ID") @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; // unique recipient number @Column(name = "FILEID") private Integer fileId; // id of file linked @Column(name = "USERTO") private String userTo; // username who can access this file @Column(name = "DOWNLOADED") private Boolean downloaded = Boolean.FALSE; // true if user has downloaded file @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "FILEID", referencedColumnName = "ID") private FileDescriptor file; // file linked public FileRecipient() { } public FileRecipient(Integer id, Integer fileId, String userTo, Boolean downloaded) { this.id = id; this.fileId = fileId; this.userTo = userTo; this.downloaded = downloaded; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getFileId() { return fileId; } public void setFileId(Integer fileId) { this.fileId = fileId; } public String getUserTo() { return userTo; } public void setUserTo(String userTo) { this.userTo = userTo; } public Boolean getDownloaded() { return downloaded; } public void setDownloaded(Boolean downloaded) { this.downloaded = downloaded; } public FileDescriptor getFile() { return file; } public void setFile(FileDescriptor file) { this.file = file; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; FileRecipient fileRecipient = (FileRecipient) o; return Objects.equals(getFileId(), fileRecipient.getFileId()) && Objects.equals(getUserTo(), fileRecipient.getUserTo()); } @Override public int hashCode() { return Objects.hash(getFileId(), getUserTo()); } @Override public String toString() { return "FileRecipient{" + "id=" + id + ", fileId=" + fileId + ", userTo='" + userTo + '\'' + ", downloaded=" + downloaded + '}'; }}
|
Elle est composée de 4 champs et un mapping:
- id: un identifiant auto-généré.
- fileId: l’identifiant du fichier (FileDescriptor) concerné.
- userTo: l’identifiant de l’utilisateur qui reçoit le fichier.
- downloaded: un booléen indiquant si le destinataire a téléchargé le fichier.
- file: un mapping vers le FileDescriptor référencé par fileId.
Entité User#
Cette entité représente un utilisateur de LoFiDroX.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
| package ch.gobothegeek.lofidrox.model.entities;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Objects;
// Described a user in this application
@Entity
@Table(name = "PUBLIC.PUBLIC.USERS")
@IdClass(UserPK.class)
public class User implements Serializable {
@Id @Column(name="USERNAME") private String username; // username for this account
@Column(name="PWD") private String pwd; // encrypted password
public User() { }
public User(String username, String pwd) {
this.username = username;
this.pwd = pwd;
}
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getPwd() { return pwd; }
public void setPwd(String pwd) { this.pwd = pwd; }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User that = (User) o;
return Objects.equals(username, that.username);
}
@Override
public int hashCode() {
return Objects.hash(username);
}
@Override
public String toString() {
return "UserEntity{" + "username='" + username + '\'' + ", pwd='" + pwd + '\'' + '}';
}
}
|
Ici il n’y a que deux champs:
- username: l’identifiant de l’utilisateur
- pwd: son mot de passe chiffré.
Conclusion#
Les entités de LoFiDroX sont simples, se stockent dans HSqlDB sans difficulté et se manipulent aisément avec Apache Deltaspike (comme nous le verrons dans les prochains articles).
Code source#
Comme annoncé dans l’article Fuyez GitHub, le code n’est plus disponible sur GitHub. L’intégralité du code est disponible sur mon CodeBerg: https://codeberg.org/GoboTheGeek/LoFiDroX
C’est par ici