Skip to content

Commit

Permalink
added config entity, DAO and Service
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomis committed Jan 21, 2015
1 parent d293f1b commit dccf7b3
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.skiwi.githubhooksechatservice.dao;


public interface ConfigDAO {

String getConfig(String key, String defaultValue);
void setConfig(String key, String value);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.skiwi.githubhooksechatservice.dao;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.skiwi.githubhooksechatservice.model.DugaConfig;

@Repository
public class ConfigDAOImpl implements ConfigDAO {

@Autowired
private SessionFactory sessionFactory;

private Session openSession() {
return sessionFactory.getCurrentSession();
}

@Override
public String getConfig(String key, String defaultValue) {
try {
Query query = openSession().createQuery("from DugaConfig conf where conf.key = :key");
query.setParameter("key", key);
DugaConfig role = (DugaConfig) query.uniqueResult();
if (role == null) {
role = new DugaConfig();
role.setKey(key);
role.setValue(defaultValue);
openSession().save(role);
}
return role.getValue();
}
catch (Exception ex) {
System.out.println("BIG FAT ERROR: " + ex);
ex.printStackTrace();
return null;
}
}

@Override
public void setConfig(String key, String value) {
try {
Query query = openSession().createQuery("from DugaConfig conf where conf.key = :key");
query.setParameter("key", key);
DugaConfig role = (DugaConfig) query.uniqueResult();
role.setValue(value);
openSession().merge(role);
}
catch (Exception ex) {
System.out.println("BIG FAT ERROR: " + ex);
ex.printStackTrace();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.skiwi.githubhooksechatservice.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="duga_config")
public class DugaConfig {

@Id
@GeneratedValue
private Integer id;

private String key;

private String value;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getKey() {
return key;
}

public String getValue() {
return value;
}

public void setKey(String key) {
this.key = key;
}

public void setValue(String value) {
this.value = value;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.skiwi.githubhooksechatservice.service;


public interface ConfigService {

String getConfig(String key, String defaultValue);
void setConfig(String key, String value);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.skiwi.githubhooksechatservice.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.skiwi.githubhooksechatservice.dao.ConfigDAO;

@Service
@Transactional
public class ConfigServiceImpl implements ConfigService {

@Autowired
private ConfigDAO configDAO;

@Override
public String getConfig(String key, String defaultValue) {
return configDAO.getConfig(key, defaultValue);
}

@Override
public void setConfig(String key, String value) {
configDAO.setConfig(key, value);
}

}

0 comments on commit dccf7b3

Please sign in to comment.