<commit_before>/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * 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 io.gravitee.repository.jdbc.management;

import io.gravitee.repository.exceptions.TechnicalException;
import io.gravitee.repository.jdbc.orm.JdbcObjectMapper;
import io.gravitee.repository.management.api.PortalNotificationConfigRepository;
import io.gravitee.repository.management.model.NotificationReferenceType;
import io.gravitee.repository.management.model.PortalNotificationConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;

import static io.gravitee.repository.jdbc.common.AbstractJdbcRepositoryConfiguration.escapeReservedWord;
import static java.lang.String.format;

/**
 *
 * @author njt
 */
@Repository
public class JdbcPortalNotificationConfigRepository implements PortalNotificationConfigRepository {

    private static final Logger LOGGER = LoggerFactory.getLogger(JdbcPortalNotificationConfigRepository.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    private static final JdbcObjectMapper ORM = JdbcObjectMapper.builder(PortalNotificationConfig.class, "portal_notification_configs")
            .updateSql("update portal_notification_configs set "
                    + escapeReservedWord("user") + " = ?"
                    + " , reference_type = ?"
                    + " , reference_id = ?"
                    + " , created_at = ? "
                    + " , updated_at = ? "
                    + " where " + escapeReservedWord("user") + " = ? "
                    + " and reference_type = ? "
                    + " and reference_id = ? "
            )
            .addColumn("user", Types.NVARCHAR, String.class)
            .addColumn("reference_type", Types.NVARCHAR, NotificationReferenceType.class)
            .addColumn("reference_id", Types.NVARCHAR, String.class)
            .addColumn("created_at", Types.TIMESTAMP, Date.class)
            .addColumn("updated_at", Types.TIMESTAMP, Date.class)
            .build();

    @Override
    public PortalNotificationConfig create(final PortalNotificationConfig portalNotificationConfig) throws TechnicalException {
        LOGGER.debug("JdbcPortalNotificationConfigRepository.create({})", portalNotificationConfig);
        try {
            jdbcTemplate.update(ORM.buildInsertPreparedStatementCreator(portalNotificationConfig));
            storeHooks(portalNotificationConfig, false);
            return findById(portalNotificationConfig.getUser(), portalNotificationConfig.getReferenceType(), portalNotificationConfig.getReferenceId()).orElse(null);
        } catch (final Exception ex) {
            LOGGER.error("Failed to create PortalNotificationConfig", ex);
            throw new TechnicalException("Failed to create PortalNotificationConfig", ex);
        }
    }

    @Override
    public List<PortalNotificationConfig> findByReferenceAndHook(String hook, NotificationReferenceType referenceType, String referenceId) throws TechnicalException {
        LOGGER.debug("JdbcPortalNotificationConfigRepository.findByReferenceAndHook({}, {}, {})", hook, referenceType, referenceId);
        try {
            List<String> users = new ArrayList<>();
            jdbcTemplate.query(
                    "select " + escapeReservedWord("user") +
                            " from portal_notification_config_hooks" +
                            " where hook = ?" +
                            " and reference_id = ?" +
                            " and reference_type = ?"
                    , rs -> { users.add(rs.getString(1)); }
                    , hook
                    , referenceId
                    , referenceType.name());

            StringBuilder q = new StringBuilder("select " + escapeReservedWord("user") + ", reference_type, reference_id, created_at, updated_at " +
                    " from portal_notification_configs" +
                    " where reference_type = ?" +
                    " and reference_id = ?");
            ORM.buildInCondition(false, q, escapeReservedWord("user"), users);
            final List<PortalNotificationConfig> items = jdbcTemplate.query(
                    q.toString()
                    , (PreparedStatement ps) -> {
                            ps.setString(1, referenceType.name());
                            ps.setString(2, referenceId);
                            ORM.setArguments(ps, users, 3);
                    }
                    , ORM.getRowMapper()
            );<commit_msg>fix(notifications): do not get notifications for unknown hook<commit_after>/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * 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 io.gravitee.repository.jdbc.management;

import io.gravitee.repository.exceptions.TechnicalException;
import io.gravitee.repository.jdbc.orm.JdbcObjectMapper;
import io.gravitee.repository.management.api.PortalNotificationConfigRepository;
import io.gravitee.repository.management.model.NotificationReferenceType;
import io.gravitee.repository.management.model.PortalNotificationConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;

import static io.gravitee.repository.jdbc.common.AbstractJdbcRepositoryConfiguration.escapeReservedWord;
import static java.lang.String.format;

/**
 *
 * @author njt
 */
@Repository
public class JdbcPortalNotificationConfigRepository implements PortalNotificationConfigRepository {

    private static final Logger LOGGER = LoggerFactory.getLogger(JdbcPortalNotificationConfigRepository.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    private static final JdbcObjectMapper ORM = JdbcObjectMapper.builder(PortalNotificationConfig.class, "portal_notification_configs")
            .updateSql("update portal_notification_configs set "
                    + escapeReservedWord("user") + " = ?"
                    + " , reference_type = ?"
                    + " , reference_id = ?"
                    + " , created_at = ? "
                    + " , updated_at = ? "
                    + " where " + escapeReservedWord("user") + " = ? "
                    + " and reference_type = ? "
                    + " and reference_id = ? "
            )
            .addColumn("user", Types.NVARCHAR, String.class)
            .addColumn("reference_type", Types.NVARCHAR, NotificationReferenceType.class)
            .addColumn("reference_id", Types.NVARCHAR, String.class)
            .addColumn("created_at", Types.TIMESTAMP, Date.class)
            .addColumn("updated_at", Types.TIMESTAMP, Date.class)
            .build();

    @Override
    public PortalNotificationConfig create(final PortalNotificationConfig portalNotificationConfig) throws TechnicalException {
        LOGGER.debug("JdbcPortalNotificationConfigRepository.create({})", portalNotificationConfig);
        try {
            jdbcTemplate.update(ORM.buildInsertPreparedStatementCreator(portalNotificationConfig));
            storeHooks(portalNotificationConfig, false);
            return findById(portalNotificationConfig.getUser(), portalNotificationConfig.getReferenceType(), portalNotificationConfig.getReferenceId()).orElse(null);
        } catch (final Exception ex) {
            LOGGER.error("Failed to create PortalNotificationConfig", ex);
            throw new TechnicalException("Failed to create PortalNotificationConfig", ex);
        }
    }

    @Override
    public List<PortalNotificationConfig> findByReferenceAndHook(String hook, NotificationReferenceType referenceType, String referenceId) throws TechnicalException {
        LOGGER.debug("JdbcPortalNotificationConfigRepository.findByReferenceAndHook({}, {}, {})", hook, referenceType, referenceId);
        try {
            StringBuilder q = new StringBuilder("select distinct pnc." + escapeReservedWord("user") + ", pnc.reference_type, pnc.reference_id, pnc.created_at, pnc.updated_at " +
                    " from portal_notification_configs pnc" +
                    " left join portal_notification_config_hooks pnch" +
                    " on pnc.reference_type = pnch.reference_type" +
                    " and pnc.reference_id = pnch.reference_id" +
                    " and pnc."+ escapeReservedWord("user") +" = pnch."+ escapeReservedWord("user") +
                    " where pnc.reference_type = ?" +
                    " and pnc.reference_id = ?" +
                    " and pnch.hook = ?"
            );
            final List<PortalNotificationConfig> items = jdbcTemplate.query(
                    q.toString()
                    , (PreparedStatement ps) -> {
                            ps.setString(1, referenceType.name());
                            ps.setString(2, referenceId);
                            ps.setString(3, hook);
                    }
                    , ORM.getRowMapper()
            );
{'fraction_non_alphanumeric': 0.09382692671842455, 'fraction_numerical': 0.0025563340276462793, 'mean_word_length': 2.735148514851485, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5401168', 'n_tokens_mistral': 2667, 'n_tokens_neox': 2593, 'n_words': 568}