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.core.server.control.EntityAttributeGroupControl;
021import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
022import com.echothree.model.control.graphql.server.graphql.count.Connections;
023import com.echothree.model.control.graphql.server.graphql.count.CountedObjects;
024import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher;
025import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData;
026import com.echothree.model.control.graphql.server.util.BaseGraphQl;
027import com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
028import com.echothree.model.control.user.server.control.UserControl;
029import com.echothree.model.data.core.common.EntityAttributeConstants;
030import com.echothree.model.data.core.common.EntityAttributeEntityAttributeGroupConstants;
031import com.echothree.model.data.core.server.entity.EntityAttributeGroup;
032import com.echothree.model.data.core.server.entity.EntityAttributeGroupDetail;
033import com.echothree.model.data.core.server.entity.EntityInstance;
034import com.echothree.util.server.persistence.Session;
035import graphql.annotations.annotationTypes.GraphQLDescription;
036import graphql.annotations.annotationTypes.GraphQLField;
037import graphql.annotations.annotationTypes.GraphQLName;
038import graphql.annotations.annotationTypes.GraphQLNonNull;
039import graphql.annotations.connection.GraphQLConnection;
040import graphql.schema.DataFetchingEnvironment;
041import java.util.ArrayList;
042import java.util.stream.Collectors;
043
044@GraphQLDescription("entity attribute group object")
045@GraphQLName("EntityAttributeGroup")
046public class EntityAttributeGroupObject
047        extends BaseEntityInstanceObject {
048    
049    private final EntityAttributeGroup entityAttributeGroup; // Always Present
050    private final EntityInstance entityInstance;
051    
052    public EntityAttributeGroupObject(EntityAttributeGroup entityAttributeGroup, EntityInstance entityInstance) {
053        super(entityAttributeGroup.getPrimaryKey());
054
055        this.entityAttributeGroup = entityAttributeGroup;
056        this.entityInstance = entityInstance;
057    }
058
059    private EntityAttributeGroupDetail entityAttributeGroupDetail; // Optional, use getEntityAttributeGroupDetail()
060    
061    private EntityAttributeGroupDetail getEntityAttributeGroupDetail() {
062        if(entityAttributeGroupDetail == null) {
063            entityAttributeGroupDetail = entityAttributeGroup.getLastDetail();
064        }
065        
066        return entityAttributeGroupDetail;
067    }
068    
069    @GraphQLField
070    @GraphQLDescription("entity type")
071    @GraphQLNonNull
072    public EntityTypeObject getEntityType() {
073        return new EntityTypeObject(getEntityAttributeGroupDetail().getEntityType());
074    }
075
076    @GraphQLField
077    @GraphQLDescription("entity attribute group name")
078    @GraphQLNonNull
079    public String getEntityAttributeGroupName() {
080        return getEntityAttributeGroupDetail().getEntityAttributeGroupName();
081    }
082    
083    @GraphQLField
084    @GraphQLDescription("is default")
085    @GraphQLNonNull
086    public boolean getIsDefault() {
087        return getEntityAttributeGroupDetail().getIsDefault();
088    }
089    
090    @GraphQLField
091    @GraphQLDescription("sort order")
092    @GraphQLNonNull
093    public int getSortOrder() {
094        return getEntityAttributeGroupDetail().getSortOrder();
095    }
096    
097    @GraphQLField
098    @GraphQLDescription("description")
099    @GraphQLNonNull
100    public String getDescription(final DataFetchingEnvironment env) {
101        var entityAttributeGroupControl = Session.getModelController(EntityAttributeGroupControl.class);
102        var userControl = Session.getModelController(UserControl.class);
103
104        return entityAttributeGroupControl.getBestEntityAttributeGroupDescription(entityAttributeGroup, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
105    }
106
107    @GraphQLField
108    @GraphQLDescription("entity attributes")
109    @GraphQLNonNull
110    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
111    public CountingPaginatedData<EntityAttributeObject> getEntityAttributes(final DataFetchingEnvironment env) {
112        if(CoreSecurityUtils.getHasEntityAttributesAccess(env)) {
113            var coreControl = Session.getModelController(CoreControl.class);
114            var totalCount = coreControl.countEntityAttributesByEntityAttributeGroup(entityAttributeGroup);
115
116            try(var objectLimiter = new ObjectLimiter(env, EntityAttributeConstants.COMPONENT_VENDOR_NAME, EntityAttributeConstants.ENTITY_TYPE_NAME, totalCount)) {
117                var entities = coreControl.getEntityAttributesByEntityAttributeGroup(entityAttributeGroup);
118                var entityAttributes = entities.stream()
119                        .map(entity -> new EntityAttributeObject(entity, entityInstance))
120                        .collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
121
122                return new CountedObjects<>(objectLimiter, entityAttributes);
123            }
124        } else {
125            return Connections.emptyConnection();
126        }
127    }
128
129    @GraphQLField
130    @GraphQLDescription("entity attribute entity attribute groups")
131    @GraphQLNonNull
132    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
133    public CountingPaginatedData<EntityAttributeEntityAttributeGroupObject> getEntityAttributeEntityAttributeGroups(final DataFetchingEnvironment env) {
134        if(CoreSecurityUtils.getHasEntityAttributeEntityAttributeGroupsAccess(env)) {
135            var entityAttributeGroupControl = Session.getModelController(EntityAttributeGroupControl.class);
136            var totalCount = entityAttributeGroupControl.countEntityAttributeEntityAttributeGroupsByEntityAttributeGroup(entityAttributeGroup);
137
138            try(var objectLimiter = new ObjectLimiter(env, EntityAttributeEntityAttributeGroupConstants.COMPONENT_VENDOR_NAME, EntityAttributeEntityAttributeGroupConstants.ENTITY_TYPE_NAME, totalCount)) {
139                var entities = entityAttributeGroupControl.getEntityAttributeEntityAttributeGroupsByEntityAttributeGroup(entityAttributeGroup);
140                var entityAttributeEntityAttributeGroups = entities.stream()
141                        .map(EntityAttributeEntityAttributeGroupObject::new)
142                        .collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
143
144                return new CountedObjects<>(objectLimiter, entityAttributeEntityAttributeGroups);
145            }
146        } else {
147            return Connections.emptyConnection();
148        }
149    }
150
151}