@ CheckForNull private PermissionTemplateDto findTemplate ( DbSession dbSession , ComponentDto component ) { String organizationUuid = component . getOrganizationUuid ( ) ; List < PermissionTemplateDto > allPermissionTemplates = dbClient . permissionTemplateDao ( ) . selectAll ( dbSession , organizationUuid , null ) ; List < PermissionTemplateDto > matchingTemplates = new ArrayList <> ( ) ; for ( PermissionTemplateDto permissionTemplateDto : allPermissionTemplates ) { String keyPattern = permissionTemplateDto . getKeyPattern ( ) ; if ( StringUtils . isNotBlank ( keyPattern ) && component . getDbKey ( ) . matches ( keyPattern ) ) { matchingTemplates . add ( permissionTemplateDto ) ; } } checkAtMostOneMatchForComponentKey ( component . getDbKey ( ) , matchingTemplates ) ; if ( matchingTemplates . size ( ) == 1 ) { return matchingTemplates . get ( 0 ) ; } DefaultTemplates defaultTemplates = dbClient . organizationDao ( ) . getDefaultTemplates ( dbSession , organizationUuid ) . orElseThrow ( ( ) -> new IllegalStateException ( format ( "No Default templates defined for organization with uuid '%s'" , organizationUuid ) ) ) ; String qualifier = component . qualifier ( ) ; DefaultTemplatesResolverImpl . ResolvedDefaultTemplates resolvedDefaultTemplates = defaultTemplatesResolver . resolve ( defaultTemplates ) ; switch ( qualifier ) { case Qualifiers . PROJECT : return dbClient . permissionTemplateDao ( ) . selectByUuid ( dbSession , resolvedDefaultTemplates . getProject ( ) ) ; case Qualifiers . VIEW : String portDefaultTemplateUuid = resolvedDefaultTemplates . getPortfolio ( ) . orElseThrow ( ( ) -> new IllegalStateException ( "Attempt to create a view when Governance plugin is not installed" ) ) ; return dbClient . permissionTemplateDao ( ) . selectByUuid ( dbSession , portDefaultTemplateUuid ) ; case Qualifiers . APP : String appDefaultTemplateUuid = resolvedDefaultTemplates . getApplication ( ) . orElseThrow ( ( ) -> new IllegalStateException ( "Attempt to create a view when Governance plugin is not installed" ) ) ; return dbClient . permissionTemplateDao ( ) . selectByUuid ( dbSession , appDefaultTemplateUuid ) ; default : throw new IllegalArgumentException ( format ( "Qualifier '%s' is not supported" , qualifier ) ) ; } }
Return the permission template for the given component . If no template key pattern match then consider default template for the component qualifier .