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.control.user.search.server.command;
018
019import com.echothree.control.user.search.common.form.BaseGetResultsFacetsForm;
020import com.echothree.control.user.search.common.result.BaseGetResultsFacetsResult;
021import com.echothree.model.control.core.common.EntityAttributeTypes;
022import com.echothree.model.control.core.server.logic.EntityTypeLogic;
023import com.echothree.model.control.search.common.transfer.UserVisitSearchFacetTransfer;
024import com.echothree.model.control.search.server.control.SearchControl;
025import com.echothree.model.control.search.server.logic.SearchLogic;
026import com.echothree.model.control.search.server.logic.UserVisitSearchFacetLogic;
027import com.echothree.model.data.user.common.pk.UserVisitPK;
028import com.echothree.util.common.command.BaseResult;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
032import com.echothree.util.server.control.BaseSimpleCommand;
033import com.echothree.util.server.control.CommandSecurityDefinition;
034import java.util.LinkedHashMap;
035import java.util.List;
036import javax.inject.Inject;
037
038public abstract class BaseGetResultsFacetsCommand<F extends BaseGetResultsFacetsForm, R extends BaseGetResultsFacetsResult>
039        extends BaseSimpleCommand<F> {
040    
041    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
042
043    static {
044        FORM_FIELD_DEFINITIONS = List.of(
045                new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, true, null, null)
046        );
047    }
048
049    @Inject
050    SearchControl searchControl;
051
052    @Inject
053    EntityTypeLogic entityTypeLogic;
054
055    @Inject
056    SearchLogic searchLogic;
057
058    @Inject
059    UserVisitSearchFacetLogic userVisitSearchFacetLogic;
060
061    /** Creates a new instance of BaseGetResultsFacetsCommand */
062    protected BaseGetResultsFacetsCommand(CommandSecurityDefinition COMMAND_SECURITY_DEFINITION) {
063        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
064    }
065
066    // Substantial portions of this are duplicated in BaseResultsObject.
067    protected BaseResult execute(final String componentVendorName, final String entityTypeName, final String searchKindName,
068            final BaseGetResultsFacetsResult result) {
069        var entityType = entityTypeLogic.getEntityTypeByName(this, componentVendorName, entityTypeName);
070        var searchType = searchLogic.getSearchTypeByName(this, searchKindName, form.getSearchTypeName());
071
072        if(!hasExecutionErrors()) {
073            var userVisitSearch = searchControl.getUserVisitSearch(getUserVisit(), searchType);
074
075            if(userVisitSearch != null) {
076                var entityAttributes = coreControl.getEntityAttributesByEntityType(entityType);
077                var userVisitSearchFacets = new LinkedHashMap<String, UserVisitSearchFacetTransfer>();
078
079                entityAttributes.forEach((entityAttribute) -> {
080                    var entityAttributeTypeName = entityAttribute.getLastDetail().getEntityAttributeType().getEntityAttributeTypeName();
081
082                    if (entityAttributeTypeName.equals(EntityAttributeTypes.LISTITEM.name())
083                            || entityAttributeTypeName.equals(EntityAttributeTypes.MULTIPLELISTITEM.name())
084                            || entityAttributeTypeName.equals(EntityAttributeTypes.INTEGER.name())
085                            || entityAttributeTypeName.equals(EntityAttributeTypes.LONG.name())) {
086                        var userVisitSearchFacet = userVisitSearchFacetLogic.getUserVisitSearchFacetTransfer(this, userVisitSearch, entityAttribute);
087
088                        if ((userVisitSearchFacet.getUserVisitSearchFacetListItems() != null && userVisitSearchFacet.getUserVisitSearchFacetListItems().getSize() > 0)
089                                || (userVisitSearchFacet.getUserVisitSearchFacetIntegers() != null && userVisitSearchFacet.getUserVisitSearchFacetIntegers().getSize() > 0)
090                                || (userVisitSearchFacet.getUserVisitSearchFacetLongs() != null && userVisitSearchFacet.getUserVisitSearchFacetLongs().getSize() > 0)) {
091                            userVisitSearchFacets.put(entityAttribute.getLastDetail().getEntityAttributeName(), userVisitSearchFacet);
092                        }
093                    }
094                });
095
096                if(!hasExecutionErrors()) {
097                    result.setUserVisitSearchFacets(userVisitSearchFacets);
098                }
099            } else {
100                addExecutionError(ExecutionErrors.UnknownUserVisitSearch.name(), searchType.getLastDetail().getSearchTypeName());
101            }
102        }
103        
104        return result;
105    }
106
107    @Override
108    protected abstract BaseResult execute();
109    
110}