com.formkiq.server.dao.ClientDaoImpl 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
/*
* 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.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.util.Strings;
/**
* Implementation of the Client Dao.
*
*/
@Repository
public class ClientDaoImpl extends AbstractDaoImpl implements ClientDao {
/** ObjectMapper. */
private ObjectMapper mapper = new ObjectMapper();
/**
* default constructor.
*/
public ClientDaoImpl() {
}
@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 fl.client_id as client, "
+ " additional_information as clientname "
+ " from oauth_client_details fl "
+ " where fl.client_id=:client";
Session session = getEntityManager().unwrap(Session.class);
SQLQuery query = session.createSQLQuery(sql.toString());
ClientDTO dto = (ClientDTO) query
.setString("client", client)
.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 clientname");
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;
}
}
}