All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.formkiq.server.dao.UserDaoImpl Maven / Gradle / Ivy

package com.formkiq.server.dao;



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.hibernate.type.IntegerType;
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.UserClient;
import com.formkiq.server.domain.type.ClientDTO;
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;

    @Override
    public void deleteUser(final User user) {

        String d0 = "delete from users where user_id=:user";
        String d1 = "delete from user_clients where user_id=:user";

        Session session = getEntityManager().unwrap(Session.class);
        session.createSQLQuery(d1).setParameter("user", user.getUserid())
                .executeUpdate();
        session.createSQLQuery(d0).setParameter("user", user.getUserid())
                .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"
                + " 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();

        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 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(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, u.status, u.role "
                        + " 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())
                .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.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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy