com.formkiq.server.dao.UserDaoImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of formkiq-server Show documentation
Show all versions of formkiq-server Show documentation
Server-side integration for the FormKiQ ios application
package com.formkiq.server.dao;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.persistence.Query;
import org.hibernate.Session;
import org.hibernate.transform.AliasToBeanResultTransformer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.formkiq.server.config.DateService;
import com.formkiq.server.domain.User;
import com.formkiq.server.domain.UserClient;
import com.formkiq.server.domain.type.ClientDTO;
import com.formkiq.server.domain.type.UserDTO;
import com.formkiq.server.domain.type.UserListDTO;
/**
* UserDao hibernate implemenation.
*
*/
@Repository
public class UserDaoImpl extends AbstractDaoImpl implements UserDao {
/** DateService. */
@Autowired
private DateService dateservice;
/** ObjectMapper. */
private ObjectMapper mapper = new ObjectMapper();
/**
* Find ClientDTO for a user.
* @param email String
* @return List<ClientDTO>
*/
@SuppressWarnings("unchecked")
private List findClientDTO(final String email) {
String sql = "select o.client_id as client, "
+ " o.additional_information as clientname "
+ " from Users u"
+ " join user_clients c on u.user_id=c.user_id"
+ " join oauth_client_details o "
+ " on c.client_id=o.client_id"
+ " where u.email=:email"
+ " order by o.additional_information";
Session session = getEntityManager().unwrap(Session.class);
List clients = session.createSQLQuery(sql)
.setParameter("email", email)
.setResultTransformer(
new AliasToBeanResultTransformer(ClientDTO.class))
.list();
try {
for (ClientDTO client : clients) {
JsonNode n = this.mapper.readValue(client.getClientname(),
JsonNode.class);
client.setClientname(n.get(CLIENT_NAME).asText());
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return clients;
}
@Override
public User findUser(final String email) {
String jql = "select u from User u where u.email=:email ";
Query query = getEntityManager().createQuery(jql)
.setParameter("email", email.toLowerCase(Locale.getDefault()));
return (User) getSingleResult(query);
}
@Override
public UserClient findUserClient(final User user, final String client) {
String jql = "select uc from UserClient uc "
+ "where uc.userid=:userid and uc.clientid=:clientid ";
Query query = getEntityManager().createQuery(jql)
.setParameter("userid", user.getUserid())
.setParameter("clientid", client);
return (UserClient) getSingleResult(query);
}
@Override
public UserDTO findUserDTO(final String email) {
String sql = "select u.user_id as userid, u.email, u.status, u.role "
+ " from Users u"
+ " where u.email=:email";
Session session = getEntityManager().unwrap(Session.class);
UserDTO dto = (UserDTO) session.createSQLQuery(sql)
.setParameter("email", email)
.setResultTransformer(
new AliasToBeanResultTransformer(UserDTO.class))
.uniqueResult();
List clients = findClientDTO(email);
dto.setClients(clients);
return dto;
}
@SuppressWarnings("unchecked")
@Override
public UserListDTO findUsers() {
String sql = "select u.user_id as userid, u.email, u.status, u.role "
+ " from Users u"
+ " order by u.email";
Session session = getEntityManager().unwrap(Session.class);
List list = session.createSQLQuery(sql)
.setResultTransformer(
new AliasToBeanResultTransformer(UserDTO.class))
.list();
UserListDTO dto = new UserListDTO();
dto.setUsers(list);
return dto;
}
@Override
public User saveUser(final User user) {
Date now = this.dateservice.now();
user.setUpdatedDate(now);
user.setEmail(user.getEmail().toLowerCase(Locale.getDefault()));
if (StringUtils.isEmpty(user.getUserid())) {
user.setInsertedDate(now);
getEntityManager().persist(user);
} else {
getEntityManager().merge(user);
}
return user;
}
@Override
public UserClient saveUserClient(final UserClient userclient) {
if (StringUtils.isEmpty(userclient.getId())) {
Date now = this.dateservice.now();
userclient.setInsertedDate(now);
getEntityManager().persist(userclient);
} else {
getEntityManager().merge(userclient);
}
return userclient;
}
}