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.BaseGetResultsFacetForm;
020import com.echothree.control.user.search.common.result.BaseGetResultsFacetResult;
021import com.echothree.model.control.core.server.logic.EntityAttributeLogic;
022import com.echothree.model.control.core.server.logic.EntityTypeLogic;
023import com.echothree.model.control.search.server.control.SearchControl;
024import com.echothree.model.control.search.server.logic.SearchLogic;
025import com.echothree.model.control.search.server.logic.UserVisitSearchFacetLogic;
026import com.echothree.util.common.command.BaseResult;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.server.control.BaseSimpleCommand;
031import com.echothree.util.server.control.CommandSecurityDefinition;
032import java.util.List;
033import javax.inject.Inject;
034
035public abstract class BaseGetResultsFacetCommand<F extends BaseGetResultsFacetForm, R extends BaseGetResultsFacetResult>
036        extends BaseSimpleCommand<F> {
037    
038    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
039
040    static {
041        FORM_FIELD_DEFINITIONS = List.of(
042                new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, true, null, null),
043                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, true, null, null)
044        );
045    }
046
047    @Inject
048    SearchControl searchControl;
049
050    @Inject
051    EntityAttributeLogic entityAttributeLogic;
052
053    @Inject
054    EntityTypeLogic entityTypeLogic;
055
056    @Inject
057    SearchLogic searchLogic;
058
059    @Inject
060    UserVisitSearchFacetLogic userVisitSearchFacetLogic;
061
062    /** Creates a new instance of BaseGetResultsFacetCommand */
063    protected BaseGetResultsFacetCommand(CommandSecurityDefinition COMMAND_SECURITY_DEFINITION) {
064        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
065    }
066    
067    protected BaseResult execute(final String componentVendorName, final String entityTypeName, final String searchKindName,
068            final BaseGetResultsFacetResult 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 entityAttribute = entityAttributeLogic.getEntityAttributeByName(this, entityType, form.getEntityAttributeName());
077
078                if(!hasExecutionErrors()) {
079                    result.setUserVisitSearchFacet(userVisitSearchFacetLogic.getUserVisitSearchFacetTransfer(this, userVisitSearch, entityAttribute));
080                }
081            } else {
082                addExecutionError(ExecutionErrors.UnknownUserVisitSearch.name(), searchType.getLastDetail().getSearchTypeName());
083            }
084        }
085        
086        return result;
087    }
088
089    @Override
090    protected abstract BaseResult execute();
091    
092}