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.database;
018
019import static com.echothree.model.jooq.server.keys.core.CoreForeignKeys.ENTITY_ATTRIBUTES_ACTIVE_DETAIL_FK;
020import static com.echothree.model.jooq.server.keys.core.CoreForeignKeys.ENTITY_TIMES_ENTITY_INSTANCE_FK;
021import static com.echothree.model.jooq.server.tables.core.EntityAttributeDetails.EntityAttributeDetails;
022import static com.echothree.model.jooq.server.tables.core.EntityAttributes.EntityAttributes;
023import static com.echothree.model.jooq.server.tables.core.EntityInstances.EntityInstances;
024import static com.echothree.model.jooq.server.tables.core.EntityTimeAttributes.EntityTimeAttributes;
025import static com.echothree.model.jooq.server.tables.core.EntityTimes.EntityTimes;
026import com.echothree.model.data.core.server.entity.EntityAttribute;
027import com.echothree.util.server.persistence.BaseDatabaseQuery;
028import com.echothree.util.server.persistence.EntityPermission;
029import com.echothree.util.server.persistence.Session;
030import java.util.List;
031
032public class EntityInstancesMissingTimeEntityAttributeQuery
033        extends BaseDatabaseQuery<EntityInstanceResult> {
034
035    public EntityInstancesMissingTimeEntityAttributeQuery() {
036        super(EntityPermission.READ_ONLY);
037    }
038    
039    public List<EntityInstanceResult> execute(final EntityAttribute entityAttribute) {
040        var query = getDslContext()
041                .select(EntityInstances.ENTITY_INSTANCE.as("EntityInstance"))
042                .from(EntityAttributes)
043                .join(EntityAttributeDetails).onKey(ENTITY_ATTRIBUTES_ACTIVE_DETAIL_FK)
044                .join(EntityInstances).on(EntityAttributeDetails.ENTITY_TYPE.eq(EntityInstances.ENTITY_TYPE))
045                .join(EntityTimes).onKey(ENTITY_TIMES_ENTITY_INSTANCE_FK)
046                .where(EntityAttributes.ENTITY_ATTRIBUTE.eq(entityAttribute.getPrimaryKey()),
047                        EntityTimes.DELETED_TIME.isNull())
048                .and(EntityInstances.ENTITY_INSTANCE.notIn(getDslContext()
049                        .select(EntityTimeAttributes.ENTITY_INSTANCE)
050                        .from(EntityTimeAttributes)
051                        .where(EntityTimeAttributes.ENTITY_ATTRIBUTE.eq(entityAttribute.getPrimaryKey()),
052                                EntityTimeAttributes.THRU_TIME.eq(Session.MAX_TIME))));
053
054        return super.execute(query);
055    }
056    
057}