I decided to try out modeling each setting individually for now. Here is what I have implemented.
JSF
<label for="blog-desc">Blog Name</label> <h:inputText id="blog-name" value="#{configurationWriter.blogName}" > <f:ajax event="blur" /> </h:inputText> <label for="blog-desc">Blog Description</label> <h:inputText id="blog-desc" value="#{configurationWriter.blogDescription}" > <f:ajax event="blur" /> </h:inputText>
ConfigurationWriter.java
@Named("configurationWriter") @ViewScoped public class ConfigurationWriter implements Serializable { @Inject private ConfigurationDAOBean ps; private String blogName; private String blogDescription; @PostConstruct private void setup(){ blogName = ps.fetch("blog-name").getValue(); blogDescription = ps.fetch("blog-description").getValue(); } public void save(){ saveHelper("blog-name", blogName); saveHelper("blog-description", blogDescription); } private void saveHelper(String field, String value){ Configuration c = ps.fetch(field); c.setValue(value); ps.store(c); } public String getBlogName() { return blogName; } public void setBlogName(String blogName) { this.blogName = blogName; } public String getBlogDescription() { return blogDescription; } public void setBlogDescription(String blogDescription) { this.blogDescription = blogDescription; }
}
ConfigurationDAOBean.java
@Stateless public class ConfigurationDAOBean { @PersistenceContext private EntityManager em; public Configuration fetch(String item) { List<Configuration> results = em.createQuery("SELECT c FROM Configuration c WHERE c.name = :name", Configuration.class) .setParameter("name", item) .getResultList(); if (results.isEmpty()) { Configuration c = new Configuration(); c.setName(item); em.persist(c); return c; } return results.get(0); } public void store(Configuration c) { em.merge(c); } }I am open to ideas!
No comments:
Post a Comment