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.search.server.database;
018
019import static com.echothree.model.jooq.server.tables.core.EntityLongAttributes.EntityLongAttributes;
020import static com.echothree.model.jooq.server.tables.search.CachedExecutedSearchResults.CachedExecutedSearchResults;
021import static com.echothree.model.jooq.server.tables.search.SearchResults.SearchResults;
022import static org.jooq.impl.DSL.count;
023import com.echothree.model.data.core.server.entity.EntityAttribute;
024import com.echothree.model.data.search.server.entity.UserVisitSearch;
025import com.echothree.util.server.persistence.Session;
026import java.util.List;
027
028public class EntityLongAttributeFacetQuery
029        extends BaseFacetQuery<EntityLongAttributeFacetResult> {
030    
031    public EntityLongAttributeFacetQuery(UserVisitSearch userVisitSearch) {
032        super(userVisitSearch);
033    }
034    
035    public List<EntityLongAttributeFacetResult> execute(EntityAttribute entityAttribute) {
036        var query = cachedExecutedSearch == null
037                ? getDslContext()
038                    .select(EntityLongAttributes.LONG_ATTRIBUTE.as("LongAttribute"),
039                            count().as("Count"))
040                    .from(SearchResults)
041                    .join(EntityLongAttributes).on(SearchResults.ENTITY_INSTANCE.eq(EntityLongAttributes.ENTITY_INSTANCE))
042                    .where(SearchResults.SEARCH.eq(search.getPrimaryKey()),
043                            EntityLongAttributes.ENTITY_ATTRIBUTE.eq(entityAttribute.getPrimaryKey()),
044                            EntityLongAttributes.THRU_TIME.eq(Session.MAX_TIME))
045                    .groupBy(EntityLongAttributes.LONG_ATTRIBUTE)
046                : getDslContext()
047                    .select(EntityLongAttributes.LONG_ATTRIBUTE.as("LongAttribute"),
048                            count().as("Count"))
049                    .from(CachedExecutedSearchResults)
050                    .join(EntityLongAttributes).on(CachedExecutedSearchResults.ENTITY_INSTANCE.eq(EntityLongAttributes.ENTITY_INSTANCE))
051                    .where(CachedExecutedSearchResults.CACHED_EXECUTED_SEARCH.eq(cachedExecutedSearch.getPrimaryKey()),
052                            EntityLongAttributes.ENTITY_ATTRIBUTE.eq(entityAttribute.getPrimaryKey()),
053                            EntityLongAttributes.THRU_TIME.eq(Session.MAX_TIME))
054                    .groupBy(EntityLongAttributes.LONG_ATTRIBUTE);
055
056        return super.execute(query);
057    }
058    
059}