package org.ovirt.engine.core.bll.storage;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.ovirt.engine.core.bll.GetVmTemplatesDisksQuery;
import org.ovirt.engine.core.common.businessentities.DiskImage;
import org.ovirt.engine.core.common.businessentities.VmTemplate;
import org.ovirt.engine.core.common.businessentities.storage_domains;
import org.ovirt.engine.core.common.queries.GetStorageDomainsByVmTemplateIdQueryParameters;
import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.dal.dbbroker.DbFacade;

public class GetStorageDomainsByVmTemplateIdQuery<P extends GetStorageDomainsByVmTemplateIdQueryParameters>
        extends GetVmTemplatesDisksQuery<P> {

    private VmTemplate vmTemplate;

    public GetStorageDomainsByVmTemplateIdQuery(P parameters) {
        super(parameters);
        vmTemplate =
                DbFacade.getInstance()
                        .getVmTemplateDAO()
                        .get(getParameters().getId(), getUserID(), getParameters().isFiltered());
    }

    @Override
    protected void executeQueryCommand() {
        ArrayList<storage_domains> result = new ArrayList<storage_domains>();

        if (vmTemplate != null && vmTemplate.getstorage_pool_id() != null) {
            List<DiskImage> templateDisks = getTemplateDisks();

            if (templateDisks.size() > 0) {

                Set<Guid> domains = new HashSet<Guid>();
                for (DiskImage templateDisk : templateDisks) {
                    domains.addAll(templateDisk.getstorage_ids());
                }

                for (Guid domainId : domains) {
                    storage_domains domain = getStorageDomain(domainId);
                    if (domain != null) {
                        result.add(domain);
                    }
                }
            }
        }
        getQueryReturnValue().setReturnValue(result);
    }

    protected storage_domains getStorageDomain(Guid domainId) {
        return DbFacade.getInstance()
                .getStorageDomainDAO()
                .getForStoragePool(domainId, vmTemplate.getstorage_pool_id().getValue());
    }
}

@@ -770,16 +770,23 @@
 Template
+ = null
 ;%0A%0A    p
@@ -871,16 +871,80 @@
 eters);%0A
+    %7D%0A%0A    @Override%0A    protected void executeQueryCommand() %7B%0A
         
@@ -1141,72 +1141,8 @@
 ));%0A
-    %7D%0A%0A    @Override%0A    protected void executeQueryCommand() %7B%0A
     

Fix QueriesCommandBaseTest.testQueryCreation failure