001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 Echo Three, LLC 003// 004// Licensed under the Apache License, Version 2.0 (the "License"); 005// you may not use this file except in compliance with the License. 006// You may obtain a copy of the License at 007// 008// http://www.apache.org/licenses/LICENSE-2.0 009// 010// Unless required by applicable law or agreed to in writing, software 011// distributed under the License is distributed on an "AS IS" BASIS, 012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013// See the License for the specific language governing permissions and 014// limitations under the License. 015// -------------------------------------------------------------------------------- 016 017package com.echothree.model.control.security.server.graphql; 018 019import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject; 020import com.echothree.model.control.graphql.server.util.BaseGraphQl; 021import com.echothree.model.control.graphql.server.graphql.count.Connections; 022import com.echothree.model.control.graphql.server.graphql.count.CountedObjects; 023import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher; 024import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData; 025import com.echothree.model.control.graphql.server.util.count.ObjectLimiter; 026import com.echothree.model.control.security.server.control.SecurityControl; 027import com.echothree.model.control.user.server.control.UserControl; 028import com.echothree.model.data.security.common.SecurityRoleConstants; 029import com.echothree.model.data.security.server.entity.SecurityRoleGroup; 030import com.echothree.model.data.security.server.entity.SecurityRoleGroupDetail; 031import com.echothree.util.server.persistence.Session; 032import graphql.annotations.annotationTypes.GraphQLDescription; 033import graphql.annotations.annotationTypes.GraphQLField; 034import graphql.annotations.annotationTypes.GraphQLName; 035import graphql.annotations.annotationTypes.GraphQLNonNull; 036import graphql.annotations.connection.GraphQLConnection; 037import graphql.schema.DataFetchingEnvironment; 038import java.util.ArrayList; 039import java.util.stream.Collectors; 040 041@GraphQLDescription("security role group object") 042@GraphQLName("SecurityRoleGroup") 043public class SecurityRoleGroupObject 044 extends BaseEntityInstanceObject { 045 046 private final SecurityRoleGroup securityRoleGroup; // Always Present 047 048 public SecurityRoleGroupObject(SecurityRoleGroup securityRoleGroup) { 049 super(securityRoleGroup.getPrimaryKey()); 050 051 this.securityRoleGroup = securityRoleGroup; 052 } 053 054 private SecurityRoleGroupDetail securityRoleGroupDetail; // Optional, use getSecurityRoleGroupDetail() 055 056 private SecurityRoleGroupDetail getSecurityRoleGroupDetail() { 057 if(securityRoleGroupDetail == null) { 058 securityRoleGroupDetail = securityRoleGroup.getLastDetail(); 059 } 060 061 return securityRoleGroupDetail; 062 } 063 064 @GraphQLField 065 @GraphQLDescription("security role group name") 066 @GraphQLNonNull 067 public String getSecurityRoleGroupName() { 068 return getSecurityRoleGroupDetail().getSecurityRoleGroupName(); 069 } 070 071 @GraphQLField 072 @GraphQLDescription("parent item category") 073 public SecurityRoleGroupObject getParentSecurityRoleGroup() { 074 SecurityRoleGroup parentSecurityRoleGroup = getSecurityRoleGroupDetail().getParentSecurityRoleGroup(); 075 076 return parentSecurityRoleGroup == null ? null : new SecurityRoleGroupObject(parentSecurityRoleGroup); 077 } 078 079 @GraphQLField 080 @GraphQLDescription("is default") 081 @GraphQLNonNull 082 public boolean getIsDefault() { 083 return getSecurityRoleGroupDetail().getIsDefault(); 084 } 085 086 @GraphQLField 087 @GraphQLDescription("sort order") 088 @GraphQLNonNull 089 public int getSortOrder() { 090 return getSecurityRoleGroupDetail().getSortOrder(); 091 } 092 093 @GraphQLField 094 @GraphQLDescription("description") 095 @GraphQLNonNull 096 public String getDescription(final DataFetchingEnvironment env) { 097 var securityControl = Session.getModelController(SecurityControl.class); 098 var userControl = Session.getModelController(UserControl.class); 099 100 return securityControl.getBestSecurityRoleGroupDescription(securityRoleGroup, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env))); 101 } 102 103 @GraphQLField 104 @GraphQLDescription("security roles") 105 @GraphQLNonNull 106 @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class) 107 public CountingPaginatedData<SecurityRoleObject> getSecurityRoles(final DataFetchingEnvironment env) { 108 if(SecuritySecurityUtils.getHasSecurityRolesAccess(env)) { 109 var securityControl = Session.getModelController(SecurityControl.class); 110 var totalCount = securityControl.countSecurityRolesBySecurityRoleGroup(securityRoleGroup); 111 112 try(var objectLimiter = new ObjectLimiter(env, SecurityRoleConstants.COMPONENT_VENDOR_NAME, SecurityRoleConstants.ENTITY_TYPE_NAME, totalCount)) { 113 var entities = securityControl.getSecurityRoles(securityRoleGroup); 114 var wishlistPriorities = entities.stream().map(SecurityRoleObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size()))); 115 116 return new CountedObjects<>(objectLimiter, wishlistPriorities); 117 } 118 } else { 119 return Connections.emptyConnection(); 120 } 121 } 122 123}