gabriel-farache commented on code in PR #4000:
URL: 
https://github.com/apache/incubator-kie-kogito-runtimes/pull/4000#discussion_r2260761936


##########
quarkus/extensions/kogito-quarkus-serverless-workflow-jdbc-token-persistence-extension/kogito-quarkus-serverless-workflow-jdbc-token-persistence/src/main/java/org/kie/kogito/serverless/workflow/token/persistence/jdbc/JdbcTokenCacheRepository.java:
##########
@@ -0,0 +1,229 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.kie.kogito.serverless.workflow.token.persistence.jdbc;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.Timestamp;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+
+import javax.sql.DataSource;
+
+import 
org.kie.kogito.addons.quarkus.token.exchange.persistence.TokenCacheRepository;
+import 
org.kie.kogito.addons.quarkus.token.exchange.persistence.model.TokenCacheRecord;
+import org.kie.kogito.addons.quarkus.token.exchange.utils.CacheUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import jakarta.annotation.Priority;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.enterprise.inject.Alternative;
+
+/**
+ * JDBC-based repository for token cache operations.
+ * Follows the same pattern as other JDBC repositories in the codebase.
+ */
+@ApplicationScoped
+@Alternative
+@Priority(200)
+public class JdbcTokenCacheRepository implements TokenCacheRepository {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(JdbcTokenCacheRepository.class);
+
+    // SQL queries following the same pattern as other JDBC repositories
+    static final String INSERT =
+            "INSERT INTO kogito_oauth2_token_cache (process_instance_id, 
auth_name, access_token, refresh_token, expiration_time, created_at, 
updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)";
+    static final String UPDATE = "UPDATE kogito_oauth2_token_cache SET 
access_token = ?, refresh_token = ?, expiration_time = ?, updated_at = ? WHERE 
process_instance_id = ? AND auth_name = ?";
+    static final String FIND_BY_KEY =
+            "SELECT process_instance_id, auth_name, access_token, 
refresh_token, expiration_time, created_at, updated_at FROM 
kogito_oauth2_token_cache WHERE process_instance_id = ? AND auth_name = ?";
+    static final String DELETE_BY_KEY = "DELETE FROM kogito_oauth2_token_cache 
WHERE process_instance_id = ? AND auth_name = ?";
+    static final String DELETE_EXPIRED = "DELETE FROM 
kogito_oauth2_token_cache WHERE expiration_time < ?";
+    static final String FIND_EXPIRING_SOON =
+            "SELECT process_instance_id, auth_name, access_token, 
refresh_token, expiration_time, created_at, updated_at FROM 
kogito_oauth2_token_cache WHERE expiration_time < ?";
+    static final String FIND_ALL = "SELECT process_instance_id, auth_name, 
access_token, refresh_token, expiration_time, created_at, updated_at FROM 
kogito_oauth2_token_cache";
+
+    private final DataSource dataSource;
+
+    public JdbcTokenCacheRepository(DataSource dataSource) {
+        this.dataSource = dataSource;
+    }
+
+    @Override
+    public TokenCacheRecord save(TokenCacheRecord record) {
+        // Check if record exists first - use direct method since we have the 
components
+        Optional<TokenCacheRecord> existing = 
findByKey(record.getProcessInstanceId(), record.getAuthName());
+
+        if (existing.isPresent()) {
+            return update(record);
+        } else {
+            return insert(record);
+        }
+    }
+
+    private TokenCacheRecord insert(TokenCacheRecord record) {
+        try (Connection connection = dataSource.getConnection();
+                PreparedStatement statement = 
connection.prepareStatement(INSERT)) {
+
+            statement.setString(1, record.getProcessInstanceId());
+            statement.setString(2, record.getAuthName());
+            statement.setString(3, record.getAccessToken());
+            statement.setString(4, record.getRefreshToken());
+            statement.setLong(5, record.getExpirationTime());
+            statement.setTimestamp(6, Timestamp.from(record.getCreatedAt()));
+            statement.setTimestamp(7, Timestamp.from(record.getUpdatedAt()));
+
+            int executed = statement.executeUpdate();
+            if (executed > 0) {
+                LOGGER.debug("Inserted token cache record for 
processInstanceId: {}, authName: {}",
+                        record.getProcessInstanceId(), record.getAuthName());
+                return record;
+            } else {
+                throw new RuntimeException("Failed to insert token cache 
record for processInstanceId: " +
+                        record.getProcessInstanceId() + ", authName: " + 
record.getAuthName());
+            }
+        } catch (Exception e) {
+            throw new RuntimeException("Error inserting token cache record for 
processInstanceId: " +
+                    record.getProcessInstanceId() + ", authName: " + 
record.getAuthName(), e);
+        }
+    }
+
+    private TokenCacheRecord update(TokenCacheRecord record) {
+        try (Connection connection = dataSource.getConnection();
+                PreparedStatement statement = 
connection.prepareStatement(UPDATE)) {
+
+            record.updateTimestamp(); // Update the timestamp
+
+            statement.setString(1, record.getAccessToken());
+            statement.setString(2, record.getRefreshToken());
+            statement.setLong(3, record.getExpirationTime());
+            statement.setTimestamp(4, Timestamp.from(record.getUpdatedAt()));
+            statement.setString(5, record.getProcessInstanceId());
+            statement.setString(6, record.getAuthName());
+
+            int executed = statement.executeUpdate();
+            if (executed > 0) {
+                LOGGER.debug("Updated token cache record for 
processInstanceId: {}, authName: {}",
+                        record.getProcessInstanceId(), record.getAuthName());
+                return record;
+            } else {
+                throw new RuntimeException("Failed to update token cache 
record for processInstanceId: " +
+                        record.getProcessInstanceId() + ", authName: " + 
record.getAuthName());
+            }
+        } catch (Exception e) {
+            throw new RuntimeException("Error updating token cache record for 
processInstanceId: " +
+                    record.getProcessInstanceId() + ", authName: " + 
record.getAuthName(), e);
+        }
+    }
+
+    @Override
+    public Optional<TokenCacheRecord> findByKey(String processInstanceId, 
String authName) {
+        try (Connection connection = dataSource.getConnection();
+                PreparedStatement statement = 
connection.prepareStatement(FIND_BY_KEY)) {
+
+            statement.setString(1, processInstanceId);
+            statement.setString(2, authName);
+
+            try (ResultSet resultSet = statement.executeQuery()) {
+                if (resultSet.next()) {
+                    return Optional.of(mapResultSetToRecord(resultSet));
+                }
+                return Optional.empty();

Review Comment:
   ah! the everlasting ternary operator :) I'll apply that though



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to