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.BaseEntityInstanceObject; 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.BaseGraphQl; 026import com.echothree.model.control.graphql.server.util.count.ObjectLimiter; 027import com.echothree.model.control.user.server.control.UserControl; 028import com.echothree.model.data.core.common.EntityAttributeConstants; 029import com.echothree.model.data.core.common.EventConstants; 030import com.echothree.model.data.core.server.entity.EntityAttributeGroup; 031import com.echothree.model.data.core.server.entity.EntityAttributeGroupDetail; 032import com.echothree.model.data.core.server.entity.EntityInstance; 033import com.echothree.util.server.persistence.Session; 034import graphql.annotations.annotationTypes.GraphQLDescription; 035import graphql.annotations.annotationTypes.GraphQLField; 036import graphql.annotations.annotationTypes.GraphQLName; 037import graphql.annotations.annotationTypes.GraphQLNonNull; 038import graphql.annotations.connection.GraphQLConnection; 039import graphql.schema.DataFetchingEnvironment; 040import java.util.ArrayList; 041import java.util.Collection; 042import java.util.List; 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 attribute group name") 071 @GraphQLNonNull 072 public String getEntityAttributeGroupName() { 073 return getEntityAttributeGroupDetail().getEntityAttributeGroupName(); 074 } 075 076 @GraphQLField 077 @GraphQLDescription("is default") 078 @GraphQLNonNull 079 public boolean getIsDefault() { 080 return getEntityAttributeGroupDetail().getIsDefault(); 081 } 082 083 @GraphQLField 084 @GraphQLDescription("sort order") 085 @GraphQLNonNull 086 public int getSortOrder() { 087 return getEntityAttributeGroupDetail().getSortOrder(); 088 } 089 090 @GraphQLField 091 @GraphQLDescription("description") 092 @GraphQLNonNull 093 public String getDescription(final DataFetchingEnvironment env) { 094 var coreControl = Session.getModelController(CoreControl.class); 095 var userControl = Session.getModelController(UserControl.class); 096 097 return coreControl.getBestEntityAttributeGroupDescription(entityAttributeGroup, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env))); 098 } 099 100 101 @GraphQLField 102 @GraphQLDescription("entity attributes") 103 @GraphQLNonNull 104 @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class) 105 public CountingPaginatedData<EntityAttributeObject> getEntityAttributes(final DataFetchingEnvironment env) { 106 if(entityInstance != null) { 107// if(CoreSecurityUtils.getHasEntityAttributesAccess(env)) { 108 var coreControl = Session.getModelController(CoreControl.class); 109 var entityType = entityInstance.getEntityType(); 110 var totalCount = coreControl.countEntityAttributesByEntityAttributeGroupAndEntityType(entityAttributeGroup, 111 entityType); 112 113 try(var objectLimiter = new ObjectLimiter(env, EntityAttributeConstants.COMPONENT_VENDOR_NAME, EntityAttributeConstants.ENTITY_TYPE_NAME, totalCount)) { 114 var entities = coreControl.getEntityAttributesByEntityAttributeGroupAndEntityType(entityAttributeGroup, 115 entityType); 116 var entityAttributes = new ArrayList<EntityAttributeObject>(entities.size()); 117 118 entities.forEach((entity) -> entityAttributes.add(new EntityAttributeObject(entity, entityInstance))); 119 120 return new CountedObjects<>(objectLimiter, entityAttributes); 121 } 122// } else { 123// return Connections.emptyConnection(); 124// } 125 } else { 126 return null; 127 } 128 } 129 130 @GraphQLField 131 @GraphQLDescription("entity attribute entity attribute groups") 132 public Collection<EntityAttributeEntityAttributeGroupObject> getEntityAttributeEntityAttributeGroups(final DataFetchingEnvironment env) { 133 Collection<EntityAttributeEntityAttributeGroupObject> entityAttributeEntityAttributeGroupObjects = null; 134 135 if(CoreSecurityUtils.getHasEntityAttributeEntityAttributeGroupsAccess(env)) { 136 var coreControl = Session.getModelController(CoreControl.class); 137 var entityAttributeEntityAttributeGroups = coreControl.getEntityAttributeEntityAttributeGroupsByEntityAttributeGroup(entityAttributeGroup); 138 139 entityAttributeEntityAttributeGroupObjects = new ArrayList<>(entityAttributeEntityAttributeGroups.size()); 140 141 for(var entityAttributeEntityAttributeGroup : entityAttributeEntityAttributeGroups) { 142 entityAttributeEntityAttributeGroupObjects.add(new EntityAttributeEntityAttributeGroupObject(entityAttributeEntityAttributeGroup)); 143 } 144 } 145 146 return entityAttributeEntityAttributeGroupObjects; 147 } 148 149}