All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.formkiq.server.dao.UserDaoImpl Maven / Gradle / Ivy
/*
* Copyright (C) 2016 FormKiQ Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.formkiq.server.dao;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import javax.persistence.Query;
import org.hibernate.Session;
import org.hibernate.transform.AliasToBeanResultTransformer;
import org.hibernate.type.DateType;
import org.hibernate.type.IntegerType;
import org.hibernate.type.PostgresUUIDType;
import org.hibernate.type.StringType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import com.formkiq.server.config.DateService;
import com.formkiq.server.domain.User;
import com.formkiq.server.domain.type.ClientDTO;
import com.formkiq.server.domain.type.FolderDTO;
import com.formkiq.server.domain.type.UserDTO;
import com.formkiq.server.domain.type.UserListDTO;
import com.formkiq.server.domain.type.UserRole;
import com.formkiq.server.util.Strings;
/**
* UserDao hibernate implemenation.
*
*/
@Repository
public class UserDaoImpl extends AbstractDaoImpl implements UserDao {
/** ClientDao. */
@Autowired
private ClientDao clientDao;
/** DateService. */
@Autowired
private DateService dateservice;
/** FolderDao. */
@Autowired
private FolderDao folderDao;
@Override
public void deleteUser(final User user) {
String d0 = "delete from users where user_id=:user";
String d2 = "delete from folders_access where user_id=:user";
Session session = getEntityManager().unwrap(Session.class);
session.createSQLQuery(d2).setParameter("user", user.getUserid(),
PostgresUUIDType.INSTANCE)
.executeUpdate();
session.createSQLQuery(d0).setParameter("user", user.getUserid(),
PostgresUUIDType.INSTANCE)
.executeUpdate();
}
/**
* 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, oauth_client_details o"
+ " 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();
for (ClientDTO client : clients) {
client.setClientname(this.clientDao
.getClientnameFromString(client.getClientname()));
}
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 UserDTO findUserDTO(final String email) {
String sql = "select u.user_id as userid, u.email as email, "
+ " u.password as password, "
+ " u.status as status, u.role as role, "
+ " u.lastlogin_date as lastlogin "
+ " from Users u"
+ " where u.email=:email";
Session session = getEntityManager().unwrap(Session.class);
UserDTO dto = (UserDTO) session.createSQLQuery(sql)
.addScalar("userid", StringType.INSTANCE)
.addScalar("email", StringType.INSTANCE)
.addScalar("password", StringType.INSTANCE)
.addScalar("status", StringType.INSTANCE)
.addScalar("role", StringType.INSTANCE)
.addScalar("lastlogin", DateType.INSTANCE)
.setParameter("email", email)
.setResultTransformer(
new AliasToBeanResultTransformer(UserDTO.class))
.uniqueResult();
if (dto != null) {
List clients = findClientDTO(email);
dto.setClients(clients);
List folders = this.folderDao.findFoldersDTO(email);
dto.setFolders(folders);
}
return dto;
}
@SuppressWarnings("unchecked")
@Override
public UserListDTO findUsers(final String token) {
int offset = Strings.getOffset(token);
int max = Strings.getMaxResults(token, DEFAULT_MAX_RESULTS);
StringBuilder sql = new StringBuilder(
"select u.user_id as userid, "
+ " u.email as email, "
+ " u.status as status, "
+ " u.role as role, "
+ " u.lastlogin_date as lastlogin "
+ " from Users u order by u.email");
sql.append(" OFFSET " + offset + " FETCH FIRST " + (max + 1)
+ " ROWS ONLY");
Session session = getEntityManager().unwrap(Session.class);
List list = session.createSQLQuery(sql.toString())
.addScalar("userid", StringType.INSTANCE)
.addScalar("email", StringType.INSTANCE)
.addScalar("status", StringType.INSTANCE)
.addScalar("role", StringType.INSTANCE)
.addScalar("lastlogin", DateType.INSTANCE)
.setResultTransformer(
new AliasToBeanResultTransformer(UserDTO.class))
.list();
UserListDTO dto = new UserListDTO();
List truncated = updatePagination(dto, offset, max, list);
dto.setUsers(truncated);
return dto;
}
@Override
public int getAdminUserCount() {
String sql = "select count(*) as count from users where role=:role";
Session session = getEntityManager().unwrap(Session.class);
Integer count = (Integer) session.createSQLQuery(sql)
.addScalar("count", IntegerType.INSTANCE)
.setParameter("role", UserRole.ROLE_ADMIN.name())
.uniqueResult();
return count.intValue();
}
@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.setUserid(UUID.randomUUID());
user.setInsertedDate(now);
getEntityManager().persist(user);
} else {
getEntityManager().merge(user);
}
return user;
}
@Override
public void updateLastLogin(final String email, final Date date) {
String sql = "update users set lastlogin_date=:date where email=:email";
Session session = getEntityManager().unwrap(Session.class);
session.createSQLQuery(sql)
.setParameter("date", date)
.setParameter("email", email)
.executeUpdate();
}
}