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.core.server.control.EntityLockControl; 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.data.core.common.EventConstants; 029import com.echothree.model.data.core.server.entity.EntityInstance; 030import com.echothree.util.server.persistence.EntityDescriptionUtils; 031import com.echothree.util.server.persistence.EntityNamesUtils; 032import com.echothree.util.server.persistence.Session; 033import graphql.annotations.annotationTypes.GraphQLDescription; 034import graphql.annotations.annotationTypes.GraphQLField; 035import graphql.annotations.annotationTypes.GraphQLName; 036import graphql.annotations.annotationTypes.GraphQLNonNull; 037import graphql.annotations.connection.GraphQLConnection; 038import graphql.schema.DataFetchingEnvironment; 039import java.util.ArrayList; 040 041@GraphQLDescription("entity instance object") 042@GraphQLName("EntityInstance") 043public class EntityInstanceObject 044 extends BaseEntityInstanceObject { 045 046 private EntityInstance entityInstance; // Always Present 047 048 public EntityInstanceObject(EntityInstance entityInstance) { 049 super(entityInstance); 050 051 this.entityInstance = entityInstance; 052 } 053 054 @GraphQLField 055 @GraphQLDescription("entity type") 056 @GraphQLNonNull 057 public EntityTypeObject getEntityType() { 058 return new EntityTypeObject(entityInstance.getEntityType()); 059 } 060 061 @GraphQLField 062 @GraphQLDescription("entity unique id") 063 @GraphQLNonNull 064 public Long getEntityUniqueId() { 065 return entityInstance.getEntityUniqueId(); 066 } 067 068 @GraphQLField 069 @GraphQLDescription("key") 070 @GraphQLNonNull 071 public String getKey() { 072 var coreControl = Session.getModelController(CoreControl.class); 073 074 entityInstance = coreControl.ensureKeyForEntityInstance(entityInstance, false); 075 076 return entityInstance.getKey(); 077 } 078 079 @GraphQLField 080 @GraphQLDescription("guid") 081 @GraphQLNonNull 082 public String getGuid() { 083 var coreControl = Session.getModelController(CoreControl.class); 084 085 entityInstance = coreControl.ensureGuidForEntityInstance(entityInstance, false); 086 087 return entityInstance.getGuid(); 088 } 089 090 @GraphQLField 091 @GraphQLDescription("ulid") 092 @GraphQLNonNull 093 public String getUlid() { 094 var coreControl = Session.getModelController(CoreControl.class); 095 096 entityInstance = coreControl.ensureUlidForEntityInstance(entityInstance, false); 097 098 return entityInstance.getUlid(); 099 } 100 101 @GraphQLField 102 @GraphQLDescription("entity ref") 103 @GraphQLNonNull 104 public String getEntityRef() { 105 var entityTypeDetail = entityInstance.getEntityType().getLastDetail(); 106 107 return new StringBuilder(entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName()) 108 .append('.').append(entityTypeDetail.getEntityTypeName()) 109 .append('.').append(entityInstance.getEntityUniqueId()).toString(); 110 } 111 112 @GraphQLField 113 @GraphQLDescription("entity time") 114 public EntityTimeObject getEntityTime() { 115 var coreControl = Session.getModelController(CoreControl.class); 116 var entityTime = coreControl.getEntityTime(entityInstance); 117 118 return entityTime == null ? null : new EntityTimeObject(entityTime); 119 } 120 121 @GraphQLField 122 @GraphQLDescription("entity visit") 123 public EntityVisitObject getEntityVisit(final DataFetchingEnvironment env) { 124 var coreControl = Session.getModelController(CoreControl.class); 125 var userSession = BaseGraphQl.getUserSession(env); 126 var visitingEntityInstance = userSession == null ? null : coreControl.getEntityInstanceByBasePK(userSession.getPartyPK()); 127 var entityVisit = visitingEntityInstance == null ? null : coreControl.getEntityVisit(visitingEntityInstance, entityInstance); 128 129 return entityVisit == null ? null : new EntityVisitObject(entityVisit); 130 } 131 132 @GraphQLField 133 @GraphQLDescription("entity appearance") 134 public EntityAppearanceObject getEntityAppearance() { 135 var coreControl = Session.getModelController(CoreControl.class); 136 var entityAppearance = coreControl.getEntityAppearance(entityInstance); 137 138 return entityAppearance == null ? null : new EntityAppearanceObject(entityAppearance); 139 } 140 141 @GraphQLField 142 @GraphQLDescription("entity lock") 143 public EntityLockObject getEntityLock(final DataFetchingEnvironment env) { 144 var entityLockControl = Session.getModelController(EntityLockControl.class); 145 var userVisit = BaseGraphQl.getUserVisit(env); 146 var entityLockTransfer = entityLockControl.getEntityLockTransferByEntityInstance(userVisit, entityInstance); 147 148 return entityLockTransfer == null ? null : new EntityLockObject(entityLockTransfer); 149 } 150 151 @GraphQLField 152 @GraphQLDescription("entity names") 153 public EntityNamesObject getEntityNames(final DataFetchingEnvironment env) { 154 var entityNamesMapping = EntityNamesUtils.getInstance().getEntityNames(entityInstance); 155 156 return entityNamesMapping == null ? null : new EntityNamesObject(entityNamesMapping.getEntityNames()); 157 } 158 159 @GraphQLField 160 @GraphQLDescription("description") 161 public String getDescription(final DataFetchingEnvironment env) { 162 var userVisit = BaseGraphQl.getUserVisit(env); 163 164 return EntityDescriptionUtils.getInstance().getDescription(userVisit, entityInstance); 165 } 166 167 @GraphQLField 168 @GraphQLDescription("events") 169 @GraphQLNonNull 170 @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class) 171 public CountingPaginatedData<EventObject> getEvents(final DataFetchingEnvironment env) { 172 if(CoreSecurityUtils.getHasEventsAccess(env)) { 173 var coreControl = Session.getModelController(CoreControl.class); 174 var totalCount = coreControl.countEventsByEntityInstance(getEntityInstanceByBasePK()); 175 176 try(var objectLimiter = new ObjectLimiter(env, EventConstants.COMPONENT_VENDOR_NAME, EventConstants.ENTITY_TYPE_NAME, totalCount)) { 177 var entities = coreControl.getEventsByEntityInstance(getEntityInstanceByBasePK()); 178 var events = new ArrayList<EventObject>(entities.size()); 179 180 for(var entity : entities) { 181 var eventobject = new EventObject(entity); 182 183 events.add(eventobject); 184 } 185 186 return new CountedObjects<>(objectLimiter, events); 187 } 188 } else { 189 return Connections.emptyConnection(); 190 } 191 } 192 193}