argos/dmp-backend/roadmap/src/main/java/eu/eudat/migration/entities/Question.java

249 lines
7.5 KiB
Java

package eu.eudat.migration.entities;
import eu.eudat.models.data.admin.components.datasetprofile.Field;
import eu.eudat.models.data.components.commons.DefaultValue;
import eu.eudat.models.data.components.commons.Visibility;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import javax.persistence.*;
import java.util.*;
import java.util.stream.Collectors;
@Entity
@Table(name = "questions")
public class Question {
public static Map<String, Long> fieldsQuestionsMap = new HashMap<>();
@Id
private Long id;
private String text;
@Column(name = "default_value")
private String defaultValue;
private Long number;
@Column(name = "created_at")
@Temporal(TemporalType.TIMESTAMP)
private Date createdAt;
@Column(name = "updated_at")
@Temporal(TemporalType.TIMESTAMP)
private Date updatedAt;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "question_format_id")
private QuestionFormat questionFormat;
@Column(name = "option_comment_display")
private Boolean optionCommentDisplay;
private Boolean modifiable;
@Column(name = "versionable_id")
private String versionableId;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "question_id")
@OrderBy("number")
@LazyCollection(LazyCollectionOption.FALSE)
private List<QuestionOption> options = new ArrayList<>();
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "question_id")
@OrderBy("number")
@LazyCollection(LazyCollectionOption.FALSE)
private List<Condition> conditions = new ArrayList<>();
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "questions_themes",
joinColumns = {@JoinColumn(name = "question_id")},
inverseJoinColumns = {@JoinColumn(name = "theme_id")}
)
@LazyCollection(LazyCollectionOption.FALSE)
private Set<Theme> themes = new HashSet<>();
@Override
public String toString() {
return "Question{" +
"id=" + id +
", text='" + text + '\'' +
", defaultValue='" + defaultValue + '\'' +
", number=" + number +
", createdAt=" + createdAt +
", updatedAt=" + updatedAt +
", questionFormat=" + questionFormat +
", optionCommentDisplay=" + optionCommentDisplay +
", modifiable=" + modifiable +
", versionableId='" + versionableId + '\'' +
", options=" + options +
", conditions=" + conditions +
'}';
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getDefaultValue() {
return defaultValue;
}
public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
}
public Long getNumber() {
return number;
}
public void setNumber(Long number) {
this.number = number;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
public QuestionFormat getQuestionFormat() {
return questionFormat;
}
public void setQuestionFormat(QuestionFormat questionFormat) {
this.questionFormat = questionFormat;
}
public Boolean getOptionCommentDisplay() {
return optionCommentDisplay;
}
public void setOptionCommentDisplay(Boolean optionCommentDisplay) {
this.optionCommentDisplay = optionCommentDisplay;
}
public Boolean getModifiable() {
return modifiable;
}
public void setModifiable(Boolean modifiable) {
this.modifiable = modifiable;
}
public String getVersionableId() {
return versionableId;
}
public void setVersionableId(String versionableId) {
this.versionableId = versionableId;
}
public List<QuestionOption> getOptions() {
return options;
}
public void setOptions(List<QuestionOption> options) {
this.options = options;
}
public List<Condition> getConditions() {
return conditions;
}
public void setConditions(List<Condition> conditions) {
this.conditions = conditions;
}
public Set<Theme> getThemes() {
return themes;
}
public void setThemes(Set<Theme> themes) {
this.themes = themes;
}
public String buildDescription() {
StringBuilder sb = new StringBuilder();
themes.forEach(theme -> {
if(theme.getTitle() != null) {
sb.append("<b>").append(theme.getTitle()).append("</b><br>");
}
if(theme.getDescription() != null) {
sb.append("<p>").append(theme.getDescription()).append("</p>");
}
if(theme.getGuidances().size() > 0) {
theme.getGuidances().forEach(guidance -> {
sb.append("<b>").append(guidance.getGuidanceGroup().getName()).append("</b><br>");
sb.append("<p>").append(guidance.getText()).append("</p>");
});
}
});
return sb.toString();
}
public List<Field> buildFields() {
Field field = new Field();
field.setId(String.valueOf(UUID.randomUUID()));
field.setOrdinal(0);
field.setDefaultValue(this.buildDefaultValue());
field.setValue(field.getDefaultValue().getValue());
field.setValidations(this.buildValidators());
field.setVisible(this.buildVisibility());
field.setViewStyle(this.getQuestionFormat().buildViewStyle());
field.setData(this.buildData());
fieldsQuestionsMap.put(field.getId(), getId());
return Collections.singletonList(field);
}
private DefaultValue buildDefaultValue() {
DefaultValue defaultValue = new DefaultValue();
defaultValue.setType("String");
defaultValue.setValue((this.defaultValue != null)?this.defaultValue :"");
return defaultValue;
}
private List<Integer> buildValidators() {
return Collections.singletonList((int) Field.ValidationType.REQUIRED.getValue());
}
private Visibility buildVisibility() {
Visibility visibility = new Visibility();
visibility.setRules(new ArrayList<>());
visibility.setStyle("");
return visibility;
}
private Map<String, Object> buildData() {
Map<String, Object> object = new HashMap<>();
object.put("label", "");
if(this.getQuestionFormat().getOptionBased()) {
object.put("options", this.getOptions().stream().map(questionOption -> {
Map<String, Object> option = new HashMap<>();
option.put(questionOption.getText(), questionOption.getText());
return option;
}).collect(Collectors.toList()));
}
if(this.getQuestionFormat().getFormattype() == QuestionFormat.FormatType.DROPDOWN ||
this.getQuestionFormat().getFormattype() == QuestionFormat.FormatType.MULTI_SELECT) {
object.put("type", "wordlist");
object.put("multiList", this.getQuestionFormat().getFormattype() == QuestionFormat.FormatType.MULTI_SELECT);
}
return object;
}
}