Package com.abiquo.server.core.infrastructure

Source Code of com.abiquo.server.core.infrastructure.RepositoryDAO

/**
* Copyright (C) 2008 - Abiquo Holdings S.L. All rights reserved.
*
* Please see /opt/abiquo/tomcat/webapps/legal/ on Abiquo server
* or contact contact@abiquo.com for licensing information.
*/
package com.abiquo.server.core.infrastructure;

import java.util.List;

import javax.persistence.EntityManager;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.abiquo.server.core.common.persistence.DefaultDAOBase;

@Repository("jpaRepositoryDAO")
public class RepositoryDAO extends
    DefaultDAOBase<Integer, com.abiquo.server.core.infrastructure.Repository>
{
    public RepositoryDAO()
    {       
        super(com.abiquo.server.core.infrastructure.Repository.class);
    }

    public RepositoryDAO(EntityManager entityManager)
    {
        super(com.abiquo.server.core.infrastructure.Repository.class, entityManager);
    }

    private static Criterion thisLocation(String repositoryLocation)
    {
        return Restrictions.eq(com.abiquo.server.core.infrastructure.Repository.URL_PROPERTY,
            repositoryLocation);
    }

    private static Criterion thisDatacenter(Datacenter datacenter)
    {
        return Restrictions.eq(
            com.abiquo.server.core.infrastructure.Repository.DATACENTER_PROPERTY, datacenter);
    }

    public com.abiquo.server.core.infrastructure.Repository findByDatacenter(Datacenter datacenter)
    {
        Criteria criteria = createCriteria(thisDatacenter(datacenter));

        return (com.abiquo.server.core.infrastructure.Repository) criteria.uniqueResult();// getSingleResult(criteria);
    }

    public boolean existRepositoryInOtherDatacenter(Datacenter datacenter, String repositoryLocation)
    {
        Criterion notDatacenter = Restrictions.not(thisDatacenter(datacenter));
        Criteria criteria = createCriteria(notDatacenter, thisLocation(repositoryLocation));

        criteria.setProjection(Projections.projectionList().add(Projections.rowCount()));

        Long count = (Long) criteria.uniqueResult();
        return count != null && count.intValue() > 0;
    }

    public boolean existRepositoryInSameDatacenter(Datacenter datacenter, String repositoryLocation)
    {
        Criteria criteria =
            createCriteria(thisDatacenter(datacenter), thisLocation(repositoryLocation));
        criteria.setProjection(Projections.projectionList().add(Projections.rowCount()));

        Long count = (Long) criteria.uniqueResult();
        return count != null && count.intValue() > 0;
    }

    public com.abiquo.server.core.infrastructure.Repository findByRepositoryLocation(
        String repositoryLocation)
    {
        Criteria criteria = createCriteria(thisLocation(repositoryLocation));

        return (com.abiquo.server.core.infrastructure.Repository) criteria.uniqueResult(); // getSingleResult(criteria);
    }

    private final static String FIND_VIRTUAL_APPS_BY_USED_VIRTUAL_IMAGE_ON_REPOSITORY =
        "SELECT va.id FROM " + //
            "VirtualAppliance as va " + //
            "inner join va.nodesVirtualImage as n, " + //
            "NodeVirtualImage as nvi " + //
            "inner join nvi.virtualImage as vi " + //
            "WHERE " + //
            "nvi.id = n.id " + //
            "AND vi.repository.id=:idRepo";

    public boolean isBeingUsed(Datacenter datacenter)
    {
        com.abiquo.server.core.infrastructure.Repository repo = findByDatacenter(datacenter);

        if (repo == null)
        {
            return false;
        }

        Query query =
            getSession().createQuery(FIND_VIRTUAL_APPS_BY_USED_VIRTUAL_IMAGE_ON_REPOSITORY);

        query.setParameter("idRepo", repo.getId());

        List<Integer> vappIds = query.list();

        return !(vappIds == null || vappIds.isEmpty());
    }

    public void updateRepositoryLocation(Datacenter datacenter, String url)
    {
        // XXX assert !isBeingUsed(datacenter)

        com.abiquo.server.core.infrastructure.Repository repo = findByDatacenter(datacenter);

        if (repo == null)
        {
            repo = new com.abiquo.server.core.infrastructure.Repository(datacenter, url);
            persist(repo);
        }
        else
        {
            repo.setUrl(url);
            flush();
        }
    }

    public void removeByDatacenter(Datacenter datacenter)
    {
        com.abiquo.server.core.infrastructure.Repository repo = findByDatacenter(datacenter);

        if (repo != null)
        {
            remove(repo);
        }
    }

}
TOP

Related Classes of com.abiquo.server.core.infrastructure.RepositoryDAO

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.