001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.model.data.user.common.pk.UserVisitPK;
027import com.echothree.util.common.command.BaseResult;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.server.control.BaseSimpleCommand;
032import com.echothree.util.server.control.CommandSecurityDefinition;
033import com.echothree.util.server.persistence.Session;
034import java.util.List;
035
036public abstract class BaseGetResultsFacetCommand<F extends BaseGetResultsFacetForm, R extends BaseGetResultsFacetResult>
037        extends BaseSimpleCommand<F> {
038    
039    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
040
041    static {
042        FORM_FIELD_DEFINITIONS = List.of(
043                new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, true, null, null),
044                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, true, null, null)
045        );
046    }
047
048    /** Creates a new instance of BaseGetResultsFacetCommand */
049    protected BaseGetResultsFacetCommand(UserVisitPK userVisitPK, F form, CommandSecurityDefinition COMMAND_SECURITY_DEFINITION) {
050        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
051    }
052    
053    protected BaseResult execute(final String componentVendorName, final String entityTypeName, final String searchKindName,
054            final BaseGetResultsFacetResult result) {
055        var entityType = EntityTypeLogic.getInstance().getEntityTypeByName(this, componentVendorName, entityTypeName);
056        var searchType = SearchLogic.getInstance().getSearchTypeByName(this, searchKindName, form.getSearchTypeName());
057
058        if(!hasExecutionErrors()) {
059            var searchControl = Session.getModelController(SearchControl.class);
060            var userVisitSearch = searchControl.getUserVisitSearch(getUserVisit(), searchType);
061
062            if(userVisitSearch != null) {
063                var entityAttribute = EntityAttributeLogic.getInstance().getEntityAttributeByName(this, entityType, form.getEntityAttributeName());
064
065                if(!hasExecutionErrors()) {
066                    result.setUserVisitSearchFacet(UserVisitSearchFacetLogic.getInstance().getUserVisitSearchFacetTransfer(this, userVisitSearch, entityAttribute));
067                }
068            } else {
069                addExecutionError(ExecutionErrors.UnknownUserVisitSearch.name(), searchType.getLastDetail().getSearchTypeName());
070            }
071        }
072        
073        return result;
074    }
075
076    @Override
077    protected abstract BaseResult execute();
078    
079}