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