001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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 com.echothree.util.server.persistence.Session;
033import java.util.List;
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    /** Creates a new instance of BaseGetResultsFacetCommand */
048    protected BaseGetResultsFacetCommand(CommandSecurityDefinition COMMAND_SECURITY_DEFINITION) {
049        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
050    }
051    
052    protected BaseResult execute(final String componentVendorName, final String entityTypeName, final String searchKindName,
053            final BaseGetResultsFacetResult result) {
054        var entityType = EntityTypeLogic.getInstance().getEntityTypeByName(this, componentVendorName, entityTypeName);
055        var searchType = SearchLogic.getInstance().getSearchTypeByName(this, searchKindName, form.getSearchTypeName());
056
057        if(!hasExecutionErrors()) {
058            var searchControl = Session.getModelController(SearchControl.class);
059            var userVisitSearch = searchControl.getUserVisitSearch(getUserVisit(), searchType);
060
061            if(userVisitSearch != null) {
062                var entityAttribute = EntityAttributeLogic.getInstance().getEntityAttributeByName(this, entityType, form.getEntityAttributeName());
063
064                if(!hasExecutionErrors()) {
065                    result.setUserVisitSearchFacet(UserVisitSearchFacetLogic.getInstance().getUserVisitSearchFacetTransfer(this, userVisitSearch, entityAttribute));
066                }
067            } else {
068                addExecutionError(ExecutionErrors.UnknownUserVisitSearch.name(), searchType.getLastDetail().getSearchTypeName());
069            }
070        }
071        
072        return result;
073    }
074
075    @Override
076    protected abstract BaseResult execute();
077    
078}