ravening commented on a change in pull request #5307:
URL: https://github.com/apache/cloudstack/pull/5307#discussion_r697536310



##########
File path: server/src/main/java/com/cloud/api/query/QueryManagerImpl.java
##########
@@ -2824,75 +2826,42 @@
             sc.addAnd("zoneId", SearchCriteria.Op.SC, zoneSC);
         }
 
-        // FIXME: disk offerings should search back up the hierarchy for
-        // available disk offerings...
-        /*
-         * sb.addAnd("domainId", sb.entity().getDomainId(),
-         * SearchCriteria.Op.EQ); if (domainId != null) {
-         * SearchBuilder<DomainVO> domainSearch =
-         * _domainDao.createSearchBuilder(); domainSearch.addAnd("path",
-         * domainSearch.entity().getPath(), SearchCriteria.Op.LIKE);
-         * sb.join("domainSearch", domainSearch, sb.entity().getDomainId(),
-         * domainSearch.entity().getId()); }
-         */
-
-        // FIXME: disk offerings should search back up the hierarchy for
-        // available disk offerings...
-        /*
-         * if (domainId != null) { sc.setParameters("domainId", domainId); //
-         * //DomainVO domain = _domainDao.findById((Long)domainId); // // I 
want
-         * to join on user_vm.domain_id = domain.id where domain.path like
-         * 'foo%' //sc.setJoinParameters("domainSearch", "path",
-         * domain.getPath() + "%"); // }
-         */
-
-        Pair<List<DiskOfferingJoinVO>, Integer> result = 
_diskOfferingJoinDao.searchAndCount(sc, searchFilter);
-        // Remove offerings that are not associated with caller's domain
-        if (account.getType() != Account.ACCOUNT_TYPE_ADMIN && 
CollectionUtils.isNotEmpty(result.first())) {
-            ListIterator<DiskOfferingJoinVO> it = 
result.first().listIterator();
-            while (it.hasNext()) {
-                DiskOfferingJoinVO offering = it.next();
-                if(!Strings.isNullOrEmpty(offering.getDomainId())) {
-                    boolean toRemove = true;
-                    String[] domainIdsArray = 
offering.getDomainId().split(",");
-                    for (String domainIdString : domainIdsArray) {
-                        Long dId = Long.valueOf(domainIdString.trim());
-                        if (isRecursive) {
-                            if 
(_domainDao.isChildDomain(account.getDomainId(), dId)) {
-                                toRemove = false;
-                                break;
-                            }
-                        } else {
-                            if (_domainDao.isChildDomain(dId, 
account.getDomainId())) {
-                                toRemove = false;
-                                break;
-                            }
-                        }
-                    }
-                    if (toRemove) {
-                        it.remove();
-                    }
-                }
+        // Filter offerings that are not associated with caller's domain
+        // Fetch the offering ids from the details table since theres no smart 
way to filter them in the join ... yet!
+        Account caller = CallContext.current().getCallingAccount();
+        if (caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
+            Domain callerDomain = _domainDao.findById(caller.getDomainId());
+            List<Long> domainIds = 
_domainDao.getDomainParentIds(callerDomain.getId())
+                .stream().collect(Collectors.toList());
+            if (isRecursive) {
+                List<Long> childrenIds = 
_domainDao.getDomainChildrenIds(callerDomain.getPath());
+                if (childrenIds != null && !childrenIds.isEmpty())
+                domainIds.addAll(childrenIds);
             }
-        }
-        return new Pair<>(result.first(), result.second());
-    }
 
-    private List<ServiceOfferingJoinVO> 
filterOfferingsOnCurrentTags(List<ServiceOfferingJoinVO> offerings, 
ServiceOfferingVO currentVmOffering) {
-        if (currentVmOffering == null) {
-            return offerings;
-        }
-        List<String> currentTagsList = 
StringUtils.csvTagsToList(currentVmOffering.getTags());
+            List<Long> ids = 
_diskOfferingDetailsDao.findOfferingIdsByDomainIds(domainIds);
+
+            if (ids == null || ids.isEmpty()) {

Review comment:
       if else part can be refactored as follows as most of it contains common 
code
   
   ```
   SearchBuilder<DiskOfferingJoinVO> sb = 
_diskOfferingJoinDao.createSearchBuilder();
   sb.or("domainId", sb.entity().getDomainId(), Op.NULL);
   
   SearchCriteria<DiskOfferingJoinVO> scc = sb.create();
   sc.addAnd("domainId", SearchCriteria.Op.SC, scc);
   
   if (ids != null &&  !ids.isEmpty()) {
       sb.and("id", sb.entity().getId(), Op.IN);
       scc.setParameters("id", ids.toArray());
   }
   
   sb.done();
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to