/*-
 * ~~~~~~licensing~~~~~~
 * bundle-service
 * ==========
 * Copyright (C) 2020 - 2021 EmDev LLC
 * ==========
 * 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.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.esb.system.management.bundle.jpa;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import ru.entaxy.esb.system.management.bundle.jpa.entity.BundleEntity;
import ru.entaxy.esb.system.management.bundle.jpa.entity.BundleType;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.List;

public class BundleServiceImpl implements BundleService {

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public Session getSession() {
        return sessionFactory.openSession();
    }

    @Override
    public List<BundleEntity> list() {
        List<BundleEntity> list;
        try (Session s = getSession()) {
            s.getTransaction().begin();
            CriteriaQuery<BundleEntity> cq = s.getCriteriaBuilder().createQuery(BundleEntity.class);
            cq.from(BundleEntity.class);
            list = s.createQuery(cq).getResultList();
            s.getTransaction().commit();
            s.close();
        }

        return list;
    }

    @Override
    public BundleEntity add(BundleEntity bundleEntity) {
        try (Session s = getSession()) {
            s.getTransaction().begin();
            s.saveOrUpdate(bundleEntity);
            s.getTransaction().commit();
            s.close();
        }
        return bundleEntity;
    }

    @Override
    public BundleEntity add(String name, String bundleUrl, String status) {
        BundleEntity bundleEntity = new BundleEntity();
        bundleEntity.setName(name);
        bundleEntity.setUrl(bundleUrl);
        bundleEntity.setStatus(status);
        return add(bundleEntity);
    }

    @Override
    public BundleEntity add(String name, BundleType type, String bundleUrl, String status) {
    	BundleEntity bundleEntity = new BundleEntity();
        bundleEntity.setName(name);
        bundleEntity.setType(type);
        bundleEntity.setUrl(bundleUrl);
        bundleEntity.setStatus(status);
        return add(bundleEntity);
    }
    
    @Override
    public BundleEntity getByName(String name) {
        BundleEntity bundleEntity;
        try (Session s = getSession()) {
            s.getTransaction().begin();

            CriteriaBuilder builder = s.getCriteriaBuilder();
            CriteriaQuery<BundleEntity> criteriaQuery = builder.createQuery(BundleEntity.class);
            Root<BundleEntity> root = criteriaQuery.from(BundleEntity.class);
            criteriaQuery.where(builder.equal(root.get("name"), name));
            bundleEntity = s.createQuery(criteriaQuery).uniqueResult();

            s.getTransaction().commit();
            s.close();
        }
        return bundleEntity;
    }

    @Override
    public BundleEntity update(String name, String status) {
        BundleEntity bundleEntity = getByName(name);
        bundleEntity.setStatus(status);
        return add(bundleEntity);
    }

}

