Skip to content

Commit

Permalink
added DAOs for Role and User
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomis committed Jan 21, 2015
1 parent 0caf552 commit adecdc9
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/skiwi/githubhooksechatservice/dao/RoleDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.skiwi.githubhooksechatservice.dao;

import com.skiwi.githubhooksechatservice.model.Role;

public interface RoleDAO {

Role getRole(int id);

Role createOrGetRole(String string);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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.Role;

@Repository
public class RoleDAOImpl implements RoleDAO {

@Autowired
private SessionFactory sessionFactory;

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

@Override
public Role getRole(int id) {
Role role = (Role) openSession().load(Role.class, id);
return role;
}

@Override
public Role createOrGetRole(String roleName) {
try {
Query query = openSession().createQuery("from Role r where r.role = :role");
query.setParameter("role", roleName);
System.out.println("executing role query with parameter " + roleName);
Role role = (Role) query.uniqueResult();
if (role != null) {
return role;
}
role = new Role();
role.setRole(roleName);
openSession().save(role);
return role;
}
catch (Exception ex) {
System.out.println("BIG FAT ERROR: " + ex);
ex.printStackTrace();
return null;
}
}

}
11 changes: 11 additions & 0 deletions src/main/java/com/skiwi/githubhooksechatservice/dao/UserDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.skiwi.githubhooksechatservice.dao;

import com.skiwi.githubhooksechatservice.model.DugaUser;

public interface UserDAO {

public DugaUser getUser(String login);

DugaUser createUser(String login, String password);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.skiwi.githubhooksechatservice.model.DugaUser;

@Repository
@Transactional
public class UserDAOImpl implements UserDAO {

@Autowired
private SessionFactory sessionFactory;

@Autowired
private RoleDAO roleDAO;

@Autowired
private PasswordEncoder passwordEncoder;

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

@Override
public DugaUser getUser(String login) {
try {
Query query = openSession().createQuery("from User u where u.login = :login");
query.setParameter("login", login);
System.out.println("executing query with parameter" + login);
return (DugaUser) query.uniqueResult();
}
catch (Exception ex) {
System.out.println("BIG FAT ERROR: " + ex);
ex.printStackTrace();
return null;
}
}

@Override
public DugaUser createUser(String login, String password) {
DugaUser user = getUser(login);
if (user != null) {
return null;
}

try {
user = new DugaUser();
user.setLogin(login);
user.setPassword(passwordEncoder.encode(password));
user.setRole(roleDAO.createOrGetRole("MODERATOR"));
openSession().save(user);
return user;
}
catch (Exception ex) {
System.out.println("BIG FAT ERROR: " + ex);
ex.printStackTrace();
return null;
}
}

}

0 comments on commit adecdc9

Please sign in to comment.