package be.redtree.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import be.redtree.library.Library;
import com.google.gson.Gson;
@Entity(name = "webform_fields")
@Table(name = "webform_fields")
public class Field implements Serializable {
private static Log sLog = LogFactory.getLog(SingleValue.class);
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(unique = true, nullable = false)
private Long id;
@Column
private String uuid;
@Column(columnDefinition = "TEXT")
private String name;
@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
public Form form;
@OneToOne(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "field", orphanRemoval = true)
private SingleValue value;
@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "field", orphanRemoval = true)
public Set<MultiValue> values;
// Ordering
@Column
private Integer weight;
// Validation
@Column
private Boolean mandatory;
@Column(columnDefinition = "TEXT")
private String message;
// Upload field extra fields
@Column
private Integer size;
@Column(columnDefinition = "TEXT")
private String formats;
/*
* Layout fields
*/
@Column(columnDefinition = "TEXT")
private String title;
@Column
private String style;
@Column(columnDefinition = "TEXT")
private String css;
/*
* Fields for form building
*/
@Transient
public String tempValue;
@Transient
private Locale localeTitle;
@Transient
private Locale localeValue;
@Transient
private Locale localeMessage;
public Field() {
}
public Field(String uuid, String name, Form form, SingleValue value, List<MultiValue> values, Integer weight, Boolean mandatory, String message, String title, String style, String css, String tempValue, Integer size, String formats) {
this.uuid = uuid;
this.name = name;
this.form = form;
this.value = value;
this.values = new HashSet<MultiValue>(values);
this.weight = weight;
this.mandatory = mandatory;
this.message = message;
this.title = title;
this.style = style;
this.tempValue = tempValue;
this.size = size;
this.formats = formats;
this.css = css;
}
public List<MultiValue> getValues() {
List<MultiValue> list = new ArrayList<MultiValue>();
if (this.values != null) {
list = new ArrayList<MultiValue>(this.values);
if (this.values != null && !this.values.isEmpty()) {
Collections.sort(list, fieldWeightComparator);
}
}
return list;
}
public void setValues(Set<MultiValue> values) {
this.values = values;
}
// Locales
@Transient
public Locale getLocaleTitle() {
this.localeTitle = Library.getCurrentLocale(this.localeTitle);
return localeTitle;
}
@Transient
public Locale getLocaleValue() {
this.localeValue = Library.getCurrentLocale(this.localeValue);
return localeValue;
}
@Transient
public Locale getLocaleMessage() {
this.localeMessage = Library.getCurrentLocale(this.localeMessage);
return localeMessage;
}
@Transient
public void setLocaleTitle(Locale localeTitle) {
this.localeTitle = localeTitle;
}
@Transient
public void setLocaleValue(Locale localeValue) {
this.localeValue = localeValue;
}
@Transient
public void setLocaleMessage(Locale localeMessage) {
this.localeMessage = localeMessage;
}
// Comparator
@Transient
public static Comparator<MultiValue> fieldWeightComparator = new Comparator<MultiValue>() {
public int compare(MultiValue f1, MultiValue f2) {
return f1.getWeight().compareTo(f2.getWeight());
}
};
// Locale Temp
public String getTempValue() {
return Library.getLocaleVersion(getLocaleValue(), this.tempValue);
}
public void setTempValue(String tempValue) {
String json = this.tempValue;
HashMap<String, String> values = Library.getJsonMapping(json);
values.put(getLocaleValue().toString(), tempValue);
this.tempValue = new Gson().toJson(values);
}
// Locale Message
public String getMessageLocale() {
return Library.getLocaleVersion(getLocaleMessage(), this.message);
}
public String getJSONMessage() {
return this.message;
}
public void setMessageLocale(String message) {
String json = this.message;
HashMap<String, String> values = Library.getJsonMapping(json);
values.put(getLocaleMessage().toString(), message);
this.message = new Gson().toJson(values);
}
// Locale Title
public String getTitle() {
if (this.title == null || this.title.isEmpty()) {
return this.name;
}
return Library.getLocaleVersion(getLocaleTitle(), this.title);
}
public String getJSONTitle() {
return this.title;
}
public void setTitle(String title) {
HashMap<String, String> values = Library.getJsonMapping(this.title);
values.put(getLocaleTitle().toString(), title);
this.title = new Gson().toJson(values);
}
// GETS AND SETS
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public String getCss() {
return css;
}
public void setCss(String css) {
this.css = css;
}
public Integer getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
public String getFormats() {
return formats;
}
public void setFormats(String formats) {
this.formats = formats;
}
public Boolean getMandatory() {
return mandatory;
}
public void setMandatory(Boolean mandatory) {
this.mandatory = mandatory;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public SingleValue getValue() {
return value;
}
public void setValue(SingleValue value) {
this.value = value;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Form getForm() {
return form;
}
public void setForm(Form form) {
this.form = form;
}
}