-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added config entity, DAO and Service
- Loading branch information
Showing
5 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
src/main/java/com/skiwi/githubhooksechatservice/dao/ConfigDAO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/com/skiwi/githubhooksechatservice/dao/ConfigDAOImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/skiwi/githubhooksechatservice/model/DugaConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/skiwi/githubhooksechatservice/service/ConfigService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/skiwi/githubhooksechatservice/service/ConfigServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |