77 lines
2.1 KiB
Java
77 lines
2.1 KiB
Java
package entities;
|
|
|
|
import notificationsDecorations.NotificationDecoration;
|
|
import utils.NotificationState;
|
|
import utils.SendNotificationStrategy;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public abstract class Notification {
|
|
protected String content;
|
|
protected User user;
|
|
protected List<NotificationDecoration> decorations;
|
|
|
|
/**
|
|
* Pas de change state car chemin multiple. On reste sur des enum !
|
|
*/
|
|
protected NotificationState state;
|
|
protected SendNotificationStrategy strategy;
|
|
|
|
public Notification(String content, User user, SendNotificationStrategy strategy) {
|
|
this.content = content;
|
|
this.user = user;
|
|
this.state = NotificationState.PENDING;
|
|
this.strategy = strategy;
|
|
this.decorations = new ArrayList<>();
|
|
}
|
|
|
|
/**
|
|
* Adds a decoration to the notification. Please use a decorator for that
|
|
* @param decoration
|
|
*/
|
|
public void addDecoration(NotificationDecoration decoration) {
|
|
decorations.add(decoration);
|
|
}
|
|
|
|
protected boolean isSendable() {
|
|
return this.state != NotificationState.SENT && this.state != NotificationState.READ;
|
|
}
|
|
|
|
/**
|
|
* Transition d'état spéciale pour indiquer que l'envoie de notification est en échec
|
|
*/
|
|
public void setKoState() {
|
|
state = NotificationState.KO;
|
|
}
|
|
|
|
/**
|
|
* Fait la transition des états
|
|
*/
|
|
public void transitionState() {
|
|
switch (state) {
|
|
case PENDING -> state = NotificationState.SENT;
|
|
case SENT -> state = NotificationState.READ;
|
|
}
|
|
}
|
|
|
|
public String getContent() {
|
|
if (isSendable() && content != "") {
|
|
transitionState();
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
if (decorations.isEmpty())
|
|
return content;
|
|
|
|
builder.append(content);
|
|
|
|
for (final NotificationDecoration decoration : decorations)
|
|
builder.append(decoration.transformContent());
|
|
|
|
return builder.toString();
|
|
}
|
|
setKoState();
|
|
return "";
|
|
}
|
|
}
|