@@ -530,6 +530,15 @@ public class ApiResponseHelper implements ResponseGenerator {
530530 @ Inject
531531 ResourceIconManager resourceIconManager ;
532532
533+ public static String getPrettyDomainPath (String path ) {
534+ if (path == null ) {
535+ return null ;
536+ }
537+ StringBuilder domainPath = new StringBuilder ("ROOT" );
538+ (domainPath .append (path )).deleteCharAt (domainPath .length () - 1 );
539+ return domainPath .toString ();
540+ }
541+
533542 @ Override
534543 public UserResponse createUserResponse (User user ) {
535544 UserAccountJoinVO vUser = ApiDBUtils .newUserView (user );
@@ -567,9 +576,7 @@ public DomainResponse createDomainResponse(Domain domain) {
567576 if (parentDomain != null ) {
568577 domainResponse .setParentDomainId (parentDomain .getUuid ());
569578 }
570- StringBuilder domainPath = new StringBuilder ("ROOT" );
571- (domainPath .append (domain .getPath ())).deleteCharAt (domainPath .length () - 1 );
572- domainResponse .setPath (domainPath .toString ());
579+ domainResponse .setPath (getPrettyDomainPath (domain .getPath ()));
573580 if (domain .getParent () != null ) {
574581 domainResponse .setParentDomainName (ApiDBUtils .findDomainById (domain .getParent ()).getName ());
575582 }
@@ -822,21 +829,6 @@ public VMSnapshotResponse createVMSnapshotResponse(VMSnapshot vmSnapshot) {
822829 }
823830 }
824831 populateOwner (vmSnapshotResponse , vmSnapshot );
825- Project project = ApiDBUtils .findProjectByProjectAccountId (vmSnapshot .getAccountId ());
826- if (project != null ) {
827- vmSnapshotResponse .setProjectId (project .getUuid ());
828- vmSnapshotResponse .setProjectName (project .getName ());
829- }
830- Account account = ApiDBUtils .findAccountById (vmSnapshot .getAccountId ());
831- if (account != null ) {
832- vmSnapshotResponse .setAccountName (account .getAccountName ());
833- }
834- DomainVO domain = ApiDBUtils .findDomainById (vmSnapshot .getDomainId ());
835- if (domain != null ) {
836- vmSnapshotResponse .setDomainId (domain .getUuid ());
837- vmSnapshotResponse .setDomainName (domain .getName ());
838- vmSnapshotResponse .setDomainPath (domain .getPath ());
839- }
840832
841833 List <? extends ResourceTag > tags = _resourceTagDao .listBy (vmSnapshot .getId (), ResourceObjectType .VMSnapshot );
842834 List <ResourceTagResponse > tagResponses = new ArrayList <ResourceTagResponse >();
@@ -2350,18 +2342,7 @@ public SecurityGroupResponse createSecurityGroupResponseFromSecurityGroupRule(Li
23502342 response .setName (securityGroup .getName ());
23512343 response .setDescription (securityGroup .getDescription ());
23522344
2353- Account account = securiytGroupAccounts .get (securityGroup .getAccountId ());
2354-
2355- if (securityGroup .getAccountType () == Account .Type .PROJECT ) {
2356- response .setProjectId (securityGroup .getProjectUuid ());
2357- response .setProjectName (securityGroup .getProjectName ());
2358- } else {
2359- response .setAccountName (securityGroup .getAccountName ());
2360- }
2361-
2362- response .setDomainId (securityGroup .getDomainUuid ());
2363- response .setDomainName (securityGroup .getDomainName ());
2364- response .setDomainPath (securityGroup .getDomainPath ());
2345+ populateOwner (response , securityGroup );
23652346
23662347 for (SecurityRule securityRule : securityRules ) {
23672348 SecurityGroupRuleResponse securityGroupData = new SecurityGroupRuleResponse ();
@@ -2758,32 +2739,18 @@ public NetworkResponse createNetworkResponse(ResponseView view, Network network)
27582739 // get domain from network_domain table
27592740 Pair <Long , Boolean > domainNetworkDetails = ApiDBUtils .getDomainNetworkDetails (network .getId ());
27602741 if (domainNetworkDetails .first () != null ) {
2761- Domain domain = ApiDBUtils .findDomainById (domainNetworkDetails .first ());
2762- if (domain != null ) {
2763- response .setDomainId (domain .getUuid ());
2764-
2765- StringBuilder domainPath = new StringBuilder ("ROOT" );
2766- (domainPath .append (domain .getPath ())).deleteCharAt (domainPath .length () - 1 );
2767- response .setDomainPath (domainPath .toString ());
2768- }
2742+ populateDomain (response , domainNetworkDetails .first ());
27692743 }
27702744 response .setSubdomainAccess (domainNetworkDetails .second ());
27712745 }
27722746
27732747 Long dedicatedDomainId = ApiDBUtils .getDedicatedNetworkDomain (network .getId ());
27742748 if (dedicatedDomainId != null ) {
2775- Domain domain = ApiDBUtils .findDomainById (dedicatedDomainId );
2776- if (domain != null ) {
2777- response .setDomainId (domain .getUuid ());
2778- response .setDomainName (domain .getName ());
2779- response .setDomainPath (domain .getPath ());
2780- }
2781-
2749+ populateDomain (response , dedicatedDomainId );
27822750 }
27832751
27842752 response .setSpecifyIpRanges (network .getSpecifyIpRanges ());
27852753
2786-
27872754 setVpcIdInResponse (network .getVpcId (), response ::setVpcId , response ::setVpcName );
27882755
27892756 setResponseAssociatedNetworkInformation (response , network .getId ());
@@ -3045,14 +3012,10 @@ private void populateOwner(ControlledEntityResponse response, ControlledEntity o
30453012 } else {
30463013 response .setAccountName (account .getAccountName ());
30473014 }
3048-
3049- Domain domain = ApiDBUtils .findDomainById (object .getDomainId ());
3050- response .setDomainId (domain .getUuid ());
3051- response .setDomainName (domain .getName ());
3052- response .setDomainPath (domain .getPath ());
3015+ populateDomain (response , object .getDomainId ());
30533016 }
30543017
3055- private void populateOwner (ControlledViewEntityResponse response , ControlledEntity object ) {
3018+ public static void populateOwner (ControlledViewEntityResponse response , ControlledEntity object ) {
30563019 Account account = ApiDBUtils .findAccountById (object .getAccountId ());
30573020
30583021 if (account .getType () == Account .Type .PROJECT ) {
@@ -3064,10 +3027,7 @@ private void populateOwner(ControlledViewEntityResponse response, ControlledEnti
30643027 response .setAccountName (account .getAccountName ());
30653028 }
30663029
3067- Domain domain = ApiDBUtils .findDomainById (object .getDomainId ());
3068- response .setDomainId (domain .getUuid ());
3069- response .setDomainName (domain .getName ());
3070- response .setDomainPath (domain .getPath ());
3030+ populateDomain (response , object .getDomainId ());
30713031 }
30723032
30733033 public static void populateOwner (ControlledViewEntityResponse response , ControlledViewEntity object ) {
@@ -3081,7 +3041,7 @@ public static void populateOwner(ControlledViewEntityResponse response, Controll
30813041
30823042 response .setDomainId (object .getDomainUuid ());
30833043 response .setDomainName (object .getDomainName ());
3084- response .setDomainPath (object .getDomainPath ());
3044+ response .setDomainPath (getPrettyDomainPath ( object .getDomainPath () ));
30853045 }
30863046
30873047 private void populateAccount (ControlledEntityResponse response , long accountId ) {
@@ -3105,10 +3065,22 @@ private void populateAccount(ControlledEntityResponse response, long accountId)
31053065
31063066 private void populateDomain (ControlledEntityResponse response , long domainId ) {
31073067 Domain domain = ApiDBUtils .findDomainById (domainId );
3068+ if (domain == null ) {
3069+ return ;
3070+ }
3071+ response .setDomainId (domain .getUuid ());
3072+ response .setDomainName (domain .getName ());
3073+ response .setDomainPath (getPrettyDomainPath (domain .getPath ()));
3074+ }
31083075
3076+ private static void populateDomain (ControlledViewEntityResponse response , long domainId ) {
3077+ Domain domain = ApiDBUtils .findDomainById (domainId );
3078+ if (domain == null ) {
3079+ return ;
3080+ }
31093081 response .setDomainId (domain .getUuid ());
31103082 response .setDomainName (domain .getName ());
3111- response .setDomainPath (domain .getPath ());
3083+ response .setDomainPath (getPrettyDomainPath ( domain .getPath () ));
31123084 }
31133085
31143086 @ Override
@@ -4100,12 +4072,7 @@ public UsageRecordResponse createUsageResponse(Usage usageRecord, Map<String, Se
41004072 usageRecResponse .setAccountName (account .getAccountName ());
41014073 }
41024074
4103- Domain domain = ApiDBUtils .findDomainById (usageRecord .getDomainId ());
4104- if (domain != null ) {
4105- usageRecResponse .setDomainId (domain .getUuid ());
4106- usageRecResponse .setDomainName (domain .getName ());
4107- usageRecResponse .setDomainPath (domain .getPath ());
4108- }
4075+ populateDomain (usageRecResponse , account .getDomainId ());
41094076
41104077 if (usageRecord .getZoneId () != null ) {
41114078 DataCenter zone = ApiDBUtils .findZoneById (usageRecord .getZoneId ());
@@ -4892,18 +4859,11 @@ public AffinityGroupResponse createAffinityGroupResponse(AffinityGroup group) {
48924859
48934860 AffinityGroupResponse response = new AffinityGroupResponse ();
48944861
4895- Account account = ApiDBUtils .findAccountById (group .getAccountId ());
48964862 response .setId (group .getUuid ());
4897- response .setAccountName (account .getAccountName ());
48984863 response .setName (group .getName ());
48994864 response .setType (group .getType ());
49004865 response .setDescription (group .getDescription ());
4901- Domain domain = ApiDBUtils .findDomainById (account .getDomainId ());
4902- if (domain != null ) {
4903- response .setDomainId (domain .getUuid ());
4904- response .setDomainName (domain .getName ());
4905- response .setDomainPath (domain .getPath ());
4906- }
4866+ populateOwner (response , group );
49074867
49084868 response .setObjectName ("affinitygroup" );
49094869 return response ;
0 commit comments