@@ -855,11 +855,11 @@ private Pair<List<UserVmJoinVO>, Integer> searchForUserVMsByCriteria(Criteria c,
855855 sc .setParameters ("stateNEQ" , "Destroyed" );
856856 }
857857 }
858-
858+
859859 if (zoneType != null ) {
860860 sc .setParameters ("dataCenterType" , zoneType );
861861 }
862-
862+
863863 if (pod != null ) {
864864 sc .setParameters ("podId" , pod );
865865
@@ -1112,7 +1112,7 @@ private Pair<List<DomainRouterJoinVO>, Integer> searchForRoutersInternal(ListRou
11121112 if (zoneType != null ) {
11131113 sc .setParameters ("dataCenterType" , zoneType );
11141114 }
1115-
1115+
11161116 if (pod != null ) {
11171117 sc .setParameters ("podId" , pod );
11181118 }
@@ -2346,7 +2346,7 @@ private Pair<List<DataCenterJoinVO>, Integer> listDataCentersInternal(ListZonesB
23462346
23472347 if (networkType != null )
23482348 sc .addAnd ("networkType" , SearchCriteria .Op .EQ , networkType );
2349-
2349+
23502350 if (id != null ) {
23512351 sc .addAnd ("id" , SearchCriteria .Op .EQ , id );
23522352 } else if (name != null ) {
@@ -2519,9 +2519,10 @@ private Pair<List<TemplateJoinVO>, Integer> searchForTemplatesInternal(ListTempl
25192519 cmd .getZoneId (), hypervisorType , showDomr , cmd .listInReadyState (), permittedAccounts , caller , listProjectResourcesCriteria , tags );
25202520 }
25212521
2522- private Pair <List <TemplateJoinVO >, Integer > searchForTemplatesInternal (Long templateId , String name , String keyword , TemplateFilter templateFilter , boolean isIso ,
2523- Boolean bootable , Long pageSize , Long startIndex , Long zoneId , HypervisorType hyperType , boolean showDomr , boolean onlyReady ,
2524- List <Account > permittedAccounts , Account caller , ListProjectResourcesCriteria listProjectResourcesCriteria , Map <String , String > tags ) {
2522+ private Pair <List <TemplateJoinVO >, Integer > searchForTemplatesInternal (Long templateId , String name , String keyword ,
2523+ TemplateFilter templateFilter , boolean isIso , Boolean bootable , Long pageSize , Long startIndex , Long zoneId , HypervisorType hyperType ,
2524+ boolean showDomr , boolean onlyReady , List <Account > permittedAccounts , Account caller ,
2525+ ListProjectResourcesCriteria listProjectResourcesCriteria , Map <String , String > tags ) {
25252526 VMTemplateVO template = null ;
25262527
25272528 // verify templateId parameter
@@ -2571,167 +2572,171 @@ private Pair<List<TemplateJoinVO>, Integer> searchForTemplatesInternal(Long temp
25712572 // add criteria for project or not
25722573 if (listProjectResourcesCriteria == ListProjectResourcesCriteria .SkipProjectResources ) {
25732574 sc .addAnd ("accountType" , SearchCriteria .Op .NEQ , Account .ACCOUNT_TYPE_PROJECT );
2574- } else if (listProjectResourcesCriteria == ListProjectResourcesCriteria .ListProjectResourcesOnly ){
2575+ } else if (listProjectResourcesCriteria == ListProjectResourcesCriteria .ListProjectResourcesOnly ) {
25752576 sc .addAnd ("accountType" , SearchCriteria .Op .EQ , Account .ACCOUNT_TYPE_PROJECT );
25762577 }
25772578
25782579 // add criteria for domain path in case of domain admin
2579- if ((templateFilter == TemplateFilter .self || templateFilter == TemplateFilter .selfexecutable ) &&
2580- (caller .getType () == Account .ACCOUNT_TYPE_DOMAIN_ADMIN || caller .getType () == Account .ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN )) {
2581- sc .addAnd ("domainPath" , SearchCriteria .Op .LIKE , domain .getPath () + "%" );
2582- }
2583-
2584- List <Long > relatedDomainIds = new ArrayList <Long >();
2585- List <Long > permittedAccountIds = new ArrayList <Long >();
2586- if (!permittedAccounts .isEmpty ()) {
2587- for (Account account : permittedAccounts ) {
2588- permittedAccountIds .add (account .getId ());
2589- DomainVO accountDomain = _domainDao .findById (account .getDomainId ());
2580+ if ((templateFilter == TemplateFilter .self || templateFilter == TemplateFilter .selfexecutable )
2581+ && (caller .getType () == Account .ACCOUNT_TYPE_DOMAIN_ADMIN || caller .getType () == Account .ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN )) {
2582+ sc .addAnd ("domainPath" , SearchCriteria .Op .LIKE , domain .getPath () + "%" );
2583+ }
25902584
2591- // get all parent domain ID's all the way till root domain
2592- DomainVO domainTreeNode = accountDomain ;
2585+ List <Long > relatedDomainIds = new ArrayList <Long >();
2586+ List <Long > permittedAccountIds = new ArrayList <Long >();
2587+ if (!permittedAccounts .isEmpty ()) {
2588+ for (Account account : permittedAccounts ) {
2589+ permittedAccountIds .add (account .getId ());
2590+ DomainVO accountDomain = _domainDao .findById (account .getDomainId ());
2591+
2592+ // get all parent domain ID's all the way till root domain
2593+ DomainVO domainTreeNode = accountDomain ;
2594+ relatedDomainIds .add (domainTreeNode .getId ());
2595+ while (domainTreeNode .getParent () != null ) {
2596+ domainTreeNode = _domainDao .findById (domainTreeNode .getParent ());
25932597 relatedDomainIds .add (domainTreeNode .getId ());
2594- while (domainTreeNode .getParent () != null ){
2595- domainTreeNode = _domainDao .findById (domainTreeNode .getParent ());
2596- relatedDomainIds .add (domainTreeNode .getId ());
2597- }
2598+ }
25982599
2599- // get all child domain ID's
2600- if (_accountMgr .isAdmin (account .getType ()) ) {
2601- List <DomainVO > allChildDomains = _domainDao .findAllChildren (accountDomain .getPath (), accountDomain .getId ());
2602- for (DomainVO childDomain : allChildDomains ) {
2603- relatedDomainIds .add (childDomain .getId ());
2604- }
2600+ // get all child domain ID's
2601+ if (_accountMgr .isAdmin (account .getType ())) {
2602+ List <DomainVO > allChildDomains = _domainDao .findAllChildren (accountDomain .getPath (), accountDomain .getId ());
2603+ for (DomainVO childDomain : allChildDomains ) {
2604+ relatedDomainIds .add (childDomain .getId ());
26052605 }
26062606 }
26072607 }
2608+ }
26082609
2609- // add hypervisor criteria
2610- if ( hypers != null && !hypers .isEmpty ()){
2610+ if (!isIso ) {
2611+ // add hypervisor criteria for template case
2612+ if (hypers != null && !hypers .isEmpty ()) {
26112613 String [] relatedHypers = new String [hypers .size ()];
2612- for (int i = 0 ; i < hypers .size (); i ++){
2614+ for (int i = 0 ; i < hypers .size (); i ++) {
26132615 relatedHypers [i ] = hypers .get (i ).toString ();
26142616 }
26152617 sc .addAnd ("hypervisorType" , SearchCriteria .Op .IN , relatedHypers );
26162618 }
2619+ }
26172620
2618- // control different template filters
2619- if (templateFilter == TemplateFilter .featured || templateFilter == TemplateFilter .community ) {
2620- sc .addAnd ("publicTemplate" , SearchCriteria .Op .EQ , true );
2621- if ( templateFilter == TemplateFilter .featured ){
2621+ // control different template filters
2622+ if (templateFilter == TemplateFilter .featured || templateFilter == TemplateFilter .community ) {
2623+ sc .addAnd ("publicTemplate" , SearchCriteria .Op .EQ , true );
2624+ if (templateFilter == TemplateFilter .featured ) {
26222625 sc .addAnd ("featured" , SearchCriteria .Op .EQ , true );
2623- }
2624- else {
2625- sc .addAnd ("featured" , SearchCriteria .Op .EQ , false );
2626- }
2627- if (!permittedAccounts .isEmpty ()) {
2628- SearchCriteria <TemplateJoinVO > scc = _templateJoinDao .createSearchCriteria ();
2629- scc .addOr ("domainId" , SearchCriteria .Op .IN , relatedDomainIds .toArray ());
2630- scc .addOr ("domainId" , SearchCriteria .Op .NULL );
2631- sc .addAnd ("domainId" , SearchCriteria .Op .SC , scc );
2632-
2633- if (!_accountMgr .isAdmin (caller .getType ())){
2634- // for non-root users, we should only show featured and community templates that they can see
2635- sc .addAnd ("accountId" , SearchCriteria .Op .IN , permittedAccountIds .toArray ());
2636- }
2637- }
2638- } else if (templateFilter == TemplateFilter .self || templateFilter == TemplateFilter .selfexecutable ) {
2639- if ( !permittedAccounts .isEmpty ()){
2626+ } else {
2627+ sc .addAnd ("featured" , SearchCriteria .Op .EQ , false );
2628+ }
2629+ if (!permittedAccounts .isEmpty ()) {
2630+ SearchCriteria <TemplateJoinVO > scc = _templateJoinDao .createSearchCriteria ();
2631+ scc .addOr ("domainId" , SearchCriteria .Op .IN , relatedDomainIds .toArray ());
2632+ scc .addOr ("domainId" , SearchCriteria .Op .NULL );
2633+ sc .addAnd ("domainId" , SearchCriteria .Op .SC , scc );
2634+
2635+ if (!_accountMgr .isAdmin (caller .getType ())) {
2636+ // for non-root users, we should only show featured and
2637+ // community templates that they can see
26402638 sc .addAnd ("accountId" , SearchCriteria .Op .IN , permittedAccountIds .toArray ());
26412639 }
2642- } else if (templateFilter == TemplateFilter .sharedexecutable || templateFilter == TemplateFilter .shared ) {
2643- SearchCriteria <TemplateJoinVO > scc = _templateJoinDao .createSearchCriteria ();
2640+ }
2641+ } else if (templateFilter == TemplateFilter .self || templateFilter == TemplateFilter .selfexecutable ) {
2642+ if (!permittedAccounts .isEmpty ()) {
2643+ sc .addAnd ("accountId" , SearchCriteria .Op .IN , permittedAccountIds .toArray ());
2644+ }
2645+ } else if (templateFilter == TemplateFilter .sharedexecutable || templateFilter == TemplateFilter .shared ) {
2646+ SearchCriteria <TemplateJoinVO > scc = _templateJoinDao .createSearchCriteria ();
2647+ scc .addOr ("accountId" , SearchCriteria .Op .IN , permittedAccountIds .toArray ());
2648+ scc .addOr ("sharedAccountId" , SearchCriteria .Op .IN , permittedAccountIds .toArray ());
2649+ sc .addAnd ("accountId" , SearchCriteria .Op .SC , scc );
2650+ } else if (templateFilter == TemplateFilter .executable ) {
2651+ SearchCriteria <TemplateJoinVO > scc = _templateJoinDao .createSearchCriteria ();
2652+ scc .addOr ("publicTemplate" , SearchCriteria .Op .EQ , true );
2653+ if (!permittedAccounts .isEmpty ()) {
26442654 scc .addOr ("accountId" , SearchCriteria .Op .IN , permittedAccountIds .toArray ());
2645- scc .addOr ("sharedAccountId" , SearchCriteria .Op .IN , permittedAccountIds .toArray ());
2646- sc .addAnd ("accountId" , SearchCriteria .Op .SC , scc );
2647- } else if (templateFilter == TemplateFilter .executable ) {
2648- SearchCriteria <TemplateJoinVO > scc = _templateJoinDao .createSearchCriteria ();
2649- scc .addOr ("publicTemplate" , SearchCriteria .Op .EQ , true );
2650- if ( !permittedAccounts .isEmpty ()){
2651- scc .addOr ("accountId" , SearchCriteria .Op .IN , permittedAccountIds .toArray ());
2652- }
2653- sc .addAnd ("publicTemplate" , SearchCriteria .Op .SC , scc );
26542655 }
2656+ sc .addAnd ("publicTemplate" , SearchCriteria .Op .SC , scc );
2657+ }
26552658
2656- // add tags criteria
2657- if (tags != null && !tags .isEmpty ()) {
2658- SearchCriteria <TemplateJoinVO > scc = _templateJoinDao .createSearchCriteria ();
2659- int count = 0 ;
2660- for (String key : tags .keySet ()) {
2661- SearchCriteria <TemplateJoinVO > scTag = _templateJoinDao .createSearchCriteria ();
2662- scTag .addAnd ("tagKey" , SearchCriteria .Op .EQ , key );
2663- scTag .addAnd ("tagValue" , SearchCriteria .Op .EQ , tags .get (key ));
2664- if ( isIso ){
2665- scTag .addAnd ("tagResourceType" , SearchCriteria .Op .EQ , TaggedResourceType .ISO );
2666- } else {
2667- scTag .addAnd ("tagResourceType" , SearchCriteria .Op .EQ , TaggedResourceType .Template );
2668- }
2669- scc .addOr ("tagKey" , SearchCriteria .Op .SC , scTag );
2670- count ++;
2659+ // add tags criteria
2660+ if (tags != null && !tags .isEmpty ()) {
2661+ SearchCriteria <TemplateJoinVO > scc = _templateJoinDao .createSearchCriteria ();
2662+ int count = 0 ;
2663+ for (String key : tags .keySet ()) {
2664+ SearchCriteria <TemplateJoinVO > scTag = _templateJoinDao .createSearchCriteria ();
2665+ scTag .addAnd ("tagKey" , SearchCriteria .Op .EQ , key );
2666+ scTag .addAnd ("tagValue" , SearchCriteria .Op .EQ , tags .get (key ));
2667+ if (isIso ) {
2668+ scTag .addAnd ("tagResourceType" , SearchCriteria .Op .EQ , TaggedResourceType .ISO );
2669+ } else {
2670+ scTag .addAnd ("tagResourceType" , SearchCriteria .Op .EQ , TaggedResourceType .Template );
26712671 }
2672- sc .addAnd ("tagKey" , SearchCriteria .Op .SC , scc );
2672+ scc .addOr ("tagKey" , SearchCriteria .Op .SC , scTag );
2673+ count ++;
26732674 }
2675+ sc .addAnd ("tagKey" , SearchCriteria .Op .SC , scc );
2676+ }
26742677
2675- // other criteria
2676- if (templateId != null ){
2677- sc .addAnd ("id" , SearchCriteria .Op .EQ , templateId );
2678- }
2679- else if (keyword != null ) {
2680- sc .addAnd ("name" , SearchCriteria .Op .LIKE , "%" + keyword + "%" );
2681- } else if (name != null ) {
2682- sc .addAnd ("name" , SearchCriteria .Op .EQ , name );
2683- }
2678+ // other criteria
2679+ if (templateId != null ) {
2680+ sc .addAnd ("id" , SearchCriteria .Op .EQ , templateId );
2681+ } else if (keyword != null ) {
2682+ sc .addAnd ("name" , SearchCriteria .Op .LIKE , "%" + keyword + "%" );
2683+ } else if (name != null ) {
2684+ sc .addAnd ("name" , SearchCriteria .Op .EQ , name );
2685+ }
26842686
2685- if (isIso ) {
2686- sc .addAnd ("format" , SearchCriteria .Op .EQ , "ISO" );
2687+ if (isIso ) {
2688+ sc .addAnd ("format" , SearchCriteria .Op .EQ , "ISO" );
26872689
2688- } else {
2689- sc .addAnd ("format" , SearchCriteria .Op .NEQ , "ISO" );
2690- }
2690+ } else {
2691+ sc .addAnd ("format" , SearchCriteria .Op .NEQ , "ISO" );
2692+ }
26912693
2692- if (!hyperType .equals (HypervisorType .None )) {
2693- sc .addAnd ("hypervisorType" , SearchCriteria .Op .EQ , hyperType );
2694- }
2694+ if (!hyperType .equals (HypervisorType .None )) {
2695+ sc .addAnd ("hypervisorType" , SearchCriteria .Op .EQ , hyperType );
2696+ }
26952697
2696- if (bootable != null ) {
2697- sc .addAnd ("bootable" , SearchCriteria .Op .EQ , bootable );
2698- }
2698+ if (bootable != null ) {
2699+ sc .addAnd ("bootable" , SearchCriteria .Op .EQ , bootable );
2700+ }
26992701
2700- if (onlyReady ){
2701- sc .addAnd ("downloadState" , SearchCriteria .Op .EQ , Status .DOWNLOADED );
2702- sc .addAnd ("destroyed" , SearchCriteria .Op .EQ , false );
2703- }
2702+ if (onlyReady ) {
2703+ sc .addAnd ("downloadState" , SearchCriteria .Op .EQ , Status .DOWNLOADED );
2704+ sc .addAnd ("destroyed" , SearchCriteria .Op .EQ , false );
2705+ }
27042706
2705- if (zoneId != null ){
2706- sc .addAnd ("dataCenterId" , SearchCriteria .Op .EQ , zoneId );
2707- }
2707+ if (zoneId != null ) {
2708+ sc .addAnd ("dataCenterId" , SearchCriteria .Op .EQ , zoneId );
2709+ }
27082710
2709- if (!showDomr ){
2710- // excluding system template
2711- sc .addAnd ("templateType" , SearchCriteria .Op .NEQ , Storage .TemplateType .SYSTEM );
2712- }
2711+ if (!showDomr ) {
2712+ // excluding system template
2713+ sc .addAnd ("templateType" , SearchCriteria .Op .NEQ , Storage .TemplateType .SYSTEM );
2714+ }
27132715
2714- // don't return removed template, this should not be needed since we changed annotation for removed field in TemplateJoinVO.
2715- //sc.addAnd("removed", SearchCriteria.Op.NULL);
2716+ // don't return removed template, this should not be needed since we
2717+ // changed annotation for removed field in TemplateJoinVO.
2718+ // sc.addAnd("removed", SearchCriteria.Op.NULL);
27162719
2717- // search unique templates and find details by Ids
2718- Pair <List <TemplateJoinVO >, Integer > uniqueTmplPair = _templateJoinDao .searchAndCount (sc , searchFilter );
2719- Integer count = uniqueTmplPair .second ();
2720- if (count .intValue () == 0 ) {
2721- // empty result
2722- return uniqueTmplPair ;
2723- }
2724- List <TemplateJoinVO > uniqueTmpls = uniqueTmplPair .first ();
2725- Long [] vrIds = new Long [uniqueTmpls .size ()];
2726- int i = 0 ;
2727- for (TemplateJoinVO v : uniqueTmpls ) {
2728- vrIds [i ++] = v .getId ();
2729- }
2730- List <TemplateJoinVO > vrs = _templateJoinDao .searchByIds (vrIds );
2731- return new Pair <List <TemplateJoinVO >, Integer >(vrs , count );
2720+ // search unique templates and find details by Ids
2721+ Pair <List <TemplateJoinVO >, Integer > uniqueTmplPair = _templateJoinDao .searchAndCount (sc , searchFilter );
2722+ Integer count = uniqueTmplPair .second ();
2723+ if (count .intValue () == 0 ) {
2724+ // empty result
2725+ return uniqueTmplPair ;
2726+ }
2727+ List <TemplateJoinVO > uniqueTmpls = uniqueTmplPair .first ();
2728+ Long [] vrIds = new Long [uniqueTmpls .size ()];
2729+ int i = 0 ;
2730+ for (TemplateJoinVO v : uniqueTmpls ) {
2731+ vrIds [i ++] = v .getId ();
2732+ }
2733+ List <TemplateJoinVO > vrs = _templateJoinDao .searchByIds (vrIds );
2734+ return new Pair <List <TemplateJoinVO >, Integer >(vrs , count );
27322735
2733- //TODO: revisit the special logic for iso search in VMTemplateDaoImpl.searchForTemplates and understand why we need to
2734- //specially handle ISO. The original logic is very twisted and no idea about what the code was doing.
2736+ // TODO: revisit the special logic for iso search in
2737+ // VMTemplateDaoImpl.searchForTemplates and understand why we need to
2738+ // specially handle ISO. The original logic is very twisted and no idea
2739+ // about what the code was doing.
27352740
27362741 }
27372742
@@ -2776,7 +2781,7 @@ private Pair<List<TemplateJoinVO>, Integer> searchForIsosInternal(ListIsosCmd cm
27762781 cmd .getStartIndex (), cmd .getZoneId (), hypervisorType , true , cmd .listInReadyState (), permittedAccounts , caller ,
27772782 listProjectResourcesCriteria , tags );
27782783 }
2779-
2784+
27802785 public ListResponse <AffinityGroupResponse > listAffinityGroups (Long affinityGroupId , String affinityGroupName ,
27812786 String affinityGroupType , Long vmId , Long startIndex , Long pageSize ) {
27822787 Pair <List <AffinityGroupJoinVO >, Integer > result = listAffinityGroupsInternal (affinityGroupId ,
0 commit comments