<commit_before>/**
 * Copyright (C) 2013 <EMAIL> (<EMAIL>)
 *
 * 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.motown.vas.viewmodel.persistence.repostories;

import io.motown.vas.viewmodel.model.Subscription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import java.util.List;

public class SubscriptionRepository {

    private static final Logger LOG = LoggerFactory.getLogger(SubscriptionRepository.class);

    private EntityManager entityManager;

    public void insert(Subscription subscription) {
        EntityTransaction transaction = entityManager.getTransaction();

        if (!transaction.isActive()) {
            transaction.begin();
        }

        try {
            entityManager.persist(subscription);
            transaction.commit();
        } finally {
            if (transaction.isActive()) {
                LOG.warn("Transaction is still active while it should not be, rolling back.");
                transaction.rollback();
            }
        }
    }

    public void delete(Subscription subscription) {
        EntityTransaction transaction = entityManager.getTransaction();

        if (!transaction.isActive()) {
            transaction.begin();
        }

        try {
            entityManager.remove(subscription);
            transaction.commit();
        } finally {
            if (transaction.isActive()) {
                LOG.warn("Transaction is still active while it should not be, rolling back.");
                transaction.rollback();
            }
        }
    }

    public Subscription findById(Long id) {
        return entityManager.find(Subscription.class, id);
    }

    public Subscription findBySubscriptionId(String subscriptionId) {
        @SuppressWarnings("JpaQlInspection")
        Query query = entityManager.createQuery("SELECT s FROM io.motown.vas.viewmodel.model.Subscription AS s WHERE s.subscriptionId = :subscriptionId").setParameter("subscriptionId", subscriptionId);
        List resultList = query.getResultList();
        if (resultList.size() > 0) {
            return (Subscription) resultList.get(0);
        } else {
            return null;
        }
    }

    public Subscription findBySubscriberIdentityAndDeliveryAddress(String subscriberIdentity, String deliveryAddress) {
        @SuppressWarnings("JpaQlInspection")
        Query query = entityManager.createQuery("SELECT s FROM io.motown.vas.viewmodel.model.Subscription AS s WHERE s.subscriberIdentity = :subscriberIdentity AND s.deliveryAddress = :deliveryAddress").setParameter("subscriberIdentity", subscriberIdentity).setParameter("deliveryAddress", deliveryAddress);
        List resultList = query.getResultList();
        if (resultList.size() > 0) {
            return (Subscription) resultList.get(0);
        } else {
            return null;
        }
    }

    public List<Subscription> findBySubscriberIdentity(String subscriberIdentity) {
        @SuppressWarnings("JpaQlInspection")
        Query query = entityManager.createQuery("SELECT s FROM io.motown.vas.viewmodel.model.Subscription AS s WHERE s.subscriberIdentity = :subscriberIdentity").setParameter("subscriberIdentity", subscriberIdentity);
        return (List<Subscription>) query.getResultList();
    }

    public List<Subscription> findAll() {
        @SuppressWarnings("JpaQlInspection")
        Query query = entityManager.createQuery("SELECT s FROM io.motown.vas.viewmodel.model.Subscription AS s");
        return (List<Subscription>) query.getResultList();
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;<commit_msg>Avoid duplicate literals<commit_after>/**
 * Copyright (C) 2013 Motown.IO (<EMAIL>)
 *
 * 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.motown.vas.viewmodel.persistence.repostories;

import io.motown.vas.viewmodel.model.Subscription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import java.util.List;

@SuppressWarnings("JpaQlInspection")
public class SubscriptionRepository {

    private static final Logger LOG = LoggerFactory.getLogger(SubscriptionRepository.class);

    private EntityManager entityManager;

    public void insert(Subscription subscription) {
        EntityTransaction transaction = entityManager.getTransaction();

        if (!transaction.isActive()) {
            transaction.begin();
        }

        try {
            entityManager.persist(subscription);
            transaction.commit();
        } finally {
            if (transaction.isActive()) {
                LOG.warn("Transaction is still active while it should not be, rolling back.");
                transaction.rollback();
            }
        }
    }

    public void delete(Subscription subscription) {
        EntityTransaction transaction = entityManager.getTransaction();

        if (!transaction.isActive()) {
            transaction.begin();
        }

        try {
            entityManager.remove(subscription);
            transaction.commit();
        } finally {
            if (transaction.isActive()) {
                LOG.warn("Transaction is still active while it should not be, rolling back.");
                transaction.rollback();
            }
        }
    }

    public Subscription findById(Long id) {
        return entityManager.find(Subscription.class, id);
    }

    public Subscription findBySubscriptionId(String subscriptionId) {
        Query query = entityManager.createQuery("SELECT s FROM io.motown.vas.viewmodel.model.Subscription AS s WHERE s.subscriptionId = :subscriptionId").setParameter("subscriptionId", subscriptionId);
        List resultList = query.getResultList();
        if (resultList.size() > 0) {
            return (Subscription) resultList.get(0);
        } else {
            return null;
        }
    }

    public Subscription findBySubscriberIdentityAndDeliveryAddress(String subscriberIdentity, String deliveryAddress) {
        Query query = entityManager.createQuery("SELECT s FROM io.motown.vas.viewmodel.model.Subscription AS s WHERE s.subscriberIdentity = :subscriberIdentity AND s.deliveryAddress = :deliveryAddress").setParameter("subscriberIdentity", subscriberIdentity).setParameter("deliveryAddress", deliveryAddress);
        List resultList = query.getResultList();
        if (resultList.size() > 0) {
            return (Subscription) resultList.get(0);
        } else {
            return null;
        }
    }

    public List<Subscription> findBySubscriberIdentity(String subscriberIdentity) {
        Query query = entityManager.createQuery("SELECT s FROM io.motown.vas.viewmodel.model.Subscription AS s WHERE s.subscriberIdentity = :subscriberIdentity").setParameter("subscriberIdentity", subscriberIdentity);
        return (List<Subscription>) query.getResultList();
    }

    public List<Subscription> findAll() {
        Query query = entityManager.createQuery("SELECT s FROM io.motown.vas.viewmodel.model.Subscription AS s");
        return (List<Subscription>) query.getResultList();
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
{'fraction_non_alphanumeric': 0.0923369823893384, 'fraction_numerical': 0.0033317467872441696, 'mean_word_length': 3.0900243309002433, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '146138', 'n_tokens_mistral': 2074, 'n_tokens_neox': 2072, 'n_words': 558}