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.Map; 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.OneToMany; 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") @Table(name = "webform_forms") public class Form implements Serializable { private static Log sLog = LogFactory.getLog(Form.class); /** * */ private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(unique = true, nullable = false) private Long id; @Column private Long companyId; @Column private String name; @Column private Boolean allowGuest; @Column private Boolean accessResults; @Column private Boolean allowMultiple; @Column(nullable = false, columnDefinition = "boolean default false") private Boolean template; @Column private Boolean userCopy; @Column private Boolean userinfo; @Column private Boolean anonymous; @Column private Boolean sendMail; @Column private String email; @Transient private Locale locale; @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "form", orphanRemoval = true) private Set<Field> fields; public Form() { } public Form(Long companyId, Locale locale, String name, Boolean template, Boolean allowGuest, Boolean accessResults, Boolean allowMultiple, Boolean userCopy, Boolean userinfo, Boolean anonymous, Boolean sendMail, String email, List<Field> fields) { this.name = name; this.allowGuest = allowGuest; this.accessResults = accessResults; this.allowMultiple = allowMultiple; this.userCopy = userCopy; this.userinfo = userinfo; this.anonymous = anonymous; this.sendMail = sendMail; this.email = email; this.companyId = companyId; this.fields = new HashSet<Field>(fields); this.template = template; this.locale = locale; } public Boolean getTemplate() { return template; } public void setTemplate(Boolean template) { this.template = template; } public Long getCompanyId() { return companyId; } public void setCompanyId(Long companyId) { this.companyId = companyId; } public Boolean getAnonymous() { return anonymous; } public void setAnonymous(Boolean anonymous) { this.anonymous = anonymous; } public Long getId() { return id; } public Boolean getUserinfo() { return userinfo; } public void setUserinfo(Boolean userinfo) { this.userinfo = userinfo; } public void setId(Long id) { this.id = id; } public Boolean getAccessResults() { return accessResults; } public void setAccessResults(Boolean accessResults) { this.accessResults = accessResults; } public Boolean getAllowMultiple() { return allowMultiple; } public void setAllowMultiple(Boolean allowMultiple) { this.allowMultiple = allowMultiple; } public Boolean getUserCopy() { return userCopy; } public void setUserCopy(Boolean userCopy) { this.userCopy = userCopy; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLocaleName() { Gson gson = new Gson(); try { Map<String, String> values = new HashMap<String, String>(); values = gson.fromJson(this.name, values.getClass()); if (values != null) { String locale_ = getLocale().toString(); String value = values.get(locale_); return value; } } catch (Exception e) { sLog.error("not a valid multilangual value " + this.name + " .. upgrading"); } return this.name; } public void setLocaleName(String name) { Gson gson = new Gson(); HashMap<String, String> values = new HashMap<String, String>(); try { values = gson.fromJson(this.name, values.getClass()); if (values == null) { values = new HashMap<String, String>(); } } catch (Exception e) { sLog.error("not a valid multilangual value " + this.name + " .. upgrading"); } String locale_ = getLocale().toString(); values.put(locale_, name); this.name = gson.toJson(values); } public Boolean getAllowGuest() { return allowGuest; } public void setAllowGuest(Boolean allowGuest) { this.allowGuest = allowGuest; } public Boolean getSendMail() { return sendMail; } public void setSendMail(Boolean sendMail) { this.sendMail = sendMail; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } // SETS public Set<Field> getFields() { return this.fields; } public void setFields(Set<Field> fields) { this.fields = fields; } // LISTS @Transient public List<Field> getSelectedFields() { List<Field> list = new ArrayList<Field>(this.fields); Collections.sort(list, fieldWeightComparator); return list; } @Transient public Locale getLocale() { this.locale = Library.getCurrentLocale(this.locale); return locale; } @Transient public void setLocale(Locale locale) { this.locale = locale; } @Transient public void setSelectedFields(List<Field> fields) { this.fields = new HashSet<Field>(fields); } @Transient public static Comparator<Field> fieldWeightComparator = new Comparator<Field>() { public int compare(Field f1, Field f2) { Integer x = 0; if (f1.getWeight() != null) { x = f1.getWeight(); } Integer y = 0; if (f2.getWeight() != null) { y = f2.getWeight(); } return x.compareTo(y); } }; }