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 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 options = new ArrayList<>(); @OneToMany(cascade = CascadeType.ALL) @JoinColumn(name = "question_id") @OrderBy("number") @LazyCollection(LazyCollectionOption.FALSE) private List 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 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 getOptions() { return options; } public void setOptions(List options) { this.options = options; } public List getConditions() { return conditions; } public void setConditions(List conditions) { this.conditions = conditions; } public Set getThemes() { return themes; } public void setThemes(Set themes) { this.themes = themes; } public String buildDescription() { StringBuilder sb = new StringBuilder(); themes.forEach(theme -> { if(theme.getTitle() != null) { sb.append("").append(theme.getTitle()).append("
"); } if(theme.getDescription() != null) { sb.append("

").append(theme.getDescription()).append("

"); } if(theme.getGuidances().size() > 0) { theme.getGuidances().forEach(guidance -> { sb.append("").append(guidance.getGuidanceGroup().getName()).append("
"); sb.append("

").append(guidance.getText()).append("

"); }); } }); return sb.toString(); } public List 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 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 buildData() { Map object = new HashMap<>(); object.put("label", ""); if(this.getQuestionFormat().getOptionBased()) { object.put("options", this.getOptions().stream().map(questionOption -> { Map 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; } }