package ca.corefacility.bioinformatics.irida.oauth2;

import java.util.List;

import org.springframework.jdbc.core.ArgumentPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationService;
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;

public class IridaOAuth2AuthorizationService extends JdbcOAuth2AuthorizationService {

    private final String QUERY_AUTHORIZATION_BY_REGISTERED_CLIENT_ID = "SELECT * FROM oauth2_authorization WHERE access_token_value IS NOT NULL and registered_client_id = ?";

    public IridaOAuth2AuthorizationService(JdbcOperations jdbcOperations,
            RegisteredClientRepository registeredClientRepository) {
        super(jdbcOperations, registeredClientRepository);
    }

    public List<OAuth2Authorization> findAccessTokensByRegisteredClientId(String registeredClientId) {
        PreparedStatementSetter pss = new ArgumentPreparedStatementSetter(new Object[] { registeredClientId });
        List<OAuth2Authorization> authorizations = getJdbcOperations()
                .query(QUERY_AUTHORIZATION_BY_REGISTERED_CLIENT_ID, pss, getAuthorizationRowMapper());
        return authorizations;
    }
}

@@ -71,16 +71,91 @@
 .List;%0A%0A
+import javax.annotation.Nullable;%0Aimport javax.transaction.Transactional;%0A%0A
 import o
@@ -208,32 +208,32 @@
 tatementSetter;%0A
-
 import org.sprin
@@ -323,32 +323,97 @@
 tatementSetter;%0A
+import org.springframework.security.oauth2.core.OAuth2TokenType;%0A
 import org.sprin
@@ -1149,16 +1149,35 @@
 %0A    %7D%0A%0A
+    @Transactional%0A
     publ
@@ -1557,16 +1557,16 @@
 per());%0A
-
         
@@ -1593,11 +1593,597 @@
 s;%0A    %7D
+%0A%0A    @Override%0A    @Transactional%0A    public OAuth2Authorization findByToken(String token, @Nullable OAuth2TokenType tokenType) %7B%0A        return super.findByToken(token, tokenType);%0A    %7D%0A%0A    @Override%0A    @Transactional%0A    public OAuth2Authorization findById(String id) %7B%0A        return super.findById(id);%0A    %7D%0A%0A    @Override%0A    @Transactional%0A    public void remove(OAuth2Authorization authorization) %7B%0A        super.remove(authorization);%0A    %7D%0A%0A    @Override%0A    @Transactional%0A    public void save(OAuth2Authorization authorization) %7B%0A        super.save(authorization);%0A    %7D
 %0A%7D%0A

Add Transactional annotations to all methods in IridaOAuth2Authorization that use db operations.