@@ -531,6 +531,15 @@ public class ApiResponseHelper implements ResponseGenerator {
531531 @ Inject
532532 ResourceIconManager resourceIconManager ;
533533
534+ public static String getPrettyDomainPath (String path ) {
535+ if (path == null ) {
536+ return null ;
537+ }
538+ StringBuilder domainPath = new StringBuilder ("ROOT" );
539+ (domainPath .append (path )).deleteCharAt (domainPath .length () - 1 );
540+ return domainPath .toString ();
541+ }
542+
534543 @ Override
535544 public UserResponse createUserResponse (User user ) {
536545 UserAccountJoinVO vUser = ApiDBUtils .newUserView (user );
@@ -568,9 +577,7 @@ public DomainResponse createDomainResponse(Domain domain) {
568577 if (parentDomain != null ) {
569578 domainResponse .setParentDomainId (parentDomain .getUuid ());
570579 }
571- StringBuilder domainPath = new StringBuilder ("ROOT" );
572- (domainPath .append (domain .getPath ())).deleteCharAt (domainPath .length () - 1 );
573- domainResponse .setPath (domainPath .toString ());
580+ domainResponse .setPath (getPrettyDomainPath (domain .getPath ()));
574581 if (domain .getParent () != null ) {
575582 domainResponse .setParentDomainName (ApiDBUtils .findDomainById (domain .getParent ()).getName ());
576583 }
@@ -823,21 +830,6 @@ public VMSnapshotResponse createVMSnapshotResponse(VMSnapshot vmSnapshot) {
823830 }
824831 }
825832 populateOwner (vmSnapshotResponse , vmSnapshot );
826- Project project = ApiDBUtils .findProjectByProjectAccountId (vmSnapshot .getAccountId ());
827- if (project != null ) {
828- vmSnapshotResponse .setProjectId (project .getUuid ());
829- vmSnapshotResponse .setProjectName (project .getName ());
830- }
831- Account account = ApiDBUtils .findAccountById (vmSnapshot .getAccountId ());
832- if (account != null ) {
833- vmSnapshotResponse .setAccountName (account .getAccountName ());
834- }
835- DomainVO domain = ApiDBUtils .findDomainById (vmSnapshot .getDomainId ());
836- if (domain != null ) {
837- vmSnapshotResponse .setDomainId (domain .getUuid ());
838- vmSnapshotResponse .setDomainName (domain .getName ());
839- vmSnapshotResponse .setDomainPath (domain .getPath ());
840- }
841833
842834 List <? extends ResourceTag > tags = _resourceTagDao .listBy (vmSnapshot .getId (), ResourceObjectType .VMSnapshot );
843835 List <ResourceTagResponse > tagResponses = new ArrayList <ResourceTagResponse >();
@@ -2359,18 +2351,7 @@ public SecurityGroupResponse createSecurityGroupResponseFromSecurityGroupRule(Li
23592351 response .setName (securityGroup .getName ());
23602352 response .setDescription (securityGroup .getDescription ());
23612353
2362- Account account = securiytGroupAccounts .get (securityGroup .getAccountId ());
2363-
2364- if (securityGroup .getAccountType () == Account .Type .PROJECT ) {
2365- response .setProjectId (securityGroup .getProjectUuid ());
2366- response .setProjectName (securityGroup .getProjectName ());
2367- } else {
2368- response .setAccountName (securityGroup .getAccountName ());
2369- }
2370-
2371- response .setDomainId (securityGroup .getDomainUuid ());
2372- response .setDomainName (securityGroup .getDomainName ());
2373- response .setDomainPath (securityGroup .getDomainPath ());
2354+ populateOwner (response , securityGroup );
23742355
23752356 for (SecurityRule securityRule : securityRules ) {
23762357 SecurityGroupRuleResponse securityGroupData = new SecurityGroupRuleResponse ();
@@ -2767,32 +2748,18 @@ public NetworkResponse createNetworkResponse(ResponseView view, Network network)
27672748 // get domain from network_domain table
27682749 Pair <Long , Boolean > domainNetworkDetails = ApiDBUtils .getDomainNetworkDetails (network .getId ());
27692750 if (domainNetworkDetails .first () != null ) {
2770- Domain domain = ApiDBUtils .findDomainById (domainNetworkDetails .first ());
2771- if (domain != null ) {
2772- response .setDomainId (domain .getUuid ());
2773-
2774- StringBuilder domainPath = new StringBuilder ("ROOT" );
2775- (domainPath .append (domain .getPath ())).deleteCharAt (domainPath .length () - 1 );
2776- response .setDomainPath (domainPath .toString ());
2777- }
2751+ populateDomain (response , domainNetworkDetails .first ());
27782752 }
27792753 response .setSubdomainAccess (domainNetworkDetails .second ());
27802754 }
27812755
27822756 Long dedicatedDomainId = ApiDBUtils .getDedicatedNetworkDomain (network .getId ());
27832757 if (dedicatedDomainId != null ) {
2784- Domain domain = ApiDBUtils .findDomainById (dedicatedDomainId );
2785- if (domain != null ) {
2786- response .setDomainId (domain .getUuid ());
2787- response .setDomainName (domain .getName ());
2788- response .setDomainPath (domain .getPath ());
2789- }
2790-
2758+ populateDomain (response , dedicatedDomainId );
27912759 }
27922760
27932761 response .setSpecifyIpRanges (network .getSpecifyIpRanges ());
27942762
2795-
27962763 setVpcIdInResponse (network .getVpcId (), response ::setVpcId , response ::setVpcName );
27972764
27982765 setResponseAssociatedNetworkInformation (response , network .getId ());
@@ -3054,14 +3021,10 @@ private void populateOwner(ControlledEntityResponse response, ControlledEntity o
30543021 } else {
30553022 response .setAccountName (account .getAccountName ());
30563023 }
3057-
3058- Domain domain = ApiDBUtils .findDomainById (object .getDomainId ());
3059- response .setDomainId (domain .getUuid ());
3060- response .setDomainName (domain .getName ());
3061- response .setDomainPath (domain .getPath ());
3024+ populateDomain (response , object .getDomainId ());
30623025 }
30633026
3064- private void populateOwner (ControlledViewEntityResponse response , ControlledEntity object ) {
3027+ public static void populateOwner (ControlledViewEntityResponse response , ControlledEntity object ) {
30653028 Account account = ApiDBUtils .findAccountById (object .getAccountId ());
30663029
30673030 if (account .getType () == Account .Type .PROJECT ) {
@@ -3073,10 +3036,7 @@ private void populateOwner(ControlledViewEntityResponse response, ControlledEnti
30733036 response .setAccountName (account .getAccountName ());
30743037 }
30753038
3076- Domain domain = ApiDBUtils .findDomainById (object .getDomainId ());
3077- response .setDomainId (domain .getUuid ());
3078- response .setDomainName (domain .getName ());
3079- response .setDomainPath (domain .getPath ());
3039+ populateDomain (response , object .getDomainId ());
30803040 }
30813041
30823042 public static void populateOwner (ControlledViewEntityResponse response , ControlledViewEntity object ) {
@@ -3090,7 +3050,7 @@ public static void populateOwner(ControlledViewEntityResponse response, Controll
30903050
30913051 response .setDomainId (object .getDomainUuid ());
30923052 response .setDomainName (object .getDomainName ());
3093- response .setDomainPath (object .getDomainPath ());
3053+ response .setDomainPath (getPrettyDomainPath ( object .getDomainPath () ));
30943054 }
30953055
30963056 private void populateAccount (ControlledEntityResponse response , long accountId ) {
@@ -3114,10 +3074,22 @@ private void populateAccount(ControlledEntityResponse response, long accountId)
31143074
31153075 private void populateDomain (ControlledEntityResponse response , long domainId ) {
31163076 Domain domain = ApiDBUtils .findDomainById (domainId );
3077+ if (domain == null ) {
3078+ return ;
3079+ }
3080+ response .setDomainId (domain .getUuid ());
3081+ response .setDomainName (domain .getName ());
3082+ response .setDomainPath (getPrettyDomainPath (domain .getPath ()));
3083+ }
31173084
3085+ private static void populateDomain (ControlledViewEntityResponse response , long domainId ) {
3086+ Domain domain = ApiDBUtils .findDomainById (domainId );
3087+ if (domain == null ) {
3088+ return ;
3089+ }
31183090 response .setDomainId (domain .getUuid ());
31193091 response .setDomainName (domain .getName ());
3120- response .setDomainPath (domain .getPath ());
3092+ response .setDomainPath (getPrettyDomainPath ( domain .getPath () ));
31213093 }
31223094
31233095 @ Override
@@ -4109,12 +4081,7 @@ public UsageRecordResponse createUsageResponse(Usage usageRecord, Map<String, Se
41094081 usageRecResponse .setAccountName (account .getAccountName ());
41104082 }
41114083
4112- Domain domain = ApiDBUtils .findDomainById (usageRecord .getDomainId ());
4113- if (domain != null ) {
4114- usageRecResponse .setDomainId (domain .getUuid ());
4115- usageRecResponse .setDomainName (domain .getName ());
4116- usageRecResponse .setDomainPath (domain .getPath ());
4117- }
4084+ populateDomain (usageRecResponse , account .getDomainId ());
41184085
41194086 if (usageRecord .getZoneId () != null ) {
41204087 DataCenter zone = ApiDBUtils .findZoneById (usageRecord .getZoneId ());
@@ -4901,18 +4868,11 @@ public AffinityGroupResponse createAffinityGroupResponse(AffinityGroup group) {
49014868
49024869 AffinityGroupResponse response = new AffinityGroupResponse ();
49034870
4904- Account account = ApiDBUtils .findAccountById (group .getAccountId ());
49054871 response .setId (group .getUuid ());
4906- response .setAccountName (account .getAccountName ());
49074872 response .setName (group .getName ());
49084873 response .setType (group .getType ());
49094874 response .setDescription (group .getDescription ());
4910- Domain domain = ApiDBUtils .findDomainById (account .getDomainId ());
4911- if (domain != null ) {
4912- response .setDomainId (domain .getUuid ());
4913- response .setDomainName (domain .getName ());
4914- response .setDomainPath (domain .getPath ());
4915- }
4875+ populateOwner (response , group );
49164876
49174877 response .setObjectName ("affinitygroup" );
49184878 return response ;
0 commit comments