001 package de.jw.cloud42.core.domain;
002
003 import java.util.List;
004
005 import javax.persistence.Entity;
006 import javax.persistence.Id;
007
008 import org.hibernate.Query;
009 import org.hibernate.Session;
010
011
012 import de.jw.cloud42.core.hibernate.HibernateUtil;
013
014 /**
015 * Holds global application settings.
016 * Uses singleton pattern to keep instance state during application lifetime and to avoid fetching settings from
017 * database on each call/instantiation.
018 *
019 * @author fbitzer
020 *
021 */
022 @Entity
023 public class Settings {
024
025 private static Settings theInstance;
026
027 //Id field for hibernate, should always be 1
028 private int id;
029
030 /**
031 * The address of the Cloud42 message endpoint (used for Notification Mechanism in Web service layer)
032 */
033 private String endpointAddress;
034
035
036
037 /**
038 * Singleton method, loads settings from database if called for the first time.
039 * @return
040 */
041 public static Settings getInstance(){
042 if (theInstance == null){
043
044 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
045
046 session.beginTransaction();
047
048 Query q = session.createQuery("SELECT s FROM Settings s WHERE s.id = :sid");
049 q.setParameter("sid", 1);
050
051 List<Settings> resultset = q.list();
052
053
054 session.getTransaction().commit();
055
056 if (resultset.size()>0) {
057 theInstance = resultset.get(0);
058 } else {
059 //no entry in database, so create new and empty settings, but set Id to 1
060 theInstance = new Settings();
061 theInstance.setId(1);
062 }
063
064
065 }
066
067 return theInstance;
068 }
069
070 /**
071 * Saves current settings in database.
072 */
073 public void save(){
074
075 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
076
077 session.beginTransaction();
078
079 session.saveOrUpdate(theInstance);
080
081 session.getTransaction().commit();
082 }
083
084
085 //getters and setters for settings properties.
086 //**********************************************
087 public String getEndpointAddress() {
088 return endpointAddress;
089 }
090
091 public void setEndpointAddress(String endpointAddress) {
092 this.endpointAddress = endpointAddress;
093 }
094
095 @Id
096 public int getId() {
097 return id;
098 }
099
100 public void setId(int id) {
101 this.id = id;
102 }
103
104
105
106
107 }