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.keys.core.CoreForeignKeys.ENTITY_LIST_ITEMS_ACTIVE_DETAIL_FK;
020import static com.echothree.model.jooq.server.keys.core.CoreForeignKeys.ENTITY_LIST_ITEM_ATTRIBUTES_ENTITY_LIST_ITEM_FK;
021import static com.echothree.model.jooq.server.tables.core.EntityListItemAttributes.EntityListItemAttributes;
022import static com.echothree.model.jooq.server.tables.core.EntityListItemDetails.EntityListItemDetails;
023import static com.echothree.model.jooq.server.tables.core.EntityListItems.EntityListItems;
024import static com.echothree.model.jooq.server.tables.search.CachedExecutedSearchResults.CachedExecutedSearchResults;
025import static com.echothree.model.jooq.server.tables.search.SearchResults.SearchResults;
026import static org.jooq.impl.DSL.count;
027import com.echothree.model.data.core.server.entity.EntityAttribute;
028import com.echothree.model.data.search.server.entity.UserVisitSearch;
029import com.echothree.util.server.persistence.Session;
030import java.util.List;
031
032public class EntityListItemAttributeFacetQuery
033        extends BaseFacetQuery<EntityListItemAttributeFacetResult> {
034    
035    public EntityListItemAttributeFacetQuery(UserVisitSearch userVisitSearch) {
036        super(userVisitSearch);
037    }
038    
039    public List<EntityListItemAttributeFacetResult> execute(EntityAttribute entityAttribute) {
040        var query = cachedExecutedSearch == null
041                ? getDslContext()
042                    .select(EntityListItems.ENTITY_LIST_ITEM.as("EntityListItem"),
043                            count().as("Count"))
044                    .from(SearchResults)
045                    .join(EntityListItemAttributes).on(SearchResults.ENTITY_INSTANCE.eq(EntityListItemAttributes.ENTITY_INSTANCE))
046                    .join(EntityListItems).onKey(ENTITY_LIST_ITEM_ATTRIBUTES_ENTITY_LIST_ITEM_FK)
047                    .join(EntityListItemDetails).onKey(ENTITY_LIST_ITEMS_ACTIVE_DETAIL_FK)
048                    .where(SearchResults.SEARCH.eq(search.getPrimaryKey()),
049                            EntityListItemDetails.ENTITY_ATTRIBUTE.eq(entityAttribute.getPrimaryKey()),
050                            EntityListItemAttributes.THRU_TIME.eq(Session.MAX_TIME))
051                    .groupBy(EntityListItems.ENTITY_LIST_ITEM)
052                    .orderBy(count().desc())
053                : getDslContext()
054                    .select(EntityListItems.ENTITY_LIST_ITEM.as("EntityListItem"),
055                            count().as("Count"))
056                    .from(CachedExecutedSearchResults)
057                    .join(EntityListItemAttributes).on(CachedExecutedSearchResults.ENTITY_INSTANCE.eq(EntityListItemAttributes.ENTITY_INSTANCE))
058                    .join(EntityListItems).onKey(ENTITY_LIST_ITEM_ATTRIBUTES_ENTITY_LIST_ITEM_FK)
059                    .join(EntityListItemDetails).onKey(ENTITY_LIST_ITEMS_ACTIVE_DETAIL_FK)
060                    .where(CachedExecutedSearchResults.CACHED_EXECUTED_SEARCH.eq(cachedExecutedSearch.getPrimaryKey()),
061                            EntityListItemDetails.ENTITY_ATTRIBUTE.eq(entityAttribute.getPrimaryKey()),
062                            EntityListItemAttributes.THRU_TIME.eq(Session.MAX_TIME))
063                    .groupBy(EntityListItems.ENTITY_LIST_ITEM)
064                    .orderBy(count().desc());
065
066        return super.execute(query);
067    }
068    
069}