org.xlcloud.service.dao.JpaVirtualClusterDefinitionsDao.java Source code

Java tutorial

Introduction

Here is the source code for org.xlcloud.service.dao.JpaVirtualClusterDefinitionsDao.java

Source

/*
 * Copyright 2012 AMG.lab, a Bull Group Company
 * 
 * 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 org.xlcloud.service.dao;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import javax.persistence.Tuple;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.log4j.Logger;

import org.xlcloud.service.model.CatalogScope;
import org.xlcloud.service.model.VirtualClusterDefinitionModel;
import org.xlcloud.service.model.VirtualClusterDefinitionModel_;

/**
 * Jpa implementation of {@link VirtualClusterDefinitionsDao} interface
 * 
 * @author Tomek Adamczewski, AMG.net
 * @author Andrzej Stasiak, AMG.net
 * @author Piotr Kulasek-Szwed, AMG.net
 */
public class JpaVirtualClusterDefinitionsDao extends JpaGenericDao<VirtualClusterDefinitionModel>
        implements VirtualClusterDefinitionsDao {

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

    /** {@inheritDoc} */
    @Override
    protected String getModelFieldName(String jaxbName) {
        return VirtualClusterDefinitionModel.FieldName.forJaxbName(jaxbName).getModelName();
    }

    /** {@inheritDoc} */
    @Override
    public Long count(Long excludeId, String name, Long accountId, CatalogScope scope) {
        CounterSupport<VirtualClusterDefinitionModel> counter = new CounterSupport<>(
                VirtualClusterDefinitionModel.class, em);
        counter.equalAccount(VirtualClusterDefinitionModel_.accountId, accountId);
        counter.equalScope(VirtualClusterDefinitionModel_.catalogScope, scope);
        counter.equalString(VirtualClusterDefinitionModel_.name, name);
        counter.excludeId(VirtualClusterDefinitionModel_.vcDefinitionId, excludeId);
        return counter.count();
    }

    @Override
    public List<VirtualClusterDefinitionModel> findPublic(boolean paged) {
        CriteriaBuilder criteriaBuilder = getCriteriaBuilder();
        CriteriaQuery<VirtualClusterDefinitionModel> query = criteriaBuilder
                .createQuery(VirtualClusterDefinitionModel.class);
        Root<VirtualClusterDefinitionModel> vcsRoot = query.from(VirtualClusterDefinitionModel.class);

        query.where(criteriaBuilder.equal(vcsRoot.get(VirtualClusterDefinitionModel_.catalogScope),
                CatalogScope.PUBLIC));
        return getEntityList(query, vcsRoot, false);
    }

    /** {@inheritDoc} */
    @Override
    public List<VirtualClusterDefinitionModel> findPublicByTypeAndTags(String type, Set<String> tags,
            boolean paged) {
        return findByTypeAndTags(null, type, tags, paged);
    }

    /** {@inheritDoc} */
    @Override
    public List<VirtualClusterDefinitionModel> findByTypeAndTags(Long accountId, String type, Set<String> tags,
            boolean paged) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("finding vc definitions with type id: " + type + " and following tag ids:"
                    + ToStringBuilder.reflectionToString(tags));
        }

        CriteriaBuilder criteriaBuilder = getCriteriaBuilder();
        CriteriaQuery<Tuple> query = criteriaBuilder.createTupleQuery();
        Root<VirtualClusterDefinitionModel> vcDefRoot = query.from(VirtualClusterDefinitionModel.class);

        Predicate typePredicate = criteriaBuilder.equal(vcDefRoot.get(VirtualClusterDefinitionModel_.type), type);
        Predicate tagsPredicate = vcDefRoot.get(VirtualClusterDefinitionModel_.tags).in(tags);

        boolean skipTagsJoin = tags.isEmpty();
        Predicate predicate = skipTagsJoin ? typePredicate : criteriaBuilder.and(typePredicate, tagsPredicate);

        Predicate scopePredicate;
        if (accountId != null) {
            Predicate accountPredicate = criteriaBuilder
                    .equal(vcDefRoot.get(VirtualClusterDefinitionModel_.accountId), accountId);
            Predicate privatePredicate = criteriaBuilder
                    .equal(vcDefRoot.get(VirtualClusterDefinitionModel_.catalogScope), CatalogScope.PRIVATE);
            scopePredicate = criteriaBuilder.and(accountPredicate, privatePredicate);
        } else {
            scopePredicate = criteriaBuilder.equal(vcDefRoot.get(VirtualClusterDefinitionModel_.catalogScope),
                    CatalogScope.PUBLIC);
        }

        predicate = criteriaBuilder.and(predicate, scopePredicate);

        query.multiselect(vcDefRoot, criteriaBuilder.count(vcDefRoot));
        query.where(predicate);
        query.groupBy(vcDefRoot);
        if (!skipTagsJoin) {
            query.having(criteriaBuilder.equal(criteriaBuilder.count(vcDefRoot), tags.size()));
        }

        List<Tuple> list = getResultList(query, vcDefRoot, paged);
        List<VirtualClusterDefinitionModel> results = new LinkedList<VirtualClusterDefinitionModel>();
        for (Tuple t : list) {
            results.add((VirtualClusterDefinitionModel) t.get(0));
        }
        return results;
    }

    @Override
    public List<VirtualClusterDefinitionModel> findByAccount(Long accountId, boolean paged) {
        CriteriaBuilder criteriaBuilder = getCriteriaBuilder();
        CriteriaQuery<VirtualClusterDefinitionModel> query = criteriaBuilder
                .createQuery(VirtualClusterDefinitionModel.class);
        Root<VirtualClusterDefinitionModel> vcsRoot = query.from(VirtualClusterDefinitionModel.class);
        query.where(criteriaBuilder.equal(vcsRoot.get(VirtualClusterDefinitionModel_.accountId), accountId));

        return getEntityList(query, vcsRoot, false);
    }

}