001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 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.core.server.graphql; 018 019import com.echothree.model.control.core.server.control.CoreControl; 020import com.echothree.model.control.graphql.server.graphql.count.Connections; 021import com.echothree.model.control.graphql.server.graphql.count.CountedObjects; 022import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher; 023import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData; 024import com.echothree.model.control.graphql.server.util.BaseGraphQl; 025import com.echothree.model.control.graphql.server.util.count.ObjectLimiter; 026import com.echothree.model.data.core.common.EntityCollectionAttributeConstants; 027import com.echothree.model.data.core.server.entity.EntityAttribute; 028import com.echothree.model.data.core.server.entity.EntityInstance; 029import com.echothree.util.server.persistence.Session; 030import graphql.annotations.annotationTypes.GraphQLDescription; 031import graphql.annotations.annotationTypes.GraphQLField; 032import graphql.annotations.annotationTypes.GraphQLName; 033import graphql.annotations.annotationTypes.GraphQLNonNull; 034import graphql.annotations.connection.GraphQLConnection; 035import graphql.schema.DataFetchingEnvironment; 036import java.util.ArrayList; 037import java.util.stream.Collectors; 038 039@GraphQLDescription("entity collection attribute object") 040@GraphQLName("EntityCollectionAttribute") 041public class EntityCollectionAttributeObject 042 implements BaseGraphQl, AttributeInterface { 043 044 private final EntityAttribute entityAttribute; // Always Present 045 private final EntityInstance entityInstance; // Always Present 046 047 public EntityCollectionAttributeObject(final EntityAttribute entityAttribute, final EntityInstance entityInstance) { 048 this.entityAttribute = entityAttribute; 049 this.entityInstance = entityInstance; 050 } 051 052 @GraphQLField 053 @GraphQLDescription("entity attribute") 054 public EntityAttributeObject getEntityAttribute(final DataFetchingEnvironment env) { 055 return CoreSecurityUtils.getHasEntityAttributeAccess(env) ? new EntityAttributeObject(entityAttribute, entityInstance) : null; 056 } 057 058 @GraphQLField 059 @GraphQLDescription("entity instance") 060 public EntityInstanceObject getEntityInstance(final DataFetchingEnvironment env) { 061 return CoreSecurityUtils.getHasEntityInstanceAccess(env) ? new EntityInstanceObject(entityInstance) : null; 062 } 063 064 @GraphQLField 065 @GraphQLDescription("entity instance attributes") 066 @GraphQLNonNull 067 @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class) 068 public CountingPaginatedData<EntityInstanceObject> getEntityInstanceAttributes(final DataFetchingEnvironment env) { 069 CountingPaginatedData<EntityInstanceObject> entityInstanceObjects; 070 071 if(CoreSecurityUtils.getHasEntityInstanceAccess(env)) { 072 var coreControl = Session.getModelController(CoreControl.class); 073 var totalCount = coreControl.countEntityCollectionAttributes(entityAttribute, entityInstance); 074 075 try(var objectLimiter = new ObjectLimiter(env, EntityCollectionAttributeConstants.COMPONENT_VENDOR_NAME, EntityCollectionAttributeConstants.ENTITY_TYPE_NAME, totalCount)) { 076 var entities = coreControl.getEntityCollectionAttributes(entityAttribute, entityInstance); 077 var entityInstances = entities.stream() 078 .map(entity -> new EntityInstanceObject(entity.getEntityInstanceAttribute())) 079 .collect(Collectors.toCollection(() -> new ArrayList<>(entities.size()))); 080 081 entityInstanceObjects = new CountedObjects<>(objectLimiter, entityInstances); 082 } 083 } else { 084 entityInstanceObjects = Connections.emptyConnection(); 085 } 086 087 return entityInstanceObjects; 088 } 089 090}