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.core.server.graphql;
018
019import com.echothree.model.control.core.server.control.CoreControl;
020import com.echothree.model.control.graphql.server.graphql.HistoryInterface;
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.data.core.common.EntityBooleanAttributeConstants;
027import com.echothree.model.data.core.server.entity.EntityBooleanAttribute;
028import com.echothree.util.server.persistence.Session;
029import graphql.annotations.annotationTypes.GraphQLDescription;
030import graphql.annotations.annotationTypes.GraphQLField;
031import graphql.annotations.annotationTypes.GraphQLName;
032import graphql.annotations.annotationTypes.GraphQLNonNull;
033import graphql.annotations.connection.GraphQLConnection;
034import graphql.schema.DataFetchingEnvironment;
035import java.util.ArrayList;
036
037@GraphQLDescription("entity boolean attribute object")
038@GraphQLName("EntityBooleanAttribute")
039public class EntityBooleanAttributeObject
040        extends BaseEntityBooleanAttributeObject
041        implements AttributeInterface, HistoryInterface<EntityBooleanAttributeHistoryObject> {
042    
043    public EntityBooleanAttributeObject(EntityBooleanAttribute entityBooleanAttribute) {
044        super(entityBooleanAttribute);
045    }
046
047    @Override
048    @GraphQLField
049    @GraphQLDescription("history")
050    @GraphQLNonNull
051    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
052    public CountingPaginatedData<EntityBooleanAttributeHistoryObject> getHistory(final DataFetchingEnvironment env) {
053        if(true) { // TODO: Security Check
054            var coreControl = Session.getModelController(CoreControl.class);
055            var entityAttribute = entityBooleanAttribute.getEntityAttribute();
056            var entityInstance = entityBooleanAttribute.getEntityInstance();
057            var totalCount = coreControl.countEntityBooleanAttributeHistory(entityAttribute, entityInstance);
058
059            try(var objectLimiter = new ObjectLimiter(env, EntityBooleanAttributeConstants.COMPONENT_VENDOR_NAME, EntityBooleanAttributeConstants.ENTITY_TYPE_NAME, totalCount)) {
060                var entities = coreControl.getEntityBooleanAttributeHistory(entityAttribute, entityInstance);
061                var entityObjects = new ArrayList<EntityBooleanAttributeHistoryObject>(entities.size());
062
063                for(var entity : entities) {
064                    var entityObject = new EntityBooleanAttributeHistoryObject(entity);
065
066                    entityObjects.add(entityObject);
067                }
068
069                return new CountedObjects<>(objectLimiter, entityObjects);
070            }
071        } else {
072            return Connections.emptyConnection();
073        }
074    }
075
076}