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.util.BaseGraphQl;
021import com.echothree.model.data.core.server.entity.EntityAttribute;
022import com.echothree.model.data.core.server.entity.EntityInstance;
023import com.echothree.util.server.persistence.Session;
024import graphql.annotations.annotationTypes.GraphQLDescription;
025import graphql.annotations.annotationTypes.GraphQLField;
026import graphql.annotations.annotationTypes.GraphQLName;
027import graphql.schema.DataFetchingEnvironment;
028import java.util.ArrayList;
029import java.util.Collection;
030import java.util.List;
031
032@GraphQLDescription("entity multiple list item attributes object")
033@GraphQLName("EntityCollectionAttributes")
034public class EntityCollectionAttributesObject
035        implements BaseGraphQl, AttributeInterface {
036
037    private final EntityAttribute entityAttribute; // Always Present
038    private final EntityInstance entityInstance; // Always Present
039
040    public EntityCollectionAttributesObject(final EntityAttribute entityAttribute, final EntityInstance entityInstance) {
041        this.entityAttribute = entityAttribute;
042        this.entityInstance = entityInstance;
043    }
044
045    @GraphQLField
046    @GraphQLDescription("entity attribute")
047    public EntityAttributeObject getEntityAttribute(final DataFetchingEnvironment env) {
048        return CoreSecurityUtils.getHasEntityAttributeAccess(env) ? new EntityAttributeObject(entityAttribute, entityInstance) : null;
049    }
050
051    @GraphQLField
052    @GraphQLDescription("entity instance")
053    public EntityInstanceObject getEntityInstance(final DataFetchingEnvironment env) {
054        return CoreSecurityUtils.getHasEntityInstanceAccess(env) ? new EntityInstanceObject(entityInstance) : null;
055    }
056
057    @GraphQLField
058    @GraphQLDescription("entity instance attributes")
059    public Collection<EntityInstanceObject> getEntityInstanceAttributes(final DataFetchingEnvironment env) {
060        List<EntityInstanceObject> entityCollectionAttributeObjects = null;
061
062        if(CoreSecurityUtils.getHasEntityInstanceAccess(env)) {
063            var coreControl = Session.getModelController(CoreControl.class);
064            var entityCollectionAttributes = coreControl.getEntityCollectionAttributes(entityAttribute, entityInstance);
065            entityCollectionAttributeObjects = new ArrayList<>(entityCollectionAttributes.size());
066
067            for(var entityCollectionAttribute : entityCollectionAttributes) {
068                entityCollectionAttributeObjects.add(new EntityInstanceObject(entityCollectionAttribute.getEntityInstanceAttribute()));
069            }
070        }
071
072        return entityCollectionAttributeObjects;
073    }
074    
075}