com.alogient.cameleon.sdk.membership.dao.impl.MembershipDaoImpl Maven / Gradle / Ivy
The newest version!
package com.alogient.cameleon.sdk.membership.dao.impl;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.hibernate.jdbc.Work;
import com.alogient.cameleon.sdk.common.dao.impl.BaseDao;
import com.alogient.cameleon.sdk.membership.dao.MembershipDao;
import com.alogient.cameleon.sdk.membership.dao.model.CustomuserfieldUservalue;
import com.alogient.cameleon.sdk.membership.dao.model.Pagegroupaccess;
import com.alogient.cameleon.sdk.membership.dao.model.Users;
import com.alogient.cameleon.sdk.membership.util.exception.DuplicateUserException;
/**
* Used to manage member
*/
public class MembershipDaoImpl extends BaseDao implements MembershipDao {
/**
* {@inheritDoc}
*/
@Override
public Users getUserById(String userId) {
return (Users) getHibernateTemplate().get(Users.class, userId);
}
/**
* {@inheritDoc}
*/
@Override
public Users getUserByUsername(String username) {
Criteria criteria = getCurrentSession().createCriteria(Users.class);
criteria.add(Restrictions.eq("username", username));
return (Users) criteria.uniqueResult();
}
/**
* {@inheritDoc}
*/
@Override
public Users getUserByEmail(String email) {
Criteria criteria = getCurrentSession().createCriteria(Users.class);
criteria.add(Restrictions.eq("email", email));
return (Users) criteria.uniqueResult();
}
/**
* {@inheritDoc}
*/
@SuppressWarnings( { "unchecked" })
@Override
public List getCustomuserFields(String username, String applicationName) {
Query query = getCurrentSession().createSQLQuery(
"select {p.*} from CustomUserField_UserValue {p} where p.username = :username and " + "p.applicationName = :applicationName").addEntity("p",
CustomuserfieldUservalue.class);
query.setString("username", username);
query.setString("applicationName", applicationName);
return query.list();
}
/**
* {@inheritDoc}
*/
@Override
public void saveUser(Users user) {
getHibernateTemplate().saveOrUpdate(user);
}
/**
* {@inheritDoc}
*/
@SuppressWarnings( { "unchecked" })
@Override
public List getRolesForNavId(Integer navId) {
Query query = getCurrentSession().createSQLQuery("select * FROM PageGroupAccess WHERE navId = :navId").addEntity(Pagegroupaccess.class);
query.setInteger("navId", navId);
return query.list();
}
/**
* {@inheritDoc}
*/
@Override
public String createUser(String applicationName, String userName, String password, String email, String[] groups) throws DuplicateUserException {
Users user = (Users) getCurrentSession().createCriteria(Users.class).add(Restrictions.eq("username", userName)).uniqueResult();
if (user != null) {
throw new DuplicateUserException(userName);
}
String pkid = UUID.randomUUID().toString();
CreateUser createUser = new CreateUser(applicationName, userName, password, email, pkid);
getCurrentSession().doWork(createUser);
if (groups != null && groups.length != 0) {
for (String group : groups) {
if (StringUtils.isNotBlank(group)) {
getCurrentSession().doWork(new AddUserToRole(applicationName, userName, group));
}
}
}
return pkid;
}
private class CreateUser implements Work {
private String applicationName;
private String userName;
private String password;
private String email;
private String pkid;
public CreateUser(String applicationName, String userName, String password, String email, String pkid) {
this.applicationName = applicationName;
this.userName = userName;
this.password = password;
this.email = email;
this.pkid = pkid;
}
/**
* {@inheritDoc}
*/
@Override
public void execute(Connection connection) throws SQLException {
CallableStatement cState = connection.prepareCall("execute [dbo].[CreateUser] ?, ?, ?, ?, NULL, NULL, 1, ?");
cState.setString(1, applicationName);
cState.setString(2, userName);
cState.setString(3, DigestUtils.md5Hex(password));
cState.setString(4, email);
cState.setString(5, pkid);
cState.execute();
}
}
private class AddUserToRole implements Work {
private String applicationName;
private String userName;
private String group;
public AddUserToRole(String applicationName, String userName, String group) {
this.applicationName = applicationName;
this.userName = userName;
this.group = group;
}
/**
* {@inheritDoc}
*/
@Override
public void execute(Connection connection) throws SQLException {
CallableStatement cState = connection.prepareCall("execute [dbo].[AddUserToRole] ?, ?, ?");
cState.setString(1, applicationName);
cState.setString(2, userName);
cState.setString(3, group);
cState.execute();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy