com.formkiq.server.dao.ClientDaoImpl Maven / Gradle / Ivy
package com.formkiq.server.dao;
import java.io.IOException;
import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.transform.AliasToBeanResultTransformer;
import org.hibernate.type.IntegerType;
import org.springframework.stereotype.Repository;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.formkiq.server.domain.User;
import com.formkiq.server.domain.type.ClientDTO;
import com.formkiq.server.domain.type.ClientListDTO;
import com.formkiq.server.domain.type.UserRole;
import com.formkiq.server.util.Strings;
/**
* Implementation of the Client Dao.
*
*/
@Repository
public class ClientDaoImpl extends AbstractDaoImpl implements ClientDao {
/** ObjectMapper. */
private ObjectMapper mapper = new ObjectMapper();
@Override
public int clientCount() {
String sql = "select count(*) as count from oauth_client_details";
Session session = getEntityManager().unwrap(Session.class);
Integer count = (Integer) session.createSQLQuery(sql)
.addScalar("count", IntegerType.INSTANCE)
.uniqueResult();
return count.intValue();
}
@Override
public ClientDTO findClient(final User user, final String client) {
String sql = "select client_id as client, "
+ " additional_information as clientname "
+ " from oauth_client_details fl "
+ " where fl.client_id=:client "
+ " and ( "
+ " exists ( select * from users u "
+ " where u.user_id=:user and u.role=:role) "
+ " or "
+ " exists ( select * from user_clients uc "
+ " where uc.user_id=:user and uc.client_id=:client) "
+ ")";
Session session = getEntityManager().unwrap(Session.class);
SQLQuery query = session.createSQLQuery(sql.toString());
ClientDTO dto = (ClientDTO) query
.setString("client", client)
.setString("user", user.getUserid())
.setString("role", UserRole.ROLE_ADMIN.name())
.setResultTransformer(
new AliasToBeanResultTransformer(ClientDTO.class))
.uniqueResult();
if (dto != null) {
dto.setClientname(getClientnameFromString(dto.getClientname()));
}
return dto;
}
@SuppressWarnings("unchecked")
@Override
public ClientListDTO findClients(final String token) {
int offset = Strings.getOffset(token);
int max = Strings.getMaxResults(token, DEFAULT_MAX_RESULTS);
StringBuilder sql = new StringBuilder(
"select u.client_id as client, "
+ "u.additional_information as clientname "
+ " from oauth_client_details u order by u.client_id");
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(ClientDTO.class))
.list();
ClientListDTO dto = new ClientListDTO();
List truncated = updatePagination(dto, offset, max, list);
dto.setClients(truncated);
for (ClientDTO cdto : truncated) {
cdto.setClientname(getClientnameFromString(cdto.getClientname()));
}
return dto;
}
@Override
public String getClientnameFromString(final String s) {
try {
JsonNode n = this.mapper.readValue(s, JsonNode.class);
return n.get(ClientDao.CLIENT_NAME).asText();
} catch (IOException e) {
return s;
}
}
}