/**
 * Copyright (C) 2013 Motown.IO (info@motown.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.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;
    }

}

@@ -919,16 +919,53 @@
 .List;%0A%0A
+@SuppressWarnings(%22JpaQlInspection%22)%0A
 public c
@@ -2380,53 +2380,8 @@
 ) %7B%0A
-        @SuppressWarnings(%22JpaQlInspection%22)%0A
     
@@ -2900,53 +2900,8 @@
 ) %7B%0A
-        @SuppressWarnings(%22JpaQlInspection%22)%0A
     
@@ -3490,53 +3490,8 @@
 ) %7B%0A
-        @SuppressWarnings(%22JpaQlInspection%22)%0A
     
@@ -3762,32 +3762,32 @@
 tList();%0A    %7D%0A%0A
+
     public List%3C
@@ -3816,53 +3816,8 @@
 ) %7B%0A
-        @SuppressWarnings(%22JpaQlInspection%22)%0A
     

Avoid duplicate literals