Java tutorial
/* * 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.core.dao; import java.io.IOException; import java.util.List; import javax.persistence.Query; import javax.sql.DataSource; import org.hibernate.SQLQuery; import org.hibernate.Session; import org.hibernate.transform.AliasToBeanResultTransformer; import org.hibernate.type.IntegerType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService; import org.springframework.stereotype.Repository; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.formkiq.core.domain.OAuthFederation; import com.formkiq.core.domain.User; import com.formkiq.core.domain.type.ClientDTO; import com.formkiq.core.domain.type.ClientListDTO; import com.formkiq.core.util.Strings; /** * Implementation of the Client Dao. * */ @Repository public class ClientDaoImpl extends AbstractDaoImpl implements ClientDao { /** Client Details Service. */ @Autowired private JdbcClientDetailsService clientDetailsService; /** DataSource. */ @Autowired private DataSource dataSource; /** ObjectMapper. */ private ObjectMapper mapper = new ObjectMapper(); /** * default constructor. */ public ClientDaoImpl() { } @SuppressWarnings("resource") @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(); } @SuppressWarnings("resource") @Override public ClientDTO findClient(final User user, final String client) { String sql = "select fl.client_id as client, " + " additional_information as clientname, " + " authorized_grant_types as granttypesasstring" + " 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", "resource" }) @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, " + "u.authorized_grant_types as granttypesasstring" + " 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<ClientDTO> list = session.createSQLQuery(sql.toString()) .setResultTransformer(new AliasToBeanResultTransformer(ClientDTO.class)).list(); ClientListDTO dto = new ClientListDTO(); List<ClientDTO> truncate = updatePagination(dto, offset, max, list); dto.setClients(truncate); for (ClientDTO cd : truncate) { cd.setClientname(getClientnameFromString(cd.getClientname())); } return dto; } @Override public OAuthFederation findOAuthFederation(final String username) { OAuthFederation fed = null; String jql = "select u from OAuthFederation u " + "where domain=:domain or username=:username"; int index = username.indexOf("@"); if (index > -1) { String domain = username.substring(username.indexOf("@")); Query query = getEntityManager().createQuery(jql).setParameter("domain", domain) .setParameter("username", username); fed = (OAuthFederation) getSingleResult(query); } return fed; } @SuppressWarnings("unchecked") @Override public List<OAuthFederation> findOAuthFederations() { String jql = "select u from OAuthFederation u"; Query query = getEntityManager().createQuery(jql); return query.getResultList(); } @Override public JdbcClientDetailsService getClientDetailsService(final boolean clientsecretencrypted) { if (clientsecretencrypted) { return new JdbcClientDetailsService(this.dataSource); } return this.clientDetailsService; } @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; } } }